Constification.
[openssl.git] / crypto / pem / pem_lib.c
index 072211b..5e41d85 100644 (file)
@@ -57,6 +57,7 @@
  */
 
 #include <stdio.h>
+#include <ctype.h>
 #include "cryptlib.h"
 #include <openssl/buffer.h>
 #include <openssl/objects.h>
 #include <openssl/x509.h>
 #include <openssl/pem.h>
 #include <openssl/pkcs12.h>
-#ifndef NO_DES
+#include "asn1_locl.h"
+#ifndef OPENSSL_NO_DES
 #include <openssl/des.h>
 #endif
+#ifndef OPENSSL_NO_ENGINE
+#include <openssl/engine.h>
+#endif
 
-const char *PEM_version="PEM" OPENSSL_VERSION_PTEXT;
+const char PEM_version[]="PEM" OPENSSL_VERSION_PTEXT;
 
 #define MIN_LENGTH     4
 
-static int def_callback(char *buf, int num, int w, void *userdata);
-static int load_iv(unsigned char **fromp,unsigned char *to, int num);
+static int load_iv(char **fromp,unsigned char *to, int num);
 static int check_pem(const char *nm, const char *name);
-static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder,
-                               int nid, const EVP_CIPHER *enc,
-                               char *kstr, int klen,
-                               pem_password_cb *cb, void *u);
-static int do_pk8pkey_fp(FILE *bp, EVP_PKEY *x, int isder,
-                               int nid, const EVP_CIPHER *enc,
-                               char *kstr, int klen,
-                               pem_password_cb *cb, void *u);
-
-static int def_callback(char *buf, int num, int w, void *key)
+int pem_check_suffix(const char *pem_str, const char *suffix);
+
+int PEM_def_callback(char *buf, int num, int w, void *key)
        {
-#ifdef NO_FP_API
+#ifdef OPENSSL_NO_FP_API
        /* We should not ever call the default callback routine from
         * windows. */
-       PEMerr(PEM_F_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+       PEMerr(PEM_F_PEM_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
        return(-1);
 #else
        int i,j;
@@ -108,10 +105,10 @@ static int def_callback(char *buf, int num, int w, void *key)
 
        for (;;)
                {
-               i=EVP_read_pw_string(buf,num,prompt,w);
+               i=EVP_read_pw_string_min(buf,MIN_LENGTH,num,prompt,w);
                if (i != 0)
                        {
-                       PEMerr(PEM_F_DEF_CALLBACK,PEM_R_PROBLEMS_GETTING_PASSWORD);
+                       PEMerr(PEM_F_PEM_DEF_CALLBACK,PEM_R_PROBLEMS_GETTING_PASSWORD);
                        memset(buf,0,(unsigned int)num);
                        return(-1);
                        }
@@ -140,21 +137,23 @@ void PEM_proc_type(char *buf, int type)
        else
                str="BAD-TYPE";
                
-       strcat(buf,"Proc-Type: 4,");
-       strcat(buf,str);
-       strcat(buf,"\n");
+       BUF_strlcat(buf,"Proc-Type: 4,",PEM_BUFSIZE);
+       BUF_strlcat(buf,str,PEM_BUFSIZE);
+       BUF_strlcat(buf,"\n",PEM_BUFSIZE);
        }
 
 void PEM_dek_info(char *buf, const char *type, int len, char *str)
        {
-       static unsigned char map[17]="0123456789ABCDEF";
+       static const unsigned char map[17]="0123456789ABCDEF";
        long i;
        int j;
 
-       strcat(buf,"DEK-Info: ");
-       strcat(buf,type);
-       strcat(buf,",");
+       BUF_strlcat(buf,"DEK-Info: ",PEM_BUFSIZE);
+       BUF_strlcat(buf,type,PEM_BUFSIZE);
+       BUF_strlcat(buf,",",PEM_BUFSIZE);
        j=strlen(buf);
+       if (j + (len * 2) + 1 > PEM_BUFSIZE)
+               return;
        for (i=0; i<len; i++)
                {
                buf[j+i*2]  =map[(str[i]>>4)&0x0f];
@@ -164,12 +163,12 @@ void PEM_dek_info(char *buf, const char *type, int len, char *str)
        buf[j+i*2+1]='\0';
        }
 
-#ifndef NO_FP_API
-char *PEM_ASN1_read(char *(*d2i)(), const char *name, FILE *fp, char **x,
-            pem_password_cb *cb, void *u)
+#ifndef OPENSSL_NO_FP_API
+void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
+                   pem_password_cb *cb, void *u)
        {
         BIO *b;
-        char *ret;
+        void *ret;
 
         if ((b=BIO_new(BIO_s_file())) == NULL)
                {
@@ -190,17 +189,56 @@ static int check_pem(const char *nm, const char *name)
 
        /* Make PEM_STRING_EVP_PKEY match any private key */
 
-       if(!strcmp(nm,PEM_STRING_PKCS8) &&
-               !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
-
-       if(!strcmp(nm,PEM_STRING_PKCS8INF) &&
-                !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
-
-       if(!strcmp(nm,PEM_STRING_RSA) &&
-               !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
+       if(!strcmp(name,PEM_STRING_EVP_PKEY))
+               {
+               int slen;
+               const EVP_PKEY_ASN1_METHOD *ameth;
+               if(!strcmp(nm,PEM_STRING_PKCS8))
+                       return 1;
+               if(!strcmp(nm,PEM_STRING_PKCS8INF))
+                       return 1;
+               slen = pem_check_suffix(nm, "PRIVATE KEY"); 
+               if (slen > 0)
+                       {
+                       /* NB: ENGINE implementations wont contain
+                        * a deprecated old private key decode function
+                        * so don't look for them.
+                        */
+                       ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
+                       if (ameth && ameth->old_priv_decode)
+                               return 1;
+                       }
+               return 0;
+               }
 
-       if(!strcmp(nm,PEM_STRING_DSA) &&
-                !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
+       if(!strcmp(name,PEM_STRING_PARAMETERS))
+               {
+               int slen;
+               const EVP_PKEY_ASN1_METHOD *ameth;
+               slen = pem_check_suffix(nm, "PARAMETERS"); 
+               if (slen > 0)
+                       {
+                       ENGINE *e;
+                       ameth = EVP_PKEY_asn1_find_str(&e, nm, slen);
+                       if (ameth)
+                               {
+                               int r;
+                               if (ameth->param_decode)
+                                       r = 1;
+                               else
+                                       r = 0;
+#ifndef OPENSSL_NO_ENGINE
+                               if (e)
+                                       ENGINE_finish(e);
+#endif
+                               return r;
+                               }
+                       }
+               return 0;
+               }
+       /* If reading DH parameters handle X9.42 DH format too */
+       if(!strcmp(nm,PEM_STRING_DHXPARAMS) &&
+               !strcmp(name,PEM_STRING_DHPARAMS)) return 1;
 
        /* Permit older strings */
 
@@ -221,17 +259,28 @@ static int check_pem(const char *nm, const char *name)
        if(!strcmp(nm, PEM_STRING_X509) &&
                !strcmp(name, PEM_STRING_PKCS7)) return 1;
 
+       if(!strcmp(nm, PEM_STRING_PKCS7_SIGNED) &&
+               !strcmp(name, PEM_STRING_PKCS7)) return 1;
+
+#ifndef OPENSSL_NO_CMS
+       if(!strcmp(nm, PEM_STRING_X509) &&
+               !strcmp(name, PEM_STRING_CMS)) return 1;
+       /* Allow CMS to be read from PKCS#7 headers */
+       if(!strcmp(nm, PEM_STRING_PKCS7) &&
+               !strcmp(name, PEM_STRING_CMS)) return 1;
+#endif
+
        return 0;
 }
 
-char *PEM_ASN1_read_bio(char *(*d2i)(), const char *name, BIO *bp, char **x,
+int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char *name, BIO *bp,
             pem_password_cb *cb, void *u)
        {
        EVP_CIPHER_INFO cipher;
        char *nm=NULL,*header=NULL;
-       unsigned char *p=NULL,*data=NULL;
+       unsigned char *data=NULL;
        long len;
-       char *ret=NULL;
+       int ret = 0;
 
        for (;;)
                {
@@ -239,66 +288,35 @@ char *PEM_ASN1_read_bio(char *(*d2i)(), const char *name, BIO *bp, char **x,
                        if(ERR_GET_REASON(ERR_peek_error()) ==
                                PEM_R_NO_START_LINE)
                                ERR_add_error_data(2, "Expecting: ", name);
-                       return(NULL);
+                       return 0;
                }
                if(check_pem(nm, name)) break;
-               Free(nm);
-               Free(header);
-               Free(data);
+               OPENSSL_free(nm);
+               OPENSSL_free(header);
+               OPENSSL_free(data);
                }
        if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err;
        if (!PEM_do_header(&cipher,data,&len,cb,u)) goto err;
-       p=data;
-       if (strcmp(name,PEM_STRING_EVP_PKEY) == 0) {
-               if (strcmp(nm,PEM_STRING_RSA) == 0)
-                       ret=d2i(EVP_PKEY_RSA,x,&p,len);
-               else if (strcmp(nm,PEM_STRING_DSA) == 0)
-                       ret=d2i(EVP_PKEY_DSA,x,&p,len);
-               else if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {
-                       PKCS8_PRIV_KEY_INFO *p8inf;
-                       p8inf=d2i_PKCS8_PRIV_KEY_INFO(
-                                       (PKCS8_PRIV_KEY_INFO **) x, &p, len);
-                       ret = (char *)EVP_PKCS82PKEY(p8inf);
-                       PKCS8_PRIV_KEY_INFO_free(p8inf);
-               } else if (strcmp(nm,PEM_STRING_PKCS8) == 0) {
-                       PKCS8_PRIV_KEY_INFO *p8inf;
-                       X509_SIG *p8;
-                       int klen;
-                       char psbuf[PEM_BUFSIZE];
-                       p8 = d2i_X509_SIG(NULL, &p, len);
-                       if(!p8) goto p8err;
-                       if (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u);
-                       else klen=def_callback(psbuf,PEM_BUFSIZE,0,u);
-                       if (klen <= 0) {
-                               PEMerr(PEM_F_PEM_ASN1_READ_BIO,
-                                               PEM_R_BAD_PASSWORD_READ);
-                               goto err;
-                       }
-                       p8inf = M_PKCS8_decrypt(p8, psbuf, klen);
-                       X509_SIG_free(p8);
-                       if(!p8inf) goto p8err;
-                       ret = (char *)EVP_PKCS82PKEY(p8inf);
-                       if(x) {
-                               if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
-                               *x = ret;
-                       }
-                       PKCS8_PRIV_KEY_INFO_free(p8inf);
-               }
-       } else  ret=d2i(x,&p,len);
-p8err:
-       if (ret == NULL)
-               PEMerr(PEM_F_PEM_ASN1_READ_BIO,ERR_R_ASN1_LIB);
+
+       *pdata = data;
+       *plen = len;
+
+       if (pnm)
+               *pnm = nm;
+
+       ret = 1;
+
 err:
-       Free(nm);
-       Free(header);
-       Free(data);
-       return(ret);
+       if (!ret || !pnm) OPENSSL_free(nm);
+       OPENSSL_free(header);
+       if (!ret) OPENSSL_free(data);
+       return ret;
        }
 
-#ifndef NO_FP_API
-int PEM_ASN1_write(int (*i2d)(), const char *name, FILE *fp, char *x,
-            const EVP_CIPHER *enc, unsigned char *kstr, int klen,
-            pem_password_cb *callback, void *u)
+#ifndef OPENSSL_NO_FP_API
+int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
+                  void *x, const EVP_CIPHER *enc, unsigned char *kstr,
+                  int klen, pem_password_cb *callback, void *u)
         {
         BIO *b;
         int ret;
@@ -315,9 +333,9 @@ int PEM_ASN1_write(int (*i2d)(), const char *name, FILE *fp, char *x,
         }
 #endif
 
-int PEM_ASN1_write_bio(int (*i2d)(), const char *name, BIO *bp, char *x,
-            const EVP_CIPHER *enc, unsigned char *kstr, int klen,
-            pem_password_cb *callback, void *u)
+int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
+                      void *x, const EVP_CIPHER *enc, unsigned char *kstr,
+                      int klen, pem_password_cb *callback, void *u)
        {
        EVP_CIPHER_CTX ctx;
        int dsize=0,i,j,ret=0;
@@ -339,12 +357,13 @@ int PEM_ASN1_write_bio(int (*i2d)(), const char *name, BIO *bp, char *x,
 
        if ((dsize=i2d(x,NULL)) < 0)
                {
-               PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE);
+               PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_ASN1_LIB);
                dsize=0;
                goto err;
                }
        /* dzise + 8 bytes are needed */
-       data=(unsigned char *)Malloc((unsigned int)dsize+20);
+       /* actually it needs the cipher block size extra... */
+       data=(unsigned char *)OPENSSL_malloc((unsigned int)dsize+20);
        if (data == NULL)
                {
                PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE);
@@ -358,7 +377,7 @@ int PEM_ASN1_write_bio(int (*i2d)(), const char *name, BIO *bp, char *x,
                if (kstr == NULL)
                        {
                        if (callback == NULL)
-                               klen=def_callback(buf,PEM_BUFSIZE,1,u);
+                               klen=PEM_def_callback(buf,PEM_BUFSIZE,1,u);
                        else
                                klen=(*callback)(buf,PEM_BUFSIZE,1,u);
                        if (klen <= 0)
@@ -373,24 +392,33 @@ int PEM_ASN1_write_bio(int (*i2d)(), const char *name, BIO *bp, char *x,
                        kstr=(unsigned char *)buf;
                        }
                RAND_add(data,i,0);/* put in the RSA key. */
-               if (RAND_bytes(iv,8) <= 0)      /* Generate a salt */
+               OPENSSL_assert(enc->iv_len <= (int)sizeof(iv));
+               if (RAND_pseudo_bytes(iv,enc->iv_len) < 0) /* Generate a salt */
                        goto err;
                /* The 'iv' is used as the iv and as a salt.  It is
                 * NOT taken from the BytesToKey function */
-               EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL);
+               if (!EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL))
+                       goto err;
+
+               if (kstr == (unsigned char *)buf) OPENSSL_cleanse(buf,PEM_BUFSIZE);
 
-               if (kstr == (unsigned char *)buf) memset(buf,0,PEM_BUFSIZE);
+               OPENSSL_assert(strlen(objstr)+23+2*enc->iv_len+13 <= sizeof buf);
 
                buf[0]='\0';
                PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
-               PEM_dek_info(buf,objstr,8,(char *)iv);
+               PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv);
                /* k=strlen(buf); */
-       
-               EVP_EncryptInit(&ctx,enc,key,iv);
-               EVP_EncryptUpdate(&ctx,data,&j,data,i);
-               EVP_EncryptFinal(&ctx,&(data[j]),&i);
+
+               EVP_CIPHER_CTX_init(&ctx);
+               ret = 1;
+               if (!EVP_EncryptInit_ex(&ctx,enc,NULL,key,iv)
+                       || !EVP_EncryptUpdate(&ctx,data,&j,data,i)
+                       || !EVP_EncryptFinal_ex(&ctx,&(data[j]),&i))
+                       ret = 0;
+               EVP_CIPHER_CTX_cleanup(&ctx);
+               if (ret == 0)
+                       goto err;
                i+=j;
-               ret=1;
                }
        else
                {
@@ -400,12 +428,15 @@ int PEM_ASN1_write_bio(int (*i2d)(), const char *name, BIO *bp, char *x,
        i=PEM_write_bio(bp,name,buf,data,i);
        if (i <= 0) ret=0;
 err:
-       memset(key,0,sizeof(key));
-       memset(iv,0,sizeof(iv));
-       memset((char *)&ctx,0,sizeof(ctx));
-       memset(buf,0,PEM_BUFSIZE);
-       memset(data,0,(unsigned int)dsize);
-       Free(data);
+       OPENSSL_cleanse(key,sizeof(key));
+       OPENSSL_cleanse(iv,sizeof(iv));
+       OPENSSL_cleanse((char *)&ctx,sizeof(ctx));
+       OPENSSL_cleanse(buf,PEM_BUFSIZE);
+       if (data != NULL)
+               {
+               OPENSSL_cleanse(data,(unsigned int)dsize);
+               OPENSSL_free(data);
+               }
        return(ret);
        }
 
@@ -422,7 +453,7 @@ int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
 
        if (cipher->cipher == NULL) return(1);
        if (callback == NULL)
-               klen=def_callback(buf,PEM_BUFSIZE,0,u);
+               klen=PEM_def_callback(buf,PEM_BUFSIZE,0,u);
        else
                klen=callback(buf,PEM_BUFSIZE,0,u);
        if (klen <= 0)
@@ -435,16 +466,20 @@ int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
        ebcdic2ascii(buf, buf, klen);
 #endif
 
-       EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]),
-               (unsigned char *)buf,klen,1,key,NULL);
+       if (!EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]),
+               (unsigned char *)buf,klen,1,key,NULL))
+               return 0;
 
        j=(int)len;
