]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/n_pkey.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / crypto / asn1 / n_pkey.c
1 /* crypto/asn1/n_pkey.c */
2 /* Copyright (C) 1995-1997 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 #include <stdio.h>
60 #include "cryptlib.h"
61 #include "rsa.h"
62 #include "objects.h"
63 #include "asn1_mac.h"
64 #include "evp.h"
65 #include "x509.h"
66
67
68 #ifndef NO_RC4
69
70 typedef struct netscape_pkey_st
71 {
72 ASN1_INTEGER *version;
73 X509_ALGOR *algor;
74 ASN1_OCTET_STRING *private_key;
75 } NETSCAPE_PKEY;
76
77 /*
78 * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_LENGTH_MISMATCH);
79 * ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_DECODING_ERROR);
80 * ASN1err(ASN1_F_D2I_NETSCAPE_PKEY,ASN1_R_DECODING_ERROR);
81 * ASN1err(ASN1_F_NETSCAPE_PKEY_NEW,ASN1_R_DECODING_ERROR);
82 */
83 #ifndef NOPROTO
84 static RSA *d2i_Netscape_RSA_2(RSA **a, unsigned char **pp, long length,
85 int (*cb)());
86 static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp);
87 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a,unsigned char **pp, long length);
88 static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void);
89 static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *);
90 #else
91 static RSA *d2i_Netscape_RSA_2();
92 static int i2d_NETSCAPE_PKEY();
93 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY();
94 static NETSCAPE_PKEY *NETSCAPE_PKEY_new();
95 static void NETSCAPE_PKEY_free();
96 #endif
97
98 int i2d_Netscape_RSA(a,pp,cb)
99 RSA *a;
100 unsigned char **pp;
101 int (*cb)();
102 {
103 int i,j,l[6];
104 NETSCAPE_PKEY *pkey;
105 unsigned char buf[256],*zz;
106 unsigned char key[EVP_MAX_KEY_LENGTH];
107 EVP_CIPHER_CTX ctx;
108 X509_ALGOR *alg=NULL;
109 ASN1_OCTET_STRING os,os2;
110 M_ASN1_I2D_vars(a);
111
112 if (a == NULL) return(0);
113
114 #ifdef WIN32
115 r=r; /* shut the damn compiler up :-) */
116 #endif
117
118 os.data=os2.data=NULL;
119 if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
120 if (!ASN1_INTEGER_set(pkey->version,0)) goto err;
121
122 if (pkey->algor->algorithm != NULL)
123 ASN1_OBJECT_free(pkey->algor->algorithm);
124 pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
125 if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
126 pkey->algor->parameter->type=V_ASN1_NULL;
127
128 l[0]=i2d_RSAPrivateKey(a,NULL);
129 pkey->private_key->length=l[0];
130
131 os2.length=i2d_NETSCAPE_PKEY(pkey,NULL);
132 l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL);
133
134 if ((alg=X509_ALGOR_new()) == NULL) goto err;
135 if (alg->algorithm != NULL)
136 ASN1_OBJECT_free(alg->algorithm);
137 alg->algorithm=OBJ_nid2obj(NID_rc4);
138 if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
139 alg->parameter->type=V_ASN1_NULL;
140
141 l[2]=i2d_X509_ALGOR(alg,NULL);
142 l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE);
143
144 os.data=(unsigned char *)"private-key";
145 os.length=11;
146 l[4]=i2d_ASN1_OCTET_STRING(&os,NULL);
147
148 l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE);
149
150 if (pp == NULL)
151 {
152 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
153 if (alg != NULL) X509_ALGOR_free(alg);
154 return(l[5]);
155 }
156
157 if (pkey->private_key->data != NULL)
158 Free((char *)pkey->private_key->data);
159 if ((pkey->private_key->data=(unsigned char *)Malloc(l[0])) == NULL)
160 {
161 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
162 goto err;
163 }
164 zz=pkey->private_key->data;
165 i2d_RSAPrivateKey(a,&zz);
166
167 if ((os2.data=(unsigned char *)Malloc(os2.length)) == NULL)
168 {
169 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
170 goto err;
171 }
172 zz=os2.data;
173 i2d_NETSCAPE_PKEY(pkey,&zz);
174
175 if (cb == NULL)
176 cb=EVP_read_pw_string;
177 i=cb(buf,256,"Enter Private Key password:",1);
178 if (i != 0)
179 {
180 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ);
181 goto err;
182 }
183 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
184 strlen((char *)buf),1,key,NULL);
185 memset(buf,0,256);
186 EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL);
187 EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length);
188 EVP_EncryptFinal(&ctx,&(os2.data[i]),&j);
189 EVP_CIPHER_CTX_cleanup(&ctx);
190
191 p= *pp;
192 ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
193 i2d_ASN1_OCTET_STRING(&os,&p);
194 ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
195 i2d_X509_ALGOR(alg,&p);
196 i2d_ASN1_OCTET_STRING(&os2,&p);
197 ret=l[5];
198 err:
199 if (os2.data != NULL) Free((char *)os2.data);
200 if (alg != NULL) X509_ALGOR_free(alg);
201 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
202 r=r;
203 return(ret);
204 }
205
206 RSA *d2i_Netscape_RSA(a,pp,length,cb)
207 RSA **a;
208 unsigned char **pp;
209 long length;
210 int (*cb)();
211 {
212 RSA *ret=NULL;
213 ASN1_OCTET_STRING *os=NULL;
214 ASN1_CTX c;
215
216 c.pp=pp;
217 c.error=ASN1_R_DECODING_ERROR;
218
219 M_ASN1_D2I_Init();
220 M_ASN1_D2I_start_sequence();
221 M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
222 if ((os->length != 11) || (strncmp("private-key",
223 (char *)os->data,os->length) != 0))
224 {
225 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
226 ASN1_BIT_STRING_free(os);
227 goto err;
228 }
229 ASN1_BIT_STRING_free(os);
230 c.q=c.p;
231 if ((ret=d2i_Netscape_RSA_2(a,&c.p,c.slen,cb)) == NULL) goto err;
232 c.slen-=(c.p-c.q);
233
234 M_ASN1_D2I_Finish(a,RSA_free,ASN1_F_D2I_NETSCAPE_RSA);
235 }
236
237 static RSA *d2i_Netscape_RSA_2(a,pp,length,cb)
238 RSA **a;
239 unsigned char **pp;
240 long length;
241 int (*cb)();
242 {
243 NETSCAPE_PKEY *pkey=NULL;
244 RSA *ret=NULL;
245 int i,j;
246 unsigned char buf[256],*zz;
247 unsigned char key[EVP_MAX_KEY_LENGTH];
248 EVP_CIPHER_CTX ctx;
249 X509_ALGOR *alg=NULL;
250 ASN1_OCTET_STRING *os=NULL;
251 ASN1_CTX c;
252
253 c.error=ASN1_R_ERROR_STACK;
254 c.pp=pp;
255
256 M_ASN1_D2I_Init();
257 M_ASN1_D2I_start_sequence();
258 M_ASN1_D2I_get(alg,d2i_X509_ALGOR);
259 if (OBJ_obj2nid(alg->algorithm) != NID_rc4)
260 {
261 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
262 goto err;
263 }
264 M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
265 if (cb == NULL)
266 cb=EVP_read_pw_string;
267 i=cb(buf,256,"Enter Private Key password:",0);
268 if (i != 0)
269 {
270 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ);
271 goto err;
272 }
273
274 EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
275 strlen((char *)buf),1,key,NULL);
276 memset(buf,0,256);
277 EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL);
278 EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
279 EVP_DecryptFinal(&ctx,&(os->data[i]),&j);
280 EVP_CIPHER_CTX_cleanup(&ctx);
281 os->length=i+j;
282
283 zz=os->data;
284
285 if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
286 {
287 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
288 goto err;
289 }
290
291 zz=pkey->private_key->data;
292 if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
293 {
294 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
295 goto err;
296 }
297 if (!asn1_Finish(&c)) goto err;
298 *pp=c.p;
299 err:
300 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
301 if (os != NULL) ASN1_BIT_STRING_free(os);
302 if (alg != NULL) X509_ALGOR_free(alg);
303 return(ret);
304 }
305
306 static int i2d_NETSCAPE_PKEY(a,pp)
307 NETSCAPE_PKEY *a;
308 unsigned char **pp;
309 {
310 M_ASN1_I2D_vars(a);
311
312
313 M_ASN1_I2D_len(a->version, i2d_ASN1_INTEGER);
314 M_ASN1_I2D_len(a->algor, i2d_X509_ALGOR);
315 M_ASN1_I2D_len(a->private_key, i2d_ASN1_OCTET_STRING);
316
317 M_ASN1_I2D_seq_total();
318
319 M_ASN1_I2D_put(a->version, i2d_ASN1_INTEGER);
320 M_ASN1_I2D_put(a->algor, i2d_X509_ALGOR);
321 M_ASN1_I2D_put(a->private_key, i2d_ASN1_OCTET_STRING);
322
323 M_ASN1_I2D_finish();
324 }
325
326 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(a,pp,length)
327 NETSCAPE_PKEY **a;
328 unsigned char **pp;
329 long length;
330 {
331 M_ASN1_D2I_vars(a,NETSCAPE_PKEY *,NETSCAPE_PKEY_new);
332
333 M_ASN1_D2I_Init();
334 M_ASN1_D2I_start_sequence();
335 M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
336 M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
337 M_ASN1_D2I_get(ret->private_key,d2i_ASN1_OCTET_STRING);
338 M_ASN1_D2I_Finish(a,NETSCAPE_PKEY_free,ASN1_F_D2I_NETSCAPE_PKEY);
339 }
340
341 static NETSCAPE_PKEY *NETSCAPE_PKEY_new()
342 {
343 NETSCAPE_PKEY *ret=NULL;
344
345 M_ASN1_New_Malloc(ret,NETSCAPE_PKEY);
346 M_ASN1_New(ret->version,ASN1_INTEGER_new);
347 M_ASN1_New(ret->algor,X509_ALGOR_new);
348 M_ASN1_New(ret->private_key,ASN1_OCTET_STRING_new);
349 return(ret);
350 M_ASN1_New_Error(ASN1_F_NETSCAPE_PKEY_NEW);
351 }
352
353 static void NETSCAPE_PKEY_free(a)
354 NETSCAPE_PKEY *a;
355 {
356 if (a == NULL) return;
357 ASN1_INTEGER_free(a->version);
358 X509_ALGOR_free(a->algor);
359 ASN1_OCTET_STRING_free(a->private_key);
360 Free((char *)a);
361 }
362
363 #endif /* NO_RC4 */
364