Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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