blob: 84bbb3df10a4d9259ac8e83db788721b150c9ac7 [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"
Przemek Stekielbc3906c2022-08-19 09:16:36 +020032#include "hash_info.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020037
Przemek Stekiela68d08f2022-08-04 08:42:06 +020038#if defined(MBEDTLS_USE_PSA_CRYPTO)
39#include "psa/crypto.h"
40#endif
41
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050042#if !defined(MBEDTLS_MD5_C)
43#include "mbedtls/psa_util.h"
44#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
45 psa_to_md_errors, \
46 psa_generic_status_to_mbedtls)
47#endif
48
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Przemek Stekiel829e97d2022-08-09 14:58:35 +020050
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020051#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
52 defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010053 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Przemek Stekiel4092ff92022-08-11 08:49:21 +020054#define PEM_RFC1421
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020055#endif /* MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
56 MBEDTLS_CIPHER_MODE_CBC &&
Przemek Stekiel4092ff92022-08-11 08:49:21 +020057 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
58
Andres AGc0db5112016-12-07 15:05:53 +000059#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010060void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000061{
Gilles Peskine449bd832023-01-11 14:50:10 +010062 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000063}
64
Przemek Stekiel0cd6f082022-08-18 12:38:30 +020065#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +000066/*
67 * Read a 16-byte hex string and convert it to binary
68 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069static int pem_get_iv(const unsigned char *s, unsigned char *iv,
70 size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000071{
Paul Bakker23986e52011-04-24 08:57:21 +000072 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 for (i = 0; i < iv_len * 2; i++, s++) {
77 if (*s >= '0' && *s <= '9') {
78 j = *s - '0';
79 } else
80 if (*s >= 'A' && *s <= 'F') {
81 j = *s - '7';
82 } else
83 if (*s >= 'a' && *s <= 'f') {
84 j = *s - 'W';
85 } else {
86 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
87 }
Paul Bakker96743fc2011-02-12 14:30:57 +000088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000092 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000095}
96
Przemek Stekielbe92bee2022-08-04 10:38:34 +020097#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int pem_pbkdf1(unsigned char *key, size_t keylen,
99 unsigned char *iv,
100 const unsigned char *pwd, size_t pwdlen)
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200101{
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100102 mbedtls_md_context_t md5_ctx;
103 const mbedtls_md_info_t *md5_info;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200104 unsigned char md5sum[16];
105 size_t use_len;
106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
107
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100108 mbedtls_md_init(&md5_ctx);
109
110 /* Prepare the context. (setup() errors gracefully on NULL info.) */
111 md5_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
112 if ((ret = mbedtls_md_setup(&md5_ctx, md5_info, 0)) != 0) {
113 goto exit;
114 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200115
116 /*
117 * key[ 0..15] = MD5(pwd || IV)
118 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100119 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100122 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100125 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100128 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200129 goto exit;
130 }
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (keylen <= 16) {
133 memcpy(key, md5sum, keylen);
134 goto exit;
135 }
136
137 memcpy(key, md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200138
139 /*
140 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
141 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100142 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200143 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100145 if ((ret = mbedtls_md_update(&md5_ctx, md5sum, 16)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100148 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100151 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100154 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200157
158 use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (keylen < 32) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200160 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 memcpy(key + 16, md5sum, use_len);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200164
165exit:
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100166 mbedtls_md_free(&md5_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return ret;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200170}
171#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static int pem_pbkdf1(unsigned char *key, size_t keylen,
173 unsigned char *iv,
174 const unsigned char *pwd, size_t pwdlen)
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200175{
176 unsigned char md5sum[16];
177 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
178 size_t output_length = 0;
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
180
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200183 goto exit;
184 }
Przemek Stekielbc3906c2022-08-19 09:16:36 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 }
189
190 if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
191 goto exit;
192 }
193
194 if ((status = psa_hash_finish(&operation, md5sum,
195 PSA_HASH_LENGTH(PSA_ALG_MD5),
196 &output_length)) != PSA_SUCCESS) {
197 goto exit;
198 }
199
200 if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
201 goto exit;
202 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200203
204 /*
205 * key[ 0..15] = MD5(pwd || IV)
206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (keylen <= 16) {
208 memcpy(key, md5sum, keylen);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200209 goto exit;
210 }
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 memcpy(key, md5sum, 16);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200213
214 /*
215 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
216 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200218 goto exit;
219 }
Przemek Stekielbc3906c2022-08-19 09:16:36 +0200220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if ((status = psa_hash_update(&operation, md5sum, 16)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200222 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 }
224
225 if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
226 goto exit;
227 }
228
229 if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
230 goto exit;
231 }
232
233 if ((status = psa_hash_finish(&operation, md5sum,
234 PSA_HASH_LENGTH(PSA_ALG_MD5),
235 &output_length)) != PSA_SUCCESS) {
236 goto exit;
237 }
238
239 if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
240 goto exit;
241 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200242
243 size_t use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (keylen < 32) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200245 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 memcpy(key + 16, md5sum, use_len);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200249
250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200252
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500253 return PSA_TO_MBEDTLS_ERR(status);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200254}
Przemek Stekield23a4ef2022-08-18 11:56:54 +0200255#endif /* MBEDTLS_MD5_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000258/*
259 * Decrypt with DES-CBC, using PBKDF1 for key derivation
260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261static int pem_des_decrypt(unsigned char des_iv[8],
262 unsigned char *buf, size_t buflen,
263 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000266 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100272 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000276 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 }
278 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
279 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000280
Andres AG51a7ae12017-02-22 16:23:26 +0000281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 mbedtls_des_free(&des_ctx);
283 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000286}
287
288/*
289 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
290 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291static int pem_des3_decrypt(unsigned char des3_iv[8],
292 unsigned char *buf, size_t buflen,
293 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000296 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000306 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 }
308 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
309 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000310
Andres AG51a7ae12017-02-22 16:23:26 +0000311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_des3_free(&des3_ctx);
313 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000320/*
321 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
322 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
324 unsigned char *buf, size_t buflen,
325 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000328 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100334 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000338 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 }
340 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
341 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000342
Andres AG51a7ae12017-02-22 16:23:26 +0000343exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 mbedtls_aes_free(&aes_ctx);
345 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000350
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200351#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
354 const unsigned char *data, const unsigned char *pwd,
355 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000356{
Paul Bakker23986e52011-04-24 08:57:21 +0000357 int ret, enc;
358 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000359 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200360 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200361#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000362 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000364#else
365 ((void) pwd);
366 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200367#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (ctx == NULL) {
370 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
371 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (s1 == NULL) {
376 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
377 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (s2 == NULL || s2 <= s1) {
382 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
383 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 s1 += strlen(header);
386 if (*s1 == ' ') {
387 s1++;
388 }
389 if (*s1 == '\r') {
390 s1++;
391 }
392 if (*s1 == '\n') {
393 s1++;
394 } else {
395 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
396 }
Paul Bakker00b28602013-06-24 13:02:41 +0200397
398 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 end += strlen(footer);
400 if (*end == ' ') {
401 end++;
402 }
403 if (*end == '\r') {
404 end++;
405 }
406 if (*end == '\n') {
407 end++;
408 }
Paul Bakker00b28602013-06-24 13:02:41 +0200409 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000410
411 enc = 0;
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200414#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000415 enc++;
416
417 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (*s1 == '\r') {
419 s1++;
420 }
421 if (*s1 == '\n') {
422 s1++;
423 } else {
424 return MBEDTLS_ERR_PEM_INVALID_DATA;
425 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000426
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000431
432 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
434 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
435 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000436
437 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000440
441 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
443 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
444 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000445
446 s1 += 16;
447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
452 if (s2 - s1 < 22) {
453 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
454 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 } else {
461 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
462 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000463
464 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
466 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
467 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000468
469 s1 += 32;
470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (enc_alg == MBEDTLS_CIPHER_NONE) {
474 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
475 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (*s1 == '\r') {
478 s1++;
479 }
480 if (*s1 == '\n') {
481 s1++;
482 } else {
483 return MBEDTLS_ERR_PEM_INVALID_DATA;
484 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000485#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200487#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (s1 >= s2) {
491 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000492 }
Paul Bakkercff68422013-09-15 20:43:33 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
495
496 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
497 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
498 }
499
500 if ((buf = mbedtls_calloc(1, len)) == NULL) {
501 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
502 }
503
504 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
505 mbedtls_platform_zeroize(buf, len);
506 mbedtls_free(buf);
507 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
508 }
509
510 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200511#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (pwd == NULL) {
513 mbedtls_platform_zeroize(buf, len);
514 mbedtls_free(buf);
515 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000516 }
517
Andres AG51a7ae12017-02-22 16:23:26 +0000518 ret = 0;
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
522 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
523 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
524 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
530 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
531 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
532 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
533 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
534 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
535 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (ret != 0) {
539 mbedtls_free(buf);
540 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000541 }
542
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200543 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200544 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
545 * length bytes (allow 4 to be sure) in all known use cases.
546 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000547 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200548 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
550 mbedtls_platform_zeroize(buf, len);
551 mbedtls_free(buf);
552 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000553 }
554#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 mbedtls_platform_zeroize(buf, len);
556 mbedtls_free(buf);
557 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200558#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000559 }
560
561 ctx->buf = buf;
562 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000565}
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200568{
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (ctx->buf != NULL) {
570 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
571 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500572 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100580int mbedtls_pem_write_buffer(const char *header, const char *footer,
581 const unsigned char *der_data, size_t der_len,
582 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200583{
Janos Follath24eed8d2019-11-22 13:21:35 +0000584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000585 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100586 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
589 add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200592 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200594 }
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (use_len != 0 &&
597 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
598 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
602 der_len)) != 0) {
603 mbedtls_free(encode_buf);
604 return ret;
605 }
606
607 memcpy(p, header, strlen(header));
608 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200609 c = encode_buf;
610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 while (use_len) {
612 len = (use_len > 64) ? 64 : use_len;
613 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200614 use_len -= len;
615 p += len;
616 c += len;
617 *p++ = '\n';
618 }
619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 memcpy(p, footer, strlen(footer));
621 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200622
623 *p++ = '\0';
624 *olen = p - buf;
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 /* Clean any remaining data previously written to the buffer */
627 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 mbedtls_free(encode_buf);
630 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_PEM_WRITE_C */
633#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */