1397d83113c30d64da2fcc2eb2a66f1a16cf59b2
[openssl.git] / pem_lib.c
1 /* crypto/pem/pem_lib.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 "buffer.h"
62 #include "objects.h"
63 #include "evp.h"
64 #include "rand.h"
65 #include "x509.h"
66 #include "pem.h"
67 #ifndef NO_DES
68 #include "des.h"
69 #endif
70
71 char *PEM_version="PEM part of SSLeay 0.8.1b 29-Jun-1998";
72
73 #define MIN_LENGTH      4
74
75 /* PEMerr(PEM_F_PEM_WRITE_BIO,ERR_R_MALLOC_FAILURE);
76  * PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
77  */
78
79 #ifndef NOPROTO
80 static int def_callback(char *buf, int num, int w);
81 static int load_iv(unsigned char **fromp,unsigned char *to, int num);
82 #else
83 static int def_callback();
84 static int load_iv();
85 #endif
86
87 static int def_callback(buf, num, w)
88 char *buf;
89 int num;
90 int w;
91         {
92 #ifdef WIN16
93         /* We should not ever call the default callback routine from
94          * windows. */
95         PEMerr(PEM_F_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
96         return(-1);
97 #else
98         int i,j;
99         char *prompt;
100
101         prompt=EVP_get_pw_prompt();
102         if (prompt == NULL)
103                 prompt="Enter PEM pass phrase:";
104
105         for (;;)
106                 {
107                 i=EVP_read_pw_string(buf,num,prompt,w);
108                 if (i != 0)
109                         {
110                         PEMerr(PEM_F_DEF_CALLBACK,PEM_R_PROBLEMS_GETTING_PASSWORD);
111                         memset(buf,0,(unsigned int)num);
112                         return(-1);
113                         }
114                 j=strlen(buf);
115                 if (j < MIN_LENGTH)
116                         {
117                         fprintf(stderr,"phrase is too short, needs to be at least %d chars\n",MIN_LENGTH);
118                         }
119                 else
120                         break;
121                 }
122         return(j);
123 #endif
124         }
125
126 void PEM_proc_type(buf, type)
127 char *buf;
128 int type;
129         {
130         char *str;
131
132         if (type == PEM_TYPE_ENCRYPTED)
133                 str="ENCRYPTED";
134         else if (type == PEM_TYPE_MIC_CLEAR)
135                 str="MIC-CLEAR";
136         else if (type == PEM_TYPE_MIC_ONLY)
137                 str="MIC-ONLY";
138         else
139                 str="BAD-TYPE";
140                 
141         strcat(buf,"Proc-Type: 4,");
142         strcat(buf,str);
143         strcat(buf,"\n");
144         }
145
146 void PEM_dek_info(buf, type, len, str)
147 char *buf;
148 char *type;
149 int len;
150 char *str;
151         {
152         static unsigned char map[17]="0123456789ABCDEF";
153         long i;
154         int j;
155
156         strcat(buf,"DEK-Info: ");
157         strcat(buf,type);
158         strcat(buf,",");
159         j=strlen(buf);
160         for (i=0; i<len; i++)
161                 {
162                 buf[j+i*2]  =map[(str[i]>>4)&0x0f];
163                 buf[j+i*2+1]=map[(str[i]   )&0x0f];
164                 }
165         buf[j+i*2]='\n';
166         buf[j+i*2+1]='\0';
167         }
168
169 #ifndef WIN16
170 char *PEM_ASN1_read(d2i,name,fp, x, cb)
171 char *(*d2i)();
172 char *name;
173 FILE *fp;
174 char **x;
175 int (*cb)();
176         {
177         BIO *b;
178         char *ret;
179
180         if ((b=BIO_new(BIO_s_file())) == NULL)
181                 {
182                 PEMerr(PEM_F_PEM_ASN1_READ,ERR_R_BUF_LIB);
183                 return(0);
184                 }
185         BIO_set_fp(b,fp,BIO_NOCLOSE);
186         ret=PEM_ASN1_read_bio(d2i,name,b,x,cb);
187         BIO_free(b);
188         return(ret);
189         }
190 #endif
191
192 char *PEM_ASN1_read_bio(d2i,name,bp, x, cb)
193 char *(*d2i)();
194 char *name;
195 BIO *bp;
196 char **x;
197 int (*cb)();
198         {
199         EVP_CIPHER_INFO cipher;
200         char *nm=NULL,*header=NULL;
201         unsigned char *p=NULL,*data=NULL;
202         long len;
203         char *ret=NULL;
204
205         for (;;)
206                 {
207                 if (!PEM_read_bio(bp,&nm,&header,&data,&len)) return(NULL);
208                 if (    (strcmp(nm,name) == 0) ||
209                         ((strcmp(nm,PEM_STRING_RSA) == 0) &&
210                          (strcmp(name,PEM_STRING_EVP_PKEY) == 0)) ||
211                         ((strcmp(nm,PEM_STRING_DSA) == 0) &&
212                          (strcmp(name,PEM_STRING_EVP_PKEY) == 0)) ||
213                         ((strcmp(nm,PEM_STRING_X509_OLD) == 0) &&
214                          (strcmp(name,PEM_STRING_X509) == 0)) ||
215                         ((strcmp(nm,PEM_STRING_X509_REQ_OLD) == 0) &&
216                          (strcmp(name,PEM_STRING_X509_REQ) == 0))
217                         )
218                         break;
219                 Free(nm);
220                 Free(header);
221                 Free(data);
222                 }
223         if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err;
224         if (!PEM_do_header(&cipher,data,&len,cb)) goto err;
225         p=data;
226         if (strcmp(name,PEM_STRING_EVP_PKEY) == 0)
227                 {
228                 if (strcmp(nm,PEM_STRING_RSA) == 0)
229                         ret=d2i(EVP_PKEY_RSA,x,&p,len);
230                 else if (strcmp(nm,PEM_STRING_DSA) == 0)
231                         ret=d2i(EVP_PKEY_DSA,x,&p,len);
232                 }
233         else    
234                 ret=d2i(x,&p,len);
235         if (ret == NULL)
236                 PEMerr(PEM_F_PEM_ASN1_READ_BIO,ERR_R_ASN1_LIB);
237 err:
238         Free(nm);
239         Free(header);
240         Free(data);
241         return(ret);
242         }
243
244 #ifndef WIN16
245 int PEM_ASN1_write(i2d,name,fp, x, enc, kstr, klen, callback)
246 int (*i2d)();
247 char *name;
248 FILE *fp;
249 char *x;
250 EVP_CIPHER *enc;
251 unsigned char *kstr;
252 int klen;
253 int (*callback)();
254         {
255         BIO *b;
256         int ret;
257
258         if ((b=BIO_new(BIO_s_file())) == NULL)
259                 {
260                 PEMerr(PEM_F_PEM_ASN1_WRITE,ERR_R_BUF_LIB);
261                 return(0);
262                 }
263         BIO_set_fp(b,fp,BIO_NOCLOSE);
264         ret=PEM_ASN1_write_bio(i2d,name,b,x,enc,kstr,klen,callback);
265         BIO_free(b);
266         return(ret);
267         }
268 #endif
269
270 int PEM_ASN1_write_bio(i2d,name,bp, x, enc, kstr, klen, callback)
271 int (*i2d)();
272 char *name;
273 BIO *bp;
274 char *x;
275 EVP_CIPHER *enc;
276 unsigned char *kstr;
277 int klen;
278 int (*callback)();
279         {
280         EVP_CIPHER_CTX ctx;
281         int dsize=0,i,j,ret=0;
282         unsigned char *p,*data=NULL;
283         char *objstr=NULL;
284 #define PEM_BUFSIZE     1024
285         char buf[PEM_BUFSIZE];
286         unsigned char key[EVP_MAX_KEY_LENGTH];
287         unsigned char iv[EVP_MAX_IV_LENGTH];
288         
289         if (enc != NULL)
290                 {
291                 objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc));
292                 if (objstr == NULL)
293                         {
294                         PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
295                         goto err;
296                         }
297                 }
298
299         dsize=i2d(x,NULL);
300         /* dzise + 8 bytes are needed */
301         data=(unsigned char *)Malloc((unsigned int)dsize+20);
302         if (data == NULL)
303                 {
304                 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE);
305                 goto err;
306                 }
307         p=data;
308         i=i2d(x,&p);
309
310         if (enc != NULL)
311                 {
312                 if (kstr == NULL)
313                         {
314                         if (callback == NULL)
315                                 klen=def_callback(buf,PEM_BUFSIZE,1);
316                         else
317                                 klen=(*callback)(buf,PEM_BUFSIZE,1);
318                         if (klen <= 0)
319                                 {
320                                 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_READ_KEY);
321                                 goto err;
322                                 }
323                         kstr=(unsigned char *)buf;
324                         }
325                 RAND_seed(data,i);/* put in the RSA key. */
326                 RAND_bytes(iv,8);       /* Generate a salt */
327                 /* The 'iv' is used as the iv and as a salt.  It is
328                  * NOT taken from the BytesToKey function */
329                 EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL);
330
331                 if (kstr == (unsigned char *)buf) memset(buf,0,PEM_BUFSIZE);
332
333                 buf[0]='\0';
334                 PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
335                 PEM_dek_info(buf,objstr,8,(char *)iv);
336                 /* k=strlen(buf); */
337         
338                 EVP_EncryptInit(&ctx,enc,key,iv);
339                 EVP_EncryptUpdate(&ctx,data,&j,data,i);
340                 EVP_EncryptFinal(&ctx,&(data[j]),&i);
341                 i+=j;
342                 ret=1;
343                 }
344         else
345                 {
346                 ret=1;
347                 buf[0]='\0';
348                 }
349         i=PEM_write_bio(bp,name,buf,data,i);
350         if (i <= 0) ret=0;
351 err:
352         memset(key,0,sizeof(key));
353         memset(iv,0,sizeof(iv));
354         memset((char *)&ctx,0,sizeof(ctx));
355         memset(buf,0,PEM_BUFSIZE);
356         memset(data,0,(unsigned int)dsize);
357         Free(data);
358         return(ret);
359         }
360
361 int PEM_do_header(cipher, data, plen, callback)
362 EVP_CIPHER_INFO *cipher;
363 unsigned char *data;
364 long *plen;
365 int (*callback)();
366         {
367         int i,j,o,klen;
368         long len;
369         EVP_CIPHER_CTX ctx;
370         unsigned char key[EVP_MAX_KEY_LENGTH];
371         char buf[PEM_BUFSIZE];
372
373         len= *plen;
374
375         if (cipher->cipher == NULL) return(1);
376         if (callback == NULL)
377                 klen=def_callback(buf,PEM_BUFSIZE,0);
378         else
379                 klen=callback(buf,PEM_BUFSIZE,0);
380         if (klen <= 0)
381                 {
382                 PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_PASSWORD_READ);
383                 return(0);
384                 }
385         EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]),
386                 (unsigned char *)buf,klen,1,key,NULL);
387
388         j=(int)len;
389         EVP_DecryptInit(&ctx,cipher->cipher,key,&(cipher->iv[0]));
390         EVP_DecryptUpdate(&ctx,data,&i,data,j);
391         o=EVP_DecryptFinal(&ctx,&(data[i]),&j);
392         EVP_CIPHER_CTX_cleanup(&ctx);
393         memset((char *)buf,0,sizeof(buf));
394         memset((char *)key,0,sizeof(key));
395         j+=i;
396         if (!o)
397                 {
398                 PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_DECRYPT);
399                 return(0);
400                 }
401         *plen=j;
402         return(1);
403         }
404
405 int PEM_get_EVP_CIPHER_INFO(header,cipher)
406 char *header;
407 EVP_CIPHER_INFO *cipher;
408         {
409         int o;
410         EVP_CIPHER *enc=NULL;
411         char *p,c;
412
413         cipher->cipher=NULL;
414         if ((header == NULL) || (*header == '\0') || (*header == '\n'))
415                 return(1);
416         if (strncmp(header,"Proc-Type: ",11) != 0)
417                 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_PROC_TYPE); return(0); }
418         header+=11;
419         if (*header != '4') return(0); header++;
420         if (*header != ',') return(0); header++;
421         if (strncmp(header,"ENCRYPTED",9) != 0)
422                 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_ENCRYPTED); return(0); }
423         for (; (*header != '\n') && (*header != '\0'); header++)
424                 ;
425         if (*header == '\0')
426                 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_SHORT_HEADER); return(0); }
427         header++;
428         if (strncmp(header,"DEK-Info: ",10) != 0)
429                 { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_DEK_INFO); return(0); }
430         header+=10;
431
432         p=header;
433         for (;;)
434                 {
435                 c= *header;
436                 if (!(  ((c >= 'A') && (c <= 'Z')) || (c == '-') ||
437                         ((c >= '0') && (c <= '9'))))
438                         break;
439                 header++;
440                 }
441         *header='\0';
442         o=OBJ_sn2nid(p);
443         cipher->cipher=enc=EVP_get_cipherbyname(p);
444         *header=c;
445         header++;
446
447         if (enc == NULL)
448                 {
449                 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION);
450                 return(0);
451                 }
452         if (!load_iv((unsigned char **)&header,&(cipher->iv[0]),8)) return(0);
453
454         return(1);
455         }
456
457 static int load_iv(fromp,to,num)
458 unsigned char **fromp,*to;
459 int num;
460         {
461         int v,i;
462         unsigned char *from;
463
464         from= *fromp;
465         for (i=0; i<num; i++) to[i]=0;
466         num*=2;
467         for (i=0; i<num; i++)
468                 {
469                 if ((*from >= '0') && (*from <= '9'))
470                         v= *from-'0';
471                 else if ((*from >= 'A') && (*from <= 'F'))
472                         v= *from-'A'+10;
473                 else if ((*from >= 'a') && (*from <= 'f'))
474                         v= *from-'a'+10;
475                 else
476                         {
477                         PEMerr(PEM_F_LOAD_IV,PEM_R_BAD_IV_CHARS);
478                         return(0);
479                         }
480                 from++;
481                 to[i/2]|=v<<(long)((!(i&1))*4);
482                 }
483
484         *fromp=from;
485         return(1);
486         }
487
488 #ifndef WIN16
489 int PEM_write(fp, name, header, data,len)
490 FILE *fp;
491 char *name;
492 char *header;
493 unsigned char *data;
494 long len;
495         {
496         BIO *b;
497         int ret;
498
499         if ((b=BIO_new(BIO_s_file())) == NULL)
500                 {
501                 PEMerr(PEM_F_PEM_WRITE,ERR_R_BUF_LIB);
502                 return(0);
503                 }
504         BIO_set_fp(b,fp,BIO_NOCLOSE);
505         ret=PEM_write_bio(b, name, header, data,len);
506         BIO_free(b);
507         return(ret);
508         }
509 #endif
510
511 int PEM_write_bio(bp, name, header, data,len)
512 BIO *bp;
513 char *name;
514 char *header;
515 unsigned char *data;
516 long len;
517         {
518         int nlen,n,i,j,outl;
519         unsigned char *buf;
520         EVP_ENCODE_CTX ctx;
521         int reason=ERR_R_BUF_LIB;
522         
523         EVP_EncodeInit(&ctx);
524         nlen=strlen(name);
525
526         if (    (BIO_write(bp,"-----BEGIN ",11) != 11) ||
527                 (BIO_write(bp,name,nlen) != nlen) ||
528                 (BIO_write(bp,"-----\n",6) != 6))
529                 goto err;
530                 
531         i=strlen(header);
532         if (i > 0)
533                 {
534                 if (    (BIO_write(bp,header,i) != i) ||
535                         (BIO_write(bp,"\n",1) != 1))
536                         goto err;
537                 }
538
539         buf=(unsigned char *)Malloc(PEM_BUFSIZE*8);
540         if (buf == NULL)
541                 {
542                 reason=ERR_R_MALLOC_FAILURE;
543                 goto err;
544                 }
545
546         i=j=0;
547         while (len > 0)
548                 {
549                 n=(int)((len>(PEM_BUFSIZE*5))?(PEM_BUFSIZE*5):len);
550                 EVP_EncodeUpdate(&ctx,buf,&outl,&(data[j]),n);
551                 if ((outl) && (BIO_write(bp,(char *)buf,outl) != outl))
552                         goto err;
553                 i+=outl;
554                 len-=n;
555                 j+=n;
556                 }
557         EVP_EncodeFinal(&ctx,buf,&outl);
558         if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err;
559         Free(buf);
560         if (    (BIO_write(bp,"-----END ",9) != 9) ||
561                 (BIO_write(bp,name,nlen) != nlen) ||
562                 (BIO_write(bp,"-----\n",6) != 6))
563                 goto err;
564         return(i+outl);
565 err:
566         PEMerr(PEM_F_PEM_WRITE_BIO,reason);
567         return(0);
568         }
569
570 #ifndef WIN16
571 int PEM_read(fp, name, header, data,len)
572 FILE *fp;
573 char **name;
574 char **header;
575 unsigned char **data;
576 long *len;
577         {
578         BIO *b;
579         int ret;
580
581         if ((b=BIO_new(BIO_s_file())) == NULL)
582                 {
583                 PEMerr(PEM_F_PEM_READ,ERR_R_BUF_LIB);
584                 return(0);
585                 }
586         BIO_set_fp(b,fp,BIO_NOCLOSE);
587         ret=PEM_read_bio(b, name, header, data,len);
588         BIO_free(b);
589         return(ret);
590         }
591 #endif
592
593 int PEM_read_bio(bp, name, header, data, len)
594 BIO *bp;
595 char **name;
596 char **header;
597 unsigned char **data;
598 long *len;
599         {
600         EVP_ENCODE_CTX ctx;
601         int end=0,i,k,bl=0,hl=0,nohead=0;
602         char buf[256];
603         BUF_MEM *nameB;
604         BUF_MEM *headerB;
605         BUF_MEM *dataB,*tmpB;
606         
607         nameB=BUF_MEM_new();
608         headerB=BUF_MEM_new();
609         dataB=BUF_MEM_new();
610         if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL))
611                 {
612                 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
613                 return(0);
614                 }
615
616         buf[254]='\0';
617         for (;;)
618                 {
619                 i=BIO_gets(bp,buf,254);
620
621                 if (i <= 0)
622                         {
623                         PEMerr(PEM_F_PEM_READ_BIO,PEM_R_NO_START_LINE);
624                         goto err;
625                         }
626
627                 while ((i >= 0) && (buf[i] <= ' ')) i--;
628                 buf[++i]='\n'; buf[++i]='\0';
629
630                 if (strncmp(buf,"-----BEGIN ",11) == 0)
631                         {
632                         i=strlen(&(buf[11]));
633
634                         if (strncmp(&(buf[11+i-6]),"-----\n",6) != 0)
635                                 continue;
636                         if (!BUF_MEM_grow(nameB,i+9))
637                                 {
638                                 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
639                                 goto err;
640                                 }
641                         strncpy(nameB->data,&(buf[11]),(unsigned int)i-6);
642                         nameB->data[i-6]='\0';
643                         break;
644                         }
645                 }
646         hl=0;
647         if (!BUF_MEM_grow(headerB,256))
648                 { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
649         headerB->data[0]='\0';
650         for (;;)
651                 {
652                 i=BIO_gets(bp,buf,254);
653                 if (i <= 0) break;
654
655                 while ((i >= 0) && (buf[i] <= ' ')) i--;
656                 buf[++i]='\n'; buf[++i]='\0';
657
658                 if (buf[0] == '\n') break;
659                 if (!BUF_MEM_grow(headerB,hl+i+9))
660                         { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
661                 if (strncmp(buf,"-----END ",9) == 0)
662                         {
663                         nohead=1;
664                         break;
665                         }
666                 strncpy(&(headerB->data[hl]),buf,(unsigned int)i);
667                 headerB->data[hl+i]='\0';
668                 hl+=i;
669                 }
670
671         bl=0;
672         if (!BUF_MEM_grow(dataB,1024))
673                 { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
674         dataB->data[0]='\0';
675         if (!nohead)
676                 {
677                 for (;;)
678                         {
679                         i=BIO_gets(bp,buf,254);
680                         if (i <= 0) break;
681
682                         while ((i >= 0) && (buf[i] <= ' ')) i--;
683                         buf[++i]='\n'; buf[++i]='\0';
684
685                         if (i != 65) end=1;
686                         if (strncmp(buf,"-----END ",9) == 0)
687                                 break;
688                         if (i > 65) break;
689                         if (!BUF_MEM_grow(dataB,i+bl+9))
690                                 {
691                                 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
692                                 goto err;
693                                 }
694                         strncpy(&(dataB->data[bl]),buf,(unsigned int)i);
695                         dataB->data[bl+i]='\0';
696                         bl+=i;
697                         if (end)
698                                 {
699                                 buf[0]='\0';
700                                 i=BIO_gets(bp,buf,254);
701                                 if (i <= 0) break;
702
703                                 while ((i >= 0) && (buf[i] <= ' ')) i--;
704                                 buf[++i]='\n'; buf[++i]='\0';
705
706                                 break;
707                                 }
708                         }
709                 }
710         else
711                 {
712                 tmpB=headerB;
713                 headerB=dataB;
714                 dataB=tmpB;
715                 bl=hl;
716                 }
717         i=strlen(nameB->data);
718         if (    (strncmp(buf,"-----END ",9) != 0) ||
719                 (strncmp(nameB->data,&(buf[9]),(unsigned int)i) != 0) ||
720                 (strncmp(&(buf[9+i]),"-----\n",6) != 0))
721                 {
722                 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE);
723                 goto err;
724                 }
725
726         EVP_DecodeInit(&ctx);
727         i=EVP_DecodeUpdate(&ctx,
728                 (unsigned char *)dataB->data,&bl,
729                 (unsigned char *)dataB->data,bl);
730         if (i < 0)
731                 {
732                 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);
733                 goto err;
734                 }
735         i=EVP_DecodeFinal(&ctx,(unsigned char *)&(dataB->data[bl]),&k);
736         if (i < 0)
737                 {
738                 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);
739                 goto err;
740                 }
741         bl+=k;
742
743         if (bl == 0) goto err;
744         *name=nameB->data;
745         *header=headerB->data;
746         *data=(unsigned char *)dataB->data;
747         *len=bl;
748         Free(nameB);
749         Free(headerB);
750         Free(dataB);
751         return(1);
752 err:
753         BUF_MEM_free(nameB);
754         BUF_MEM_free(headerB);
755         BUF_MEM_free(dataB);
756         return(0);
757         }