blob: 6069a23dec7901abce6cb4ff9442f0a8fe8da4a4 [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"
Paul Bakker96743fc2011-02-12 14:30:57 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020042#else
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020044#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020046#endif
47
Andres AGc0db5112016-12-07 15:05:53 +000048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000052}
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
55 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000056/*
57 * Read a 16-byte hex string and convert it to binary
58 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020059static int pem_get_iv( const unsigned char *s, unsigned char *iv,
60 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000061{
Paul Bakker23986e52011-04-24 08:57:21 +000062 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000063
64 memset( iv, 0, iv_len );
65
66 for( i = 0; i < iv_len * 2; i++, s++ )
67 {
68 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
69 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
70 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +000072
73 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
74
75 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
76 }
77
78 return( 0 );
79}
80
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010081static int pem_pbkdf1( unsigned char *key, size_t keylen,
82 unsigned char *iv,
83 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000086 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000087 size_t use_len;
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010088 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020091
Paul Bakker96743fc2011-02-12 14:30:57 +000092 /*
93 * key[ 0..15] = MD5(pwd || IV)
94 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010095 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010096 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010097 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010098 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010099 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100100 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100101 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100102 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000103
104 if( keylen <= 16 )
105 {
106 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100107 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000108 }
109
110 memcpy( key, md5sum, 16 );
111
112 /*
113 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
114 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100115 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100116 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100117 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100118 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100119 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100120 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100121 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100122 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100123 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100124 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000125
126 use_len = 16;
127 if( keylen < 32 )
128 use_len = keylen - 16;
129
130 memcpy( key + 16, md5sum, use_len );
131
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100132exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_md5_free( &md5_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500134 mbedtls_platform_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100135
136 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000137}
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000140/*
141 * Decrypt with DES-CBC, using PBKDF1 for key derivation
142 */
Andres AG51a7ae12017-02-22 16:23:26 +0000143static int pem_des_decrypt( unsigned char des_iv[8],
144 unsigned char *buf, size_t buflen,
145 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000148 unsigned char des_key[8];
Andres AG51a7ae12017-02-22 16:23:26 +0000149 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200152
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100153 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
154 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000155
Andres AG51a7ae12017-02-22 16:23:26 +0000156 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
157 goto exit;
158 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000159 des_iv, buf, buf );
160
Andres AG51a7ae12017-02-22 16:23:26 +0000161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_des_free( &des_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500163 mbedtls_platform_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000164
165 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000166}
167
168/*
169 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
170 */
Andres AG51a7ae12017-02-22 16:23:26 +0000171static int pem_des3_decrypt( unsigned char des3_iv[8],
172 unsigned char *buf, size_t buflen,
173 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000176 unsigned char des3_key[24];
Andres AG51a7ae12017-02-22 16:23:26 +0000177 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200180
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100181 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
182 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000183
Andres AG51a7ae12017-02-22 16:23:26 +0000184 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
185 goto exit;
186 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000187 des3_iv, buf, buf );
188
Andres AG51a7ae12017-02-22 16:23:26 +0000189exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_des3_free( &des3_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500191 mbedtls_platform_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000192
193 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000198/*
199 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
200 */
Andres AG51a7ae12017-02-22 16:23:26 +0000201static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
202 unsigned char *buf, size_t buflen,
203 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000206 unsigned char aes_key[32];
Andres AG51a7ae12017-02-22 16:23:26 +0000207 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200210
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100211 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
212 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000213
Andres AG51a7ae12017-02-22 16:23:26 +0000214 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
215 goto exit;
216 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000217 aes_iv, buf, buf );
218
Andres AG51a7ae12017-02-22 16:23:26 +0000219exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_aes_free( &aes_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500221 mbedtls_platform_zeroize( aes_key, keylen );
Andres AG51a7ae12017-02-22 16:23:26 +0000222
223 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
228 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200231 const unsigned char *data, const unsigned char *pwd,
232 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000233{
Paul Bakker23986e52011-04-24 08:57:21 +0000234 int ret, enc;
235 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000236 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200237 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
239 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000240 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000242#else
243 ((void) pwd);
244 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
246 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000247
248 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000250
Paul Bakker3c2122f2013-06-24 19:03:14 +0200251 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000252
253 if( s1 == NULL )
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
Paul Bakker3c2122f2013-06-24 19:03:14 +0200256 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000257
258 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000260
261 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200262 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000263 if( *s1 == '\r' ) s1++;
264 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200266
267 end = s2;
268 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200269 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200270 if( *end == '\r' ) end++;
271 if( *end == '\n' ) end++;
272 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000273
274 enc = 0;
275
Andres AG703990b2016-10-24 11:23:36 +0100276 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
279 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000280 enc++;
281
282 s1 += 22;
283 if( *s1 == '\r' ) s1++;
284 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000286
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100289 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000292
293 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100294 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000296
297 s1 += 16;
298 }
Andres AG703990b2016-10-24 11:23:36 +0100299 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000302
303 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100304 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000306
307 s1 += 16;
308 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100312 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000313 {
Andres AG703990b2016-10-24 11:23:36 +0100314 if( s2 - s1 < 22 )
315 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
316 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000318 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000320 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000324
325 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100326 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000328
329 s1 += 32;
330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 if( enc_alg == MBEDTLS_CIPHER_NONE )
334 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000335
336 if( *s1 == '\r' ) s1++;
337 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000339#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
341#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
342 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000343 }
344
Andres AG703990b2016-10-24 11:23:36 +0100345 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100346 return( MBEDTLS_ERR_PEM_INVALID_DATA );
347
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100348 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
351 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000352
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200353 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200354 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000355
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100356 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000357 {
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_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000361 }
Paul Bakkercff68422013-09-15 20:43:33 +0200362
Paul Bakker96743fc2011-02-12 14:30:57 +0000363 if( enc != 0 )
364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
366 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000367 if( pwd == NULL )
368 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500369 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_free( buf );
371 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000372 }
373
Andres AG51a7ae12017-02-22 16:23:26 +0000374 ret = 0;
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_DES_C)
377 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000378 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000380 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_AES_C)
384 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000385 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000387 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000389 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000391
Andres AG51a7ae12017-02-22 16:23:26 +0000392 if( ret != 0 )
393 {
394 mbedtls_free( buf );
395 return( ret );
396 }
397
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200398 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200399 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
400 * length bytes (allow 4 to be sure) in all known use cases.
401 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000402 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200403 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200404 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000405 {
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_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000409 }
410#else
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500411 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_free( buf );
413 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
414#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
415 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000416 }
417
418 ctx->buf = buf;
419 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000420
421 return( 0 );
422}
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200425{
Ron Eldor65112b12017-09-06 17:09:41 +0300426 if( ctx->buf != NULL )
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500427 mbedtls_platform_zeroize( ctx->buf, ctx->buflen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_free( ctx->buf );
429 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200430
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500431 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200432}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#if defined(MBEDTLS_PEM_WRITE_C)
436int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200437 const unsigned char *der_data, size_t der_len,
438 unsigned char *buf, size_t buf_len, size_t *olen )
439{
440 int ret;
Andres AG9cf1f962017-01-30 14:34:25 +0000441 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100442 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200443
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100444 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200445 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
446
Paul Bakker77e23fb2013-09-15 20:03:26 +0200447 if( use_len + add_len > buf_len )
448 {
449 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200451 }
452
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100453 if( use_len != 0 &&
454 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200455 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200456
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100457 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200458 der_len ) ) != 0 )
459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200461 return( ret );
462 }
463
464 memcpy( p, header, strlen( header ) );
465 p += strlen( header );
466 c = encode_buf;
467
468 while( use_len )
469 {
470 len = ( use_len > 64 ) ? 64 : use_len;
471 memcpy( p, c, len );
472 use_len -= len;
473 p += len;
474 c += len;
475 *p++ = '\n';
476 }
477
478 memcpy( p, footer, strlen( footer ) );
479 p += strlen( footer );
480
481 *p++ = '\0';
482 *olen = p - buf;
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 */