| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/asn1.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 |
| 23 | #include "../internal.h" |
| 24 | #include "internal.h" |
| 25 |
| 26 |
| 27 | int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, |
| 28 | ossl_ssize_t len) { |
| 29 | return ASN1_STRING_set(x, d, len); |
| 30 | } |
| 31 |
| 32 | int asn1_bit_string_length(const ASN1_BIT_STRING *str, |
| 33 | uint8_t *out_padding_bits) { |
| 34 | int len = str->length; |
| 35 | if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) { |
| 36 | // If the string is already empty, it cannot have padding bits. |
| 37 | *out_padding_bits = len == 0 ? 0 : str->flags & 0x07; |
| 38 | return len; |
| 39 | } |
| 40 |
| 41 | // TODO(https://crbug.com/boringssl/447): If we move this logic to |
| 42 | // |ASN1_BIT_STRING_set_bit|, can we remove this representation? |
| 43 | while (len > 0 && str->data[len - 1] == 0) { |
| 44 | len--; |
| 45 | } |
| 46 | uint8_t padding_bits = 0; |
| 47 | if (len > 0) { |
| 48 | uint8_t last = str->data[len - 1]; |
| 49 | assert(last != 0); |
| 50 | for (; padding_bits < 7; padding_bits++) { |
| 51 | if (last & (1 << padding_bits)) { |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | *out_padding_bits = padding_bits; |
| 57 | return len; |
| 58 | } |
| 59 |
| 60 | int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out) { |
| 61 | uint8_t padding_bits; |
| 62 | int len = asn1_bit_string_length(str, &padding_bits); |
| 63 | if (padding_bits != 0) { |
| 64 | return 0; |
| 65 | } |
| 66 | *out = len; |
| 67 | return 1; |
| 68 | } |
| 69 |
| 70 | int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp) { |
| 71 | if (a == NULL) { |
| 72 | return 0; |
| 73 | } |
| 74 |
| 75 | uint8_t bits; |
| 76 | int len = asn1_bit_string_length(a, &bits); |
| 77 | if (len > INT_MAX - 1) { |
| 78 | OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW); |
| 79 | return 0; |
| 80 | } |
| 81 | int ret = 1 + len; |
| 82 | if (pp == NULL) { |
| 83 | return ret; |
| 84 | } |
| 85 |
| 86 | uint8_t *p = *pp; |
| 87 | *(p++) = bits; |
| 88 | OPENSSL_memcpy(p, a->data, len); |
| 89 | if (len > 0) { |
| 90 | p[len - 1] &= (0xff << bits); |
| 91 | } |
| 92 | p += len; |
| 93 | *pp = p; |
| 94 | return ret; |
| 95 | } |
| 96 |
| 97 | ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, |
| 98 | const unsigned char **pp, long len) { |
| 99 | ASN1_BIT_STRING *ret = NULL; |
| 100 | const unsigned char *p; |
| 101 | unsigned char *s; |
| 102 | int padding; |
| 103 | uint8_t padding_mask; |
| 104 |
| 105 | if (len < 1) { |
| 106 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT); |
| 107 | goto err; |
| 108 | } |
| 109 |
| 110 | if (len > INT_MAX) { |
| 111 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG); |
| 112 | goto err; |
| 113 | } |
| 114 |
| 115 | if ((a == NULL) || ((*a) == NULL)) { |
| 116 | if ((ret = ASN1_BIT_STRING_new()) == NULL) { |
| 117 | return NULL; |
| 118 | } |
| 119 | } else { |
| 120 | ret = (*a); |
| 121 | } |
| 122 |
| 123 | p = *pp; |
| 124 | padding = *(p++); |
| 125 | len--; |
| 126 | if (padding > 7) { |
| 127 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT); |
| 128 | goto err; |
| 129 | } |
| 130 |
| 131 | // Unused bits in a BIT STRING must be zero. |
| 132 | padding_mask = (1 << padding) - 1; |
| 133 | if (padding != 0 && (len < 1 || (p[len - 1] & padding_mask) != 0)) { |
| 134 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_PADDING); |
| 135 | goto err; |
| 136 | } |
| 137 |
| 138 | // We do this to preserve the settings. If we modify the settings, via |
| 139 | // the _set_bit function, we will recalculate on output |
| 140 | ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); // clear |
| 141 | ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | padding); // set |
| 142 |
| 143 | if (len > 0) { |
| 144 | s = reinterpret_cast<uint8_t *>(OPENSSL_memdup(p, len)); |
| 145 | if (s == NULL) { |
| 146 | goto err; |
| 147 | } |
| 148 | p += len; |
| 149 | } else { |
| 150 | s = NULL; |
| 151 | } |
| 152 |
| 153 | ret->length = (int)len; |
| 154 | OPENSSL_free(ret->data); |
| 155 | ret->data = s; |
| 156 | ret->type = V_ASN1_BIT_STRING; |
| 157 | if (a != NULL) { |
| 158 | (*a) = ret; |
| 159 | } |
| 160 | *pp = p; |
| 161 | return ret; |
| 162 | err: |
| 163 | if ((ret != NULL) && ((a == NULL) || (*a != ret))) { |
| 164 | ASN1_BIT_STRING_free(ret); |
| 165 | } |
| 166 | return NULL; |
| 167 | } |
| 168 |
| 169 | // These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de> |
| 170 | int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) { |
| 171 | int w, v, iv; |
| 172 | unsigned char *c; |
| 173 |
| 174 | w = n / 8; |
| 175 | v = 1 << (7 - (n & 0x07)); |
| 176 | iv = ~v; |
| 177 | if (!value) { |
| 178 | v = 0; |
| 179 | } |
| 180 |
| 181 | if (a == NULL) { |
| 182 | return 0; |
| 183 | } |
| 184 |
| 185 | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); // clear, set on write |
| 186 |
| 187 | if ((a->length < (w + 1)) || (a->data == NULL)) { |
| 188 | if (!value) { |
| 189 | return 1; // Don't need to set |
| 190 | } |
| 191 | if (a->data == NULL) { |
| 192 | c = (unsigned char *)OPENSSL_malloc(w + 1); |
| 193 | } else { |
| 194 | c = (unsigned char *)OPENSSL_realloc(a->data, w + 1); |
| 195 | } |
| 196 | if (c == NULL) { |
| 197 | return 0; |
| 198 | } |
| 199 | if (w + 1 - a->length > 0) { |
| 200 | OPENSSL_memset(c + a->length, 0, w + 1 - a->length); |
| 201 | } |
| 202 | a->data = c; |
| 203 | a->length = w + 1; |
| 204 | } |
| 205 | a->data[w] = ((a->data[w]) & iv) | v; |
| 206 | while ((a->length > 0) && (a->data[a->length - 1] == 0)) { |
| 207 | a->length--; |
| 208 | } |
| 209 | return 1; |
| 210 | } |
| 211 |
| 212 | int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) { |
| 213 | int w, v; |
| 214 |
| 215 | w = n / 8; |
| 216 | v = 1 << (7 - (n & 0x07)); |
| 217 | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) { |
| 218 | return 0; |
| 219 | } |
| 220 | return ((a->data[w] & v) != 0); |
| 221 | } |
| 222 |
| 223 | // Checks if the given bit string contains only bits specified by |
| 224 | // the flags vector. Returns 0 if there is at least one bit set in 'a' |
| 225 | // which is not specified in 'flags', 1 otherwise. |
| 226 | // 'len' is the length of 'flags'. |
| 227 | int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags, |
| 228 | int flags_len) { |
| 229 | int i, ok; |
| 230 | // Check if there is one bit set at all. |
| 231 | if (!a || !a->data) { |
| 232 | return 1; |
| 233 | } |
| 234 |
| 235 | // Check each byte of the internal representation of the bit string. |
| 236 | ok = 1; |
| 237 | for (i = 0; i < a->length && ok; ++i) { |
| 238 | unsigned char mask = i < flags_len ? ~flags[i] : 0xff; |
| 239 | // We are done if there is an unneeded bit set. |
| 240 | ok = (a->data[i] & mask) == 0; |
| 241 | } |
| 242 | return ok; |
| 243 | } |
| 244 |