xref: /onnv-gate/usr/src/common/openssl/crypto/pem/pem_info.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/pem/pem_info.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/buffer.h>
620Sstevel@tonic-gate #include <openssl/objects.h>
630Sstevel@tonic-gate #include <openssl/evp.h>
640Sstevel@tonic-gate #include <openssl/x509.h>
650Sstevel@tonic-gate #include <openssl/pem.h>
66*2139Sjp161948 #ifndef OPENSSL_NO_RSA
67*2139Sjp161948 #include <openssl/rsa.h>
68*2139Sjp161948 #endif
69*2139Sjp161948 #ifndef OPENSSL_NO_DSA
70*2139Sjp161948 #include <openssl/dsa.h>
71*2139Sjp161948 #endif
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
STACK_OF(X509_INFO)740Sstevel@tonic-gate STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u)
750Sstevel@tonic-gate 	{
760Sstevel@tonic-gate         BIO *b;
770Sstevel@tonic-gate         STACK_OF(X509_INFO) *ret;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate         if ((b=BIO_new(BIO_s_file())) == NULL)
800Sstevel@tonic-gate 		{
810Sstevel@tonic-gate 		PEMerr(PEM_F_PEM_X509_INFO_READ,ERR_R_BUF_LIB);
820Sstevel@tonic-gate                 return(0);
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate         BIO_set_fp(b,fp,BIO_NOCLOSE);
850Sstevel@tonic-gate         ret=PEM_X509_INFO_read_bio(b,sk,cb,u);
860Sstevel@tonic-gate         BIO_free(b);
870Sstevel@tonic-gate         return(ret);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate 
STACK_OF(X509_INFO)910Sstevel@tonic-gate STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u)
920Sstevel@tonic-gate 	{
930Sstevel@tonic-gate 	X509_INFO *xi=NULL;
94*2139Sjp161948 	char *name=NULL,*header=NULL;
95*2139Sjp161948 	void *pp;
96*2139Sjp161948 	unsigned char *data=NULL;
97*2139Sjp161948 	const unsigned char *p;
980Sstevel@tonic-gate 	long len,error=0;
990Sstevel@tonic-gate 	int ok=0;
1000Sstevel@tonic-gate 	STACK_OF(X509_INFO) *ret=NULL;
1010Sstevel@tonic-gate 	unsigned int i,raw;
102*2139Sjp161948 	d2i_of_void *d2i;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (sk == NULL)
1050Sstevel@tonic-gate 		{
1060Sstevel@tonic-gate 		if ((ret=sk_X509_INFO_new_null()) == NULL)
1070Sstevel@tonic-gate 			{
1080Sstevel@tonic-gate 			PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_MALLOC_FAILURE);
1090Sstevel@tonic-gate 			goto err;
1100Sstevel@tonic-gate 			}
1110Sstevel@tonic-gate 		}
1120Sstevel@tonic-gate 	else
1130Sstevel@tonic-gate 		ret=sk;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if ((xi=X509_INFO_new()) == NULL) goto err;
1160Sstevel@tonic-gate 	for (;;)
1170Sstevel@tonic-gate 		{
1180Sstevel@tonic-gate 		raw=0;
1190Sstevel@tonic-gate 		i=PEM_read_bio(bp,&name,&header,&data,&len);
1200Sstevel@tonic-gate 		if (i == 0)
1210Sstevel@tonic-gate 			{
1220Sstevel@tonic-gate 			error=ERR_GET_REASON(ERR_peek_last_error());
1230Sstevel@tonic-gate 			if (error == PEM_R_NO_START_LINE)
1240Sstevel@tonic-gate 				{
1250Sstevel@tonic-gate 				ERR_clear_error();
1260Sstevel@tonic-gate 				break;
1270Sstevel@tonic-gate 				}
1280Sstevel@tonic-gate 			goto err;
1290Sstevel@tonic-gate 			}
1300Sstevel@tonic-gate start:
1310Sstevel@tonic-gate 		if (	(strcmp(name,PEM_STRING_X509) == 0) ||
1320Sstevel@tonic-gate 			(strcmp(name,PEM_STRING_X509_OLD) == 0))
1330Sstevel@tonic-gate 			{
134*2139Sjp161948 			d2i=(D2I_OF(void))d2i_X509;
1350Sstevel@tonic-gate 			if (xi->x509 != NULL)
1360Sstevel@tonic-gate 				{
1370Sstevel@tonic-gate 				if (!sk_X509_INFO_push(ret,xi)) goto err;
1380Sstevel@tonic-gate 				if ((xi=X509_INFO_new()) == NULL) goto err;
1390Sstevel@tonic-gate 				goto start;
1400Sstevel@tonic-gate 				}
141*2139Sjp161948 			pp=&(xi->x509);
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 		else if ((strcmp(name,PEM_STRING_X509_TRUSTED) == 0))
1440Sstevel@tonic-gate 			{
145*2139Sjp161948 			d2i=(D2I_OF(void))d2i_X509_AUX;
1460Sstevel@tonic-gate 			if (xi->x509 != NULL)
1470Sstevel@tonic-gate 				{
1480Sstevel@tonic-gate 				if (!sk_X509_INFO_push(ret,xi)) goto err;
1490Sstevel@tonic-gate 				if ((xi=X509_INFO_new()) == NULL) goto err;
1500Sstevel@tonic-gate 				goto start;
1510Sstevel@tonic-gate 				}
152*2139Sjp161948 			pp=&(xi->x509);
1530Sstevel@tonic-gate 			}
1540Sstevel@tonic-gate 		else if (strcmp(name,PEM_STRING_X509_CRL) == 0)
1550Sstevel@tonic-gate 			{
156*2139Sjp161948 			d2i=(D2I_OF(void))d2i_X509_CRL;
1570Sstevel@tonic-gate 			if (xi->crl != NULL)
1580Sstevel@tonic-gate 				{
1590Sstevel@tonic-gate 				if (!sk_X509_INFO_push(ret,xi)) goto err;
1600Sstevel@tonic-gate 				if ((xi=X509_INFO_new()) == NULL) goto err;
1610Sstevel@tonic-gate 				goto start;
1620Sstevel@tonic-gate 				}
163*2139Sjp161948 			pp=&(xi->crl);
1640Sstevel@tonic-gate 			}
1650Sstevel@tonic-gate 		else
1660Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
1670Sstevel@tonic-gate 			if (strcmp(name,PEM_STRING_RSA) == 0)
1680Sstevel@tonic-gate 			{
169*2139Sjp161948 			d2i=(D2I_OF(void))d2i_RSAPrivateKey;
1700Sstevel@tonic-gate 			if (xi->x_pkey != NULL)
1710Sstevel@tonic-gate 				{
1720Sstevel@tonic-gate 				if (!sk_X509_INFO_push(ret,xi)) goto err;
1730Sstevel@tonic-gate 				if ((xi=X509_INFO_new()) == NULL) goto err;
1740Sstevel@tonic-gate 				goto start;
1750Sstevel@tonic-gate 				}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 			xi->enc_data=NULL;
1780Sstevel@tonic-gate 			xi->enc_len=0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			xi->x_pkey=X509_PKEY_new();
1810Sstevel@tonic-gate 			if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
1820Sstevel@tonic-gate 				goto err;
1830Sstevel@tonic-gate 			xi->x_pkey->dec_pkey->type=EVP_PKEY_RSA;
184*2139Sjp161948 			pp=&(xi->x_pkey->dec_pkey->pkey.rsa);
1850Sstevel@tonic-gate 			if ((int)strlen(header) > 10) /* assume encrypted */
1860Sstevel@tonic-gate 				raw=1;
1870Sstevel@tonic-gate 			}
1880Sstevel@tonic-gate 		else
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
1910Sstevel@tonic-gate 			if (strcmp(name,PEM_STRING_DSA) == 0)
1920Sstevel@tonic-gate 			{
193*2139Sjp161948 			d2i=(D2I_OF(void))d2i_DSAPrivateKey;
1940Sstevel@tonic-gate 			if (xi->x_pkey != NULL)
1950Sstevel@tonic-gate 				{
1960Sstevel@tonic-gate 				if (!sk_X509_INFO_push(ret,xi)) goto err;
1970Sstevel@tonic-gate 				if ((xi=X509_INFO_new()) == NULL) goto err;
1980Sstevel@tonic-gate 				goto start;
1990Sstevel@tonic-gate 				}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 			xi->enc_data=NULL;
2020Sstevel@tonic-gate 			xi->enc_len=0;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 			xi->x_pkey=X509_PKEY_new();
2050Sstevel@tonic-gate 			if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
2060Sstevel@tonic-gate 				goto err;
2070Sstevel@tonic-gate 			xi->x_pkey->dec_pkey->type=EVP_PKEY_DSA;
2080Sstevel@tonic-gate 			pp=(char **)&(xi->x_pkey->dec_pkey->pkey.dsa);
2090Sstevel@tonic-gate 			if ((int)strlen(header) > 10) /* assume encrypted */
2100Sstevel@tonic-gate 				raw=1;
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 		else
2130Sstevel@tonic-gate #endif
214*2139Sjp161948 #ifndef OPENSSL_NO_EC
215*2139Sjp161948  			if (strcmp(name,PEM_STRING_ECPRIVATEKEY) == 0)
216*2139Sjp161948  			{
217*2139Sjp161948  				d2i=(D2I_OF(void))d2i_ECPrivateKey;
218*2139Sjp161948  				if (xi->x_pkey != NULL)
219*2139Sjp161948  				{
220*2139Sjp161948  					if (!sk_X509_INFO_push(ret,xi)) goto err;
221*2139Sjp161948  					if ((xi=X509_INFO_new()) == NULL) goto err;
222*2139Sjp161948  						goto start;
223*2139Sjp161948  				}
224*2139Sjp161948 
225*2139Sjp161948  			xi->enc_data=NULL;
226*2139Sjp161948  			xi->enc_len=0;
227*2139Sjp161948 
228*2139Sjp161948  			xi->x_pkey=X509_PKEY_new();
229*2139Sjp161948  			if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
230*2139Sjp161948  				goto err;
231*2139Sjp161948  			xi->x_pkey->dec_pkey->type=EVP_PKEY_EC;
232*2139Sjp161948  			pp=&(xi->x_pkey->dec_pkey->pkey.ec);
233*2139Sjp161948  			if ((int)strlen(header) > 10) /* assume encrypted */
234*2139Sjp161948  				raw=1;
235*2139Sjp161948 			}
236*2139Sjp161948 		else
237*2139Sjp161948 #endif
2380Sstevel@tonic-gate 			{
2390Sstevel@tonic-gate 			d2i=NULL;
2400Sstevel@tonic-gate 			pp=NULL;
2410Sstevel@tonic-gate 			}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		if (d2i != NULL)
2440Sstevel@tonic-gate 			{
2450Sstevel@tonic-gate 			if (!raw)
2460Sstevel@tonic-gate 				{
2470Sstevel@tonic-gate 				EVP_CIPHER_INFO cipher;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 				if (!PEM_get_EVP_CIPHER_INFO(header,&cipher))
2500Sstevel@tonic-gate 					goto err;
2510Sstevel@tonic-gate 				if (!PEM_do_header(&cipher,data,&len,cb,u))
2520Sstevel@tonic-gate 					goto err;
2530Sstevel@tonic-gate 				p=data;
2540Sstevel@tonic-gate 				if (d2i(pp,&p,len) == NULL)
2550Sstevel@tonic-gate 					{
2560Sstevel@tonic-gate 					PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_ASN1_LIB);
2570Sstevel@tonic-gate 					goto err;
2580Sstevel@tonic-gate 					}
2590Sstevel@tonic-gate 				}
2600Sstevel@tonic-gate 			else
2610Sstevel@tonic-gate 				{ /* encrypted RSA data */
2620Sstevel@tonic-gate 				if (!PEM_get_EVP_CIPHER_INFO(header,
2630Sstevel@tonic-gate 					&xi->enc_cipher)) goto err;
2640Sstevel@tonic-gate 				xi->enc_data=(char *)data;
2650Sstevel@tonic-gate 				xi->enc_len=(int)len;
2660Sstevel@tonic-gate 				data=NULL;
2670Sstevel@tonic-gate 				}
2680Sstevel@tonic-gate 			}
2690Sstevel@tonic-gate 		else	{
2700Sstevel@tonic-gate 			/* unknown */
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 		if (name != NULL) OPENSSL_free(name);
2730Sstevel@tonic-gate 		if (header != NULL) OPENSSL_free(header);
2740Sstevel@tonic-gate 		if (data != NULL) OPENSSL_free(data);
2750Sstevel@tonic-gate 		name=NULL;
2760Sstevel@tonic-gate 		header=NULL;
2770Sstevel@tonic-gate 		data=NULL;
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/* if the last one hasn't been pushed yet and there is anything
2810Sstevel@tonic-gate 	 * in it then add it to the stack ...
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate 	if ((xi->x509 != NULL) || (xi->crl != NULL) ||
2840Sstevel@tonic-gate 		(xi->x_pkey != NULL) || (xi->enc_data != NULL))
2850Sstevel@tonic-gate 		{
2860Sstevel@tonic-gate 		if (!sk_X509_INFO_push(ret,xi)) goto err;
2870Sstevel@tonic-gate 		xi=NULL;
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 	ok=1;
2900Sstevel@tonic-gate err:
2910Sstevel@tonic-gate 	if (xi != NULL) X509_INFO_free(xi);
2920Sstevel@tonic-gate 	if (!ok)
2930Sstevel@tonic-gate 		{
2940Sstevel@tonic-gate 		for (i=0; ((int)i)<sk_X509_INFO_num(ret); i++)
2950Sstevel@tonic-gate 			{
2960Sstevel@tonic-gate 			xi=sk_X509_INFO_value(ret,i);
2970Sstevel@tonic-gate 			X509_INFO_free(xi);
2980Sstevel@tonic-gate 			}
2990Sstevel@tonic-gate 		if (ret != sk) sk_X509_INFO_free(ret);
3000Sstevel@tonic-gate 		ret=NULL;
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (name != NULL) OPENSSL_free(name);
3040Sstevel@tonic-gate 	if (header != NULL) OPENSSL_free(header);
3050Sstevel@tonic-gate 	if (data != NULL) OPENSSL_free(data);
3060Sstevel@tonic-gate 	return(ret);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /* A TJH addition */
PEM_X509_INFO_write_bio(BIO * bp,X509_INFO * xi,EVP_CIPHER * enc,unsigned char * kstr,int klen,pem_password_cb * cb,void * u)3110Sstevel@tonic-gate int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
3120Sstevel@tonic-gate 	     unsigned char *kstr, int klen, pem_password_cb *cb, void *u)
3130Sstevel@tonic-gate 	{
3140Sstevel@tonic-gate 	EVP_CIPHER_CTX ctx;
3150Sstevel@tonic-gate 	int i,ret=0;
3160Sstevel@tonic-gate 	unsigned char *data=NULL;
3170Sstevel@tonic-gate 	const char *objstr=NULL;
3180Sstevel@tonic-gate 	char buf[PEM_BUFSIZE];
3190Sstevel@tonic-gate 	unsigned char *iv=NULL;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (enc != NULL)
3220Sstevel@tonic-gate 		{
3230Sstevel@tonic-gate 		objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc));
3240Sstevel@tonic-gate 		if (objstr == NULL)
3250Sstevel@tonic-gate 			{
3260Sstevel@tonic-gate 			PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
3270Sstevel@tonic-gate 			goto err;
3280Sstevel@tonic-gate 			}
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/* now for the fun part ... if we have a private key then
3320Sstevel@tonic-gate 	 * we have to be able to handle a not-yet-decrypted key
3330Sstevel@tonic-gate 	 * being written out correctly ... if it is decrypted or
3340Sstevel@tonic-gate 	 * it is non-encrypted then we use the base code
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	if (xi->x_pkey!=NULL)
3370Sstevel@tonic-gate 		{
3380Sstevel@tonic-gate 		if ( (xi->enc_data!=NULL) && (xi->enc_len>0) )
3390Sstevel@tonic-gate 			{
3400Sstevel@tonic-gate 			/* copy from weirdo names into more normal things */
3410Sstevel@tonic-gate 			iv=xi->enc_cipher.iv;
3420Sstevel@tonic-gate 			data=(unsigned char *)xi->enc_data;
3430Sstevel@tonic-gate 			i=xi->enc_len;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 			/* we take the encryption data from the
3460Sstevel@tonic-gate 			 * internal stuff rather than what the
3470Sstevel@tonic-gate 			 * user has passed us ... as we have to
3480Sstevel@tonic-gate 			 * match exactly for some strange reason
3490Sstevel@tonic-gate 			 */
3500Sstevel@tonic-gate 			objstr=OBJ_nid2sn(
3510Sstevel@tonic-gate 				EVP_CIPHER_nid(xi->enc_cipher.cipher));
3520Sstevel@tonic-gate 			if (objstr == NULL)
3530Sstevel@tonic-gate 				{
3540Sstevel@tonic-gate 				PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
3550Sstevel@tonic-gate 				goto err;
3560Sstevel@tonic-gate 				}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 			/* create the right magic header stuff */
3590Sstevel@tonic-gate 			OPENSSL_assert(strlen(objstr)+23+2*enc->iv_len+13 <= sizeof buf);
3600Sstevel@tonic-gate 			buf[0]='\0';
3610Sstevel@tonic-gate 			PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
3620Sstevel@tonic-gate 			PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 			/* use the normal code to write things out */
3650Sstevel@tonic-gate 			i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i);
3660Sstevel@tonic-gate 			if (i <= 0) goto err;
3670Sstevel@tonic-gate 			}
3680Sstevel@tonic-gate 		else
3690Sstevel@tonic-gate 			{
3700Sstevel@tonic-gate 			/* Add DSA/DH */
3710Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
3720Sstevel@tonic-gate 			/* normal optionally encrypted stuff */
3730Sstevel@tonic-gate 			if (PEM_write_bio_RSAPrivateKey(bp,
3740Sstevel@tonic-gate 				xi->x_pkey->dec_pkey->pkey.rsa,
3750Sstevel@tonic-gate 				enc,kstr,klen,cb,u)<=0)
3760Sstevel@tonic-gate 				goto err;
3770Sstevel@tonic-gate #endif
3780Sstevel@tonic-gate 			}
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/* if we have a certificate then write it out now */
3820Sstevel@tonic-gate 	if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp,xi->x509) <= 0))
3830Sstevel@tonic-gate 		goto err;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/* we are ignoring anything else that is loaded into the X509_INFO
3860Sstevel@tonic-gate 	 * structure for the moment ... as I don't need it so I'm not
3870Sstevel@tonic-gate 	 * coding it here and Eric can do it when this makes it into the
3880Sstevel@tonic-gate 	 * base library --tjh
3890Sstevel@tonic-gate 	 */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	ret=1;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate err:
3940Sstevel@tonic-gate 	OPENSSL_cleanse((char *)&ctx,sizeof(ctx));
3950Sstevel@tonic-gate 	OPENSSL_cleanse(buf,PEM_BUFSIZE);
3960Sstevel@tonic-gate 	return(ret);
3970Sstevel@tonic-gate 	}
398