Deprecate the recursive includes of bn.h from various API headers (asn1.h,
[openssl.git] / crypto / evp / evp_pkey.c
1 /* evp_pkey.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2002 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include "cryptlib.h"
62 #include <openssl/x509.h>
63 #include <openssl/rand.h>
64 #include <openssl/rsa.h>
65 #include <openssl/dsa.h>
66 #include <openssl/bn.h>
67
68 #ifndef OPENSSL_NO_DSA
69 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
70 #endif
71 #ifndef OPENSSL_NO_EC
72 static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
73 #endif
74
75 /* Extract a private key from a PKCS8 structure */
76
77 EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8)
78 {
79         EVP_PKEY *pkey = NULL;
80 #ifndef OPENSSL_NO_RSA
81         RSA *rsa = NULL;
82 #endif
83 #ifndef OPENSSL_NO_DSA
84         DSA *dsa = NULL;
85         ASN1_TYPE *t1, *t2;
86         ASN1_INTEGER *privkey;
87         STACK_OF(ASN1_TYPE) *ndsa = NULL;
88 #endif
89 #ifndef OPENSSL_NO_EC
90         EC_KEY *eckey = NULL;
91         const unsigned char *p_tmp;
92 #endif
93 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
94         ASN1_TYPE    *param = NULL;     
95         BN_CTX *ctx = NULL;
96         int plen;
97 #endif
98         X509_ALGOR *a;
99         const unsigned char *p;
100         const unsigned char *cp;
101         int pkeylen;
102         int  nid;
103         char obj_tmp[80];
104
105         if(p8->pkey->type == V_ASN1_OCTET_STRING) {
106                 p8->broken = PKCS8_OK;
107                 p = p8->pkey->value.octet_string->data;
108                 pkeylen = p8->pkey->value.octet_string->length;
109         } else {
110                 p8->broken = PKCS8_NO_OCTET;
111                 p = p8->pkey->value.sequence->data;
112                 pkeylen = p8->pkey->value.sequence->length;
113         }
114         if (!(pkey = EVP_PKEY_new())) {
115                 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
116                 return NULL;
117         }
118         a = p8->pkeyalg;
119         nid = OBJ_obj2nid(a->algorithm);
120         switch(nid)
121         {
122 #ifndef OPENSSL_NO_RSA
123                 case NID_rsaEncryption:
124                 cp = p;
125                 if (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) {
126                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
127                         return NULL;
128                 }
129                 EVP_PKEY_assign_RSA (pkey, rsa);
130                 break;
131 #endif
132 #ifndef OPENSSL_NO_DSA
133                 case NID_dsa:
134                 /* PKCS#8 DSA is weird: you just get a private key integer
135                  * and parameters in the AlgorithmIdentifier the pubkey must
136                  * be recalculated.
137                  */
138         
139                 /* Check for broken DSA PKCS#8, UGH! */
140                 if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {
141                     if(!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen, 
142                                                           d2i_ASN1_TYPE,
143                                                           ASN1_TYPE_free))) {
144                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
145                         goto dsaerr;
146                     }
147                     if(sk_ASN1_TYPE_num(ndsa) != 2 ) {
148                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
149                         goto dsaerr;
150                     }
151                     /* Handle Two broken types:
152                      * SEQUENCE {parameters, priv_key}
153                      * SEQUENCE {pub_key, priv_key}
154                      */
155
156                     t1 = sk_ASN1_TYPE_value(ndsa, 0);
157                     t2 = sk_ASN1_TYPE_value(ndsa, 1);
158                     if(t1->type == V_ASN1_SEQUENCE) {
159                         p8->broken = PKCS8_EMBEDDED_PARAM;
160                         param = t1;
161                     } else if(a->parameter->type == V_ASN1_SEQUENCE) {
162                         p8->broken = PKCS8_NS_DB;
163                         param = a->parameter;
164                     } else {
165                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
166                         goto dsaerr;
167                     }
168
169                     if(t2->type != V_ASN1_INTEGER) {
170                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
171                         goto dsaerr;
172                     }
173                     privkey = t2->value.integer;
174                 } else {
175                         if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
176                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
177                                 goto dsaerr;
178                         }
179                         param = p8->pkeyalg->parameter;
180                 }
181                 if (!param || (param->type != V_ASN1_SEQUENCE)) {
182                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
183                         goto dsaerr;
184                 }
185                 cp = p = param->value.sequence->data;
186                 plen = param->value.sequence->length;
187                 if (!(dsa = d2i_DSAparams (NULL, &cp, plen))) {
188                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
189                         goto dsaerr;
190                 }
191                 /* We have parameters now set private key */
192                 if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
193                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
194                         goto dsaerr;
195                 }
196                 /* Calculate public key (ouch!) */
197                 if (!(dsa->pub_key = BN_new())) {
198                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
199                         goto dsaerr;
200                 }
201                 if (!(ctx = BN_CTX_new())) {
202                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
203                         goto dsaerr;
204                 }
205                         
206                 if (!BN_mod_exp(dsa->pub_key, dsa->g,
207                                                  dsa->priv_key, dsa->p, ctx)) {
208                         
209                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);
210                         goto dsaerr;
211                 }
212
213                 EVP_PKEY_assign_DSA(pkey, dsa);
214                 BN_CTX_free (ctx);
215                 if(ndsa) sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
216                 else ASN1_INTEGER_free(privkey);
217                 break;
218                 dsaerr:
219                 BN_CTX_free (ctx);
220                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
221                 DSA_free(dsa);
222                 EVP_PKEY_free(pkey);
223                 return NULL;
224                 break;
225 #endif
226 #ifndef OPENSSL_NO_EC
227                 case NID_X9_62_id_ecPublicKey:
228                 p_tmp = p;
229                 /* extract the ec parameters */
230                 param = p8->pkeyalg->parameter;
231
232                 if (!param || ((param->type != V_ASN1_SEQUENCE) &&
233                     (param->type != V_ASN1_OBJECT)))
234                 {
235                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
236                         goto ecerr;
237                 }
238
239                 if (param->type == V_ASN1_SEQUENCE)
240                 {
241                         cp = p = param->value.sequence->data;
242                         plen = param->value.sequence->length;
243
244                         if (!(eckey = d2i_ECParameters(NULL, &cp, plen)))
245                         {
246                                 EVPerr(EVP_F_EVP_PKCS82PKEY,
247                                         EVP_R_DECODE_ERROR);
248                                 goto ecerr;
249                         }
250                 }
251                 else
252                 {
253                         cp = p = param->value.object->data;
254                         plen = param->value.object->length;
255
256                         /* type == V_ASN1_OBJECT => the parameters are given
257                          * by an asn1 OID
258                          */
259                         if ((eckey = EC_KEY_new()) == NULL)
260                         {
261                                 EVPerr(EVP_F_EVP_PKCS82PKEY,
262                                         ERR_R_MALLOC_FAILURE);
263                                 goto ecerr;
264                         }
265                         if ((eckey->group = EC_GROUP_new_by_nid(
266                              OBJ_obj2nid(a->parameter->value.object))) == NULL)
267                                 goto ecerr;
268                         EC_GROUP_set_asn1_flag(eckey->group, 
269                                                 OPENSSL_EC_NAMED_CURVE);
270                 }
271
272                 /* We have parameters now set private key */
273                 if (!d2i_ECPrivateKey(&eckey, &p_tmp, pkeylen))
274                 {
275                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
276                         goto ecerr;
277                 }
278
279                 /* calculate public key (if necessary) */
280                 if (!eckey->pub_key)
281                 {
282                         /* the public key was not included in the SEC1 private
283                          * key => calculate the public key */
284                         eckey->pub_key = EC_POINT_new(eckey->group);
285                         if (!eckey->pub_key)
286                         {
287                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
288                                 goto ecerr;
289                         }
290                         if (!EC_POINT_copy(eckey->pub_key, 
291                                 EC_GROUP_get0_generator(eckey->group)))
292                         {
293                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
294                                 goto ecerr;
295                         }
296                         if (!EC_POINT_mul(eckey->group, eckey->pub_key, 
297                                 eckey->priv_key, NULL, NULL, ctx))
298                         {
299                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
300                                 goto ecerr;
301                         }
302                 }
303
304                 EVP_PKEY_assign_EC_KEY(pkey, eckey);
305                 if (ctx)
306                         BN_CTX_free(ctx);
307                 break;
308 ecerr:
309                 if (ctx)
310                         BN_CTX_free(ctx);
311                 if (eckey)
312                         EC_KEY_free(eckey);
313                 if (pkey)
314                         EVP_PKEY_free(pkey);
315                 return NULL;
316 #endif
317                 default:
318                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
319                 if (!a->algorithm) BUF_strlcpy (obj_tmp, "NULL", sizeof obj_tmp);
320                 else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
321                 ERR_add_error_data(2, "TYPE=", obj_tmp);
322                 EVP_PKEY_free (pkey);
323                 return NULL;
324         }
325         return pkey;
326 }
327
328 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
329 {
330         return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
331 }
332
333 /* Turn a private key into a PKCS8 structure */
334
335 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
336 {
337         PKCS8_PRIV_KEY_INFO *p8;
338
339         if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {        
340                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
341                 return NULL;
342         }
343         p8->broken = broken;
344         ASN1_INTEGER_set (p8->version, 0);
345         if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
346                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
347                 PKCS8_PRIV_KEY_INFO_free (p8);
348                 return NULL;
349         }
350         p8->pkey->type = V_ASN1_OCTET_STRING;
351         switch (EVP_PKEY_type(pkey->type)) {
352 #ifndef OPENSSL_NO_RSA
353                 case EVP_PKEY_RSA:
354
355                 if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE;
356
357                 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
358                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
359                 if (!ASN1_pack_string ((char *)pkey, i2d_PrivateKey,
360                                          &p8->pkey->value.octet_string)) {
361                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
362                         PKCS8_PRIV_KEY_INFO_free (p8);
363                         return NULL;
364                 }
365                 break;
366 #endif
367 #ifndef OPENSSL_NO_DSA
368                 case EVP_PKEY_DSA:
369                 if(!dsa_pkey2pkcs8(p8, pkey)) {
370                         PKCS8_PRIV_KEY_INFO_free (p8);
371                         return NULL;
372                 }
373
374                 break;
375 #endif
376 #ifndef OPENSSL_NO_EC
377                 case EVP_PKEY_EC:
378                 if (!eckey_pkey2pkcs8(p8, pkey))
379                 {
380                         PKCS8_PRIV_KEY_INFO_free(p8);
381                         return(NULL);
382                 }
383                 break;
384 #endif
385                 default:
386                 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
387                 PKCS8_PRIV_KEY_INFO_free (p8);
388                 return NULL;
389         }
390         RAND_add(p8->pkey->value.octet_string->data,
391                  p8->pkey->value.octet_string->length, 0.0);
392         return p8;
393 }
394
395 PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
396 {
397         switch (broken) {
398
399                 case PKCS8_OK:
400                 p8->broken = PKCS8_OK;
401                 return p8;
402                 break;
403
404                 case PKCS8_NO_OCTET:
405                 p8->broken = PKCS8_NO_OCTET;
406                 p8->pkey->type = V_ASN1_SEQUENCE;
407                 return p8;
408                 break;
409
410                 default:
411                 EVPerr(EVP_F_EVP_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
412                 return NULL;
413                 break;
414                 
415         }
416 }
417
418 #ifndef OPENSSL_NO_DSA
419 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
420 {
421         ASN1_STRING *params;
422         ASN1_INTEGER *prkey;
423         ASN1_TYPE *ttmp;
424         STACK_OF(ASN1_TYPE) *ndsa;
425         unsigned char *p, *q;
426         int len;
427
428         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
429         len = i2d_DSAparams (pkey->pkey.dsa, NULL);
430         if (!(p = OPENSSL_malloc(len))) {
431                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
432                 PKCS8_PRIV_KEY_INFO_free (p8);
433                 return 0;
434         }
435         q = p;
436         i2d_DSAparams (pkey->pkey.dsa, &q);
437         params = ASN1_STRING_new();
438         ASN1_STRING_set(params, p, len);
439         OPENSSL_free(p);
440         /* Get private key into integer */
441         if (!(prkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
442                 EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
443                 return 0;
444         }
445
446         switch(p8->broken) {
447
448                 case PKCS8_OK:
449                 case PKCS8_NO_OCTET:
450
451                 if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER,
452                                          &p8->pkey->value.octet_string)) {
453                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
454                         M_ASN1_INTEGER_free (prkey);
455                         return 0;
456                 }
457
458                 M_ASN1_INTEGER_free (prkey);
459                 p8->pkeyalg->parameter->value.sequence = params;
460                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
461
462                 break;
463
464                 case PKCS8_NS_DB:
465
466                 p8->pkeyalg->parameter->value.sequence = params;
467                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
468                 ndsa = sk_ASN1_TYPE_new_null();
469                 ttmp = ASN1_TYPE_new();
470                 if (!(ttmp->value.integer = BN_to_ASN1_INTEGER (pkey->pkey.dsa->pub_key, NULL))) {
471                         EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
472                         PKCS8_PRIV_KEY_INFO_free(p8);
473                         return 0;
474                 }
475                 ttmp->type = V_ASN1_INTEGER;
476                 sk_ASN1_TYPE_push(ndsa, ttmp);
477
478                 ttmp = ASN1_TYPE_new();
479                 ttmp->value.integer = prkey;
480                 ttmp->type = V_ASN1_INTEGER;
481                 sk_ASN1_TYPE_push(ndsa, ttmp);
482
483                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
484
485                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
486                                          &p8->pkey->value.octet_string->data,
487                                          &p8->pkey->value.octet_string->length)) {
488
489                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
490                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
491                         M_ASN1_INTEGER_free(prkey);
492                         return 0;
493                 }
494                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
495                 break;
496
497                 case PKCS8_EMBEDDED_PARAM:
498
499                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
500                 ndsa = sk_ASN1_TYPE_new_null();
501                 ttmp = ASN1_TYPE_new();
502                 ttmp->value.sequence = params;
503                 ttmp->type = V_ASN1_SEQUENCE;
504                 sk_ASN1_TYPE_push(ndsa, ttmp);
505
506                 ttmp = ASN1_TYPE_new();
507                 ttmp->value.integer = prkey;
508                 ttmp->type = V_ASN1_INTEGER;
509                 sk_ASN1_TYPE_push(ndsa, ttmp);
510
511                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
512
513                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
514                                          &p8->pkey->value.octet_string->data,
515                                          &p8->pkey->value.octet_string->length)) {
516
517                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
518                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
519                         M_ASN1_INTEGER_free (prkey);
520                         return 0;
521                 }
522                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
523                 break;
524         }
525         return 1;
526 }
527 #endif
528
529 #ifndef OPENSSL_NO_EC
530 static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
531 {
532         EC_KEY          *eckey;
533         unsigned char   *p, *pp;
534         int             nid, i, ret = 0;
535         unsigned int    tmp_flags;
536
537         if (pkey->pkey.eckey == NULL || pkey->pkey.eckey->group == NULL)
538         {
539                 EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, EVP_R_MISSING_PARAMETERS);
540                 return 0;
541         }
542         eckey = pkey->pkey.eckey;
543
544         /* set the ec parameters OID */
545         if (p8->pkeyalg->algorithm)
546                 ASN1_OBJECT_free(p8->pkeyalg->algorithm);
547
548         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_X9_62_id_ecPublicKey);
549
550         /* set the ec parameters */
551
552         if (p8->pkeyalg->parameter)
553         {
554                 ASN1_TYPE_free(p8->pkeyalg->parameter);
555                 p8->pkeyalg->parameter = NULL;
556         }
557
558         if ((p8->pkeyalg->parameter = ASN1_TYPE_new()) == NULL)
559         {
560                 EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
561                 return 0;
562         }
563         
564         if (EC_GROUP_get_asn1_flag(eckey->group)
565                      && (nid = EC_GROUP_get_nid(eckey->group)))
566         {
567                 /* we have a 'named curve' => just set the OID */
568                 p8->pkeyalg->parameter->type = V_ASN1_OBJECT;
569                 p8->pkeyalg->parameter->value.object = OBJ_nid2obj(nid);
570         }
571         else    /* explicit parameters */
572         {
573                 if ((i = i2d_ECParameters(eckey, NULL)) == 0)
574                 {
575                         EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
576                         return 0;
577                 }
578                 if ((p = (unsigned char *) OPENSSL_malloc(i)) == NULL)
579                 {
580                         EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
581                         return 0;
582                 }       
583                 pp = p;
584                 if (!i2d_ECParameters(eckey, &pp))
585                 {
586                         EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
587                         OPENSSL_free(p);
588                         return 0;
589                 }
590                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
591                 if ((p8->pkeyalg->parameter->value.sequence 
592                         = ASN1_STRING_new()) == NULL)
593                 {
594                         EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_ASN1_LIB);
595                         OPENSSL_free(p);
596                         return 0;
597                 }
598                 ASN1_STRING_set(p8->pkeyalg->parameter->value.sequence, p, i);
599                 OPENSSL_free(p);
600         }
601
602         /* set the private key */
603
604         /* do not include the parameters in the SEC1 private key
605          * see PKCS#11 12.11 */
606         tmp_flags  = pkey->pkey.eckey->enc_flag;
607         pkey->pkey.eckey->enc_flag |= EC_PKEY_NO_PARAMETERS;
608         i = i2d_ECPrivateKey(pkey->pkey.eckey, NULL);
609         if (!i)
610         {
611                 pkey->pkey.eckey->enc_flag = tmp_flags;
612                 EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
613                 return 0;
614         }
615         p = (unsigned char *) OPENSSL_malloc(i);
616         if (!p)
617         {
618                 pkey->pkey.eckey->enc_flag = tmp_flags;
619                 EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
620                 return 0;
621         }
622         pp = p;
623         if (!i2d_ECPrivateKey(pkey->pkey.eckey, &pp))
624         {
625                 pkey->pkey.eckey->enc_flag = tmp_flags;
626                 EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
627                 OPENSSL_free(p);
628                 return 0;
629         }
630         /* restore old encoding flags */
631         pkey->pkey.eckey->enc_flag = tmp_flags;
632
633         switch(p8->broken) {
634
635                 case PKCS8_OK:
636                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
637                 if (!p8->pkey->value.octet_string ||
638                     !M_ASN1_OCTET_STRING_set(p8->pkey->value.octet_string,
639                     (const void *)p, i))
640
641                 {
642                         EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
643                 }
644                 else
645                         ret = 1;
646                 break;
647                 case PKCS8_NO_OCTET:            /* RSA specific */
648                 case PKCS8_NS_DB:               /* DSA specific */
649                 case PKCS8_EMBEDDED_PARAM:      /* DSA specific */
650                 default:
651                         EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
652         }
653         OPENSSL_cleanse(p, (size_t)i);
654         OPENSSL_free(p);
655         return ret;
656 }
657 #endif