/src/openssl30/crypto/ec/ec_asn1.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2002-2022 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | /* | 
| 11 |  |  * ECDSA low level APIs are deprecated for public use, but still ok for | 
| 12 |  |  * internal use. | 
| 13 |  |  */ | 
| 14 |  | #include "internal/deprecated.h" | 
| 15 |  |  | 
| 16 |  | #include <string.h> | 
| 17 |  | #include "ec_local.h" | 
| 18 |  | #include <openssl/err.h> | 
| 19 |  | #include <openssl/asn1t.h> | 
| 20 |  | #include <openssl/objects.h> | 
| 21 |  | #include "internal/nelem.h" | 
| 22 |  | #include "crypto/asn1_dsa.h" | 
| 23 |  |  | 
| 24 |  | #ifndef FIPS_MODULE | 
| 25 |  |  | 
| 26 |  | /* some structures needed for the asn1 encoding */ | 
| 27 |  | typedef struct x9_62_pentanomial_st { | 
| 28 |  |     int32_t k1; | 
| 29 |  |     int32_t k2; | 
| 30 |  |     int32_t k3; | 
| 31 |  | } X9_62_PENTANOMIAL; | 
| 32 |  |  | 
| 33 |  | typedef struct x9_62_characteristic_two_st { | 
| 34 |  |     int32_t m; | 
| 35 |  |     ASN1_OBJECT *type; | 
| 36 |  |     union { | 
| 37 |  |         char *ptr; | 
| 38 |  |         /* NID_X9_62_onBasis */ | 
| 39 |  |         ASN1_NULL *onBasis; | 
| 40 |  |         /* NID_X9_62_tpBasis */ | 
| 41 |  |         ASN1_INTEGER *tpBasis; | 
| 42 |  |         /* NID_X9_62_ppBasis */ | 
| 43 |  |         X9_62_PENTANOMIAL *ppBasis; | 
| 44 |  |         /* anything else */ | 
| 45 |  |         ASN1_TYPE *other; | 
| 46 |  |     } p; | 
| 47 |  | } X9_62_CHARACTERISTIC_TWO; | 
| 48 |  |  | 
| 49 |  | typedef struct x9_62_fieldid_st { | 
| 50 |  |     ASN1_OBJECT *fieldType; | 
| 51 |  |     union { | 
| 52 |  |         char *ptr; | 
| 53 |  |         /* NID_X9_62_prime_field */ | 
| 54 |  |         ASN1_INTEGER *prime; | 
| 55 |  |         /* NID_X9_62_characteristic_two_field */ | 
| 56 |  |         X9_62_CHARACTERISTIC_TWO *char_two; | 
| 57 |  |         /* anything else */ | 
| 58 |  |         ASN1_TYPE *other; | 
| 59 |  |     } p; | 
| 60 |  | } X9_62_FIELDID; | 
| 61 |  |  | 
| 62 |  | typedef struct x9_62_curve_st { | 
| 63 |  |     ASN1_OCTET_STRING *a; | 
| 64 |  |     ASN1_OCTET_STRING *b; | 
| 65 |  |     ASN1_BIT_STRING *seed; | 
| 66 |  | } X9_62_CURVE; | 
| 67 |  |  | 
| 68 |  | struct ec_parameters_st { | 
| 69 |  |     int32_t version; | 
| 70 |  |     X9_62_FIELDID *fieldID; | 
| 71 |  |     X9_62_CURVE *curve; | 
| 72 |  |     ASN1_OCTET_STRING *base; | 
| 73 |  |     ASN1_INTEGER *order; | 
| 74 |  |     ASN1_INTEGER *cofactor; | 
| 75 |  | } /* ECPARAMETERS */ ; | 
| 76 |  |  | 
| 77 |  | typedef enum { | 
| 78 |  |     ECPKPARAMETERS_TYPE_NAMED = 0, | 
| 79 |  |     ECPKPARAMETERS_TYPE_EXPLICIT, | 
| 80 |  |     ECPKPARAMETERS_TYPE_IMPLICIT | 
| 81 |  | } ecpk_parameters_type_t; | 
| 82 |  |  | 
| 83 |  | struct ecpk_parameters_st { | 
| 84 |  |     int type; | 
| 85 |  |     union { | 
| 86 |  |         ASN1_OBJECT *named_curve; | 
| 87 |  |         ECPARAMETERS *parameters; | 
| 88 |  |         ASN1_NULL *implicitlyCA; | 
| 89 |  |     } value; | 
| 90 |  | } /* ECPKPARAMETERS */ ; | 
| 91 |  |  | 
| 92 |  | /* SEC1 ECPrivateKey */ | 
| 93 |  | typedef struct ec_privatekey_st { | 
| 94 |  |     int32_t version; | 
| 95 |  |     ASN1_OCTET_STRING *privateKey; | 
| 96 |  |     ECPKPARAMETERS *parameters; | 
| 97 |  |     ASN1_BIT_STRING *publicKey; | 
| 98 |  | } EC_PRIVATEKEY; | 
| 99 |  |  | 
| 100 |  | /* the OpenSSL ASN.1 definitions */ | 
| 101 |  | ASN1_SEQUENCE(X9_62_PENTANOMIAL) = { | 
| 102 |  |         ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32), | 
| 103 |  |         ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32), | 
| 104 |  |         ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32) | 
| 105 |  | } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL) | 
| 106 |  |  | 
| 107 |  | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) | 
| 108 |  | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) | 
| 109 |  |  | 
| 110 |  | ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY); | 
| 111 |  |  | 
| 112 |  | ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = { | 
| 113 |  |         ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)), | 
| 114 |  |         ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)), | 
| 115 |  |         ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL)) | 
| 116 |  | } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL); | 
| 117 |  |  | 
| 118 |  | ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = { | 
| 119 |  |         ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32), | 
| 120 |  |         ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT), | 
| 121 |  |         ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO) | 
| 122 |  | } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO) | 
| 123 |  |  | 
| 124 |  | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) | 
| 125 |  | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) | 
| 126 |  |  | 
| 127 |  | ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY); | 
| 128 |  |  | 
| 129 |  | ASN1_ADB(X9_62_FIELDID) = { | 
| 130 |  |         ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)), | 
| 131 |  |         ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO)) | 
| 132 |  | } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL); | 
| 133 |  |  | 
| 134 |  | ASN1_SEQUENCE(X9_62_FIELDID) = { | 
| 135 |  |         ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT), | 
| 136 |  |         ASN1_ADB_OBJECT(X9_62_FIELDID) | 
| 137 |  | } static_ASN1_SEQUENCE_END(X9_62_FIELDID) | 
| 138 |  |  | 
| 139 |  | ASN1_SEQUENCE(X9_62_CURVE) = { | 
| 140 |  |         ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING), | 
| 141 |  |         ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING), | 
| 142 |  |         ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING) | 
| 143 |  | } static_ASN1_SEQUENCE_END(X9_62_CURVE) | 
| 144 |  |  | 
| 145 |  | ASN1_SEQUENCE(ECPARAMETERS) = { | 
| 146 |  |         ASN1_EMBED(ECPARAMETERS, version, INT32), | 
| 147 |  |         ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID), | 
| 148 |  |         ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE), | 
| 149 |  |         ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING), | 
| 150 |  |         ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER), | 
| 151 |  |         ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER) | 
| 152 |  | } ASN1_SEQUENCE_END(ECPARAMETERS) | 
| 153 |  |  | 
| 154 |  | DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) | 
| 155 |  | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) | 
| 156 |  |  | 
| 157 |  | ASN1_CHOICE(ECPKPARAMETERS) = { | 
| 158 |  |         ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT), | 
| 159 |  |         ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS), | 
| 160 |  |         ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL) | 
| 161 |  | } ASN1_CHOICE_END(ECPKPARAMETERS) | 
| 162 |  |  | 
| 163 |  | DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS) | 
| 164 |  | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS) | 
| 165 |  | IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS) | 
| 166 |  |  | 
| 167 |  | ASN1_SEQUENCE(EC_PRIVATEKEY) = { | 
| 168 |  |         ASN1_EMBED(EC_PRIVATEKEY, version, INT32), | 
| 169 |  |         ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), | 
| 170 |  |         ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), | 
| 171 |  |         ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) | 
| 172 |  | } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY) | 
| 173 |  |  | 
| 174 |  | DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY) | 
| 175 |  | DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY) | 
| 176 |  | IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY) | 
| 177 |  |  | 
| 178 |  | /* some declarations of internal function */ | 
| 179 |  |  | 
| 180 |  | /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */ | 
| 181 |  | static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *); | 
| 182 |  | /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */ | 
| 183 |  | static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *); | 
| 184 |  |  | 
| 185 |  | /* the function definitions */ | 
| 186 |  |  | 
| 187 |  | static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) | 
| 188 | 533 | { | 
| 189 | 533 |     int ok = 0, nid; | 
| 190 | 533 |     BIGNUM *tmp = NULL; | 
| 191 |  |  | 
| 192 | 533 |     if (group == NULL || field == NULL) | 
| 193 | 0 |         return 0; | 
| 194 |  |  | 
| 195 |  |     /* clear the old values (if necessary) */ | 
| 196 | 533 |     ASN1_OBJECT_free(field->fieldType); | 
| 197 | 533 |     ASN1_TYPE_free(field->p.other); | 
| 198 |  |  | 
| 199 | 533 |     nid = EC_GROUP_get_field_type(group); | 
| 200 |  |     /* set OID for the field */ | 
| 201 | 533 |     if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { | 
| 202 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); | 
| 203 | 0 |         goto err; | 
| 204 | 0 |     } | 
| 205 |  |  | 
| 206 | 533 |     if (nid == NID_X9_62_prime_field) { | 
| 207 | 533 |         if ((tmp = BN_new()) == NULL) { | 
| 208 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 209 | 0 |             goto err; | 
| 210 | 0 |         } | 
| 211 |  |         /* the parameters are specified by the prime number p */ | 
| 212 | 533 |         if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) { | 
| 213 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 214 | 0 |             goto err; | 
| 215 | 0 |         } | 
| 216 |  |         /* set the prime number */ | 
| 217 | 533 |         field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); | 
| 218 | 533 |         if (field->p.prime == NULL) { | 
| 219 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 220 | 0 |             goto err; | 
| 221 | 0 |         } | 
| 222 | 533 |     } else if (nid == NID_X9_62_characteristic_two_field) | 
| 223 |  | #ifdef OPENSSL_NO_EC2M | 
| 224 |  |     { | 
| 225 |  |         ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); | 
| 226 |  |         goto err; | 
| 227 |  |     } | 
| 228 |  | #else | 
| 229 | 0 |     { | 
| 230 | 0 |         int field_type; | 
| 231 | 0 |         X9_62_CHARACTERISTIC_TWO *char_two; | 
| 232 |  | 
 | 
| 233 | 0 |         field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); | 
| 234 | 0 |         char_two = field->p.char_two; | 
| 235 |  | 
 | 
| 236 | 0 |         if (char_two == NULL) { | 
| 237 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 238 | 0 |             goto err; | 
| 239 | 0 |         } | 
| 240 |  |  | 
| 241 | 0 |         char_two->m = (long)EC_GROUP_get_degree(group); | 
| 242 |  | 
 | 
| 243 | 0 |         field_type = EC_GROUP_get_basis_type(group); | 
| 244 |  | 
 | 
| 245 | 0 |         if (field_type == 0) { | 
| 246 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 247 | 0 |             goto err; | 
| 248 | 0 |         } | 
| 249 |  |         /* set base type OID */ | 
| 250 | 0 |         if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { | 
| 251 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); | 
| 252 | 0 |             goto err; | 
| 253 | 0 |         } | 
| 254 |  |  | 
| 255 | 0 |         if (field_type == NID_X9_62_tpBasis) { | 
| 256 | 0 |             unsigned int k; | 
| 257 |  | 
 | 
| 258 | 0 |             if (!EC_GROUP_get_trinomial_basis(group, &k)) | 
| 259 | 0 |                 goto err; | 
| 260 |  |  | 
| 261 | 0 |             char_two->p.tpBasis = ASN1_INTEGER_new(); | 
| 262 | 0 |             if (char_two->p.tpBasis == NULL) { | 
| 263 | 0 |                 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 264 | 0 |                 goto err; | 
| 265 | 0 |             } | 
| 266 | 0 |             if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) { | 
| 267 | 0 |                 ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 268 | 0 |                 goto err; | 
| 269 | 0 |             } | 
| 270 | 0 |         } else if (field_type == NID_X9_62_ppBasis) { | 
| 271 | 0 |             unsigned int k1, k2, k3; | 
| 272 |  | 
 | 
| 273 | 0 |             if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) | 
| 274 | 0 |                 goto err; | 
| 275 |  |  | 
| 276 | 0 |             char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); | 
| 277 | 0 |             if (char_two->p.ppBasis == NULL) { | 
| 278 | 0 |                 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 279 | 0 |                 goto err; | 
| 280 | 0 |             } | 
| 281 |  |  | 
| 282 |  |             /* set k? values */ | 
| 283 | 0 |             char_two->p.ppBasis->k1 = (long)k1; | 
| 284 | 0 |             char_two->p.ppBasis->k2 = (long)k2; | 
| 285 | 0 |             char_two->p.ppBasis->k3 = (long)k3; | 
| 286 | 0 |         } else {                /* field_type == NID_X9_62_onBasis */ | 
| 287 |  |  | 
| 288 |  |             /* for ONB the parameters are (asn1) NULL */ | 
| 289 | 0 |             char_two->p.onBasis = ASN1_NULL_new(); | 
| 290 | 0 |             if (char_two->p.onBasis == NULL) { | 
| 291 | 0 |                 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 292 | 0 |                 goto err; | 
| 293 | 0 |             } | 
| 294 | 0 |         } | 
| 295 | 0 |     } | 
| 296 | 0 | #endif | 
| 297 | 0 |     else { | 
| 298 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD); | 
| 299 | 0 |         goto err; | 
| 300 | 0 |     } | 
| 301 |  |  | 
| 302 | 533 |     ok = 1; | 
| 303 |  |  | 
| 304 | 533 |  err: | 
| 305 | 533 |     BN_free(tmp); | 
| 306 | 533 |     return ok; | 
| 307 | 533 | } | 
| 308 |  |  | 
| 309 |  | static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve) | 
| 310 | 533 | { | 
| 311 | 533 |     int ok = 0; | 
| 312 | 533 |     BIGNUM *tmp_1 = NULL, *tmp_2 = NULL; | 
| 313 | 533 |     unsigned char *a_buf = NULL, *b_buf = NULL; | 
| 314 | 533 |     size_t len; | 
| 315 |  |  | 
| 316 | 533 |     if (!group || !curve || !curve->a || !curve->b) | 
| 317 | 0 |         return 0; | 
| 318 |  |  | 
| 319 | 533 |     if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { | 
| 320 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 321 | 0 |         goto err; | 
| 322 | 0 |     } | 
| 323 |  |  | 
| 324 |  |     /* get a and b */ | 
| 325 | 533 |     if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) { | 
| 326 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 327 | 0 |         goto err; | 
| 328 | 0 |     } | 
| 329 |  |  | 
| 330 |  |     /* | 
| 331 |  |      * Per SEC 1, the curve coefficients must be padded up to size. See C.2's | 
| 332 |  |      * definition of Curve, C.1's definition of FieldElement, and 2.3.5's | 
| 333 |  |      * definition of how to encode the field elements. | 
| 334 |  |      */ | 
| 335 | 533 |     len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8; | 
| 336 | 533 |     if ((a_buf = OPENSSL_malloc(len)) == NULL | 
| 337 | 533 |         || (b_buf = OPENSSL_malloc(len)) == NULL) { | 
| 338 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 339 | 0 |         goto err; | 
| 340 | 0 |     } | 
| 341 | 533 |     if (BN_bn2binpad(tmp_1, a_buf, len) < 0 | 
| 342 | 533 |         || BN_bn2binpad(tmp_2, b_buf, len) < 0) { | 
| 343 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
| 344 | 0 |         goto err; | 
| 345 | 0 |     } | 
| 346 |  |  | 
| 347 |  |     /* set a and b */ | 
| 348 | 533 |     if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len) | 
| 349 | 533 |         || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) { | 
| 350 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 351 | 0 |         goto err; | 
| 352 | 0 |     } | 
| 353 |  |  | 
| 354 |  |     /* set the seed (optional) */ | 
| 355 | 533 |     if (group->seed) { | 
| 356 | 158 |         if (!curve->seed) | 
| 357 | 158 |             if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { | 
| 358 | 0 |                 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 359 | 0 |                 goto err; | 
| 360 | 0 |             } | 
| 361 | 158 |         curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | 
| 362 | 158 |         curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; | 
| 363 | 158 |         if (!ASN1_BIT_STRING_set(curve->seed, group->seed, | 
| 364 | 158 |                                  (int)group->seed_len)) { | 
| 365 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 366 | 0 |             goto err; | 
| 367 | 0 |         } | 
| 368 | 375 |     } else { | 
| 369 | 375 |         ASN1_BIT_STRING_free(curve->seed); | 
| 370 | 375 |         curve->seed = NULL; | 
| 371 | 375 |     } | 
| 372 |  |  | 
| 373 | 533 |     ok = 1; | 
| 374 |  |  | 
| 375 | 533 |  err: | 
| 376 | 533 |     OPENSSL_free(a_buf); | 
| 377 | 533 |     OPENSSL_free(b_buf); | 
| 378 | 533 |     BN_free(tmp_1); | 
| 379 | 533 |     BN_free(tmp_2); | 
| 380 | 533 |     return ok; | 
| 381 | 533 | } | 
| 382 |  |  | 
| 383 |  | ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, | 
| 384 |  |                                         ECPARAMETERS *params) | 
| 385 | 533 | { | 
| 386 | 533 |     size_t len = 0; | 
| 387 | 533 |     ECPARAMETERS *ret = NULL; | 
| 388 | 533 |     const BIGNUM *tmp; | 
| 389 | 533 |     unsigned char *buffer = NULL; | 
| 390 | 533 |     const EC_POINT *point = NULL; | 
| 391 | 533 |     point_conversion_form_t form; | 
| 392 | 533 |     ASN1_INTEGER *orig; | 
| 393 |  |  | 
| 394 | 533 |     if (params == NULL) { | 
| 395 | 533 |         if ((ret = ECPARAMETERS_new()) == NULL) { | 
| 396 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 397 | 0 |             goto err; | 
| 398 | 0 |         } | 
| 399 | 533 |     } else | 
| 400 | 0 |         ret = params; | 
| 401 |  |  | 
| 402 |  |     /* set the version (always one) */ | 
| 403 | 533 |     ret->version = (long)0x1; | 
| 404 |  |  | 
| 405 |  |     /* set the fieldID */ | 
| 406 | 533 |     if (!ec_asn1_group2fieldid(group, ret->fieldID)) { | 
| 407 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 408 | 0 |         goto err; | 
| 409 | 0 |     } | 
| 410 |  |  | 
| 411 |  |     /* set the curve */ | 
| 412 | 533 |     if (!ec_asn1_group2curve(group, ret->curve)) { | 
| 413 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 414 | 0 |         goto err; | 
| 415 | 0 |     } | 
| 416 |  |  | 
| 417 |  |     /* set the base point */ | 
| 418 | 533 |     if ((point = EC_GROUP_get0_generator(group)) == NULL) { | 
| 419 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR); | 
| 420 | 0 |         goto err; | 
| 421 | 0 |     } | 
| 422 |  |  | 
| 423 | 533 |     form = EC_GROUP_get_point_conversion_form(group); | 
| 424 |  |  | 
| 425 | 533 |     len = EC_POINT_point2buf(group, point, form, &buffer, NULL); | 
| 426 | 533 |     if (len == 0) { | 
| 427 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 428 | 0 |         goto err; | 
| 429 | 0 |     } | 
| 430 | 533 |     if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { | 
| 431 | 0 |         OPENSSL_free(buffer); | 
| 432 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 433 | 0 |         goto err; | 
| 434 | 0 |     } | 
| 435 | 533 |     ASN1_STRING_set0(ret->base, buffer, len); | 
| 436 |  |  | 
| 437 |  |     /* set the order */ | 
| 438 | 533 |     tmp = EC_GROUP_get0_order(group); | 
| 439 | 533 |     if (tmp == NULL) { | 
| 440 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 441 | 0 |         goto err; | 
| 442 | 0 |     } | 
| 443 | 533 |     ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order); | 
| 444 | 533 |     if (ret->order == NULL) { | 
| 445 | 0 |         ret->order = orig; | 
| 446 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 447 | 0 |         goto err; | 
| 448 | 0 |     } | 
| 449 |  |  | 
| 450 |  |     /* set the cofactor (optional) */ | 
| 451 | 533 |     tmp = EC_GROUP_get0_cofactor(group); | 
| 452 | 533 |     if (tmp != NULL) { | 
| 453 | 533 |         ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor); | 
| 454 | 533 |         if (ret->cofactor == NULL) { | 
| 455 | 0 |             ret->cofactor = orig; | 
| 456 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 457 | 0 |             goto err; | 
| 458 | 0 |         } | 
| 459 | 533 |     } | 
| 460 |  |  | 
| 461 | 533 |     return ret; | 
| 462 |  |  | 
| 463 | 0 |  err: | 
| 464 | 0 |     if (params == NULL) | 
| 465 | 0 |         ECPARAMETERS_free(ret); | 
| 466 | 0 |     return NULL; | 
| 467 | 533 | } | 
| 468 |  |  | 
| 469 |  | ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, | 
| 470 |  |                                             ECPKPARAMETERS *params) | 
| 471 | 2.32k | { | 
| 472 | 2.32k |     int ok = 1, tmp; | 
| 473 | 2.32k |     ECPKPARAMETERS *ret = params; | 
| 474 |  |  | 
| 475 | 2.32k |     if (ret == NULL) { | 
| 476 | 2.32k |         if ((ret = ECPKPARAMETERS_new()) == NULL) { | 
| 477 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 478 | 0 |             return NULL; | 
| 479 | 0 |         } | 
| 480 | 2.32k |     } else { | 
| 481 | 0 |         if (ret->type == ECPKPARAMETERS_TYPE_NAMED) | 
| 482 | 0 |             ASN1_OBJECT_free(ret->value.named_curve); | 
| 483 | 0 |         else if (ret->type == ECPKPARAMETERS_TYPE_EXPLICIT | 
| 484 | 0 |                  && ret->value.parameters != NULL) | 
| 485 | 0 |             ECPARAMETERS_free(ret->value.parameters); | 
| 486 | 0 |     } | 
| 487 |  |  | 
| 488 | 2.32k |     if (EC_GROUP_get_asn1_flag(group) == OPENSSL_EC_NAMED_CURVE) { | 
| 489 |  |         /* | 
| 490 |  |          * use the asn1 OID to describe the elliptic curve parameters | 
| 491 |  |          */ | 
| 492 | 1.79k |         tmp = EC_GROUP_get_curve_name(group); | 
| 493 | 1.79k |         if (tmp) { | 
| 494 | 1.79k |             ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp); | 
| 495 |  |  | 
| 496 | 1.79k |             if (asn1obj == NULL || OBJ_length(asn1obj) == 0) { | 
| 497 | 0 |                 ASN1_OBJECT_free(asn1obj); | 
| 498 | 0 |                 ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID); | 
| 499 | 0 |                 ok = 0; | 
| 500 | 1.79k |             } else { | 
| 501 | 1.79k |                 ret->type = ECPKPARAMETERS_TYPE_NAMED; | 
| 502 | 1.79k |                 ret->value.named_curve = asn1obj; | 
| 503 | 1.79k |             } | 
| 504 | 1.79k |         } else | 
| 505 |  |             /* we don't know the nid => ERROR */ | 
| 506 | 0 |             ok = 0; | 
| 507 | 1.79k |     } else { | 
| 508 |  |         /* use the ECPARAMETERS structure */ | 
| 509 | 533 |         ret->type = ECPKPARAMETERS_TYPE_EXPLICIT; | 
| 510 | 533 |         if ((ret->value.parameters = | 
| 511 | 533 |              EC_GROUP_get_ecparameters(group, NULL)) == NULL) | 
| 512 | 0 |             ok = 0; | 
| 513 | 533 |     } | 
| 514 |  |  | 
| 515 | 2.32k |     if (!ok) { | 
| 516 | 0 |         ECPKPARAMETERS_free(ret); | 
| 517 | 0 |         return NULL; | 
| 518 | 0 |     } | 
| 519 | 2.32k |     return ret; | 
| 520 | 2.32k | } | 
| 521 |  |  | 
| 522 |  | EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params) | 
| 523 | 10.4k | { | 
| 524 | 10.4k |     int ok = 0, tmp; | 
| 525 | 10.4k |     EC_GROUP *ret = NULL, *dup = NULL; | 
| 526 | 10.4k |     BIGNUM *p = NULL, *a = NULL, *b = NULL; | 
| 527 | 10.4k |     EC_POINT *point = NULL; | 
| 528 | 10.4k |     long field_bits; | 
| 529 | 10.4k |     int curve_name = NID_undef; | 
| 530 | 10.4k |     BN_CTX *ctx = NULL; | 
| 531 |  |  | 
| 532 | 10.4k |     if (params->fieldID == NULL | 
| 533 | 10.4k |             || params->fieldID->fieldType == NULL | 
| 534 | 10.4k |             || params->fieldID->p.ptr == NULL) { | 
| 535 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 536 | 0 |         goto err; | 
| 537 | 0 |     } | 
| 538 |  |  | 
| 539 |  |     /* | 
| 540 |  |      * Now extract the curve parameters a and b. Note that, although SEC 1 | 
| 541 |  |      * specifies the length of their encodings, historical versions of OpenSSL | 
| 542 |  |      * encoded them incorrectly, so we must accept any length for backwards | 
| 543 |  |      * compatibility. | 
| 544 |  |      */ | 
| 545 | 10.4k |     if (params->curve == NULL | 
| 546 | 10.4k |             || params->curve->a == NULL || params->curve->a->data == NULL | 
| 547 | 10.4k |             || params->curve->b == NULL || params->curve->b->data == NULL) { | 
| 548 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 549 | 0 |         goto err; | 
| 550 | 0 |     } | 
| 551 | 10.4k |     a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); | 
| 552 | 10.4k |     if (a == NULL) { | 
| 553 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
| 554 | 0 |         goto err; | 
| 555 | 0 |     } | 
| 556 | 10.4k |     b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); | 
| 557 | 10.4k |     if (b == NULL) { | 
| 558 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
| 559 | 0 |         goto err; | 
| 560 | 0 |     } | 
| 561 |  |  | 
| 562 |  |     /* get the field parameters */ | 
| 563 | 10.4k |     tmp = OBJ_obj2nid(params->fieldID->fieldType); | 
| 564 | 10.4k |     if (tmp == NID_X9_62_characteristic_two_field) | 
| 565 |  | #ifdef OPENSSL_NO_EC2M | 
| 566 |  |     { | 
| 567 |  |         ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); | 
| 568 |  |         goto err; | 
| 569 |  |     } | 
| 570 |  | #else | 
| 571 | 0 |     { | 
| 572 | 0 |         X9_62_CHARACTERISTIC_TWO *char_two; | 
| 573 |  | 
 | 
| 574 | 0 |         char_two = params->fieldID->p.char_two; | 
| 575 |  | 
 | 
| 576 | 0 |         field_bits = char_two->m; | 
| 577 | 0 |         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | 
| 578 | 0 |             ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); | 
| 579 | 0 |             goto err; | 
| 580 | 0 |         } | 
| 581 |  |  | 
| 582 | 0 |         if ((p = BN_new()) == NULL) { | 
| 583 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 584 | 0 |             goto err; | 
| 585 | 0 |         } | 
| 586 |  |  | 
| 587 |  |         /* get the base type */ | 
| 588 | 0 |         tmp = OBJ_obj2nid(char_two->type); | 
| 589 |  | 
 | 
