Stop compiler warnings.
[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 #ifndef OPENSSL_NO_RSA
65 #include <openssl/rsa.h>
66 #endif
67 #ifndef OPENSSL_NO_DSA
68 #include <openssl/dsa.h>
69 #endif
70 #include <openssl/bn.h>
71
72 #ifndef OPENSSL_NO_DSA
73 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
74 #endif
75 #ifndef OPENSSL_NO_EC
76 static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
77 #endif
78
79 /* Extract a private key from a PKCS8 structure */
80
81 EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8)
82 {
83         EVP_PKEY *pkey = NULL;
84 #ifndef OPENSSL_NO_RSA
85         RSA *rsa = NULL;
86 #endif
87 #ifndef OPENSSL_NO_DSA
88         DSA *dsa = NULL;
89         ASN1_TYPE *t1, *t2;
90         ASN1_INTEGER *privkey;
91         STACK_OF(ASN1_TYPE) *ndsa = NULL;
92 #endif
93 #ifndef OPENSSL_NO_EC
94         EC_KEY *eckey = NULL;
95         const unsigned char *p_tmp;
96 #endif
97 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
98         ASN1_TYPE    *param = NULL;     
99         BN_CTX *ctx = NULL;
100         int plen;
101 #endif
102         X509_ALGOR *a;
103         const unsigned char *p;
104         const unsigned char *cp;
105         int pkeylen;
106         int  nid;
107         char obj_tmp[80];
108
109         if(p8->pkey->type == V_ASN1_OCTET_STRING) {
110                 p8->broken = PKCS8_OK;
111                 p = p8->pkey->value.octet_string->data;
112                 pkeylen = p8->pkey->value.octet_string->length;
113         } else {
114                 p8->broken = PKCS8_NO_OCTET;
115                 p = p8->pkey->value.sequence->data;
116                 pkeylen = p8->pkey->value.sequence->length;
117         }
118         if (!(pkey = EVP_PKEY_new())) {
119                 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
120                 return NULL;
121         }
122         a = p8->pkeyalg;
123         nid = OBJ_obj2nid(a->algorithm);
124         switch(nid)
125         {
126 #ifndef OPENSSL_NO_RSA
127                 case NID_rsaEncryption:
128                 cp = p;
129                 if (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) {
130                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
131                         return NULL;
132                 }
133                 EVP_PKEY_assign_RSA (pkey, rsa);
134                 break;
135 #endif
136 #ifndef OPENSSL_NO_DSA
137                 case NID_dsa:
138                 /* PKCS#8 DSA is weird: you just get a private key integer
139                  * and parameters in the AlgorithmIdentifier the pubkey must
140                  * be recalculated.
141                  */
142         
143                 /* Check for broken DSA PKCS#8, UGH! */
144                 if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {
145                     if(!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen, 
146                                                           d2i_ASN1_TYPE,
147                                                           ASN1_TYPE_free))) {
148                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
149                         goto dsaerr;
150                     }
151                     if(sk_ASN1_TYPE_num(ndsa) != 2 ) {
152                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
153                         goto dsaerr;
154                     }
155                     /* Handle Two broken types:
156                      * SEQUENCE {parameters, priv_key}
157                      * SEQUENCE {pub_key, priv_key}
158                      */
159
160                     t1 = sk_ASN1_TYPE_value(ndsa, 0);
161                     t2 = sk_ASN1_TYPE_value(ndsa, 1);
162                     if(t1->type == V_ASN1_SEQUENCE) {
163                         p8->broken = PKCS8_EMBEDDED_PARAM;
164                         param = t1;
165                     } else if(a->parameter->type == V_ASN1_SEQUENCE) {
166                         p8->broken = PKCS8_NS_DB;
167                         param = a->parameter;
168                     } else {
169                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
170                         goto dsaerr;
171                     }
172
173                     if(t2->type != V_ASN1_INTEGER) {
174                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
175                         goto dsaerr;
176                     }
177                     privkey = t2->value.integer;
178                 } else {
179                         if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
180                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
181                                 goto dsaerr;
182                         }
183                         param = p8->pkeyalg->parameter;
184                 }
185                 if (!param || (param->type != V_ASN1_SEQUENCE)) {
186                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
187                         goto dsaerr;
188                 }
189                 cp = p = param->value.sequence->data;
190                 plen = param->value.sequence->length;
191                 if (!(dsa = d2i_DSAparams (NULL, &cp, plen))) {
192                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
193                         goto dsaerr;
194                 }
195                 /* We have parameters now set private key */
196                 if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
197                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
198                         goto dsaerr;
199                 }
200                 /* Calculate public key (ouch!) */
201                 if (!(dsa->pub_key = BN_new())) {
202                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
203                         goto dsaerr;
204                 }
205                 if (!(ctx = BN_CTX_new())) {
206                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
207                         goto dsaerr;
208                 }
209                         
210                 if (!BN_mod_exp(dsa->pub_key, dsa->g,
211                                                  dsa->priv_key, dsa->p, ctx)) {
212                         
213                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);
214                         goto dsaerr;
215                 }
216
217                 EVP_PKEY_assign_DSA(pkey, dsa);
218                 BN_CTX_free (ctx);
219                 if(ndsa) sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
220                 else ASN1_INTEGER_free(privkey);
221                 break;
222                 dsaerr:
223                 BN_CTX_free (ctx);
224                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
225                 DSA_free(dsa);
226                 EVP_PKEY_free(pkey);
227                 return NULL;
228                 break;
229 #endif
230 #ifndef OPENSSL_NO_EC
231                 case NID_X9_62_id_ecPublicKey:
232                 p_tmp = p;
233                 /* extract the ec parameters */
234                 param = p8->pkeyalg->parameter;
235
236                 if (!param || ((param->type != V_ASN1_SEQUENCE) &&
237                     (param->type != V_ASN1_OBJECT)))
238                 {
239                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
240                         goto ecerr;
241                 }
242
243                 if (param->type == V_ASN1_SEQUENCE)
244                 {
245                         cp = p = param->value.sequence->data;
246                         plen = param->value.sequence->length;
247
248                         if (!(eckey = d2i_ECParameters(NULL, &cp, plen)))
249                         {
250                                 EVPerr(EVP_F_EVP_PKCS82PKEY,
251                                         EVP_R_DECODE_ERROR);
252                                 goto ecerr;
253                         }
254                 }
255                 else
256                 {
257                         EC_GROUP *group;
258                         cp = p = param->value.object->data;
259                         plen = param->value.object->length;
260
261                         /* type == V_ASN1_OBJECT => the parameters are given
262                          * by an asn1 OID
263                          */
264                         if ((eckey = EC_KEY_new()) == NULL)
265                         {
266                                 EVPerr(EVP_F_EVP_PKCS82PKEY,
267                                         ERR_R_MALLOC_FAILURE);
268                                 goto ecerr;
269                         }
270                         group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(a->parameter->value.object));
271                         if (group == NULL)
272                                 goto ecerr;
273                         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
274                         if (EC_KEY_set_group(eckey, group) == 0)
275                                 goto ecerr;
276                         EC_GROUP_free(group);
277                 }
278
279                 /* We have parameters now set private key */
280                 if (!d2i_ECPrivateKey(&eckey, &p_tmp, pkeylen))
281                 {
282                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
283                         goto ecerr;
284                 }
285
286                 /* calculate public key (if necessary) */
287                 if (EC_KEY_get0_public_key(eckey) == NULL)
288                 {
289                         const BIGNUM *priv_key;
290                         const EC_GROUP *group;
291                         EC_POINT *pub_key;
292                         /* the public key was not included in the SEC1 private
293                          * key => calculate the public key */
294                         group   = EC_KEY_get0_group(eckey);
295                         pub_key = EC_POINT_new(group);
296                         if (pub_key == NULL)
297                         {
298                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
299                                 goto ecerr;
300                         }
301                         if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group)))
302                         {
303                                 EC_POINT_free(pub_key);
304                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
305                                 goto ecerr;
306                         }
307                         priv_key = EC_KEY_get0_private_key(eckey);
308                         if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL))
309                         {
310                                 EC_POINT_free(pub_key);
311                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
312                                 goto ecerr;
313                         }
314                         if (EC_KEY_set_public_key(eckey, pub_key) == 0)
315                         {
316                                 EC_POINT_free(pub_key);
317                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
318                                 goto ecerr;
319                         }
320                         EC_POINT_free(pub_key);
321                 }
322
323                 EVP_PKEY_assign_EC_KEY(pkey, eckey);
324                 break;
325 ecerr:
326                 if (eckey)
327                         EC_KEY_free(eckey);
328                 if (pkey)
329                         EVP_PKEY_free(pkey);
330                 return NULL;
331 #endif
332                 default:
333                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
334                 if (!a->algorithm) BUF_strlcpy (obj_tmp, "NULL", sizeof obj_tmp);
335                 else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
336                 ERR_add_error_data(2, "TYPE=", obj_tmp);
337                 EVP_PKEY_free (pkey);
338                 return NULL;
339         }
340         return pkey;
341 }
342
343 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
344 {
345         return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
346 }
347
348 /* Turn a private key into a PKCS8 structure */
349
350 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
351 {
352         PKCS8_PRIV_KEY_INFO *p8;
353
354         if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {        
355                 EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN,ERR_R_MALLOC_FAILURE);
356                 return NULL;
357         }
358         p8->broken = broken;
359         if (!ASN1_INTEGER_set(p8->version, 0)) {
360                 EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN,ERR_R_MALLOC_FAILURE);
361                 PKCS8_PRIV_KEY_INFO_free (p8);
362                 return NULL;
363         }
364         if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
365                 EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN,ERR_R_MALLOC_FAILURE);
366                 PKCS8_PRIV_KEY_INFO_free (p8);
367                 return NULL;
368         }
369         p8->pkey->type = V_ASN1_OCTET_STRING;
370         switch (EVP_PKEY_type(pkey->type)) {
371 #ifndef OPENSSL_NO_RSA
372                 case EVP_PKEY_RSA:
373
374                 if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE;
375
376                 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
377                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
378                 if (!ASN1_pack_string_of (EVP_PKEY,pkey, i2d_PrivateKey,
379                                          &p8->pkey->value.octet_string)) {
380                         EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN,ERR_R_MALLOC_FAILURE);
381                         PKCS8_PRIV_KEY_INFO_free (p8);
382                         return NULL;
383                 }
384                 break;
385 #endif
386 #ifndef OPENSSL_NO_DSA
387                 case EVP_PKEY_DSA:
388                 if(!dsa_pkey2pkcs8(p8, pkey)) {
389                         PKCS8_PRIV_KEY_INFO_free (p8);
390                         return NULL;
391                 }
392
393                 break;
394 #endif
395 #ifndef OPENSSL_NO_EC
396                 case EVP_PKEY_EC:
397                 if (!eckey_pkey2pkcs8(p8, pkey))
398                 {
399                         PKCS8_PRIV_KEY_INFO_free(p8);
400                         return(NULL);
401                 }
402                 break;
403 #endif
404                 default:
405                 EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
406                 PKCS8_PRIV_KEY_INFO_free (p8);
407                 return NULL;
408         }
409         RAND_add(p8->pkey->value.octet_string->data,
410                  p8->pkey->value.octet_string->length, 0.0);
411         return p8;
412 }
413
414 PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
415 {
416         switch (broken) {
417
418                 case PKCS8_OK:
419                 p8->broken = PKCS8_OK;
420                 return p8;
421                 break;
422
423                 case PKCS8_NO_OCTET:
424                 p8->broken = PKCS8_NO_OCTET;
425                 p8->pkey->type = V_ASN1_SEQUENCE;
426                 return p8;
427                 break;
428
429                 default:
430                 EVPerr(EVP_F_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
431                 return NULL;
432         }
433 }
434
435 #ifndef OPENSSL_NO_DSA
436 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
437 {
438         ASN1_STRING *params = NULL;
439         ASN1_INTEGER *prkey = NULL;
440         ASN1_TYPE *ttmp = NULL;
441         STACK_OF(ASN1_TYPE) *ndsa = NULL;
442         unsigned char *p = NULL, *q;
443         int len;
444
445         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
446         len = i2d_DSAparams (pkey->pkey.dsa, NULL);
447         if (!(p = OPENSSL_malloc(len))) {
448                 EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
449                 goto err;
450         }
451         q = p;
452         i2d_DSAparams (pkey->pkey.dsa, &q);
453         if (!(params = ASN1_STRING_new())) {
454                 EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
455                 goto err;
456         }
457         if (!ASN1_STRING_set(params, p, len)) {
458                 EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
459                 goto err;
460         }
461         OPENSSL_free(p);
462         p = NULL;
463         /* Get private key into integer */
464         if (!(prkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
465                 EVPerr(EVP_F_DSA_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
466                 goto err;
467         }
468
469         switch(p8->broken) {
470
471                 case PKCS8_OK:
472                 case PKCS8_NO_OCTET:
473
474                 if (!ASN1_pack_string_of(ASN1_INTEGER,prkey, i2d_ASN1_INTEGER,
475                                          &p8->pkey->value.octet_string)) {
476                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
477                         goto err;
478                 }
479
480                 M_ASN1_INTEGER_free (prkey);
481                 prkey = NULL;
482                 p8->pkeyalg->parameter->value.sequence = params;
483                 params = NULL;
484                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
485
486                 break;
487
488                 case PKCS8_NS_DB:
489
490                 p8->pkeyalg->parameter->value.sequence = params;
491                 params = NULL;
492                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
493                 if (!(ndsa = sk_ASN1_TYPE_new_null())) {
494                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
495                         goto err;
496                 }
497                 if (!(ttmp = ASN1_TYPE_new())) {
498                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
499                         goto err;
500                 }
501                 if (!(ttmp->value.integer =
502                         BN_to_ASN1_INTEGER(pkey->pkey.dsa->pub_key, NULL))) {
503                         EVPerr(EVP_F_DSA_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
504                         goto err;
505                 }
506                 ttmp->type = V_ASN1_INTEGER;
507                 if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
508                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
509                         goto err;
510                 }
511
512                 if (!(ttmp = ASN1_TYPE_new())) {
513                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
514                         goto err;
515                 }
516                 ttmp->value.integer = prkey;
517                 prkey = NULL;
518                 ttmp->type = V_ASN1_INTEGER;
519                 if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
520                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
521                         goto err;
522                 }
523                 ttmp = NULL;
524
525                 if (!(p8->pkey->value.octet_string = ASN1_OCTET_STRING_new())) {
526                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
527                         goto err;
528                 }
529
530                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
531                                          &p8->pkey->value.octet_string->data,
532                                          &p8->pkey->value.octet_string->length)) {
533
534                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
535                         goto err;
536                 }
537                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
538                 break;
539
540                 case PKCS8_EMBEDDED_PARAM:
541
542                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
543                 if (!(ndsa = sk_ASN1_TYPE_new_null())) {
544                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
545                         goto err;
546                 }
547                 if (!(ttmp = ASN1_TYPE_new())) {
548                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
549                         goto err;
550                 }
551                 ttmp->value.sequence = params;
552                 params = NULL;
553                 ttmp->type = V_ASN1_SEQUENCE;
554                 if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
555                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
556                         goto err;
557                 }
558
559                 if (!(ttmp = ASN1_TYPE_new())) {
560                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
561                         goto err;
562                 }
563                 ttmp->value.integer = prkey;
564                 prkey = NULL;
565                 ttmp->type = V_ASN1_INTEGER;
566                 if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
567                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
568                         goto err;
569                 }
570                 ttmp = NULL;
571
572                 if (!(p8->pkey->value.octet_string = ASN1_OCTET_STRING_new())) {
573                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
574                         goto err;
575                 }
576
577                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
578                                          &p8->pkey->value.octet_string->data,
579                                          &p8->pkey->value.octet_string->length)) {
580
581                         EVPerr(EVP_F_DSA_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
582                         goto err;
583                 }
584                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
585                 break;
586         }
587         return 1;
588 err:
589         if (p != NULL) OPENSSL_free(p);
590         if (params != NULL) ASN1_STRING_free(params);
591         if (prkey != NULL) M_ASN1_INTEGER_free(prkey);
592         if (ttmp != NULL) ASN1_TYPE_free(ttmp);
593         if (ndsa != NULL) sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
594         return 0;
595 }
596 #endif
597
598 #ifndef OPENSSL_NO_EC
599 static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
600 {
601         EC_KEY          *ec_key;
602         const EC_GROUP  *group;
603         unsigned char   *p, *pp;
604         int             nid, i, ret = 0;
605         unsigned int    tmp_flags, old_flags;
606
607         ec_key = pkey->pkey.ec;
608         if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) 
609         {
610                 EVPerr(EVP_F_ECKEY_PKEY2PKCS8, EVP_R_MISSING_PARAMETERS);
611                 return 0;
612         }
613
614         /* set the ec parameters OID */
615         if (p8->pkeyalg->algorithm)
616                 ASN1_OBJECT_free(p8->pkeyalg->algorithm);
617
618         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_X9_62_id_ecPublicKey);
619
620         /* set the ec parameters */
621
622         if (p8->pkeyalg->parameter)
623         {
624                 ASN1_TYPE_free(p8->pkeyalg->parameter);
625                 p8->pkeyalg->parameter = NULL;
626         }
627
628         if ((p8->pkeyalg->parameter = ASN1_TYPE_new()) == NULL)
629         {
630                 EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
631                 return 0;
632         }
633         
634         if (EC_GROUP_get_asn1_flag(group)
635                      && (nid = EC_GROUP_get_curve_name(group)))
636         {
637                 /* we have a 'named curve' => just set the OID */
638                 p8->pkeyalg->parameter->type = V_ASN1_OBJECT;
639                 p8->pkeyalg->parameter->value.object = OBJ_nid2obj(nid);
640         }
641         else    /* explicit parameters */
642         {
643                 if ((i = i2d_ECParameters(ec_key, NULL)) == 0)
644                 {
645                         EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
646                         return 0;
647                 }
648                 if ((p = (unsigned char *) OPENSSL_malloc(i)) == NULL)
649                 {
650                         EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
651                         return 0;
652                 }       
653                 pp = p;
654                 if (!i2d_ECParameters(ec_key, &pp))
655                 {
656                         EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
657                         OPENSSL_free(p);
658                         return 0;
659                 }
660                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
661                 if ((p8->pkeyalg->parameter->value.sequence 
662                         = ASN1_STRING_new()) == NULL)
663                 {
664                         EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_ASN1_LIB);
665                         OPENSSL_free(p);
666                         return 0;
667                 }
668                 ASN1_STRING_set(p8->pkeyalg->parameter->value.sequence, p, i);
669                 OPENSSL_free(p);
670         }
671
672         /* set the private key */
673
674         /* do not include the parameters in the SEC1 private key
675          * see PKCS#11 12.11 */
676         old_flags = EC_KEY_get_enc_flags(pkey->pkey.ec);
677         tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
678         EC_KEY_set_enc_flags(pkey->pkey.ec, tmp_flags);
679         i = i2d_ECPrivateKey(pkey->pkey.ec, NULL);
680         if (!i)
681         {
682                 EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
683                 EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
684                 return 0;
685         }
686         p = (unsigned char *) OPENSSL_malloc(i);
687         if (!p)
688         {
689                 EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
690                 EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
691                 return 0;
692         }
693         pp = p;
694         if (!i2d_ECPrivateKey(pkey->pkey.ec, &pp))
695         {
696                 EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
697                 EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
698                 OPENSSL_free(p);
699                 return 0;
700         }
701         /* restore old encoding flags */
702         EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
703
704         switch(p8->broken) {
705
706                 case PKCS8_OK:
707                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
708                 if (!p8->pkey->value.octet_string ||
709                     !M_ASN1_OCTET_STRING_set(p8->pkey->value.octet_string,
710                     (const void *)p, i))
711
712                 {
713                         EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
714                 }
715                 else
716                         ret = 1;
717                 break;
718                 case PKCS8_NO_OCTET:            /* RSA specific */
719                 case PKCS8_NS_DB:               /* DSA specific */
720                 case PKCS8_EMBEDDED_PARAM:      /* DSA specific */
721                 default:
722                         EVPerr(EVP_F_ECKEY_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
723         }
724         OPENSSL_cleanse(p, (size_t)i);
725         OPENSSL_free(p);
726         return ret;
727 }
728 #endif
729
730 /* EVP_PKEY attribute functions */
731
732 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
733 {
734         return X509at_get_attr_count(key->attributes);
735 }
736
737 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid,
738                           int lastpos)
739 {
740         return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
741 }
742
743 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, ASN1_OBJECT *obj,
744                           int lastpos)
745 {
746         return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
747 }
748
749 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
750 {
751         return X509at_get_attr(key->attributes, loc);
752 }
753
754 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
755 {
756         return X509at_delete_attr(key->attributes, loc);
757 }
758
759 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
760 {
761         if(X509at_add1_attr(&key->attributes, attr)) return 1;
762         return 0;
763 }
764
765 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
766                         const ASN1_OBJECT *obj, int type,
767                         const unsigned char *bytes, int len)
768 {
769         if(X509at_add1_attr_by_OBJ(&key->attributes, obj,
770                                 type, bytes, len)) return 1;
771         return 0;
772 }
773
774 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
775                         int nid, int type,
776                         const unsigned char *bytes, int len)
777 {
778         if(X509at_add1_attr_by_NID(&key->attributes, nid,
779                                 type, bytes, len)) return 1;
780         return 0;
781 }
782
783 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
784                         const char *attrname, int type,
785                         const unsigned char *bytes, int len)
786 {
787         if(X509at_add1_attr_by_txt(&key->attributes, attrname,
788                                 type, bytes, len)) return 1;
789         return 0;
790 }