-       EVP_DecryptInit(&ctx,cipher->cipher,key,&(cipher->iv[0]));
-       EVP_DecryptUpdate(&ctx,data,&i,data,j);
-       o=EVP_DecryptFinal(&ctx,&(data[i]),&j);
+       EVP_CIPHER_CTX_init(&ctx);
+       o = EVP_DecryptInit_ex(&ctx,cipher->cipher,NULL, key,&(cipher->iv[0]));
+       if (o)
+               o = EVP_DecryptUpdate(&ctx,data,&i,data,j);
+       if (o)
+               o = EVP_DecryptFinal_ex(&ctx,&(data[i]),&j);
        EVP_CIPHER_CTX_cleanup(&ctx);
-       memset((char *)buf,0,sizeof(buf));
-       memset((char *)key,0,sizeof(key));
+       OPENSSL_cleanse((char *)buf,sizeof(buf));
+       OPENSSL_cleanse((char *)key,sizeof(key));
        j+=i;
        if (!o)
                {
@@ -457,9 +492,9 @@ int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
 
 int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
        {
-       int o;
        const EVP_CIPHER *enc=NULL;
        char *p,c;
+       char **header_pp = &header;
 
        cipher->cipher=NULL;
        if ((header == NULL) || (*header == '\0') || (*header == '\n'))
@@ -496,7 +531,6 @@ int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
                header++;
                }
        *header='\0';
-       o=OBJ_sn2nid(p);
        cipher->cipher=enc=EVP_get_cipherbyname(p);
        *header=c;
        header++;
@@ -506,15 +540,16 @@ int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
                PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION);
                return(0);
                }
-       if (!load_iv((unsigned char **)&header,&(cipher->iv[0]),8)) return(0);
+       if (!load_iv(header_pp,&(cipher->iv[0]),enc->iv_len))
+               return(0);
 
        return(1);
        }
 
-static int load_iv(unsigned char **fromp, unsigned char *to, int num)
+static int load_iv(char **fromp, unsigned char *to, int num)
        {
        int v,i;
-       unsigned char *from;
+       char *from;
 
        from= *fromp;
        for (i=0; i<num; i++) to[i]=0;
@@ -540,9 +575,9 @@ static int load_iv(unsigned char **fromp, unsigned char *to, int num)
        return(1);
        }
 
-#ifndef NO_FP_API
-int PEM_write(FILE *fp, char *name, char *header, unsigned char *data,
-            long len)
+#ifndef OPENSSL_NO_FP_API
+int PEM_write(FILE *fp, const char *name, const char *header,
+             const unsigned char *data, long len)
         {
         BIO *b;
         int ret;
@@ -559,11 +594,11 @@ int PEM_write(FILE *fp, char *name, char *header, unsigned char *data,
         }
 #endif
 
-int PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
-            long len)
+int PEM_write_bio(BIO *bp, const char *name, const char *header,
+                 const unsigned char *data, long len)
        {
        int nlen,n,i,j,outl;
-       unsigned char *buf;
+       unsigned char *buf = NULL;
        EVP_ENCODE_CTX ctx;
        int reason=ERR_R_BUF_LIB;
        
@@ -583,7 +618,7 @@ int PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
                        goto err;
                }
 
-       buf=(unsigned char *)Malloc(PEM_BUFSIZE*8);
+       buf = OPENSSL_malloc(PEM_BUFSIZE*8);
        if (buf == NULL)
                {
                reason=ERR_R_MALLOC_FAILURE;
@@ -603,18 +638,24 @@ int PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
                }
        EVP_EncodeFinal(&ctx,buf,&outl);
        if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err;
-       Free(buf);
+       OPENSSL_cleanse(buf, PEM_BUFSIZE*8);
+       OPENSSL_free(buf);
+       buf = NULL;
        if (    (BIO_write(bp,"-----END ",9) != 9) ||
                (BIO_write(bp,name,nlen) != nlen) ||
                (BIO_write(bp,"-----\n",6) != 6))
                goto err;
        return(i+outl);
 err:
+       if (buf) {
+               OPENSSL_cleanse(buf, PEM_BUFSIZE*8);
+               OPENSSL_free(buf);
+       }
        PEMerr(PEM_F_PEM_WRITE_BIO,reason);
        return(0);
        }
 
-#ifndef NO_FP_API
+#ifndef OPENSSL_NO_FP_API
 int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
             long *len)
         {
@@ -648,6 +689,9 @@ int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
        dataB=BUF_MEM_new();
        if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL))
                {
+               BUF_MEM_free(nameB);
+               BUF_MEM_free(headerB);
+               BUF_MEM_free(dataB);
                PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
                return(0);
                }
