blob: 3b9a3e91f2919127e19dde1472d75985a3790588 [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"
Paul Bakker96743fc2011-02-12 14:30:57 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020036
Andres AGc0db5112016-12-07 15:05:53 +000037#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038void mbedtls_pem_init(mbedtls_pem_context *ctx)
Paul Bakker96743fc2011-02-12 14:30:57 +000039{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 memset(ctx, 0, sizeof(mbedtls_pem_context));
Paul Bakker96743fc2011-02-12 14:30:57 +000041}
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +000045/*
46 * Read a 16-byte hex string and convert it to binary
47 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048static int pem_get_iv(const unsigned char *s, unsigned char *iv,
49 size_t iv_len)
Paul Bakker96743fc2011-02-12 14:30:57 +000050{
Paul Bakker23986e52011-04-24 08:57:21 +000051 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 memset(iv, 0, iv_len);
Paul Bakker96743fc2011-02-12 14:30:57 +000054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 for (i = 0; i < iv_len * 2; i++, s++) {
56 if (*s >= '0' && *s <= '9') {
57 j = *s - '0';
58 } else
59 if (*s >= 'A' && *s <= 'F') {
60 j = *s - '7';
61 } else
62 if (*s >= 'a' && *s <= 'f') {
63 j = *s - 'W';
64 } else {
65 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
66 }
Paul Bakker96743fc2011-02-12 14:30:57 +000067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 k = ((i & 1) != 0) ? j : j << 4;
Paul Bakker96743fc2011-02-12 14:30:57 +000069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
Paul Bakker96743fc2011-02-12 14:30:57 +000071 }
72
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +000074}
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076static int pem_pbkdf1(unsigned char *key, size_t keylen,
77 unsigned char *iv,
78 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +000079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000081 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000082 size_t use_len;
Janos Follath24eed8d2019-11-22 13:21:35 +000083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +000084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 mbedtls_md5_init(&md5_ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020086
Paul Bakker96743fc2011-02-12 14:30:57 +000087 /*
88 * key[ 0..15] = MD5(pwd || IV)
89 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 if ((ret = mbedtls_md5_starts_ret(&md5_ctx)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010091 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 }
93 if ((ret = mbedtls_md5_update_ret(&md5_ctx, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010094 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 }
96 if ((ret = mbedtls_md5_update_ret(&md5_ctx, iv, 8)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010097 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 }
99 if ((ret = mbedtls_md5_finish_ret(&md5_ctx, md5sum)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100100 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000101 }
102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 if (keylen <= 16) {
104 memcpy(key, md5sum, keylen);
105 goto exit;
106 }
107
108 memcpy(key, md5sum, 16);
Paul Bakker96743fc2011-02-12 14:30:57 +0000109
110 /*
111 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if ((ret = mbedtls_md5_starts_ret(&md5_ctx)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100114 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 }
116 if ((ret = mbedtls_md5_update_ret(&md5_ctx, md5sum, 16)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100117 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 }
119 if ((ret = mbedtls_md5_update_ret(&md5_ctx, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100120 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 }
122 if ((ret = mbedtls_md5_update_ret(&md5_ctx, iv, 8)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100123 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 }
125 if ((ret = mbedtls_md5_finish_ret(&md5_ctx, md5sum)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100126 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000128
129 use_len = 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 if (keylen < 32) {
Paul Bakker96743fc2011-02-12 14:30:57 +0000131 use_len = keylen - 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 memcpy(key + 16, md5sum, use_len);
Paul Bakker96743fc2011-02-12 14:30:57 +0000135
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100136exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 mbedtls_md5_free(&md5_ctx);
138 mbedtls_platform_zeroize(md5sum, 16);
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000144/*
145 * Decrypt with DES-CBC, using PBKDF1 for key derivation
146 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147static int pem_des_decrypt(unsigned char des_iv[8],
148 unsigned char *buf, size_t buflen,
149 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000152 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 mbedtls_des_init(&des_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100158 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000162 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 }
164 ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
165 des_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000166
Andres AG51a7ae12017-02-22 16:23:26 +0000167exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 mbedtls_des_free(&des_ctx);
169 mbedtls_platform_zeroize(des_key, 8);
Andres AG51a7ae12017-02-22 16:23:26 +0000170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000172}
173
174/*
175 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
176 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177static int pem_des3_decrypt(unsigned char des3_iv[8],
178 unsigned char *buf, size_t buflen,
179 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000182 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 mbedtls_des3_init(&des3_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100188 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000192 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 }
194 ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
195 des3_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000196
Andres AG51a7ae12017-02-22 16:23:26 +0000197exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 mbedtls_des3_free(&des3_ctx);
199 mbedtls_platform_zeroize(des3_key, 24);
Andres AG51a7ae12017-02-22 16:23:26 +0000200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000206/*
207 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
208 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
210 unsigned char *buf, size_t buflen,
211 const unsigned char *pwd, size_t pwdlen)
Paul Bakker96743fc2011-02-12 14:30:57 +0000212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000214 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 mbedtls_aes_init(&aes_ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100220 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
Andres AG51a7ae12017-02-22 16:23:26 +0000224 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 }
226 ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
227 aes_iv, buf, buf);
Paul Bakker96743fc2011-02-12 14:30:57 +0000228
Andres AG51a7ae12017-02-22 16:23:26 +0000229exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 mbedtls_aes_free(&aes_ctx);
231 mbedtls_platform_zeroize(aes_key, keylen);
Andres AG51a7ae12017-02-22 16:23:26 +0000232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
238 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
241 const unsigned char *data, const unsigned char *pwd,
242 size_t pwdlen, size_t *use_len)
Paul Bakker96743fc2011-02-12 14:30:57 +0000243{
Paul Bakker23986e52011-04-24 08:57:21 +0000244 int ret, enc;
245 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000246 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200247 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +0000250 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000252#else
253 ((void) pwd);
254 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
256 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (ctx == NULL) {
259 return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
260 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 s1 = (unsigned char *) strstr((const char *) data, header);
Paul Bakker96743fc2011-02-12 14:30:57 +0000263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if (s1 == NULL) {
265 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
266 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 s2 = (unsigned char *) strstr((const char *) data, footer);
Paul Bakker96743fc2011-02-12 14:30:57 +0000269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 if (s2 == NULL || s2 <= s1) {
271 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
272 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 s1 += strlen(header);
275 if (*s1 == ' ') {
276 s1++;
277 }
278 if (*s1 == '\r') {
279 s1++;
280 }
281 if (*s1 == '\n') {
282 s1++;
283 } else {
284 return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
285 }
Paul Bakker00b28602013-06-24 13:02:41 +0200286
287 end = s2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 end += strlen(footer);
289 if (*end == ' ') {
290 end++;
291 }
292 if (*end == '\r') {
293 end++;
294 }
295 if (*end == '\n') {
296 end++;
297 }
Paul Bakker00b28602013-06-24 13:02:41 +0200298 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000299
300 enc = 0;
301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
Paul Bakker96743fc2011-02-12 14:30:57 +0000305 enc++;
306
307 s1 += 22;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 if (*s1 == '\r') {
309 s1++;
310 }
311 if (*s1 == '\n') {
312 s1++;
313 } else {
314 return MBEDTLS_ERR_PEM_INVALID_DATA;
315 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000316
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#if defined(MBEDTLS_DES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000321
322 s1 += 23;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
324 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
325 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000326
327 s1 += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 } else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000330
331 s1 += 18;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
333 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
334 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000335
336 s1 += 16;
337 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
342 if (s2 - s1 < 22) {
343 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
344 } else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 } else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 } else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 } else {
351 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
352 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000353
354 s1 += 22;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
356 return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
357 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000358
359 s1 += 32;
360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 if (enc_alg == MBEDTLS_CIPHER_NONE) {
364 return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
365 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 if (*s1 == '\r') {
368 s1++;
369 }
370 if (*s1 == '\n') {
371 s1++;
372 } else {
373 return MBEDTLS_ERR_PEM_INVALID_DATA;
374 }
Paul Bakker96743fc2011-02-12 14:30:57 +0000375#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
378 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000379 }
380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 if (s1 >= s2) {
382 return MBEDTLS_ERR_PEM_INVALID_DATA;
Paul Bakker96743fc2011-02-12 14:30:57 +0000383 }
Paul Bakkercff68422013-09-15 20:43:33 +0200384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 ret = mbedtls_base64_decode(NULL, 0, &len, s1, s2 - s1);
386
387 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
388 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
389 }
390
391 if ((buf = mbedtls_calloc(1, len)) == NULL) {
392 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
393 }
394
395 if ((ret = mbedtls_base64_decode(buf, len, &len, s1, s2 - s1)) != 0) {
396 mbedtls_platform_zeroize(buf, len);
397 mbedtls_free(buf);
398 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
399 }
400
401 if (enc != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 (defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
404 if (pwd == NULL) {
405 mbedtls_platform_zeroize(buf, len);
406 mbedtls_free(buf);
407 return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000408 }
409
Andres AG51a7ae12017-02-22 16:23:26 +0000410 ret = 0;
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_DES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
414 ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
415 } else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
416 ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
422 ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
423 } else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
424 ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
425 } else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
426 ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if (ret != 0) {
431 mbedtls_free(buf);
432 return ret;
Andres AG51a7ae12017-02-22 16:23:26 +0000433 }
434
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200435 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200436 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
437 * length bytes (allow 4 to be sure) in all known use cases.
438 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000439 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200440 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 if (len <= 2 || buf[0] != 0x30 || buf[1] > 0x83) {
442 mbedtls_platform_zeroize(buf, len);
443 mbedtls_free(buf);
444 return MBEDTLS_ERR_PEM_PASSWORD_MISMATCH;
Paul Bakker96743fc2011-02-12 14:30:57 +0000445 }
446#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 mbedtls_platform_zeroize(buf, len);
448 mbedtls_free(buf);
449 return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
451 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000452 }
453
454 ctx->buf = buf;
455 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 return 0;
Paul Bakker96743fc2011-02-12 14:30:57 +0000458}
459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460void mbedtls_pem_free(mbedtls_pem_context *ctx)
Paul Bakkercff68422013-09-15 20:43:33 +0200461{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 if (ctx->buf != NULL) {
463 mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
464 mbedtls_free(ctx->buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 mbedtls_free(ctx->info);
Paul Bakkercff68422013-09-15 20:43:33 +0200467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
Paul Bakkercff68422013-09-15 20:43:33 +0200469}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473int mbedtls_pem_write_buffer(const char *header, const char *footer,
474 const unsigned char *der_data, size_t der_len,
475 unsigned char *buf, size_t buf_len, size_t *olen)
Paul Bakker77e23fb2013-09-15 20:03:26 +0200476{
Janos Follath24eed8d2019-11-22 13:21:35 +0000477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000478 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100479 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
482 add_len = strlen(header) + strlen(footer) + (use_len / 64) + 1;
Paul Bakker16300582014-04-11 13:28:43 +0200483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 if (use_len + add_len > buf_len) {
Paul Bakker77e23fb2013-09-15 20:03:26 +0200485 *olen = use_len + add_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200487 }
488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 if (use_len != 0 &&
490 ((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
491 return MBEDTLS_ERR_PEM_ALLOC_FAILED;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200492 }
493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
495 der_len)) != 0) {
496 mbedtls_free(encode_buf);
497 return ret;
498 }
499
500 memcpy(p, header, strlen(header));
501 p += strlen(header);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200502 c = encode_buf;
503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 while (use_len) {
505 len = (use_len > 64) ? 64 : use_len;
506 memcpy(p, c, len);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200507 use_len -= len;
508 p += len;
509 c += len;
510 *p++ = '\n';
511 }
512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 memcpy(p, footer, strlen(footer));
514 p += strlen(footer);
Paul Bakker77e23fb2013-09-15 20:03:26 +0200515
516 *p++ = '\0';
517 *olen = p - buf;
518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 /* Clean any remaining data previously written to the buffer */
520 memset(buf + *olen, 0, buf_len - *olen);
Paul Elliott557b8d62020-11-19 09:46:56 +0000521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 mbedtls_free(encode_buf);
523 return 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#endif /* MBEDTLS_PEM_WRITE_C */
526#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */