David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/bn.h> |
| 16 | |
| 17 | #include <openssl/bytestring.h> |
| 18 | #include <openssl/err.h> |
| 19 | |
| 20 | |
David Benjamin | acb2451 | 2015-12-22 15:02:01 -0500 | [diff] [blame] | 21 | int BN_parse_asn1_unsigned(CBS *cbs, BIGNUM *ret) { |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 22 | CBS child; |
| 23 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_INTEGER) || |
| 24 | CBS_len(&child) == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 25 | OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING); |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 26 | return 0; |
| 27 | } |
David Benjamin | 231cb82 | 2015-09-15 16:47:35 -0400 | [diff] [blame] | 28 | |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 29 | if (CBS_data(&child)[0] & 0x80) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 30 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 31 | return 0; |
| 32 | } |
David Benjamin | 231cb82 | 2015-09-15 16:47:35 -0400 | [diff] [blame] | 33 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 34 | // INTEGERs must be minimal. |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 35 | if (CBS_data(&child)[0] == 0x00 && |
| 36 | CBS_len(&child) > 1 && |
| 37 | !(CBS_data(&child)[1] & 0x80)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 38 | OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING); |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 39 | return 0; |
| 40 | } |
David Benjamin | 231cb82 | 2015-09-15 16:47:35 -0400 | [diff] [blame] | 41 | |
| 42 | return BN_bin2bn(CBS_data(&child), CBS_len(&child), ret) != NULL; |
| 43 | } |
| 44 | |
David Benjamin | acb2451 | 2015-12-22 15:02:01 -0500 | [diff] [blame] | 45 | int BN_parse_asn1_unsigned_buggy(CBS *cbs, BIGNUM *ret) { |
David Benjamin | 231cb82 | 2015-09-15 16:47:35 -0400 | [diff] [blame] | 46 | CBS child; |
| 47 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_INTEGER) || |
| 48 | CBS_len(&child) == 0) { |
| 49 | OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING); |
| 50 | return 0; |
| 51 | } |
| 52 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 53 | // This function intentionally does not reject negative numbers or non-minimal |
| 54 | // encodings. Estonian IDs issued between September 2014 to September 2015 are |
| 55 | // broken. See https://crbug.com/532048 and https://crbug.com/534766. |
| 56 | // |
| 57 | // TODO(davidben): Remove this code and callers in March 2016. |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 58 | return BN_bin2bn(CBS_data(&child), CBS_len(&child), ret) != NULL; |
| 59 | } |
| 60 | |
David Benjamin | acb2451 | 2015-12-22 15:02:01 -0500 | [diff] [blame] | 61 | int BN_marshal_asn1(CBB *cbb, const BIGNUM *bn) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 62 | // Negative numbers are unsupported. |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 63 | if (BN_is_negative(bn)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 64 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | CBB child; |
David Benjamin | acb2451 | 2015-12-22 15:02:01 -0500 | [diff] [blame] | 69 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER) || |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 70 | // The number must be padded with a leading zero if the high bit would |
| 71 | // otherwise be set or if |bn| is zero. |
David Benjamin | acb2451 | 2015-12-22 15:02:01 -0500 | [diff] [blame] | 72 | (BN_num_bits(bn) % 8 == 0 && !CBB_add_u8(&child, 0x00)) || |
| 73 | !BN_bn2cbb_padded(&child, BN_num_bytes(bn), bn) || |
| 74 | !CBB_flush(cbb)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 75 | OPENSSL_PUT_ERROR(BN, BN_R_ENCODE_ERROR); |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
David Benjamin | b9c579d | 2015-06-11 22:52:07 -0400 | [diff] [blame] | 79 | return 1; |
| 80 | } |