| 590 | 0 |         if (tmp == NID_X9_62_tpBasis) { | 
| 591 | 0 |             long tmp_long; | 
| 592 |  | 
 | 
| 593 | 0 |             if (!char_two->p.tpBasis) { | 
| 594 | 0 |                 ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 595 | 0 |                 goto err; | 
| 596 | 0 |             } | 
| 597 |  |  | 
| 598 | 0 |             tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); | 
| 599 |  | 
 | 
| 600 | 0 |             if (!(char_two->m > tmp_long && tmp_long > 0)) { | 
| 601 | 0 |                 ERR_raise(ERR_LIB_EC, EC_R_INVALID_TRINOMIAL_BASIS); | 
| 602 | 0 |                 goto err; | 
| 603 | 0 |             } | 
| 604 |  |  | 
| 605 |  |             /* create the polynomial */ | 
| 606 | 0 |             if (!BN_set_bit(p, (int)char_two->m)) | 
| 607 | 0 |                 goto err; | 
| 608 | 0 |             if (!BN_set_bit(p, (int)tmp_long)) | 
| 609 | 0 |                 goto err; | 
| 610 | 0 |             if (!BN_set_bit(p, 0)) | 
| 611 | 0 |                 goto err; | 
| 612 | 0 |         } else if (tmp == NID_X9_62_ppBasis) { | 
| 613 | 0 |             X9_62_PENTANOMIAL *penta; | 
| 614 |  | 
 | 
| 615 | 0 |             penta = char_two->p.ppBasis; | 
| 616 | 0 |             if (penta == NULL) { | 
| 617 | 0 |                 ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 618 | 0 |                 goto err; | 
| 619 | 0 |             } | 
| 620 |  |  | 
| 621 | 0 |             if (! | 
| 622 | 0 |                 (char_two->m > penta->k3 && penta->k3 > penta->k2 | 
| 623 | 0 |                  && penta->k2 > penta->k1 && penta->k1 > 0)) { | 
| 624 | 0 |                 ERR_raise(ERR_LIB_EC, EC_R_INVALID_PENTANOMIAL_BASIS); | 
| 625 | 0 |                 goto err; | 
| 626 | 0 |             } | 
| 627 |  |  | 
| 628 |  |             /* create the polynomial */ | 
| 629 | 0 |             if (!BN_set_bit(p, (int)char_two->m)) | 
| 630 | 0 |                 goto err; | 
| 631 | 0 |             if (!BN_set_bit(p, (int)penta->k1)) | 
| 632 | 0 |                 goto err; | 
| 633 | 0 |             if (!BN_set_bit(p, (int)penta->k2)) | 
| 634 | 0 |                 goto err; | 
| 635 | 0 |             if (!BN_set_bit(p, (int)penta->k3)) | 
| 636 | 0 |                 goto err; | 
| 637 | 0 |             if (!BN_set_bit(p, 0)) | 
| 638 | 0 |                 goto err; | 
| 639 | 0 |         } else if (tmp == NID_X9_62_onBasis) { | 
| 640 | 0 |             ERR_raise(ERR_LIB_EC, EC_R_NOT_IMPLEMENTED); | 
| 641 | 0 |             goto err; | 
| 642 | 0 |         } else {                /* error */ | 
| 643 |  | 
 | 
| 644 | 0 |             ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 645 | 0 |             goto err; | 
| 646 | 0 |         } | 
| 647 |  |  | 
| 648 |  |         /* create the EC_GROUP structure */ | 
| 649 | 0 |         ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL); | 
| 650 | 0 |     } | 
| 651 | 10.4k | #endif | 
| 652 | 10.4k |     else if (tmp == NID_X9_62_prime_field) { | 
| 653 |  |         /* we have a curve over a prime field */ | 
| 654 |  |         /* extract the prime number */ | 
| 655 | 10.3k |         if (params->fieldID->p.prime == NULL) { | 
| 656 | 0 |             ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 657 | 0 |             goto err; | 
| 658 | 0 |         } | 
| 659 | 10.3k |         p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); | 
| 660 | 10.3k |         if (p == NULL) { | 
| 661 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 662 | 0 |             goto err; | 
| 663 | 0 |         } | 
| 664 |  |  | 
| 665 | 10.3k |         if (BN_is_negative(p) || BN_is_zero(p)) { | 
| 666 | 219 |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); | 
| 667 | 219 |             goto err; | 
| 668 | 219 |         } | 
| 669 |  |  | 
| 670 | 10.1k |         field_bits = BN_num_bits(p); | 
| 671 | 10.1k |         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | 
| 672 | 0 |             ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); | 
| 673 | 0 |             goto err; | 
| 674 | 0 |         } | 
| 675 |  |  | 
| 676 |  |         /* create the EC_GROUP structure */ | 
| 677 | 10.1k |         ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); | 
| 678 | 10.1k |     } else { | 
| 679 | 148 |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); | 
| 680 | 148 |         goto err; | 
| 681 | 148 |     } | 
| 682 |  |  | 
| 683 | 10.1k |     if (ret == NULL) { | 
| 684 | 1.61k |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 685 | 1.61k |         goto err; | 
| 686 | 1.61k |     } | 
| 687 |  |  | 
| 688 |  |     /* extract seed (optional) */ | 
| 689 | 8.49k |     if (params->curve->seed != NULL) { | 
| 690 |  |         /* | 
| 691 |  |          * This happens for instance with | 
| 692 |  |          * fuzz/corpora/asn1/65cf44e85614c62f10cf3b7a7184c26293a19e4a | 
| 693 |  |          * and causes the OPENSSL_malloc below to choke on the | 
| 694 |  |          * zero length allocation request. | 
| 695 |  |          */ | 
| 696 | 7.21k |         if (params->curve->seed->length == 0) { | 
| 697 | 18 |             ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 698 | 18 |             goto err; | 
| 699 | 18 |         } | 
| 700 | 7.19k |         OPENSSL_free(ret->seed); | 
| 701 | 7.19k |         if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) { | 
| 702 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 703 | 0 |             goto err; | 
| 704 | 0 |         } | 
| 705 | 7.19k |         memcpy(ret->seed, params->curve->seed->data, | 
| 706 | 7.19k |                params->curve->seed->length); | 
| 707 | 7.19k |         ret->seed_len = params->curve->seed->length; | 
| 708 | 7.19k |     } | 
| 709 |  |  | 
| 710 | 8.47k |     if (params->order == NULL | 
| 711 | 8.47k |             || params->base == NULL | 
| 712 | 8.47k |             || params->base->data == NULL | 
| 713 | 8.47k |             || params->base->length == 0) { | 
| 714 | 52 |         ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 715 | 52 |         goto err; | 
| 716 | 52 |     } | 
| 717 |  |  | 
| 718 | 8.42k |     if ((point = EC_POINT_new(ret)) == NULL) | 
| 719 | 0 |         goto err; | 
| 720 |  |  | 
| 721 |  |     /* set the point conversion form */ | 
| 722 | 8.42k |     EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t) | 
| 723 | 8.42k |                                        (params->base->data[0] & ~0x01)); | 
| 724 |  |  | 
| 725 |  |     /* extract the ec point */ | 
| 726 | 8.42k |     if (!EC_POINT_oct2point(ret, point, params->base->data, | 
| 727 | 8.42k |                             params->base->length, NULL)) { | 
| 728 | 2.71k |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 729 | 2.71k |         goto err; | 
| 730 | 2.71k |     } | 
| 731 |  |  | 
| 732 |  |     /* extract the order */ | 
| 733 | 5.70k |     if (ASN1_INTEGER_to_BN(params->order, a) == NULL) { | 
| 734 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 735 | 0 |         goto err; | 
| 736 | 0 |     } | 
| 737 | 5.70k |     if (BN_is_negative(a) || BN_is_zero(a)) { | 
| 738 | 191 |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); | 
| 739 | 191 |         goto err; | 
| 740 | 191 |     } | 
| 741 | 5.51k |     if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */ | 
| 742 | 164 |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); | 
| 743 | 164 |         goto err; | 
| 744 | 164 |     } | 
| 745 |  |  | 
| 746 |  |     /* extract the cofactor (optional) */ | 
| 747 | 5.35k |     if (params->cofactor == NULL) { | 
| 748 | 878 |         BN_free(b); | 
| 749 | 878 |         b = NULL; | 
| 750 | 4.47k |     } else if (ASN1_INTEGER_to_BN(params->cofactor, b) == NULL) { | 
| 751 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); | 
| 752 | 0 |         goto err; | 
| 753 | 0 |     } | 
| 754 |  |     /* set the generator, order and cofactor (if present) */ | 
| 755 | 5.35k |     if (!EC_GROUP_set_generator(ret, point, a, b)) { | 
| 756 | 35 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 757 | 35 |         goto err; | 
| 758 | 35 |     } | 
| 759 |  |  | 
| 760 |  |     /* | 
| 761 |  |      * Check if the explicit parameters group just created matches one of the | 
| 762 |  |      * built-in curves. | 
| 763 |  |      * | 
| 764 |  |      * We create a copy of the group just built, so that we can remove optional | 
| 765 |  |      * fields for the lookup: we do this to avoid the possibility that one of | 
| 766 |  |      * the optional parameters is used to force the library into using a less | 
| 767 |  |      * performant and less secure EC_METHOD instead of the specialized one. | 
| 768 |  |      * In any case, `seed` is not really used in any computation, while a | 
| 769 |  |      * cofactor different from the one in the built-in table is just | 
| 770 |  |      * mathematically wrong anyway and should not be used. | 
| 771 |  |      */ | 
| 772 | 5.31k |     if ((ctx = BN_CTX_new()) == NULL) { | 
| 773 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
| 774 | 0 |         goto err; | 
| 775 | 0 |     } | 
| 776 | 5.31k |     if ((dup = EC_GROUP_dup(ret)) == NULL | 
| 777 | 5.31k |             || EC_GROUP_set_seed(dup, NULL, 0) != 1 | 
| 778 | 5.31k |             || !EC_GROUP_set_generator(dup, point, a, NULL)) { | 
| 779 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 780 | 0 |         goto err; | 
| 781 | 0 |     } | 
| 782 | 5.31k |     if ((curve_name = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) { | 
| 783 |  |         /* | 
| 784 |  |          * The input explicit parameters successfully matched one of the | 
| 785 |  |          * built-in curves: often for built-in curves we have specialized | 
| 786 |  |          * methods with better performance and hardening. | 
| 787 |  |          * | 
| 788 |  |          * In this case we replace the `EC_GROUP` created through explicit | 
| 789 |  |          * parameters with one created from a named group. | 
| 790 |  |          */ | 
| 791 | 83 |         EC_GROUP *named_group = NULL; | 
| 792 |  |  | 
| 793 | 83 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | 
| 794 |  |         /* | 
| 795 |  |          * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for | 
| 796 |  |          * the same curve, we prefer the SECP nid when matching explicit | 
| 797 |  |          * parameters as that is associated with a specialized EC_METHOD. | 
| 798 |  |          */ | 
| 799 | 83 |         if (curve_name == NID_wap_wsg_idm_ecid_wtls12) | 
| 800 | 0 |             curve_name = NID_secp224r1; | 
| 801 | 83 | #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */ | 
| 802 |  |  | 
| 803 | 83 |         if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) { | 
| 804 | 58 |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 805 | 58 |             goto err; | 
| 806 | 58 |         } | 
| 807 | 25 |         EC_GROUP_free(ret); | 
| 808 | 25 |         ret = named_group; | 
| 809 |  |  | 
| 810 |  |         /* | 
| 811 |  |          * Set the flag so that EC_GROUPs created from explicit parameters are | 
| 812 |  |          * serialized using explicit parameters by default. | 
| 813 |  |          */ | 
| 814 | 25 |         EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); | 
| 815 |  |  | 
| 816 |  |         /* | 
| 817 |  |          * If the input params do not contain the optional seed field we make | 
| 818 |  |          * sure it is not added to the returned group. | 
| 819 |  |          * | 
| 820 |  |          * The seed field is not really used inside libcrypto anyway, and | 
| 821 |  |          * adding it to parsed explicit parameter keys would alter their DER | 
| 822 |  |          * encoding output (because of the extra field) which could impact | 
| 823 |  |          * applications fingerprinting keys by their DER encoding. | 
| 824 |  |          */ | 
| 825 | 25 |         if (params->curve->seed == NULL) { | 
| 826 | 10 |             if (EC_GROUP_set_seed(ret, NULL, 0) != 1) | 
| 827 | 0 |                 goto err; | 
| 828 | 10 |         } | 
| 829 | 25 |     } | 
| 830 |  |  | 
| 831 | 5.25k |     ok = 1; | 
| 832 |  |  | 
| 833 | 10.4k |  err: | 
| 834 | 10.4k |     if (!ok) { | 
| 835 | 5.21k |         EC_GROUP_free(ret); | 
| 836 | 5.21k |         ret = NULL; | 
| 837 | 5.21k |     } | 
| 838 | 10.4k |     EC_GROUP_free(dup); | 
| 839 |  |  | 
| 840 | 10.4k |     BN_free(p); | 
| 841 | 10.4k |     BN_free(a); | 
| 842 | 10.4k |     BN_free(b); | 
| 843 | 10.4k |     EC_POINT_free(point); | 
| 844 |  |  | 
| 845 | 10.4k |     BN_CTX_free(ctx); | 
| 846 |  |  | 
| 847 | 10.4k |     return ret; | 
| 848 | 5.25k | } | 
| 849 |  |  | 
| 850 |  | EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params) | 
| 851 | 38.0k | { | 
| 852 | 38.0k |     EC_GROUP *ret = NULL; | 
| 853 | 38.0k |     int tmp = 0; | 
| 854 |  |  | 
| 855 | 38.0k |     if (params == NULL) { | 
| 856 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); | 
| 857 | 0 |         return NULL; | 
| 858 | 0 |     } | 
| 859 |  |  | 
| 860 | 38.0k |     if (params->type == ECPKPARAMETERS_TYPE_NAMED) { | 
| 861 |  |         /* the curve is given by an OID */ | 
| 862 | 24.0k |         tmp = OBJ_obj2nid(params->value.named_curve); | 
| 863 | 24.0k |         if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { | 
| 864 | 3.22k |             ERR_raise(ERR_LIB_EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); | 
| 865 | 3.22k |             return NULL; | 
| 866 | 3.22k |         } | 
| 867 | 20.7k |         EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); | 
| 868 | 20.7k |     } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) { | 
| 869 |  |         /* the parameters are given by an ECPARAMETERS structure */ | 
| 870 | 13.9k |         ret = EC_GROUP_new_from_ecparameters(params->value.parameters); | 
| 871 | 13.9k |         if (!ret) { | 
| 872 | 7.05k |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 873 | 7.05k |             return NULL; | 
| 874 | 7.05k |         } | 
| 875 | 6.90k |         EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); | 
| 876 | 6.90k |     } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) { | 
| 877 |  |         /* implicit parameters inherited from CA - unsupported */ | 
| 878 | 25 |         return NULL; | 
| 879 | 25 |     } else { | 
| 880 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | 
| 881 | 0 |         return NULL; | 
| 882 | 0 |     } | 
| 883 |  |  | 
| 884 | 27.7k |     return ret; | 
| 885 | 38.0k | } | 
| 886 |  |  | 
| 887 |  | /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ | 
| 888 |  |  | 
| 889 |  | EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) | 
| 890 | 127k | { | 
| 891 | 127k |     EC_GROUP *group = NULL; | 
| 892 | 127k |     ECPKPARAMETERS *params = NULL; | 
| 893 | 127k |     const unsigned char *p = *in; | 
| 894 |  |  | 
| 895 | 127k |     if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) { | 
| 896 | 112k |         ECPKPARAMETERS_free(params); | 
| 897 | 112k |         return NULL; | 
| 898 | 112k |     } | 
| 899 |  |  | 
| 900 | 15.8k |     if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) { | 
| 901 | 9.28k |         ECPKPARAMETERS_free(params); | 
| 902 | 9.28k |         return NULL; | 
| 903 | 9.28k |     } | 
| 904 |  |  | 
| 905 | 6.56k |     if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) | 
| 906 | 6.11k |         group->decoded_from_explicit_params = 1; | 
| 907 |  |  | 
| 908 | 6.56k |     if (a) { | 
| 909 | 4.15k |         EC_GROUP_free(*a); | 
| 910 | 4.15k |         *a = group; | 
| 911 | 4.15k |     } | 
| 912 |  |  | 
| 913 | 6.56k |     ECPKPARAMETERS_free(params); | 
| 914 | 6.56k |     *in = p; | 
| 915 | 6.56k |     return group; | 
| 916 | 15.8k | } | 
| 917 |  |  | 
| 918 |  | int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) | 
| 919 | 504 | { | 
| 920 | 504 |     int ret = 0; | 
| 921 | 504 |     ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL); | 
| 922 | 504 |     if (tmp == NULL) { | 
| 923 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_GROUP2PKPARAMETERS_FAILURE); | 
| 924 | 0 |         return 0; | 
| 925 | 0 |     } | 
| 926 | 504 |     if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { | 
| 927 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_I2D_ECPKPARAMETERS_FAILURE); | 
| 928 | 0 |         ECPKPARAMETERS_free(tmp); | 
| 929 | 0 |         return 0; | 
| 930 | 0 |     } | 
| 931 | 504 |     ECPKPARAMETERS_free(tmp); | 
| 932 | 504 |     return ret; | 
| 933 | 504 | } | 
| 934 |  |  | 
| 935 |  | /* some EC_KEY functions */ | 
| 936 |  |  | 
| 937 |  | EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) | 
| 938 | 115k | { | 
| 939 | 115k |     EC_KEY *ret = NULL; | 
| 940 | 115k |     EC_PRIVATEKEY *priv_key = NULL; | 
| 941 | 115k |     const unsigned char *p = *in; | 
| 942 |  |  | 
| 943 | 115k |     if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) | 
| 944 | 99.7k |         return NULL; | 
| 945 |  |  | 
| 946 | 15.4k |     if (a == NULL || *a == NULL) { | 
| 947 | 15.3k |         if ((ret = EC_KEY_new()) == NULL) { | 
| 948 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 949 | 0 |             goto err; | 
| 950 | 0 |         } | 
| 951 | 15.3k |     } else | 
| 952 | 70 |         ret = *a; | 
| 953 |  |  | 
| 954 | 15.4k |     if (priv_key->parameters) { | 
| 955 | 15.2k |         EC_GROUP_free(ret->group); | 
| 956 | 15.2k |         ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters); | 
| 957 | 15.2k |         if (ret->group != NULL | 
| 958 | 15.2k |             && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT) | 
| 959 | 671 |             ret->group->decoded_from_explicit_params = 1; | 
| 960 | 15.2k |     } | 
| 961 |  |  | 
| 962 | 15.4k |     if (ret->group == NULL) { | 
| 963 | 1.03k |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 964 | 1.03k |         goto err; | 
| 965 | 1.03k |     } | 
| 966 |  |  | 
| 967 | 14.3k |     ret->version = priv_key->version; | 
| 968 |  |  | 
| 969 | 14.3k |     if (priv_key->privateKey) { | 
| 970 | 14.3k |         ASN1_OCTET_STRING *pkey = priv_key->privateKey; | 
| 971 | 14.3k |         if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey), | 
| 972 | 14.3k |                             ASN1_STRING_length(pkey)) == 0) | 
| 973 | 0 |             goto err; | 
| 974 | 14.3k |     } else { | 
| 975 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); | 
| 976 | 0 |         goto err; | 
| 977 | 0 |     } | 
| 978 |  |  | 
| 979 | 14.3k |     if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) | 
| 980 | 32 |         EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); | 
| 981 |  |  | 
| 982 | 14.3k |     EC_POINT_clear_free(ret->pub_key); | 
| 983 | 14.3k |     ret->pub_key = EC_POINT_new(ret->group); | 
| 984 | 14.3k |     if (ret->pub_key == NULL) { | 
| 985 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 986 | 0 |         goto err; | 
| 987 | 0 |     } | 
| 988 |  |  | 
| 989 | 14.3k |     if (priv_key->publicKey) { | 
| 990 | 11.3k |         const unsigned char *pub_oct; | 
| 991 | 11.3k |         int pub_oct_len; | 
| 992 |  |  | 
| 993 | 11.3k |         pub_oct = ASN1_STRING_get0_data(priv_key->publicKey); | 
| 994 | 11.3k |         pub_oct_len = ASN1_STRING_length(priv_key->publicKey); | 
| 995 | 11.3k |         if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) { | 
| 996 | 629 |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 997 | 629 |             goto err; | 
| 998 | 629 |         } | 
| 999 | 11.3k |     } else { | 
| 1000 | 3.03k |         if (ret->group->meth->keygenpub == NULL | 
| 1001 | 3.03k |             || ret->group->meth->keygenpub(ret) == 0) | 
| 1002 | 0 |                 goto err; | 
| 1003 |  |         /* Remember the original private-key-only encoding. */ | 
| 1004 | 3.03k |         ret->enc_flag |= EC_PKEY_NO_PUBKEY; | 
| 1005 | 3.03k |     } | 
| 1006 |  |  | 
| 1007 | 13.7k |     if (a) | 
| 1008 | 48 |         *a = ret; | 
| 1009 | 13.7k |     EC_PRIVATEKEY_free(priv_key); | 
| 1010 | 13.7k |     *in = p; | 
| 1011 | 13.7k |     ret->dirty_cnt++; | 
| 1012 | 13.7k |     return ret; | 
| 1013 |  |  | 
| 1014 | 1.66k |  err: | 
| 1015 | 1.66k |     if (a == NULL || *a != ret) | 
| 1016 | 1.64k |         EC_KEY_free(ret); | 
| 1017 | 1.66k |     EC_PRIVATEKEY_free(priv_key); | 
| 1018 | 1.66k |     return NULL; | 
| 1019 | 14.3k | } | 
| 1020 |  |  | 
| 1021 |  | int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out) | 
| 1022 | 2.92k | { | 
| 1023 | 2.92k |     int ret = 0, ok = 0; | 
| 1024 | 2.92k |     unsigned char *priv= NULL, *pub= NULL; | 
| 1025 | 2.92k |     size_t privlen = 0, publen = 0; | 
| 1026 |  |  | 
| 1027 | 2.92k |     EC_PRIVATEKEY *priv_key = NULL; | 
| 1028 |  |  | 
| 1029 | 2.92k |     if (a == NULL || a->group == NULL || | 
| 1030 | 2.92k |         (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) { | 
| 1031 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); | 
| 1032 | 0 |         goto err; | 
| 1033 | 0 |     } | 
| 1034 |  |  | 
| 1035 | 2.92k |     if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { | 
| 1036 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 1037 | 0 |         goto err; | 
| 1038 | 0 |     } | 
| 1039 |  |  | 
| 1040 | 2.92k |     priv_key->version = a->version; | 
| 1041 |  |  | 
| 1042 | 2.92k |     privlen = EC_KEY_priv2buf(a, &priv); | 
| 1043 |  |  | 
| 1044 | 2.92k |     if (privlen == 0) { | 
| 1045 | 1.09k |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 1046 | 1.09k |         goto err; | 
| 1047 | 1.09k |     } | 
| 1048 |  |  | 
| 1049 | 1.82k |     ASN1_STRING_set0(priv_key->privateKey, priv, privlen); | 
| 1050 | 1.82k |     priv = NULL; | 
| 1051 |  |  | 
| 1052 | 1.82k |     if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { | 
| 1053 | 1.81k |         if ((priv_key->parameters = | 
| 1054 | 1.81k |              EC_GROUP_get_ecpkparameters(a->group, | 
| 1055 | 1.81k |                                         priv_key->parameters)) == NULL) { | 
| 1056 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 1057 | 0 |             goto err; | 
| 1058 | 0 |         } | 
| 1059 | 1.81k |     } | 
| 1060 |  |  | 
| 1061 | 1.82k |     if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) { | 
| 1062 | 378 |         priv_key->publicKey = ASN1_BIT_STRING_new(); | 
| 1063 | 378 |         if (priv_key->publicKey == NULL) { | 
| 1064 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 1065 | 0 |             goto err; | 
| 1066 | 0 |         } | 
| 1067 |  |  | 
| 1068 | 378 |         publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL); | 
| 1069 |  |  | 
| 1070 | 378 |         if (publen == 0) { | 
| 1071 | 13 |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 1072 | 13 |             goto err; | 
| 1073 | 13 |         } | 
| 1074 |  |  | 
| 1075 | 365 |         priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | 
| 1076 | 365 |         priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; | 
| 1077 | 365 |         ASN1_STRING_set0(priv_key->publicKey, pub, publen); | 
| 1078 | 365 |         pub = NULL; | 
| 1079 | 365 |     } | 
| 1080 |  |  | 
| 1081 | 1.80k |     if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { | 
| 1082 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 1083 | 0 |         goto err; | 
| 1084 | 0 |     } | 
| 1085 | 1.80k |     ok = 1; | 
| 1086 | 2.92k |  err: | 
| 1087 | 2.92k |     OPENSSL_clear_free(priv, privlen); | 
| 1088 | 2.92k |     OPENSSL_free(pub); | 
| 1089 | 2.92k |     EC_PRIVATEKEY_free(priv_key); | 
| 1090 | 2.92k |     return (ok ? ret : 0); | 
| 1091 | 1.80k | } | 
| 1092 |  |  | 
| 1093 |  | int i2d_ECParameters(const EC_KEY *a, unsigned char **out) | 
| 1094 | 289 | { | 
| 1095 | 289 |     if (a == NULL) { | 
| 1096 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); | 
| 1097 | 0 |         return 0; | 
| 1098 | 0 |     } | 
| 1099 | 289 |     return i2d_ECPKParameters(a->group, out); | 
| 1100 | 289 | } | 
| 1101 |  |  | 
| 1102 |  | EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) | 
| 1103 | 54.8k | { | 
| 1104 | 54.8k |     EC_KEY *ret; | 
| 1105 |  |  | 
| 1106 | 54.8k |     if (in == NULL || *in == NULL) { | 
| 1107 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); | 
| 1108 | 0 |         return NULL; | 
| 1109 | 0 |     } | 
| 1110 |  |  | 
| 1111 | 54.8k |     if (a == NULL || *a == NULL) { | 
| 1112 | 37.2k |         if ((ret = EC_KEY_new()) == NULL) { | 
| 1113 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 1114 | 0 |             return NULL; | 
| 1115 | 0 |         } | 
| 1116 | 37.2k |     } else | 
| 1117 | 17.6k |         ret = *a; | 
| 1118 |  |  | 
| 1119 | 54.8k |     if (!d2i_ECPKParameters(&ret->group, in, len)) { | 
| 1120 | 52.2k |         if (a == NULL || *a != ret) | 
| 1121 | 36.8k |              EC_KEY_free(ret); | 
| 1122 | 15.4k |         else | 
| 1123 | 15.4k |             ret->dirty_cnt++; | 
| 1124 | 52.2k |         return NULL; | 
| 1125 | 52.2k |     } | 
| 1126 |  |  | 
| 1127 | 2.58k |     if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) | 
| 1128 | 3 |         EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); | 
| 1129 |  |  | 
| 1130 | 2.58k |     ret->dirty_cnt++; | 
| 1131 |  |  | 
| 1132 | 2.58k |     if (a) | 
| 1133 | 2.18k |         *a = ret; | 
| 1134 |  |  | 
| 1135 | 2.58k |     return ret; | 
| 1136 | 54.8k | } | 
| 1137 |  |  | 
| 1138 |  | EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) | 
| 1139 | 210k | { | 
| 1140 | 210k |     EC_KEY *ret = NULL; | 
| 1141 |  |  | 
| 1142 | 210k |     if (a == NULL || (*a) == NULL || (*a)->group == NULL) { | 
| 1143 |  |         /* | 
| 1144 |  |          * sorry, but a EC_GROUP-structure is necessary to set the public key | 
| 1145 |  |          */ | 
| 1146 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); | 
| 1147 | 0 |         return 0; | 
| 1148 | 0 |     } | 
| 1149 | 210k |     ret = *a; | 
| 1150 |  |     /* EC_KEY_opt2key updates dirty_cnt */ | 
| 1151 | 210k |     if (!EC_KEY_oct2key(ret, *in, len, NULL)) { | 
| 1152 | 82.1k |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 1153 | 82.1k |         return 0; | 
| 1154 | 82.1k |     } | 
| 1155 | 128k |     *in += len; | 
| 1156 | 128k |     return ret; | 
| 1157 | 210k | } | 
| 1158 |  |  | 
| 1159 |  | int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out) | 
| 1160 | 0 | { | 
| 1161 | 0 |     size_t buf_len = 0; | 
| 1162 | 0 |     int new_buffer = 0; | 
| 1163 |  | 
 | 
| 1164 | 0 |     if (a == NULL) { | 
| 1165 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); | 
| 1166 | 0 |         return 0; | 
| 1167 | 0 |     } | 
| 1168 |  |  | 
| 1169 | 0 |     buf_len = EC_POINT_point2oct(a->group, a->pub_key, | 
| 1170 | 0 |                                  a->conv_form, NULL, 0, NULL); | 
| 1171 |  | 
 | 
