pem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Privacy Enhanced Mail (PEM) decoding
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  21. #include "mbedtls/pem.h"
  22. #include "mbedtls/base64.h"
  23. #include "mbedtls/des.h"
  24. #include "mbedtls/aes.h"
  25. #include "mbedtls/md.h"
  26. #include "mbedtls/cipher.h"
  27. #include "mbedtls/platform_util.h"
  28. #include "mbedtls/error.h"
  29. #include "hash_info.h"
  30. #include <string.h>
  31. #include "mbedtls/platform.h"
  32. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  33. #include "psa/crypto.h"
  34. #endif
  35. #if !defined(MBEDTLS_MD5_C)
  36. #include "mbedtls/psa_util.h"
  37. #define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
  38. psa_to_md_errors, \
  39. psa_generic_status_to_mbedtls)
  40. #endif
  41. #include "mbedtls/legacy_or_psa.h"
  42. #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
  43. defined(MBEDTLS_CIPHER_MODE_CBC) && \
  44. (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
  45. #define PEM_RFC1421
  46. #endif /* MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
  47. MBEDTLS_CIPHER_MODE_CBC &&
  48. ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
  49. #if defined(MBEDTLS_PEM_PARSE_C)
  50. void mbedtls_pem_init(mbedtls_pem_context *ctx)
  51. {
  52. memset(ctx, 0, sizeof(mbedtls_pem_context));
  53. }
  54. #if defined(PEM_RFC1421)
  55. /*
  56. * Read a 16-byte hex string and convert it to binary
  57. */
  58. static int pem_get_iv(const unsigned char *s, unsigned char *iv,
  59. size_t iv_len)
  60. {
  61. size_t i, j, k;
  62. memset(iv, 0, iv_len);
  63. for (i = 0; i < iv_len * 2; i++, s++) {
  64. if (*s >= '0' && *s <= '9') {
  65. j = *s - '0';
  66. } else
  67. if (*s >= 'A' && *s <= 'F') {
  68. j = *s - '7';
  69. } else
  70. if (*s >= 'a' && *s <= 'f') {
  71. j = *s - 'W';
  72. } else {
  73. return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
  74. }
  75. k = ((i & 1) != 0) ? j : j << 4;
  76. iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
  77. }
  78. return 0;
  79. }
  80. #if defined(MBEDTLS_MD5_C)
  81. static int pem_pbkdf1(unsigned char *key, size_t keylen,
  82. unsigned char *iv,
  83. const unsigned char *pwd, size_t pwdlen)
  84. {
  85. mbedtls_md_context_t md5_ctx;
  86. const mbedtls_md_info_t *md5_info;
  87. unsigned char md5sum[16];
  88. size_t use_len;
  89. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  90. mbedtls_md_init(&md5_ctx);
  91. /* Prepare the context. (setup() errors gracefully on NULL info.) */
  92. md5_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
  93. if ((ret = mbedtls_md_setup(&md5_ctx, md5_info, 0)) != 0) {
  94. goto exit;
  95. }
  96. /*
  97. * key[ 0..15] = MD5(pwd || IV)
  98. */
  99. if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
  100. goto exit;
  101. }
  102. if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
  103. goto exit;
  104. }
  105. if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
  106. goto exit;
  107. }
  108. if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
  109. goto exit;
  110. }
  111. if (keylen <= 16) {
  112. memcpy(key, md5sum, keylen);
  113. goto exit;
  114. }
  115. memcpy(key, md5sum, 16);
  116. /*
  117. * key[16..23] = MD5(key[ 0..15] || pwd || IV])
  118. */
  119. if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
  120. goto exit;
  121. }
  122. if ((ret = mbedtls_md_update(&md5_ctx, md5sum, 16)) != 0) {
  123. goto exit;
  124. }
  125. if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
  126. goto exit;
  127. }
  128. if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
  129. goto exit;
  130. }
  131. if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
  132. goto exit;
  133. }
  134. use_len = 16;
  135. if (keylen < 32) {
  136. use_len = keylen - 16;
  137. }
  138. memcpy(key + 16, md5sum, use_len);
  139. exit:
  140. mbedtls_md_free(&md5_ctx);
  141. mbedtls_platform_zeroize(md5sum, 16);
  142. return ret;
  143. }
  144. #else
  145. static int pem_pbkdf1(unsigned char *key, size_t keylen,
  146. unsigned char *iv,
  147. const unsigned char *pwd, size_t pwdlen)
  148. {
  149. unsigned char md5sum[16];
  150. psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
  151. size_t output_length = 0;
  152. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  153. if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
  154. goto exit;
  155. }
  156. if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
  157. goto exit;
  158. }
  159. if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
  160. goto exit;
  161. }
  162. if ((status = psa_hash_finish(&operation, md5sum,
  163. PSA_HASH_LENGTH(PSA_ALG_MD5),
  164. &output_length)) != PSA_SUCCESS) {
  165. goto exit;
  166. }
  167. if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
  168. goto exit;
  169. }
  170. /*
  171. * key[ 0..15] = MD5(pwd || IV)
  172. */
  173. if (keylen <= 16) {
  174. memcpy(key, md5sum, keylen);
  175. goto exit;
  176. }
  177. memcpy(key, md5sum, 16);
  178. /*
  179. * key[16..23] = MD5(key[ 0..15] || pwd || IV])
  180. */
  181. if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
  182. goto exit;
  183. }
  184. if ((status = psa_hash_update(&operation, md5sum, 16)) != PSA_SUCCESS) {
  185. goto exit;
  186. }
  187. if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
  188. goto exit;
  189. }
  190. if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
  191. goto exit;
  192. }
  193. if ((status = psa_hash_finish(&operation, md5sum,
  194. PSA_HASH_LENGTH(PSA_ALG_MD5),
  195. &output_length)) != PSA_SUCCESS) {
  196. goto exit;
  197. }
  198. if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
  199. goto exit;
  200. }
  201. size_t use_len = 16;
  202. if (keylen < 32) {
  203. use_len = keylen - 16;
  204. }
  205. memcpy(key + 16, md5sum, use_len);
  206. exit:
  207. mbedtls_platform_zeroize(md5sum, 16);
  208. return PSA_TO_MBEDTLS_ERR(status);
  209. }
  210. #endif /* MBEDTLS_MD5_C */
  211. #if defined(MBEDTLS_DES_C)
  212. /*
  213. * Decrypt with DES-CBC, using PBKDF1 for key derivation
  214. */
  215. static int pem_des_decrypt(unsigned char des_iv[8],
  216. unsigned char *buf, size_t buflen,
  217. const unsigned char *pwd, size_t pwdlen)
  218. {
  219. mbedtls_des_context des_ctx;
  220. unsigned char des_key[8];
  221. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  222. mbedtls_des_init(&des_ctx);
  223. if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
  224. goto exit;
  225. }
  226. if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
  227. goto exit;
  228. }
  229. ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
  230. des_iv, buf, buf);
  231. exit:
  232. mbedtls_des_free(&des_ctx);
  233. mbedtls_platform_zeroize(des_key, 8);
  234. return ret;
  235. }
  236. /*
  237. * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
  238. */
  239. static int pem_des3_decrypt(unsigned char des3_iv[8],
  240. unsigned char *buf, size_t buflen,
  241. const unsigned char *pwd, size_t pwdlen)
  242. {
  243. mbedtls_des3_context des3_ctx;
  244. unsigned char des3_key[24];
  245. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  246. mbedtls_des3_init(&des3_ctx);
  247. if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
  248. goto exit;
  249. }
  250. if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
  251. goto exit;
  252. }
  253. ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
  254. des3_iv, buf, buf);
  255. exit:
  256. mbedtls_des3_free(&des3_ctx);
  257. mbedtls_platform_zeroize(des3_key, 24);
  258. return ret;
  259. }
  260. #endif /* MBEDTLS_DES_C */
  261. #if defined(MBEDTLS_AES_C)
  262. /*
  263. * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
  264. */
  265. static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
  266. unsigned char *buf, size_t buflen,
  267. const unsigned char *pwd, size_t pwdlen)
  268. {
  269. mbedtls_aes_context aes_ctx;
  270. unsigned char aes_key[32];
  271. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  272. mbedtls_aes_init(&aes_ctx);
  273. if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
  274. goto exit;
  275. }
  276. if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
  277. goto exit;
  278. }
  279. ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
  280. aes_iv, buf, buf);
  281. exit:
  282. mbedtls_aes_free(&aes_ctx);
  283. mbedtls_platform_zeroize(aes_key, keylen);
  284. return ret;
  285. }
  286. #endif /* MBEDTLS_AES_C */
  287. #endif /* PEM_RFC1421 */
  288. int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
  289. const unsigned char *data, const unsigned char *pwd,
  290. size_t pwdlen, size_t *use_len)
  291. {
  292. int ret, enc;
  293. size_t len;
  294. unsigned char *buf;
  295. const unsigned char *s1, *s2, *end;
  296. #if defined(PEM_RFC1421)
  297. unsigned char pem_iv[16];
  298. mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
  299. #else
  300. ((void) pwd);
  301. ((void) pwdlen);
  302. #endif /* PEM_RFC1421 */
  303. if (ctx == NULL) {
  304. return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
  305. }
  306. s1 = (unsigned char *) strstr((const char *) data, header);
  307. if (s1 == NULL) {
  308. return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  309. }
  310. s2 = (unsigned char *) strstr((const char *) data, footer);
  311. if (s2 == NULL || s2 <= s1) {
  312. return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  313. }
  314. s1 += strlen(header);
  315. if (*s1 == ' ') {
  316. s1++;
  317. }
  318. if (*s1 == '\r') {
  319. s1++;
  320. }
  321. if (*s1 == '\n') {
  322. s1++;
  323. } else {
  324. return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  325. }
  326. end = s2;
  327. end += strlen(footer);
  328. if (*end == ' ') {
  329. end++;
  330. }
  331. if (*end == '\r') {
  332. end++;
  333. }
  334. if (*end == '\n') {
  335. end++;
  336. }
  337. *use_len = end - data;
  338. enc = 0;
  339. if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
  340. #if defined(PEM_RFC1421)
  341. enc++;
  342. s1 += 22;
  343. if (*s1 == '\r') {
  344. s1++;
  345. }
  346. if (*s1 == '\n') {
  347. s1++;
  348. } else {
  349. return MBEDTLS_ERR_PEM_INVALID_DATA;
  350. }
  351. #if defined(MBEDTLS_DES_C)
  352. if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
  353. enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
  354. s1 += 23;
  355. if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
  356. return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
  357. }
  358. s1 += 16;
  359. } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
  360. enc_alg = MBEDTLS_CIPHER_DES_CBC;
  361. s1 += 18;
  362. if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
  363. return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
  364. }
  365. s1 += 16;
  366. }
  367. #endif /* MBEDTLS_DES_C */
  368. #if defined(MBEDTLS_AES_C)
  369. if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
  370. if (s2 - s1 < 22) {
  371. return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
  372. } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
  373. enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
  374. } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
  375. enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
  376. } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
  377. enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
  378. } else {
  379. return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
  380. }
  381. s1 += 22;
  382. if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
  383. return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
  384. }
  385. s1 += 32;
  386. }
  387. #endif /* MBEDTLS_AES_C */
  388. if (enc_alg == MBEDTLS_CIPHER_NONE) {
  389. return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
  390. }
  391. if (*s1 == '\r') {
  392. s1++;
  393. }
  394. if (*s1 == '\n') {
  395. s1++;
  396. } else {
  397. return MBEDTLS_ERR_PEM_INVALID_DATA;
  398. }
  399. #else
  400. return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
  401. #endif /* PEM_RFC1421 */
  402. }
  403. if (s1 >= s2) {
  404. return MBEDTLS_ERR_PEM_INVALID_DATA;
  405. }
  406. ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
  407. if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
  408. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
  409. }
  410. if ((buf = mbedtls_calloc(1, len)) == NULL) {
  411. return MBEDTLS_ERR_PEM_ALLOC_FAILED;
  412. }
  413. if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
  414. mbedtls_platform_zeroize(buf, len);
  415. mbedtls_free(buf);
  416. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
  417. }
  418. if (enc != 0) {
  419. #if defined(PEM_RFC1421)
  420. if (pwd == NULL) {
  421. mbedtls_platform_zeroize(buf, len);
  422. mbedtls_free(buf);
  423. return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
  424. }
  425. ret = 0;
  426. #if defined(MBEDTLS_DES_C)
  427. if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
  428. ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
  429. } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
  430. ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
  431. }
  432. #endif /* MBEDTLS_DES_C */
  433. #if defined(MBEDTLS_AES_C)
  434. if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
  435. ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
  436. } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
  437. ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
  438. } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
  439. ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
  440. }
  441. #endif /* MBEDTLS_AES_C */
  442. if (ret != 0) {
  443. mbedtls_free(buf);
  444. return ret;
  445. }
  446. /*
  447. * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
  448. * length bytes (allow 4 to be sure) in all known use cases.
  449. *
  450. * Use that as a heuristic to try to detect password mismatches.
  451. */
  452. if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
  453. mbedtls_platform_zeroize(buf, len);
  454. mbedtls_free(buf);
  455. return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
  456. }
  457. #else
  458. mbedtls_platform_zeroize(buf, len);
  459. mbedtls_free(buf);
  460. return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
  461. #endif /* PEM_RFC1421 */
  462. }
  463. ctx->buf = buf;
  464. ctx->buflen = len;
  465. return 0;
  466. }
  467. void mbedtls_pem_free(mbedtls_pem_context *ctx)
  468. {
  469. if (ctx->buf != NULL) {
  470. mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
  471. mbedtls_free(ctx->buf);
  472. }
  473. mbedtls_free(ctx->info);
  474. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
  475. }
  476. #endif /* MBEDTLS_PEM_PARSE_C */
  477. #if defined(MBEDTLS_PEM_WRITE_C)
  478. int mbedtls_pem_write_buffer(const char *header, const char *footer,
  479. const unsigned char *der_data, size_t der_len,
  480. unsigned char *buf, size_t buf_len, size_t *olen)
  481. {
  482. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  483. unsigned char *encode_buf = NULL, *c, *p = buf;
  484. size_t len = 0, use_len, add_len = 0;
  485. mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
  486. add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
  487. if (use_len + add_len > buf_len) {
  488. *olen = use_len + add_len;
  489. return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
  490. }
  491. if (use_len != 0 &&
  492. ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
  493. return MBEDTLS_ERR_PEM_ALLOC_FAILED;
  494. }
  495. if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
  496. der_len)) != 0) {
  497. mbedtls_free(encode_buf);
  498. return ret;
  499. }
  500. memcpy(p, header, strlen(header));
  501. p += strlen(header);
  502. c = encode_buf;
  503. while (use_len) {
  504. len = (use_len > 64) ? 64 : use_len;
  505. memcpy(p, c, len);
  506. use_len -= len;
  507. p += len;
  508. c += len;
  509. *p++ = '\n';
  510. }
  511. memcpy(p, footer, strlen(footer));
  512. p += strlen(footer);
  513. *p++ = '\0';
  514. *olen = p - buf;
  515. /* Clean any remaining data previously written to the buffer */
  516. memset(buf + *olen, 0, buf_len - *olen);
  517. mbedtls_free(encode_buf);
  518. return 0;
  519. }
  520. #endif /* MBEDTLS_PEM_WRITE_C */
  521. #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */