blob: cb1c82b3e4d77fa59711b860c4f2f60373756392 [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)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 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) && \
44 ( 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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020048static 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
53 memset( iv, 0, iv_len );
54
55 for( i = 0; i < iv_len * 2; i++, s++ )
56 {
57 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
58 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
59 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +000061
62 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
63
64 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
65 }
66
67 return( 0 );
68}
69
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010070static int pem_pbkdf1( unsigned char *key, size_t keylen,
71 unsigned char *iv,
72 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000075 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000076 size_t use_len;
Janos Follath24eed8d2019-11-22 13:21:35 +000077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +000078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020080
Paul Bakker96743fc2011-02-12 14:30:57 +000081 /*
82 * key[ 0..15] = MD5(pwd || IV)
83 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010084 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010085 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010086 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010087 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010088 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010089 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010090 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010091 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +000092
93 if( keylen <= 16 )
94 {
95 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010096 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +000097 }
98
99 memcpy( key, md5sum, 16 );
100
101 /*
102 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
103 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100104 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100105 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100106 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100107 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100108 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100109 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100110 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100111 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100112 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100113 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000114
115 use_len = 16;
116 if( keylen < 32 )
117 use_len = keylen - 16;
118
119 memcpy( key + 16, md5sum, use_len );
120
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100121exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_md5_free( &md5_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500123 mbedtls_platform_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100124
125 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000126}
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000129/*
130 * Decrypt with DES-CBC, using PBKDF1 for key derivation
131 */
Andres AG51a7ae12017-02-22 16:23:26 +0000132static int pem_des_decrypt( unsigned char des_iv[8],
133 unsigned char *buf, size_t buflen,
134 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000137 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200141
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100142 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
143 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000144
Andres AG51a7ae12017-02-22 16:23:26 +0000145 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
146 goto exit;
147 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000148 des_iv, buf, buf );
149
Andres AG51a7ae12017-02-22 16:23:26 +0000150exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_des_free( &des_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500152 mbedtls_platform_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000153
154 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000155}
156
157/*
158 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
159 */
Andres AG51a7ae12017-02-22 16:23:26 +0000160static int pem_des3_decrypt( unsigned char des3_iv[8],
161 unsigned char *buf, size_t buflen,
162 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000165 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200169
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100170 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
171 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000172
Andres AG51a7ae12017-02-22 16:23:26 +0000173 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
174 goto exit;
175 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000176 des3_iv, buf, buf );
177
Andres AG51a7ae12017-02-22 16:23:26 +0000178exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_des3_free( &des3_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500180 mbedtls_platform_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000181
182 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000187/*
188 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
189 */
Andres AG51a7ae12017-02-22 16:23:26 +0000190static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
191 unsigned char *buf, size_t buflen,
192 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000195 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200199
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100200 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
201 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000202
Andres AG51a7ae12017-02-22 16:23:26 +0000203 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
204 goto exit;
205 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000206 aes_iv, buf, buf );
207
Andres AG51a7ae12017-02-22 16:23:26 +0000208exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_aes_free( &aes_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500210 mbedtls_platform_zeroize( aes_key, keylen );
Andres AG51a7ae12017-02-22 16:23:26 +0000211
212 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
217 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200220 const unsigned char *data, const unsigned char *pwd,
221 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000222{
Paul Bakker23986e52011-04-24 08:57:21 +0000223 int ret, enc;
224 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000225 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200226 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
228 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000229 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000231#else
232 ((void) pwd);
233 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
235 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000236
237 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000239
Paul Bakker3c2122f2013-06-24 19:03:14 +0200240 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000241
242 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000244
Paul Bakker3c2122f2013-06-24 19:03:14 +0200245 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000246
247 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000249
250 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200251 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000252 if( *s1 == '\r' ) s1++;
253 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200255
256 end = s2;
257 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200258 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200259 if( *end == '\r' ) end++;
260 if( *end == '\n' ) end++;
261 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000262
263 enc = 0;
264
Andres AG703990b2016-10-24 11:23:36 +0100265 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
268 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000269 enc++;
270
271 s1 += 22;
272 if( *s1 == '\r' ) s1++;
273 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000275
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100278 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000281
282 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100283 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000285
286 s1 += 16;
287 }
Andres AG703990b2016-10-24 11:23:36 +0100288 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000291
292 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100293 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000295
296 s1 += 16;
297 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100301 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000302 {
Andres AG703990b2016-10-24 11:23:36 +0100303 if( s2 - s1 < 22 )
304 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
305 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000307 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000309 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000311 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000313
314 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100315 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000317
318 s1 += 32;
319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 if( enc_alg == MBEDTLS_CIPHER_NONE )
323 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000324
325 if( *s1 == '\r' ) s1++;
326 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000328#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
330#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
331 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000332 }
333
Andres AG703990b2016-10-24 11:23:36 +0100334 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100335 return( MBEDTLS_ERR_PEM_INVALID_DATA );
336
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100337 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Chris Jones9f7a6932021-04-14 12:12:09 +0100340 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) );
Paul Bakker96743fc2011-02-12 14:30:57 +0000341
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200342 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200343 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000344
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100345 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000346 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500347 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_free( buf );
Chris Jones9f7a6932021-04-14 12:12:09 +0100349 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) );
Paul Bakker96743fc2011-02-12 14:30:57 +0000350 }
Paul Bakkercff68422013-09-15 20:43:33 +0200351
Paul Bakker96743fc2011-02-12 14:30:57 +0000352 if( enc != 0 )
353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
355 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000356 if( pwd == NULL )
357 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500358 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_free( buf );
360 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000361 }
362
Andres AG51a7ae12017-02-22 16:23:26 +0000363 ret = 0;
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_DES_C)
366 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000367 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000369 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_AES_C)
373 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000374 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000376 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000378 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000380
Andres AG51a7ae12017-02-22 16:23:26 +0000381 if( ret != 0 )
382 {
383 mbedtls_free( buf );
384 return( ret );
385 }
386
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200387 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200388 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
389 * length bytes (allow 4 to be sure) in all known use cases.
390 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000391 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200392 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200393 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000394 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500395 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_free( buf );
397 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000398 }
399#else
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500400 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_free( buf );
402 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
403#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
404 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000405 }
406
407 ctx->buf = buf;
408 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000409
410 return( 0 );
411}
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200414{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500415 if ( ctx->buf != NULL )
416 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500417 mbedtls_platform_zeroize( ctx->buf, ctx->buflen );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500418 mbedtls_free( ctx->buf );
419 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200421
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500422 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_PEM_WRITE_C)
427int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200428 const unsigned char *der_data, size_t der_len,
429 unsigned char *buf, size_t buf_len, size_t *olen )
430{
Janos Follath24eed8d2019-11-22 13:21:35 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000432 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100433 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200434
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100435 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200436 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
437
Paul Bakker77e23fb2013-09-15 20:03:26 +0200438 if( use_len + add_len > buf_len )
439 {
440 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200442 }
443
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100444 if( use_len != 0 &&
445 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200446 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200447
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100448 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200449 der_len ) ) != 0 )
450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200452 return( ret );
453 }
454
455 memcpy( p, header, strlen( header ) );
456 p += strlen( header );
457 c = encode_buf;
458
459 while( use_len )
460 {
461 len = ( use_len > 64 ) ? 64 : use_len;
462 memcpy( p, c, len );
463 use_len -= len;
464 p += len;
465 c += len;
466 *p++ = '\n';
467 }
468
469 memcpy( p, footer, strlen( footer ) );
470 p += strlen( footer );
471
472 *p++ = '\0';
473 *olen = p - buf;
474
Paul Elliott557b8d62020-11-19 09:46:56 +0000475 /* Clean any remaining data previously written to the buffer */
476 memset( buf + *olen, 0, buf_len - *olen );
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200479 return( 0 );
480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_PEM_WRITE_C */
482#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
Paul Elliott557b8d62020-11-19 09:46:56 +0000483