Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
| 2 | * project 2006. |
| 3 | */ |
| 4 | /* ==================================================================== |
| 5 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * |
| 19 | * 3. All advertising materials mentioning features or use of this |
| 20 | * software must display the following acknowledgment: |
| 21 | * "This product includes software developed by the OpenSSL Project |
| 22 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 23 | * |
| 24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 25 | * endorse or promote products derived from this software without |
| 26 | * prior written permission. For written permission, please contact |
| 27 | * licensing@OpenSSL.org. |
| 28 | * |
| 29 | * 5. Products derived from this software may not be called "OpenSSL" |
| 30 | * nor may "OpenSSL" appear in their names without prior written |
| 31 | * permission of the OpenSSL Project. |
| 32 | * |
| 33 | * 6. Redistributions of any form whatsoever must retain the following |
| 34 | * acknowledgment: |
| 35 | * "This product includes software developed by the OpenSSL Project |
| 36 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 37 | * |
| 38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 49 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 50 | * ==================================================================== |
| 51 | * |
| 52 | * This product includes cryptographic software written by Eric Young |
| 53 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 54 | * Hudson (tjh@cryptsoft.com). */ |
| 55 | |
| 56 | #include <openssl/evp.h> |
| 57 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 58 | #include <openssl/bn.h> |
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/ec.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 61 | #include <openssl/ec_key.h> |
| 62 | #include <openssl/ecdsa.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | #include <openssl/err.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 64 | |
| 65 | #include "internal.h" |
| 66 | |
| 67 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 68 | static int eckey_pub_encode(CBB *out, const EVP_PKEY *key) { |
| 69 | const EC_KEY *ec_key = key->pkey.ec; |
| 70 | const EC_GROUP *group = EC_KEY_get0_group(ec_key); |
| 71 | const EC_POINT *public_key = EC_KEY_get0_public_key(ec_key); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 72 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 73 | // See RFC 5480, section 2. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 74 | CBB spki, algorithm, oid, key_bitstring; |
| 75 | if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) || |
| 76 | !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || |
| 77 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
| 78 | !CBB_add_bytes(&oid, ec_asn1_meth.oid, ec_asn1_meth.oid_len) || |
| 79 | !EC_KEY_marshal_curve_name(&algorithm, group) || |
| 80 | !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) || |
| 81 | !CBB_add_u8(&key_bitstring, 0 /* padding */) || |
| 82 | !EC_POINT_point2cbb(&key_bitstring, group, public_key, |
| 83 | POINT_CONVERSION_UNCOMPRESSED, NULL) || |
| 84 | !CBB_flush(out)) { |
| 85 | OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 92 | static int eckey_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 93 | // See RFC 5480, section 2. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 94 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 95 | // The parameters are a named curve. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 96 | EC_POINT *point = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 97 | EC_KEY *eckey = NULL; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 98 | EC_GROUP *group = EC_KEY_parse_curve_name(params); |
| 99 | if (group == NULL || CBS_len(params) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 100 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 101 | goto err; |
| 102 | } |
| 103 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 104 | eckey = EC_KEY_new(); |
| 105 | if (eckey == NULL || !EC_KEY_set_group(eckey, group)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 106 | goto err; |
| 107 | } |
| 108 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 109 | point = EC_POINT_new(group); |
| 110 | if (point == NULL || |
| 111 | !EC_POINT_oct2point(group, point, CBS_data(key), CBS_len(key), NULL) || |
| 112 | !EC_KEY_set_public_key(eckey, point)) { |
| 113 | goto err; |
| 114 | } |
| 115 | |
| 116 | EC_GROUP_free(group); |
| 117 | EC_POINT_free(point); |
| 118 | EVP_PKEY_assign_EC_KEY(out, eckey); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 119 | return 1; |
| 120 | |
| 121 | err: |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 122 | EC_GROUP_free(group); |
| 123 | EC_POINT_free(point); |
| 124 | EC_KEY_free(eckey); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) { |
| 129 | int r; |
| 130 | const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); |
| 131 | const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), |
| 132 | *pb = EC_KEY_get0_public_key(b->pkey.ec); |
| 133 | r = EC_POINT_cmp(group, pa, pb, NULL); |
| 134 | if (r == 0) { |
| 135 | return 1; |
| 136 | } else if (r == 1) { |
| 137 | return 0; |
| 138 | } else { |
| 139 | return -2; |
| 140 | } |
| 141 | } |
| 142 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 143 | static int eckey_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 144 | // See RFC 5915. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 145 | EC_GROUP *group = EC_KEY_parse_parameters(params); |
| 146 | if (group == NULL || CBS_len(params) != 0) { |
| 147 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 148 | EC_GROUP_free(group); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 149 | return 0; |
| 150 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 152 | EC_KEY *ec_key = EC_KEY_parse_private_key(key, group); |
| 153 | EC_GROUP_free(group); |
| 154 | if (ec_key == NULL || CBS_len(key) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 155 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 156 | EC_KEY_free(ec_key); |
| 157 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | } |
| 159 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 160 | EVP_PKEY_assign_EC_KEY(out, ec_key); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 161 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | } |
| 163 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 164 | static int eckey_priv_encode(CBB *out, const EVP_PKEY *key) { |
| 165 | const EC_KEY *ec_key = key->pkey.ec; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 166 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 167 | // Omit the redundant copy of the curve name. This contradicts RFC 5915 but |
| 168 | // aligns with PKCS #11. SEC 1 only says they may be omitted if known by other |
| 169 | // means. Both OpenSSL and NSS omit the redundant parameters, so we omit them |
| 170 | // as well. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 171 | unsigned enc_flags = EC_KEY_get_enc_flags(ec_key) | EC_PKEY_NO_PARAMETERS; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 172 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 173 | // See RFC 5915. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 174 | CBB pkcs8, algorithm, oid, private_key; |
| 175 | if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) || |
| 176 | !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) || |
| 177 | !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) || |
| 178 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
| 179 | !CBB_add_bytes(&oid, ec_asn1_meth.oid, ec_asn1_meth.oid_len) || |
| 180 | !EC_KEY_marshal_curve_name(&algorithm, EC_KEY_get0_group(ec_key)) || |
| 181 | !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) || |
| 182 | !EC_KEY_marshal_private_key(&private_key, ec_key, enc_flags) || |
| 183 | !CBB_flush(out)) { |
| 184 | OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | static int int_ec_size(const EVP_PKEY *pkey) { |
| 192 | return ECDSA_size(pkey->pkey.ec); |
| 193 | } |
| 194 | |
| 195 | static int ec_bits(const EVP_PKEY *pkey) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 196 | const EC_GROUP *group = EC_KEY_get0_group(pkey->pkey.ec); |
| 197 | if (group == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 198 | ERR_clear_error(); |
| 199 | return 0; |
| 200 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 201 | return BN_num_bits(EC_GROUP_get0_order(group)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int ec_missing_parameters(const EVP_PKEY *pkey) { |
| 205 | return EC_KEY_get0_group(pkey->pkey.ec) == NULL; |
| 206 | } |
| 207 | |
| 208 | static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) { |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 209 | return EC_KEY_set_group(to->pkey.ec, EC_KEY_get0_group(from->pkey.ec)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) { |
| 213 | const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), |
| 214 | *group_b = EC_KEY_get0_group(b->pkey.ec); |
Adam Langley | ef93571 | 2015-02-25 11:16:32 -0800 | [diff] [blame] | 215 | if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 216 | // mismatch |
Adam Langley | ef93571 | 2015-02-25 11:16:32 -0800 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void int_ec_free(EVP_PKEY *pkey) { EC_KEY_free(pkey->pkey.ec); } |
| 223 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 224 | static int eckey_opaque(const EVP_PKEY *pkey) { |
| 225 | return EC_KEY_is_opaque(pkey->pkey.ec); |
| 226 | } |
| 227 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | const EVP_PKEY_ASN1_METHOD ec_asn1_meth = { |
| 229 | EVP_PKEY_EC, |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 230 | // 1.2.840.10045.2.1 |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 231 | {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01}, 7, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 232 | |
| 233 | eckey_pub_decode, |
| 234 | eckey_pub_encode, |
| 235 | eckey_pub_cmp, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 236 | |
| 237 | eckey_priv_decode, |
| 238 | eckey_priv_encode, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 239 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 240 | NULL /* set_priv_raw */, |
| 241 | NULL /* set_pub_raw */, |
| 242 | NULL /* get_priv_raw */, |
| 243 | NULL /* get_pub_raw */, |
| 244 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 245 | eckey_opaque, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 246 | |
| 247 | int_ec_size, |
| 248 | ec_bits, |
| 249 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 250 | ec_missing_parameters, |
| 251 | ec_copy_parameters, |
| 252 | ec_cmp_parameters, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 253 | |
| 254 | int_ec_free, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | }; |