summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_asn1.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ec/ec_asn1.c1208
1 files changed, 503 insertions, 705 deletions
diff --git a/src/lib/libcrypto/ec/ec_asn1.c b/src/lib/libcrypto/ec/ec_asn1.c
index 2bde9a6a3c..70670a4c75 100644
--- a/src/lib/libcrypto/ec/ec_asn1.c
+++ b/src/lib/libcrypto/ec/ec_asn1.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -63,12 +63,13 @@
63#include <openssl/objects.h> 63#include <openssl/objects.h>
64 64
65 65
66int EC_GROUP_get_basis_type(const EC_GROUP *group) 66int
67 { 67EC_GROUP_get_basis_type(const EC_GROUP * group)
68 int i=0; 68{
69 int i = 0;
69 70
70 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != 71 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
71 NID_X9_62_characteristic_two_field) 72 NID_X9_62_characteristic_two_field)
72 /* everything else is currently not supported */ 73 /* everything else is currently not supported */
73 return 0; 74 return 0;
74 75
@@ -82,40 +83,38 @@ int EC_GROUP_get_basis_type(const EC_GROUP *group)
82 else 83 else
83 /* everything else is currently not supported */ 84 /* everything else is currently not supported */
84 return 0; 85 return 0;
85 } 86}
86#ifndef OPENSSL_NO_EC2M 87#ifndef OPENSSL_NO_EC2M
87int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k) 88int
88 { 89EC_GROUP_get_trinomial_basis(const EC_GROUP * group, unsigned int *k)
90{
89 if (group == NULL) 91 if (group == NULL)
90 return 0; 92 return 0;
91 93
92 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != 94 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
93 NID_X9_62_characteristic_two_field 95 NID_X9_62_characteristic_two_field
94 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0))) 96 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0))) {
95 {
96 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 97 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
97 return 0; 98 return 0;
98 } 99 }
99
100 if (k) 100 if (k)
101 *k = group->poly[1]; 101 *k = group->poly[1];
102 102
103 return 1; 103 return 1;
104 } 104}
105int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1, 105int
106 unsigned int *k2, unsigned int *k3) 106EC_GROUP_get_pentanomial_basis(const EC_GROUP * group, unsigned int *k1,
107 { 107 unsigned int *k2, unsigned int *k3)
108{
108 if (group == NULL) 109 if (group == NULL)
109 return 0; 110 return 0;
110 111
111 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != 112 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
112 NID_X9_62_characteristic_two_field 113 NID_X9_62_characteristic_two_field
113 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0))) 114 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0))) {
114 {
115 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 115 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
116 return 0; 116 return 0;
117 } 117 }
118
119 if (k1) 118 if (k1)
120 *k1 = group->poly[3]; 119 *k1 = group->poly[3];
121 if (k2) 120 if (k2)
@@ -124,7 +123,7 @@ int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
124 *k3 = group->poly[1]; 123 *k3 = group->poly[1];
125 124
126 return 1; 125 return 1;
127 } 126}
128#endif 127#endif
129 128
130 129
@@ -133,27 +132,27 @@ typedef struct x9_62_pentanomial_st {
133 long k1; 132 long k1;
134 long k2; 133 long k2;
135 long k3; 134 long k3;
136 } X9_62_PENTANOMIAL; 135} X9_62_PENTANOMIAL;
137 136
138typedef struct x9_62_characteristic_two_st { 137typedef struct x9_62_characteristic_two_st {
139 long m; 138 long m;
140 ASN1_OBJECT *type; 139 ASN1_OBJECT *type;
141 union { 140 union {
142 char *ptr; 141 char *ptr;
143 /* NID_X9_62_onBasis */ 142 /* NID_X9_62_onBasis */
144 ASN1_NULL *onBasis; 143 ASN1_NULL *onBasis;
145 /* NID_X9_62_tpBasis */ 144 /* NID_X9_62_tpBasis */
146 ASN1_INTEGER *tpBasis; 145 ASN1_INTEGER *tpBasis;
147 /* NID_X9_62_ppBasis */ 146 /* NID_X9_62_ppBasis */
148 X9_62_PENTANOMIAL *ppBasis; 147 X9_62_PENTANOMIAL *ppBasis;
149 /* anything else */ 148 /* anything else */
150 ASN1_TYPE *other; 149 ASN1_TYPE *other;
151 } p; 150 } p;
152 } X9_62_CHARACTERISTIC_TWO; 151} X9_62_CHARACTERISTIC_TWO;
153 152
154typedef struct x9_62_fieldid_st { 153typedef struct x9_62_fieldid_st {
155 ASN1_OBJECT *fieldType; 154 ASN1_OBJECT *fieldType;
156 union { 155 union {
157 char *ptr; 156 char *ptr;
158 /* NID_X9_62_prime_field */ 157 /* NID_X9_62_prime_field */
159 ASN1_INTEGER *prime; 158 ASN1_INTEGER *prime;
@@ -161,40 +160,40 @@ typedef struct x9_62_fieldid_st {
161 X9_62_CHARACTERISTIC_TWO *char_two; 160 X9_62_CHARACTERISTIC_TWO *char_two;
162 /* anything else */ 161 /* anything else */
163 ASN1_TYPE *other; 162 ASN1_TYPE *other;
164 } p; 163 } p;
165 } X9_62_FIELDID; 164} X9_62_FIELDID;
166 165
167typedef struct x9_62_curve_st { 166typedef struct x9_62_curve_st {
168 ASN1_OCTET_STRING *a; 167 ASN1_OCTET_STRING *a;
169 ASN1_OCTET_STRING *b; 168 ASN1_OCTET_STRING *b;
170 ASN1_BIT_STRING *seed; 169 ASN1_BIT_STRING *seed;
171 } X9_62_CURVE; 170} X9_62_CURVE;
172 171
173typedef struct ec_parameters_st { 172typedef struct ec_parameters_st {
174 long version; 173 long version;
175 X9_62_FIELDID *fieldID; 174 X9_62_FIELDID *fieldID;
176 X9_62_CURVE *curve; 175 X9_62_CURVE *curve;
177 ASN1_OCTET_STRING *base; 176 ASN1_OCTET_STRING *base;
178 ASN1_INTEGER *order; 177 ASN1_INTEGER *order;
179 ASN1_INTEGER *cofactor; 178 ASN1_INTEGER *cofactor;
180 } ECPARAMETERS; 179} ECPARAMETERS;
181 180
182struct ecpk_parameters_st { 181struct ecpk_parameters_st {
183 int type; 182 int type;
184 union { 183 union {
185 ASN1_OBJECT *named_curve; 184 ASN1_OBJECT *named_curve;
186 ECPARAMETERS *parameters; 185 ECPARAMETERS *parameters;
187 ASN1_NULL *implicitlyCA; 186 ASN1_NULL *implicitlyCA;
188 } value; 187 } value;
189 }/* ECPKPARAMETERS */; 188} /* ECPKPARAMETERS */ ;
190 189
191/* SEC1 ECPrivateKey */ 190/* SEC1 ECPrivateKey */
192typedef struct ec_privatekey_st { 191typedef struct ec_privatekey_st {
193 long version; 192 long version;
194 ASN1_OCTET_STRING *privateKey; 193 ASN1_OCTET_STRING *privateKey;
195 ECPKPARAMETERS *parameters; 194 ECPKPARAMETERS *parameters;
196 ASN1_BIT_STRING *publicKey; 195 ASN1_BIT_STRING *publicKey;
197 } EC_PRIVATEKEY; 196} EC_PRIVATEKEY;
198 197
199/* the OpenSSL ASN.1 definitions */ 198/* the OpenSSL ASN.1 definitions */
200ASN1_SEQUENCE(X9_62_PENTANOMIAL) = { 199ASN1_SEQUENCE(X9_62_PENTANOMIAL) = {
@@ -219,10 +218,8 @@ ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = {
219 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT), 218 ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT),
220 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO) 219 ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO)
221} ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO) 220} ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO)
222
223DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) 221DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
224IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) 222IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO)
225
226ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY); 223ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY);
227 224
228ASN1_ADB(X9_62_FIELDID) = { 225ASN1_ADB(X9_62_FIELDID) = {
@@ -249,7 +246,6 @@ ASN1_SEQUENCE(ECPARAMETERS) = {
249 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER), 246 ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER),
250 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER) 247 ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER)
251} ASN1_SEQUENCE_END(ECPARAMETERS) 248} ASN1_SEQUENCE_END(ECPARAMETERS)
252
253DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) 249DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
254IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) 250IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
255 251
@@ -258,7 +254,6 @@ ASN1_CHOICE(ECPKPARAMETERS) = {
258 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS), 254 ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS),
259 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL) 255 ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL)
260} ASN1_CHOICE_END(ECPKPARAMETERS) 256} ASN1_CHOICE_END(ECPKPARAMETERS)
261
262DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS) 257DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
263DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS) 258DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
264IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS) 259IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
@@ -269,39 +264,38 @@ ASN1_SEQUENCE(EC_PRIVATEKEY) = {
269 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), 264 ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
270 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) 265 ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
271} ASN1_SEQUENCE_END(EC_PRIVATEKEY) 266} ASN1_SEQUENCE_END(EC_PRIVATEKEY)
272
273DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY) 267DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
274DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY) 268DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
275IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY) 269IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
276
277/* some declarations of internal function */ 270/* some declarations of internal function */
278 271
279/* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */ 272/* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
280static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *); 273static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *);
281/* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */ 274/* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
282static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *); 275static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *);
283/* ec_asn1_parameters2group() creates a EC_GROUP object from a 276/* ec_asn1_parameters2group() creates a EC_GROUP object from a
284 * ECPARAMETERS object */ 277 * ECPARAMETERS object */
285static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *); 278static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *);
286/* ec_asn1_group2parameters() creates a ECPARAMETERS object from a 279/* ec_asn1_group2parameters() creates a ECPARAMETERS object from a
287 * EC_GROUP object */ 280 * EC_GROUP object */
288static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *,ECPARAMETERS *); 281static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *, ECPARAMETERS *);
289/* ec_asn1_pkparameters2group() creates a EC_GROUP object from a 282/* ec_asn1_pkparameters2group() creates a EC_GROUP object from a
290 * ECPKPARAMETERS object */ 283 * ECPKPARAMETERS object */
291static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *); 284static EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *);
292/* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a 285/* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
293 * EC_GROUP object */ 286 * EC_GROUP object */
294static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *, 287static ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *,
295 ECPKPARAMETERS *); 288 ECPKPARAMETERS *);
296 289
297 290
298/* the function definitions */ 291/* the function definitions */
299 292
300static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) 293static int
301 { 294ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
302 int ok=0, nid; 295{
303 BIGNUM *tmp = NULL; 296 int ok = 0, nid;
304 297 BIGNUM *tmp = NULL;
298
305 if (group == NULL || field == NULL) 299 if (group == NULL || field == NULL)
306 return 0; 300 return 0;
307 301
@@ -313,248 +307,203 @@ static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
313 307
314 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); 308 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
315 /* set OID for the field */ 309 /* set OID for the field */
316 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) 310 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
317 {
318 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); 311 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
319 goto err; 312 goto err;
320 } 313 }
321 314 if (nid == NID_X9_62_prime_field) {
322 if (nid == NID_X9_62_prime_field) 315 if ((tmp = BN_new()) == NULL) {
323 {
324 if ((tmp = BN_new()) == NULL)
325 {
326 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 316 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
327 goto err; 317 goto err;
328 } 318 }
329 /* the parameters are specified by the prime number p */ 319 /* the parameters are specified by the prime number p */
330 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) 320 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
331 {
332 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); 321 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
333 goto err; 322 goto err;
334 } 323 }
335 /* set the prime number */ 324 /* set the prime number */
336 field->p.prime = BN_to_ASN1_INTEGER(tmp,NULL); 325 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
337 if (field->p.prime == NULL) 326 if (field->p.prime == NULL) {
338 {
339 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB); 327 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
340 goto err; 328 goto err;
341 }
342 } 329 }
343 else /* nid == NID_X9_62_characteristic_two_field */ 330 } else /* nid == NID_X9_62_characteristic_two_field */
344#ifdef OPENSSL_NO_EC2M 331#ifdef OPENSSL_NO_EC2M
345 { 332 {
346 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED); 333 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
347 goto err; 334 goto err;
348 } 335 }
349#else 336#else
350 { 337 {
351 int field_type; 338 int field_type;
352 X9_62_CHARACTERISTIC_TWO *char_two; 339 X9_62_CHARACTERISTIC_TWO *char_two;
353 340
354 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); 341 field->p.char_two = X9_62_CHARACTERISTIC_TWO_new();
355 char_two = field->p.char_two; 342 char_two = field->p.char_two;
356 343
357 if (char_two == NULL) 344 if (char_two == NULL) {
358 {
359 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 345 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
360 goto err; 346 goto err;
361 } 347 }
362 348 char_two->m = (long) EC_GROUP_get_degree(group);
363 char_two->m = (long)EC_GROUP_get_degree(group);
364 349
365 field_type = EC_GROUP_get_basis_type(group); 350 field_type = EC_GROUP_get_basis_type(group);
366 351
367 if (field_type == 0) 352 if (field_type == 0) {
368 {
369 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); 353 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
370 goto err; 354 goto err;
371 } 355 }
372 /* set base type OID */ 356 /* set base type OID */
373 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) 357 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
374 {
375 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); 358 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
376 goto err; 359 goto err;
377 } 360 }
378 361 if (field_type == NID_X9_62_tpBasis) {
379 if (field_type == NID_X9_62_tpBasis)
380 {
381 unsigned int k; 362 unsigned int k;
382 363
383 if (!EC_GROUP_get_trinomial_basis(group, &k)) 364 if (!EC_GROUP_get_trinomial_basis(group, &k))
384 goto err; 365 goto err;
385 366
386 char_two->p.tpBasis = ASN1_INTEGER_new(); 367 char_two->p.tpBasis = ASN1_INTEGER_new();
387 if (!char_two->p.tpBasis) 368 if (!char_two->p.tpBasis) {
388 {
389 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 369 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
390 goto err; 370 goto err;
391 } 371 }
392 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) 372 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long) k)) {
393 {
394 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, 373 ECerr(EC_F_EC_ASN1_GROUP2FIELDID,
395 ERR_R_ASN1_LIB); 374 ERR_R_ASN1_LIB);
396 goto err; 375 goto err;
397 }
398 } 376 }
399 else if (field_type == NID_X9_62_ppBasis) 377 } else if (field_type == NID_X9_62_ppBasis) {
400 {
401 unsigned int k1, k2, k3; 378 unsigned int k1, k2, k3;
402 379
403 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) 380 if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3))
404 goto err; 381 goto err;
405 382
406 char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); 383 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
407 if (!char_two->p.ppBasis) 384 if (!char_two->p.ppBasis) {
408 {
409 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 385 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
410 goto err; 386 goto err;
411 }
412
413 /* set k? values */
414 char_two->p.ppBasis->k1 = (long)k1;
415 char_two->p.ppBasis->k2 = (long)k2;
416 char_two->p.ppBasis->k3 = (long)k3;
417 } 387 }
418 else /* field_type == NID_X9_62_onBasis */ 388 /* set k? values */
419 { 389 char_two->p.ppBasis->k1 = (long) k1;
390 char_two->p.ppBasis->k2 = (long) k2;
391 char_two->p.ppBasis->k3 = (long) k3;
392 } else { /* field_type == NID_X9_62_onBasis */
420 /* for ONB the parameters are (asn1) NULL */ 393 /* for ONB the parameters are (asn1) NULL */
421 char_two->p.onBasis = ASN1_NULL_new(); 394 char_two->p.onBasis = ASN1_NULL_new();
422 if (!char_two->p.onBasis) 395 if (!char_two->p.onBasis) {
423 {
424 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 396 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
425 goto err; 397 goto err;
426 }
427 } 398 }
428 } 399 }
400 }
429#endif 401#endif
430 402
431 ok = 1; 403 ok = 1;
432 404
433err : if (tmp) 405err: if (tmp)
434 BN_free(tmp); 406 BN_free(tmp);
435 return(ok); 407 return (ok);
436} 408}
437 409
438static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve) 410static int
439 { 411ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve)
440 int ok=0, nid; 412{
441 BIGNUM *tmp_1=NULL, *tmp_2=NULL; 413 int ok = 0, nid;
442 unsigned char *buffer_1=NULL, *buffer_2=NULL, 414 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL;
443 *a_buf=NULL, *b_buf=NULL; 415 unsigned char *buffer_1 = NULL, *buffer_2 = NULL, *a_buf = NULL,
444 size_t len_1, len_2; 416 *b_buf = NULL;
417 size_t len_1, len_2;
445 unsigned char char_zero = 0; 418 unsigned char char_zero = 0;
446 419
447 if (!group || !curve || !curve->a || !curve->b) 420 if (!group || !curve || !curve->a || !curve->b)
448 return 0; 421 return 0;
449 422
450 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) 423 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
451 {
452 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); 424 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
453 goto err; 425 goto err;
454 } 426 }
455
456 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); 427 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
457 428
458 /* get a and b */ 429 /* get a and b */
459 if (nid == NID_X9_62_prime_field) 430 if (nid == NID_X9_62_prime_field) {
460 { 431 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
461 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL))
462 {
463 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB); 432 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
464 goto err; 433 goto err;
465 }
466 } 434 }
435 }
467#ifndef OPENSSL_NO_EC2M 436#ifndef OPENSSL_NO_EC2M
468 else /* nid == NID_X9_62_characteristic_two_field */ 437 else { /* nid == NID_X9_62_characteristic_two_field */
469 { 438 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
470 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL))
471 {
472 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB); 439 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
473 goto err; 440 goto err;
474 }
475 } 441 }
442 }
476#endif 443#endif
477 len_1 = (size_t)BN_num_bytes(tmp_1); 444 len_1 = (size_t) BN_num_bytes(tmp_1);
478 len_2 = (size_t)BN_num_bytes(tmp_2); 445 len_2 = (size_t) BN_num_bytes(tmp_2);
479 446
480 if (len_1 == 0) 447 if (len_1 == 0) {
481 {
482 /* len_1 == 0 => a == 0 */ 448 /* len_1 == 0 => a == 0 */
483 a_buf = &char_zero; 449 a_buf = &char_zero;
484 len_1 = 1; 450 len_1 = 1;
485 } 451 } else {
486 else 452 if ((buffer_1 = malloc(len_1)) == NULL) {
487 {
488 if ((buffer_1 = malloc(len_1)) == NULL)
489 {
490 ECerr(EC_F_EC_ASN1_GROUP2CURVE, 453 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
491 ERR_R_MALLOC_FAILURE); 454 ERR_R_MALLOC_FAILURE);
492 goto err; 455 goto err;
493 } 456 }
494 if ( (len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) 457 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
495 {
496 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB); 458 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
497 goto err; 459 goto err;
498 }
499 a_buf = buffer_1;
500 } 460 }
461 a_buf = buffer_1;
462 }
501 463
502 if (len_2 == 0) 464 if (len_2 == 0) {
503 {
504 /* len_2 == 0 => b == 0 */ 465 /* len_2 == 0 => b == 0 */
505 b_buf = &char_zero; 466 b_buf = &char_zero;
506 len_2 = 1; 467 len_2 = 1;
507 } 468 } else {
508 else 469 if ((buffer_2 = malloc(len_2)) == NULL) {
509 {
510 if ((buffer_2 = malloc(len_2)) == NULL)
511 {
512 ECerr(EC_F_EC_ASN1_GROUP2CURVE, 470 ECerr(EC_F_EC_ASN1_GROUP2CURVE,
513 ERR_R_MALLOC_FAILURE); 471 ERR_R_MALLOC_FAILURE);
514 goto err; 472 goto err;
515 } 473 }
516 if ( (len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) 474 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
517 {
518 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB); 475 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
519 goto err; 476 goto err;
520 }
521 b_buf = buffer_2;
522 } 477 }
523 478 b_buf = buffer_2;
479 }
480
524 /* set a and b */ 481 /* set a and b */
525 if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) || 482 if (!M_ASN1_OCTET_STRING_set(curve->a, a_buf, len_1) ||
526 !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) 483 !M_ASN1_OCTET_STRING_set(curve->b, b_buf, len_2)) {
527 {
528 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); 484 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
529 goto err; 485 goto err;
530 } 486 }
531
532 /* set the seed (optional) */ 487 /* set the seed (optional) */
533 if (group->seed) 488 if (group->seed) {
534 {
535 if (!curve->seed) 489 if (!curve->seed)
536 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) 490 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
537 {
538 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); 491 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
539 goto err; 492 goto err;
540 } 493 }
541 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); 494 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
542 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; 495 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
543 if (!ASN1_BIT_STRING_set(curve->seed, group->seed, 496 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
544 (int)group->seed_len)) 497 (int) group->seed_len)) {
545 {
546 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); 498 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
547 goto err; 499 goto err;
548 }
549 } 500 }
550 else 501 } else {
551 { 502 if (curve->seed) {
552 if (curve->seed)
553 {
554 ASN1_BIT_STRING_free(curve->seed); 503 ASN1_BIT_STRING_free(curve->seed);
555 curve->seed = NULL; 504 curve->seed = NULL;
556 }
557 } 505 }
506 }
558 507
559 ok = 1; 508 ok = 1;
560 509
@@ -566,880 +515,729 @@ err: if (buffer_1)
566 BN_free(tmp_1); 515 BN_free(tmp_1);
567 if (tmp_2) 516 if (tmp_2)
568 BN_free(tmp_2); 517 BN_free(tmp_2);
569 return(ok); 518 return (ok);
570 } 519}
571 520
572static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group, 521static ECPARAMETERS *
573 ECPARAMETERS *param) 522ec_asn1_group2parameters(const EC_GROUP * group, ECPARAMETERS * param)
574 { 523{
575 int ok=0; 524 int ok = 0;
576 size_t len=0; 525 size_t len = 0;
577 ECPARAMETERS *ret=NULL; 526 ECPARAMETERS *ret = NULL;
578 BIGNUM *tmp=NULL; 527 BIGNUM *tmp = NULL;
579 unsigned char *buffer=NULL; 528 unsigned char *buffer = NULL;
580 const EC_POINT *point=NULL; 529 const EC_POINT *point = NULL;
581 point_conversion_form_t form; 530 point_conversion_form_t form;
582 531
583 if ((tmp = BN_new()) == NULL) 532 if ((tmp = BN_new()) == NULL) {
584 {
585 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); 533 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
586 goto err; 534 goto err;
587 }
588
589 if (param == NULL)
590 {
591 if ((ret = ECPARAMETERS_new()) == NULL)
592 {
593 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
594 ERR_R_MALLOC_FAILURE);
595 goto err;
596 }
597 } 535 }
598 else 536 if (param == NULL) {
537 if ((ret = ECPARAMETERS_new()) == NULL) {
538 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS,
539 ERR_R_MALLOC_FAILURE);
540 goto err;
541 }
542 } else
599 ret = param; 543 ret = param;
600 544
601 /* set the version (always one) */ 545 /* set the version (always one) */
602 ret->version = (long)0x1; 546 ret->version = (long) 0x1;
603 547
604 /* set the fieldID */ 548 /* set the fieldID */
605 if (!ec_asn1_group2fieldid(group, ret->fieldID)) 549 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
606 {
607 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 550 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
608 goto err; 551 goto err;
609 } 552 }
610
611 /* set the curve */ 553 /* set the curve */
612 if (!ec_asn1_group2curve(group, ret->curve)) 554 if (!ec_asn1_group2curve(group, ret->curve)) {
613 {
614 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 555 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
615 goto err; 556 goto err;
616 } 557 }
617
618 /* set the base point */ 558 /* set the base point */
619 if ((point = EC_GROUP_get0_generator(group)) == NULL) 559 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
620 {
621 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR); 560 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR);
622 goto err; 561 goto err;
623 } 562 }
624
625 form = EC_GROUP_get_point_conversion_form(group); 563 form = EC_GROUP_get_point_conversion_form(group);
626 564
627 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL); 565 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
628 if (len == 0) 566 if (len == 0) {
629 {
630 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 567 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
631 goto err; 568 goto err;
632 } 569 }
633 if ((buffer = malloc(len)) == NULL) 570 if ((buffer = malloc(len)) == NULL) {
634 {
635 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); 571 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
636 goto err; 572 goto err;
637 } 573 }
638 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) 574 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
639 {
640 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 575 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
641 goto err; 576 goto err;
642 } 577 }
643 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) 578 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
644 {
645 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); 579 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
646 goto err; 580 goto err;
647 } 581 }
648 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) 582 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
649 {
650 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); 583 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
651 goto err; 584 goto err;
652 } 585 }
653
654 /* set the order */ 586 /* set the order */
655 if (!EC_GROUP_get_order(group, tmp, NULL)) 587 if (!EC_GROUP_get_order(group, tmp, NULL)) {
656 {
657 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 588 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
658 goto err; 589 goto err;
659 } 590 }
660 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order); 591 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
661 if (ret->order == NULL) 592 if (ret->order == NULL) {
662 {
663 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); 593 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
664 goto err; 594 goto err;
665 } 595 }
666
667 /* set the cofactor (optional) */ 596 /* set the cofactor (optional) */
668 if (EC_GROUP_get_cofactor(group, tmp, NULL)) 597 if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
669 {
670 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor); 598 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
671 if (ret->cofactor == NULL) 599 if (ret->cofactor == NULL) {
672 {
673 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); 600 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB);
674 goto err; 601 goto err;
675 }
676 } 602 }
677 603 }
678 ok = 1; 604 ok = 1;
679 605
680err : if(!ok) 606err: if (!ok) {
681 {
682 if (ret && !param) 607 if (ret && !param)
683 ECPARAMETERS_free(ret); 608 ECPARAMETERS_free(ret);
684 ret = NULL; 609 ret = NULL;
685 } 610 }
686 if (tmp) 611 if (tmp)
687 BN_free(tmp); 612 BN_free(tmp);
688 if (buffer) 613 if (buffer)
689 free(buffer); 614 free(buffer);
690 return(ret); 615 return (ret);
691 } 616}
692 617
693ECPKPARAMETERS *ec_asn1_group2pkparameters(const EC_GROUP *group, 618ECPKPARAMETERS *
694 ECPKPARAMETERS *params) 619ec_asn1_group2pkparameters(const EC_GROUP * group, ECPKPARAMETERS * params)
695 { 620{
696 int ok = 1, tmp; 621 int ok = 1, tmp;
697 ECPKPARAMETERS *ret = params; 622 ECPKPARAMETERS *ret = params;
698 623
699 if (ret == NULL) 624 if (ret == NULL) {
700 { 625 if ((ret = ECPKPARAMETERS_new()) == NULL) {
701 if ((ret = ECPKPARAMETERS_new()) == NULL) 626 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS,
702 { 627 ERR_R_MALLOC_FAILURE);
703 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS,
704 ERR_R_MALLOC_FAILURE);
705 return NULL; 628 return NULL;
706 }
707 } 629 }
708 else 630 } else {
709 {
710 if (ret->type == 0 && ret->value.named_curve) 631 if (ret->type == 0 && ret->value.named_curve)
711 ASN1_OBJECT_free(ret->value.named_curve); 632 ASN1_OBJECT_free(ret->value.named_curve);
712 else if (ret->type == 1 && ret->value.parameters) 633 else if (ret->type == 1 && ret->value.parameters)
713 ECPARAMETERS_free(ret->value.parameters); 634 ECPARAMETERS_free(ret->value.parameters);
714 } 635 }
715 636
716 if (EC_GROUP_get_asn1_flag(group)) 637 if (EC_GROUP_get_asn1_flag(group)) {
717 { 638 /*
718 /* use the asn1 OID to describe the 639 * use the asn1 OID to describe the the elliptic curve
719 * the elliptic curve parameters 640 * parameters
720 */ 641 */
721 tmp = EC_GROUP_get_curve_name(group); 642 tmp = EC_GROUP_get_curve_name(group);
722 if (tmp) 643 if (tmp) {
723 {
724 ret->type = 0; 644 ret->type = 0;
725 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL) 645 if ((ret->value.named_curve = OBJ_nid2obj(tmp)) == NULL)
726 ok = 0; 646 ok = 0;
727 } 647 } else
728 else
729 /* we don't kmow the nid => ERROR */ 648 /* we don't kmow the nid => ERROR */
730 ok = 0; 649 ok = 0;
731 } 650 } else {
732 else
733 {
734 /* use the ECPARAMETERS structure */ 651 /* use the ECPARAMETERS structure */
735 ret->type = 1; 652 ret->type = 1;
736 if ((ret->value.parameters = ec_asn1_group2parameters( 653 if ((ret->value.parameters = ec_asn1_group2parameters(
737 group, NULL)) == NULL) 654 group, NULL)) == NULL)
738 ok = 0; 655 ok = 0;
739 } 656 }
740 657
741 if (!ok) 658 if (!ok) {
742 {
743 ECPKPARAMETERS_free(ret); 659 ECPKPARAMETERS_free(ret);
744 return NULL; 660 return NULL;
745 }
746 return ret;
747 } 661 }
662 return ret;
663}
748 664
749static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params) 665static EC_GROUP *
750 { 666ec_asn1_parameters2group(const ECPARAMETERS * params)
751 int ok = 0, tmp; 667{
752 EC_GROUP *ret = NULL; 668 int ok = 0, tmp;
753 BIGNUM *p = NULL, *a = NULL, *b = NULL; 669 EC_GROUP *ret = NULL;
754 EC_POINT *point=NULL; 670 BIGNUM *p = NULL, *a = NULL, *b = NULL;
755 long field_bits; 671 EC_POINT *point = NULL;
756 672 long field_bits;
757 if (!params->fieldID || !params->fieldID->fieldType || 673
758 !params->fieldID->p.ptr) 674 if (!params->fieldID || !params->fieldID->fieldType ||
759 { 675 !params->fieldID->p.ptr) {
760 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 676 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
761 goto err; 677 goto err;
762 } 678 }
763
764 /* now extract the curve parameters a and b */ 679 /* now extract the curve parameters a and b */
765 if (!params->curve || !params->curve->a || 680 if (!params->curve || !params->curve->a ||
766 !params->curve->a->data || !params->curve->b || 681 !params->curve->a->data || !params->curve->b ||
767 !params->curve->b->data) 682 !params->curve->b->data) {
768 {
769 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 683 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
770 goto err; 684 goto err;
771 } 685 }
772 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); 686 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
773 if (a == NULL) 687 if (a == NULL) {
774 {
775 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB); 688 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
776 goto err; 689 goto err;
777 } 690 }
778 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); 691 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
779 if (b == NULL) 692 if (b == NULL) {
780 {
781 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB); 693 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);
782 goto err; 694 goto err;
783 } 695 }
784
785 /* get the field parameters */ 696 /* get the field parameters */
786 tmp = OBJ_obj2nid(params->fieldID->fieldType); 697 tmp = OBJ_obj2nid(params->fieldID->fieldType);
787 if (tmp == NID_X9_62_characteristic_two_field) 698 if (tmp == NID_X9_62_characteristic_two_field)
788#ifdef OPENSSL_NO_EC2M 699#ifdef OPENSSL_NO_EC2M
789 { 700 {
790 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED); 701 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);
791 goto err; 702 goto err;
792 } 703 }
793#else 704#else
794 { 705 {
795 X9_62_CHARACTERISTIC_TWO *char_two; 706 X9_62_CHARACTERISTIC_TWO *char_two;
796 707
797 char_two = params->fieldID->p.char_two; 708 char_two = params->fieldID->p.char_two;
798 709
799 field_bits = char_two->m; 710 field_bits = char_two->m;
800 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) 711 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
801 {
802 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE); 712 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
803 goto err; 713 goto err;
804 } 714 }
805 715 if ((p = BN_new()) == NULL) {
806 if ((p = BN_new()) == NULL)
807 {
808 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE); 716 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);
809 goto err; 717 goto err;
810 } 718 }
811
812 /* get the base type */ 719 /* get the base type */
813 tmp = OBJ_obj2nid(char_two->type); 720 tmp = OBJ_obj2nid(char_two->type);
814 721
815 if (tmp == NID_X9_62_tpBasis) 722 if (tmp == NID_X9_62_tpBasis) {
816 {
817 long tmp_long; 723 long tmp_long;
818 724
819 if (!char_two->p.tpBasis) 725 if (!char_two->p.tpBasis) {
820 {
821 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 726 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
822 goto err; 727 goto err;
823 } 728 }
824
825 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); 729 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
826 730
827 if (!(char_two->m > tmp_long && tmp_long > 0)) 731 if (!(char_two->m > tmp_long && tmp_long > 0)) {
828 {
829 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_TRINOMIAL_BASIS); 732 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_TRINOMIAL_BASIS);
830 goto err; 733 goto err;
831 } 734 }
832
833 /* create the polynomial */ 735 /* create the polynomial */
834 if (!BN_set_bit(p, (int)char_two->m)) 736 if (!BN_set_bit(p, (int) char_two->m))
835 goto err; 737 goto err;
836 if (!BN_set_bit(p, (int)tmp_long)) 738 if (!BN_set_bit(p, (int) tmp_long))
837 goto err; 739 goto err;
838 if (!BN_set_bit(p, 0)) 740 if (!BN_set_bit(p, 0))
839 goto err; 741 goto err;
840 } 742 } else if (tmp == NID_X9_62_ppBasis) {
841 else if (tmp == NID_X9_62_ppBasis)
842 {
843 X9_62_PENTANOMIAL *penta; 743 X9_62_PENTANOMIAL *penta;
844 744
845 penta = char_two->p.ppBasis; 745 penta = char_two->p.ppBasis;
846 if (!penta) 746 if (!penta) {
847 {
848 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 747 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
849 goto err; 748 goto err;
850 } 749 }
851 750 if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0)) {
852 if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0))
853 {
854 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_PENTANOMIAL_BASIS); 751 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_PENTANOMIAL_BASIS);
855 goto err; 752 goto err;
856 }
857
858 /* create the polynomial */
859 if (!BN_set_bit(p, (int)char_two->m)) goto err;
860 if (!BN_set_bit(p, (int)penta->k1)) goto err;
861 if (!BN_set_bit(p, (int)penta->k2)) goto err;
862 if (!BN_set_bit(p, (int)penta->k3)) goto err;
863 if (!BN_set_bit(p, 0)) goto err;
864 } 753 }
865 else if (tmp == NID_X9_62_onBasis) 754 /* create the polynomial */
866 { 755 if (!BN_set_bit(p, (int) char_two->m))
756 goto err;
757 if (!BN_set_bit(p, (int) penta->k1))
758 goto err;
759 if (!BN_set_bit(p, (int) penta->k2))
760 goto err;
761 if (!BN_set_bit(p, (int) penta->k3))
762 goto err;
763 if (!BN_set_bit(p, 0))
764 goto err;
765 } else if (tmp == NID_X9_62_onBasis) {
867 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED); 766 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);
868 goto err; 767 goto err;
869 } 768 } else { /* error */
870 else /* error */
871 {
872 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 769 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
873 goto err; 770 goto err;
874 } 771 }
875 772
876 /* create the EC_GROUP structure */ 773 /* create the EC_GROUP structure */
877 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL); 774 ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
878 } 775 }
879#endif 776#endif
880 else if (tmp == NID_X9_62_prime_field) 777 else if (tmp == NID_X9_62_prime_field) {
881 {
882 /* we have a curve over a prime field */ 778 /* we have a curve over a prime field */
883 /* extract the prime number */ 779 /* extract the prime number */
884 if (!params->fieldID->p.prime) 780 if (!params->fieldID->p.prime) {
885 {
886 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 781 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
887 goto err; 782 goto err;
888 } 783 }
889 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); 784 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
890 if (p == NULL) 785 if (p == NULL) {
891 {
892 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); 786 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
893 goto err; 787 goto err;
894 } 788 }
895 789 if (BN_is_negative(p) || BN_is_zero(p)) {
896 if (BN_is_negative(p) || BN_is_zero(p))
897 {
898 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD); 790 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
899 goto err; 791 goto err;
900 } 792 }
901
902 field_bits = BN_num_bits(p); 793 field_bits = BN_num_bits(p);
903 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) 794 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
904 {
905 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE); 795 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);
906 goto err; 796 goto err;
907 } 797 }
908
909 /* create the EC_GROUP structure */ 798 /* create the EC_GROUP structure */
910 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); 799 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
911 } 800 } else {
912 else
913 {
914 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD); 801 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);
915 goto err; 802 goto err;
916 } 803 }
917 804
918 if (ret == NULL) 805 if (ret == NULL) {
919 {
920 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); 806 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
921 goto err; 807 goto err;
922 } 808 }
923
924 /* extract seed (optional) */ 809 /* extract seed (optional) */
925 if (params->curve->seed != NULL) 810 if (params->curve->seed != NULL) {
926 {
927 if (ret->seed != NULL) 811 if (ret->seed != NULL)
928 free(ret->seed); 812 free(ret->seed);
929 if (!(ret->seed = malloc(params->curve->seed->length))) 813 if (!(ret->seed = malloc(params->curve->seed->length))) {
930 { 814 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,
931 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, 815 ERR_R_MALLOC_FAILURE);
932 ERR_R_MALLOC_FAILURE);
933 goto err; 816 goto err;
934 }
935 memcpy(ret->seed, params->curve->seed->data,
936 params->curve->seed->length);
937 ret->seed_len = params->curve->seed->length;
938 } 817 }
939 818 memcpy(ret->seed, params->curve->seed->data,
940 if (!params->order || !params->base || !params->base->data) 819 params->curve->seed->length);
941 { 820 ret->seed_len = params->curve->seed->length;
821 }
822 if (!params->order || !params->base || !params->base->data) {
942 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 823 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);
943 goto err; 824 goto err;
944 } 825 }
945 826 if ((point = EC_POINT_new(ret)) == NULL)
946 if ((point = EC_POINT_new(ret)) == NULL) goto err; 827 goto err;
947 828
948 /* set the point conversion form */ 829 /* set the point conversion form */
949 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t) 830 EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)
950 (params->base->data[0] & ~0x01)); 831 (params->base->data[0] & ~0x01));
951 832
952 /* extract the ec point */ 833 /* extract the ec point */
953 if (!EC_POINT_oct2point(ret, point, params->base->data, 834 if (!EC_POINT_oct2point(ret, point, params->base->data,
954 params->base->length, NULL)) 835 params->base->length, NULL)) {
955 {
956 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); 836 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
957 goto err; 837 goto err;
958 } 838 }
959
960 /* extract the order */ 839 /* extract the order */
961 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) 840 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
962 {
963 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); 841 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
964 goto err; 842 goto err;
965 } 843 }
966 if (BN_is_negative(a) || BN_is_zero(a)) 844 if (BN_is_negative(a) || BN_is_zero(a)) {
967 {
968 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER); 845 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
969 goto err; 846 goto err;
970 } 847 }
971 if (BN_num_bits(a) > (int)field_bits + 1) /* Hasse bound */ 848 if (BN_num_bits(a) > (int) field_bits + 1) { /* Hasse bound */
972 {
973 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER); 849 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);
974 goto err; 850 goto err;
975 } 851 }
976
977 /* extract the cofactor (optional) */ 852 /* extract the cofactor (optional) */
978 if (params->cofactor == NULL) 853 if (params->cofactor == NULL) {
979 { 854 if (b) {
980 if (b)
981 {
982 BN_free(b); 855 BN_free(b);
983 b = NULL; 856 b = NULL;
984 }
985 } 857 }
986 else 858 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
987 if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) 859 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);
988 { 860 goto err;
989 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); 861 }
990 goto err;
991 }
992 /* set the generator, order and cofactor (if present) */ 862 /* set the generator, order and cofactor (if present) */
993 if (!EC_GROUP_set_generator(ret, point, a, b)) 863 if (!EC_GROUP_set_generator(ret, point, a, b)) {
994 {
995 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); 864 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);
996 goto err; 865 goto err;
997 } 866 }
998
999 ok = 1; 867 ok = 1;
1000 868
1001err: if (!ok) 869err: if (!ok) {
1002 { 870 if (ret)
1003 if (ret)
1004 EC_GROUP_clear_free(ret); 871 EC_GROUP_clear_free(ret);
1005 ret = NULL; 872 ret = NULL;
1006 } 873 }
1007 874 if (p)
1008 if (p)
1009 BN_free(p); 875 BN_free(p);
1010 if (a) 876 if (a)
1011 BN_free(a); 877 BN_free(a);
1012 if (b) 878 if (b)
1013 BN_free(b); 879 BN_free(b);
1014 if (point) 880 if (point)
1015 EC_POINT_free(point); 881 EC_POINT_free(point);
1016 return(ret); 882 return (ret);
1017} 883}
1018 884
1019EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params) 885EC_GROUP *
1020 { 886ec_asn1_pkparameters2group(const ECPKPARAMETERS * params)
1021 EC_GROUP *ret=NULL; 887{
1022 int tmp=0; 888 EC_GROUP *ret = NULL;
889 int tmp = 0;
1023 890
1024 if (params == NULL) 891 if (params == NULL) {
1025 { 892 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
1026 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 893 EC_R_MISSING_PARAMETERS);
1027 EC_R_MISSING_PARAMETERS);
1028 return NULL; 894 return NULL;
1029 } 895 }
1030 896 if (params->type == 0) {/* the curve is given by an OID */
1031 if (params->type == 0)
1032 { /* the curve is given by an OID */
1033 tmp = OBJ_obj2nid(params->value.named_curve); 897 tmp = OBJ_obj2nid(params->value.named_curve);
1034 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) 898 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
1035 { 899 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,
1036 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 900 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
1037 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
1038 return NULL; 901 return NULL;
1039 }
1040 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
1041 } 902 }
1042 else if (params->type == 1) 903 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
1043 { /* the parameters are given by a ECPARAMETERS 904 } else if (params->type == 1) { /* the parameters are given by a
1044 * structure */ 905 * ECPARAMETERS structure */
1045 ret = ec_asn1_parameters2group(params->value.parameters); 906 ret = ec_asn1_parameters2group(params->value.parameters);
1046 if (!ret) 907 if (!ret) {
1047 {
1048 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB); 908 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);
1049 return NULL; 909 return NULL;
1050 }
1051 EC_GROUP_set_asn1_flag(ret, 0x0);
1052 } 910 }
1053 else if (params->type == 2) 911 EC_GROUP_set_asn1_flag(ret, 0x0);
1054 { /* implicitlyCA */ 912 } else if (params->type == 2) { /* implicitlyCA */
1055 return NULL; 913 return NULL;
1056 } 914 } else {
1057 else
1058 {
1059 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR); 915 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);
1060 return NULL; 916 return NULL;
1061 } 917 }
1062 918
1063 return ret; 919 return ret;
1064 } 920}
1065 921
1066/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ 922/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
1067 923
1068EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) 924EC_GROUP *
1069 { 925d2i_ECPKParameters(EC_GROUP ** a, const unsigned char **in, long len)
1070 EC_GROUP *group = NULL; 926{
1071 ECPKPARAMETERS *params = NULL; 927 EC_GROUP *group = NULL;
928 ECPKPARAMETERS *params = NULL;
1072 929
1073 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) 930 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
1074 {
1075 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE); 931 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
1076 ECPKPARAMETERS_free(params); 932 ECPKPARAMETERS_free(params);
1077 return NULL; 933 return NULL;
1078 } 934 }
1079 935 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
1080 if ((group = ec_asn1_pkparameters2group(params)) == NULL)
1081 {
1082 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE); 936 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
1083 ECPKPARAMETERS_free(params); 937 ECPKPARAMETERS_free(params);
1084 return NULL; 938 return NULL;
1085 } 939 }
1086
1087
1088 if (a && *a) 940 if (a && *a)
1089 EC_GROUP_clear_free(*a); 941 EC_GROUP_clear_free(*a);
1090 if (a) 942 if (a)
1091 *a = group; 943 *a = group;
1092 944
1093 ECPKPARAMETERS_free(params); 945 ECPKPARAMETERS_free(params);
1094 return(group); 946 return (group);
1095 } 947}
1096 948
1097int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) 949int
1098 { 950i2d_ECPKParameters(const EC_GROUP * a, unsigned char **out)
1099 int ret=0; 951{
1100 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL); 952 int ret = 0;
1101 if (tmp == NULL) 953 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
1102 { 954 if (tmp == NULL) {
1103 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE); 955 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
1104 return 0; 956 return 0;
1105 } 957 }
1106 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) 958 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
1107 {
1108 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE); 959 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
1109 ECPKPARAMETERS_free(tmp); 960 ECPKPARAMETERS_free(tmp);
1110 return 0; 961 return 0;
1111 }
1112 ECPKPARAMETERS_free(tmp);
1113 return(ret);
1114 } 962 }
963 ECPKPARAMETERS_free(tmp);
964 return (ret);
965}
1115 966
1116/* some EC_KEY functions */ 967/* some EC_KEY functions */
1117 968
1118EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) 969EC_KEY *
1119 { 970d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1120 int ok=0; 971{
1121 EC_KEY *ret=NULL; 972 int ok = 0;
1122 EC_PRIVATEKEY *priv_key=NULL; 973 EC_KEY *ret = NULL;
974 EC_PRIVATEKEY *priv_key = NULL;
1123 975
1124 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) 976 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1125 {
1126 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); 977 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1127 return NULL; 978 return NULL;
1128 } 979 }
1129 980 if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL) {
1130 if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL)
1131 {
1132 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 981 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1133 EC_PRIVATEKEY_free(priv_key); 982 EC_PRIVATEKEY_free(priv_key);
1134 return NULL; 983 return NULL;
1135 } 984 }
1136 985 if (a == NULL || *a == NULL) {
1137 if (a == NULL || *a == NULL) 986 if ((ret = EC_KEY_new()) == NULL) {
1138 {
1139 if ((ret = EC_KEY_new()) == NULL)
1140 {
1141 ECerr(EC_F_D2I_ECPRIVATEKEY, 987 ECerr(EC_F_D2I_ECPRIVATEKEY,
1142 ERR_R_MALLOC_FAILURE); 988 ERR_R_MALLOC_FAILURE);
1143 goto err; 989 goto err;
1144 } 990 }
1145 if (a) 991 if (a)
1146 *a = ret; 992 *a = ret;
1147 } 993 } else
1148 else
1149 ret = *a; 994 ret = *a;
1150 995
1151 if (priv_key->parameters) 996 if (priv_key->parameters) {
1152 {
1153 if (ret->group) 997 if (ret->group)
1154 EC_GROUP_clear_free(ret->group); 998 EC_GROUP_clear_free(ret->group);
1155 ret->group = ec_asn1_pkparameters2group(priv_key->parameters); 999 ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1156 } 1000 }
1157 1001 if (ret->group == NULL) {
1158 if (ret->group == NULL)
1159 {
1160 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 1002 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1161 goto err; 1003 goto err;
1162 } 1004 }
1163
1164 ret->version = priv_key->version; 1005 ret->version = priv_key->version;
1165 1006
1166 if (priv_key->privateKey) 1007 if (priv_key->privateKey) {
1167 {
1168 ret->priv_key = BN_bin2bn( 1008 ret->priv_key = BN_bin2bn(
1169 M_ASN1_STRING_data(priv_key->privateKey), 1009 M_ASN1_STRING_data(priv_key->privateKey),
1170 M_ASN1_STRING_length(priv_key->privateKey), 1010 M_ASN1_STRING_length(priv_key->privateKey),
1171 ret->priv_key); 1011 ret->priv_key);
1172 if (ret->priv_key == NULL) 1012 if (ret->priv_key == NULL) {
1173 {
1174 ECerr(EC_F_D2I_ECPRIVATEKEY, 1013 ECerr(EC_F_D2I_ECPRIVATEKEY,
1175 ERR_R_BN_LIB); 1014 ERR_R_BN_LIB);
1176 goto err; 1015 goto err;
1177 }
1178 } 1016 }
1179 else 1017 } else {
1180 { 1018 ECerr(EC_F_D2I_ECPRIVATEKEY,
1181 ECerr(EC_F_D2I_ECPRIVATEKEY, 1019 EC_R_MISSING_PRIVATE_KEY);
1182 EC_R_MISSING_PRIVATE_KEY);
1183 goto err; 1020 goto err;
1184 } 1021 }
1185 1022
1186 if (priv_key->publicKey) 1023 if (priv_key->publicKey) {
1187 {
1188 const unsigned char *pub_oct; 1024 const unsigned char *pub_oct;
1189 size_t pub_oct_len; 1025 size_t pub_oct_len;
1190 1026
1191 if (ret->pub_key) 1027 if (ret->pub_key)
1192 EC_POINT_clear_free(ret->pub_key); 1028 EC_POINT_clear_free(ret->pub_key);
1193 ret->pub_key = EC_POINT_new(ret->group); 1029 ret->pub_key = EC_POINT_new(ret->group);
1194 if (ret->pub_key == NULL) 1030 if (ret->pub_key == NULL) {
1195 {
1196 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 1031 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1197 goto err; 1032 goto err;
1198 } 1033 }
1199 pub_oct = M_ASN1_STRING_data(priv_key->publicKey); 1034 pub_oct = M_ASN1_STRING_data(priv_key->publicKey);
1200 pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey); 1035 pub_oct_len = M_ASN1_STRING_length(priv_key->publicKey);
1201 /* save the point conversion form */ 1036 /* save the point conversion form */
1202 ret->conv_form = (point_conversion_form_t)(pub_oct[0] & ~0x01); 1037 ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1203 if (!EC_POINT_oct2point(ret->group, ret->pub_key, 1038 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1204 pub_oct, pub_oct_len, NULL)) 1039 pub_oct, pub_oct_len, NULL)) {
1205 {
1206 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 1040 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
1207 goto err; 1041 goto err;
1208 }
1209 } 1042 }
1210 1043 }
1211 ok = 1; 1044 ok = 1;
1212err: 1045err:
1213 if (!ok) 1046 if (!ok) {
1214 {
1215 if (ret) 1047 if (ret)
1216 EC_KEY_free(ret); 1048 EC_KEY_free(ret);
1217 ret = NULL; 1049 ret = NULL;
1218 } 1050 }
1219
1220 if (priv_key) 1051 if (priv_key)
1221 EC_PRIVATEKEY_free(priv_key); 1052 EC_PRIVATEKEY_free(priv_key);
1222 1053
1223 return(ret); 1054 return (ret);
1224 } 1055}
1225 1056
1226int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out) 1057int
1227 { 1058i2d_ECPrivateKey(EC_KEY * a, unsigned char **out)
1228 int ret=0, ok=0; 1059{
1229 unsigned char *buffer=NULL; 1060 int ret = 0, ok = 0;
1230 size_t buf_len=0, tmp_len; 1061 unsigned char *buffer = NULL;
1231 EC_PRIVATEKEY *priv_key=NULL; 1062 size_t buf_len = 0, tmp_len;
1063 EC_PRIVATEKEY *priv_key = NULL;
1232 1064
1233 if (a == NULL || a->group == NULL || a->priv_key == NULL) 1065 if (a == NULL || a->group == NULL || a->priv_key == NULL) {
1234 {
1235 ECerr(EC_F_I2D_ECPRIVATEKEY, 1066 ECerr(EC_F_I2D_ECPRIVATEKEY,
1236 ERR_R_PASSED_NULL_PARAMETER); 1067 ERR_R_PASSED_NULL_PARAMETER);
1237 goto err; 1068 goto err;
1238 } 1069 }
1239 1070 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1240 if ((priv_key = EC_PRIVATEKEY_new()) == NULL)
1241 {
1242 ECerr(EC_F_I2D_ECPRIVATEKEY, 1071 ECerr(EC_F_I2D_ECPRIVATEKEY,
1243 ERR_R_MALLOC_FAILURE); 1072 ERR_R_MALLOC_FAILURE);
1244 goto err; 1073 goto err;
1245 } 1074 }
1246
1247 priv_key->version = a->version; 1075 priv_key->version = a->version;
1248 1076
1249 buf_len = (size_t)BN_num_bytes(a->priv_key); 1077 buf_len = (size_t) BN_num_bytes(a->priv_key);
1250 buffer = malloc(buf_len); 1078 buffer = malloc(buf_len);
1251 if (buffer == NULL) 1079 if (buffer == NULL) {
1252 {
1253 ECerr(EC_F_I2D_ECPRIVATEKEY, 1080 ECerr(EC_F_I2D_ECPRIVATEKEY,
1254 ERR_R_MALLOC_FAILURE); 1081 ERR_R_MALLOC_FAILURE);
1255 goto err; 1082 goto err;
1256 } 1083 }
1257 1084 if (!BN_bn2bin(a->priv_key, buffer)) {
1258 if (!BN_bn2bin(a->priv_key, buffer))
1259 {
1260 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB); 1085 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB);
1261 goto err; 1086 goto err;
1262 } 1087 }
1263 1088 if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len)) {
1264 if (!M_ASN1_OCTET_STRING_set(priv_key->privateKey, buffer, buf_len))
1265 {
1266 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); 1089 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1267 goto err; 1090 goto err;
1268 } 1091 }
1269 1092 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1270 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS))
1271 {
1272 if ((priv_key->parameters = ec_asn1_group2pkparameters( 1093 if ((priv_key->parameters = ec_asn1_group2pkparameters(
1273 a->group, priv_key->parameters)) == NULL) 1094 a->group, priv_key->parameters)) == NULL) {
1274 {
1275 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); 1095 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1276 goto err; 1096 goto err;
1277 }
1278 } 1097 }
1279 1098 }
1280 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) 1099 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) {
1281 {
1282 priv_key->publicKey = M_ASN1_BIT_STRING_new(); 1100 priv_key->publicKey = M_ASN1_BIT_STRING_new();
1283 if (priv_key->publicKey == NULL) 1101 if (priv_key->publicKey == NULL) {
1284 {
1285 ECerr(EC_F_I2D_ECPRIVATEKEY, 1102 ECerr(EC_F_I2D_ECPRIVATEKEY,
1286 ERR_R_MALLOC_FAILURE); 1103 ERR_R_MALLOC_FAILURE);
1287 goto err; 1104 goto err;
1288 } 1105 }
1289 1106 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
1290 tmp_len = EC_POINT_point2oct(a->group, a->pub_key, 1107 a->conv_form, NULL, 0, NULL);
1291 a->conv_form, NULL, 0, NULL);
1292 1108
1293 if (tmp_len > buf_len) 1109 if (tmp_len > buf_len) {
1294 {
1295 unsigned char *tmp_buffer = realloc(buffer, tmp_len); 1110 unsigned char *tmp_buffer = realloc(buffer, tmp_len);
1296 if (!tmp_buffer) 1111 if (!tmp_buffer) {
1297 {
1298 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); 1112 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1299 goto err; 1113 goto err;
1300 } 1114 }
1301 buffer = tmp_buffer; 1115 buffer = tmp_buffer;
1302 buf_len = tmp_len; 1116 buf_len = tmp_len;
1303 } 1117 }
1304 1118 if (!EC_POINT_point2oct(a->group, a->pub_key,
1305 if (!EC_POINT_point2oct(a->group, a->pub_key, 1119 a->conv_form, buffer, buf_len, NULL)) {
1306 a->conv_form, buffer, buf_len, NULL))
1307 {
1308 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); 1120 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1309 goto err; 1121 goto err;
1310 } 1122 }
1311 1123 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1312 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
1313 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; 1124 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1314 if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer, 1125 if (!M_ASN1_BIT_STRING_set(priv_key->publicKey, buffer,
1315 buf_len)) 1126 buf_len)) {
1316 {
1317 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); 1127 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB);
1318 goto err; 1128 goto err;
1319 }
1320 } 1129 }
1321 1130 }
1322 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) 1131 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1323 {
1324 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); 1132 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
1325 goto err; 1133 goto err;
1326 } 1134 }
1327 ok=1; 1135 ok = 1;
1328err: 1136err:
1329 if (buffer) 1137 if (buffer)
1330 free(buffer); 1138 free(buffer);
1331 if (priv_key) 1139 if (priv_key)
1332 EC_PRIVATEKEY_free(priv_key); 1140 EC_PRIVATEKEY_free(priv_key);
1333 return(ok?ret:0); 1141 return (ok ? ret : 0);
1334 } 1142}
1335 1143
1336int i2d_ECParameters(EC_KEY *a, unsigned char **out) 1144int
1337 { 1145i2d_ECParameters(EC_KEY * a, unsigned char **out)
1338 if (a == NULL) 1146{
1339 { 1147 if (a == NULL) {
1340 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); 1148 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1341 return 0; 1149 return 0;
1342 }
1343 return i2d_ECPKParameters(a->group, out);
1344 } 1150 }
1151 return i2d_ECPKParameters(a->group, out);
1152}
1345 1153
1346EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) 1154EC_KEY *
1347 { 1155d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len)
1348 EC_KEY *ret; 1156{
1157 EC_KEY *ret;
1349 1158
1350 if (in == NULL || *in == NULL) 1159 if (in == NULL || *in == NULL) {
1351 {
1352 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); 1160 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
1353 return NULL; 1161 return NULL;
1354 } 1162 }
1355 1163 if (a == NULL || *a == NULL) {
1356 if (a == NULL || *a == NULL) 1164 if ((ret = EC_KEY_new()) == NULL) {
1357 {
1358 if ((ret = EC_KEY_new()) == NULL)
1359 {
1360 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE); 1165 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1361 return NULL; 1166 return NULL;
1362 } 1167 }
1363 if (a) 1168 if (a)
1364 *a = ret; 1169 *a = ret;
1365 } 1170 } else
1366 else
1367 ret = *a; 1171 ret = *a;
1368 1172
1369 if (!d2i_ECPKParameters(&ret->group, in, len)) 1173 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1370 {
1371 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB); 1174 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1372 return NULL; 1175 return NULL;
1373 }
1374
1375 return ret;
1376 } 1176 }
1177 return ret;
1178}
1377 1179
1378EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) 1180EC_KEY *
1379 { 1181o2i_ECPublicKey(EC_KEY ** a, const unsigned char **in, long len)
1380 EC_KEY *ret=NULL; 1182{
1183 EC_KEY *ret = NULL;
1381 1184
1382 if (a == NULL || (*a) == NULL || (*a)->group == NULL) 1185 if (a == NULL || (*a) == NULL || (*a)->group == NULL) {
1383 { 1186 /*
1384 /* sorry, but a EC_GROUP-structur is necessary 1187 * sorry, but a EC_GROUP-structur is necessary to set the
1385 * to set the public key */ 1188 * public key
1189 */
1386 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); 1190 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1387 return 0; 1191 return 0;
1388 } 1192 }
1389 ret = *a; 1193 ret = *a;
1390 if (ret->pub_key == NULL && 1194 if (ret->pub_key == NULL &&
1391 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) 1195 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1392 {
1393 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE); 1196 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1394 return 0; 1197 return 0;
1395 } 1198 }
1396 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) 1199 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1397 {
1398 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB); 1200 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
1399 return 0; 1201 return 0;
1400 } 1202 }
1401 /* save the point conversion form */ 1203 /* save the point conversion form */
1402 ret->conv_form = (point_conversion_form_t)(*in[0] & ~0x01); 1204 ret->conv_form = (point_conversion_form_t) (*in[0] & ~0x01);
1403 *in += len; 1205 *in += len;
1404 return ret; 1206 return ret;
1405 } 1207}
1406 1208
1407int i2o_ECPublicKey(EC_KEY *a, unsigned char **out) 1209int
1408 { 1210i2o_ECPublicKey(EC_KEY * a, unsigned char **out)
1409 size_t buf_len=0; 1211{
1212 size_t buf_len = 0;
1410 int new_buffer = 0; 1213 int new_buffer = 0;
1411 1214
1412 if (a == NULL) 1215 if (a == NULL) {
1413 {
1414 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); 1216 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
1415 return 0; 1217 return 0;
1416 } 1218 }
1417 1219 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
1418 buf_len = EC_POINT_point2oct(a->group, a->pub_key, 1220 a->conv_form, NULL, 0, NULL);
1419 a->conv_form, NULL, 0, NULL);
1420 1221
1421 if (out == NULL || buf_len == 0) 1222 if (out == NULL || buf_len == 0)
1422 /* out == NULL => just return the length of the octet string */ 1223 /* out == NULL => just return the length of the octet string */
1423 return buf_len; 1224 return buf_len;
1424 1225
1425 if (*out == NULL) 1226 if (*out == NULL) {
1426 { 1227 if ((*out = malloc(buf_len)) == NULL) {
1427 if ((*out = malloc(buf_len)) == NULL)
1428 {
1429 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE); 1228 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
1430 return 0; 1229 return 0;
1431 }
1432 new_buffer = 1;
1433 } 1230 }
1434 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, 1231 new_buffer = 1;
1435 *out, buf_len, NULL)) 1232 }
1436 { 1233 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1234 *out, buf_len, NULL)) {
1437 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB); 1235 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
1438 free(*out); 1236 free(*out);
1439 *out = NULL; 1237 *out = NULL;
1440 return 0; 1238 return 0;
1441 } 1239 }
1442 if (!new_buffer) 1240 if (!new_buffer)
1443 *out += buf_len; 1241 *out += buf_len;
1444 return buf_len; 1242 return buf_len;
1445 } 1243}