| 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 <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/bytestring.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/mem.h> |
| 24 |
| 25 | #include "../internal.h" |
| 26 |
| 27 |
| 28 | ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x) { |
| 29 | return ASN1_STRING_dup(x); |
| 30 | } |
| 31 |
| 32 | int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) { |
| 33 | // Compare signs. |
| 34 | int neg = x->type & V_ASN1_NEG; |
| 35 | if (neg != (y->type & V_ASN1_NEG)) { |
| 36 | return neg ? -1 : 1; |
| 37 | } |
| 38 |
| 39 | int ret = ASN1_STRING_cmp(x, y); |
| 40 | if (neg) { |
| 41 | // This could be |-ret|, but |ASN1_STRING_cmp| is not forbidden from |
| 42 | // returning |INT_MIN|. |
| 43 | if (ret < 0) { |
| 44 | return 1; |
| 45 | } else if (ret > 0) { |
| 46 | return -1; |
| 47 | } else { |
| 48 | return 0; |
| 49 | } |
| 50 | } |
| 51 |
| 52 | return ret; |
| 53 | } |
| 54 |
| 55 | // negate_twos_complement negates |len| bytes from |buf| in-place, interpreted |
| 56 | // as a signed, big-endian two's complement value. |
| 57 | static void negate_twos_complement(uint8_t *buf, size_t len) { |
| 58 | uint8_t borrow = 0; |
| 59 | for (size_t i = len - 1; i < len; i--) { |
| 60 | uint8_t t = buf[i]; |
| 61 | buf[i] = 0u - borrow - t; |
| 62 | borrow |= t != 0; |
| 63 | } |
| 64 | } |
| 65 |
| 66 | static int is_all_zeros(const uint8_t *in, size_t len) { |
| 67 | for (size_t i = 0; i < len; i++) { |
| 68 | if (in[i] != 0) { |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | return 1; |
| 73 | } |
| 74 |
| 75 | int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) { |
| 76 | if (in == NULL) { |
| 77 | return 0; |
| 78 | } |
| 79 |
| 80 | // |ASN1_INTEGER|s should be represented minimally, but it is possible to |
| 81 | // construct invalid ones. Skip leading zeros so this does not produce an |
| 82 | // invalid encoding or break invariants. |
| 83 | CBS cbs; |
| 84 | CBS_init(&cbs, in->data, in->length); |
| 85 | while (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0) { |
| 86 | CBS_skip(&cbs, 1); |
| 87 | } |
| 88 |
| 89 | int is_negative = (in->type & V_ASN1_NEG) != 0; |
| 90 | size_t pad; |
| 91 | CBS copy = cbs; |
| 92 | uint8_t msb; |
| 93 | if (!CBS_get_u8(©, &msb)) { |
| 94 | // Zero is represented as a single byte. |
| 95 | is_negative = 0; |
| 96 | pad = 1; |
| 97 | } else if (is_negative) { |
| 98 | // 0x80...01 through 0xff...ff have a two's complement of 0x7f...ff |
| 99 | // through 0x00...01 and need an extra byte to be negative. |
| 100 | // 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff |
| 101 | // through 0x80...00 and can be negated as-is. |
| 102 | pad = msb > 0x80 || |
| 103 | (msb == 0x80 && !is_all_zeros(CBS_data(©), CBS_len(©))); |
| 104 | } else { |
| 105 | // If the high bit is set, the signed representation needs an extra |
| 106 | // byte to be positive. |
| 107 | pad = (msb & 0x80) != 0; |
| 108 | } |
| 109 |
| 110 | if (CBS_len(&cbs) > INT_MAX - pad) { |
| 111 | OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW); |
| 112 | return 0; |
| 113 | } |
| 114 | int len = (int)(pad + CBS_len(&cbs)); |
| 115 | assert(len > 0); |
| 116 | if (outp == NULL) { |
| 117 | return len; |
| 118 | } |
| 119 |
| 120 | if (pad) { |
| 121 | (*outp)[0] = 0; |
| 122 | } |
| 123 | OPENSSL_memcpy(*outp + pad, CBS_data(&cbs), CBS_len(&cbs)); |
| 124 | if (is_negative) { |
| 125 | negate_twos_complement(*outp, len); |
| 126 | assert((*outp)[0] >= 0x80); |
| 127 | } else { |
| 128 | assert((*outp)[0] < 0x80); |
| 129 | } |
| 130 | *outp += len; |
| 131 | return len; |
| 132 | } |
| 133 |
| 134 | ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **out, const unsigned char **inp, |
| 135 | long len) { |
| 136 | // This function can handle lengths up to INT_MAX - 1, but the rest of the |
| 137 | // legacy ASN.1 code mixes integer types, so avoid exposing it to |
| 138 | // ASN1_INTEGERS with larger lengths. |
| 139 | if (len < 0 || len > INT_MAX / 2) { |
| 140 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); |
| 141 | return NULL; |
| 142 | } |
| 143 |
| 144 | CBS cbs; |
| 145 | CBS_init(&cbs, *inp, (size_t)len); |
| 146 | int is_negative; |
| 147 | if (!CBS_is_valid_asn1_integer(&cbs, &is_negative)) { |
| 148 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER); |
| 149 | return NULL; |
| 150 | } |
| 151 |
| 152 | ASN1_INTEGER *ret = NULL; |
| 153 | if (out == NULL || *out == NULL) { |
| 154 | ret = ASN1_INTEGER_new(); |
| 155 | if (ret == NULL) { |
| 156 | return NULL; |
| 157 | } |
| 158 | } else { |
| 159 | ret = *out; |
| 160 | } |
| 161 |
| 162 | // Convert to |ASN1_INTEGER|'s sign-and-magnitude representation. First, |
| 163 | // determine the size needed for a minimal result. |
| 164 | if (is_negative) { |
| 165 | // 0xff00...01 through 0xff7f..ff have a two's complement of 0x00ff...ff |
| 166 | // through 0x000100...001 and need one leading zero removed. 0x8000...00 |
| 167 | // through 0xff00...00 have a two's complement of 0x8000...00 through |
| 168 | // 0x0100...00 and will be minimally-encoded as-is. |
| 169 | if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0xff && |
| 170 | !is_all_zeros(CBS_data(&cbs) + 1, CBS_len(&cbs) - 1)) { |
| 171 | CBS_skip(&cbs, 1); |
| 172 | } |
| 173 | } else { |
| 174 | // Remove the leading zero byte, if any. |
| 175 | if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0x00) { |
| 176 | CBS_skip(&cbs, 1); |
| 177 | } |
| 178 | } |
| 179 |
| 180 | if (!ASN1_STRING_set(ret, CBS_data(&cbs), CBS_len(&cbs))) { |
| 181 | goto err; |
| 182 | } |
| 183 |
| 184 | if (is_negative) { |
| 185 | ret->type = V_ASN1_NEG_INTEGER; |
| 186 | negate_twos_complement(ret->data, ret->length); |
| 187 | } else { |
| 188 | ret->type = V_ASN1_INTEGER; |
| 189 | } |
| 190 |
| 191 | // The value should be minimally-encoded. |
| 192 | assert(ret->length == 0 || ret->data[0] != 0); |
| 193 | // Zero is not negative. |
| 194 | assert(!is_negative || ret->length > 0); |
| 195 |
| 196 | *inp += len; |
| 197 | if (out != NULL) { |
| 198 | *out = ret; |
| 199 | } |
| 200 | return ret; |
| 201 |
| 202 | err: |
| 203 | if (ret != NULL && (out == NULL || *out != ret)) { |
| 204 | ASN1_INTEGER_free(ret); |
| 205 | } |
| 206 | return NULL; |
| 207 | } |
| 208 |
| 209 | int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t v) { |
| 210 | if (v >= 0) { |
| 211 | return ASN1_INTEGER_set_uint64(a, (uint64_t)v); |
| 212 | } |
| 213 |
| 214 | if (!ASN1_INTEGER_set_uint64(a, 0 - (uint64_t)v)) { |
| 215 | return 0; |
| 216 | } |
| 217 |
| 218 | a->type = V_ASN1_NEG_INTEGER; |
| 219 | return 1; |
| 220 | } |
| 221 |
| 222 | int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t v) { |
| 223 | if (v >= 0) { |
| 224 | return ASN1_ENUMERATED_set_uint64(a, (uint64_t)v); |
| 225 | } |
| 226 |
| 227 | if (!ASN1_ENUMERATED_set_uint64(a, 0 - (uint64_t)v)) { |
| 228 | return 0; |
| 229 | } |
| 230 |
| 231 | a->type = V_ASN1_NEG_ENUMERATED; |
| 232 | return 1; |
| 233 | } |
| 234 |
| 235 | int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) { |
| 236 | static_assert(sizeof(long) <= sizeof(int64_t), "long fits in int64_t"); |
| 237 | return ASN1_INTEGER_set_int64(a, v); |
| 238 | } |
| 239 |
| 240 | int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) { |
| 241 | static_assert(sizeof(long) <= sizeof(int64_t), "long fits in int64_t"); |
| 242 | return ASN1_ENUMERATED_set_int64(a, v); |
| 243 | } |
| 244 |
| 245 | static int asn1_string_set_uint64(ASN1_STRING *out, uint64_t v, int type) { |
| 246 | uint8_t buf[sizeof(uint64_t)]; |
| 247 | CRYPTO_store_u64_be(buf, v); |
| 248 | size_t leading_zeros; |
| 249 | for (leading_zeros = 0; leading_zeros < sizeof(buf); leading_zeros++) { |
| 250 | if (buf[leading_zeros] != 0) { |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 |
| 255 | if (!ASN1_STRING_set(out, buf + leading_zeros, sizeof(buf) - leading_zeros)) { |
| 256 | return 0; |
| 257 | } |
| 258 | out->type = type; |
| 259 | return 1; |
| 260 | } |
| 261 |
| 262 | int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v) { |
| 263 | return asn1_string_set_uint64(out, v, V_ASN1_INTEGER); |
| 264 | } |
| 265 |
| 266 | int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v) { |
| 267 | return asn1_string_set_uint64(out, v, V_ASN1_ENUMERATED); |
| 268 | } |
| 269 |
| 270 | static int asn1_string_get_abs_uint64(uint64_t *out, const ASN1_STRING *a, |
| 271 | int type) { |
| 272 | if ((a->type & ~V_ASN1_NEG) != type) { |
| 273 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE); |
| 274 | return 0; |
| 275 | } |
| 276 | uint8_t buf[sizeof(uint64_t)] = {0}; |
| 277 | if (a->length > (int)sizeof(buf)) { |
| 278 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER); |
| 279 | return 0; |
| 280 | } |
| 281 | OPENSSL_memcpy(buf + sizeof(buf) - a->length, a->data, a->length); |
| 282 | *out = CRYPTO_load_u64_be(buf); |
| 283 | return 1; |
| 284 | } |
| 285 |
| 286 | static int asn1_string_get_uint64(uint64_t *out, const ASN1_STRING *a, |
| 287 | int type) { |
| 288 | if (!asn1_string_get_abs_uint64(out, a, type)) { |
| 289 | return 0; |
| 290 | } |
| 291 | if (a->type & V_ASN1_NEG) { |
| 292 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER); |
| 293 | return 0; |
| 294 | } |
| 295 | return 1; |
| 296 | } |
| 297 |
| 298 | int ASN1_INTEGER_get_uint64(uint64_t *out, const ASN1_INTEGER *a) { |
| 299 | return asn1_string_get_uint64(out, a, V_ASN1_INTEGER); |
| 300 | } |
| 301 |
| 302 | int ASN1_ENUMERATED_get_uint64(uint64_t *out, const ASN1_ENUMERATED *a) { |
| 303 | return asn1_string_get_uint64(out, a, V_ASN1_ENUMERATED); |
| 304 | } |
| 305 |
| 306 | static int asn1_string_get_int64(int64_t *out, const ASN1_STRING *a, int type) { |
| 307 | uint64_t v; |
| 308 | if (!asn1_string_get_abs_uint64(&v, a, type)) { |
| 309 | return 0; |
| 310 | } |
| 311 | int64_t i64; |
| 312 | int fits_in_i64; |
| 313 | // Check |v != 0| to handle manually-constructed negative zeros. |
| 314 | if ((a->type & V_ASN1_NEG) && v != 0) { |
| 315 | i64 = (int64_t)(0u - v); |
| 316 | fits_in_i64 = i64 < 0; |
| 317 | } else { |
| 318 | i64 = (int64_t)v; |
| 319 | fits_in_i64 = i64 >= 0; |
| 320 | } |
| 321 | if (!fits_in_i64) { |
| 322 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER); |
| 323 | return 0; |
| 324 | } |
| 325 | *out = i64; |
| 326 | return 1; |
| 327 | } |
| 328 |
| 329 | int ASN1_INTEGER_get_int64(int64_t *out, const ASN1_INTEGER *a) { |
| 330 | return asn1_string_get_int64(out, a, V_ASN1_INTEGER); |
| 331 | } |
| 332 |
| 333 | int ASN1_ENUMERATED_get_int64(int64_t *out, const ASN1_ENUMERATED *a) { |
| 334 | return asn1_string_get_int64(out, a, V_ASN1_ENUMERATED); |
| 335 | } |
| 336 |
| 337 | static long asn1_string_get_long(const ASN1_STRING *a, int type) { |
| 338 | if (a == NULL) { |
| 339 | return 0; |
| 340 | } |
| 341 |
| 342 | int64_t v; |
| 343 | if (!asn1_string_get_int64(&v, a, type) || // |
| 344 | v < LONG_MIN || v > LONG_MAX) { |
| 345 | // This function's return value does not distinguish overflow from -1. |
| 346 | ERR_clear_error(); |
| 347 | return -1; |
| 348 | } |
| 349 |
| 350 | return (long)v; |
| 351 | } |
| 352 |
| 353 | long ASN1_INTEGER_get(const ASN1_INTEGER *a) { |
| 354 | return asn1_string_get_long(a, V_ASN1_INTEGER); |
| 355 | } |
| 356 |
| 357 | long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a) { |
| 358 | return asn1_string_get_long(a, V_ASN1_ENUMERATED); |
| 359 | } |
| 360 |
| 361 | static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai, |
| 362 | int type) { |
| 363 | ASN1_INTEGER *ret; |
| 364 | if (ai == NULL) { |
| 365 | ret = ASN1_STRING_type_new(type); |
| 366 | } else { |
| 367 | ret = ai; |
| 368 | } |
| 369 | int len; |
| 370 | if (ret == NULL) { |
| 371 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 372 | goto err; |
| 373 | } |
| 374 |
| 375 | if (BN_is_negative(bn) && !BN_is_zero(bn)) { |
| 376 | ret->type = type | V_ASN1_NEG; |
| 377 | } else { |
| 378 | ret->type = type; |
| 379 | } |
| 380 |
| 381 | len = BN_num_bytes(bn); |
| 382 | if (!ASN1_STRING_set(ret, NULL, len) || |
| 383 | !BN_bn2bin_padded(ret->data, len, bn)) { |
| 384 | goto err; |
| 385 | } |
| 386 | return ret; |
| 387 |
| 388 | err: |
| 389 | if (ret != ai) { |
| 390 | ASN1_STRING_free(ret); |
| 391 | } |
| 392 | return NULL; |
| 393 | } |
| 394 |
| 395 | ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) { |
| 396 | return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER); |
| 397 | } |
| 398 |
| 399 | ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) { |
| 400 | return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED); |
| 401 | } |
| 402 |
| 403 | static BIGNUM *asn1_string_to_bn(const ASN1_STRING *ai, BIGNUM *bn, int type) { |
| 404 | if ((ai->type & ~V_ASN1_NEG) != type) { |
| 405 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE); |
| 406 | return NULL; |
| 407 | } |
| 408 |
| 409 | BIGNUM *ret; |
| 410 | if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) { |
| 411 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB); |
| 412 | } else if (ai->type & V_ASN1_NEG) { |
| 413 | BN_set_negative(ret, 1); |
| 414 | } |
| 415 | return ret; |
| 416 | } |
| 417 |
| 418 | BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) { |
| 419 | return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER); |
| 420 | } |
| 421 |
| 422 | BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn) { |
| 423 | return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED); |
| 424 | } |
| 425 |