blob: c1a47b0da47c7d331adae02bdacc6914a2bc8321 [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 Rodgman7ff79652023-11-03 12:04:52 +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"
16#include "mbedtls/md5.h"
17#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"
Paul Bakker96743fc2011-02-12 14:30:57 +000020
Rich Evans00ab4702015-02-06 13:43:58 +000021#include <string.h>
22
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020024
Andres AGc0db5112016-12-07 15:05:53 +000025#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000027{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010028 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000029}
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +000033/*
34 * Read a 16-byte hex string and convert it to binary
35 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036static int pem_get_iv(const unsigned char *s, unsigned char *iv,
37 size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000038{
Paul Bakker23986e52011-04-24 08:57:21 +000039 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043 for (i = 0; i < iv_len * 2; i++, s++) {
44 if (*s >= '0' && *s <= '9') {
45 j = *s - '0';
46 } else
47 if (*s >= 'A' && *s <= 'F') {
48 j = *s - '7';
49 } else
50 if (*s >= 'a' && *s <= 'f') {
51 j = *s - 'W';
52 } else {
53 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
54 }
Paul Bakker96743fc2011-02-12 14:30:57 +000055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058 iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000059 }
60
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000062}
63
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064static int pem_pbkdf1(unsigned char *key, size_t keylen,
65 unsigned char *iv,
66 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +000067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000069 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000070 size_t use_len;
Janos Follath24eed8d2019-11-22 13:21:35 +000071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +000072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_md5_init(&md5_ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020074
Paul Bakker96743fc2011-02-12 14:30:57 +000075 /*
76 * key[ 0..15] = MD5(pwd || IV)
77 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 if ((ret = mbedtls_md5_starts_ret(&md5_ctx)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010079 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 }
81 if ((ret = mbedtls_md5_update_ret(&md5_ctx, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010082 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 }
84 if ((ret = mbedtls_md5_update_ret(&md5_ctx, iv, 8)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010085 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 }
87 if ((ret = mbedtls_md5_finish_ret(&md5_ctx, md5sum)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010088 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +000089 }
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 if (keylen <= 16) {
92 memcpy(key, md5sum, keylen);
93 goto exit;
94 }
95
96 memcpy(key, md5sum, 16);
Paul Bakker96743fc2011-02-12 14:30:57 +000097
98 /*
99 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
100 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 if ((ret = mbedtls_md5_starts_ret(&md5_ctx)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100102 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 }
104 if ((ret = mbedtls_md5_update_ret(&md5_ctx, md5sum, 16)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100105 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 }
107 if ((ret = mbedtls_md5_update_ret(&md5_ctx, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100108 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 }
110 if ((ret = mbedtls_md5_update_ret(&md5_ctx, iv, 8)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100111 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 }
113 if ((ret = mbedtls_md5_finish_ret(&md5_ctx, md5sum)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100114 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000116
117 use_len = 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 if (keylen < 32) {
Paul Bakker96743fc2011-02-12 14:30:57 +0000119 use_len = keylen - 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 memcpy(key + 16, md5sum, use_len);
Paul Bakker96743fc2011-02-12 14:30:57 +0000123
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100124exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 mbedtls_md5_free(&md5_ctx);
126 mbedtls_platform_zeroize(md5sum, 16);
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000132/*
133 * Decrypt with DES-CBC, using PBKDF1 for key derivation
134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135static int pem_des_decrypt(unsigned char des_iv[8],
136 unsigned char *buf, size_t buflen,
137 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000140 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100146 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000150 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 }
152 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
153 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000154
Andres AG51a7ae12017-02-22 16:23:26 +0000155exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 mbedtls_des_free(&des_ctx);
157 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000160}
161
162/*
163 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
164 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165static int pem_des3_decrypt(unsigned char des3_iv[8],
166 unsigned char *buf, size_t buflen,
167 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000170 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100176 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000180 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 }
182 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
183 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000184
Andres AG51a7ae12017-02-22 16:23:26 +0000185exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 mbedtls_des3_free(&des3_ctx);
187 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000194/*
195 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
196 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
198 unsigned char *buf, size_t buflen,
199 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000202 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100208 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000212 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 }
214 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
215 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000216
Andres AG51a7ae12017-02-22 16:23:26 +0000217exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 mbedtls_aes_free(&aes_ctx);
219 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
226 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
229 const unsigned char *data, const unsigned char *pwd,
230 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000231{
Paul Bakker23986e52011-04-24 08:57:21 +0000232 int ret, enc;
233 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000234 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200235 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +0000238 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000240#else
241 ((void) pwd);
242 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
244 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 if (ctx == NULL) {
247 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
248 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (s1 == NULL) {
253 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
254 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (s2 == NULL || s2 <= s1) {
259 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
260 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 s1 += strlen(header);
263 if (*s1 == ' ') {
264 s1++;
265 }
266 if (*s1 == '\r') {
267 s1++;
268 }
269 if (*s1 == '\n') {
270 s1++;
271 } else {
272 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
273 }
Paul Bakker00b28602013-06-24 13:02:41 +0200274
275 end = s2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 end += strlen(footer);
277 if (*end == ' ') {
278 end++;
279 }
280 if (*end == '\r') {
281 end++;
282 }
283 if (*end == '\n') {
284 end++;
285 }
Paul Bakker00b28602013-06-24 13:02:41 +0200286 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000287
288 enc = 0;
289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +0000293 enc++;
294
295 s1 += 22;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 if (*s1 == '\r') {
297 s1++;
298 }
299 if (*s1 == '\n') {
300 s1++;
301 } else {
302 return MBEDTLS_ERR_PEM_INVALID_DATA;
303 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000304
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_DES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000309
310 s1 += 23;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
312 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
313 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000314
315 s1 += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000318
319 s1 += 18;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
321 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
322 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000323
324 s1 += 16;
325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
330 if (s2 - s1 < 22) {
331 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
332 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 } else {
339 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
340 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000341
342 s1 += 22;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
344 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
345 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000346
347 s1 += 32;
348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 if (enc_alg == MBEDTLS_CIPHER_NONE) {
352 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
353 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 if (*s1 == '\r') {
356 s1++;
357 }
358 if (*s1 == '\n') {
359 s1++;
360 } else {
361 return MBEDTLS_ERR_PEM_INVALID_DATA;
362 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000363#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
366 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000367 }
368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 if (s1 >= s2) {
370 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000371 }
Paul Bakkercff68422013-09-15 20:43:33 +0200372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
374
375 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
377 }
378
379 if ((buf = mbedtls_calloc(1, len)) == NULL) {
380 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
381 }
382
383 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
384 mbedtls_platform_zeroize(buf, len);
385 mbedtls_free(buf);
386 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
387 }
388
389 if (enc != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
392 if (pwd == NULL) {
393 mbedtls_platform_zeroize(buf, len);
394 mbedtls_free(buf);
395 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000396 }
397
Andres AG51a7ae12017-02-22 16:23:26 +0000398 ret = 0;
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_DES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
402 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
403 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
404 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
405 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
410 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
411 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
412 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
413 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
414 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
415 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 if (ret != 0) {
419 mbedtls_free(buf);
420 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000421 }
422
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200423 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200424 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
425 * length bytes (allow 4 to be sure) in all known use cases.
426 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000427 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200428 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
430 mbedtls_platform_zeroize(buf, len);
431 mbedtls_free(buf);
432 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000433 }
434#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 mbedtls_platform_zeroize(buf, len);
436 mbedtls_free(buf);
437 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
439 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000440 }
441
442 ctx->buf = buf;
443 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000446}
447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200449{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 if (ctx->buf != NULL) {
451 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
452 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500453 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461int mbedtls_pem_write_buffer(const char *header, const char *footer,
462 const unsigned char *der_data, size_t der_len,
463 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200464{
Janos Follath24eed8d2019-11-22 13:21:35 +0000465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000466 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100467 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
470 add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200473 *olen = use_len + add_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200475 }
476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 if (use_len != 0 &&
478 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
479 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200480 }
481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
483 der_len)) != 0) {
484 mbedtls_free(encode_buf);
485 return ret;
486 }
487
488 memcpy(p, header, strlen(header));
489 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200490 c = encode_buf;
491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 while (use_len) {
493 len = (use_len > 64) ? 64 : use_len;
494 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200495 use_len -= len;
496 p += len;
497 c += len;
498 *p++ = '\n';
499 }
500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 memcpy(p, footer, strlen(footer));
502 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200503
504 *p++ = '\0';
505 *olen = p - buf;
506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 /* Clean any remaining data previously written to the buffer */
508 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 mbedtls_free(encode_buf);
511 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_PEM_WRITE_C */
514#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */