blob: 7e7f86ff53153fe99cbd9c63668472ea28c692ec [file] [log] [blame]
Paul Bakker96743fc2011-02-12 14:30:57 +00001/*
2 * Privacy Enhanced Mail (PEM) decoding
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker96743fc2011-02-12 14:30:57 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker96743fc2011-02-12 14:30:57 +00009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
Rich Evansce2f2372015-02-06 13:57:42 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/pem.h"
13#include "mbedtls/base64.h"
14#include "mbedtls/des.h"
15#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010016#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050018#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000019#include "mbedtls/error.h"
Valerio Setti2653e922024-02-08 17:51:00 +010020#include "mbedtls/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000021
Rich Evans00ab4702015-02-06 13:43:58 +000022#include <string.h>
23
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020025
Przemek Stekiela68d08f2022-08-04 08:42:06 +020026#if defined(MBEDTLS_USE_PSA_CRYPTO)
27#include "psa/crypto.h"
28#endif
29
Manuel Pégourié-Gonnard52d02a82023-03-16 10:24:47 +010030#if defined(MBEDTLS_MD_CAN_MD5) && \
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020031 defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010032 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Przemek Stekiel4092ff92022-08-11 08:49:21 +020033#define PEM_RFC1421
Manuel Pégourié-Gonnard52d02a82023-03-16 10:24:47 +010034#endif /* MBEDTLS_MD_CAN_MD5 &&
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020035 MBEDTLS_CIPHER_MODE_CBC &&
Przemek Stekiel4092ff92022-08-11 08:49:21 +020036 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
37
Andres AGc0db5112016-12-07 15:05:53 +000038#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010039void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000040{
Gilles Peskine449bd832023-01-11 14:50:10 +010041 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000042}
43
Przemek Stekiel0cd6f082022-08-18 12:38:30 +020044#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +000045/*
46 * Read a 16-byte hex string and convert it to binary
47 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048static int pem_get_iv(const unsigned char *s, unsigned char *iv,
49 size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000050{
Paul Bakker23986e52011-04-24 08:57:21 +000051 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000052
Gilles Peskine449bd832023-01-11 14:50:10 +010053 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 for (i = 0; i < iv_len * 2; i++, s++) {
56 if (*s >= '0' && *s <= '9') {
57 j = *s - '0';
58 } else
59 if (*s >= 'A' && *s <= 'F') {
60 j = *s - '7';
61 } else
62 if (*s >= 'a' && *s <= 'f') {
63 j = *s - 'W';
64 } else {
65 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
66 }
Paul Bakker96743fc2011-02-12 14:30:57 +000067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000071 }
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000074}
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076static int pem_pbkdf1(unsigned char *key, size_t keylen,
77 unsigned char *iv,
78 const unsigned char *pwd, size_t pwdlen)
Przemek Stekielbe92bee2022-08-04 10:38:34 +020079{
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010080 mbedtls_md_context_t md5_ctx;
81 const mbedtls_md_info_t *md5_info;
Przemek Stekielbe92bee2022-08-04 10:38:34 +020082 unsigned char md5sum[16];
83 size_t use_len;
84 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
85
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010086 mbedtls_md_init(&md5_ctx);
87
88 /* Prepare the context. (setup() errors gracefully on NULL info.) */
89 md5_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
90 if ((ret = mbedtls_md_setup(&md5_ctx, md5_info, 0)) != 0) {
91 goto exit;
92 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +020093
94 /*
95 * key[ 0..15] = MD5(pwd || IV)
96 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010097 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +020098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100100 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100103 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100106 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200107 goto exit;
108 }
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (keylen <= 16) {
111 memcpy(key, md5sum, keylen);
112 goto exit;
113 }
114
115 memcpy(key, md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200116
117 /*
118 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
119 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100120 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100123 if ((ret = mbedtls_md_update(&md5_ctx, md5sum, 16)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100126 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100129 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100132 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200135
136 use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (keylen < 32) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200138 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 memcpy(key + 16, md5sum, use_len);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200142
143exit:
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100144 mbedtls_md_free(&md5_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return ret;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200148}
Paul Bakker96743fc2011-02-12 14:30:57 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000151/*
152 * Decrypt with DES-CBC, using PBKDF1 for key derivation
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154static int pem_des_decrypt(unsigned char des_iv[8],
155 unsigned char *buf, size_t buflen,
156 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000159 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 }
171 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
172 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000173
Andres AG51a7ae12017-02-22 16:23:26 +0000174exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 mbedtls_des_free(&des_ctx);
176 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000179}
180
181/*
182 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184static int pem_des3_decrypt(unsigned char des3_iv[8],
185 unsigned char *buf, size_t buflen,
186 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000189 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000199 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 }
201 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
202 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000203
Andres AG51a7ae12017-02-22 16:23:26 +0000204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_des3_free(&des3_ctx);
206 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000213/*
214 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
217 unsigned char *buf, size_t buflen,
218 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000221 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000231 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 }
233 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
234 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000235
Andres AG51a7ae12017-02-22 16:23:26 +0000236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_aes_free(&aes_ctx);
238 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000243
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200244#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
247 const unsigned char *data, const unsigned char *pwd,
248 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000249{
Paul Bakker23986e52011-04-24 08:57:21 +0000250 int ret, enc;
251 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000252 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200253 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200254#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000255 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000257#else
258 ((void) pwd);
259 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200260#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (ctx == NULL) {
263 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
264 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (s1 == NULL) {
269 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
270 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (s2 == NULL || s2 <= s1) {
275 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
276 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 s1 += strlen(header);
279 if (*s1 == ' ') {
280 s1++;
281 }
282 if (*s1 == '\r') {
283 s1++;
284 }
285 if (*s1 == '\n') {
286 s1++;
287 } else {
288 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
289 }
Paul Bakker00b28602013-06-24 13:02:41 +0200290
291 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 end += strlen(footer);
293 if (*end == ' ') {
294 end++;
295 }
296 if (*end == '\r') {
297 end++;
298 }
299 if (*end == '\n') {
300 end++;
301 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000302 *use_len = (size_t) (end - data);
Paul Bakker96743fc2011-02-12 14:30:57 +0000303
304 enc = 0;
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200307#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000308 enc++;
309
310 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (*s1 == '\r') {
312 s1++;
313 }
314 if (*s1 == '\n') {
315 s1++;
316 } else {
317 return MBEDTLS_ERR_PEM_INVALID_DATA;
318 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000319
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000324
325 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
327 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
328 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000329
330 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000333
334 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
336 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
337 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000338
339 s1 += 16;
340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
345 if (s2 - s1 < 22) {
346 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
347 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 } else {
354 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
355 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000356
357 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
359 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
360 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000361
362 s1 += 32;
363 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (enc_alg == MBEDTLS_CIPHER_NONE) {
367 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
368 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (*s1 == '\r') {
371 s1++;
372 }
373 if (*s1 == '\n') {
374 s1++;
375 } else {
376 return MBEDTLS_ERR_PEM_INVALID_DATA;
377 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000378#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200380#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000381 }
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (s1 >= s2) {
384 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000385 }
Paul Bakkercff68422013-09-15 20:43:33 +0200386
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000387 ret = mbedtls_base64_decode(NULL, 0, &len, s1, (size_t) (s2 - s1));
Gilles Peskine449bd832023-01-11 14:50:10 +0100388
389 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
390 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
391 }
392
393 if ((buf = mbedtls_calloc(1, len)) == NULL) {
394 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
395 }
396
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000397 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, (size_t) (s2 - s1))) != 0) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100398 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
400 }
401
402 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200403#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (pwd == NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100405 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000407 }
408
Andres AG51a7ae12017-02-22 16:23:26 +0000409 ret = 0;
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
413 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
414 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
415 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
416 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
421 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
422 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
423 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
424 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
425 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ret != 0) {
430 mbedtls_free(buf);
431 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000432 }
433
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200434 /*
Valerio Setti2653e922024-02-08 17:51:00 +0100435 * The result will be ASN.1 starting with a SEQUENCE tag. Parse it
436 * with ASN.1 functions in order to:
437 * - Have an heuristic guess about password mismatches.
438 * - Update len variable to the amount of valid data inside buf.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200439 */
Valerio Setti2653e922024-02-08 17:51:00 +0100440 unsigned char *p = buf;
441 ret = mbedtls_asn1_get_tag(&p, buf + len, &len,
442 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED);
443 if (ret != 0) {
444 mbedtls_free(buf);
445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
Paul Bakker96743fc2011-02-12 14:30:57 +0000446 }
Valerio Setti2653e922024-02-08 17:51:00 +0100447 /* Add also the sequence block (tag + len) to the total amount of valid data. */
448 len += (p - buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000449#else
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100450 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200452#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000453 }
454
455 ctx->buf = buf;
456 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000459}
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200462{
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (ctx->buf != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100464 mbedtls_zeroize_and_free(ctx->buf, ctx->buflen);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200469}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100473int mbedtls_pem_write_buffer(const char *header, const char *footer,
474 const unsigned char *der_data, size_t der_len,
475 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200476{
Janos Follath24eed8d2019-11-22 13:21:35 +0000477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000478 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100479 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
Jethro Beekman746df882023-05-03 14:49:28 +0200482 add_len = strlen(header) + strlen(footer) + (((use_len > 2) ? (use_len - 2) : 0) / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200485 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200487 }
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (use_len != 0 &&
490 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
491 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
495 der_len)) != 0) {
496 mbedtls_free(encode_buf);
497 return ret;
498 }
499
500 memcpy(p, header, strlen(header));
501 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200502 c = encode_buf;
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 while (use_len) {
505 len = (use_len > 64) ? 64 : use_len;
506 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200507 use_len -= len;
508 p += len;
509 c += len;
510 *p++ = '\n';
511 }
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 memcpy(p, footer, strlen(footer));
514 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200515
516 *p++ = '\0';
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000517 *olen = (size_t) (p - buf);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 /* Clean any remaining data previously written to the buffer */
520 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_free(encode_buf);
523 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_PEM_WRITE_C */
526#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */