blob: 8044ed644650e75f914f4f38e0ac58e78b8972ca [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"
28#include "mbedtls/md5.h"
29#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
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020042#include "mbedtls/legacy_or_psa.h"
Przemek Stekiel829e97d2022-08-09 14:58:35 +020043
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020044#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
45 defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010046 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Przemek Stekiel4092ff92022-08-11 08:49:21 +020047#define PEM_RFC1421
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020048#endif /* MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
49 MBEDTLS_CIPHER_MODE_CBC &&
Przemek Stekiel4092ff92022-08-11 08:49:21 +020050 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
51
Andres AGc0db5112016-12-07 15:05:53 +000052#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010053void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000054{
Gilles Peskine449bd832023-01-11 14:50:10 +010055 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000056}
57
Przemek Stekiel0cd6f082022-08-18 12:38:30 +020058#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +000059/*
60 * Read a 16-byte hex string and convert it to binary
61 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062static int pem_get_iv(const unsigned char *s, unsigned char *iv,
63 size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000064{
Paul Bakker23986e52011-04-24 08:57:21 +000065 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 for (i = 0; i < iv_len * 2; i++, s++) {
70 if (*s >= '0' && *s <= '9') {
71 j = *s - '0';
72 } else
73 if (*s >= 'A' && *s <= 'F') {
74 j = *s - '7';
75 } else
76 if (*s >= 'a' && *s <= 'f') {
77 j = *s - 'W';
78 } else {
79 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
80 }
Paul Bakker96743fc2011-02-12 14:30:57 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000085 }
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000088}
89
Przemek Stekielbe92bee2022-08-04 10:38:34 +020090#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010091static int pem_pbkdf1(unsigned char *key, size_t keylen,
92 unsigned char *iv,
93 const unsigned char *pwd, size_t pwdlen)
Przemek Stekielbe92bee2022-08-04 10:38:34 +020094{
95 mbedtls_md5_context md5_ctx;
96 unsigned char md5sum[16];
97 size_t use_len;
98 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_md5_init(&md5_ctx);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200101
102 /*
103 * key[ 0..15] = MD5(pwd || IV)
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if ((ret = mbedtls_md5_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 }
108 if ((ret = mbedtls_md5_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 }
111 if ((ret = mbedtls_md5_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
114 if ((ret = mbedtls_md5_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200115 goto exit;
116 }
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (keylen <= 16) {
119 memcpy(key, md5sum, keylen);
120 goto exit;
121 }
122
123 memcpy(key, md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200124
125 /*
126 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if ((ret = mbedtls_md5_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 }
131 if ((ret = mbedtls_md5_update(&md5_ctx, md5sum, 16)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 }
134 if ((ret = mbedtls_md5_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 }
137 if ((ret = mbedtls_md5_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 }
140 if ((ret = mbedtls_md5_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200143
144 use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (keylen < 32) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200146 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 memcpy(key + 16, md5sum, use_len);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200150
151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_md5_free(&md5_ctx);
153 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return ret;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200156}
157#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100158static int pem_pbkdf1(unsigned char *key, size_t keylen,
159 unsigned char *iv,
160 const unsigned char *pwd, size_t pwdlen)
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200161{
162 unsigned char md5sum[16];
163 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
164 size_t output_length = 0;
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
166
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200169 goto exit;
170 }
Przemek Stekielbc3906c2022-08-19 09:16:36 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 }
175
176 if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
177 goto exit;
178 }
179
180 if ((status = psa_hash_finish(&operation, md5sum,
181 PSA_HASH_LENGTH(PSA_ALG_MD5),
182 &output_length)) != PSA_SUCCESS) {
183 goto exit;
184 }
185
186 if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
187 goto exit;
188 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200189
190 /*
191 * key[ 0..15] = MD5(pwd || IV)
192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (keylen <= 16) {
194 memcpy(key, md5sum, keylen);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200195 goto exit;
196 }
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 memcpy(key, md5sum, 16);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200199
200 /*
201 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
202 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200204 goto exit;
205 }
Przemek Stekielbc3906c2022-08-19 09:16:36 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((status = psa_hash_update(&operation, md5sum, 16)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200208 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 }
210
211 if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
212 goto exit;
213 }
214
215 if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
216 goto exit;
217 }
218
219 if ((status = psa_hash_finish(&operation, md5sum,
220 PSA_HASH_LENGTH(PSA_ALG_MD5),
221 &output_length)) != PSA_SUCCESS) {
222 goto exit;
223 }
224
225 if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
226 goto exit;
227 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200228
229 size_t use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (keylen < 32) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200231 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 memcpy(key + 16, md5sum, use_len);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200235
236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return mbedtls_md_error_from_psa(status);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200240}
Przemek Stekield23a4ef2022-08-18 11:56:54 +0200241#endif /* MBEDTLS_MD5_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000244/*
245 * Decrypt with DES-CBC, using PBKDF1 for key derivation
246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247static int pem_des_decrypt(unsigned char des_iv[8],
248 unsigned char *buf, size_t buflen,
249 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000252 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000262 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 }
264 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
265 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000266
Andres AG51a7ae12017-02-22 16:23:26 +0000267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_des_free(&des_ctx);
269 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000272}
273
274/*
275 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277static int pem_des3_decrypt(unsigned char des3_iv[8],
278 unsigned char *buf, size_t buflen,
279 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000282 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100288 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000292 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 }
294 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
295 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000296
Andres AG51a7ae12017-02-22 16:23:26 +0000297exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_des3_free(&des3_ctx);
299 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000306/*
307 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
308 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
310 unsigned char *buf, size_t buflen,
311 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000314 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100320 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000324 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
326 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
327 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000328
Andres AG51a7ae12017-02-22 16:23:26 +0000329exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_aes_free(&aes_ctx);
331 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000336
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200337#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
340 const unsigned char *data, const unsigned char *pwd,
341 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000342{
Paul Bakker23986e52011-04-24 08:57:21 +0000343 int ret, enc;
344 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000345 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200346 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200347#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000348 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000350#else
351 ((void) pwd);
352 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200353#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (ctx == NULL) {
356 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
357 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (s1 == NULL) {
362 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
363 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (s2 == NULL || s2 <= s1) {
368 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
369 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 s1 += strlen(header);
372 if (*s1 == ' ') {
373 s1++;
374 }
375 if (*s1 == '\r') {
376 s1++;
377 }
378 if (*s1 == '\n') {
379 s1++;
380 } else {
381 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
382 }
Paul Bakker00b28602013-06-24 13:02:41 +0200383
384 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 end += strlen(footer);
386 if (*end == ' ') {
387 end++;
388 }
389 if (*end == '\r') {
390 end++;
391 }
392 if (*end == '\n') {
393 end++;
394 }
Paul Bakker00b28602013-06-24 13:02:41 +0200395 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000396
397 enc = 0;
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200400#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000401 enc++;
402
403 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (*s1 == '\r') {
405 s1++;
406 }
407 if (*s1 == '\n') {
408 s1++;
409 } else {
410 return MBEDTLS_ERR_PEM_INVALID_DATA;
411 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000412
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000417
418 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
420 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
421 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000422
423 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000426
427 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
429 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
430 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000431
432 s1 += 16;
433 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
438 if (s2 - s1 < 22) {
439 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
440 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 } else {
447 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
448 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000449
450 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
452 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
453 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000454
455 s1 += 32;
456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (enc_alg == MBEDTLS_CIPHER_NONE) {
460 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
461 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (*s1 == '\r') {
464 s1++;
465 }
466 if (*s1 == '\n') {
467 s1++;
468 } else {
469 return MBEDTLS_ERR_PEM_INVALID_DATA;
470 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000471#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200473#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000474 }
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (s1 >= s2) {
477 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000478 }
Paul Bakkercff68422013-09-15 20:43:33 +0200479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
481
482 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
483 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
484 }
485
486 if ((buf = mbedtls_calloc(1, len)) == NULL) {
487 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
488 }
489
490 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
491 mbedtls_platform_zeroize(buf, len);
492 mbedtls_free(buf);
493 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
494 }
495
496 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200497#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (pwd == NULL) {
499 mbedtls_platform_zeroize(buf, len);
500 mbedtls_free(buf);
501 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000502 }
503
Andres AG51a7ae12017-02-22 16:23:26 +0000504 ret = 0;
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
508 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
509 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
510 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
511 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
516 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
517 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
518 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
519 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
520 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (ret != 0) {
525 mbedtls_free(buf);
526 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000527 }
528
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200529 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200530 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
531 * length bytes (allow 4 to be sure) in all known use cases.
532 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000533 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200534 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
536 mbedtls_platform_zeroize(buf, len);
537 mbedtls_free(buf);
538 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000539 }
540#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 mbedtls_platform_zeroize(buf, len);
542 mbedtls_free(buf);
543 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200544#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000545 }
546
547 ctx->buf = buf;
548 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000551}
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200554{
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (ctx->buf != NULL) {
556 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
557 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500558 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100566int mbedtls_pem_write_buffer(const char *header, const char *footer,
567 const unsigned char *der_data, size_t der_len,
568 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200569{
Janos Follath24eed8d2019-11-22 13:21:35 +0000570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000571 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100572 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
575 add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200578 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200580 }
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (use_len != 0 &&
583 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
584 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200585 }
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
588 der_len)) != 0) {
589 mbedtls_free(encode_buf);
590 return ret;
591 }
592
593 memcpy(p, header, strlen(header));
594 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200595 c = encode_buf;
596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 while (use_len) {
598 len = (use_len > 64) ? 64 : use_len;
599 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200600 use_len -= len;
601 p += len;
602 c += len;
603 *p++ = '\n';
604 }
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 memcpy(p, footer, strlen(footer));
607 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200608
609 *p++ = '\0';
610 *olen = p - buf;
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 /* Clean any remaining data previously written to the buffer */
613 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 mbedtls_free(encode_buf);
616 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200617}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_PEM_WRITE_C */
619#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */