63f408885fd3c152471ebeabccfa57b8a9e83a8e
[openssl.git] / crypto / asn1 / n_pkey.c
1 /* crypto/asn1/n_pkey.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #ifndef NO_RSA
60 #include <stdio.h>
61 #include "cryptlib.h"
62 #include <openssl/rsa.h>
63 #include <openssl/objects.h>
64 #include <openssl/asn1_mac.h>
65 #include <openssl/evp.h>
66 #include <openssl/x509.h>
67
68
69 #ifndef NO_RC4
70
71 typedef struct netscape_pkey_st
72         {
73         ASN1_INTEGER *version;
74         X509_ALGOR *algor;
75         ASN1_OCTET_STRING *private_key;
76         } NETSCAPE_PKEY;
77
78 static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp);
79 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a,unsigned char **pp, long length);
80 static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void);
81 static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *);
82
83 int i2d_Netscape_RSA(RSA *a, unsigned char **pp, int (*cb)())
84         {
85         int i,j,l[6];
86         NETSCAPE_PKEY *pkey;
87         unsigned char buf[256],*zz;
88         unsigned char key[EVP_MAX_KEY_LENGTH];
89         EVP_CIPHER_CTX ctx;
90         X509_ALGOR *alg=NULL;
91         ASN1_OCTET_STRING os,os2;
92         M_ASN1_I2D_vars(a);
93
94         if (a == NULL) return(0);
95
96 #ifdef WIN32
97         r=r; /* shut the damn compiler up :-) */
98 #endif
99
100         os.data=os2.data=NULL;
101         if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
102         if (!ASN1_INTEGER_set(pkey->version,0)) goto err;
103
104         if (pkey->algor->algorithm != NULL)
105                 ASN1_OBJECT_free(pkey->algor->algorithm);
106         pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
107         if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
108         pkey->algor->parameter->type=V_ASN1_NULL;
109
110         l[0]=i2d_RSAPrivateKey(a,NULL);
111         pkey->private_key->length=l[0];
112
113         os2.length=i2d_NETSCAPE_PKEY(pkey,NULL);
114         l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL);
115
116         if ((alg=X509_ALGOR_new()) == NULL) goto err;
117         if (alg->algorithm != NULL)
118                 ASN1_OBJECT_free(alg->algorithm);
119         alg->algorithm=OBJ_nid2obj(NID_rc4);
120         if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
121         alg->parameter->type=V_ASN1_NULL;
122
123         l[2]=i2d_X509_ALGOR(alg,NULL);
124         l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE);
125
126 #ifndef CONST_STRICT
127         os.data=(unsigned char *)"private-key";
128 #endif
129         os.length=11;
130         l[4]=i2d_ASN1_OCTET_STRING(&os,NULL);
131
132         l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE);
133
134         if (pp == NULL)
135                 {
136                 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
137                 if (alg != NULL) X509_ALGOR_free(alg);
138                 return(l[5]);
139                 }
140
141         if (pkey->private_key->data != NULL)
142                 OPENSSL_free(pkey->private_key->data);
143         if ((pkey->private_key->data=(unsigned char *)OPENSSL_malloc(l[0])) == NULL)
144                 {
145                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
146                 goto err;
147                 }
148         zz=pkey->private_key->data;
149         i2d_RSAPrivateKey(a,&zz);
150
151         if ((os2.data=(unsigned char *)OPENSSL_malloc(os2.length)) == NULL)
152                 {
153                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
154                 goto err;
155                 }
156         zz=os2.data;
157         i2d_NETSCAPE_PKEY(pkey,&zz);
158                 
159         if (cb == NULL)
160                 cb=EVP_read_pw_string;
161         i=cb(buf,256,"Enter Private Key password:",1);
162         if (i != 0)
163                 {
164                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ);
165                 goto err;
166                 }
167         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
168                 strlen((char *)buf),1,key,NULL);
169         memset(buf,0,256);
170
171         EVP_CIPHER_CTX_init(&ctx);
172         EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL);
173         EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length);
174         EVP_EncryptFinal(&ctx,&(os2.data[i]),&j);
175         EVP_CIPHER_CTX_cleanup(&ctx);
176
177         p= *pp;
178         ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
179         i2d_ASN1_OCTET_STRING(&os,&p);
180         ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
181         i2d_X509_ALGOR(alg,&p);
182         i2d_ASN1_OCTET_STRING(&os2,&p);
183         ret=l[5];
184 err:
185         if (os2.data != NULL) OPENSSL_free(os2.data);
186         if (alg != NULL) X509_ALGOR_free(alg);
187         if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
188         r=r;
189         return(ret);
190         }
191
192 RSA *d2i_Netscape_RSA(RSA **a, unsigned char **pp, long length, int (*cb)())
193         {
194         RSA *ret=NULL;
195         ASN1_OCTET_STRING *os=NULL;
196         ASN1_CTX c;
197
198         c.pp=pp;
199         c.error=ASN1_R_DECODING_ERROR;
200
201         M_ASN1_D2I_Init();
202         M_ASN1_D2I_start_sequence();
203         M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
204         if ((os->length != 11) || (strncmp("private-key",
205                 (char *)os->data,os->length) != 0))
206                 {
207                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
208                 M_ASN1_BIT_STRING_free(os);
209                 goto err;
210                 }
211         M_ASN1_BIT_STRING_free(os);
212         c.q=c.p;
213         if ((ret=d2i_Netscape_RSA_2(a,&c.p,c.slen,cb)) == NULL) goto err;
214         /* Note: some versions of IIS key files use length values that are
215          * too small for the surrounding SEQUENCEs. This following line
216          * effectively disable length checking.
217          */
218         c.slen = 0;
219
220         M_ASN1_D2I_Finish(a,RSA_free,ASN1_F_D2I_NETSCAPE_RSA);
221         }
222
223 RSA *d2i_Netscape_RSA_2(RSA **a, unsigned char **pp, long length,
224              int (*cb)())
225         {
226         NETSCAPE_PKEY *pkey=NULL;
227         RSA *ret=NULL;
228         int i,j;
229         unsigned char buf[256],*zz;
230         unsigned char key[EVP_MAX_KEY_LENGTH];
231         EVP_CIPHER_CTX ctx;
232         X509_ALGOR *alg=NULL;
233         ASN1_OCTET_STRING *os=NULL;
234         ASN1_CTX c;
235
236         c.error=ERR_R_NESTED_ASN1_ERROR;
237         c.pp=pp;
238
239         M_ASN1_D2I_Init();
240         M_ASN1_D2I_start_sequence();
241         M_ASN1_D2I_get(alg,d2i_X509_ALGOR);
242         if (OBJ_obj2nid(alg->algorithm) != NID_rc4)
243                 {
244                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
245                 goto err;
246                 }
247         M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
248         if (cb == NULL)
249                 cb=EVP_read_pw_string;
250         i=cb(buf,256,"Enter Private Key password:",0);
251         if (i != 0)
252                 {
253                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ);
254                 goto err;
255                 }
256
257         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
258                 strlen((char *)buf),1,key,NULL);
259         memset(buf,0,256);
260
261         EVP_CIPHER_CTX_init(&ctx);
262         EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL);
263         EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
264         EVP_DecryptFinal(&ctx,&(os->data[i]),&j);
265         EVP_CIPHER_CTX_cleanup(&ctx);
266         os->length=i+j;
267
268         zz=os->data;
269
270         if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
271                 {
272                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
273                 goto err;
274                 }
275                 
276         zz=pkey->private_key->data;
277         if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
278                 {
279                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
280                 goto err;
281                 }
282         if (!asn1_Finish(&c)) goto err;
283         *pp=c.p;
284 err:
285         if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
286         if (os != NULL) M_ASN1_BIT_STRING_free(os);
287         if (alg != NULL) X509_ALGOR_free(alg);
288         return(ret);
289         }
290
291 static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp)
292         {
293         M_ASN1_I2D_vars(a);
294
295
296         M_ASN1_I2D_len(a->version,      i2d_ASN1_INTEGER);
297         M_ASN1_I2D_len(a->algor,        i2d_X509_ALGOR);
298         M_ASN1_I2D_len(a->private_key,  i2d_ASN1_OCTET_STRING);
299
300         M_ASN1_I2D_seq_total();
301
302         M_ASN1_I2D_put(a->version,      i2d_ASN1_INTEGER);
303         M_ASN1_I2D_put(a->algor,        i2d_X509_ALGOR);
304         M_ASN1_I2D_put(a->private_key,  i2d_ASN1_OCTET_STRING);
305
306         M_ASN1_I2D_finish();
307         }
308
309 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a, unsigned char **pp,
310              long length)
311         {
312         M_ASN1_D2I_vars(a,NETSCAPE_PKEY *,NETSCAPE_PKEY_new);
313
314         M_ASN1_D2I_Init();
315         M_ASN1_D2I_start_sequence();
316         M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
317         M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
318         M_ASN1_D2I_get(ret->private_key,d2i_ASN1_OCTET_STRING);
319         M_ASN1_D2I_Finish(a,NETSCAPE_PKEY_free,ASN1_F_D2I_NETSCAPE_PKEY);
320         }
321
322 static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void)
323         {
324         NETSCAPE_PKEY *ret=NULL;
325         ASN1_CTX c;
326
327         M_ASN1_New_Malloc(ret,NETSCAPE_PKEY);
328         M_ASN1_New(ret->version,M_ASN1_INTEGER_new);
329         M_ASN1_New(ret->algor,X509_ALGOR_new);
330         M_ASN1_New(ret->private_key,M_ASN1_OCTET_STRING_new);
331         return(ret);
332         M_ASN1_New_Error(ASN1_F_NETSCAPE_PKEY_NEW);
333         }
334
335 static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *a)
336         {
337         if (a == NULL) return;
338         M_ASN1_INTEGER_free(a->version);
339         X509_ALGOR_free(a->algor);
340         M_ASN1_OCTET_STRING_free(a->private_key);
341         OPENSSL_free(a);
342         }
343
344 #endif /* NO_RC4 */
345
346 #else /* !NO_RSA */
347
348 # if PEDANTIC
349 static void *dummy=&dummy;
350 # endif
351
352 #endif