blob: 9f14052e59cc83ad7563ce7cb3d15b2911740483 [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
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{
102 mbedtls_md5_context md5_ctx;
103 unsigned char md5sum[16];
104 size_t use_len;
105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_md5_init(&md5_ctx);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200108
109 /*
110 * key[ 0..15] = MD5(pwd || IV)
111 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if ((ret = mbedtls_md5_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 }
115 if ((ret = mbedtls_md5_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 }
118 if ((ret = mbedtls_md5_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 }
121 if ((ret = mbedtls_md5_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200122 goto exit;
123 }
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (keylen <= 16) {
126 memcpy(key, md5sum, keylen);
127 goto exit;
128 }
129
130 memcpy(key, md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200131
132 /*
133 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if ((ret = mbedtls_md5_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
138 if ((ret = mbedtls_md5_update(&md5_ctx, md5sum, 16)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 }
141 if ((ret = mbedtls_md5_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 }
144 if ((ret = mbedtls_md5_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 }
147 if ((ret = mbedtls_md5_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200150
151 use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (keylen < 32) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200153 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 memcpy(key + 16, md5sum, use_len);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200157
158exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_md5_free(&md5_ctx);
160 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return ret;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200163}
164#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100165static int pem_pbkdf1(unsigned char *key, size_t keylen,
166 unsigned char *iv,
167 const unsigned char *pwd, size_t pwdlen)
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200168{
169 unsigned char md5sum[16];
170 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
171 size_t output_length = 0;
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
173
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200176 goto exit;
177 }
Przemek Stekielbc3906c2022-08-19 09:16:36 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
182
183 if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
184 goto exit;
185 }
186
187 if ((status = psa_hash_finish(&operation, md5sum,
188 PSA_HASH_LENGTH(PSA_ALG_MD5),
189 &output_length)) != PSA_SUCCESS) {
190 goto exit;
191 }
192
193 if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
194 goto exit;
195 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200196
197 /*
198 * key[ 0..15] = MD5(pwd || IV)
199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (keylen <= 16) {
201 memcpy(key, md5sum, keylen);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200202 goto exit;
203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 memcpy(key, md5sum, 16);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200206
207 /*
208 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if ((status = psa_hash_setup(&operation, PSA_ALG_MD5)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200211 goto exit;
212 }
Przemek Stekielbc3906c2022-08-19 09:16:36 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if ((status = psa_hash_update(&operation, md5sum, 16)) != PSA_SUCCESS) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 }
217
218 if ((status = psa_hash_update(&operation, pwd, pwdlen)) != PSA_SUCCESS) {
219 goto exit;
220 }
221
222 if ((status = psa_hash_update(&operation, iv, 8)) != PSA_SUCCESS) {
223 goto exit;
224 }
225
226 if ((status = psa_hash_finish(&operation, md5sum,
227 PSA_HASH_LENGTH(PSA_ALG_MD5),
228 &output_length)) != PSA_SUCCESS) {
229 goto exit;
230 }
231
232 if ((status = psa_hash_abort(&operation)) != PSA_SUCCESS) {
233 goto exit;
234 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200235
236 size_t use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (keylen < 32) {
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200238 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 }
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 memcpy(key + 16, md5sum, use_len);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200242
243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200245
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500246 return PSA_TO_MBEDTLS_ERR(status);
Przemek Stekiela68d08f2022-08-04 08:42:06 +0200247}
Przemek Stekield23a4ef2022-08-18 11:56:54 +0200248#endif /* MBEDTLS_MD5_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000251/*
252 * Decrypt with DES-CBC, using PBKDF1 for key derivation
253 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254static int pem_des_decrypt(unsigned char des_iv[8],
255 unsigned char *buf, size_t buflen,
256 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000259 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100265 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 }
271 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
272 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000273
Andres AG51a7ae12017-02-22 16:23:26 +0000274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_des_free(&des_ctx);
276 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000279}
280
281/*
282 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284static int pem_des3_decrypt(unsigned char des3_iv[8],
285 unsigned char *buf, size_t buflen,
286 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000289 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 }
301 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
302 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000303
Andres AG51a7ae12017-02-22 16:23:26 +0000304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 mbedtls_des3_free(&des3_ctx);
306 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000313/*
314 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
315 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
317 unsigned char *buf, size_t buflen,
318 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000321 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100327 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000331 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 }
333 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
334 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000335
Andres AG51a7ae12017-02-22 16:23:26 +0000336exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 mbedtls_aes_free(&aes_ctx);
338 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000343
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200344#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
347 const unsigned char *data, const unsigned char *pwd,
348 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000349{
Paul Bakker23986e52011-04-24 08:57:21 +0000350 int ret, enc;
351 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000352 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200353 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200354#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000355 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000357#else
358 ((void) pwd);
359 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200360#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (ctx == NULL) {
363 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
364 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (s1 == NULL) {
369 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
370 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (s2 == NULL || s2 <= s1) {
375 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
376 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 s1 += strlen(header);
379 if (*s1 == ' ') {
380 s1++;
381 }
382 if (*s1 == '\r') {
383 s1++;
384 }
385 if (*s1 == '\n') {
386 s1++;
387 } else {
388 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
389 }
Paul Bakker00b28602013-06-24 13:02:41 +0200390
391 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 end += strlen(footer);
393 if (*end == ' ') {
394 end++;
395 }
396 if (*end == '\r') {
397 end++;
398 }
399 if (*end == '\n') {
400 end++;
401 }
Paul Bakker00b28602013-06-24 13:02:41 +0200402 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000403
404 enc = 0;
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200407#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000408 enc++;
409
410 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (*s1 == '\r') {
412 s1++;
413 }
414 if (*s1 == '\n') {
415 s1++;
416 } else {
417 return MBEDTLS_ERR_PEM_INVALID_DATA;
418 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000419
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000424
425 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
427 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
428 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000429
430 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000433
434 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
436 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
437 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000438
439 s1 += 16;
440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
445 if (s2 - s1 < 22) {
446 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
447 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 } else {
454 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
455 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000456
457 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
459 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
460 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000461
462 s1 += 32;
463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (enc_alg == MBEDTLS_CIPHER_NONE) {
467 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
468 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (*s1 == '\r') {
471 s1++;
472 }
473 if (*s1 == '\n') {
474 s1++;
475 } else {
476 return MBEDTLS_ERR_PEM_INVALID_DATA;
477 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000478#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200480#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (s1 >= s2) {
484 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000485 }
Paul Bakkercff68422013-09-15 20:43:33 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
488
489 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
490 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
491 }
492
493 if ((buf = mbedtls_calloc(1, len)) == NULL) {
494 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
495 }
496
497 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
498 mbedtls_platform_zeroize(buf, len);
499 mbedtls_free(buf);
500 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
501 }
502
503 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200504#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (pwd == NULL) {
506 mbedtls_platform_zeroize(buf, len);
507 mbedtls_free(buf);
508 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000509 }
510
Andres AG51a7ae12017-02-22 16:23:26 +0000511 ret = 0;
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
515 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
516 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
517 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
523 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
524 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
525 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
526 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
527 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (ret != 0) {
532 mbedtls_free(buf);
533 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000534 }
535
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200536 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200537 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
538 * length bytes (allow 4 to be sure) in all known use cases.
539 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000540 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200541 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
543 mbedtls_platform_zeroize(buf, len);
544 mbedtls_free(buf);
545 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000546 }
547#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 mbedtls_platform_zeroize(buf, len);
549 mbedtls_free(buf);
550 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200551#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000552 }
553
554 ctx->buf = buf;
555 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000558}
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200561{
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (ctx->buf != NULL) {
563 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
564 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500565 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100573int mbedtls_pem_write_buffer(const char *header, const char *footer,
574 const unsigned char *der_data, size_t der_len,
575 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200576{
Janos Follath24eed8d2019-11-22 13:21:35 +0000577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000578 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100579 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
582 add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200585 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200587 }
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (use_len != 0 &&
590 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
591 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200592 }
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
595 der_len)) != 0) {
596 mbedtls_free(encode_buf);
597 return ret;
598 }
599
600 memcpy(p, header, strlen(header));
601 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200602 c = encode_buf;
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 while (use_len) {
605 len = (use_len > 64) ? 64 : use_len;
606 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200607 use_len -= len;
608 p += len;
609 c += len;
610 *p++ = '\n';
611 }
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 memcpy(p, footer, strlen(footer));
614 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200615
616 *p++ = '\0';
617 *olen = p - buf;
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 /* Clean any remaining data previously written to the buffer */
620 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 mbedtls_free(encode_buf);
623 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#endif /* MBEDTLS_PEM_WRITE_C */
626#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */