summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/pem/pem_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/pem/pem_lib.c')
-rw-r--r--src/lib/libcrypto/pem/pem_lib.c329
1 files changed, 163 insertions, 166 deletions
diff --git a/src/lib/libcrypto/pem/pem_lib.c b/src/lib/libcrypto/pem/pem_lib.c
index 7a2c0ad83b..18b751a91a 100644
--- a/src/lib/libcrypto/pem/pem_lib.c
+++ b/src/lib/libcrypto/pem/pem_lib.c
@@ -58,45 +58,40 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include "buffer.h" 61#include <openssl/buffer.h>
62#include "objects.h" 62#include <openssl/objects.h>
63#include "evp.h" 63#include <openssl/evp.h>
64#include "rand.h" 64#include <openssl/rand.h>
65#include "x509.h" 65#include <openssl/x509.h>
66#include "pem.h" 66#include <openssl/pem.h>
67#ifndef NO_DES 67#include <openssl/pkcs12.h>
68#include "des.h" 68#ifndef OPENSSL_NO_DES
69#include <openssl/des.h>
69#endif 70#endif
70 71
71char *PEM_version="PEM part of SSLeay 0.9.0b 29-Jun-1998"; 72const char *PEM_version="PEM" OPENSSL_VERSION_PTEXT;
72 73
73#define MIN_LENGTH 4 74#define MIN_LENGTH 4
74 75
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
80static int def_callback(char *buf, int num, int w);
81static int load_iv(unsigned char **fromp,unsigned char *to, int num); 76static int load_iv(unsigned char **fromp,unsigned char *to, int num);
82#else 77static int check_pem(const char *nm, const char *name);
83static int def_callback();
84static int load_iv();
85#endif
86 78
87static int def_callback(buf, num, w) 79int PEM_def_callback(char *buf, int num, int w, void *key)
88char *buf;
89int num;
90int w;
91 { 80 {
92#ifdef NO_FP_API 81#ifdef OPENSSL_NO_FP_API
93 /* We should not ever call the default callback routine from 82 /* We should not ever call the default callback routine from
94 * windows. */ 83 * windows. */
95 PEMerr(PEM_F_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 84 PEMerr(PEM_F_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
96 return(-1); 85 return(-1);
97#else 86#else
98 int i,j; 87 int i,j;
99 char *prompt; 88 const char *prompt;
89 if(key) {
90 i=strlen(key);
91 i=(i > num)?num:i;
92 memcpy(buf,key,i);
93 return(i);
94 }
100 95
101 prompt=EVP_get_pw_prompt(); 96 prompt=EVP_get_pw_prompt();
102 if (prompt == NULL) 97 if (prompt == NULL)
@@ -123,11 +118,9 @@ int w;
123#endif 118#endif
124 } 119 }
125 120
126void PEM_proc_type(buf, type) 121void PEM_proc_type(char *buf, int type)
127char *buf;
128int type;
129 { 122 {
130 char *str; 123 const char *str;
131 124
132 if (type == PEM_TYPE_ENCRYPTED) 125 if (type == PEM_TYPE_ENCRYPTED)
133 str="ENCRYPTED"; 126 str="ENCRYPTED";
@@ -143,11 +136,7 @@ int type;
143 strcat(buf,"\n"); 136 strcat(buf,"\n");
144 } 137 }
145 138
146void PEM_dek_info(buf, type, len, str) 139void PEM_dek_info(char *buf, const char *type, int len, char *str)
147char *buf;
148char *type;
149int len;
150char *str;
151 { 140 {
152 static unsigned char map[17]="0123456789ABCDEF"; 141 static unsigned char map[17]="0123456789ABCDEF";
153 long i; 142 long i;
@@ -166,13 +155,9 @@ char *str;
166 buf[j+i*2+1]='\0'; 155 buf[j+i*2+1]='\0';
167 } 156 }
168 157
169#ifndef NO_FP_API 158#ifndef OPENSSL_NO_FP_API
170char *PEM_ASN1_read(d2i,name,fp, x, cb) 159char *PEM_ASN1_read(char *(*d2i)(), const char *name, FILE *fp, char **x,
171char *(*d2i)(); 160 pem_password_cb *cb, void *u)
172char *name;
173FILE *fp;
174char **x;
175int (*cb)();
176 { 161 {
177 BIO *b; 162 BIO *b;
178 char *ret; 163 char *ret;
@@ -183,74 +168,97 @@ int (*cb)();
183 return(0); 168 return(0);
184 } 169 }
185 BIO_set_fp(b,fp,BIO_NOCLOSE); 170 BIO_set_fp(b,fp,BIO_NOCLOSE);
186 ret=PEM_ASN1_read_bio(d2i,name,b,x,cb); 171 ret=PEM_ASN1_read_bio(d2i,name,b,x,cb,u);
187 BIO_free(b); 172 BIO_free(b);
188 return(ret); 173 return(ret);
189 } 174 }
190#endif 175#endif
191 176
192char *PEM_ASN1_read_bio(d2i,name,bp, x, cb) 177static int check_pem(const char *nm, const char *name)
193char *(*d2i)(); 178{
194char *name; 179 /* Normal matching nm and name */
195BIO *bp; 180 if (!strcmp(nm,name)) return 1;
196char **x; 181
197int (*cb)(); 182 /* Make PEM_STRING_EVP_PKEY match any private key */
183
184 if(!strcmp(nm,PEM_STRING_PKCS8) &&
185 !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
186
187 if(!strcmp(nm,PEM_STRING_PKCS8INF) &&
188 !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
189
190 if(!strcmp(nm,PEM_STRING_RSA) &&
191 !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
192
193 if(!strcmp(nm,PEM_STRING_DSA) &&
194 !strcmp(name,PEM_STRING_EVP_PKEY)) return 1;
195
196 /* Permit older strings */
197
198 if(!strcmp(nm,PEM_STRING_X509_OLD) &&
199 !strcmp(name,PEM_STRING_X509)) return 1;
200
201 if(!strcmp(nm,PEM_STRING_X509_REQ_OLD) &&
202 !strcmp(name,PEM_STRING_X509_REQ)) return 1;
203
204 /* Allow normal certs to be read as trusted certs */
205 if(!strcmp(nm,PEM_STRING_X509) &&
206 !strcmp(name,PEM_STRING_X509_TRUSTED)) return 1;
207
208 if(!strcmp(nm,PEM_STRING_X509_OLD) &&
209 !strcmp(name,PEM_STRING_X509_TRUSTED)) return 1;
210
211 /* Some CAs use PKCS#7 with CERTIFICATE headers */
212 if(!strcmp(nm, PEM_STRING_X509) &&
213 !strcmp(name, PEM_STRING_PKCS7)) return 1;
214
215 return 0;
216}
217
218int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char *name, BIO *bp,
219 pem_password_cb *cb, void *u)
198 { 220 {
199 EVP_CIPHER_INFO cipher; 221 EVP_CIPHER_INFO cipher;
200 char *nm=NULL,*header=NULL; 222 char *nm=NULL,*header=NULL;
201 unsigned char *p=NULL,*data=NULL; 223 unsigned char *data=NULL;
202 long len; 224 long len;
203 char *ret=NULL; 225 int ret = 0;
204 226
205 for (;;) 227 for (;;)
206 { 228 {
207 if (!PEM_read_bio(bp,&nm,&header,&data,&len)) return(NULL); 229 if (!PEM_read_bio(bp,&nm,&header,&data,&len)) {
208 if ( (strcmp(nm,name) == 0) || 230 if(ERR_GET_REASON(ERR_peek_error()) ==
209 ((strcmp(nm,PEM_STRING_RSA) == 0) && 231 PEM_R_NO_START_LINE)
210 (strcmp(name,PEM_STRING_EVP_PKEY) == 0)) || 232 ERR_add_error_data(2, "Expecting: ", name);
211 ((strcmp(nm,PEM_STRING_DSA) == 0) && 233 return 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 } 234 }
223 if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err; 235 if(check_pem(nm, name)) break;
224 if (!PEM_do_header(&cipher,data,&len,cb)) goto err; 236 OPENSSL_free(nm);
225 p=data; 237 OPENSSL_free(header);
226 if (strcmp(name,PEM_STRING_EVP_PKEY) == 0) 238 OPENSSL_free(data);
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 } 239 }
233 else 240 if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err;
234 ret=d2i(x,&p,len); 241 if (!PEM_do_header(&cipher,data,&len,cb,u)) goto err;
235 if (ret == NULL) 242
236 PEMerr(PEM_F_PEM_ASN1_READ_BIO,ERR_R_ASN1_LIB); 243 *pdata = data;
244 *plen = len;
245
246 if (pnm)
247 *pnm = nm;
248
249 ret = 1;
250
237err: 251err:
238 Free(nm); 252 if (!pnm) OPENSSL_free(nm);
239 Free(header); 253 OPENSSL_free(header);
240 Free(data); 254 if (!ret) OPENSSL_free(data);
241 return(ret); 255 return ret;
242 } 256 }
243 257
244#ifndef NO_FP_API 258#ifndef OPENSSL_NO_FP_API
245int PEM_ASN1_write(i2d,name,fp, x, enc, kstr, klen, callback) 259int PEM_ASN1_write(int (*i2d)(), const char *name, FILE *fp, char *x,
246int (*i2d)(); 260 const EVP_CIPHER *enc, unsigned char *kstr, int klen,
247char *name; 261 pem_password_cb *callback, void *u)
248FILE *fp;
249char *x;
250EVP_CIPHER *enc;
251unsigned char *kstr;
252int klen;
253int (*callback)();
254 { 262 {
255 BIO *b; 263 BIO *b;
256 int ret; 264 int ret;
@@ -261,27 +269,20 @@ int (*callback)();
261 return(0); 269 return(0);
262 } 270 }
263 BIO_set_fp(b,fp,BIO_NOCLOSE); 271 BIO_set_fp(b,fp,BIO_NOCLOSE);
264 ret=PEM_ASN1_write_bio(i2d,name,b,x,enc,kstr,klen,callback); 272 ret=PEM_ASN1_write_bio(i2d,name,b,x,enc,kstr,klen,callback,u);
265 BIO_free(b); 273 BIO_free(b);
266 return(ret); 274 return(ret);
267 } 275 }
268#endif 276#endif
269 277
270int PEM_ASN1_write_bio(i2d,name,bp, x, enc, kstr, klen, callback) 278int PEM_ASN1_write_bio(int (*i2d)(), const char *name, BIO *bp, char *x,
271int (*i2d)(); 279 const EVP_CIPHER *enc, unsigned char *kstr, int klen,
272char *name; 280 pem_password_cb *callback, void *u)
273BIO *bp;
274char *x;
275EVP_CIPHER *enc;
276unsigned char *kstr;
277int klen;
278int (*callback)();
279 { 281 {
280 EVP_CIPHER_CTX ctx; 282 EVP_CIPHER_CTX ctx;
281 int dsize=0,i,j,ret=0; 283 int dsize=0,i,j,ret=0;
282 unsigned char *p,*data=NULL; 284 unsigned char *p,*data=NULL;
283 char *objstr=NULL; 285 const char *objstr=NULL;
284#define PEM_BUFSIZE 1024
285 char buf[PEM_BUFSIZE]; 286 char buf[PEM_BUFSIZE];
286 unsigned char key[EVP_MAX_KEY_LENGTH]; 287 unsigned char key[EVP_MAX_KEY_LENGTH];
287 unsigned char iv[EVP_MAX_IV_LENGTH]; 288 unsigned char iv[EVP_MAX_IV_LENGTH];
@@ -303,7 +304,7 @@ int (*callback)();
303 goto err; 304 goto err;
304 } 305 }
305 /* dzise + 8 bytes are needed */ 306 /* dzise + 8 bytes are needed */
306 data=(unsigned char *)Malloc((unsigned int)dsize+20); 307 data=(unsigned char *)OPENSSL_malloc((unsigned int)dsize+20);
307 if (data == NULL) 308 if (data == NULL)
308 { 309 {
309 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE); 310 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE);
@@ -317,18 +318,23 @@ int (*callback)();
317 if (kstr == NULL) 318 if (kstr == NULL)
318 { 319 {
319 if (callback == NULL) 320 if (callback == NULL)
320 klen=def_callback(buf,PEM_BUFSIZE,1); 321 klen=PEM_def_callback(buf,PEM_BUFSIZE,1,u);
321 else 322 else
322 klen=(*callback)(buf,PEM_BUFSIZE,1); 323 klen=(*callback)(buf,PEM_BUFSIZE,1,u);
323 if (klen <= 0) 324 if (klen <= 0)
324 { 325 {
325 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_READ_KEY); 326 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_READ_KEY);
326 goto err; 327 goto err;
327 } 328 }
329#ifdef CHARSET_EBCDIC
330 /* Convert the pass phrase from EBCDIC */
331 ebcdic2ascii(buf, buf, klen);
332#endif
328 kstr=(unsigned char *)buf; 333 kstr=(unsigned char *)buf;
329 } 334 }
330 RAND_seed(data,i);/* put in the RSA key. */ 335 RAND_add(data,i,0);/* put in the RSA key. */
331 RAND_bytes(iv,8); /* Generate a salt */ 336 if (RAND_pseudo_bytes(iv,enc->iv_len) < 0) /* Generate a salt */
337 goto err;
332 /* The 'iv' is used as the iv and as a salt. It is 338 /* The 'iv' is used as the iv and as a salt. It is
333 * NOT taken from the BytesToKey function */ 339 * NOT taken from the BytesToKey function */
334 EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL); 340 EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL);
@@ -337,12 +343,14 @@ int (*callback)();
337 343
338 buf[0]='\0'; 344 buf[0]='\0';
339 PEM_proc_type(buf,PEM_TYPE_ENCRYPTED); 345 PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
340 PEM_dek_info(buf,objstr,8,(char *)iv); 346 PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv);
341 /* k=strlen(buf); */ 347 /* k=strlen(buf); */
342 348
343 EVP_EncryptInit(&ctx,enc,key,iv); 349 EVP_CIPHER_CTX_init(&ctx);
350 EVP_EncryptInit_ex(&ctx,enc,NULL,key,iv);
344 EVP_EncryptUpdate(&ctx,data,&j,data,i); 351 EVP_EncryptUpdate(&ctx,data,&j,data,i);
345 EVP_EncryptFinal(&ctx,&(data[j]),&i); 352 EVP_EncryptFinal_ex(&ctx,&(data[j]),&i);
353 EVP_CIPHER_CTX_cleanup(&ctx);
346 i+=j; 354 i+=j;
347 ret=1; 355 ret=1;
348 } 356 }
@@ -359,15 +367,12 @@ err:
359 memset((char *)&ctx,0,sizeof(ctx)); 367 memset((char *)&ctx,0,sizeof(ctx));
360 memset(buf,0,PEM_BUFSIZE); 368 memset(buf,0,PEM_BUFSIZE);
361 memset(data,0,(unsigned int)dsize); 369 memset(data,0,(unsigned int)dsize);
362 Free(data); 370 OPENSSL_free(data);
363 return(ret); 371 return(ret);
364 } 372 }
365 373
366int PEM_do_header(cipher, data, plen, callback) 374int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
367EVP_CIPHER_INFO *cipher; 375 pem_password_cb *callback,void *u)
368unsigned char *data;
369long *plen;
370int (*callback)();
371 { 376 {
372 int i,j,o,klen; 377 int i,j,o,klen;
373 long len; 378 long len;
@@ -379,21 +384,27 @@ int (*callback)();
379 384
380 if (cipher->cipher == NULL) return(1); 385 if (cipher->cipher == NULL) return(1);
381 if (callback == NULL) 386 if (callback == NULL)
382 klen=def_callback(buf,PEM_BUFSIZE,0); 387 klen=PEM_def_callback(buf,PEM_BUFSIZE,0,u);
383 else 388 else
384 klen=callback(buf,PEM_BUFSIZE,0); 389 klen=callback(buf,PEM_BUFSIZE,0,u);
385 if (klen <= 0) 390 if (klen <= 0)
386 { 391 {
387 PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_PASSWORD_READ); 392 PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_PASSWORD_READ);
388 return(0); 393 return(0);
389 } 394 }
395#ifdef CHARSET_EBCDIC
396 /* Convert the pass phrase from EBCDIC */
397 ebcdic2ascii(buf, buf, klen);
398#endif
399
390 EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]), 400 EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]),
391 (unsigned char *)buf,klen,1,key,NULL); 401 (unsigned char *)buf,klen,1,key,NULL);
392 402
393 j=(int)len; 403 j=(int)len;
394 EVP_DecryptInit(&ctx,cipher->cipher,key,&(cipher->iv[0])); 404 EVP_CIPHER_CTX_init(&ctx);
405 EVP_DecryptInit_ex(&ctx,cipher->cipher,NULL, key,&(cipher->iv[0]));
395 EVP_DecryptUpdate(&ctx,data,&i,data,j); 406 EVP_DecryptUpdate(&ctx,data,&i,data,j);
396 o=EVP_DecryptFinal(&ctx,&(data[i]),&j); 407 o=EVP_DecryptFinal_ex(&ctx,&(data[i]),&j);
397 EVP_CIPHER_CTX_cleanup(&ctx); 408 EVP_CIPHER_CTX_cleanup(&ctx);
398 memset((char *)buf,0,sizeof(buf)); 409 memset((char *)buf,0,sizeof(buf));
399 memset((char *)key,0,sizeof(key)); 410 memset((char *)key,0,sizeof(key));
@@ -407,12 +418,10 @@ int (*callback)();
407 return(1); 418 return(1);
408 } 419 }
409 420
410int PEM_get_EVP_CIPHER_INFO(header,cipher) 421int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
411char *header;
412EVP_CIPHER_INFO *cipher;
413 { 422 {
414 int o; 423 int o;
415 EVP_CIPHER *enc=NULL; 424 const EVP_CIPHER *enc=NULL;
416 char *p,c; 425 char *p,c;
417 426
418 cipher->cipher=NULL; 427 cipher->cipher=NULL;
@@ -438,9 +447,15 @@ EVP_CIPHER_INFO *cipher;
438 for (;;) 447 for (;;)
439 { 448 {
440 c= *header; 449 c= *header;
450#ifndef CHARSET_EBCDIC
441 if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') || 451 if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') ||
442 ((c >= '0') && (c <= '9')))) 452 ((c >= '0') && (c <= '9'))))
443 break; 453 break;
454#else
455 if (!( isupper(c) || (c == '-') ||
456 isdigit(c)))
457 break;
458#endif
444 header++; 459 header++;
445 } 460 }
446 *header='\0'; 461 *header='\0';
@@ -454,14 +469,12 @@ EVP_CIPHER_INFO *cipher;
454 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION); 469 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION);
455 return(0); 470 return(0);
456 } 471 }
457 if (!load_iv((unsigned char **)&header,&(cipher->iv[0]),8)) return(0); 472 if (!load_iv((unsigned char **)&header,&(cipher->iv[0]),enc->iv_len)) return(0);
458 473
459 return(1); 474 return(1);
460 } 475 }
461 476
462static int load_iv(fromp,to,num) 477static int load_iv(unsigned char **fromp, unsigned char *to, int num)
463unsigned char **fromp,*to;
464int num;
465 { 478 {
466 int v,i; 479 int v,i;
467 unsigned char *from; 480 unsigned char *from;
@@ -490,13 +503,9 @@ int num;
490 return(1); 503 return(1);
491 } 504 }
492 505
493#ifndef NO_FP_API 506#ifndef OPENSSL_NO_FP_API
494int PEM_write(fp, name, header, data,len) 507int PEM_write(FILE *fp, char *name, char *header, unsigned char *data,
495FILE *fp; 508 long len)
496char *name;
497char *header;
498unsigned char *data;
499long len;
500 { 509 {
501 BIO *b; 510 BIO *b;
502 int ret; 511 int ret;
@@ -513,12 +522,8 @@ long len;
513 } 522 }
514#endif 523#endif
515 524
516int PEM_write_bio(bp, name, header, data,len) 525int PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
517BIO *bp; 526 long len)
518char *name;
519char *header;
520unsigned char *data;
521long len;
522 { 527 {
523 int nlen,n,i,j,outl; 528 int nlen,n,i,j,outl;
524 unsigned char *buf; 529 unsigned char *buf;
@@ -541,7 +546,7 @@ long len;
541 goto err; 546 goto err;
542 } 547 }
543 548
544 buf=(unsigned char *)Malloc(PEM_BUFSIZE*8); 549 buf=(unsigned char *)OPENSSL_malloc(PEM_BUFSIZE*8);
545 if (buf == NULL) 550 if (buf == NULL)
546 { 551 {
547 reason=ERR_R_MALLOC_FAILURE; 552 reason=ERR_R_MALLOC_FAILURE;
@@ -561,7 +566,7 @@ long len;
561 } 566 }
562 EVP_EncodeFinal(&ctx,buf,&outl); 567 EVP_EncodeFinal(&ctx,buf,&outl);
563 if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err; 568 if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err;
564 Free(buf); 569 OPENSSL_free(buf);
565 if ( (BIO_write(bp,"-----END ",9) != 9) || 570 if ( (BIO_write(bp,"-----END ",9) != 9) ||
566 (BIO_write(bp,name,nlen) != nlen) || 571 (BIO_write(bp,name,nlen) != nlen) ||
567 (BIO_write(bp,"-----\n",6) != 6)) 572 (BIO_write(bp,"-----\n",6) != 6))
@@ -572,13 +577,9 @@ err:
572 return(0); 577 return(0);
573 } 578 }
574 579
575#ifndef NO_FP_API 580#ifndef OPENSSL_NO_FP_API
576int PEM_read(fp, name, header, data,len) 581int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
577FILE *fp; 582 long *len)
578char **name;
579char **header;
580unsigned char **data;
581long *len;
582 { 583 {
583 BIO *b; 584 BIO *b;
584 int ret; 585 int ret;
@@ -595,12 +596,8 @@ long *len;
595 } 596 }
596#endif 597#endif
597 598
598int PEM_read_bio(bp, name, header, data, len) 599int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
599BIO *bp; 600 long *len)
600char **name;
601char **header;
602unsigned char **data;
603long *len;
604 { 601 {
605 EVP_ENCODE_CTX ctx; 602 EVP_ENCODE_CTX ctx;
606 int end=0,i,k,bl=0,hl=0,nohead=0; 603 int end=0,i,k,bl=0,hl=0,nohead=0;
@@ -643,7 +640,7 @@ long *len;
643 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); 640 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
644 goto err; 641 goto err;
645 } 642 }
646 strncpy(nameB->data,&(buf[11]),(unsigned int)i-6); 643 memcpy(nameB->data,&(buf[11]),i-6);
647 nameB->data[i-6]='\0'; 644 nameB->data[i-6]='\0';
648 break; 645 break;
649 } 646 }
@@ -668,7 +665,7 @@ long *len;
668 nohead=1; 665 nohead=1;
669 break; 666 break;
670 } 667 }
671 strncpy(&(headerB->data[hl]),buf,(unsigned int)i); 668 memcpy(&(headerB->data[hl]),buf,i);
672 headerB->data[hl+i]='\0'; 669 headerB->data[hl+i]='\0';
673 hl+=i; 670 hl+=i;
674 } 671 }
@@ -696,7 +693,7 @@ long *len;
696 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); 693 PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
697 goto err; 694 goto err;
698 } 695 }
699 strncpy(&(dataB->data[bl]),buf,(unsigned int)i); 696 memcpy(&(dataB->data[bl]),buf,i);
700 dataB->data[bl+i]='\0'; 697 dataB->data[bl+i]='\0';
701 bl+=i; 698 bl+=i;
702 if (end) 699 if (end)
@@ -721,7 +718,7 @@ long *len;
721 } 718 }
722 i=strlen(nameB->data); 719 i=strlen(nameB->data);
723 if ( (strncmp(buf,"-----END ",9) != 0) || 720 if ( (strncmp(buf,"-----END ",9) != 0) ||
724 (strncmp(nameB->data,&(buf[9]),(unsigned int)i) != 0) || 721 (strncmp(nameB->data,&(buf[9]),i) != 0) ||
725 (strncmp(&(buf[9+i]),"-----\n",6) != 0)) 722 (strncmp(&(buf[9+i]),"-----\n",6) != 0))
726 { 723 {
727 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE); 724 PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE);
@@ -750,9 +747,9 @@ long *len;
750 *header=headerB->data; 747 *header=headerB->data;
751 *data=(unsigned char *)dataB->data; 748 *data=(unsigned char *)dataB->data;
752 *len=bl; 749 *len=bl;
753 Free(nameB); 750 OPENSSL_free(nameB);
754 Free(headerB); 751 OPENSSL_free(headerB);
755 Free(dataB); 752 OPENSSL_free(dataB);
756 return(1); 753 return(1);
757err: 754err:
758 BUF_MEM_free(nameB); 755 BUF_MEM_free(nameB);