summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_ameth.c
diff options
context:
space:
mode:
authortedu <>2014-05-06 03:56:27 +0000
committertedu <>2014-05-06 03:56:27 +0000
commit2518b24aa4315d557b967bff48dfc9efed909569 (patch)
treece2ee4fdddbbe61dd0ccb045a1604a3d92a86a00 /src/lib/libcrypto/ec/ec_ameth.c
parent0539604f5771dae2c3ecffa8122b5651ff283719 (diff)
downloadopenbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.gz
openbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.bz2
openbsd-2518b24aa4315d557b967bff48dfc9efed909569.zip
knf approximation
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ec/ec_ameth.c443
1 files changed, 212 insertions, 231 deletions
diff --git a/src/lib/libcrypto/ec/ec_ameth.c b/src/lib/libcrypto/ec/ec_ameth.c
index 0e6381f543..9b52c3cbfc 100644
--- a/src/lib/libcrypto/ec/ec_ameth.c
+++ b/src/lib/libcrypto/ec/ec_ameth.c
@@ -9,7 +9,7 @@
9 * are met: 9 * are met:
10 * 10 *
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 13 *
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in 15 * notice, this list of conditions and the following disclaimer in
@@ -65,54 +65,51 @@
65#endif 65#endif
66#include "asn1_locl.h" 66#include "asn1_locl.h"
67 67
68static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key) 68static int
69 { 69eckey_param2type(int *pptype, void **ppval, EC_KEY * ec_key)
70 const EC_GROUP *group; 70{
71 const EC_GROUP *group;
71 int nid; 72 int nid;
72 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) 73 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
73 {
74 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS); 74 ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
75 return 0; 75 return 0;
76 } 76 }
77 if (EC_GROUP_get_asn1_flag(group) 77 if (EC_GROUP_get_asn1_flag(group) &&
78 && (nid = EC_GROUP_get_curve_name(group))) 78 (nid = EC_GROUP_get_curve_name(group))) {
79 /* we have a 'named curve' => just set the OID */ 79 /* we have a 'named curve' => just set the OID */
80 {
81 *ppval = OBJ_nid2obj(nid); 80 *ppval = OBJ_nid2obj(nid);
82 *pptype = V_ASN1_OBJECT; 81 *pptype = V_ASN1_OBJECT;
83 } 82 } else {
84 else /* explicit parameters */ 83 /* explicit parameters */
85 {
86 ASN1_STRING *pstr = NULL; 84 ASN1_STRING *pstr = NULL;
87 pstr = ASN1_STRING_new(); 85 pstr = ASN1_STRING_new();
88 if (!pstr) 86 if (!pstr)
89 return 0; 87 return 0;
90 pstr->length = i2d_ECParameters(ec_key, &pstr->data); 88 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
91 if (pstr->length <= 0) 89 if (pstr->length <= 0) {
92 {
93 ASN1_STRING_free(pstr); 90 ASN1_STRING_free(pstr);
94 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB); 91 ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
95 return 0; 92 return 0;
96 } 93 }
97 *ppval = pstr; 94 *ppval = pstr;
98 *pptype = V_ASN1_SEQUENCE; 95 *pptype = V_ASN1_SEQUENCE;
99 }
100 return 1;
101 } 96 }
97 return 1;
98}
102 99
103static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) 100static int
104 { 101eckey_pub_encode(X509_PUBKEY * pk, const EVP_PKEY * pkey)
102{
105 EC_KEY *ec_key = pkey->pkey.ec; 103 EC_KEY *ec_key = pkey->pkey.ec;
106 void *pval = NULL; 104 void *pval = NULL;
107 int ptype; 105 int ptype;
108 unsigned char *penc = NULL, *p; 106 unsigned char *penc = NULL, *p;
109 int penclen; 107 int penclen;
110 108
111 if (!eckey_param2type(&ptype, &pval, ec_key)) 109 if (!eckey_param2type(&ptype, &pval, ec_key)) {
112 {
113 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB); 110 ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
114 return 0; 111 return 0;
115 } 112 }
116 penclen = i2o_ECPublicKey(ec_key, NULL); 113 penclen = i2o_ECPublicKey(ec_key, NULL);
117 if (penclen <= 0) 114 if (penclen <= 0)
118 goto err; 115 goto err;
@@ -124,9 +121,9 @@ static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
124 if (penclen <= 0) 121 if (penclen <= 0)
125 goto err; 122 goto err;
126 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC), 123 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
127 ptype, pval, penc, penclen)) 124 ptype, pval, penc, penclen))
128 return 1; 125 return 1;
129 err: 126err:
130 if (ptype == V_ASN1_OBJECT) 127 if (ptype == V_ASN1_OBJECT)
131 ASN1_OBJECT_free(pval); 128 ASN1_OBJECT_free(pval);
132 else 129 else
@@ -134,37 +131,36 @@ static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
134 if (penc) 131 if (penc)
135 free(penc); 132 free(penc);
136 return 0; 133 return 0;
137 } 134}
138 135
139static EC_KEY *eckey_type2param(int ptype, void *pval) 136static EC_KEY *
140 { 137eckey_type2param(int ptype, void *pval)
138{
141 EC_KEY *eckey = NULL; 139 EC_KEY *eckey = NULL;
142 if (ptype == V_ASN1_SEQUENCE) 140
143 { 141 if (ptype == V_ASN1_SEQUENCE) {
144 ASN1_STRING *pstr = pval; 142 ASN1_STRING *pstr = pval;
145 const unsigned char *pm = NULL; 143 const unsigned char *pm = NULL;
146 int pmlen; 144 int pmlen;
145
147 pm = pstr->data; 146 pm = pstr->data;
148 pmlen = pstr->length; 147 pmlen = pstr->length;
149 if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen))) 148 if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen))) {
150 {
151 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR); 149 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
152 goto ecerr; 150 goto ecerr;
153 }
154 } 151 }
155 else if (ptype == V_ASN1_OBJECT) 152 } else if (ptype == V_ASN1_OBJECT) {
156 {
157 ASN1_OBJECT *poid = pval; 153 ASN1_OBJECT *poid = pval;
158 EC_GROUP *group; 154 EC_GROUP *group;
159 155
160 /* type == V_ASN1_OBJECT => the parameters are given 156 /*
161 * by an asn1 OID 157 * type == V_ASN1_OBJECT => the parameters are given by an
158 * asn1 OID
162 */ 159 */
163 if ((eckey = EC_KEY_new()) == NULL) 160 if ((eckey = EC_KEY_new()) == NULL) {
164 {
165 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE); 161 ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
166 goto ecerr; 162 goto ecerr;
167 } 163 }
168 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid)); 164 group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
169 if (group == NULL) 165 if (group == NULL)
170 goto ecerr; 166 goto ecerr;
@@ -172,23 +168,22 @@ static EC_KEY *eckey_type2param(int ptype, void *pval)
172 if (EC_KEY_set_group(eckey, group) == 0) 168 if (EC_KEY_set_group(eckey, group) == 0)
173 goto ecerr; 169 goto ecerr;
174 EC_GROUP_free(group); 170 EC_GROUP_free(group);
175 } 171 } else {
176 else
177 {
178 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR); 172 ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
179 goto ecerr; 173 goto ecerr;
180 } 174 }
181 175
182 return eckey; 176 return eckey;
183 177
184 ecerr: 178ecerr:
185 if (eckey) 179 if (eckey)
186 EC_KEY_free(eckey); 180 EC_KEY_free(eckey);
187 return NULL; 181 return NULL;
188 } 182}
189 183
190static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) 184static int
191 { 185eckey_pub_decode(EVP_PKEY * pkey, X509_PUBKEY * pubkey)
186{
192 const unsigned char *p = NULL; 187 const unsigned char *p = NULL;
193 void *pval; 188 void *pval;
194 int ptype, pklen; 189 int ptype, pklen;
@@ -201,44 +196,42 @@ static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
201 196
202 eckey = eckey_type2param(ptype, pval); 197 eckey = eckey_type2param(ptype, pval);
203 198
204 if (!eckey) 199 if (!eckey) {
205 {
206 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB); 200 ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
207 return 0; 201 return 0;
208 } 202 }
209
210 /* We have parameters now set public key */ 203 /* We have parameters now set public key */
211 if (!o2i_ECPublicKey(&eckey, &p, pklen)) 204 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
212 {
213 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR); 205 ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
214 goto ecerr; 206 goto ecerr;
215 } 207 }
216
217 EVP_PKEY_assign_EC_KEY(pkey, eckey); 208 EVP_PKEY_assign_EC_KEY(pkey, eckey);
218 return 1; 209 return 1;
219 210
220 ecerr: 211ecerr:
221 if (eckey) 212 if (eckey)
222 EC_KEY_free(eckey); 213 EC_KEY_free(eckey);
223 return 0; 214 return 0;
224 } 215}
225 216
226static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) 217static int
227 { 218eckey_pub_cmp(const EVP_PKEY * a, const EVP_PKEY * b)
228 int r; 219{
220 int r;
229 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); 221 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
230 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), 222 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), *pb = EC_KEY_get0_public_key(b->pkey.ec);
231 *pb = EC_KEY_get0_public_key(b->pkey.ec); 223
232 r = EC_POINT_cmp(group, pa, pb, NULL); 224 r = EC_POINT_cmp(group, pa, pb, NULL);
233 if (r == 0) 225 if (r == 0)
234 return 1; 226 return 1;
235 if (r == 1) 227 if (r == 1)
236 return 0; 228 return 0;
237 return -2; 229 return -2;
238 } 230}
239 231
240static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) 232static int
241 { 233eckey_priv_decode(EVP_PKEY * pkey, PKCS8_PRIV_KEY_INFO * p8)
234{
242 const unsigned char *p = NULL; 235 const unsigned char *p = NULL;
243 void *pval; 236 void *pval;
244 int ptype, pklen; 237 int ptype, pklen;
@@ -255,100 +248,92 @@ static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
255 goto ecliberr; 248 goto ecliberr;
256 249
257 /* We have parameters now set private key */ 250 /* We have parameters now set private key */
258 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) 251 if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
259 {
260 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR); 252 ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
261 goto ecerr; 253 goto ecerr;
262 } 254 }
263
264 /* calculate public key (if necessary) */ 255 /* calculate public key (if necessary) */
265 if (EC_KEY_get0_public_key(eckey) == NULL) 256 if (EC_KEY_get0_public_key(eckey) == NULL) {
266 {
267 const BIGNUM *priv_key; 257 const BIGNUM *priv_key;
268 const EC_GROUP *group; 258 const EC_GROUP *group;
269 EC_POINT *pub_key; 259 EC_POINT *pub_key;
270 /* the public key was not included in the SEC1 private 260 /*
271 * key => calculate the public key */ 261 * the public key was not included in the SEC1 private key =>
272 group = EC_KEY_get0_group(eckey); 262 * calculate the public key
263 */
264 group = EC_KEY_get0_group(eckey);
273 pub_key = EC_POINT_new(group); 265 pub_key = EC_POINT_new(group);
274 if (pub_key == NULL) 266 if (pub_key == NULL) {
275 {
276 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 267 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
277 goto ecliberr; 268 goto ecliberr;
278 } 269 }
279 if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) 270 if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
280 {
281 EC_POINT_free(pub_key); 271 EC_POINT_free(pub_key);
282 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 272 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
283 goto ecliberr; 273 goto ecliberr;
284 } 274 }
285 priv_key = EC_KEY_get0_private_key(eckey); 275 priv_key = EC_KEY_get0_private_key(eckey);
286 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) 276 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) {
287 {
288 EC_POINT_free(pub_key); 277 EC_POINT_free(pub_key);
289 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 278 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
290 goto ecliberr; 279 goto ecliberr;
291 } 280 }
292 if (EC_KEY_set_public_key(eckey, pub_key) == 0) 281 if (EC_KEY_set_public_key(eckey, pub_key) == 0) {
293 {
294 EC_POINT_free(pub_key); 282 EC_POINT_free(pub_key);
295 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 283 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
296 goto ecliberr; 284 goto ecliberr;
297 }
298 EC_POINT_free(pub_key);
299 } 285 }
300 286 EC_POINT_free(pub_key);
287 }
301 EVP_PKEY_assign_EC_KEY(pkey, eckey); 288 EVP_PKEY_assign_EC_KEY(pkey, eckey);
302 return 1; 289 return 1;
303 290
304 ecliberr: 291ecliberr:
305 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); 292 ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
306 ecerr: 293ecerr:
307 if (eckey) 294 if (eckey)
308 EC_KEY_free(eckey); 295 EC_KEY_free(eckey);
309 return 0; 296 return 0;
310 } 297}
311 298
312static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) 299static int
300eckey_priv_encode(PKCS8_PRIV_KEY_INFO * p8, const EVP_PKEY * pkey)
313{ 301{
314 EC_KEY *ec_key; 302 EC_KEY *ec_key;
315 unsigned char *ep, *p; 303 unsigned char *ep, *p;
316 int eplen, ptype; 304 int eplen, ptype;
317 void *pval; 305 void *pval;
318 unsigned int tmp_flags, old_flags; 306 unsigned int tmp_flags, old_flags;
319 307
320 ec_key = pkey->pkey.ec; 308 ec_key = pkey->pkey.ec;
321 309
322 if (!eckey_param2type(&ptype, &pval, ec_key)) 310 if (!eckey_param2type(&ptype, &pval, ec_key)) {
323 {
324 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR); 311 ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
325 return 0; 312 return 0;
326 } 313 }
327
328 /* set the private key */ 314 /* set the private key */
329 315
330 /* do not include the parameters in the SEC1 private key 316 /*
331 * see PKCS#11 12.11 */ 317 * do not include the parameters in the SEC1 private key see PKCS#11
318 * 12.11
319 */
332 old_flags = EC_KEY_get_enc_flags(ec_key); 320 old_flags = EC_KEY_get_enc_flags(ec_key);
333 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS; 321 tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
334 EC_KEY_set_enc_flags(ec_key, tmp_flags); 322 EC_KEY_set_enc_flags(ec_key, tmp_flags);
335 eplen = i2d_ECPrivateKey(ec_key, NULL); 323 eplen = i2d_ECPrivateKey(ec_key, NULL);
336 if (!eplen) 324 if (!eplen) {
337 {
338 EC_KEY_set_enc_flags(ec_key, old_flags); 325 EC_KEY_set_enc_flags(ec_key, old_flags);
339 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB); 326 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
340 return 0; 327 return 0;
341 } 328 }
342 ep = (unsigned char *) malloc(eplen); 329 ep = (unsigned char *) malloc(eplen);
343 if (!ep) 330 if (!ep) {
344 {
345 EC_KEY_set_enc_flags(ec_key, old_flags); 331 EC_KEY_set_enc_flags(ec_key, old_flags);
346 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); 332 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
347 return 0; 333 return 0;
348 } 334 }
349 p = ep; 335 p = ep;
350 if (!i2d_ECPrivateKey(ec_key, &p)) 336 if (!i2d_ECPrivateKey(ec_key, &p)) {
351 {
352 EC_KEY_set_enc_flags(ec_key, old_flags); 337 EC_KEY_set_enc_flags(ec_key, old_flags);
353 free(ep); 338 free(ep);
354 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB); 339 ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
@@ -358,49 +343,50 @@ static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
358 EC_KEY_set_enc_flags(ec_key, old_flags); 343 EC_KEY_set_enc_flags(ec_key, old_flags);
359 344
360 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0, 345 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
361 ptype, pval, ep, eplen)) 346 ptype, pval, ep, eplen))
362 return 0; 347 return 0;
363 348
364 return 1; 349 return 1;
365} 350}
366 351
367static int int_ec_size(const EVP_PKEY *pkey) 352static int
368 { 353int_ec_size(const EVP_PKEY * pkey)
354{
369 return ECDSA_size(pkey->pkey.ec); 355 return ECDSA_size(pkey->pkey.ec);
370 } 356}
371 357
372static int ec_bits(const EVP_PKEY *pkey) 358static int
373 { 359ec_bits(const EVP_PKEY * pkey)
360{
374 BIGNUM *order = BN_new(); 361 BIGNUM *order = BN_new();
375 const EC_GROUP *group; 362 const EC_GROUP *group;
376 int ret; 363 int ret;
377 364
378 if (!order) 365 if (!order) {
379 {
380 ERR_clear_error(); 366 ERR_clear_error();
381 return 0; 367 return 0;
382 } 368 }
383 group = EC_KEY_get0_group(pkey->pkey.ec); 369 group = EC_KEY_get0_group(pkey->pkey.ec);
384 if (!EC_GROUP_get_order(group, order, NULL)) 370 if (!EC_GROUP_get_order(group, order, NULL)) {
385 {
386 ERR_clear_error(); 371 ERR_clear_error();
387 return 0; 372 return 0;
388 } 373 }
389
390 ret = BN_num_bits(order); 374 ret = BN_num_bits(order);
391 BN_free(order); 375 BN_free(order);
392 return ret; 376 return ret;
393 } 377}
394 378
395static int ec_missing_parameters(const EVP_PKEY *pkey) 379static int
396 { 380ec_missing_parameters(const EVP_PKEY * pkey)
381{
397 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL) 382 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
398 return 1; 383 return 1;
399 return 0; 384 return 0;
400 } 385}
401 386
402static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) 387static int
403 { 388ec_copy_parameters(EVP_PKEY * to, const EVP_PKEY * from)
389{
404 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec)); 390 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
405 if (group == NULL) 391 if (group == NULL)
406 return 0; 392 return 0;
@@ -408,79 +394,70 @@ static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
408 return 0; 394 return 0;
409 EC_GROUP_free(group); 395 EC_GROUP_free(group);
410 return 1; 396 return 1;
411 } 397}
412 398
413static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) 399static int
414 { 400ec_cmp_parameters(const EVP_PKEY * a, const EVP_PKEY * b)
415 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), 401{
416 *group_b = EC_KEY_get0_group(b->pkey.ec); 402 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), *group_b = EC_KEY_get0_group(b->pkey.ec);
417 if (EC_GROUP_cmp(group_a, group_b, NULL)) 403 if (EC_GROUP_cmp(group_a, group_b, NULL))
418 return 0; 404 return 0;
419 else 405 else
420 return 1; 406 return 1;
421 } 407}
422 408
423static void int_ec_free(EVP_PKEY *pkey) 409static void
424 { 410int_ec_free(EVP_PKEY * pkey)
411{
425 EC_KEY_free(pkey->pkey.ec); 412 EC_KEY_free(pkey->pkey.ec);
426 } 413}
427 414
428static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) 415static int
429 { 416do_EC_KEY_print(BIO * bp, const EC_KEY * x, int off, int ktype)
430 unsigned char *buffer=NULL; 417{
418 unsigned char *buffer = NULL;
431 const char *ecstr; 419 const char *ecstr;
432 size_t buf_len=0, i; 420 size_t buf_len = 0, i;
433 int ret=0, reason=ERR_R_BIO_LIB; 421 int ret = 0, reason = ERR_R_BIO_LIB;
434 BIGNUM *pub_key=NULL, *order=NULL; 422 BIGNUM *pub_key = NULL, *order = NULL;
435 BN_CTX *ctx=NULL; 423 BN_CTX *ctx = NULL;
436 const EC_GROUP *group; 424 const EC_GROUP *group;
437 const EC_POINT *public_key; 425 const EC_POINT *public_key;
438 const BIGNUM *priv_key; 426 const BIGNUM *priv_key;
439 427
440 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) 428 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
441 {
442 reason = ERR_R_PASSED_NULL_PARAMETER; 429 reason = ERR_R_PASSED_NULL_PARAMETER;
443 goto err; 430 goto err;
444 } 431 }
445
446 ctx = BN_CTX_new(); 432 ctx = BN_CTX_new();
447 if (ctx == NULL) 433 if (ctx == NULL) {
448 {
449 reason = ERR_R_MALLOC_FAILURE; 434 reason = ERR_R_MALLOC_FAILURE;
450 goto err; 435 goto err;
451 } 436 }
452 437 if (ktype > 0) {
453 if (ktype > 0)
454 {
455 public_key = EC_KEY_get0_public_key(x); 438 public_key = EC_KEY_get0_public_key(x);
456 if ((pub_key = EC_POINT_point2bn(group, public_key, 439 if ((pub_key = EC_POINT_point2bn(group, public_key,
457 EC_KEY_get_conv_form(x), NULL, ctx)) == NULL) 440 EC_KEY_get_conv_form(x), NULL, ctx)) == NULL) {
458 {
459 reason = ERR_R_EC_LIB; 441 reason = ERR_R_EC_LIB;
460 goto err; 442 goto err;
461 }
462 if (pub_key)
463 buf_len = (size_t)BN_num_bytes(pub_key);
464 } 443 }
465 444 if (pub_key)
466 if (ktype == 2) 445 buf_len = (size_t) BN_num_bytes(pub_key);
467 { 446 }
447 if (ktype == 2) {
468 priv_key = EC_KEY_get0_private_key(x); 448 priv_key = EC_KEY_get0_private_key(x);
469 if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) 449 if (priv_key && (i = (size_t) BN_num_bytes(priv_key)) > buf_len)
470 buf_len = i; 450 buf_len = i;
471 } 451 } else
472 else
473 priv_key = NULL; 452 priv_key = NULL;
474 453
475 if (ktype > 0) 454 if (ktype > 0) {
476 {
477 buf_len += 10; 455 buf_len += 10;
478 if ((buffer = malloc(buf_len)) == NULL) 456 if ((buffer = malloc(buf_len)) == NULL) {
479 {
480 reason = ERR_R_MALLOC_FAILURE; 457 reason = ERR_R_MALLOC_FAILURE;
481 goto err; 458 goto err;
482 }
483 } 459 }
460 }
484 if (ktype == 2) 461 if (ktype == 2)
485 ecstr = "Private-Key"; 462 ecstr = "Private-Key";
486 else if (ktype == 1) 463 else if (ktype == 1)
@@ -495,9 +472,10 @@ static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
495 if (!EC_GROUP_get_order(group, order, NULL)) 472 if (!EC_GROUP_get_order(group, order, NULL))
496 goto err; 473 goto err;
497 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, 474 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
498 BN_num_bits(order)) <= 0) goto err; 475 BN_num_bits(order)) <= 0)
499 476 goto err;
500 if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key, 477
478 if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key,
501 buffer, off)) 479 buffer, off))
502 goto err; 480 goto err;
503 if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key, 481 if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key,
@@ -505,11 +483,11 @@ static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype)
505 goto err; 483 goto err;
506 if (!ECPKParameters_print(bp, group, off)) 484 if (!ECPKParameters_print(bp, group, off))
507 goto err; 485 goto err;
508 ret=1; 486 ret = 1;
509err: 487err:
510 if (!ret) 488 if (!ret)
511 ECerr(EC_F_DO_EC_KEY_PRINT, reason); 489 ECerr(EC_F_DO_EC_KEY_PRINT, reason);
512 if (pub_key) 490 if (pub_key)
513 BN_free(pub_key); 491 BN_free(pub_key);
514 if (order) 492 if (order)
515 BN_free(order); 493 BN_free(order);
@@ -517,71 +495,75 @@ err:
517 BN_CTX_free(ctx); 495 BN_CTX_free(ctx);
518 if (buffer != NULL) 496 if (buffer != NULL)
519 free(buffer); 497 free(buffer);
520 return(ret); 498 return (ret);
521 } 499}
522 500
523static int eckey_param_decode(EVP_PKEY *pkey, 501static int
524 const unsigned char **pder, int derlen) 502eckey_param_decode(EVP_PKEY * pkey,
525 { 503 const unsigned char **pder, int derlen)
504{
526 EC_KEY *eckey; 505 EC_KEY *eckey;
527 if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) 506 if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) {
528 {
529 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB); 507 ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
530 return 0; 508 return 0;
531 } 509 }
532 EVP_PKEY_assign_EC_KEY(pkey, eckey); 510 EVP_PKEY_assign_EC_KEY(pkey, eckey);
533 return 1; 511 return 1;
534 } 512}
535 513
536static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder) 514static int
537 { 515eckey_param_encode(const EVP_PKEY * pkey, unsigned char **pder)
516{
538 return i2d_ECParameters(pkey->pkey.ec, pder); 517 return i2d_ECParameters(pkey->pkey.ec, pder);
539 } 518}
540 519
541static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, 520static int
542 ASN1_PCTX *ctx) 521eckey_param_print(BIO * bp, const EVP_PKEY * pkey, int indent,
543 { 522 ASN1_PCTX * ctx)
523{
544 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0); 524 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
545 } 525}
546 526
547static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, 527static int
548 ASN1_PCTX *ctx) 528eckey_pub_print(BIO * bp, const EVP_PKEY * pkey, int indent,
549 { 529 ASN1_PCTX * ctx)
530{
550 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1); 531 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
551 } 532}
552 533
553 534
554static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, 535static int
555 ASN1_PCTX *ctx) 536eckey_priv_print(BIO * bp, const EVP_PKEY * pkey, int indent,
556 { 537 ASN1_PCTX * ctx)
538{
557 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2); 539 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
558 } 540}
559 541
560static int old_ec_priv_decode(EVP_PKEY *pkey, 542static int
561 const unsigned char **pder, int derlen) 543old_ec_priv_decode(EVP_PKEY * pkey,
562 { 544 const unsigned char **pder, int derlen)
545{
563 EC_KEY *ec; 546 EC_KEY *ec;
564 if (!(ec = d2i_ECPrivateKey (NULL, pder, derlen))) 547 if (!(ec = d2i_ECPrivateKey(NULL, pder, derlen))) {
565 {
566 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR); 548 ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
567 return 0; 549 return 0;
568 } 550 }
569 EVP_PKEY_assign_EC_KEY(pkey, ec); 551 EVP_PKEY_assign_EC_KEY(pkey, ec);
570 return 1; 552 return 1;
571 } 553}
572 554
573static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) 555static int
574 { 556old_ec_priv_encode(const EVP_PKEY * pkey, unsigned char **pder)
557{
575 return i2d_ECPrivateKey(pkey->pkey.ec, pder); 558 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
576 } 559}
577 560
578static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) 561static int
579 { 562ec_pkey_ctrl(EVP_PKEY * pkey, int op, long arg1, void *arg2)
580 switch (op) 563{
581 { 564 switch (op) {
582 case ASN1_PKEY_CTRL_PKCS7_SIGN: 565 case ASN1_PKEY_CTRL_PKCS7_SIGN:
583 if (arg1 == 0) 566 if (arg1 == 0) {
584 {
585 int snid, hnid; 567 int snid, hnid;
586 X509_ALGOR *alg1, *alg2; 568 X509_ALGOR *alg1, *alg2;
587 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2); 569 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
@@ -591,41 +573,40 @@ static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
591 if (hnid == NID_undef) 573 if (hnid == NID_undef)
592 return -1; 574 return -1;
593 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) 575 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
594 return -1; 576 return -1;
595 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); 577 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
596 } 578 }
597 return 1; 579 return 1;
598#ifndef OPENSSL_NO_CMS 580#ifndef OPENSSL_NO_CMS
599 case ASN1_PKEY_CTRL_CMS_SIGN: 581 case ASN1_PKEY_CTRL_CMS_SIGN:
600 if (arg1 == 0) 582 if (arg1 == 0) {
601 {
602 int snid, hnid; 583 int snid, hnid;
603 X509_ALGOR *alg1, *alg2; 584 X509_ALGOR *alg1, *alg2;
604 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, 585 CMS_SignerInfo_get0_algs(arg2, NULL, NULL,
605 &alg1, &alg2); 586 &alg1, &alg2);
606 if (alg1 == NULL || alg1->algorithm == NULL) 587 if (alg1 == NULL || alg1->algorithm == NULL)
607 return -1; 588 return -1;
608 hnid = OBJ_obj2nid(alg1->algorithm); 589 hnid = OBJ_obj2nid(alg1->algorithm);
609 if (hnid == NID_undef) 590 if (hnid == NID_undef)
610 return -1; 591 return -1;
611 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) 592 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
612 return -1; 593 return -1;
613 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); 594 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
614 } 595 }
615 return 1; 596 return 1;
616#endif 597#endif
617 598
618 case ASN1_PKEY_CTRL_DEFAULT_MD_NID: 599 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
619 *(int *)arg2 = NID_sha1; 600 *(int *) arg2 = NID_sha1;
620 return 2; 601 return 2;
621 602
622 default: 603 default:
623 return -2; 604 return -2;
624 605
625 }
626
627 } 606 }
628 607
608}
609
629const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = { 610const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
630 .pkey_id = EVP_PKEY_EC, 611 .pkey_id = EVP_PKEY_EC,
631 .pkey_base_id = EVP_PKEY_EC, 612 .pkey_base_id = EVP_PKEY_EC,