blob: 1b40e5e5585e761e5f1bbf78e6813277a970d1ee [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020037#else
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020039#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020041#endif
42
Andres AGc0db5112016-12-07 15:05:53 +000043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000047}
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
50 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000051/*
52 * Read a 16-byte hex string and convert it to binary
53 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020054static int pem_get_iv( const unsigned char *s, unsigned char *iv,
55 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000056{
Paul Bakker23986e52011-04-24 08:57:21 +000057 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000058
59 memset( iv, 0, iv_len );
60
61 for( i = 0; i < iv_len * 2; i++, s++ )
62 {
63 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
64 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
65 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +000067
68 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
69
70 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
71 }
72
73 return( 0 );
74}
75
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 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 */
TRodziewicz26371e42021-06-08 16:45:41 +020090 if( ( ret = mbedtls_md5_starts( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010091 goto exit;
TRodziewicz26371e42021-06-08 16:45:41 +020092 if( ( ret = mbedtls_md5_update( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010093 goto exit;
TRodziewicz26371e42021-06-08 16:45:41 +020094 if( ( ret = mbedtls_md5_update( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010095 goto exit;
TRodziewicz26371e42021-06-08 16:45:41 +020096 if( ( ret = mbedtls_md5_finish( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010097 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +000098
99 if( keylen <= 16 )
100 {
101 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100102 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000103 }
104
105 memcpy( key, md5sum, 16 );
106
107 /*
108 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
109 */
TRodziewicz26371e42021-06-08 16:45:41 +0200110 if( ( ret = mbedtls_md5_starts( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100111 goto exit;
TRodziewicz26371e42021-06-08 16:45:41 +0200112 if( ( ret = mbedtls_md5_update( &md5_ctx, md5sum, 16 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100113 goto exit;
TRodziewicz26371e42021-06-08 16:45:41 +0200114 if( ( ret = mbedtls_md5_update( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100115 goto exit;
TRodziewicz26371e42021-06-08 16:45:41 +0200116 if( ( ret = mbedtls_md5_update( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100117 goto exit;
TRodziewicz26371e42021-06-08 16:45:41 +0200118 if( ( ret = mbedtls_md5_finish( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100119 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000120
121 use_len = 16;
122 if( keylen < 32 )
123 use_len = keylen - 16;
124
125 memcpy( key + 16, md5sum, use_len );
126
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100127exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_md5_free( &md5_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500129 mbedtls_platform_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100130
131 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000135/*
136 * Decrypt with DES-CBC, using PBKDF1 for key derivation
137 */
Andres AG51a7ae12017-02-22 16:23:26 +0000138static int pem_des_decrypt( unsigned char des_iv[8],
139 unsigned char *buf, size_t buflen,
140 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000143 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200147
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100148 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
149 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000150
Andres AG51a7ae12017-02-22 16:23:26 +0000151 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
152 goto exit;
153 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000154 des_iv, buf, buf );
155
Andres AG51a7ae12017-02-22 16:23:26 +0000156exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_des_free( &des_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500158 mbedtls_platform_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000159
160 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000161}
162
163/*
164 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
165 */
Andres AG51a7ae12017-02-22 16:23:26 +0000166static int pem_des3_decrypt( unsigned char des3_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_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000171 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200175
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100176 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
177 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000178
Andres AG51a7ae12017-02-22 16:23:26 +0000179 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
180 goto exit;
181 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000182 des3_iv, buf, buf );
183
Andres AG51a7ae12017-02-22 16:23:26 +0000184exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_des3_free( &des3_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500186 mbedtls_platform_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000187
188 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000193/*
194 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
195 */
Andres AG51a7ae12017-02-22 16:23:26 +0000196static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
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_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000201 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200205
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100206 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
207 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000208
Andres AG51a7ae12017-02-22 16:23:26 +0000209 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
210 goto exit;
211 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000212 aes_iv, buf, buf );
213
Andres AG51a7ae12017-02-22 16:23:26 +0000214exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_aes_free( &aes_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500216 mbedtls_platform_zeroize( aes_key, keylen );
Andres AG51a7ae12017-02-22 16:23:26 +0000217
218 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
223 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200226 const unsigned char *data, const unsigned char *pwd,
227 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000228{
Paul Bakker23986e52011-04-24 08:57:21 +0000229 int ret, enc;
230 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000231 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200232 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
234 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000235 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000237#else
238 ((void) pwd);
239 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
241 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000242
243 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000245
Paul Bakker3c2122f2013-06-24 19:03:14 +0200246 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000247
248 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000250
Paul Bakker3c2122f2013-06-24 19:03:14 +0200251 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000252
253 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000255
256 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200257 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000258 if( *s1 == '\r' ) s1++;
259 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200261
262 end = s2;
263 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200264 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200265 if( *end == '\r' ) end++;
266 if( *end == '\n' ) end++;
267 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000268
269 enc = 0;
270
Andres AG703990b2016-10-24 11:23:36 +0100271 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
274 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000275 enc++;
276
277 s1 += 22;
278 if( *s1 == '\r' ) s1++;
279 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000281
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100284 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000287
288 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100289 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000291
292 s1 += 16;
293 }
Andres AG703990b2016-10-24 11:23:36 +0100294 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000297
298 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100299 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000301
302 s1 += 16;
303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100307 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000308 {
Andres AG703990b2016-10-24 11:23:36 +0100309 if( s2 - s1 < 22 )
310 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
311 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000313 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000315 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000317 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000319
320 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100321 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000323
324 s1 += 32;
325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( enc_alg == MBEDTLS_CIPHER_NONE )
329 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000330
331 if( *s1 == '\r' ) s1++;
332 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000334#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
336#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
337 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000338 }
339
Andres AG703990b2016-10-24 11:23:36 +0100340 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100341 return( MBEDTLS_ERR_PEM_INVALID_DATA );
342
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100343 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Chris Jones9f7a6932021-04-14 12:12:09 +0100346 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) );
Paul Bakker96743fc2011-02-12 14:30:57 +0000347
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200348 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200349 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000350
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100351 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000352 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500353 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_free( buf );
Chris Jones9f7a6932021-04-14 12:12:09 +0100355 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) );
Paul Bakker96743fc2011-02-12 14:30:57 +0000356 }
Paul Bakkercff68422013-09-15 20:43:33 +0200357
Paul Bakker96743fc2011-02-12 14:30:57 +0000358 if( enc != 0 )
359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
361 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000362 if( pwd == NULL )
363 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500364 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_free( buf );
366 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000367 }
368
Andres AG51a7ae12017-02-22 16:23:26 +0000369 ret = 0;
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_DES_C)
372 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000373 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000375 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_AES_C)
379 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000380 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000382 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000384 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000386
Andres AG51a7ae12017-02-22 16:23:26 +0000387 if( ret != 0 )
388 {
389 mbedtls_free( buf );
390 return( ret );
391 }
392
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200393 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200394 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
395 * length bytes (allow 4 to be sure) in all known use cases.
396 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000397 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200398 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200399 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000400 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500401 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_free( buf );
403 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000404 }
405#else
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500406 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_free( buf );
408 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
409#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
410 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000411 }
412
413 ctx->buf = buf;
414 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000415
416 return( 0 );
417}
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200420{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500421 if ( ctx->buf != NULL )
422 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500423 mbedtls_platform_zeroize( ctx->buf, ctx->buflen );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500424 mbedtls_free( ctx->buf );
425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200427
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500428 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_PEM_WRITE_C)
433int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200434 const unsigned char *der_data, size_t der_len,
435 unsigned char *buf, size_t buf_len, size_t *olen )
436{
Janos Follath24eed8d2019-11-22 13:21:35 +0000437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000438 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100439 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200440
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100441 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200442 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
443
Paul Bakker77e23fb2013-09-15 20:03:26 +0200444 if( use_len + add_len > buf_len )
445 {
446 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200448 }
449
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100450 if( use_len != 0 &&
451 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200452 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200453
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100454 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200455 der_len ) ) != 0 )
456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200458 return( ret );
459 }
460
461 memcpy( p, header, strlen( header ) );
462 p += strlen( header );
463 c = encode_buf;
464
465 while( use_len )
466 {
467 len = ( use_len > 64 ) ? 64 : use_len;
468 memcpy( p, c, len );
469 use_len -= len;
470 p += len;
471 c += len;
472 *p++ = '\n';
473 }
474
475 memcpy( p, footer, strlen( footer ) );
476 p += strlen( footer );
477
478 *p++ = '\0';
479 *olen = p - buf;
480
Paul Elliott557b8d62020-11-19 09:46:56 +0000481 /* Clean any remaining data previously written to the buffer */
482 memset( buf + *olen, 0, buf_len - *olen );
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200485 return( 0 );
486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_PEM_WRITE_C */
488#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
Paul Elliott557b8d62020-11-19 09:46:56 +0000489