blob: 264c2e47766c4ee12e7efba89661765b6e398ea1 [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é-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
Gilles Peskine449bd832023-01-11 14:50:10 +010090static int pem_pbkdf1(unsigned char *key, size_t keylen,
91 unsigned char *iv,
92 const unsigned char *pwd, size_t pwdlen)
Przemek Stekielbe92bee2022-08-04 10:38:34 +020093{
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +010094 mbedtls_md_context_t md5_ctx;
95 const mbedtls_md_info_t *md5_info;
Przemek Stekielbe92bee2022-08-04 10:38:34 +020096 unsigned char md5sum[16];
97 size_t use_len;
98 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
99
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100100 mbedtls_md_init(&md5_ctx);
101
102 /* Prepare the context. (setup() errors gracefully on NULL info.) */
103 md5_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
104 if ((ret = mbedtls_md_setup(&md5_ctx, md5_info, 0)) != 0) {
105 goto exit;
106 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200107
108 /*
109 * key[ 0..15] = MD5(pwd || IV)
110 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100111 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100114 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100117 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100120 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200121 goto exit;
122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (keylen <= 16) {
125 memcpy(key, md5sum, keylen);
126 goto exit;
127 }
128
129 memcpy(key, md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200130
131 /*
132 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
133 */
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100134 if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100137 if ((ret = mbedtls_md_update(&md5_ctx, md5sum, 16)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100140 if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100143 if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 }
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100146 if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200149
150 use_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (keylen < 32) {
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200152 use_len = keylen - 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 }
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 memcpy(key + 16, md5sum, use_len);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200156
157exit:
Manuel Pégourié-Gonnard83162092023-03-06 23:58:50 +0100158 mbedtls_md_free(&md5_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_platform_zeroize(md5sum, 16);
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return ret;
Przemek Stekielbe92bee2022-08-04 10:38:34 +0200162}
Paul Bakker96743fc2011-02-12 14:30:57 +0000163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000165/*
166 * Decrypt with DES-CBC, using PBKDF1 for key derivation
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168static int pem_des_decrypt(unsigned char des_iv[8],
169 unsigned char *buf, size_t buflen,
170 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000173 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 }
185 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
186 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000187
Andres AG51a7ae12017-02-22 16:23:26 +0000188exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_des_free(&des_ctx);
190 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000193}
194
195/*
196 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
197 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198static int pem_des3_decrypt(unsigned char des3_iv[8],
199 unsigned char *buf, size_t buflen,
200 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000203 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 }
215 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
216 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000217
Andres AG51a7ae12017-02-22 16:23:26 +0000218exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_des3_free(&des3_ctx);
220 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000227/*
228 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
229 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
231 unsigned char *buf, size_t buflen,
232 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000233{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000235 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000245 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 }
247 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
248 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000249
Andres AG51a7ae12017-02-22 16:23:26 +0000250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 mbedtls_aes_free(&aes_ctx);
252 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000257
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200258#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
261 const unsigned char *data, const unsigned char *pwd,
262 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000263{
Paul Bakker23986e52011-04-24 08:57:21 +0000264 int ret, enc;
265 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000266 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200267 const unsigned char *s1, *s2, *end;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200268#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000269 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000271#else
272 ((void) pwd);
273 ((void) pwdlen);
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200274#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (ctx == NULL) {
277 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
278 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (s1 == NULL) {
283 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
284 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (s2 == NULL || s2 <= s1) {
289 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
290 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 s1 += strlen(header);
293 if (*s1 == ' ') {
294 s1++;
295 }
296 if (*s1 == '\r') {
297 s1++;
298 }
299 if (*s1 == '\n') {
300 s1++;
301 } else {
302 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
303 }
Paul Bakker00b28602013-06-24 13:02:41 +0200304
305 end = s2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 end += strlen(footer);
307 if (*end == ' ') {
308 end++;
309 }
310 if (*end == '\r') {
311 end++;
312 }
313 if (*end == '\n') {
314 end++;
315 }
Paul Bakker00b28602013-06-24 13:02:41 +0200316 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000317
318 enc = 0;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200321#if defined(PEM_RFC1421)
Paul Bakker96743fc2011-02-12 14:30:57 +0000322 enc++;
323
324 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (*s1 == '\r') {
326 s1++;
327 }
328 if (*s1 == '\n') {
329 s1++;
330 } else {
331 return MBEDTLS_ERR_PEM_INVALID_DATA;
332 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000333
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000338
339 s1 += 23;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
341 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
342 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000343
344 s1 += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000347
348 s1 += 18;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
350 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
351 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000352
353 s1 += 16;
354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
359 if (s2 - s1 < 22) {
360 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
361 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 } else {
368 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
369 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000370
371 s1 += 22;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
373 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
374 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000375
376 s1 += 32;
377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (enc_alg == MBEDTLS_CIPHER_NONE) {
381 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
382 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (*s1 == '\r') {
385 s1++;
386 }
387 if (*s1 == '\n') {
388 s1++;
389 } else {
390 return MBEDTLS_ERR_PEM_INVALID_DATA;
391 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000392#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200394#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (s1 >= s2) {
398 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000399 }
Paul Bakkercff68422013-09-15 20:43:33 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
402
403 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
404 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
405 }
406
407 if ((buf = mbedtls_calloc(1, len)) == NULL) {
408 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
409 }
410
411 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
412 mbedtls_platform_zeroize(buf, len);
413 mbedtls_free(buf);
414 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
415 }
416
417 if (enc != 0) {
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200418#if defined(PEM_RFC1421)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (pwd == NULL) {
420 mbedtls_platform_zeroize(buf, len);
421 mbedtls_free(buf);
422 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000423 }
424
Andres AG51a7ae12017-02-22 16:23:26 +0000425 ret = 0;
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
429 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
430 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
431 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
437 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
438 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
439 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
440 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
441 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
442 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (ret != 0) {
446 mbedtls_free(buf);
447 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000448 }
449
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200450 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200451 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
452 * length bytes (allow 4 to be sure) in all known use cases.
453 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000454 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200455 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
457 mbedtls_platform_zeroize(buf, len);
458 mbedtls_free(buf);
459 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000460 }
461#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 mbedtls_platform_zeroize(buf, len);
463 mbedtls_free(buf);
464 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Przemek Stekiel0cd6f082022-08-18 12:38:30 +0200465#endif /* PEM_RFC1421 */
Paul Bakker96743fc2011-02-12 14:30:57 +0000466 }
467
468 ctx->buf = buf;
469 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000472}
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200475{
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (ctx->buf != NULL) {
477 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
478 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500479 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100487int mbedtls_pem_write_buffer(const char *header, const char *footer,
488 const unsigned char *der_data, size_t der_len,
489 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200490{
Janos Follath24eed8d2019-11-22 13:21:35 +0000491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000492 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100493 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
496 add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200499 *olen = use_len + add_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200501 }
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (use_len != 0 &&
504 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
505 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200506 }
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
509 der_len)) != 0) {
510 mbedtls_free(encode_buf);
511 return ret;
512 }
513
514 memcpy(p, header, strlen(header));
515 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200516 c = encode_buf;
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 while (use_len) {
519 len = (use_len > 64) ? 64 : use_len;
520 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200521 use_len -= len;
522 p += len;
523 c += len;
524 *p++ = '\n';
525 }
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 memcpy(p, footer, strlen(footer));
528 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200529
530 *p++ = '\0';
531 *olen = p - buf;
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 /* Clean any remaining data previously written to the buffer */
534 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 mbedtls_free(encode_buf);
537 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#endif /* MBEDTLS_PEM_WRITE_C */
540#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */