Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Written by Nils Larsch for the OpenSSL project. */ |
| 2 | /* ==================================================================== |
| 3 | * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * 3. All advertising materials mentioning features or use of this |
| 18 | * software must display the following acknowledgment: |
| 19 | * "This product includes software developed by the OpenSSL Project |
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 21 | * |
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 23 | * endorse or promote products derived from this software without |
| 24 | * prior written permission. For written permission, please contact |
| 25 | * licensing@OpenSSL.org. |
| 26 | * |
| 27 | * 5. Products derived from this software may not be called "OpenSSL" |
| 28 | * nor may "OpenSSL" appear in their names without prior written |
| 29 | * permission of the OpenSSL Project. |
| 30 | * |
| 31 | * 6. Redistributions of any form whatsoever must retain the following |
| 32 | * acknowledgment: |
| 33 | * "This product includes software developed by the OpenSSL Project |
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | * ==================================================================== |
| 49 | * |
| 50 | * This product includes cryptographic software written by Eric Young |
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 52 | * Hudson (tjh@cryptsoft.com). */ |
| 53 | |
| 54 | #include <openssl/ec.h> |
| 55 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 56 | #include <limits.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 57 | #include <string.h> |
| 58 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 59 | #include <openssl/bytestring.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 60 | #include <openssl/bn.h> |
| 61 | #include <openssl/err.h> |
| 62 | #include <openssl/mem.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 63 | #include <openssl/nid.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 64 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 65 | #include "../fipsmodule/ec/internal.h" |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 66 | #include "../bytestring/internal.h" |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 67 | #include "../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 68 | |
| 69 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 70 | static const unsigned kParametersTag = |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 71 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 72 | static const unsigned kPublicKeyTag = |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 73 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 74 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 75 | EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) { |
| 76 | CBS ec_private_key, private_key; |
| 77 | uint64_t version; |
| 78 | if (!CBS_get_asn1(cbs, &ec_private_key, CBS_ASN1_SEQUENCE) || |
| 79 | !CBS_get_asn1_uint64(&ec_private_key, &version) || |
| 80 | version != 1 || |
| 81 | !CBS_get_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING)) { |
| 82 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
| 85 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 86 | // Parse the optional parameters field. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 87 | EC_GROUP *inner_group = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 88 | EC_KEY *ret = NULL; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 89 | if (CBS_peek_asn1_tag(&ec_private_key, kParametersTag)) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 90 | // Per SEC 1, as an alternative to omitting it, one is allowed to specify |
| 91 | // this field and put in a NULL to mean inheriting this value. This was |
| 92 | // omitted in a previous version of this logic without problems, so leave it |
| 93 | // unimplemented. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 94 | CBS child; |
| 95 | if (!CBS_get_asn1(&ec_private_key, &child, kParametersTag)) { |
| 96 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 97 | goto err; |
| 98 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 99 | inner_group = EC_KEY_parse_parameters(&child); |
| 100 | if (inner_group == NULL) { |
| 101 | goto err; |
| 102 | } |
| 103 | if (group == NULL) { |
| 104 | group = inner_group; |
| 105 | } else if (EC_GROUP_cmp(group, inner_group, NULL) != 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 106 | // If a group was supplied externally, it must match. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 107 | OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH); |
| 108 | goto err; |
| 109 | } |
| 110 | if (CBS_len(&child) != 0) { |
| 111 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 112 | goto err; |
| 113 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | } |
| 115 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 116 | if (group == NULL) { |
| 117 | OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 118 | goto err; |
| 119 | } |
| 120 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 121 | ret = EC_KEY_new(); |
| 122 | if (ret == NULL || !EC_KEY_set_group(ret, group)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 123 | goto err; |
| 124 | } |
| 125 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 126 | // Although RFC 5915 specifies the length of the key, OpenSSL historically |
| 127 | // got this wrong, so accept any length. See upstream's |
| 128 | // 30cd4ff294252c4b6a4b69cbef6a5b4117705d22. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 129 | ret->priv_key = |
| 130 | BN_bin2bn(CBS_data(&private_key), CBS_len(&private_key), NULL); |
| 131 | ret->pub_key = EC_POINT_new(group); |
| 132 | if (ret->priv_key == NULL || ret->pub_key == NULL) { |
| 133 | goto err; |
| 134 | } |
| 135 | |
| 136 | if (BN_cmp(ret->priv_key, EC_GROUP_get0_order(group)) >= 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 137 | OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER); |
| 138 | goto err; |
| 139 | } |
| 140 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 141 | if (CBS_peek_asn1_tag(&ec_private_key, kPublicKeyTag)) { |
| 142 | CBS child, public_key; |
| 143 | uint8_t padding; |
| 144 | if (!CBS_get_asn1(&ec_private_key, &child, kPublicKeyTag) || |
| 145 | !CBS_get_asn1(&child, &public_key, CBS_ASN1_BITSTRING) || |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 146 | // As in a SubjectPublicKeyInfo, the byte-encoded public key is then |
| 147 | // encoded as a BIT STRING with bits ordered as in the DER encoding. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 148 | !CBS_get_u8(&public_key, &padding) || |
| 149 | padding != 0 || |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 150 | // Explicitly check |public_key| is non-empty to save the conversion |
| 151 | // form later. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 152 | CBS_len(&public_key) == 0 || |
| 153 | !EC_POINT_oct2point(group, ret->pub_key, CBS_data(&public_key), |
| 154 | CBS_len(&public_key), NULL) || |
| 155 | CBS_len(&child) != 0) { |
| 156 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | goto err; |
| 158 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 159 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 160 | // Save the point conversion form. |
| 161 | // TODO(davidben): Consider removing this. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 162 | ret->conv_form = |
| 163 | (point_conversion_form_t)(CBS_data(&public_key)[0] & ~0x01); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 164 | } else { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 165 | // Compute the public key instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 166 | if (!EC_POINT_mul(group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 167 | goto err; |
| 168 | } |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 169 | // Remember the original private-key-only encoding. |
| 170 | // TODO(davidben): Consider removing this. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 171 | ret->enc_flag |= EC_PKEY_NO_PUBKEY; |
| 172 | } |
| 173 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 174 | if (CBS_len(&ec_private_key) != 0) { |
| 175 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 176 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 177 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 178 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 179 | // Ensure the resulting key is valid. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 180 | if (!EC_KEY_check_key(ret)) { |
| 181 | goto err; |
| 182 | } |
| 183 | |
| 184 | EC_GROUP_free(inner_group); |
| 185 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 186 | |
| 187 | err: |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 188 | EC_KEY_free(ret); |
| 189 | EC_GROUP_free(inner_group); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key, |
| 194 | unsigned enc_flags) { |
| 195 | if (key == NULL || key->group == NULL || key->priv_key == NULL) { |
| 196 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 197 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 198 | } |
| 199 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 200 | CBB ec_private_key, private_key; |
| 201 | if (!CBB_add_asn1(cbb, &ec_private_key, CBS_ASN1_SEQUENCE) || |
| 202 | !CBB_add_asn1_uint64(&ec_private_key, 1 /* version */) || |
| 203 | !CBB_add_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING) || |
| 204 | !BN_bn2cbb_padded(&private_key, |
| 205 | BN_num_bytes(EC_GROUP_get0_order(key->group)), |
| 206 | key->priv_key)) { |
| 207 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 208 | return 0; |
| 209 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 211 | if (!(enc_flags & EC_PKEY_NO_PARAMETERS)) { |
| 212 | CBB child; |
| 213 | if (!CBB_add_asn1(&ec_private_key, &child, kParametersTag) || |
| 214 | !EC_KEY_marshal_curve_name(&child, key->group) || |
| 215 | !CBB_flush(&ec_private_key)) { |
| 216 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 217 | return 0; |
| 218 | } |
| 219 | } |
| 220 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 221 | // TODO(fork): replace this flexibility with sensible default? |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 222 | if (!(enc_flags & EC_PKEY_NO_PUBKEY) && key->pub_key != NULL) { |
| 223 | CBB child, public_key; |
| 224 | if (!CBB_add_asn1(&ec_private_key, &child, kPublicKeyTag) || |
| 225 | !CBB_add_asn1(&child, &public_key, CBS_ASN1_BITSTRING) || |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 226 | // As in a SubjectPublicKeyInfo, the byte-encoded public key is then |
| 227 | // encoded as a BIT STRING with bits ordered as in the DER encoding. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 228 | !CBB_add_u8(&public_key, 0 /* padding */) || |
| 229 | !EC_POINT_point2cbb(&public_key, key->group, key->pub_key, |
| 230 | key->conv_form, NULL) || |
| 231 | !CBB_flush(&ec_private_key)) { |
| 232 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 233 | return 0; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (!CBB_flush(cbb)) { |
| 238 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | return 1; |
| 243 | } |
| 244 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 245 | // is_unsigned_integer returns one if |cbs| is a valid unsigned DER INTEGER and |
| 246 | // zero otherwise. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 247 | static int is_unsigned_integer(const CBS *cbs) { |
| 248 | if (CBS_len(cbs) == 0) { |
| 249 | return 0; |
| 250 | } |
| 251 | uint8_t byte = CBS_data(cbs)[0]; |
| 252 | if ((byte & 0x80) || |
| 253 | (byte == 0 && CBS_len(cbs) > 1 && (CBS_data(cbs)[1] & 0x80) == 0)) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 254 | // Negative or not minimally-encoded. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | return 1; |
| 258 | } |
| 259 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 260 | // kPrimeFieldOID is the encoding of 1.2.840.10045.1.1. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 261 | static const uint8_t kPrimeField[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01}; |
| 262 | |
| 263 | static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a, |
| 264 | CBS *out_b, CBS *out_base_x, |
| 265 | CBS *out_base_y, CBS *out_order) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 266 | // See RFC 3279, section 2.3.5. Note that RFC 3279 calls this structure an |
| 267 | // ECParameters while RFC 5480 calls it a SpecifiedECDomain. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 268 | CBS params, field_id, field_type, curve, base; |
| 269 | uint64_t version; |
| 270 | if (!CBS_get_asn1(in, ¶ms, CBS_ASN1_SEQUENCE) || |
| 271 | !CBS_get_asn1_uint64(¶ms, &version) || |
| 272 | version != 1 || |
| 273 | !CBS_get_asn1(¶ms, &field_id, CBS_ASN1_SEQUENCE) || |
| 274 | !CBS_get_asn1(&field_id, &field_type, CBS_ASN1_OBJECT) || |
| 275 | CBS_len(&field_type) != sizeof(kPrimeField) || |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 276 | OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) != 0 || |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 277 | !CBS_get_asn1(&field_id, out_prime, CBS_ASN1_INTEGER) || |
| 278 | !is_unsigned_integer(out_prime) || |
| 279 | CBS_len(&field_id) != 0 || |
| 280 | !CBS_get_asn1(¶ms, &curve, CBS_ASN1_SEQUENCE) || |
| 281 | !CBS_get_asn1(&curve, out_a, CBS_ASN1_OCTETSTRING) || |
| 282 | !CBS_get_asn1(&curve, out_b, CBS_ASN1_OCTETSTRING) || |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 283 | // |curve| has an optional BIT STRING seed which we ignore. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 284 | !CBS_get_asn1(¶ms, &base, CBS_ASN1_OCTETSTRING) || |
| 285 | !CBS_get_asn1(¶ms, out_order, CBS_ASN1_INTEGER) || |
| 286 | !is_unsigned_integer(out_order)) { |
| 287 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 288 | return 0; |
| 289 | } |
| 290 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 291 | // |params| has an optional cofactor which we ignore. With the optional seed |
| 292 | // in |curve|, a group already has arbitrarily many encodings. Parse enough to |
| 293 | // uniquely determine the curve. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 294 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 295 | // Require that the base point use uncompressed form. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 296 | uint8_t form; |
| 297 | if (!CBS_get_u8(&base, &form) || form != POINT_CONVERSION_UNCOMPRESSED) { |
| 298 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | if (CBS_len(&base) % 2 != 0) { |
| 303 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 304 | return 0; |
| 305 | } |
| 306 | size_t field_len = CBS_len(&base) / 2; |
| 307 | CBS_init(out_base_x, CBS_data(&base), field_len); |
| 308 | CBS_init(out_base_y, CBS_data(&base) + field_len, field_len); |
| 309 | |
| 310 | return 1; |
| 311 | } |
| 312 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 313 | // integers_equal returns one if |a| and |b| are equal, up to leading zeros, and |
| 314 | // zero otherwise. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 315 | static int integers_equal(const CBS *a, const uint8_t *b, size_t b_len) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 316 | // Remove leading zeros from |a| and |b|. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 317 | CBS a_copy = *a; |
| 318 | while (CBS_len(&a_copy) > 0 && CBS_data(&a_copy)[0] == 0) { |
| 319 | CBS_skip(&a_copy, 1); |
| 320 | } |
| 321 | while (b_len > 0 && b[0] == 0) { |
| 322 | b++; |
| 323 | b_len--; |
| 324 | } |
| 325 | return CBS_mem_equal(&a_copy, b, b_len); |
| 326 | } |
| 327 | |
| 328 | EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) { |
| 329 | CBS named_curve; |
| 330 | if (!CBS_get_asn1(cbs, &named_curve, CBS_ASN1_OBJECT)) { |
| 331 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 335 | // Look for a matching curve. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 336 | const struct built_in_curves *const curves = OPENSSL_built_in_curves(); |
| 337 | for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) { |
| 338 | const struct built_in_curve *curve = &curves->curves[i]; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 339 | if (CBS_len(&named_curve) == curve->oid_len && |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 340 | OPENSSL_memcmp(CBS_data(&named_curve), curve->oid, curve->oid_len) == |
| 341 | 0) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 342 | return EC_GROUP_new_by_curve_name(curve->nid); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group) { |
| 351 | int nid = EC_GROUP_get_curve_name(group); |
| 352 | if (nid == NID_undef) { |
| 353 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 354 | return 0; |
| 355 | } |
| 356 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 357 | const struct built_in_curves *const curves = OPENSSL_built_in_curves(); |
| 358 | for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) { |
| 359 | const struct built_in_curve *curve = &curves->curves[i]; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 360 | if (curve->nid == nid) { |
| 361 | CBB child; |
| 362 | return CBB_add_asn1(cbb, &child, CBS_ASN1_OBJECT) && |
| 363 | CBB_add_bytes(&child, curve->oid, curve->oid_len) && |
| 364 | CBB_flush(cbb); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) { |
| 373 | if (!CBS_peek_asn1_tag(cbs, CBS_ASN1_SEQUENCE)) { |
| 374 | return EC_KEY_parse_curve_name(cbs); |
| 375 | } |
| 376 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 377 | // OpenSSL sometimes produces ECPrivateKeys with explicitly-encoded versions |
| 378 | // of named curves. |
| 379 | // |
| 380 | // TODO(davidben): Remove support for this. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 381 | CBS prime, a, b, base_x, base_y, order; |
| 382 | if (!parse_explicit_prime_curve(cbs, &prime, &a, &b, &base_x, &base_y, |
| 383 | &order)) { |
| 384 | return NULL; |
| 385 | } |
| 386 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 387 | // Look for a matching prime curve. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 388 | const struct built_in_curves *const curves = OPENSSL_built_in_curves(); |
| 389 | for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) { |
| 390 | const struct built_in_curve *curve = &curves->curves[i]; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 391 | const unsigned param_len = curve->param_len; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 392 | // |curve->params| is ordered p, a, b, x, y, order, each component |
| 393 | // zero-padded up to the field length. Although SEC 1 states that the |
| 394 | // Field-Element-to-Octet-String conversion also pads, OpenSSL mis-encodes |
| 395 | // |a| and |b|, so this comparison must allow omitting leading zeros. (This |
| 396 | // is relevant for P-521 whose |b| has a leading 0.) |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 397 | if (integers_equal(&prime, curve->params, param_len) && |
| 398 | integers_equal(&a, curve->params + param_len, param_len) && |
| 399 | integers_equal(&b, curve->params + param_len * 2, param_len) && |
| 400 | integers_equal(&base_x, curve->params + param_len * 3, param_len) && |
| 401 | integers_equal(&base_y, curve->params + param_len * 4, param_len) && |
| 402 | integers_equal(&order, curve->params + param_len * 5, param_len)) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 403 | return EC_GROUP_new_by_curve_name(curve->nid); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 411 | int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point, |
| 412 | point_conversion_form_t form, BN_CTX *ctx) { |
| 413 | size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); |
| 414 | if (len == 0) { |
| 415 | return 0; |
| 416 | } |
| 417 | uint8_t *p; |
| 418 | return CBB_add_space(out, &p, len) && |
| 419 | EC_POINT_point2oct(group, point, form, p, len, ctx) == len; |
| 420 | } |
| 421 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 422 | EC_KEY *d2i_ECPrivateKey(EC_KEY **out, const uint8_t **inp, long len) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 423 | // This function treats its |out| parameter differently from other |d2i| |
| 424 | // functions. If supplied, take the group from |*out|. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 425 | const EC_GROUP *group = NULL; |
| 426 | if (out != NULL && *out != NULL) { |
| 427 | group = EC_KEY_get0_group(*out); |
| 428 | } |
| 429 | |
| 430 | if (len < 0) { |
| 431 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 432 | return NULL; |
| 433 | } |
| 434 | CBS cbs; |
| 435 | CBS_init(&cbs, *inp, (size_t)len); |
| 436 | EC_KEY *ret = EC_KEY_parse_private_key(&cbs, group); |
| 437 | if (ret == NULL) { |
| 438 | return NULL; |
| 439 | } |
| 440 | if (out != NULL) { |
| 441 | EC_KEY_free(*out); |
| 442 | *out = ret; |
| 443 | } |
| 444 | *inp = CBS_data(&cbs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 449 | CBB cbb; |
| 450 | if (!CBB_init(&cbb, 0) || |
| 451 | !EC_KEY_marshal_private_key(&cbb, key, EC_KEY_get_enc_flags(key))) { |
| 452 | CBB_cleanup(&cbb); |
| 453 | return -1; |
| 454 | } |
| 455 | return CBB_finish_i2d(&cbb, outp); |
| 456 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 457 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 458 | EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len) { |
| 459 | if (len < 0) { |
| 460 | return NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 461 | } |
| 462 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 463 | CBS cbs; |
| 464 | CBS_init(&cbs, *inp, (size_t)len); |
| 465 | EC_GROUP *group = EC_KEY_parse_parameters(&cbs); |
| 466 | if (group == NULL) { |
| 467 | return NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 468 | } |
| 469 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 470 | EC_KEY *ret = EC_KEY_new(); |
| 471 | if (ret == NULL || !EC_KEY_set_group(ret, group)) { |
| 472 | EC_GROUP_free(group); |
| 473 | EC_KEY_free(ret); |
| 474 | return NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 475 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 476 | EC_GROUP_free(group); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 477 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 478 | if (out_key != NULL) { |
| 479 | EC_KEY_free(*out_key); |
| 480 | *out_key = ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 481 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 482 | *inp = CBS_data(&cbs); |
| 483 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | int i2d_ECParameters(const EC_KEY *key, uint8_t **outp) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 487 | if (key == NULL || key->group == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 488 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 489 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 490 | } |
| 491 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 492 | CBB cbb; |
| 493 | if (!CBB_init(&cbb, 0) || |
| 494 | !EC_KEY_marshal_curve_name(&cbb, key->group)) { |
| 495 | CBB_cleanup(&cbb); |
| 496 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 497 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 498 | return CBB_finish_i2d(&cbb, outp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) { |
| 502 | EC_KEY *ret = NULL; |
| 503 | |
| 504 | if (keyp == NULL || *keyp == NULL || (*keyp)->group == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 505 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 506 | return NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 507 | } |
| 508 | ret = *keyp; |
| 509 | if (ret->pub_key == NULL && |
| 510 | (ret->pub_key = EC_POINT_new(ret->group)) == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 511 | OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 512 | return NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 513 | } |
| 514 | if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 515 | OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 516 | return NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 517 | } |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 518 | // save the point conversion form |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 519 | ret->conv_form = (point_conversion_form_t)(*inp[0] & ~0x01); |
| 520 | *inp += len; |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) { |
| 525 | size_t buf_len = 0; |
| 526 | int new_buffer = 0; |
| 527 | |
| 528 | if (key == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 529 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | buf_len = EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, NULL, |
| 534 | 0, NULL); |
| 535 | |
| 536 | if (outp == NULL || buf_len == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 537 | // out == NULL => just return the length of the octet string |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 538 | return buf_len; |
| 539 | } |
| 540 | |
| 541 | if (*outp == NULL) { |
| 542 | *outp = OPENSSL_malloc(buf_len); |
| 543 | if (*outp == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 544 | OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | new_buffer = 1; |
| 548 | } |
| 549 | if (!EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, *outp, |
| 550 | buf_len, NULL)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 551 | OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 552 | if (new_buffer) { |
| 553 | OPENSSL_free(*outp); |
| 554 | *outp = NULL; |
| 555 | } |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | if (!new_buffer) { |
| 560 | *outp += buf_len; |
| 561 | } |
| 562 | return buf_len; |
| 563 | } |