sl@0: /* crypto/pem/pem_lib.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_DES sl@0: #include sl@0: #endif sl@0: sl@0: const char PEM_version[]="PEM" OPENSSL_VERSION_PTEXT; sl@0: sl@0: #define MIN_LENGTH 4 sl@0: sl@0: static int load_iv(char **fromp,unsigned char *to, int num); sl@0: static int check_pem(const char *nm, const char *name); sl@0: sl@0: EXPORT_C int PEM_def_callback(char *buf, int num, int w, void *key) sl@0: { sl@0: #ifdef OPENSSL_NO_FP_API sl@0: /* We should not ever call the default callback routine from sl@0: * windows. */ sl@0: PEMerr(PEM_F_PEM_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); sl@0: return(-1); sl@0: #else sl@0: int i,j; sl@0: const char *prompt; sl@0: if(key) { sl@0: i=strlen(key); sl@0: i=(i > num)?num:i; sl@0: memcpy(buf,key,i); sl@0: return(i); sl@0: } sl@0: sl@0: prompt=EVP_get_pw_prompt(); sl@0: if (prompt == NULL) sl@0: prompt="Enter PEM pass phrase:"; sl@0: sl@0: for (;;) sl@0: { sl@0: i=EVP_read_pw_string(buf,num,prompt,w); sl@0: if (i != 0) sl@0: { sl@0: PEMerr(PEM_F_PEM_DEF_CALLBACK,PEM_R_PROBLEMS_GETTING_PASSWORD); sl@0: memset(buf,0,(unsigned int)num); sl@0: return(-1); sl@0: } sl@0: j=strlen(buf); sl@0: if (j < MIN_LENGTH) sl@0: { sl@0: fprintf(stderr,"phrase is too short, needs to be at least %d chars\n",MIN_LENGTH); sl@0: } sl@0: else sl@0: break; sl@0: } sl@0: return(j); sl@0: #endif sl@0: } sl@0: sl@0: EXPORT_C void PEM_proc_type(char *buf, int type) sl@0: { sl@0: const char *str; sl@0: sl@0: if (type == PEM_TYPE_ENCRYPTED) sl@0: str="ENCRYPTED"; sl@0: else if (type == PEM_TYPE_MIC_CLEAR) sl@0: str="MIC-CLEAR"; sl@0: else if (type == PEM_TYPE_MIC_ONLY) sl@0: str="MIC-ONLY"; sl@0: else sl@0: str="BAD-TYPE"; sl@0: sl@0: BUF_strlcat(buf,"Proc-Type: 4,",PEM_BUFSIZE); sl@0: BUF_strlcat(buf,str,PEM_BUFSIZE); sl@0: BUF_strlcat(buf,"\n",PEM_BUFSIZE); sl@0: } sl@0: sl@0: EXPORT_C void PEM_dek_info(char *buf, const char *type, int len, char *str) sl@0: { sl@0: static const unsigned char map[17]="0123456789ABCDEF"; sl@0: long i; sl@0: int j; sl@0: sl@0: BUF_strlcat(buf,"DEK-Info: ",PEM_BUFSIZE); sl@0: BUF_strlcat(buf,type,PEM_BUFSIZE); sl@0: BUF_strlcat(buf,",",PEM_BUFSIZE); sl@0: j=strlen(buf); sl@0: if (j + (len * 2) + 1 > PEM_BUFSIZE) sl@0: return; sl@0: for (i=0; i>4)&0x0f]; sl@0: buf[j+i*2+1]=map[(str[i] )&0x0f]; sl@0: } sl@0: buf[j+i*2]='\n'; sl@0: buf[j+i*2+1]='\0'; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, sl@0: pem_password_cb *cb, void *u) sl@0: { sl@0: BIO *b; sl@0: void *ret; sl@0: sl@0: if ((b=BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: PEMerr(PEM_F_PEM_ASN1_READ,ERR_R_BUF_LIB); sl@0: return(0); sl@0: } sl@0: BIO_set_fp(b,fp,BIO_NOCLOSE); sl@0: ret=PEM_ASN1_read_bio(d2i,name,b,x,cb,u); sl@0: BIO_free(b); sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: static int check_pem(const char *nm, const char *name) sl@0: { sl@0: /* Normal matching nm and name */ sl@0: if (!strcmp(nm,name)) return 1; sl@0: sl@0: /* Make PEM_STRING_EVP_PKEY match any private key */ sl@0: sl@0: if(!strcmp(nm,PEM_STRING_PKCS8) && sl@0: !strcmp(name,PEM_STRING_EVP_PKEY)) return 1; sl@0: sl@0: if(!strcmp(nm,PEM_STRING_PKCS8INF) && sl@0: !strcmp(name,PEM_STRING_EVP_PKEY)) return 1; sl@0: sl@0: if(!strcmp(nm,PEM_STRING_RSA) && sl@0: !strcmp(name,PEM_STRING_EVP_PKEY)) return 1; sl@0: sl@0: if(!strcmp(nm,PEM_STRING_DSA) && sl@0: !strcmp(name,PEM_STRING_EVP_PKEY)) return 1; sl@0: sl@0: if(!strcmp(nm,PEM_STRING_ECPRIVATEKEY) && sl@0: !strcmp(name,PEM_STRING_EVP_PKEY)) return 1; sl@0: /* Permit older strings */ sl@0: sl@0: if(!strcmp(nm,PEM_STRING_X509_OLD) && sl@0: !strcmp(name,PEM_STRING_X509)) return 1; sl@0: sl@0: if(!strcmp(nm,PEM_STRING_X509_REQ_OLD) && sl@0: !strcmp(name,PEM_STRING_X509_REQ)) return 1; sl@0: sl@0: /* Allow normal certs to be read as trusted certs */ sl@0: if(!strcmp(nm,PEM_STRING_X509) && sl@0: !strcmp(name,PEM_STRING_X509_TRUSTED)) return 1; sl@0: sl@0: if(!strcmp(nm,PEM_STRING_X509_OLD) && sl@0: !strcmp(name,PEM_STRING_X509_TRUSTED)) return 1; sl@0: sl@0: /* Some CAs use PKCS#7 with CERTIFICATE headers */ sl@0: if(!strcmp(nm, PEM_STRING_X509) && sl@0: !strcmp(name, PEM_STRING_PKCS7)) return 1; sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: EXPORT_C int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char *name, BIO *bp, sl@0: pem_password_cb *cb, void *u) sl@0: { sl@0: EVP_CIPHER_INFO cipher; sl@0: char *nm=NULL,*header=NULL; sl@0: unsigned char *data=NULL; sl@0: long len; sl@0: int ret = 0; sl@0: sl@0: for (;;) sl@0: { sl@0: if (!PEM_read_bio(bp,&nm,&header,&data,&len)) { sl@0: if(ERR_GET_REASON(ERR_peek_error()) == sl@0: PEM_R_NO_START_LINE) sl@0: ERR_add_error_data(2, "Expecting: ", name); sl@0: return 0; sl@0: } sl@0: if(check_pem(nm, name)) break; sl@0: OPENSSL_free(nm); sl@0: OPENSSL_free(header); sl@0: OPENSSL_free(data); sl@0: } sl@0: if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err; sl@0: if (!PEM_do_header(&cipher,data,&len,cb,u)) goto err; sl@0: sl@0: *pdata = data; sl@0: *plen = len; sl@0: sl@0: if (pnm) sl@0: *pnm = nm; sl@0: sl@0: ret = 1; sl@0: sl@0: err: sl@0: if (!ret || !pnm) OPENSSL_free(nm); sl@0: OPENSSL_free(header); sl@0: if (!ret) OPENSSL_free(data); sl@0: return ret; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, sl@0: char *x, const EVP_CIPHER *enc, unsigned char *kstr, sl@0: int klen, pem_password_cb *callback, void *u) sl@0: { sl@0: BIO *b; sl@0: int ret; sl@0: sl@0: if ((b=BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: PEMerr(PEM_F_PEM_ASN1_WRITE,ERR_R_BUF_LIB); sl@0: return(0); sl@0: } sl@0: BIO_set_fp(b,fp,BIO_NOCLOSE); sl@0: ret=PEM_ASN1_write_bio(i2d,name,b,x,enc,kstr,klen,callback,u); sl@0: BIO_free(b); sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, sl@0: char *x, const EVP_CIPHER *enc, unsigned char *kstr, sl@0: int klen, pem_password_cb *callback, void *u) sl@0: { sl@0: EVP_CIPHER_CTX ctx; sl@0: int dsize=0,i,j,ret=0; sl@0: unsigned char *p,*data=NULL; sl@0: const char *objstr=NULL; sl@0: char buf[PEM_BUFSIZE]; sl@0: unsigned char key[EVP_MAX_KEY_LENGTH]; sl@0: unsigned char iv[EVP_MAX_IV_LENGTH]; sl@0: sl@0: if (enc != NULL) sl@0: { sl@0: objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc)); sl@0: if (objstr == NULL) sl@0: { sl@0: PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if ((dsize=i2d(x,NULL)) < 0) sl@0: { sl@0: PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_ASN1_LIB); sl@0: dsize=0; sl@0: goto err; sl@0: } sl@0: /* dzise + 8 bytes are needed */ sl@0: /* actually it needs the cipher block size extra... */ sl@0: data=(unsigned char *)OPENSSL_malloc((unsigned int)dsize+20); sl@0: if (data == NULL) sl@0: { sl@0: PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: p=data; sl@0: i=i2d(x,&p); sl@0: sl@0: if (enc != NULL) sl@0: { sl@0: if (kstr == NULL) sl@0: { sl@0: if (callback == NULL) sl@0: klen=PEM_def_callback(buf,PEM_BUFSIZE,1,u); sl@0: else sl@0: klen=(*callback)(buf,PEM_BUFSIZE,1,u); sl@0: if (klen <= 0) sl@0: { sl@0: PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_READ_KEY); sl@0: goto err; sl@0: } sl@0: #ifdef CHARSET_EBCDIC sl@0: /* Convert the pass phrase from EBCDIC */ sl@0: ebcdic2ascii(buf, buf, klen); sl@0: #endif sl@0: kstr=(unsigned char *)buf; sl@0: } sl@0: RAND_add(data,i,0);/* put in the RSA key. */ sl@0: OPENSSL_assert(enc->iv_len <= (int)sizeof(iv)); sl@0: if (RAND_pseudo_bytes(iv,enc->iv_len) < 0) /* Generate a salt */ sl@0: goto err; sl@0: /* The 'iv' is used as the iv and as a salt. It is sl@0: * NOT taken from the BytesToKey function */ sl@0: EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL); sl@0: sl@0: if (kstr == (unsigned char *)buf) OPENSSL_cleanse(buf,PEM_BUFSIZE); sl@0: sl@0: OPENSSL_assert(strlen(objstr)+23+2*enc->iv_len+13 <= sizeof buf); sl@0: sl@0: buf[0]='\0'; sl@0: PEM_proc_type(buf,PEM_TYPE_ENCRYPTED); sl@0: PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv); sl@0: /* k=strlen(buf); */ sl@0: sl@0: EVP_CIPHER_CTX_init(&ctx); sl@0: EVP_EncryptInit_ex(&ctx,enc,NULL,key,iv); sl@0: EVP_EncryptUpdate(&ctx,data,&j,data,i); sl@0: EVP_EncryptFinal_ex(&ctx,&(data[j]),&i); sl@0: EVP_CIPHER_CTX_cleanup(&ctx); sl@0: i+=j; sl@0: ret=1; sl@0: } sl@0: else sl@0: { sl@0: ret=1; sl@0: buf[0]='\0'; sl@0: } sl@0: i=PEM_write_bio(bp,name,buf,data,i); sl@0: if (i <= 0) ret=0; sl@0: err: sl@0: OPENSSL_cleanse(key,sizeof(key)); sl@0: OPENSSL_cleanse(iv,sizeof(iv)); sl@0: OPENSSL_cleanse((char *)&ctx,sizeof(ctx)); sl@0: OPENSSL_cleanse(buf,PEM_BUFSIZE); sl@0: if (data != NULL) sl@0: { sl@0: OPENSSL_cleanse(data,(unsigned int)dsize); sl@0: OPENSSL_free(data); sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen, sl@0: pem_password_cb *callback,void *u) sl@0: { sl@0: int i,j,o,klen; sl@0: long len; sl@0: EVP_CIPHER_CTX ctx; sl@0: unsigned char key[EVP_MAX_KEY_LENGTH]; sl@0: char buf[PEM_BUFSIZE]; sl@0: sl@0: len= *plen; sl@0: sl@0: if (cipher->cipher == NULL) return(1); sl@0: if (callback == NULL) sl@0: klen=PEM_def_callback(buf,PEM_BUFSIZE,0,u); sl@0: else sl@0: klen=callback(buf,PEM_BUFSIZE,0,u); sl@0: if (klen <= 0) sl@0: { sl@0: PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_PASSWORD_READ); sl@0: return(0); sl@0: } sl@0: #ifdef CHARSET_EBCDIC sl@0: /* Convert the pass phrase from EBCDIC */ sl@0: ebcdic2ascii(buf, buf, klen); sl@0: #endif sl@0: sl@0: EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]), sl@0: (unsigned char *)buf,klen,1,key,NULL); sl@0: sl@0: j=(int)len; sl@0: EVP_CIPHER_CTX_init(&ctx); sl@0: EVP_DecryptInit_ex(&ctx,cipher->cipher,NULL, key,&(cipher->iv[0])); sl@0: EVP_DecryptUpdate(&ctx,data,&i,data,j); sl@0: o=EVP_DecryptFinal_ex(&ctx,&(data[i]),&j); sl@0: EVP_CIPHER_CTX_cleanup(&ctx); sl@0: OPENSSL_cleanse((char *)buf,sizeof(buf)); sl@0: OPENSSL_cleanse((char *)key,sizeof(key)); sl@0: j+=i; sl@0: if (!o) sl@0: { sl@0: PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_DECRYPT); sl@0: return(0); sl@0: } sl@0: *plen=j; sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher) sl@0: { sl@0: int o; sl@0: const EVP_CIPHER *enc=NULL; sl@0: char *p,c; sl@0: char **header_pp = &header; sl@0: sl@0: cipher->cipher=NULL; sl@0: if ((header == NULL) || (*header == '\0') || (*header == '\n')) sl@0: return(1); sl@0: if (strncmp(header,"Proc-Type: ",11) != 0) sl@0: { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_PROC_TYPE); return(0); } sl@0: header+=11; sl@0: if (*header != '4') return(0); header++; sl@0: if (*header != ',') return(0); header++; sl@0: if (strncmp(header,"ENCRYPTED",9) != 0) sl@0: { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_ENCRYPTED); return(0); } sl@0: for (; (*header != '\n') && (*header != '\0'); header++) sl@0: ; sl@0: if (*header == '\0') sl@0: { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_SHORT_HEADER); return(0); } sl@0: header++; sl@0: if (strncmp(header,"DEK-Info: ",10) != 0) sl@0: { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_DEK_INFO); return(0); } sl@0: header+=10; sl@0: sl@0: p=header; sl@0: for (;;) sl@0: { sl@0: c= *header; sl@0: #ifndef CHARSET_EBCDIC sl@0: if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') || sl@0: ((c >= '0') && (c <= '9')))) sl@0: break; sl@0: #else sl@0: if (!( isupper(c) || (c == '-') || sl@0: isdigit(c))) sl@0: break; sl@0: #endif sl@0: header++; sl@0: } sl@0: *header='\0'; sl@0: o=OBJ_sn2nid(p); sl@0: cipher->cipher=enc=EVP_get_cipherbyname(p); sl@0: *header=c; sl@0: header++; sl@0: sl@0: if (enc == NULL) sl@0: { sl@0: PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION); sl@0: return(0); sl@0: } sl@0: if (!load_iv(header_pp,&(cipher->iv[0]),enc->iv_len)) sl@0: return(0); sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: static int load_iv(char **fromp, unsigned char *to, int num) sl@0: { sl@0: int v,i; sl@0: char *from; sl@0: sl@0: from= *fromp; sl@0: for (i=0; i= '0') && (*from <= '9')) sl@0: v= *from-'0'; sl@0: else if ((*from >= 'A') && (*from <= 'F')) sl@0: v= *from-'A'+10; sl@0: else if ((*from >= 'a') && (*from <= 'f')) sl@0: v= *from-'a'+10; sl@0: else sl@0: { sl@0: PEMerr(PEM_F_LOAD_IV,PEM_R_BAD_IV_CHARS); sl@0: return(0); sl@0: } sl@0: from++; sl@0: to[i/2]|=v<<(long)((!(i&1))*4); sl@0: } sl@0: sl@0: *fromp=from; sl@0: return(1); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int PEM_write(FILE *fp, char *name, char *header, unsigned char *data, sl@0: long len) sl@0: { sl@0: BIO *b; sl@0: int ret; sl@0: sl@0: if ((b=BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: PEMerr(PEM_F_PEM_WRITE,ERR_R_BUF_LIB); sl@0: return(0); sl@0: } sl@0: BIO_set_fp(b,fp,BIO_NOCLOSE); sl@0: ret=PEM_write_bio(b, name, header, data,len); sl@0: BIO_free(b); sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data, sl@0: long len) sl@0: { sl@0: int nlen,n,i,j,outl; sl@0: unsigned char *buf = NULL; sl@0: EVP_ENCODE_CTX ctx; sl@0: int reason=ERR_R_BUF_LIB; sl@0: sl@0: EVP_EncodeInit(&ctx); sl@0: nlen=strlen(name); sl@0: sl@0: if ( (BIO_write(bp,"-----BEGIN ",11) != 11) || sl@0: (BIO_write(bp,name,nlen) != nlen) || sl@0: (BIO_write(bp,"-----\n",6) != 6)) sl@0: goto err; sl@0: sl@0: i=strlen(header); sl@0: if (i > 0) sl@0: { sl@0: if ( (BIO_write(bp,header,i) != i) || sl@0: (BIO_write(bp,"\n",1) != 1)) sl@0: goto err; sl@0: } sl@0: sl@0: buf = OPENSSL_malloc(PEM_BUFSIZE*8); sl@0: if (buf == NULL) sl@0: { sl@0: reason=ERR_R_MALLOC_FAILURE; sl@0: goto err; sl@0: } sl@0: sl@0: i=j=0; sl@0: while (len > 0) sl@0: { sl@0: n=(int)((len>(PEM_BUFSIZE*5))?(PEM_BUFSIZE*5):len); sl@0: EVP_EncodeUpdate(&ctx,buf,&outl,&(data[j]),n); sl@0: if ((outl) && (BIO_write(bp,(char *)buf,outl) != outl)) sl@0: goto err; sl@0: i+=outl; sl@0: len-=n; sl@0: j+=n; sl@0: } sl@0: EVP_EncodeFinal(&ctx,buf,&outl); sl@0: if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err; sl@0: OPENSSL_cleanse(buf, PEM_BUFSIZE*8); sl@0: OPENSSL_free(buf); sl@0: buf = NULL; sl@0: if ( (BIO_write(bp,"-----END ",9) != 9) || sl@0: (BIO_write(bp,name,nlen) != nlen) || sl@0: (BIO_write(bp,"-----\n",6) != 6)) sl@0: goto err; sl@0: return(i+outl); sl@0: err: sl@0: if (buf) { sl@0: OPENSSL_cleanse(buf, PEM_BUFSIZE*8); sl@0: OPENSSL_free(buf); sl@0: } sl@0: PEMerr(PEM_F_PEM_WRITE_BIO,reason); sl@0: return(0); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int PEM_read(FILE *fp, char **name, char **header, unsigned char **data, sl@0: long *len) sl@0: { sl@0: BIO *b; sl@0: int ret; sl@0: sl@0: if ((b=BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: PEMerr(PEM_F_PEM_READ,ERR_R_BUF_LIB); sl@0: return(0); sl@0: } sl@0: BIO_set_fp(b,fp,BIO_NOCLOSE); sl@0: ret=PEM_read_bio(b, name, header, data,len); sl@0: BIO_free(b); sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, sl@0: long *len) sl@0: { sl@0: EVP_ENCODE_CTX ctx; sl@0: int end=0,i,k,bl=0,hl=0,nohead=0; sl@0: char buf[256]; sl@0: BUF_MEM *nameB; sl@0: BUF_MEM *headerB; sl@0: BUF_MEM *dataB,*tmpB; sl@0: sl@0: nameB=BUF_MEM_new(); sl@0: headerB=BUF_MEM_new(); sl@0: dataB=BUF_MEM_new(); sl@0: if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) sl@0: { sl@0: BUF_MEM_free(nameB); sl@0: BUF_MEM_free(headerB); sl@0: BUF_MEM_free(dataB); sl@0: PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); sl@0: return(0); sl@0: } sl@0: sl@0: buf[254]='\0'; sl@0: for (;;) sl@0: { sl@0: i=BIO_gets(bp,buf,254); sl@0: sl@0: if (i <= 0) sl@0: { sl@0: PEMerr(PEM_F_PEM_READ_BIO,PEM_R_NO_START_LINE); sl@0: goto err; sl@0: } sl@0: sl@0: while ((i >= 0) && (buf[i] <= ' ')) i--; sl@0: buf[++i]='\n'; buf[++i]='\0'; sl@0: sl@0: if (strncmp(buf,"-----BEGIN ",11) == 0) sl@0: { sl@0: i=strlen(&(buf[11])); sl@0: sl@0: if (strncmp(&(buf[11+i-6]),"-----\n",6) != 0) sl@0: continue; sl@0: if (!BUF_MEM_grow(nameB,i+9)) sl@0: { sl@0: PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: memcpy(nameB->data,&(buf[11]),i-6); sl@0: nameB->data[i-6]='\0'; sl@0: break; sl@0: } sl@0: } sl@0: hl=0; sl@0: if (!BUF_MEM_grow(headerB,256)) sl@0: { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; } sl@0: headerB->data[0]='\0'; sl@0: for (;;) sl@0: { sl@0: i=BIO_gets(bp,buf,254); sl@0: if (i <= 0) break; sl@0: sl@0: while ((i >= 0) && (buf[i] <= ' ')) i--; sl@0: buf[++i]='\n'; buf[++i]='\0'; sl@0: sl@0: if (buf[0] == '\n') break; sl@0: if (!BUF_MEM_grow(headerB,hl+i+9)) sl@0: { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; } sl@0: if (strncmp(buf,"-----END ",9) == 0) sl@0: { sl@0: nohead=1; sl@0: break; sl@0: } sl@0: memcpy(&(headerB->data[hl]),buf,i); sl@0: headerB->data[hl+i]='\0'; sl@0: hl+=i; sl@0: } sl@0: sl@0: bl=0; sl@0: if (!BUF_MEM_grow(dataB,1024)) sl@0: { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; } sl@0: dataB->data[0]='\0'; sl@0: if (!nohead) sl@0: { sl@0: for (;;) sl@0: { sl@0: i=BIO_gets(bp,buf,254); sl@0: if (i <= 0) break; sl@0: sl@0: while ((i >= 0) && (buf[i] <= ' ')) i--; sl@0: buf[++i]='\n'; buf[++i]='\0'; sl@0: sl@0: if (i != 65) end=1; sl@0: if (strncmp(buf,"-----END ",9) == 0) sl@0: break; sl@0: if (i > 65) break; sl@0: if (!BUF_MEM_grow_clean(dataB,i+bl+9)) sl@0: { sl@0: PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: memcpy(&(dataB->data[bl]),buf,i); sl@0: dataB->data[bl+i]='\0'; sl@0: bl+=i; sl@0: if (end) sl@0: { sl@0: buf[0]='\0'; sl@0: i=BIO_gets(bp,buf,254); sl@0: if (i <= 0) break; sl@0: sl@0: while ((i >= 0) && (buf[i] <= ' ')) i--; sl@0: buf[++i]='\n'; buf[++i]='\0'; sl@0: sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: tmpB=headerB; sl@0: headerB=dataB; sl@0: dataB=tmpB; sl@0: bl=hl; sl@0: } sl@0: i=strlen(nameB->data); sl@0: if ( (strncmp(buf,"-----END ",9) != 0) || sl@0: (strncmp(nameB->data,&(buf[9]),i) != 0) || sl@0: (strncmp(&(buf[9+i]),"-----\n",6) != 0)) sl@0: { sl@0: PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE); sl@0: goto err; sl@0: } sl@0: sl@0: EVP_DecodeInit(&ctx); sl@0: i=EVP_DecodeUpdate(&ctx, sl@0: (unsigned char *)dataB->data,&bl, sl@0: (unsigned char *)dataB->data,bl); sl@0: if (i < 0) sl@0: { sl@0: PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE); sl@0: goto err; sl@0: } sl@0: i=EVP_DecodeFinal(&ctx,(unsigned char *)&(dataB->data[bl]),&k); sl@0: if (i < 0) sl@0: { sl@0: PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE); sl@0: goto err; sl@0: } sl@0: bl+=k; sl@0: sl@0: if (bl == 0) goto err; sl@0: *name=nameB->data; sl@0: *header=headerB->data; sl@0: *data=(unsigned char *)dataB->data; sl@0: *len=bl; sl@0: OPENSSL_free(nameB); sl@0: OPENSSL_free(headerB); sl@0: OPENSSL_free(dataB); sl@0: return(1); sl@0: err: sl@0: BUF_MEM_free(nameB); sl@0: BUF_MEM_free(headerB); sl@0: BUF_MEM_free(dataB); sl@0: return(0); sl@0: }