Constify the RSA parts of the ASN.1 library. Note some ugly casts
[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(const RSA *a, unsigned char **pp, int (*cb)())
84 {
85         return i2d_RSA_NET(a, pp, cb, 0);
86 }
87
88 int i2d_RSA_NET(const RSA *a, unsigned char **pp, int (*cb)(), int sgckey)
89         {
90         int i,j,l[6];
91         NETSCAPE_PKEY *pkey;
92         unsigned char buf[256],*zz;
93         unsigned char key[EVP_MAX_KEY_LENGTH];
94         EVP_CIPHER_CTX ctx;
95         X509_ALGOR *alg=NULL;
96         ASN1_OCTET_STRING os,os2;
97         M_ASN1_I2D_vars(a);
98
99         if (a == NULL) return(0);
100
101 #ifdef WIN32
102         r=r; /* shut the damn compiler up :-) */
103 #endif
104
105         os.data=os2.data=NULL;
106         if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
107         if (!ASN1_INTEGER_set(pkey->version,0)) goto err;
108
109         if (pkey->algor->algorithm != NULL)
110                 ASN1_OBJECT_free(pkey->algor->algorithm);
111         pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
112         if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
113         pkey->algor->parameter->type=V_ASN1_NULL;
114
115         l[0]=i2d_RSAPrivateKey(a,NULL);
116         pkey->private_key->length=l[0];
117
118         os2.length=i2d_NETSCAPE_PKEY(pkey,NULL);
119         l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL);
120
121         if ((alg=X509_ALGOR_new()) == NULL) goto err;
122         if (alg->algorithm != NULL)
123                 ASN1_OBJECT_free(alg->algorithm);
124         alg->algorithm=OBJ_nid2obj(NID_rc4);
125         if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
126         alg->parameter->type=V_ASN1_NULL;
127
128         l[2]=i2d_X509_ALGOR(alg,NULL);
129         l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE);
130
131 #ifndef CONST_STRICT
132         os.data=(unsigned char *)"private-key";
133 #endif
134         os.length=11;
135         l[4]=i2d_ASN1_OCTET_STRING(&os,NULL);
136
137         l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE);
138
139         if (pp == NULL)
140                 {
141                 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
142                 if (alg != NULL) X509_ALGOR_free(alg);
143                 return(l[5]);
144                 }
145
146         if (pkey->private_key->data != NULL)
147                 OPENSSL_free(pkey->private_key->data);
148         if ((pkey->private_key->data=(unsigned char *)OPENSSL_malloc(l[0])) == NULL)
149                 {
150                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
151                 goto err;
152                 }
153         zz=pkey->private_key->data;
154         i2d_RSAPrivateKey(a,&zz);
155
156         if ((os2.data=(unsigned char *)OPENSSL_malloc(os2.length)) == NULL)
157                 {
158                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
159                 goto err;
160                 }
161         zz=os2.data;
162         i2d_NETSCAPE_PKEY(pkey,&zz);
163                 
164         if (cb == NULL)
165                 cb=EVP_read_pw_string;
166         i=cb(buf,256,"Enter Private Key password:",1);
167         if (i != 0)
168                 {
169                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ);
170                 goto err;
171                 }
172         i = strlen((char *)buf);
173         /* If the key is used for SGC the algorithm is modified a little. */
174         if(sgckey){
175                 EVP_MD_CTX mctx;
176                 EVP_DigestInit(&mctx, EVP_md5());
177                 EVP_DigestUpdate(&mctx, buf, i);
178                 EVP_DigestFinal(&mctx, buf, NULL);
179                 memcpy(buf + 16, "SGCKEYSALT", 10);
180                 i = 26;
181         }
182                 
183         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
184         memset(buf,0,256);
185
186         EVP_CIPHER_CTX_init(&ctx);
187         EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL);
188         EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length);
189         EVP_EncryptFinal(&ctx,&(os2.data[i]),&j);
190         EVP_CIPHER_CTX_cleanup(&ctx);
191
192         p= *pp;
193         ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
194         i2d_ASN1_OCTET_STRING(&os,&p);
195         ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
196         i2d_X509_ALGOR(alg,&p);
197         i2d_ASN1_OCTET_STRING(&os2,&p);
198         ret=l[5];
199 err:
200         if (os2.data != NULL) OPENSSL_free(os2.data);
201         if (alg != NULL) X509_ALGOR_free(alg);
202         if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
203         r=r;
204         return(ret);
205         }
206
207
208 RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, int (*cb)())
209 {
210         return d2i_RSA_NET(a, pp, length, cb, 0);
211 }
212
213 RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, int (*cb)(), int sgckey)
214         {
215         RSA *ret=NULL;
216         ASN1_OCTET_STRING *os=NULL;
217         ASN1_CTX c;
218
219         c.pp=(unsigned char **)pp; /* TMP UGLY CAST */
220         c.error=ASN1_R_DECODING_ERROR;
221
222         M_ASN1_D2I_Init();
223         M_ASN1_D2I_start_sequence();
224         M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
225         if ((os->length != 11) || (strncmp("private-key",
226                 (char *)os->data,os->length) != 0))
227                 {
228                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
229                 M_ASN1_BIT_STRING_free(os);
230                 goto err;
231                 }
232         M_ASN1_BIT_STRING_free(os);
233         c.q=c.p;
234         if ((ret=d2i_RSA_NET_2(a,(const unsigned char **)&c.p, /* TMP UGLY CAST */
235                                c.slen,cb, sgckey)) == NULL) goto err;
236         /* Note: some versions of IIS key files use length values that are
237          * too small for the surrounding SEQUENCEs. This following line
238          * effectively disable length checking.
239          */
240         c.slen = 0;
241
242         M_ASN1_D2I_Finish(a,RSA_free,ASN1_F_D2I_NETSCAPE_RSA);
243         }
244
245 RSA *d2i_Netscape_RSA_2(RSA **a, const unsigned char **pp, long length,
246              int (*cb)())
247 {
248         return d2i_RSA_NET_2(a, pp, length, cb, 0);
249 }
250
251 RSA *d2i_RSA_NET_2(RSA **a, const unsigned char **pp, long length,
252              int (*cb)(), int sgckey)
253         {
254         NETSCAPE_PKEY *pkey=NULL;
255         RSA *ret=NULL;
256         int i,j;
257         unsigned char buf[256],*zz;
258         unsigned char key[EVP_MAX_KEY_LENGTH];
259         EVP_CIPHER_CTX ctx;
260         X509_ALGOR *alg=NULL;
261         ASN1_OCTET_STRING *os=NULL;
262         ASN1_CTX c;
263
264         c.error=ERR_R_NESTED_ASN1_ERROR;
265         c.pp=(unsigned char **)pp;
266
267         M_ASN1_D2I_Init();
268         M_ASN1_D2I_start_sequence();
269         M_ASN1_D2I_get(alg,d2i_X509_ALGOR);
270         if (OBJ_obj2nid(alg->algorithm) != NID_rc4)
271                 {
272                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
273                 goto err;
274                 }
275         M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
276         if (cb == NULL)
277                 cb=EVP_read_pw_string;
278         i=cb(buf,256,"Enter Private Key password:",0);
279         if (i != 0)
280                 {
281                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ);
282                 goto err;
283                 }
284
285         i = strlen((char *)buf);
286         if(sgckey){
287                 EVP_MD_CTX mctx;
288                 EVP_DigestInit(&mctx, EVP_md5());
289                 EVP_DigestUpdate(&mctx, buf, i);
290                 EVP_DigestFinal(&mctx, buf, NULL);
291                 memcpy(buf + 16, "SGCKEYSALT", 10);
292                 i = 26;
293         }
294                 
295         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
296         memset(buf,0,256);
297
298         EVP_CIPHER_CTX_init(&ctx);
299         EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL);
300         EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
301         EVP_DecryptFinal(&ctx,&(os->data[i]),&j);
302         EVP_CIPHER_CTX_cleanup(&ctx);
303         os->length=i+j;
304
305         zz=os->data;
306
307         if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
308                 {
309                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
310                 goto err;
311                 }
312                 
313         zz=pkey->private_key->data;
314         if ((ret=d2i_RSAPrivateKey(a,(const unsigned char **)&zz, /* TMP UGLY CAST */
315                 pkey->private_key->length)) == NULL)
316                 {
317                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
318                 goto err;
319                 }
320         if (!asn1_Finish(&c)) goto err;
321         *pp=c.p;
322 err:
323         if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
324         if (os != NULL) M_ASN1_BIT_STRING_free(os);
325         if (alg != NULL) X509_ALGOR_free(alg);
326         return(ret);
327         }
328
329 static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp)
330         {
331         M_ASN1_I2D_vars(a);
332
333
334         M_ASN1_I2D_len(a->version,      i2d_ASN1_INTEGER);
335         M_ASN1_I2D_len(a->algor,        i2d_X509_ALGOR);
336         M_ASN1_I2D_len(a->private_key,  i2d_ASN1_OCTET_STRING);
337
338         M_ASN1_I2D_seq_total();
339
340         M_ASN1_I2D_put(a->version,      i2d_ASN1_INTEGER);
341         M_ASN1_I2D_put(a->algor,        i2d_X509_ALGOR);
342         M_ASN1_I2D_put(a->private_key,  i2d_ASN1_OCTET_STRING);
343
344         M_ASN1_I2D_finish();
345         }
346
347 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a, unsigned char **pp,
348              long length)
349         {
350         M_ASN1_D2I_vars(a,NETSCAPE_PKEY *,NETSCAPE_PKEY_new);
351
352         M_ASN1_D2I_Init();
353         M_ASN1_D2I_start_sequence();
354         M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
355         M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
356         M_ASN1_D2I_get(ret->private_key,d2i_ASN1_OCTET_STRING);
357         M_ASN1_D2I_Finish(a,NETSCAPE_PKEY_free,ASN1_F_D2I_NETSCAPE_PKEY);
358         }
359
360 static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void)
361         {
362         NETSCAPE_PKEY *ret=NULL;
363         ASN1_CTX c;
364
365         M_ASN1_New_Malloc(ret,NETSCAPE_PKEY);
366         M_ASN1_New(ret->version,M_ASN1_INTEGER_new);
367         M_ASN1_New(ret->algor,X509_ALGOR_new);
368         M_ASN1_New(ret->private_key,M_ASN1_OCTET_STRING_new);
369         return(ret);
370         M_ASN1_New_Error(ASN1_F_NETSCAPE_PKEY_NEW);
371         }
372
373 static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *a)
374         {
375         if (a == NULL) return;
376         M_ASN1_INTEGER_free(a->version);
377         X509_ALGOR_free(a->algor);
378         M_ASN1_OCTET_STRING_free(a->private_key);
379         OPENSSL_free(a);
380         }
381
382 #endif /* NO_RC4 */
383
384 #else /* !NO_RSA */
385
386 # if PEDANTIC
387 static void *dummy=&dummy;
388 # endif
389
390 #endif