blob: 31f4a9a25e956112c4067ef545c8ee57c916a95e [file] [log] [blame]
Paul Bakker96743fc2011-02-12 14:30:57 +00001/*
2 * Privacy Enhanced Mail (PEM) decoding
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker96743fc2011-02-12 14:30:57 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker96743fc2011-02-12 14:30:57 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
Rich Evansce2f2372015-02-06 13:57:42 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pem.h"
31#include "mbedtls/base64.h"
32#include "mbedtls/des.h"
33#include "mbedtls/aes.h"
34#include "mbedtls/md5.h"
35#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000037#include "mbedtls/error.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020043#else
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020045#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020047#endif
48
Andres AGc0db5112016-12-07 15:05:53 +000049#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000053}
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
56 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000057/*
58 * Read a 16-byte hex string and convert it to binary
59 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020060static int pem_get_iv( const unsigned char *s, unsigned char *iv,
61 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000062{
Paul Bakker23986e52011-04-24 08:57:21 +000063 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000064
65 memset( iv, 0, iv_len );
66
67 for( i = 0; i < iv_len * 2; i++, s++ )
68 {
69 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
70 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
71 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +000073
74 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
75
76 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
77 }
78
79 return( 0 );
80}
81
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010082static int pem_pbkdf1( unsigned char *key, size_t keylen,
83 unsigned char *iv,
84 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000087 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000088 size_t use_len;
Janos Follath24eed8d2019-11-22 13:21:35 +000089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020092
Paul Bakker96743fc2011-02-12 14:30:57 +000093 /*
94 * key[ 0..15] = MD5(pwd || IV)
95 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010096 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010097 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010098 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010099 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100100 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100101 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100102 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100103 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000104
105 if( keylen <= 16 )
106 {
107 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100108 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000109 }
110
111 memcpy( key, md5sum, 16 );
112
113 /*
114 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
115 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100116 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100117 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100118 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100119 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100120 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100121 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100122 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100123 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100124 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100125 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000126
127 use_len = 16;
128 if( keylen < 32 )
129 use_len = keylen - 16;
130
131 memcpy( key + 16, md5sum, use_len );
132
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100133exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_md5_free( &md5_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500135 mbedtls_platform_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100136
137 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000141/*
142 * Decrypt with DES-CBC, using PBKDF1 for key derivation
143 */
Andres AG51a7ae12017-02-22 16:23:26 +0000144static int pem_des_decrypt( unsigned char des_iv[8],
145 unsigned char *buf, size_t buflen,
146 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000149 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200153
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100154 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
155 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000156
Andres AG51a7ae12017-02-22 16:23:26 +0000157 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
158 goto exit;
159 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000160 des_iv, buf, buf );
161
Andres AG51a7ae12017-02-22 16:23:26 +0000162exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_des_free( &des_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500164 mbedtls_platform_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000165
166 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000167}
168
169/*
170 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
171 */
Andres AG51a7ae12017-02-22 16:23:26 +0000172static int pem_des3_decrypt( unsigned char des3_iv[8],
173 unsigned char *buf, size_t buflen,
174 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000177 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200181
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100182 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
183 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000184
Andres AG51a7ae12017-02-22 16:23:26 +0000185 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
186 goto exit;
187 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000188 des3_iv, buf, buf );
189
Andres AG51a7ae12017-02-22 16:23:26 +0000190exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_des3_free( &des3_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500192 mbedtls_platform_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000193
194 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000199/*
200 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
201 */
Andres AG51a7ae12017-02-22 16:23:26 +0000202static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
203 unsigned char *buf, size_t buflen,
204 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000207 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200211
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100212 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
213 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000214
Andres AG51a7ae12017-02-22 16:23:26 +0000215 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
216 goto exit;
217 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000218 aes_iv, buf, buf );
219
Andres AG51a7ae12017-02-22 16:23:26 +0000220exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_aes_free( &aes_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500222 mbedtls_platform_zeroize( aes_key, keylen );
Andres AG51a7ae12017-02-22 16:23:26 +0000223
224 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
229 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200232 const unsigned char *data, const unsigned char *pwd,
233 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000234{
Paul Bakker23986e52011-04-24 08:57:21 +0000235 int ret, enc;
236 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000237 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200238 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
240 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000241 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000243#else
244 ((void) pwd);
245 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
247 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000248
249 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000251
Paul Bakker3c2122f2013-06-24 19:03:14 +0200252 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000253
254 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000256
Paul Bakker3c2122f2013-06-24 19:03:14 +0200257 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000258
259 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000261
262 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200263 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000264 if( *s1 == '\r' ) s1++;
265 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200267
268 end = s2;
269 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200270 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200271 if( *end == '\r' ) end++;
272 if( *end == '\n' ) end++;
273 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000274
275 enc = 0;
276
Andres AG703990b2016-10-24 11:23:36 +0100277 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
280 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000281 enc++;
282
283 s1 += 22;
284 if( *s1 == '\r' ) s1++;
285 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000287
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100290 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000293
294 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100295 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000297
298 s1 += 16;
299 }
Andres AG703990b2016-10-24 11:23:36 +0100300 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000303
304 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100305 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000307
308 s1 += 16;
309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100313 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000314 {
Andres AG703990b2016-10-24 11:23:36 +0100315 if( s2 - s1 < 22 )
316 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
317 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000319 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000321 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000323 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000325
326 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100327 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000329
330 s1 += 32;
331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 if( enc_alg == MBEDTLS_CIPHER_NONE )
335 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000336
337 if( *s1 == '\r' ) s1++;
338 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000340#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
342#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
343 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000344 }
345
Andres AG703990b2016-10-24 11:23:36 +0100346 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100347 return( MBEDTLS_ERR_PEM_INVALID_DATA );
348
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100349 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
352 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000353
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200354 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200355 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000356
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100357 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000358 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500359 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_free( buf );
361 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000362 }
Paul Bakkercff68422013-09-15 20:43:33 +0200363
Paul Bakker96743fc2011-02-12 14:30:57 +0000364 if( enc != 0 )
365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
367 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000368 if( pwd == NULL )
369 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500370 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_free( buf );
372 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000373 }
374
Andres AG51a7ae12017-02-22 16:23:26 +0000375 ret = 0;
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_DES_C)
378 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000379 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000381 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_AES_C)
385 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000386 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000388 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000390 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000392
Andres AG51a7ae12017-02-22 16:23:26 +0000393 if( ret != 0 )
394 {
395 mbedtls_free( buf );
396 return( ret );
397 }
398
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200399 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200400 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
401 * length bytes (allow 4 to be sure) in all known use cases.
402 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000403 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200404 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200405 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000406 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500407 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_free( buf );
409 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000410 }
411#else
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500412 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_free( buf );
414 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
415#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
416 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000417 }
418
419 ctx->buf = buf;
420 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000421
422 return( 0 );
423}
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200426{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 if ( ctx->buf != NULL )
428 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500429 mbedtls_platform_zeroize( ctx->buf, ctx->buflen );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 mbedtls_free( ctx->buf );
431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200433
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500434 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_PEM_WRITE_C)
439int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200440 const unsigned char *der_data, size_t der_len,
441 unsigned char *buf, size_t buf_len, size_t *olen )
442{
Janos Follath24eed8d2019-11-22 13:21:35 +0000443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000444 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100445 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200446
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100447 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200448 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
449
Paul Bakker77e23fb2013-09-15 20:03:26 +0200450 if( use_len + add_len > buf_len )
451 {
452 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200454 }
455
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100456 if( use_len != 0 &&
457 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200458 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200459
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100460 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200461 der_len ) ) != 0 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200464 return( ret );
465 }
466
467 memcpy( p, header, strlen( header ) );
468 p += strlen( header );
469 c = encode_buf;
470
471 while( use_len )
472 {
473 len = ( use_len > 64 ) ? 64 : use_len;
474 memcpy( p, c, len );
475 use_len -= len;
476 p += len;
477 c += len;
478 *p++ = '\n';
479 }
480
481 memcpy( p, footer, strlen( footer ) );
482 p += strlen( footer );
483
484 *p++ = '\0';
485 *olen = p - buf;
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200488 return( 0 );
489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#endif /* MBEDTLS_PEM_WRITE_C */
491#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */