blob: a8801eba90cdbd642589476ad3806d18249666d8 [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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020024# 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"
30# include "mbedtls/platform_util.h"
31# include "mbedtls/error.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000034
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# if defined(MBEDTLS_PLATFORM_C)
36# include "mbedtls/platform.h"
37# else
38# include <stdlib.h>
39# define mbedtls_calloc calloc
40# define mbedtls_free free
41# endif
Paul Bakker6e339b52013-07-03 13:37:05 +020042
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043# if defined(MBEDTLS_PEM_PARSE_C)
44void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000045{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000047}
48
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049# if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
50 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +000051/*
52 * Read a 16-byte hex string and convert it to binary
53 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054static int pem_get_iv(const unsigned char *s, unsigned char *iv, size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000055{
Paul Bakker23986e52011-04-24 08:57:21 +000056 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000057
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000059
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060 for (i = 0; i < iv_len * 2; i++, s++) {
61 if (*s >= '0' && *s <= '9')
62 j = *s - '0';
63 else if (*s >= 'A' && *s <= 'F')
64 j = *s - '7';
65 else if (*s >= 'a' && *s <= 'f')
66 j = *s - 'W';
67 else
68 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
Paul Bakker96743fc2011-02-12 14:30:57 +000069
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072 iv[i >> 1] = (unsigned char)(iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000073 }
74
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020075 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000076}
77
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078static int pem_pbkdf1(unsigned char *key,
79 size_t keylen,
80 unsigned char *iv,
81 const unsigned char *pwd,
82 size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +000083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000085 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000086 size_t use_len;
Janos Follath24eed8d2019-11-22 13:21:35 +000087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +000088
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089 mbedtls_md5_init(&md5_ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020090
Paul Bakker96743fc2011-02-12 14:30:57 +000091 /*
92 * key[ 0..15] = MD5(pwd || IV)
93 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020094 if ((ret = mbedtls_md5_starts(&md5_ctx)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010095 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 if ((ret = mbedtls_md5_update(&md5_ctx, pwd, pwdlen)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010097 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020098 if ((ret = mbedtls_md5_update(&md5_ctx, iv, 8)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010099 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100 if ((ret = mbedtls_md5_finish(&md5_ctx, md5sum)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100101 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000102
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103 if (keylen <= 16) {
104 memcpy(key, md5sum, keylen);
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100105 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000106 }
107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108 memcpy(key, md5sum, 16);
Paul Bakker96743fc2011-02-12 14:30:57 +0000109
110 /*
111 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
112 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200113 if ((ret = mbedtls_md5_starts(&md5_ctx)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100114 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 if ((ret = mbedtls_md5_update(&md5_ctx, md5sum, 16)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100116 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 if ((ret = mbedtls_md5_update(&md5_ctx, pwd, pwdlen)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100118 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200119 if ((ret = mbedtls_md5_update(&md5_ctx, iv, 8)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100120 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121 if ((ret = mbedtls_md5_finish(&md5_ctx, md5sum)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100122 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000123
124 use_len = 16;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200125 if (keylen < 32)
Paul Bakker96743fc2011-02-12 14:30:57 +0000126 use_len = keylen - 16;
127
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128 memcpy(key + 16, md5sum, use_len);
Paul Bakker96743fc2011-02-12 14:30:57 +0000129
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100130exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131 mbedtls_md5_free(&md5_ctx);
132 mbedtls_platform_zeroize(md5sum, 16);
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100133
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000135}
136
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200137# if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000138/*
139 * Decrypt with DES-CBC, using PBKDF1 for key derivation
140 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141static int pem_des_decrypt(unsigned char des_iv[8],
142 unsigned char *buf,
143 size_t buflen,
144 const unsigned char *pwd,
145 size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000148 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000150
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200152
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200153 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100154 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0)
Andres AG51a7ae12017-02-22 16:23:26 +0000157 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200158 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen, des_iv,
159 buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000160
Andres AG51a7ae12017-02-22 16:23:26 +0000161exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200162 mbedtls_des_free(&des_ctx);
163 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000164
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200165 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000166}
167
168/*
169 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
170 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200171static int pem_des3_decrypt(unsigned char des3_iv[8],
172 unsigned char *buf,
173 size_t buflen,
174 const unsigned char *pwd,
175 size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000178 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000180
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200182
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200183 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100184 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0)
Andres AG51a7ae12017-02-22 16:23:26 +0000187 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
189 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000190
Andres AG51a7ae12017-02-22 16:23:26 +0000191exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200192 mbedtls_des3_free(&des3_ctx);
193 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000194
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200195 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000196}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200197# endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000198
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200199# if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000200/*
201 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
202 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203static int pem_aes_decrypt(unsigned char aes_iv[16],
204 unsigned int keylen,
205 unsigned char *buf,
206 size_t buflen,
207 const unsigned char *pwd,
208 size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000211 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000213
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200214 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200216 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0)
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100217 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000218
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0)
Andres AG51a7ae12017-02-22 16:23:26 +0000220 goto exit;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200221 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen, aes_iv,
222 buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000223
Andres AG51a7ae12017-02-22 16:23:26 +0000224exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200225 mbedtls_aes_free(&aes_ctx);
226 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000227
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200228 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000229}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200230# endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000231
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200232# endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && \
233 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000234
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx,
236 const char *header,
237 const char *footer,
238 const unsigned char *data,
239 const unsigned char *pwd,
240 size_t pwdlen,
241 size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000242{
Paul Bakker23986e52011-04-24 08:57:21 +0000243 int ret, enc;
244 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000245 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200246 const unsigned char *s1, *s2, *end;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247# if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
248 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +0000249 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200251# else
252 ((void)pwd);
253 ((void)pwdlen);
254# endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && \
255 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000256
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200257 if (ctx == NULL)
258 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000259
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 s1 = (unsigned char *)strstr((const char *)data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000261
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200262 if (s1 == NULL)
263 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Paul Bakker96743fc2011-02-12 14:30:57 +0000264
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265 s2 = (unsigned char *)strstr((const char *)data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000266
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267 if (s2 == NULL || s2 <= s1)
268 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Paul Bakker96743fc2011-02-12 14:30:57 +0000269
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200270 s1 += strlen(header);
271 if (*s1 == ' ')
272 s1++;
273 if (*s1 == '\r')
274 s1++;
275 if (*s1 == '\n')
276 s1++;
277 else
278 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Paul Bakker00b28602013-06-24 13:02:41 +0200279
280 end = s2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200281 end += strlen(footer);
282 if (*end == ' ')
283 end++;
284 if (*end == '\r')
285 end++;
286 if (*end == '\n')
287 end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200288 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000289
290 enc = 0;
291
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
293# if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
294 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +0000295 enc++;
296
297 s1 += 22;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298 if (*s1 == '\r')
299 s1++;
300 if (*s1 == '\n')
301 s1++;
302 else
303 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000304
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200305# if defined(MBEDTLS_DES_C)
306 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000308
309 s1 += 23;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200310 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0)
311 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
Paul Bakker96743fc2011-02-12 14:30:57 +0000312
313 s1 += 16;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200314 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000316
317 s1 += 18;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200318 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0)
319 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
Paul Bakker96743fc2011-02-12 14:30:57 +0000320
321 s1 += 16;
322 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200323# endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000324
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200325# if defined(MBEDTLS_AES_C)
326 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
327 if (s2 - s1 < 22)
328 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
329 else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331 else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200333 else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000335 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200336 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
Paul Bakker96743fc2011-02-12 14:30:57 +0000337
338 s1 += 22;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0)
340 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
Paul Bakker96743fc2011-02-12 14:30:57 +0000341
342 s1 += 32;
343 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200344# endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200345
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200346 if (enc_alg == MBEDTLS_CIPHER_NONE)
347 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
Paul Bakker96743fc2011-02-12 14:30:57 +0000348
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200349 if (*s1 == '\r')
350 s1++;
351 if (*s1 == '\n')
352 s1++;
353 else
354 return MBEDTLS_ERR_PEM_INVALID_DATA;
355# else
356 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
357# endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && \
358 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000359 }
360
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200361 if (s1 >= s2)
362 return MBEDTLS_ERR_PEM_INVALID_DATA;
Simon Butchera45aa132015-10-05 00:26:36 +0100363
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200364 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
Paul Bakker96743fc2011-02-12 14:30:57 +0000365
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER)
367 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
Paul Bakker96743fc2011-02-12 14:30:57 +0000368
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200369 if ((buf = mbedtls_calloc(1, len)) == NULL)
370 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000371
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200372 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
373 mbedtls_platform_zeroize(buf, len);
374 mbedtls_free(buf);
375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
Paul Bakker96743fc2011-02-12 14:30:57 +0000376 }
Paul Bakkercff68422013-09-15 20:43:33 +0200377
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200378 if (enc != 0) {
379# if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
380 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
381 if (pwd == NULL) {
382 mbedtls_platform_zeroize(buf, len);
383 mbedtls_free(buf);
384 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000385 }
386
Andres AG51a7ae12017-02-22 16:23:26 +0000387 ret = 0;
388
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200389# if defined(MBEDTLS_DES_C)
390 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC)
391 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
392 else if (enc_alg == MBEDTLS_CIPHER_DES_CBC)
393 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
394# endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000395
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200396# if defined(MBEDTLS_AES_C)
397 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC)
398 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
399 else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC)
400 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
401 else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC)
402 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
403# endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000404
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200405 if (ret != 0) {
406 mbedtls_free(buf);
407 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000408 }
409
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200410 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200411 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
412 * length bytes (allow 4 to be sure) in all known use cases.
413 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000414 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200415 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200416 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
417 mbedtls_platform_zeroize(buf, len);
418 mbedtls_free(buf);
419 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000420 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200421# else
422 mbedtls_platform_zeroize(buf, len);
423 mbedtls_free(buf);
424 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
425# endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC && \
426 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000427 }
428
429 ctx->buf = buf;
430 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000431
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200432 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000433}
434
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200435void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200436{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200437 if (ctx->buf != NULL) {
438 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
439 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500440 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200441 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200442
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200443 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200444}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200445# endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200446
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447# if defined(MBEDTLS_PEM_WRITE_C)
448int mbedtls_pem_write_buffer(const char *header,
449 const char *footer,
450 const unsigned char *der_data,
451 size_t der_len,
452 unsigned char *buf,
453 size_t buf_len,
454 size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200455{
Janos Follath24eed8d2019-11-22 13:21:35 +0000456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000457 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100458 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200459
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200460 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
461 add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200462
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200463 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200464 *olen = use_len + add_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200465 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200466 }
467
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200468 if (use_len != 0 && ((encode_buf = mbedtls_calloc(1, use_len)) == NULL))
469 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200470
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
472 der_len)) != 0) {
473 mbedtls_free(encode_buf);
474 return ret;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200475 }
476
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200477 memcpy(p, header, strlen(header));
478 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200479 c = encode_buf;
480
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200481 while (use_len) {
482 len = (use_len > 64) ? 64 : use_len;
483 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200484 use_len -= len;
485 p += len;
486 c += len;
487 *p++ = '\n';
488 }
489
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200490 memcpy(p, footer, strlen(footer));
491 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200492
493 *p++ = '\0';
494 *olen = p - buf;
495
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200496 /* Clean any remaining data previously written to the buffer */
497 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200499 mbedtls_free(encode_buf);
500 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200501}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502# endif /* MBEDTLS_PEM_WRITE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */