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