blob: aed4788bfe704fd22dc9bb917e8e6c9d45bca585 [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
Manuel Pégourié-Gonnard52d02a82023-03-16 10:24:47 +010042#if defined(MBEDTLS_MD_CAN_MD5) && \
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020043 defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010044 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Przemek Stekiel4092ff92022-08-11 08:49:21 +020045#define PEM_RFC1421
Manuel Pégourié-Gonnard52d02a82023-03-16 10:24:47 +010046#endif /* MBEDTLS_MD_CAN_MD5 &&
Manuel Pégourié-Gonnard1dc37252022-09-15 11:10:26 +020047 MBEDTLS_CIPHER_MODE_CBC &&
Przemek Stekiel4092ff92022-08-11 08:49:21 +020048 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
49
Andres AGc0db5112016-12-07 15:05:53 +000050#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010051void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000052{
Gilles Peskine449bd832023-01-11 14:50:10 +010053 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000054}
55
Przemek Stekiel0cd6f082022-08-18 12:38:30 +020056#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +000057/*
58 * Read a 16-byte hex string and convert it to binary
59 */
Gilles Peskine449bd832023-01-11 14:50:10 +010060static int pem_get_iv(const unsigned char *s, unsigned char *iv,
61 size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000062{
Paul Bakker23986e52011-04-24 08:57:21 +000063 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000064
Gilles Peskine449bd832023-01-11 14:50:10 +010065 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 for (i = 0; i < iv_len * 2; i++, s++) {
68 if (*s >= '0' && *s <= '9') {
69 j = *s - '0';
70 } else
71 if (*s >= 'A' && *s <= 'F') {
72 j = *s - '7';
73 } else
74 if (*s >= 'a' && *s <= 'f') {
75 j = *s - 'W';
76 } else {
77 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
78 }
Paul Bakker96743fc2011-02-12 14:30:57 +000079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000083 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000086}
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088static int pem_pbkdf1(unsigned char *key, size_t keylen,
89 unsigned char *iv,
90 const unsigned char *pwd, size_t pwdlen)
Przemek Stekielbe92bee2022-08-04 10:38:34 +020091{
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010092 mbedtls_md_context_t md5_ctx;
93 const mbedtls_md_info_t *md5_info;
Przemek Stekielbe92bee2022-08-04 10:38:34 +020094 unsigned char md5sum[16];
95 size_t use_len;
96 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
97
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010098 mbedtls_md_init(&md5_ctx);
99
100 /* Prepare the context. (setup() errors gracefully on NULL info.) */
101 md5_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
102 if ((ret = mbedtls_md_setup(&md5_ctx, md5_info, 0)) != 0) {
103 goto exit;
104 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200105
106 /*
107 * key[ 0..15] = MD5(pwd || IV)
108 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100109 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100112 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100115 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100118 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200119 goto exit;
120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (keylen <= 16) {
123 memcpy(key, md5sum, keylen);
124 goto exit;
125 }
126
127 memcpy(key, md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200128
129 /*
130 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
131 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100132 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100135 if ((ret = mbedtls_md_update(&md5_ctx, md5sum, 16)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100138 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100141 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100144 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200147
148 use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (keylen < 32) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200150 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 memcpy(key + 16, md5sum, use_len);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200154
155exit:
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100156 mbedtls_md_free(&md5_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return ret;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200160}
Paul Bakker96743fc2011-02-12 14:30:57 +0000161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000163/*
164 * Decrypt with DES-CBC, using PBKDF1 for key derivation
165 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166static int pem_des_decrypt(unsigned char des_iv[8],
167 unsigned char *buf, size_t buflen,
168 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000171 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100177 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
183 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
184 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000185
Andres AG51a7ae12017-02-22 16:23:26 +0000186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_des_free(&des_ctx);
188 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000191}
192
193/*
194 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196static int pem_des3_decrypt(unsigned char des3_iv[8],
197 unsigned char *buf, size_t buflen,
198 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000201 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 }
213 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
214 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000215
Andres AG51a7ae12017-02-22 16:23:26 +0000216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_des3_free(&des3_ctx);
218 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000225/*
226 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
227 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
229 unsigned char *buf, size_t buflen,
230 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000233 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 }
245 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
246 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000247
Andres AG51a7ae12017-02-22 16:23:26 +0000248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_aes_free(&aes_ctx);
250 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000255
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200256#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
259 const unsigned char *data, const unsigned char *pwd,
260 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000261{
Paul Bakker23986e52011-04-24 08:57:21 +0000262 int ret, enc;
263 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000264 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200265 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200266#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000267 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000269#else
270 ((void) pwd);
271 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200272#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (ctx == NULL) {
275 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
276 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (s1 == NULL) {
281 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
282 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (s2 == NULL || s2 <= s1) {
287 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
288 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 s1 += strlen(header);
291 if (*s1 == ' ') {
292 s1++;
293 }
294 if (*s1 == '\r') {
295 s1++;
296 }
297 if (*s1 == '\n') {
298 s1++;
299 } else {
300 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
301 }
Paul Bakker00b28602013-06-24 13:02:41 +0200302
303 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 end += strlen(footer);
305 if (*end == ' ') {
306 end++;
307 }
308 if (*end == '\r') {
309 end++;
310 }
311 if (*end == '\n') {
312 end++;
313 }
Paul Bakker00b28602013-06-24 13:02:41 +0200314 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000315
316 enc = 0;
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200319#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000320 enc++;
321
322 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (*s1 == '\r') {
324 s1++;
325 }
326 if (*s1 == '\n') {
327 s1++;
328 } else {
329 return MBEDTLS_ERR_PEM_INVALID_DATA;
330 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000331
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000336
337 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
339 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
340 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000341
342 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000345
346 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
348 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
349 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000350
351 s1 += 16;
352 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
357 if (s2 - s1 < 22) {
358 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
359 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 } else {
366 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
367 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000368
369 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
371 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
372 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000373
374 s1 += 32;
375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (enc_alg == MBEDTLS_CIPHER_NONE) {
379 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
380 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (*s1 == '\r') {
383 s1++;
384 }
385 if (*s1 == '\n') {
386 s1++;
387 } else {
388 return MBEDTLS_ERR_PEM_INVALID_DATA;
389 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000390#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200392#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (s1 >= s2) {
396 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000397 }
Paul Bakkercff68422013-09-15 20:43:33 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
400
401 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
402 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
403 }
404
405 if ((buf = mbedtls_calloc(1, len)) == NULL) {
406 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
407 }
408
409 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
410 mbedtls_platform_zeroize(buf, len);
411 mbedtls_free(buf);
412 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
413 }
414
415 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200416#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (pwd == NULL) {
418 mbedtls_platform_zeroize(buf, len);
419 mbedtls_free(buf);
420 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000421 }
422
Andres AG51a7ae12017-02-22 16:23:26 +0000423 ret = 0;
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
427 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
428 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
429 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
430 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
435 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
436 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
437 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
438 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
439 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (ret != 0) {
444 mbedtls_free(buf);
445 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000446 }
447
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200448 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200449 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
450 * length bytes (allow 4 to be sure) in all known use cases.
451 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000452 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
455 mbedtls_platform_zeroize(buf, len);
456 mbedtls_free(buf);
457 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000458 }
459#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 mbedtls_platform_zeroize(buf, len);
461 mbedtls_free(buf);
462 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200463#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000464 }
465
466 ctx->buf = buf;
467 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000470}
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200473{
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (ctx->buf != NULL) {
475 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
476 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500477 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100485int mbedtls_pem_write_buffer(const char *header, const char *footer,
486 const unsigned char *der_data, size_t der_len,
487 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200488{
Janos Follath24eed8d2019-11-22 13:21:35 +0000489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000490 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100491 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
Jethro Beekman746df882023-05-03 14:49:28 +0200494 add_len = strlen(header) + strlen(footer) + (((use_len > 2) ? (use_len - 2) : 0) / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200497 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200499 }
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (use_len != 0 &&
502 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
503 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200504 }
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
507 der_len)) != 0) {
508 mbedtls_free(encode_buf);
509 return ret;
510 }
511
512 memcpy(p, header, strlen(header));
513 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200514 c = encode_buf;
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 while (use_len) {
517 len = (use_len > 64) ? 64 : use_len;
518 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200519 use_len -= len;
520 p += len;
521 c += len;
522 *p++ = '\n';
523 }
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 memcpy(p, footer, strlen(footer));
526 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200527
528 *p++ = '\0';
529 *olen = p - buf;
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 /* Clean any remaining data previously written to the buffer */
532 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 mbedtls_free(encode_buf);
535 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#endif /* MBEDTLS_PEM_WRITE_C */
538#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */