blob: a111970bb54a8a191fafe45fe32988aa884531c9 [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{
Valerio Setti0f286d52024-02-16 14:30:58 +0100247 /* input_len > 0 is guaranteed by mbedtls_pem_read_buffer(). */
Valerio Setti095e1ac2024-02-12 11:01:37 +0100248 size_t pad_len = input[input_len - 1];
249 size_t i;
250
251 if (pad_len > input_len) {
252 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
253 }
254
255 *data_len = input_len - pad_len;
256
257 for (i = *data_len; i < input_len; i++) {
258 if (input[i] != pad_len) {
259 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
260 }
261 }
262
263 return 0;
264}
265#endif /* MBEDTLS_DES_C || MBEDTLS_AES_C */
266
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200267#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
270 const unsigned char *data, const unsigned char *pwd,
271 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000272{
Paul Bakker23986e52011-04-24 08:57:21 +0000273 int ret, enc;
274 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000275 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200276 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200277#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000278 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000280#else
281 ((void) pwd);
282 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200283#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (ctx == NULL) {
286 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
287 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (s1 == NULL) {
292 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
293 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (s2 == NULL || s2 <= s1) {
298 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
299 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 s1 += strlen(header);
302 if (*s1 == ' ') {
303 s1++;
304 }
305 if (*s1 == '\r') {
306 s1++;
307 }
308 if (*s1 == '\n') {
309 s1++;
310 } else {
311 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
312 }
Paul Bakker00b28602013-06-24 13:02:41 +0200313
314 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 end += strlen(footer);
316 if (*end == ' ') {
317 end++;
318 }
319 if (*end == '\r') {
320 end++;
321 }
322 if (*end == '\n') {
323 end++;
324 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000325 *use_len = (size_t) (end - data);
Paul Bakker96743fc2011-02-12 14:30:57 +0000326
327 enc = 0;
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200330#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000331 enc++;
332
333 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (*s1 == '\r') {
335 s1++;
336 }
337 if (*s1 == '\n') {
338 s1++;
339 } else {
340 return MBEDTLS_ERR_PEM_INVALID_DATA;
341 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000342
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000347
348 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
350 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
351 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000352
353 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000356
357 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
359 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
360 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000361
362 s1 += 16;
363 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
368 if (s2 - s1 < 22) {
369 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
370 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 } else {
377 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
378 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000379
380 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
382 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
383 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000384
385 s1 += 32;
386 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (enc_alg == MBEDTLS_CIPHER_NONE) {
390 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
391 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (*s1 == '\r') {
394 s1++;
395 }
396 if (*s1 == '\n') {
397 s1++;
398 } else {
399 return MBEDTLS_ERR_PEM_INVALID_DATA;
400 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000401#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200403#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000404 }
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (s1 >= s2) {
407 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000408 }
Paul Bakkercff68422013-09-15 20:43:33 +0200409
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000410 ret = mbedtls_base64_decode(NULL, 0, &len, s1, (size_t) (s2 - s1));
Gilles Peskine449bd832023-01-11 14:50:10 +0100411
412 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
414 }
415
Valerio Setti0f286d52024-02-16 14:30:58 +0100416 if (len == 0) {
417 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
418 }
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if ((buf = mbedtls_calloc(1, len)) == NULL) {
421 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
422 }
423
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000424 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, (size_t) (s2 - s1))) != 0) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100425 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
427 }
428
429 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200430#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (pwd == NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100432 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000434 }
435
Andres AG51a7ae12017-02-22 16:23:26 +0000436 ret = 0;
437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
440 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
441 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
442 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
448 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
449 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
450 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
451 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
452 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (ret != 0) {
457 mbedtls_free(buf);
458 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000459 }
460
Valerio Setti095e1ac2024-02-12 11:01:37 +0100461 /* Check PKCS padding and update data length based on padding info.
462 * This can be used to detect invalid padding data and password
463 * mismatches. */
464 ret = pem_check_pkcs_padding(buf, len, &len);
465 if (ret != 0) {
466 mbedtls_free(buf);
467 return ret;
468 }
469
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200470 /*
Valerio Setti095e1ac2024-02-12 11:01:37 +0100471 * In RFC1421 PEM is used as container for DER (ASN.1) content so we
472 * can use ASN.1 functions to parse the main SEQUENCE tag and to get its
473 * length.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200474 */
Valerio Setti2653e922024-02-08 17:51:00 +0100475 unsigned char *p = buf;
Valerio Setti095e1ac2024-02-12 11:01:37 +0100476 size_t sequence_len;
477 ret = mbedtls_asn1_get_tag(&p, buf + len, &sequence_len,
Valerio Setti2653e922024-02-08 17:51:00 +0100478 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED);
479 if (ret != 0) {
480 mbedtls_free(buf);
481 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
Paul Bakker96743fc2011-02-12 14:30:57 +0000482 }
Valerio Setti2653e922024-02-08 17:51:00 +0100483 /* Add also the sequence block (tag + len) to the total amount of valid data. */
Valerio Setti095e1ac2024-02-12 11:01:37 +0100484 sequence_len += (p - buf);
485
486 /* Ensure that the reported SEQUENCE length matches the data len (i.e. no
487 * trailing garbage data). */
488 if (len != sequence_len) {
489 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
490 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000491#else
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100492 mbedtls_zeroize_and_free(buf, len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200494#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000495 }
496
497 ctx->buf = buf;
498 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000501}
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200504{
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (ctx->buf != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100506 mbedtls_zeroize_and_free(ctx->buf, ctx->buflen);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500507 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100515int mbedtls_pem_write_buffer(const char *header, const char *footer,
516 const unsigned char *der_data, size_t der_len,
517 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200518{
Janos Follath24eed8d2019-11-22 13:21:35 +0000519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000520 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100521 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
Jethro Beekman746df882023-05-03 14:49:28 +0200524 add_len = strlen(header) + strlen(footer) + (((use_len > 2) ? (use_len - 2) : 0) / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200527 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200529 }
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (use_len != 0 &&
532 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
533 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
537 der_len)) != 0) {
538 mbedtls_free(encode_buf);
539 return ret;
540 }
541
542 memcpy(p, header, strlen(header));
543 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200544 c = encode_buf;
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 while (use_len) {
547 len = (use_len > 64) ? 64 : use_len;
548 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200549 use_len -= len;
550 p += len;
551 c += len;
552 *p++ = '\n';
553 }
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 memcpy(p, footer, strlen(footer));
556 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200557
558 *p++ = '\0';
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000559 *olen = (size_t) (p - buf);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 /* Clean any remaining data previously written to the buffer */
562 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 mbedtls_free(encode_buf);
565 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_PEM_WRITE_C */
568#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */