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