@@ -725,7 +769,7 @@ int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
                        if (strncmp(buf,"-----END ",9) == 0)
                                break;
                        if (i > 65) break;
-                       if (!BUF_MEM_grow(dataB,i+bl+9))
+                       if (!BUF_MEM_grow_clean(dataB,i+bl+9))
                                {
                                PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
                                goto err;
@@ -784,9 +828,9 @@ int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
        *header=headerB->data;
        *data=(unsigned char *)dataB->data;
        *len=bl;
-       Free(nameB);
-       Free(headerB);
-       Free(dataB);
+       OPENSSL_free(nameB);
+       OPENSSL_free(headerB);
+       OPENSSL_free(dataB);
        return(1);
 err:
        BUF_MEM_free(nameB);
@@ -795,169 +839,24 @@ err:
        return(0);
        }
 
-/* These functions write a private key in PKCS#8 format: it is a "drop in"
- * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
- * is NULL then it uses the unencrypted private key form. The 'nid' versions
- * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
+/* Check pem string and return prefix length.
+ * If for example the pem_str == "RSA PRIVATE KEY" and suffix = "PRIVATE KEY"
+ * the return value is 3 for the string "RSA".
  */
 
-int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
-}
-
-int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
-}
-
-int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
-}
-
-int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
-}
-
-static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       X509_SIG *p8;
-       PKCS8_PRIV_KEY_INFO *p8inf;
-       char buf[PEM_BUFSIZE];
-       int ret;
-       if(!(p8inf = EVP_PKEY2PKCS8(x))) {
-               PEMerr(PEM_F_PEM_WRITE_BIO_PKCS8PRIVATEKEY,
-                                       PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
+int pem_check_suffix(const char *pem_str, const char *suffix)
+       {
+       int pem_len = strlen(pem_str);
+       int suffix_len = strlen(suffix);
+       const char *p;
+       if (suffix_len + 1 >= pem_len)
                return 0;
+       p = pem_str + pem_len - suffix_len;
+       if (strcmp(p, suffix))
+               return 0;
+       p--;
+       if (*p != ' ')
+               return 0;
+       return p - pem_str;
        }
-       if(enc || (nid != -1)) {
-               if(!kstr) {
-                       if(!cb) klen = def_callback(buf, PEM_BUFSIZE, 1, u);
-                       else klen = cb(buf, PEM_BUFSIZE, 1, u);
-                       if(klen <= 0) {
-                               PEMerr(PEM_F_PEM_WRITE_BIO_PKCS8PRIVATEKEY,
-                                                               PEM_R_READ_KEY);
-                               PKCS8_PRIV_KEY_INFO_free(p8inf);
-                               return 0;
-                       }
-                               
-                       kstr = buf;
-               }
-               p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
-               if(kstr == buf) memset(buf, 0, klen);
-               PKCS8_PRIV_KEY_INFO_free(p8inf);
-               if(isder) ret = i2d_PKCS8_bio(bp, p8);
-               else ret = PEM_write_bio_PKCS8(bp, p8);
-               X509_SIG_free(p8);
-               return ret;
-       } else {
-               if(isder) ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
-               else ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
-               PKCS8_PRIV_KEY_INFO_free(p8inf);
-               return ret;
-       }
-}
-
-/* Finally the DER version to read PKCS#8 encrypted private keys. It has to be
- * here to access the default callback.
- */
-
-EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
-{
-       PKCS8_PRIV_KEY_INFO *p8inf = NULL;
-       X509_SIG *p8 = NULL;
-       int klen;
-       EVP_PKEY *ret;
-       char psbuf[PEM_BUFSIZE];
-       p8 = d2i_PKCS8_bio(bp, NULL);
-       if(!p8) return NULL;
-       if (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u);
-       else klen=def_callback(psbuf,PEM_BUFSIZE,0,u);
-       if (klen <= 0) {
-               PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
-               X509_SIG_free(p8);
-               return NULL;    
-       }
-       p8inf = M_PKCS8_decrypt(p8, psbuf, klen);
-       X509_SIG_free(p8);
-       if(!p8inf) return NULL;
-       ret = EVP_PKCS82PKEY(p8inf);
-       PKCS8_PRIV_KEY_INFO_free(p8inf);
-       if(!ret) return NULL;
-       if(x) {
-               if(*x) EVP_PKEY_free(*x);
-               *x = ret;
-       }
-       return ret;
-}
-
-#ifndef NO_FP_API
-
-int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
-}
-
-int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
-}
-
-int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
-}
-
-int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
-                             char *kstr, int klen, pem_password_cb *cb, void *u)
-{
-       return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
-}
-
-static int do_pk8pkey_fp(FILE *fp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
-                                 char *kstr, int klen,
-                                 pem_password_cb *cb, void *u)
-{
-       BIO *bp;
-       int ret;
-       if(!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
-               PEMerr(PEM_F_PEM_F_DO_PK8KEY_FP,ERR_R_BUF_LIB);
-                return(0);
-       }
-       ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
-       BIO_free(bp);
-       return ret;
-}
-
-EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
-{
-       BIO *bp;
-       EVP_PKEY *ret;
-       if(!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
-               PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP,ERR_R_BUF_LIB);
-                return NULL;
-       }
-       ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
-       BIO_free(bp);
-       return ret;
-}
 
-#endif