blob: f090f493124c778bafe890fbaf75643c6123740b [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
Valerio Setti095e1ac2024-02-12 11:01:37 +0100244#if defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C)
245static int pem_check_pkcs_padding(unsigned char *input, size_t input_len, size_t *data_len)
246{
247 size_t pad_len = input[input_len - 1];
248 size_t i;
249
250 if (pad_len > input_len) {
251 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
252 }
253
254 *data_len = input_len - pad_len;
255
256 for (i = *data_len; i < input_len; i++) {
257 if (input[i] != pad_len) {
258 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
259 }
260 }
261
262 return 0;
263}
264#endif /* MBEDTLS_DES_C || MBEDTLS_AES_C */
265
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200266#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
269 const unsigned char *data, const unsigned char *pwd,
270 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000271{
Paul Bakker23986e52011-04-24 08:57:21 +0000272 int ret, enc;
273 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000274 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200275 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200276#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000277 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000279#else
280 ((void) pwd);
281 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200282#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (ctx == NULL) {
285 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
286 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (s1 == NULL) {
291 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
292 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (s2 == NULL || s2 <= s1) {
297 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
298 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 s1 += strlen(header);
301 if (*s1 == ' ') {
302 s1++;
303 }
304 if (*s1 == '\r') {
305 s1++;
306 }
307 if (*s1 == '\n') {
308 s1++;
309 } else {
310 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
311 }
Paul Bakker00b28602013-06-24 13:02:41 +0200312
313 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 end += strlen(footer);
315 if (*end == ' ') {
316 end++;
317 }
318 if (*end == '\r') {
319 end++;
320 }
321 if (*end == '\n') {
322 end++;
323 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000324 *use_len = (size_t) (end - data);
Paul Bakker96743fc2011-02-12 14:30:57 +0000325
326 enc = 0;
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200329#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000330 enc++;
331
332 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (*s1 == '\r') {
334 s1++;
335 }
336 if (*s1 == '\n') {
337 s1++;
338 } else {
339 return MBEDTLS_ERR_PEM_INVALID_DATA;
340 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000341
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000346
347 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
349 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
350 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000351
352 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000355
356 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
358 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
359 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000360
361 s1 += 16;
362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
367 if (s2 - s1 < 22) {
368 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
369 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 } else {
376 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
377 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000378
379 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
381 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
382 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000383
384 s1 += 32;
385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (enc_alg == MBEDTLS_CIPHER_NONE) {
389 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
390 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (*s1 == '\r') {
393 s1++;
394 }
395 if (*s1 == '\n') {
396 s1++;
397 } else {
398 return MBEDTLS_ERR_PEM_INVALID_DATA;
399 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000400#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200402#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000403 }
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (s1 >= s2) {
406 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000407 }
Paul Bakkercff68422013-09-15 20:43:33 +0200408
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000409 ret = mbedtls_base64_decode(NULL, 0, &len, s1, (size_t) (s2 - s1));
Gilles Peskine449bd832023-01-11 14:50:10 +0100410
411 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
412 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
413 }
414
415 if ((buf = mbedtls_calloc(1, len)) == NULL) {
416 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
417 }
418
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000419 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, (size_t) (s2 - s1))) != 0) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100420 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
422 }
423
424 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200425#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (pwd == NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100427 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000429 }
430
Andres AG51a7ae12017-02-22 16:23:26 +0000431 ret = 0;
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
435 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
436 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
437 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
443 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
444 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
445 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
446 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
447 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
448 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (ret != 0) {
452 mbedtls_free(buf);
453 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000454 }
455
Valerio Setti095e1ac2024-02-12 11:01:37 +0100456 /* Check PKCS padding and update data length based on padding info.
457 * This can be used to detect invalid padding data and password
458 * mismatches. */
459 ret = pem_check_pkcs_padding(buf, len, &len);
460 if (ret != 0) {
461 mbedtls_free(buf);
462 return ret;
463 }
464
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200465 /*
Valerio Setti095e1ac2024-02-12 11:01:37 +0100466 * In RFC1421 PEM is used as container for DER (ASN.1) content so we
467 * can use ASN.1 functions to parse the main SEQUENCE tag and to get its
468 * length.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200469 */
Valerio Setti2653e922024-02-08 17:51:00 +0100470 unsigned char *p = buf;
Valerio Setti095e1ac2024-02-12 11:01:37 +0100471 size_t sequence_len;
472 ret = mbedtls_asn1_get_tag(&p, buf + len, &sequence_len,
Valerio Setti2653e922024-02-08 17:51:00 +0100473 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED);
474 if (ret != 0) {
475 mbedtls_free(buf);
476 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
Paul Bakker96743fc2011-02-12 14:30:57 +0000477 }
Valerio Setti2653e922024-02-08 17:51:00 +0100478 /* Add also the sequence block (tag + len) to the total amount of valid data. */
Valerio Setti095e1ac2024-02-12 11:01:37 +0100479 sequence_len += (p - buf);
480
481 /* Ensure that the reported SEQUENCE length matches the data len (i.e. no
482 * trailing garbage data). */
483 if (len != sequence_len) {
484 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
485 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000486#else
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100487 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200489#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000490 }
491
492 ctx->buf = buf;
493 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000496}
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200499{
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (ctx->buf != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100501 mbedtls_zeroize_and_free(ctx->buf, ctx->buflen);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500502 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100510int mbedtls_pem_write_buffer(const char *header, const char *footer,
511 const unsigned char *der_data, size_t der_len,
512 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200513{
Janos Follath24eed8d2019-11-22 13:21:35 +0000514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000515 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100516 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
Jethro Beekman746df882023-05-03 14:49:28 +0200519 add_len = strlen(header) + strlen(footer) + (((use_len > 2) ? (use_len - 2) : 0) / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200522 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200524 }
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (use_len != 0 &&
527 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
528 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200529 }
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
532 der_len)) != 0) {
533 mbedtls_free(encode_buf);
534 return ret;
535 }
536
537 memcpy(p, header, strlen(header));
538 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200539 c = encode_buf;
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 while (use_len) {
542 len = (use_len > 64) ? 64 : use_len;
543 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200544 use_len -= len;
545 p += len;
546 c += len;
547 *p++ = '\n';
548 }
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 memcpy(p, footer, strlen(footer));
551 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200552
553 *p++ = '\0';
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000554 *olen = (size_t) (p - buf);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 /* Clean any remaining data previously written to the buffer */
557 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_free(encode_buf);
560 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#endif /* MBEDTLS_PEM_WRITE_C */
563#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */