Add type-safe STACKs and SETs.
[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 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 "x509.h"
63 #include "rand.h"
64
65 /* Extract a private key from a PKCS8 structure */
66
67 EVP_PKEY *EVP_PKCS82PKEY (p8)
68 PKCS8_PRIV_KEY_INFO *p8;
69 {
70         EVP_PKEY *pkey;
71         RSA *rsa;
72         DSA *dsa;
73         ASN1_INTEGER *dsapriv;
74         X509_ALGOR *a;
75         STACK *ndsa;
76         BN_CTX *ctx;
77         unsigned char *p;
78         int plen, pkeylen;
79         char obj_tmp[80];
80
81         switch (p8->broken) {
82                 case PKCS8_OK:
83                 p = p8->pkey->value.octet_string->data;
84                 pkeylen = p8->pkey->value.octet_string->length;
85                 break;
86
87                 case PKCS8_NO_OCTET:
88                 p = p8->pkey->value.sequence->data;
89                 pkeylen = p8->pkey->value.sequence->length;
90                 break;
91
92                 default:
93                 EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
94                 return NULL;
95                 break;
96         }
97         if (!(pkey = EVP_PKEY_new())) {
98                 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
99                 return NULL;
100         }
101         a = p8->pkeyalg;
102         switch (OBJ_obj2nid(a->algorithm))
103         {
104                 case NID_rsaEncryption:
105                 if (!(rsa = d2i_RSAPrivateKey (NULL, &p, pkeylen))) {
106                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
107                         return NULL;
108                 }
109                 EVP_PKEY_assign_RSA (pkey, rsa);
110                 break;
111                 
112                 case NID_dsa:
113                 /* PKCS#8 DSA is weird: you just get a private key integer
114                  * and parameters in the AlgorithmIdentifier the pubkey must
115                  * be recalculated.
116                  */
117         
118                 /* Check for broken Netscape Database DSA PKCS#8, UGH! */
119                 if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {
120                     if(!(ndsa = ASN1_seq_unpack(p, pkeylen, 
121                                         (char *(*)())d2i_ASN1_INTEGER,
122                                                          ASN1_STRING_free))) {
123                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
124                         return NULL;
125                     }
126                     if(sk_num(ndsa) != 2 ) {
127                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
128                         sk_pop_free(ndsa, ASN1_STRING_free);
129                         return NULL;
130                     }
131                     dsapriv = (ASN1_INTEGER *) sk_pop(ndsa);
132                     sk_pop_free(ndsa, ASN1_STRING_free);
133                 } else if (!(dsapriv=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
134                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
135                         return NULL;
136                 }
137                 /* Retrieve parameters */
138                 if (a->parameter->type != V_ASN1_SEQUENCE) {
139                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_NO_DSA_PARAMETERS);
140                         return NULL;
141                 }
142                 p = a->parameter->value.sequence->data;
143                 plen = a->parameter->value.sequence->length;
144                 if (!(dsa = d2i_DSAparams (NULL, &p, plen))) {
145                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
146                         return NULL;
147                 }
148                 /* We have parameters now set private key */
149                 if (!(dsa->priv_key = ASN1_INTEGER_to_BN(dsapriv, NULL))) {
150                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
151                         DSA_free (dsa);
152                         return NULL;
153                 }
154                 /* Calculate public key (ouch!) */
155                 if (!(dsa->pub_key = BN_new())) {
156                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
157                         DSA_free (dsa);
158                         return NULL;
159                 }
160                 if (!(ctx = BN_CTX_new())) {
161                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
162                         DSA_free (dsa);
163                         return NULL;
164                 }
165                         
166                 if (!BN_mod_exp(dsa->pub_key, dsa->g,
167                                                  dsa->priv_key, dsa->p, ctx)) {
168                         
169                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);
170                         BN_CTX_free (ctx);
171                         DSA_free (dsa);
172                         return NULL;
173                 }
174
175                 EVP_PKEY_assign_DSA (pkey, dsa);
176                 BN_CTX_free (ctx);
177                 break;
178
179                 default:
180                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
181                 if (!a->algorithm) strcpy (obj_tmp, "NULL");
182                 else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
183                 ERR_add_error_data(2, "TYPE=", obj_tmp);
184                 EVP_PKEY_free (pkey);
185                 return NULL;
186         }
187         return pkey;
188 }
189
190 /* Turn a private key into a PKCS8 structure */
191
192 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(pkey)
193 EVP_PKEY *pkey;
194 {
195         PKCS8_PRIV_KEY_INFO *p8;
196         ASN1_INTEGER *dpkey;
197         unsigned char *p, *q;
198         int len;
199         if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {        
200                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
201                 return NULL;
202         }
203         ASN1_INTEGER_set (p8->version, 0);
204         if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
205                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
206                 PKCS8_PRIV_KEY_INFO_free (p8);
207                 return NULL;
208         }
209         switch (EVP_PKEY_type(pkey->type)) {
210                 case EVP_PKEY_RSA:
211
212                 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
213                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
214                 if (!ASN1_pack_string ((char *)pkey, i2d_PrivateKey,
215                                          &p8->pkey->value.octet_string)) {
216                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
217                         PKCS8_PRIV_KEY_INFO_free (p8);
218                         return NULL;
219                 }
220                 break;
221
222                 case EVP_PKEY_DSA:
223                 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
224
225                 /* get paramaters and place in AlgorithmIdentifier */
226                 len = i2d_DSAparams (pkey->pkey.dsa, NULL);
227                 if (!(p = Malloc(len))) {
228                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
229                         PKCS8_PRIV_KEY_INFO_free (p8);
230                         return NULL;
231                 }
232                 q = p;
233                 i2d_DSAparams (pkey->pkey.dsa, &q);
234                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
235                 p8->pkeyalg->parameter->value.sequence = ASN1_STRING_new();
236                 ASN1_STRING_set(p8->pkeyalg->parameter->value.sequence, p, len);
237                 Free(p);
238                 /* Get private key into an integer and pack */
239                 if (!(dpkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
240                         EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
241                         PKCS8_PRIV_KEY_INFO_free (p8);
242                         return NULL;
243                 }
244                 
245                 if (!ASN1_pack_string((char *)dpkey, i2d_ASN1_INTEGER,
246                                          &p8->pkey->value.octet_string)) {
247                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
248                         ASN1_INTEGER_free (dpkey);
249                         PKCS8_PRIV_KEY_INFO_free (p8);
250                         return NULL;
251                 }
252                 ASN1_INTEGER_free (dpkey);
253                 break;
254
255                 default:
256                 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
257                 PKCS8_PRIV_KEY_INFO_free (p8);
258                 return NULL;
259         }
260         p8->pkey->type = V_ASN1_OCTET_STRING;
261         RAND_seed (p8->pkey->value.octet_string->data,
262                                          p8->pkey->value.octet_string->length);
263         return p8;
264 }
265
266 PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(p8, broken)
267 PKCS8_PRIV_KEY_INFO *p8;
268 int broken;
269 {
270         switch (broken) {
271
272                 case PKCS8_OK:
273                 p8->broken = PKCS8_OK;
274                 return p8;
275                 break;
276
277                 case PKCS8_NO_OCTET:
278                 p8->broken = PKCS8_NO_OCTET;
279                 p8->pkey->type = V_ASN1_SEQUENCE;
280                 return p8;
281                 break;
282
283                 default:
284                 EVPerr(EVP_F_EVP_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
285                 return NULL;
286                 break;
287                 
288         }
289 }
290
291