1 /* $OpenBSD: pem_lib.c,v 1.33 2014/07/11 08:44:49 jsing Exp $ */ 2 /* Copyright (C) 1995-1998 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 <ctype.h> 60 #include <stdio.h> 61 #include <string.h> 62 63 #include <openssl/opensslconf.h> 64 65 #include <openssl/buffer.h> 66 #include <openssl/evp.h> 67 #include <openssl/objects.h> 68 #include <openssl/pem.h> 69 #include <openssl/pkcs12.h> 70 #include <openssl/rand.h> 71 #include <openssl/x509.h> 72 73 #ifndef OPENSSL_NO_DES 74 #include <openssl/des.h> 75 #endif 76 #ifndef OPENSSL_NO_ENGINE 77 #include <openssl/engine.h> 78 #endif 79 80 #include "asn1_locl.h" 81 82 #define MIN_LENGTH 4 83 84 static int load_iv(char **fromp, unsigned char *to, int num); 85 static int check_pem(const char *nm, const char *name); 86 int pem_check_suffix(const char *pem_str, const char *suffix); 87 88 int 89 PEM_def_callback(char *buf, int num, int w, void *key) 90 { 91 int i, j; 92 const char *prompt; 93 94 if (key) { 95 i = strlen(key); 96 i = (i > num) ? num : i; 97 memcpy(buf, key, i); 98 return (i); 99 } 100 101 prompt = EVP_get_pw_prompt(); 102 if (prompt == NULL) 103 prompt = "Enter PEM pass phrase:"; 104 105 for (;;) { 106 i = EVP_read_pw_string_min(buf, MIN_LENGTH, num, prompt, w); 107 if (i != 0) { 108 PEMerr(PEM_F_PEM_DEF_CALLBACK, 109 PEM_R_PROBLEMS_GETTING_PASSWORD); 110 memset(buf, 0, num); 111 return (-1); 112 } 113 j = strlen(buf); 114 if (j < MIN_LENGTH) { 115 fprintf(stderr, "phrase is too short, needs to be at least %d chars\n", MIN_LENGTH); 116 } else 117 break; 118 } 119 return (j); 120 } 121 122 void 123 PEM_proc_type(char *buf, int type) 124 { 125 const char *str; 126 127 if (type == PEM_TYPE_ENCRYPTED) 128 str = "ENCRYPTED"; 129 else if (type == PEM_TYPE_MIC_CLEAR) 130 str = "MIC-CLEAR"; 131 else if (type == PEM_TYPE_MIC_ONLY) 132 str = "MIC-ONLY"; 133 else 134 str = "BAD-TYPE"; 135 136 strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE); 137 strlcat(buf, str, PEM_BUFSIZE); 138 strlcat(buf, "\n", PEM_BUFSIZE); 139 } 140 141 void 142 PEM_dek_info(char *buf, const char *type, int len, char *str) 143 { 144 static const unsigned char map[17] = "0123456789ABCDEF"; 145 long i; 146 int j; 147 148 strlcat(buf, "DEK-Info: ", PEM_BUFSIZE); 149 strlcat(buf, type, PEM_BUFSIZE); 150 strlcat(buf, ",", PEM_BUFSIZE); 151 j = strlen(buf); 152 if (j + (len * 2) + 1 > PEM_BUFSIZE) 153 return; 154 for (i = 0; i < len; i++) { 155 buf[j + i * 2] = map[(str[i] >> 4) & 0x0f]; 156 buf[j + i * 2 + 1] = map[(str[i]) & 0x0f]; 157 } 158 buf[j + i * 2] = '\n'; 159 buf[j + i * 2 + 1] = '\0'; 160 } 161 162 void * 163 PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, 164 pem_password_cb *cb, void *u) 165 { 166 BIO *b; 167 void *ret; 168 169 if ((b = BIO_new(BIO_s_file())) == NULL) { 170 PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB); 171 return (0); 172 } 173 BIO_set_fp(b, fp, BIO_NOCLOSE); 174 ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u); 175 BIO_free(b); 176 return (ret); 177 } 178 179 static int 180 check_pem(const char *nm, const char *name) 181 { 182 /* Normal matching nm and name */ 183 if (!strcmp(nm, name)) 184 return 1; 185 186 /* Make PEM_STRING_EVP_PKEY match any private key */ 187 188 if (!strcmp(name, PEM_STRING_EVP_PKEY)) { 189 int slen; 190 const EVP_PKEY_ASN1_METHOD *ameth; 191 if (!strcmp(nm, PEM_STRING_PKCS8)) 192 return 1; 193 if (!strcmp(nm, PEM_STRING_PKCS8INF)) 194 return 1; 195 slen = pem_check_suffix(nm, "PRIVATE KEY"); 196 if (slen > 0) { 197 /* NB: ENGINE implementations wont contain 198 * a deprecated old private key decode function 199 * so don't look for them. 200 */ 201 ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); 202 if (ameth && ameth->old_priv_decode) 203 return 1; 204 } 205 return 0; 206 } 207 208 if (!strcmp(name, PEM_STRING_PARAMETERS)) { 209 int slen; 210 const EVP_PKEY_ASN1_METHOD *ameth; 211 slen = pem_check_suffix(nm, "PARAMETERS"); 212 if (slen > 0) { 213 ENGINE *e; 214 ameth = EVP_PKEY_asn1_find_str(&e, nm, slen); 215 if (ameth) { 216 int r; 217 if (ameth->param_decode) 218 r = 1; 219 else 220 r = 0; 221 #ifndef OPENSSL_NO_ENGINE 222 if (e) 223 ENGINE_finish(e); 224 #endif 225 return r; 226 } 227 } 228 return 0; 229 } 230 231 /* Permit older strings */ 232 233 if (!strcmp(nm, PEM_STRING_X509_OLD) && 234 !strcmp(name, PEM_STRING_X509)) 235 return 1; 236 237 if (!strcmp(nm, PEM_STRING_X509_REQ_OLD) && 238 !strcmp(name, PEM_STRING_X509_REQ)) 239 return 1; 240 241 /* Allow normal certs to be read as trusted certs */ 242 if (!strcmp(nm, PEM_STRING_X509) && 243 !strcmp(name, PEM_STRING_X509_TRUSTED)) 244 return 1; 245 246 if (!strcmp(nm, PEM_STRING_X509_OLD) && 247 !strcmp(name, PEM_STRING_X509_TRUSTED)) 248 return 1; 249 250 /* Some CAs use PKCS#7 with CERTIFICATE headers */ 251 if (!strcmp(nm, PEM_STRING_X509) && 252 !strcmp(name, PEM_STRING_PKCS7)) 253 return 1; 254 255 if (!strcmp(nm, PEM_STRING_PKCS7_SIGNED) && 256 !strcmp(name, PEM_STRING_PKCS7)) 257 return 1; 258 259 #ifndef OPENSSL_NO_CMS 260 if (!strcmp(nm, PEM_STRING_X509) && 261 !strcmp(name, PEM_STRING_CMS)) 262 return 1; 263 /* Allow CMS to be read from PKCS#7 headers */ 264 if (!strcmp(nm, PEM_STRING_PKCS7) && 265 !strcmp(name, PEM_STRING_CMS)) 266 return 1; 267 #endif 268 269 return 0; 270 } 271 272 int 273 PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, 274 const char *name, BIO *bp, pem_password_cb *cb, void *u) 275 { 276 EVP_CIPHER_INFO cipher; 277 char *nm = NULL, *header = NULL; 278 unsigned char *data = NULL; 279 long len; 280 int ret = 0; 281 282 for (;;) { 283 if (!PEM_read_bio(bp, &nm, &header, &data, &len)) { 284 if (ERR_GET_REASON(ERR_peek_error()) == 285 PEM_R_NO_START_LINE) 286 ERR_asprintf_error_data("Expecting: %s", name); 287 return 0; 288 } 289 if (check_pem(nm, name)) 290 break; 291 free(nm); 292 free(header); 293 free(data); 294 } 295 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher)) 296 goto err; 297 if (!PEM_do_header(&cipher, data, &len, cb, u)) 298 goto err; 299 300 *pdata = data; 301 *plen = len; 302 303 if (pnm) 304 *pnm = nm; 305 306 ret = 1; 307 308 err: 309 if (!ret || !pnm) 310 free(nm); 311 free(header); 312 if (!ret) 313 free(data); 314 return ret; 315 } 316 317 int 318 PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x, 319 const EVP_CIPHER *enc, unsigned char *kstr, int klen, 320 pem_password_cb *callback, void *u) 321 { 322 BIO *b; 323 int ret; 324 325 if ((b = BIO_new(BIO_s_file())) == NULL) { 326 PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB); 327 return (0); 328 } 329 BIO_set_fp(b, fp, BIO_NOCLOSE); 330 ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u); 331 BIO_free(b); 332 return (ret); 333 } 334 335 int 336 PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, 337 const EVP_CIPHER *enc, unsigned char *kstr, int klen, 338 pem_password_cb *callback, void *u) 339 { 340 EVP_CIPHER_CTX ctx; 341 int dsize = 0, i, j, ret = 0; 342 unsigned char *p, *data = NULL; 343 const char *objstr = NULL; 344 char buf[PEM_BUFSIZE]; 345 unsigned char key[EVP_MAX_KEY_LENGTH]; 346 unsigned char iv[EVP_MAX_IV_LENGTH]; 347 348 if (enc != NULL) { 349 objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc)); 350 if (objstr == NULL) { 351 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, 352 PEM_R_UNSUPPORTED_CIPHER); 353 goto err; 354 } 355 } 356 357 if ((dsize = i2d(x, NULL)) < 0) { 358 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB); 359 dsize = 0; 360 goto err; 361 } 362 /* dzise + 8 bytes are needed */ 363 /* actually it needs the cipher block size extra... */ 364 data = malloc((unsigned int)dsize + 20); 365 if (data == NULL) { 366 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE); 367 goto err; 368 } 369 p = data; 370 i = i2d(x, &p); 371 372 if (enc != NULL) { 373 if (kstr == NULL) { 374 if (callback == NULL) 375 klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u); 376 else 377 klen = (*callback)(buf, PEM_BUFSIZE, 1, u); 378 if (klen <= 0) { 379 PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, 380 PEM_R_READ_KEY); 381 goto err; 382 } 383 kstr = (unsigned char *)buf; 384 } 385 OPENSSL_assert(enc->iv_len <= (int)sizeof(iv)); 386 if (RAND_pseudo_bytes(iv, enc->iv_len) < 0) /* Generate a salt */ 387 goto err; 388 /* The 'iv' is used as the iv and as a salt. It is 389 * NOT taken from the BytesToKey function */ 390 if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, 391 key, NULL)) 392 goto err; 393 394 if (kstr == (unsigned char *)buf) 395 OPENSSL_cleanse(buf, PEM_BUFSIZE); 396 397 OPENSSL_assert(strlen(objstr) + 23 + 398 2 * enc->iv_len + 13 <= sizeof buf); 399 400 buf[0] = '\0'; 401 PEM_proc_type(buf, PEM_TYPE_ENCRYPTED); 402 PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv); 403 /* k=strlen(buf); */ 404 405 EVP_CIPHER_CTX_init(&ctx); 406 ret = 1; 407 if (!EVP_EncryptInit_ex(&ctx, enc, NULL, key, iv) || 408 !EVP_EncryptUpdate(&ctx, data, &j, data, i) || 409 !EVP_EncryptFinal_ex(&ctx, &(data[j]), &i)) 410 ret = 0; 411 EVP_CIPHER_CTX_cleanup(&ctx); 412 if (ret == 0) 413 goto err; 414 i += j; 415 } else { 416 ret = 1; 417 buf[0] = '\0'; 418 } 419 i = PEM_write_bio(bp, name, buf, data, i); 420 if (i <= 0) 421 ret = 0; 422 err: 423 OPENSSL_cleanse(key, sizeof(key)); 424 OPENSSL_cleanse(iv, sizeof(iv)); 425 OPENSSL_cleanse((char *)&ctx, sizeof(ctx)); 426 OPENSSL_cleanse(buf, PEM_BUFSIZE); 427 if (data != NULL) { 428 OPENSSL_cleanse(data, (unsigned int)dsize); 429 free(data); 430 } 431 return (ret); 432 } 433 434 int 435 PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen, 436 pem_password_cb *callback, void *u) 437 { 438 int i, j, o, klen; 439 long len; 440 EVP_CIPHER_CTX ctx; 441 unsigned char key[EVP_MAX_KEY_LENGTH]; 442 char buf[PEM_BUFSIZE]; 443 444 len = *plen; 445 446 if (cipher->cipher == NULL) 447 return (1); 448 if (callback == NULL) 449 klen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u); 450 else 451 klen = callback(buf, PEM_BUFSIZE, 0, u); 452 if (klen <= 0) { 453 PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ); 454 return (0); 455 } 456 if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]), 457 (unsigned char *)buf, klen, 1, key, NULL)) 458 return 0; 459 460 j = (int)len; 461 EVP_CIPHER_CTX_init(&ctx); 462 o = EVP_DecryptInit_ex(&ctx, cipher->cipher, NULL, key, 463 &(cipher->iv[0])); 464 if (o) 465 o = EVP_DecryptUpdate(&ctx, data, &i, data, j); 466 if (o) 467 o = EVP_DecryptFinal_ex(&ctx, &(data[i]), &j); 468 EVP_CIPHER_CTX_cleanup(&ctx); 469 OPENSSL_cleanse((char *)buf, sizeof(buf)); 470 OPENSSL_cleanse((char *)key, sizeof(key)); 471 if (!o) { 472 PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT); 473 return (0); 474 } 475 *plen = j + i; 476 return (1); 477 } 478 479 int 480 PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher) 481 { 482 const EVP_CIPHER *enc = NULL; 483 char *p, c; 484 char **header_pp = &header; 485 486 cipher->cipher = NULL; 487 if ((header == NULL) || (*header == '\0') || (*header == '\n')) 488 return (1); 489 if (strncmp(header, "Proc-Type: ", 11) != 0) { 490 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE); 491 return (0); 492 } 493 header += 11; 494 if (*header != '4') 495 return (0); 496 header++; 497 if (*header != ',') 498 return (0); 499 header++; 500 if (strncmp(header, "ENCRYPTED", 9) != 0) { 501 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED); 502 return (0); 503 } 504 for (; (*header != '\n') && (*header != '\0'); header++) 505 ; 506 if (*header == '\0') { 507 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER); 508 return (0); 509 } 510 header++; 511 if (strncmp(header, "DEK-Info: ", 10) != 0) { 512 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO); 513 return (0); 514 } 515 header += 10; 516 517 p = header; 518 for (;;) { 519 c= *header; 520 if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') || 521 ((c >= '0') && (c <= '9')))) 522 break; 523 header++; 524 } 525 *header = '\0'; 526 cipher->cipher = enc = EVP_get_cipherbyname(p); 527 *header = c; 528 header++; 529 530 if (enc == NULL) { 531 PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, 532 PEM_R_UNSUPPORTED_ENCRYPTION); 533 return (0); 534 } 535 if (!load_iv(header_pp, &(cipher->iv[0]), enc->iv_len)) 536 return (0); 537 538 return (1); 539 } 540 541 static int 542 load_iv(char **fromp, unsigned char *to, int num) 543 { 544 int v, i; 545 char *from; 546 547 from= *fromp; 548 for (i = 0; i < num; i++) 549 to[i] = 0; 550 num *= 2; 551 for (i = 0; i < num; i++) { 552 if ((*from >= '0') && (*from <= '9')) 553 v = *from - '0'; 554 else if ((*from >= 'A') && (*from <= 'F')) 555 v = *from - 'A' + 10; 556 else if ((*from >= 'a') && (*from <= 'f')) 557 v = *from - 'a' + 10; 558 else { 559 PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS); 560 return (0); 561 } 562 from++; 563 to[i / 2] |= v << (long)((!(i & 1)) * 4); 564 } 565 566 *fromp = from; 567 return (1); 568 } 569 570 int 571 PEM_write(FILE *fp, char *name, char *header, unsigned char *data, long len) 572 { 573 BIO *b; 574 int ret; 575 576 if ((b = BIO_new(BIO_s_file())) == NULL) { 577 PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB); 578 return (0); 579 } 580 BIO_set_fp(b, fp, BIO_NOCLOSE); 581 ret = PEM_write_bio(b, name, header, data, len); 582 BIO_free(b); 583 return (ret); 584 } 585 586 int 587 PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data, 588 long len) 589 { 590 int nlen, n, i, j, outl; 591 unsigned char *buf = NULL; 592 EVP_ENCODE_CTX ctx; 593 int reason = ERR_R_BUF_LIB; 594 595 EVP_EncodeInit(&ctx); 596 nlen = strlen(name); 597 598 if ((BIO_write(bp, "-----BEGIN ", 11) != 11) || 599 (BIO_write(bp, name, nlen) != nlen) || 600 (BIO_write(bp, "-----\n", 6) != 6)) 601 goto err; 602 603 i = strlen(header); 604 if (i > 0) { 605 if ((BIO_write(bp, header, i) != i) || 606 (BIO_write(bp, "\n", 1) != 1)) 607 goto err; 608 } 609 610 buf = reallocarray(NULL, PEM_BUFSIZE, 8); 611 if (buf == NULL) { 612 reason = ERR_R_MALLOC_FAILURE; 613 goto err; 614 } 615 616 i = j = 0; 617 while (len > 0) { 618 n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len); 619 EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n); 620 if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl)) 621 goto err; 622 i += outl; 623 len -= n; 624 j += n; 625 } 626 EVP_EncodeFinal(&ctx, buf, &outl); 627 if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl)) 628 goto err; 629 OPENSSL_cleanse(buf, PEM_BUFSIZE * 8); 630 free(buf); 631 buf = NULL; 632 if ((BIO_write(bp, "-----END ", 9) != 9) || 633 (BIO_write(bp, name, nlen) != nlen) || 634 (BIO_write(bp, "-----\n", 6) != 6)) 635 goto err; 636 return (i + outl); 637 638 err: 639 if (buf) { 640 OPENSSL_cleanse(buf, PEM_BUFSIZE * 8); 641 free(buf); 642 } 643 PEMerr(PEM_F_PEM_WRITE_BIO, reason); 644 return (0); 645 } 646 647 int 648 PEM_read(FILE *fp, char **name, char **header, unsigned char **data, long *len) 649 { 650 BIO *b; 651 int ret; 652 653 if ((b = BIO_new(BIO_s_file())) == NULL) { 654 PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB); 655 return (0); 656 } 657 BIO_set_fp(b, fp, BIO_NOCLOSE); 658 ret = PEM_read_bio(b, name, header, data, len); 659 BIO_free(b); 660 return (ret); 661 } 662 663 int 664 PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, 665 long *len) 666 { 667 EVP_ENCODE_CTX ctx; 668 int end = 0, i, k, bl = 0, hl = 0, nohead = 0; 669 char buf[256]; 670 BUF_MEM *nameB; 671 BUF_MEM *headerB; 672 BUF_MEM *dataB, *tmpB; 673 674 nameB = BUF_MEM_new(); 675 headerB = BUF_MEM_new(); 676 dataB = BUF_MEM_new(); 677 if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) { 678 BUF_MEM_free(nameB); 679 BUF_MEM_free(headerB); 680 BUF_MEM_free(dataB); 681 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); 682 return (0); 683 } 684 685 buf[254] = '\0'; 686 for (;;) { 687 i = BIO_gets(bp, buf, 254); 688 689 if (i <= 0) { 690 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE); 691 goto err; 692 } 693 694 while ((i >= 0) && (buf[i] <= ' ')) 695 i--; 696 buf[++i] = '\n'; 697 buf[++i] = '\0'; 698 699 if (strncmp(buf, "-----BEGIN ", 11) == 0) { 700 i = strlen(&(buf[11])); 701 702 if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0) 703 continue; 704 if (!BUF_MEM_grow(nameB, i + 9)) { 705 PEMerr(PEM_F_PEM_READ_BIO, 706 ERR_R_MALLOC_FAILURE); 707 goto err; 708 } 709 memcpy(nameB->data, &(buf[11]), i - 6); 710 nameB->data[i - 6] = '\0'; 711 break; 712 } 713 } 714 hl = 0; 715 if (!BUF_MEM_grow(headerB, 256)) { 716 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); 717 goto err; 718 } 719 headerB->data[0] = '\0'; 720 for (;;) { 721 i = BIO_gets(bp, buf, 254); 722 if (i <= 0) 723 break; 724 725 while ((i >= 0) && (buf[i] <= ' ')) 726 i--; 727 buf[++i] = '\n'; 728 buf[++i] = '\0'; 729 730 if (buf[0] == '\n') 731 break; 732 if (!BUF_MEM_grow(headerB, hl + i + 9)) { 733 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); 734 goto err; 735 } 736 if (strncmp(buf, "-----END ", 9) == 0) { 737 nohead = 1; 738 break; 739 } 740 memcpy(&(headerB->data[hl]), buf, i); 741 headerB->data[hl + i] = '\0'; 742 hl += i; 743 } 744 745 bl = 0; 746 if (!BUF_MEM_grow(dataB, 1024)) { 747 PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); 748 goto err; 749 } 750 dataB->data[0] = '\0'; 751 if (!nohead) { 752 for (;;) { 753 i = BIO_gets(bp, buf, 254); 754 if (i <= 0) 755 break; 756 757 while ((i >= 0) && (buf[i] <= ' ')) 758 i--; 759 buf[++i] = '\n'; 760 buf[++i] = '\0'; 761 762 if (i != 65) 763 end = 1; 764 if (strncmp(buf, "-----END ", 9) == 0) 765 break; 766 if (i > 65) 767 break; 768 if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) { 769 PEMerr(PEM_F_PEM_READ_BIO, 770 ERR_R_MALLOC_FAILURE); 771 goto err; 772 } 773 memcpy(&(dataB->data[bl]), buf, i); 774 dataB->data[bl + i] = '\0'; 775 bl += i; 776 if (end) { 777 buf[0] = '\0'; 778 i = BIO_gets(bp, buf, 254); 779 if (i <= 0) 780 break; 781 782 while ((i >= 0) && (buf[i] <= ' ')) 783 i--; 784 buf[++i] = '\n'; 785 buf[++i] = '\0'; 786 787 break; 788 } 789 } 790 } else { 791 tmpB = headerB; 792 headerB = dataB; 793 dataB = tmpB; 794 bl = hl; 795 } 796 i = strlen(nameB->data); 797 if ((strncmp(buf, "-----END ", 9) != 0) || 798 (strncmp(nameB->data, &(buf[9]), i) != 0) || 799 (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) { 800 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE); 801 goto err; 802 } 803 804 EVP_DecodeInit(&ctx); 805 i = EVP_DecodeUpdate(&ctx, 806 (unsigned char *)dataB->data, &bl, 807 (unsigned char *)dataB->data, bl); 808 if (i < 0) { 809 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); 810 goto err; 811 } 812 i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k); 813 if (i < 0) { 814 PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); 815 goto err; 816 } 817 bl += k; 818 819 if (bl == 0) 820 goto err; 821 *name = nameB->data; 822 *header = headerB->data; 823 *data = (unsigned char *)dataB->data; 824 *len = bl; 825 free(nameB); 826 free(headerB); 827 free(dataB); 828 return (1); 829 830 err: 831 BUF_MEM_free(nameB); 832 BUF_MEM_free(headerB); 833 BUF_MEM_free(dataB); 834 return (0); 835 } 836 837 /* Check pem string and return prefix length. 838 * If for example the pem_str == "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" 839 * the return value is 3 for the string "RSA". 840 */ 841 842 int 843 pem_check_suffix(const char *pem_str, const char *suffix) 844 { 845 int pem_len = strlen(pem_str); 846 int suffix_len = strlen(suffix); 847 const char *p; 848 849 if (suffix_len + 1 >= pem_len) 850 return 0; 851 p = pem_str + pem_len - suffix_len; 852 if (strcmp(p, suffix)) 853 return 0; 854 p--; 855 if (*p != ' ') 856 return 0; 857 return p - pem_str; 858 } 859