Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

777 linhas
22 KiB

  1. /* crypto/pem/pem_lib.c */
  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. #include <assert.h>
  58. #include <ctype.h>
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <openssl/base64.h>
  62. #include <openssl/buf.h>
  63. #include <openssl/des.h>
  64. #include <openssl/err.h>
  65. #include <openssl/evp.h>
  66. #include <openssl/mem.h>
  67. #include <openssl/obj.h>
  68. #include <openssl/pem.h>
  69. #include <openssl/rand.h>
  70. #include <openssl/x509.h>
  71. #include "../internal.h"
  72. #define MIN_LENGTH 4
  73. static int load_iv(char **fromp, unsigned char *to, int num);
  74. static int check_pem(const char *nm, const char *name);
  75. void PEM_proc_type(char *buf, int type)
  76. {
  77. const char *str;
  78. if (type == PEM_TYPE_ENCRYPTED)
  79. str = "ENCRYPTED";
  80. else if (type == PEM_TYPE_MIC_CLEAR)
  81. str = "MIC-CLEAR";
  82. else if (type == PEM_TYPE_MIC_ONLY)
  83. str = "MIC-ONLY";
  84. else
  85. str = "BAD-TYPE";
  86. BUF_strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE);
  87. BUF_strlcat(buf, str, PEM_BUFSIZE);
  88. BUF_strlcat(buf, "\n", PEM_BUFSIZE);
  89. }
  90. void PEM_dek_info(char *buf, const char *type, int len, char *str)
  91. {
  92. static const unsigned char map[17] = "0123456789ABCDEF";
  93. long i;
  94. int j;
  95. BUF_strlcat(buf, "DEK-Info: ", PEM_BUFSIZE);
  96. BUF_strlcat(buf, type, PEM_BUFSIZE);
  97. BUF_strlcat(buf, ",", PEM_BUFSIZE);
  98. j = strlen(buf);
  99. if (j + (len * 2) + 1 > PEM_BUFSIZE)
  100. return;
  101. for (i = 0; i < len; i++) {
  102. buf[j + i * 2] = map[(str[i] >> 4) & 0x0f];
  103. buf[j + i * 2 + 1] = map[(str[i]) & 0x0f];
  104. }
  105. buf[j + i * 2] = '\n';
  106. buf[j + i * 2 + 1] = '\0';
  107. }
  108. #ifndef OPENSSL_NO_FP_API
  109. void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
  110. pem_password_cb *cb, void *u)
  111. {
  112. BIO *b;
  113. void *ret;
  114. if ((b = BIO_new(BIO_s_file())) == NULL) {
  115. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  116. return (0);
  117. }
  118. BIO_set_fp(b, fp, BIO_NOCLOSE);
  119. ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
  120. BIO_free(b);
  121. return (ret);
  122. }
  123. #endif
  124. static int check_pem(const char *nm, const char *name)
  125. {
  126. /* Normal matching nm and name */
  127. if (!strcmp(nm, name))
  128. return 1;
  129. /* Make PEM_STRING_EVP_PKEY match any private key */
  130. if (!strcmp(name, PEM_STRING_EVP_PKEY)) {
  131. return !strcmp(nm, PEM_STRING_PKCS8) ||
  132. !strcmp(nm, PEM_STRING_PKCS8INF) ||
  133. !strcmp(nm, PEM_STRING_RSA) ||
  134. !strcmp(nm, PEM_STRING_EC) ||
  135. !strcmp(nm, PEM_STRING_DSA);
  136. }
  137. /* Permit older strings */
  138. if (!strcmp(nm, PEM_STRING_X509_OLD) && !strcmp(name, PEM_STRING_X509))
  139. return 1;
  140. if (!strcmp(nm, PEM_STRING_X509_REQ_OLD) &&
  141. !strcmp(name, PEM_STRING_X509_REQ))
  142. return 1;
  143. /* Allow normal certs to be read as trusted certs */
  144. if (!strcmp(nm, PEM_STRING_X509) &&
  145. !strcmp(name, PEM_STRING_X509_TRUSTED))
  146. return 1;
  147. if (!strcmp(nm, PEM_STRING_X509_OLD) &&
  148. !strcmp(name, PEM_STRING_X509_TRUSTED))
  149. return 1;
  150. /* Some CAs use PKCS#7 with CERTIFICATE headers */
  151. if (!strcmp(nm, PEM_STRING_X509) && !strcmp(name, PEM_STRING_PKCS7))
  152. return 1;
  153. if (!strcmp(nm, PEM_STRING_PKCS7_SIGNED) &&
  154. !strcmp(name, PEM_STRING_PKCS7))
  155. return 1;
  156. #ifndef OPENSSL_NO_CMS
  157. if (!strcmp(nm, PEM_STRING_X509) && !strcmp(name, PEM_STRING_CMS))
  158. return 1;
  159. /* Allow CMS to be read from PKCS#7 headers */
  160. if (!strcmp(nm, PEM_STRING_PKCS7) && !strcmp(name, PEM_STRING_CMS))
  161. return 1;
  162. #endif
  163. return 0;
  164. }
  165. int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
  166. const char *name, BIO *bp, pem_password_cb *cb,
  167. void *u)
  168. {
  169. EVP_CIPHER_INFO cipher;
  170. char *nm = NULL, *header = NULL;
  171. unsigned char *data = NULL;
  172. long len;
  173. int ret = 0;
  174. for (;;) {
  175. if (!PEM_read_bio(bp, &nm, &header, &data, &len)) {
  176. if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
  177. ERR_add_error_data(2, "Expecting: ", name);
  178. return 0;
  179. }
  180. if (check_pem(nm, name))
  181. break;
  182. OPENSSL_free(nm);
  183. OPENSSL_free(header);
  184. OPENSSL_free(data);
  185. }
  186. if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
  187. goto err;
  188. if (!PEM_do_header(&cipher, data, &len, cb, u))
  189. goto err;
  190. *pdata = data;
  191. *plen = len;
  192. if (pnm)
  193. *pnm = nm;
  194. ret = 1;
  195. err:
  196. if (!ret || !pnm)
  197. OPENSSL_free(nm);
  198. OPENSSL_free(header);
  199. if (!ret)
  200. OPENSSL_free(data);
  201. return ret;
  202. }
  203. #ifndef OPENSSL_NO_FP_API
  204. int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
  205. void *x, const EVP_CIPHER *enc, unsigned char *kstr,
  206. int klen, pem_password_cb *callback, void *u)
  207. {
  208. BIO *b;
  209. int ret;
  210. if ((b = BIO_new(BIO_s_file())) == NULL) {
  211. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  212. return (0);
  213. }
  214. BIO_set_fp(b, fp, BIO_NOCLOSE);
  215. ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
  216. BIO_free(b);
  217. return (ret);
  218. }
  219. #endif
  220. int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
  221. void *x, const EVP_CIPHER *enc, unsigned char *kstr,
  222. int klen, pem_password_cb *callback, void *u)
  223. {
  224. EVP_CIPHER_CTX ctx;
  225. int dsize = 0, i, j, ret = 0;
  226. unsigned char *p, *data = NULL;
  227. const char *objstr = NULL;
  228. char buf[PEM_BUFSIZE];
  229. unsigned char key[EVP_MAX_KEY_LENGTH];
  230. unsigned char iv[EVP_MAX_IV_LENGTH];
  231. if (enc != NULL) {
  232. objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
  233. if (objstr == NULL || EVP_CIPHER_iv_length(enc) == 0) {
  234. OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER);
  235. goto err;
  236. }
  237. }
  238. if ((dsize = i2d(x, NULL)) < 0) {
  239. OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
  240. dsize = 0;
  241. goto err;
  242. }
  243. /* dzise + 8 bytes are needed */
  244. /* actually it needs the cipher block size extra... */
  245. data = (unsigned char *)OPENSSL_malloc((unsigned int)dsize + 20);
  246. if (data == NULL) {
  247. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  248. goto err;
  249. }
  250. p = data;
  251. i = i2d(x, &p);
  252. if (enc != NULL) {
  253. const unsigned iv_len = EVP_CIPHER_iv_length(enc);
  254. if (kstr == NULL) {
  255. klen = 0;
  256. if (!callback)
  257. callback = PEM_def_callback;
  258. klen = (*callback) (buf, PEM_BUFSIZE, 1, u);
  259. if (klen <= 0) {
  260. OPENSSL_PUT_ERROR(PEM, PEM_R_READ_KEY);
  261. goto err;
  262. }
  263. kstr = (unsigned char *)buf;
  264. }
  265. assert(iv_len <= (int)sizeof(iv));
  266. if (!RAND_bytes(iv, iv_len)) /* Generate a salt */
  267. goto err;
  268. /*
  269. * The 'iv' is used as the iv and as a salt. It is NOT taken from
  270. * the BytesToKey function
  271. */
  272. if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, key, NULL))
  273. goto err;
  274. if (kstr == (unsigned char *)buf)
  275. OPENSSL_cleanse(buf, PEM_BUFSIZE);
  276. assert(strlen(objstr) + 23 + 2 * iv_len + 13 <= sizeof buf);
  277. buf[0] = '\0';
  278. PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
  279. PEM_dek_info(buf, objstr, iv_len, (char *)iv);
  280. /* k=strlen(buf); */
  281. EVP_CIPHER_CTX_init(&ctx);
  282. ret = 1;
  283. if (!EVP_EncryptInit_ex(&ctx, enc, NULL, key, iv)
  284. || !EVP_EncryptUpdate(&ctx, data, &j, data, i)
  285. || !EVP_EncryptFinal_ex(&ctx, &(data[j]), &i))
  286. ret = 0;
  287. else
  288. i += j;
  289. EVP_CIPHER_CTX_cleanup(&ctx);
  290. if (ret == 0)
  291. goto err;
  292. } else {
  293. ret = 1;
  294. buf[0] = '\0';
  295. }
  296. i = PEM_write_bio(bp, name, buf, data, i);
  297. if (i <= 0)
  298. ret = 0;
  299. err:
  300. OPENSSL_cleanse(key, sizeof(key));
  301. OPENSSL_cleanse(iv, sizeof(iv));
  302. OPENSSL_cleanse((char *)&ctx, sizeof(ctx));
  303. OPENSSL_cleanse(buf, PEM_BUFSIZE);
  304. OPENSSL_free(data);
  305. return (ret);
  306. }
  307. int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
  308. pem_password_cb *callback, void *u)
  309. {
  310. int i = 0, j, o, klen;
  311. long len;
  312. EVP_CIPHER_CTX ctx;
  313. unsigned char key[EVP_MAX_KEY_LENGTH];
  314. char buf[PEM_BUFSIZE];
  315. len = *plen;
  316. if (cipher->cipher == NULL)
  317. return (1);
  318. klen = 0;
  319. if (!callback)
  320. callback = PEM_def_callback;
  321. klen = callback(buf, PEM_BUFSIZE, 0, u);
  322. if (klen <= 0) {
  323. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ);
  324. return (0);
  325. }
  326. if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]),
  327. (unsigned char *)buf, klen, 1, key, NULL))
  328. return 0;
  329. j = (int)len;
  330. EVP_CIPHER_CTX_init(&ctx);
  331. o = EVP_DecryptInit_ex(&ctx, cipher->cipher, NULL, key, &(cipher->iv[0]));
  332. if (o)
  333. o = EVP_DecryptUpdate(&ctx, data, &i, data, j);
  334. if (o)
  335. o = EVP_DecryptFinal_ex(&ctx, &(data[i]), &j);
  336. EVP_CIPHER_CTX_cleanup(&ctx);
  337. OPENSSL_cleanse((char *)buf, sizeof(buf));
  338. OPENSSL_cleanse((char *)key, sizeof(key));
  339. if (!o) {
  340. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_DECRYPT);
  341. return (0);
  342. }
  343. j += i;
  344. *plen = j;
  345. return (1);
  346. }
  347. static const EVP_CIPHER *cipher_by_name(const char *name)
  348. {
  349. /* This is similar to the (deprecated) function |EVP_get_cipherbyname|. */
  350. if (0 == strcmp(name, SN_rc4)) {
  351. return EVP_rc4();
  352. } else if (0 == strcmp(name, SN_des_cbc)) {
  353. return EVP_des_cbc();
  354. } else if (0 == strcmp(name, SN_des_ede3_cbc)) {
  355. return EVP_des_ede3_cbc();
  356. } else if (0 == strcmp(name, SN_aes_128_cbc)) {
  357. return EVP_aes_128_cbc();
  358. } else if (0 == strcmp(name, SN_aes_192_cbc)) {
  359. return EVP_aes_192_cbc();
  360. } else if (0 == strcmp(name, SN_aes_256_cbc)) {
  361. return EVP_aes_256_cbc();
  362. } else {
  363. return NULL;
  364. }
  365. }
  366. int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
  367. {
  368. const EVP_CIPHER *enc = NULL;
  369. char *p, c;
  370. char **header_pp = &header;
  371. cipher->cipher = NULL;
  372. if ((header == NULL) || (*header == '\0') || (*header == '\n'))
  373. return (1);
  374. if (strncmp(header, "Proc-Type: ", 11) != 0) {
  375. OPENSSL_PUT_ERROR(PEM, PEM_R_NOT_PROC_TYPE);
  376. return (0);
  377. }
  378. header += 11;
  379. if (*header != '4')
  380. return (0);
  381. header++;
  382. if (*header != ',')
  383. return (0);
  384. header++;
  385. if (strncmp(header, "ENCRYPTED", 9) != 0) {
  386. OPENSSL_PUT_ERROR(PEM, PEM_R_NOT_ENCRYPTED);
  387. return (0);
  388. }
  389. for (; (*header != '\n') && (*header != '\0'); header++) ;
  390. if (*header == '\0') {
  391. OPENSSL_PUT_ERROR(PEM, PEM_R_SHORT_HEADER);
  392. return (0);
  393. }
  394. header++;
  395. if (strncmp(header, "DEK-Info: ", 10) != 0) {
  396. OPENSSL_PUT_ERROR(PEM, PEM_R_NOT_DEK_INFO);
  397. return (0);
  398. }
  399. header += 10;
  400. p = header;
  401. for (;;) {
  402. c = *header;
  403. if (!(((c >= 'A') && (c <= 'Z')) || (c == '-') ||
  404. ((c >= '0') && (c <= '9'))))
  405. break;
  406. header++;
  407. }
  408. *header = '\0';
  409. cipher->cipher = enc = cipher_by_name(p);
  410. *header = c;
  411. header++;
  412. if (enc == NULL) {
  413. OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_ENCRYPTION);
  414. return (0);
  415. }
  416. if (!load_iv(header_pp, &(cipher->iv[0]), EVP_CIPHER_iv_length(enc)))
  417. return (0);
  418. return (1);
  419. }
  420. static int load_iv(char **fromp, unsigned char *to, int num)
  421. {
  422. int v, i;
  423. char *from;
  424. from = *fromp;
  425. for (i = 0; i < num; i++)
  426. to[i] = 0;
  427. num *= 2;
  428. for (i = 0; i < num; i++) {
  429. if ((*from >= '0') && (*from <= '9'))
  430. v = *from - '0';
  431. else if ((*from >= 'A') && (*from <= 'F'))
  432. v = *from - 'A' + 10;
  433. else if ((*from >= 'a') && (*from <= 'f'))
  434. v = *from - 'a' + 10;
  435. else {
  436. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_IV_CHARS);
  437. return (0);
  438. }
  439. from++;
  440. to[i / 2] |= v << (long)((!(i & 1)) * 4);
  441. }
  442. *fromp = from;
  443. return (1);
  444. }
  445. #ifndef OPENSSL_NO_FP_API
  446. int PEM_write(FILE *fp, const char *name, const char *header,
  447. const unsigned char *data, long len)
  448. {
  449. BIO *b;
  450. int ret;
  451. if ((b = BIO_new(BIO_s_file())) == NULL) {
  452. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  453. return (0);
  454. }
  455. BIO_set_fp(b, fp, BIO_NOCLOSE);
  456. ret = PEM_write_bio(b, name, header, data, len);
  457. BIO_free(b);
  458. return (ret);
  459. }
  460. #endif
  461. int PEM_write_bio(BIO *bp, const char *name, const char *header,
  462. const unsigned char *data, long len)
  463. {
  464. int nlen, n, i, j, outl;
  465. unsigned char *buf = NULL;
  466. EVP_ENCODE_CTX ctx;
  467. int reason = ERR_R_BUF_LIB;
  468. EVP_EncodeInit(&ctx);
  469. nlen = strlen(name);
  470. if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
  471. (BIO_write(bp, name, nlen) != nlen) ||
  472. (BIO_write(bp, "-----\n", 6) != 6))
  473. goto err;
  474. i = strlen(header);
  475. if (i > 0) {
  476. if ((BIO_write(bp, header, i) != i) || (BIO_write(bp, "\n", 1) != 1))
  477. goto err;
  478. }
  479. buf = OPENSSL_malloc(PEM_BUFSIZE * 8);
  480. if (buf == NULL) {
  481. reason = ERR_R_MALLOC_FAILURE;
  482. goto err;
  483. }
  484. i = j = 0;
  485. while (len > 0) {
  486. n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
  487. EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n);
  488. if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
  489. goto err;
  490. i += outl;
  491. len -= n;
  492. j += n;
  493. }
  494. EVP_EncodeFinal(&ctx, buf, &outl);
  495. if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
  496. goto err;
  497. OPENSSL_free(buf);
  498. buf = NULL;
  499. if ((BIO_write(bp, "-----END ", 9) != 9) ||
  500. (BIO_write(bp, name, nlen) != nlen) ||
  501. (BIO_write(bp, "-----\n", 6) != 6))
  502. goto err;
  503. return (i + outl);
  504. err:
  505. if (buf) {
  506. OPENSSL_free(buf);
  507. }
  508. OPENSSL_PUT_ERROR(PEM, reason);
  509. return (0);
  510. }
  511. #ifndef OPENSSL_NO_FP_API
  512. int PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
  513. long *len)
  514. {
  515. BIO *b;
  516. int ret;
  517. if ((b = BIO_new(BIO_s_file())) == NULL) {
  518. OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
  519. return (0);
  520. }
  521. BIO_set_fp(b, fp, BIO_NOCLOSE);
  522. ret = PEM_read_bio(b, name, header, data, len);
  523. BIO_free(b);
  524. return (ret);
  525. }
  526. #endif
  527. int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
  528. long *len)
  529. {
  530. EVP_ENCODE_CTX ctx;
  531. int end = 0, i, k, bl = 0, hl = 0, nohead = 0;
  532. char buf[256];
  533. BUF_MEM *nameB;
  534. BUF_MEM *headerB;
  535. BUF_MEM *dataB, *tmpB;
  536. nameB = BUF_MEM_new();
  537. headerB = BUF_MEM_new();
  538. dataB = BUF_MEM_new();
  539. if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {
  540. BUF_MEM_free(nameB);
  541. BUF_MEM_free(headerB);
  542. BUF_MEM_free(dataB);
  543. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  544. return (0);
  545. }
  546. buf[254] = '\0';
  547. for (;;) {
  548. i = BIO_gets(bp, buf, 254);
  549. if (i <= 0) {
  550. OPENSSL_PUT_ERROR(PEM, PEM_R_NO_START_LINE);
  551. goto err;
  552. }
  553. while ((i >= 0) && (buf[i] <= ' '))
  554. i--;
  555. buf[++i] = '\n';
  556. buf[++i] = '\0';
  557. if (strncmp(buf, "-----BEGIN ", 11) == 0) {
  558. i = strlen(&(buf[11]));
  559. if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0)
  560. continue;
  561. if (!BUF_MEM_grow(nameB, i + 9)) {
  562. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  563. goto err;
  564. }
  565. OPENSSL_memcpy(nameB->data, &(buf[11]), i - 6);
  566. nameB->data[i - 6] = '\0';
  567. break;
  568. }
  569. }
  570. hl = 0;
  571. if (!BUF_MEM_grow(headerB, 256)) {
  572. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  573. goto err;
  574. }
  575. headerB->data[0] = '\0';
  576. for (;;) {
  577. i = BIO_gets(bp, buf, 254);
  578. if (i <= 0)
  579. break;
  580. while ((i >= 0) && (buf[i] <= ' '))
  581. i--;
  582. buf[++i] = '\n';
  583. buf[++i] = '\0';
  584. if (buf[0] == '\n')
  585. break;
  586. if (!BUF_MEM_grow(headerB, hl + i + 9)) {
  587. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  588. goto err;
  589. }
  590. if (strncmp(buf, "-----END ", 9) == 0) {
  591. nohead = 1;
  592. break;
  593. }
  594. OPENSSL_memcpy(&(headerB->data[hl]), buf, i);
  595. headerB->data[hl + i] = '\0';
  596. hl += i;
  597. }
  598. bl = 0;
  599. if (!BUF_MEM_grow(dataB, 1024)) {
  600. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  601. goto err;
  602. }
  603. dataB->data[0] = '\0';
  604. if (!nohead) {
  605. for (;;) {
  606. i = BIO_gets(bp, buf, 254);
  607. if (i <= 0)
  608. break;
  609. while ((i >= 0) && (buf[i] <= ' '))
  610. i--;
  611. buf[++i] = '\n';
  612. buf[++i] = '\0';
  613. if (i != 65)
  614. end = 1;
  615. if (strncmp(buf, "-----END ", 9) == 0)
  616. break;
  617. if (i > 65)
  618. break;
  619. if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) {
  620. OPENSSL_PUT_ERROR(PEM, ERR_R_MALLOC_FAILURE);
  621. goto err;
  622. }
  623. OPENSSL_memcpy(&(dataB->data[bl]), buf, i);
  624. dataB->data[bl + i] = '\0';
  625. bl += i;
  626. if (end) {
  627. buf[0] = '\0';
  628. i = BIO_gets(bp, buf, 254);
  629. if (i <= 0)
  630. break;
  631. while ((i >= 0) && (buf[i] <= ' '))
  632. i--;
  633. buf[++i] = '\n';
  634. buf[++i] = '\0';
  635. break;
  636. }
  637. }
  638. } else {
  639. tmpB = headerB;
  640. headerB = dataB;
  641. dataB = tmpB;
  642. bl = hl;
  643. }
  644. i = strlen(nameB->data);
  645. if ((strncmp(buf, "-----END ", 9) != 0) ||
  646. (strncmp(nameB->data, &(buf[9]), i) != 0) ||
  647. (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) {
  648. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_END_LINE);
  649. goto err;
  650. }
  651. EVP_DecodeInit(&ctx);
  652. i = EVP_DecodeUpdate(&ctx,
  653. (unsigned char *)dataB->data, &bl,
  654. (unsigned char *)dataB->data, bl);
  655. if (i < 0) {
  656. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_BASE64_DECODE);
  657. goto err;
  658. }
  659. i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k);
  660. if (i < 0) {
  661. OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_BASE64_DECODE);
  662. goto err;
  663. }
  664. bl += k;
  665. if (bl == 0)
  666. goto err;
  667. *name = nameB->data;
  668. *header = headerB->data;
  669. *data = (unsigned char *)dataB->data;
  670. *len = bl;
  671. OPENSSL_free(nameB);
  672. OPENSSL_free(headerB);
  673. OPENSSL_free(dataB);
  674. return (1);
  675. err:
  676. BUF_MEM_free(nameB);
  677. BUF_MEM_free(headerB);
  678. BUF_MEM_free(dataB);
  679. return (0);
  680. }
  681. int PEM_def_callback(char *buf, int size, int rwflag, void *userdata)
  682. {
  683. if (!buf || !userdata || size < 0) {
  684. return 0;
  685. }
  686. size_t len = strlen((char *)userdata);
  687. if (len >= (size_t)size) {
  688. return 0;
  689. }
  690. BUF_strlcpy(buf, userdata, (size_t)size);
  691. return len;
  692. }