| 1172 | 0 |     if (out == NULL || buf_len == 0) | 
| 1173 |  |         /* out == NULL => just return the length of the octet string */ | 
| 1174 | 0 |         return buf_len; | 
| 1175 |  |  | 
| 1176 | 0 |     if (*out == NULL) { | 
| 1177 | 0 |         if ((*out = OPENSSL_malloc(buf_len)) == NULL) { | 
| 1178 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 1179 | 0 |             return 0; | 
| 1180 | 0 |         } | 
| 1181 | 0 |         new_buffer = 1; | 
| 1182 | 0 |     } | 
| 1183 | 0 |     if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, | 
| 1184 | 0 |                             *out, buf_len, NULL)) { | 
| 1185 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
| 1186 | 0 |         if (new_buffer) { | 
| 1187 | 0 |             OPENSSL_free(*out); | 
| 1188 | 0 |             *out = NULL; | 
| 1189 | 0 |         } | 
| 1190 | 0 |         return 0; | 
| 1191 | 0 |     } | 
| 1192 | 0 |     if (!new_buffer) | 
| 1193 | 0 |         *out += buf_len; | 
| 1194 | 0 |     return buf_len; | 
| 1195 | 0 | } | 
| 1196 |  |  | 
| 1197 |  | DECLARE_ASN1_FUNCTIONS(ECDSA_SIG) | 
| 1198 |  | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG) | 
| 1199 |  |  | 
| 1200 |  | #endif /* FIPS_MODULE */ | 
| 1201 |  |  | 
| 1202 |  | ECDSA_SIG *ECDSA_SIG_new(void) | 
| 1203 | 14.1k | { | 
| 1204 | 14.1k |     ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); | 
| 1205 | 14.1k |     if (sig == NULL) | 
| 1206 | 14.1k |         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 1207 | 14.1k |     return sig; | 
| 1208 | 14.1k | } | 
| 1209 |  |  | 
| 1210 |  | void ECDSA_SIG_free(ECDSA_SIG *sig) | 
| 1211 | 26.8k | { | 
| 1212 | 26.8k |     if (sig == NULL) | 
| 1213 | 8 |         return; | 
| 1214 | 26.7k |     BN_clear_free(sig->r); | 
| 1215 | 26.7k |     BN_clear_free(sig->s); | 
| 1216 | 26.7k |     OPENSSL_free(sig); | 
| 1217 | 26.7k | } | 
| 1218 |  |  | 
| 1219 |  | ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len) | 
| 1220 | 24.4k | { | 
| 1221 | 24.4k |     ECDSA_SIG *sig; | 
| 1222 |  |  | 
| 1223 | 24.4k |     if (len < 0) | 
| 1224 | 0 |         return NULL; | 
| 1225 | 24.4k |     if (psig != NULL && *psig != NULL) { | 
| 1226 | 440 |         sig = *psig; | 
| 1227 | 24.0k |     } else { | 
| 1228 | 24.0k |         sig = ECDSA_SIG_new(); | 
| 1229 | 24.0k |         if (sig == NULL) | 
| 1230 | 0 |             return NULL; | 
| 1231 | 24.0k |     } | 
| 1232 | 24.4k |     if (sig->r == NULL) | 
| 1233 | 24.4k |         sig->r = BN_new(); | 
| 1234 | 24.4k |     if (sig->s == NULL) | 
| 1235 | 24.4k |         sig->s = BN_new(); | 
| 1236 | 24.4k |     if (sig->r == NULL || sig->s == NULL | 
| 1237 | 24.4k |         || ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) { | 
| 1238 | 24.1k |         if (psig == NULL || *psig == NULL) | 
| 1239 | 23.9k |             ECDSA_SIG_free(sig); | 
| 1240 | 24.1k |         return NULL; | 
| 1241 | 24.1k |     } | 
| 1242 | 262 |     if (psig != NULL && *psig == NULL) | 
| 1243 | 0 |         *psig = sig; | 
| 1244 | 262 |     return sig; | 
| 1245 | 24.4k | } | 
| 1246 |  |  | 
| 1247 |  | int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout) | 
| 1248 | 119k | { | 
| 1249 | 119k |     BUF_MEM *buf = NULL; | 
| 1250 | 119k |     size_t encoded_len; | 
| 1251 | 119k |     WPACKET pkt; | 
| 1252 |  |  | 
| 1253 | 119k |     if (ppout == NULL) { | 
| 1254 | 118k |         if (!WPACKET_init_null(&pkt, 0)) | 
| 1255 | 0 |             return -1; | 
| 1256 | 118k |     } else if (*ppout == NULL) { | 
| 1257 | 262 |         if ((buf = BUF_MEM_new()) == NULL | 
| 1258 | 262 |                 || !WPACKET_init_len(&pkt, buf, 0)) { | 
| 1259 | 0 |             BUF_MEM_free(buf); | 
| 1260 | 0 |             return -1; | 
| 1261 | 0 |         } | 
| 1262 | 1.32k |     } else { | 
| 1263 | 1.32k |         if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0)) | 
| 1264 | 0 |             return -1; | 
| 1265 | 1.32k |     } | 
| 1266 |  |  | 
| 1267 | 119k |     if (!ossl_encode_der_dsa_sig(&pkt, sig->r, sig->s) | 
| 1268 | 119k |             || !WPACKET_get_total_written(&pkt, &encoded_len) | 
| 1269 | 119k |             || !WPACKET_finish(&pkt)) { | 
| 1270 | 0 |         BUF_MEM_free(buf); | 
| 1271 | 0 |         WPACKET_cleanup(&pkt); | 
| 1272 | 0 |         return -1; | 
| 1273 | 0 |     } | 
| 1274 |  |  | 
| 1275 | 119k |     if (ppout != NULL) { | 
| 1276 | 1.59k |         if (*ppout == NULL) { | 
| 1277 | 262 |             *ppout = (unsigned char *)buf->data; | 
| 1278 | 262 |             buf->data = NULL; | 
| 1279 | 262 |             BUF_MEM_free(buf); | 
| 1280 | 1.32k |         } else { | 
| 1281 | 1.32k |             *ppout += encoded_len; | 
| 1282 | 1.32k |         } | 
| 1283 | 1.59k |     } | 
| 1284 |  |  | 
| 1285 | 119k |     return (int)encoded_len; | 
| 1286 | 119k | } | 
| 1287 |  |  | 
| 1288 |  | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) | 
| 1289 | 0 | { | 
| 1290 | 0 |     if (pr != NULL) | 
| 1291 | 0 |         *pr = sig->r; | 
| 1292 | 0 |     if (ps != NULL) | 
| 1293 | 0 |         *ps = sig->s; | 
| 1294 | 0 | } | 
| 1295 |  |  | 
| 1296 |  | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) | 
| 1297 | 0 | { | 
| 1298 | 0 |     return sig->r; | 
| 1299 | 0 | } | 
| 1300 |  |  | 
| 1301 |  | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) | 
| 1302 | 0 | { | 
| 1303 | 0 |     return sig->s; | 
| 1304 | 0 | } | 
| 1305 |  |  | 
| 1306 |  | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) | 
| 1307 | 0 | { | 
| 1308 | 0 |     if (r == NULL || s == NULL) | 
| 1309 | 0 |         return 0; | 
| 1310 | 0 |     BN_clear_free(sig->r); | 
| 1311 | 0 |     BN_clear_free(sig->s); | 
| 1312 | 0 |     sig->r = r; | 
| 1313 | 0 |     sig->s = s; | 
| 1314 | 0 |     return 1; | 
| 1315 | 0 | } | 
| 1316 |  |  | 
| 1317 |  | int ECDSA_size(const EC_KEY *ec) | 
| 1318 | 118k | { | 
| 1319 | 118k |     int ret; | 
| 1320 | 118k |     ECDSA_SIG sig; | 
| 1321 | 118k |     const EC_GROUP *group; | 
| 1322 | 118k |     const BIGNUM *bn; | 
| 1323 |  |  | 
| 1324 | 118k |     if (ec == NULL) | 
| 1325 | 0 |         return 0; | 
| 1326 | 118k |     group = EC_KEY_get0_group(ec); | 
| 1327 | 118k |     if (group == NULL) | 
| 1328 | 0 |         return 0; | 
| 1329 |  |  | 
| 1330 | 118k |     bn = EC_GROUP_get0_order(group); | 
| 1331 | 118k |     if (bn == NULL) | 
| 1332 | 0 |         return 0; | 
| 1333 |  |  | 
| 1334 | 118k |     sig.r = sig.s = (BIGNUM *)bn; | 
| 1335 | 118k |     ret = i2d_ECDSA_SIG(&sig, NULL); | 
| 1336 |  |  | 
| 1337 | 118k |     if (ret < 0) | 
| 1338 | 0 |         ret = 0; | 
| 1339 | 118k |     return ret; | 
| 1340 | 118k | } |