blob: 07886195a92044f6dcf5b683cd94e0a92c2d026f [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"
Paul Bakker96743fc2011-02-12 14:30:57 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020041#else
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020043#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020045#endif
46
Andres AG480a9582016-12-07 15:05:53 +000047#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker34617722014-06-13 17:20:13 +020048/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020050 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
51}
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000056}
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
59 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000060/*
61 * Read a 16-byte hex string and convert it to binary
62 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020063static int pem_get_iv( const unsigned char *s, unsigned char *iv,
64 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000065{
Paul Bakker23986e52011-04-24 08:57:21 +000066 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000067
68 memset( iv, 0, iv_len );
69
70 for( i = 0; i < iv_len * 2; i++, s++ )
71 {
72 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
73 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
74 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +000076
77 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
78
79 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
80 }
81
82 return( 0 );
83}
84
Paul Bakker23986e52011-04-24 08:57:21 +000085static void pem_pbkdf1( unsigned char *key, size_t keylen,
Paul Bakker96743fc2011-02-12 14:30:57 +000086 unsigned char *iv,
Paul Bakker23986e52011-04-24 08:57:21 +000087 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000090 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000091 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +000092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020094
Paul Bakker96743fc2011-02-12 14:30:57 +000095 /*
96 * key[ 0..15] = MD5(pwd || IV)
97 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_md5_starts( &md5_ctx );
99 mbedtls_md5_update( &md5_ctx, pwd, pwdlen );
100 mbedtls_md5_update( &md5_ctx, iv, 8 );
101 mbedtls_md5_finish( &md5_ctx, md5sum );
Paul Bakker96743fc2011-02-12 14:30:57 +0000102
103 if( keylen <= 16 )
104 {
105 memcpy( key, md5sum, keylen );
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_md5_free( &md5_ctx );
108 mbedtls_zeroize( md5sum, 16 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000109 return;
110 }
111
112 memcpy( key, md5sum, 16 );
113
114 /*
115 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_md5_starts( &md5_ctx );
118 mbedtls_md5_update( &md5_ctx, md5sum, 16 );
119 mbedtls_md5_update( &md5_ctx, pwd, pwdlen );
120 mbedtls_md5_update( &md5_ctx, iv, 8 );
121 mbedtls_md5_finish( &md5_ctx, md5sum );
Paul Bakker96743fc2011-02-12 14:30:57 +0000122
123 use_len = 16;
124 if( keylen < 32 )
125 use_len = keylen - 16;
126
127 memcpy( key + 16, md5sum, use_len );
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_md5_free( &md5_ctx );
130 mbedtls_zeroize( md5sum, 16 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000131}
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000134/*
135 * Decrypt with DES-CBC, using PBKDF1 for key derivation
136 */
Andres AG705cc652017-02-22 16:23:26 +0000137static int pem_des_decrypt( unsigned char des_iv[8],
138 unsigned char *buf, size_t buflen,
139 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000142 unsigned char des_key[8];
Andres AG705cc652017-02-22 16:23:26 +0000143 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200146
Paul Bakker96743fc2011-02-12 14:30:57 +0000147 pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen );
148
Andres AG705cc652017-02-22 16:23:26 +0000149 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
150 goto exit;
151 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000152 des_iv, buf, buf );
153
Andres AG705cc652017-02-22 16:23:26 +0000154exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_des_free( &des_ctx );
156 mbedtls_zeroize( des_key, 8 );
Andres AG705cc652017-02-22 16:23:26 +0000157
158 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000159}
160
161/*
162 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
163 */
Andres AG705cc652017-02-22 16:23:26 +0000164static int pem_des3_decrypt( unsigned char des3_iv[8],
165 unsigned char *buf, size_t buflen,
166 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000169 unsigned char des3_key[24];
Andres AG705cc652017-02-22 16:23:26 +0000170 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200173
Paul Bakker96743fc2011-02-12 14:30:57 +0000174 pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen );
175
Andres AG705cc652017-02-22 16:23:26 +0000176 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
177 goto exit;
178 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000179 des3_iv, buf, buf );
180
Andres AG705cc652017-02-22 16:23:26 +0000181exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_des3_free( &des3_ctx );
183 mbedtls_zeroize( des3_key, 24 );
Andres AG705cc652017-02-22 16:23:26 +0000184
185 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000190/*
191 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
192 */
Andres AG705cc652017-02-22 16:23:26 +0000193static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
194 unsigned char *buf, size_t buflen,
195 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000198 unsigned char aes_key[32];
Andres AG705cc652017-02-22 16:23:26 +0000199 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200202
Paul Bakker96743fc2011-02-12 14:30:57 +0000203 pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen );
204
Andres AG705cc652017-02-22 16:23:26 +0000205 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
206 goto exit;
207 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000208 aes_iv, buf, buf );
209
Andres AG705cc652017-02-22 16:23:26 +0000210exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_aes_free( &aes_ctx );
212 mbedtls_zeroize( aes_key, keylen );
Andres AG705cc652017-02-22 16:23:26 +0000213
214 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000215}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
219 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200222 const unsigned char *data, const unsigned char *pwd,
223 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000224{
Paul Bakker23986e52011-04-24 08:57:21 +0000225 int ret, enc;
226 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000227 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200228 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
230 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000231 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000233#else
234 ((void) pwd);
235 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
237 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000238
239 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000241
Paul Bakker3c2122f2013-06-24 19:03:14 +0200242 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000243
244 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000246
Paul Bakker3c2122f2013-06-24 19:03:14 +0200247 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000248
249 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000251
252 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200253 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000254 if( *s1 == '\r' ) s1++;
255 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200257
258 end = s2;
259 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200260 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200261 if( *end == '\r' ) end++;
262 if( *end == '\n' ) end++;
263 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000264
265 enc = 0;
266
Andres AGc6559722016-10-24 11:23:36 +0100267 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
270 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000271 enc++;
272
273 s1 += 22;
274 if( *s1 == '\r' ) s1++;
275 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000277
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_DES_C)
Andres AGc6559722016-10-24 11:23:36 +0100280 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000283
284 s1 += 23;
Andres AGc6559722016-10-24 11:23:36 +0100285 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000287
288 s1 += 16;
289 }
Andres AGc6559722016-10-24 11:23:36 +0100290 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000293
294 s1 += 18;
Andres AGc6559722016-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 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_AES_C)
Andres AGc6559722016-10-24 11:23:36 +0100303 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000304 {
Andres AGc6559722016-10-24 11:23:36 +0100305 if( s2 - s1 < 22 )
306 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
307 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000309 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000311 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000313 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000315
316 s1 += 22;
Andres AGc6559722016-10-24 11:23:36 +0100317 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000319
320 s1 += 32;
321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( enc_alg == MBEDTLS_CIPHER_NONE )
325 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000326
327 if( *s1 == '\r' ) s1++;
328 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000330#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
332#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
333 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000334 }
335
Andres AGc6559722016-10-24 11:23:36 +0100336 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100337 return( MBEDTLS_ERR_PEM_INVALID_DATA );
338
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100339 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
342 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000343
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200344 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200345 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000346
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100347 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000348 {
Andres Amaya Garcia246b1632017-07-07 10:46:51 +0100349 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_free( buf );
351 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000352 }
Paul Bakkercff68422013-09-15 20:43:33 +0200353
Paul Bakker96743fc2011-02-12 14:30:57 +0000354 if( enc != 0 )
355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
357 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000358 if( pwd == NULL )
359 {
Andres Amaya Garcia4f02a7b2017-06-26 11:44:54 +0100360 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_free( buf );
362 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000363 }
364
Andres AG705cc652017-02-22 16:23:26 +0000365 ret = 0;
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_DES_C)
368 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG705cc652017-02-22 16:23:26 +0000369 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG705cc652017-02-22 16:23:26 +0000371 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#if defined(MBEDTLS_AES_C)
375 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG705cc652017-02-22 16:23:26 +0000376 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG705cc652017-02-22 16:23:26 +0000378 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG705cc652017-02-22 16:23:26 +0000380 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000382
Andres AG705cc652017-02-22 16:23:26 +0000383 if( ret != 0 )
384 {
385 mbedtls_free( buf );
386 return( ret );
387 }
388
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200389 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200390 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
391 * length bytes (allow 4 to be sure) in all known use cases.
392 *
393 * Use that as heurisitic to try detecting password mismatchs.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200394 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200395 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000396 {
Andres Amaya Garcia4f02a7b2017-06-26 11:44:54 +0100397 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_free( buf );
399 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000400 }
401#else
Andres Amaya Garcia4f02a7b2017-06-26 11:44:54 +0100402 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_free( buf );
404 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
405#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
406 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000407 }
408
409 ctx->buf = buf;
410 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000411
412 return( 0 );
413}
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200416{
Ron Eldor070c8092017-09-06 17:09:41 +0300417 if( ctx->buf != NULL )
Ron Eldor82a4b812017-09-05 17:17:31 +0300418 mbedtls_zeroize( ctx->buf, ctx->buflen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_free( ctx->buf );
420 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_PEM_WRITE_C)
427int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200428 const unsigned char *der_data, size_t der_len,
429 unsigned char *buf, size_t buf_len, size_t *olen )
430{
431 int ret;
Andres AG8ad5acd2017-01-30 14:34:25 +0000432 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100433 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200434
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100435 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200436 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
437
Paul Bakker77e23fb2013-09-15 20:03:26 +0200438 if( use_len + add_len > buf_len )
439 {
440 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200442 }
443
Andres Amaya Garcia5dc2fe72017-07-06 10:06:58 +0100444 if( use_len != 0 &&
445 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200446 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200447
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100448 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200449 der_len ) ) != 0 )
450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200452 return( ret );
453 }
454
455 memcpy( p, header, strlen( header ) );
456 p += strlen( header );
457 c = encode_buf;
458
459 while( use_len )
460 {
461 len = ( use_len > 64 ) ? 64 : use_len;
462 memcpy( p, c, len );
463 use_len -= len;
464 p += len;
465 c += len;
466 *p++ = '\n';
467 }
468
469 memcpy( p, footer, strlen( footer ) );
470 p += strlen( footer );
471
472 *p++ = '\0';
473 *olen = p - buf;
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200476 return( 0 );
477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_PEM_WRITE_C */
479#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */