blob: 056c98c7716e10a0e5a2a2a599eaf4972d020a70 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker96743fc2011-02-12 14:30:57 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
Rich Evansce2f2372015-02-06 13:57:42 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pem.h"
25#include "mbedtls/base64.h"
26#include "mbedtls/des.h"
27#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010028#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020036
Przemek Stekiela68d08f2022-08-04 08:42:06 +020037#if defined(MBEDTLS_USE_PSA_CRYPTO)
38#include "psa/crypto.h"
39#endif
40
Manuel Pégourié-Gonnard52d02a82023-03-16 10:24:47 +010041#if defined(MBEDTLS_MD_CAN_MD5) && \
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020042 defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010043 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Przemek Stekiel4092ff92022-08-11 08:49:21 +020044#define PEM_RFC1421
Manuel Pégourié-Gonnard52d02a82023-03-16 10:24:47 +010045#endif /* MBEDTLS_MD_CAN_MD5 &&
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020046 MBEDTLS_CIPHER_MODE_CBC &&
Przemek Stekiel4092ff92022-08-11 08:49:21 +020047 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
48
Andres AGc0db5112016-12-07 15:05:53 +000049#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010050void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000051{
Gilles Peskine449bd832023-01-11 14:50:10 +010052 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000053}
54
Przemek Stekiel0cd6f082022-08-18 12:38:30 +020055#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +000056/*
57 * Read a 16-byte hex string and convert it to binary
58 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059static int pem_get_iv(const unsigned char *s, unsigned char *iv,
60 size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000061{
Paul Bakker23986e52011-04-24 08:57:21 +000062 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 for (i = 0; i < iv_len * 2; i++, s++) {
67 if (*s >= '0' && *s <= '9') {
68 j = *s - '0';
69 } else
70 if (*s >= 'A' && *s <= 'F') {
71 j = *s - '7';
72 } else
73 if (*s >= 'a' && *s <= 'f') {
74 j = *s - 'W';
75 } else {
76 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
77 }
Paul Bakker96743fc2011-02-12 14:30:57 +000078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000082 }
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000085}
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087static int pem_pbkdf1(unsigned char *key, size_t keylen,
88 unsigned char *iv,
89 const unsigned char *pwd, size_t pwdlen)
Przemek Stekielbe92bee2022-08-04 10:38:34 +020090{
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010091 mbedtls_md_context_t md5_ctx;
92 const mbedtls_md_info_t *md5_info;
Przemek Stekielbe92bee2022-08-04 10:38:34 +020093 unsigned char md5sum[16];
94 size_t use_len;
95 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
96
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010097 mbedtls_md_init(&md5_ctx);
98
99 /* Prepare the context. (setup() errors gracefully on NULL info.) */
100 md5_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
101 if ((ret = mbedtls_md_setup(&md5_ctx, md5_info, 0)) != 0) {
102 goto exit;
103 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200104
105 /*
106 * key[ 0..15] = MD5(pwd || IV)
107 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100108 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100111 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100114 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100117 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200118 goto exit;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (keylen <= 16) {
122 memcpy(key, md5sum, keylen);
123 goto exit;
124 }
125
126 memcpy(key, md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200127
128 /*
129 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
130 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100131 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100134 if ((ret = mbedtls_md_update(&md5_ctx, md5sum, 16)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100137 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100140 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100143 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200146
147 use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (keylen < 32) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200149 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 memcpy(key + 16, md5sum, use_len);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200153
154exit:
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100155 mbedtls_md_free(&md5_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return ret;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200159}
Paul Bakker96743fc2011-02-12 14:30:57 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000162/*
163 * Decrypt with DES-CBC, using PBKDF1 for key derivation
164 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165static int pem_des_decrypt(unsigned char des_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_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000170 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100176 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
182 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
183 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000184
Andres AG51a7ae12017-02-22 16:23:26 +0000185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_des_free(&des_ctx);
187 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000190}
191
192/*
193 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195static int pem_des3_decrypt(unsigned char des3_iv[8],
196 unsigned char *buf, size_t buflen,
197 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000200 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 }
212 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
213 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000214
Andres AG51a7ae12017-02-22 16:23:26 +0000215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_des3_free(&des3_ctx);
217 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000224/*
225 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
228 unsigned char *buf, size_t buflen,
229 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000232 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100238 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 }
244 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
245 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000246
Andres AG51a7ae12017-02-22 16:23:26 +0000247exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 mbedtls_aes_free(&aes_ctx);
249 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000254
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200255#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
258 const unsigned char *data, const unsigned char *pwd,
259 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000260{
Paul Bakker23986e52011-04-24 08:57:21 +0000261 int ret, enc;
262 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000263 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200264 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200265#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000266 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000268#else
269 ((void) pwd);
270 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200271#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (ctx == NULL) {
274 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
275 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (s1 == NULL) {
280 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
281 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (s2 == NULL || s2 <= s1) {
286 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
287 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 s1 += strlen(header);
290 if (*s1 == ' ') {
291 s1++;
292 }
293 if (*s1 == '\r') {
294 s1++;
295 }
296 if (*s1 == '\n') {
297 s1++;
298 } else {
299 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
300 }
Paul Bakker00b28602013-06-24 13:02:41 +0200301
302 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 end += strlen(footer);
304 if (*end == ' ') {
305 end++;
306 }
307 if (*end == '\r') {
308 end++;
309 }
310 if (*end == '\n') {
311 end++;
312 }
Paul Bakker00b28602013-06-24 13:02:41 +0200313 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000314
315 enc = 0;
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200318#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000319 enc++;
320
321 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (*s1 == '\r') {
323 s1++;
324 }
325 if (*s1 == '\n') {
326 s1++;
327 } else {
328 return MBEDTLS_ERR_PEM_INVALID_DATA;
329 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000330
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000335
336 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
338 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
339 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000340
341 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000344
345 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
347 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
348 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000349
350 s1 += 16;
351 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
356 if (s2 - s1 < 22) {
357 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
358 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 } else {
365 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
366 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000367
368 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
370 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
371 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000372
373 s1 += 32;
374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (enc_alg == MBEDTLS_CIPHER_NONE) {
378 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
379 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (*s1 == '\r') {
382 s1++;
383 }
384 if (*s1 == '\n') {
385 s1++;
386 } else {
387 return MBEDTLS_ERR_PEM_INVALID_DATA;
388 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000389#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200391#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000392 }
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (s1 >= s2) {
395 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000396 }
Paul Bakkercff68422013-09-15 20:43:33 +0200397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
399
400 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
401 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
402 }
403
404 if ((buf = mbedtls_calloc(1, len)) == NULL) {
405 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
406 }
407
408 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
409 mbedtls_platform_zeroize(buf, len);
410 mbedtls_free(buf);
411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
412 }
413
414 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200415#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (pwd == NULL) {
417 mbedtls_platform_zeroize(buf, len);
418 mbedtls_free(buf);
419 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000420 }
421
Andres AG51a7ae12017-02-22 16:23:26 +0000422 ret = 0;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
426 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
427 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
428 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
434 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
435 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
436 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
437 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
438 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (ret != 0) {
443 mbedtls_free(buf);
444 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000445 }
446
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200447 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200448 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
449 * length bytes (allow 4 to be sure) in all known use cases.
450 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000451 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200452 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
454 mbedtls_platform_zeroize(buf, len);
455 mbedtls_free(buf);
456 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000457 }
458#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 mbedtls_platform_zeroize(buf, len);
460 mbedtls_free(buf);
461 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200462#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000463 }
464
465 ctx->buf = buf;
466 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000469}
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200472{
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (ctx->buf != NULL) {
474 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
475 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100484int mbedtls_pem_write_buffer(const char *header, const char *footer,
485 const unsigned char *der_data, size_t der_len,
486 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200487{
Janos Follath24eed8d2019-11-22 13:21:35 +0000488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000489 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100490 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
Jethro Beekman746df882023-05-03 14:49:28 +0200493 add_len = strlen(header) + strlen(footer) + (((use_len > 2) ? (use_len - 2) : 0) / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200496 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200498 }
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (use_len != 0 &&
501 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
502 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200503 }
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
506 der_len)) != 0) {
507 mbedtls_free(encode_buf);
508 return ret;
509 }
510
511 memcpy(p, header, strlen(header));
512 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200513 c = encode_buf;
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 while (use_len) {
516 len = (use_len > 64) ? 64 : use_len;
517 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200518 use_len -= len;
519 p += len;
520 c += len;
521 *p++ = '\n';
522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 memcpy(p, footer, strlen(footer));
525 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200526
527 *p++ = '\0';
528 *olen = p - buf;
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 /* Clean any remaining data previously written to the buffer */
531 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 mbedtls_free(encode_buf);
534 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_PEM_WRITE_C */
537#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */