blob: 8c07048708ba5b256eb073863ee83e1a3e6e02fe [file] [log] [blame]
Paul Bakker96743fc2011-02-12 14:30:57 +00001/*
2 * Privacy Enhanced Mail (PEM) decoding
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker96743fc2011-02-12 14:30:57 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker96743fc2011-02-12 14:30:57 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker96743fc2011-02-12 14:30:57 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
Rich Evansce2f2372015-02-06 13:57:42 +000054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pem.h"
56#include "mbedtls/base64.h"
57#include "mbedtls/des.h"
58#include "mbedtls/aes.h"
59#include "mbedtls/md5.h"
60#include "mbedtls/cipher.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000061
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <string.h>
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020066#else
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020068#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020070#endif
71
Andres AGc0db5112016-12-07 15:05:53 +000072#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker34617722014-06-13 17:20:13 +020073/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020075 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
76}
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000081}
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
84 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000085/*
86 * Read a 16-byte hex string and convert it to binary
87 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020088static int pem_get_iv( const unsigned char *s, unsigned char *iv,
89 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000090{
Paul Bakker23986e52011-04-24 08:57:21 +000091 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000092
93 memset( iv, 0, iv_len );
94
95 for( i = 0; i < iv_len * 2; i++, s++ )
96 {
97 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
98 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
99 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000101
102 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
103
104 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
105 }
106
107 return( 0 );
108}
109
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100110static int pem_pbkdf1( unsigned char *key, size_t keylen,
111 unsigned char *iv,
112 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000115 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +0000116 size_t use_len;
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100117 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200120
Paul Bakker96743fc2011-02-12 14:30:57 +0000121 /*
122 * key[ 0..15] = MD5(pwd || IV)
123 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100124 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100125 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100126 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100127 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100128 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100129 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100130 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100131 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000132
133 if( keylen <= 16 )
134 {
135 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100136 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000137 }
138
139 memcpy( key, md5sum, 16 );
140
141 /*
142 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
143 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100144 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100145 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100146 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100147 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100148 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100149 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100150 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100151 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100152 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100153 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000154
155 use_len = 16;
156 if( keylen < 32 )
157 use_len = keylen - 16;
158
159 memcpy( key + 16, md5sum, use_len );
160
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_md5_free( &md5_ctx );
163 mbedtls_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100164
165 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000166}
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000169/*
170 * Decrypt with DES-CBC, using PBKDF1 for key derivation
171 */
Andres AG51a7ae12017-02-22 16:23:26 +0000172static int pem_des_decrypt( unsigned char des_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_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000177 unsigned char des_key[8];
Andres AG51a7ae12017-02-22 16:23:26 +0000178 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200181
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100182 if( ( ret = pem_pbkdf1( des_key, 8, des_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_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
186 goto exit;
187 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000188 des_iv, buf, buf );
189
Andres AG51a7ae12017-02-22 16:23:26 +0000190exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_des_free( &des_ctx );
192 mbedtls_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000193
194 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000195}
196
197/*
198 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
199 */
Andres AG51a7ae12017-02-22 16:23:26 +0000200static int pem_des3_decrypt( unsigned char des3_iv[8],
201 unsigned char *buf, size_t buflen,
202 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000205 unsigned char des3_key[24];
Andres AG51a7ae12017-02-22 16:23:26 +0000206 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200209
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100210 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
211 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000212
Andres AG51a7ae12017-02-22 16:23:26 +0000213 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
214 goto exit;
215 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000216 des3_iv, buf, buf );
217
Andres AG51a7ae12017-02-22 16:23:26 +0000218exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_des3_free( &des3_ctx );
220 mbedtls_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000221
222 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000227/*
228 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
229 */
Andres AG51a7ae12017-02-22 16:23:26 +0000230static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
231 unsigned char *buf, size_t buflen,
232 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000233{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000235 unsigned char aes_key[32];
Andres AG51a7ae12017-02-22 16:23:26 +0000236 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200239
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100240 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
241 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000242
Andres AG51a7ae12017-02-22 16:23:26 +0000243 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
244 goto exit;
245 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000246 aes_iv, buf, buf );
247
Andres AG51a7ae12017-02-22 16:23:26 +0000248exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_aes_free( &aes_ctx );
250 mbedtls_zeroize( aes_key, keylen );
Andres AG51a7ae12017-02-22 16:23:26 +0000251
252 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
257 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200260 const unsigned char *data, const unsigned char *pwd,
261 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000262{
Paul Bakker23986e52011-04-24 08:57:21 +0000263 int ret, enc;
264 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000265 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200266 const unsigned char *s1, *s2, *end;
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 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000271#else
272 ((void) pwd);
273 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
275 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000276
277 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000279
Paul Bakker3c2122f2013-06-24 19:03:14 +0200280 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000281
282 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000284
Paul Bakker3c2122f2013-06-24 19:03:14 +0200285 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000286
287 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000289
290 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200291 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000292 if( *s1 == '\r' ) s1++;
293 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200295
296 end = s2;
297 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200298 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200299 if( *end == '\r' ) end++;
300 if( *end == '\n' ) end++;
301 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000302
303 enc = 0;
304
Andres AG703990b2016-10-24 11:23:36 +0100305 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
308 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000309 enc++;
310
311 s1 += 22;
312 if( *s1 == '\r' ) s1++;
313 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000315
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100318 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000319 {
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;
Andres AG703990b2016-10-24 11:23:36 +0100323 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000325
326 s1 += 16;
327 }
Andres AG703990b2016-10-24 11:23:36 +0100328 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000331
332 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100333 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
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)
Andres AG703990b2016-10-24 11:23:36 +0100341 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000342 {
Andres AG703990b2016-10-24 11:23:36 +0100343 if( s2 - s1 < 22 )
344 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
345 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000347 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000349 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000351 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000353
354 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100355 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000357
358 s1 += 32;
359 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( enc_alg == MBEDTLS_CIPHER_NONE )
363 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000364
365 if( *s1 == '\r' ) s1++;
366 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000368#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
370#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
371 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000372 }
373
Andres AG703990b2016-10-24 11:23:36 +0100374 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100375 return( MBEDTLS_ERR_PEM_INVALID_DATA );
376
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100377 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
380 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000381
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200382 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200383 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000384
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100385 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000386 {
Andres Amaya Garcia92d46f02017-07-07 10:46:51 +0100387 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_free( buf );
389 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000390 }
Paul Bakkercff68422013-09-15 20:43:33 +0200391
Paul Bakker96743fc2011-02-12 14:30:57 +0000392 if( enc != 0 )
393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
395 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000396 if( pwd == NULL )
397 {
Andres Amaya Garcia03d70502017-06-26 11:44:54 +0100398 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_free( buf );
400 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000401 }
402
Andres AG51a7ae12017-02-22 16:23:26 +0000403 ret = 0;
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_DES_C)
406 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000407 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000409 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_AES_C)
413 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000414 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000416 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000418 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000420
Andres AG51a7ae12017-02-22 16:23:26 +0000421 if( ret != 0 )
422 {
423 mbedtls_free( buf );
424 return( ret );
425 }
426
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200427 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200428 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
429 * length bytes (allow 4 to be sure) in all known use cases.
430 *
431 * Use that as heurisitic to try detecting password mismatchs.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200432 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200433 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000434 {
Andres Amaya Garcia03d70502017-06-26 11:44:54 +0100435 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_free( buf );
437 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000438 }
439#else
Andres Amaya Garcia03d70502017-06-26 11:44:54 +0100440 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_free( buf );
442 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
443#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
444 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000445 }
446
447 ctx->buf = buf;
448 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000449
450 return( 0 );
451}
452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200454{
Ron Eldor65112b12017-09-06 17:09:41 +0300455 if( ctx->buf != NULL )
Ron Eldor9d84b4c2017-09-05 17:17:31 +0300456 mbedtls_zeroize( ctx->buf, ctx->buflen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_free( ctx->buf );
458 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_PEM_WRITE_C)
465int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200466 const unsigned char *der_data, size_t der_len,
467 unsigned char *buf, size_t buf_len, size_t *olen )
468{
469 int ret;
Andres AG9cf1f962017-01-30 14:34:25 +0000470 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100471 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200472
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100473 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200474 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
475
Paul Bakker77e23fb2013-09-15 20:03:26 +0200476 if( use_len + add_len > buf_len )
477 {
478 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200480 }
481
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100482 if( use_len != 0 &&
483 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200484 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200485
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100486 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200487 der_len ) ) != 0 )
488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200490 return( ret );
491 }
492
493 memcpy( p, header, strlen( header ) );
494 p += strlen( header );
495 c = encode_buf;
496
497 while( use_len )
498 {
499 len = ( use_len > 64 ) ? 64 : use_len;
500 memcpy( p, c, len );
501 use_len -= len;
502 p += len;
503 c += len;
504 *p++ = '\n';
505 }
506
507 memcpy( p, footer, strlen( footer ) );
508 p += strlen( footer );
509
510 *p++ = '\0';
511 *olen = p - buf;
512
Paul Elliott102bac72020-12-03 19:34:30 +0000513 /* Clean any remaining data previously written to the buffer */
514 memset( buf + *olen, 0, buf_len - *olen );
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200517 return( 0 );
518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#endif /* MBEDTLS_PEM_WRITE_C */
520#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */