blob: dea6f996232aa885db59d4d046ad88d5b3f2a85f [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 AGc0db5112016-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
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010085static int pem_pbkdf1( unsigned char *key, size_t keylen,
86 unsigned char *iv,
87 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;
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010092 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020095
Paul Bakker96743fc2011-02-12 14:30:57 +000096 /*
97 * key[ 0..15] = MD5(pwd || IV)
98 */
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010099 if( ( ret = mbedtls_md5_starts_ext( &md5_ctx ) ) != 0 )
100 goto exit;
101 if( ( ret = mbedtls_md5_update_ext( &md5_ctx, pwd, pwdlen ) ) != 0 )
102 goto exit;
103 if( ( ret = mbedtls_md5_update_ext( &md5_ctx, iv, 8 ) ) != 0 )
104 goto exit;
105 if( ( ret = mbedtls_md5_finish_ext( &md5_ctx, md5sum ) ) != 0 )
106 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000107
108 if( keylen <= 16 )
109 {
110 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100111 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000112 }
113
114 memcpy( key, md5sum, 16 );
115
116 /*
117 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
118 */
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100119 if( ( ret = mbedtls_md5_starts_ext( &md5_ctx ) ) != 0 )
120 goto exit;
121 if( ( ret = mbedtls_md5_update_ext( &md5_ctx, md5sum, 16 ) ) != 0 )
122 goto exit;
123 if( ( ret = mbedtls_md5_update_ext( &md5_ctx, pwd, pwdlen ) ) != 0 )
124 goto exit;
125 if( ( ret = mbedtls_md5_update_ext( &md5_ctx, iv, 8 ) ) != 0 )
126 goto exit;
127 if( ( ret = mbedtls_md5_finish_ext( &md5_ctx, md5sum ) ) != 0 )
128 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000129
130 use_len = 16;
131 if( keylen < 32 )
132 use_len = keylen - 16;
133
134 memcpy( key + 16, md5sum, use_len );
135
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100136exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_md5_free( &md5_ctx );
138 mbedtls_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100139
140 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000144/*
145 * Decrypt with DES-CBC, using PBKDF1 for key derivation
146 */
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100147static int pem_des_decrypt( unsigned char des_iv[8],
148 unsigned char *buf, size_t buflen,
149 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000152 unsigned char des_key[8];
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100153 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200156
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100157 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
158 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000159
Andres AG51a7ae12017-02-22 16:23:26 +0000160 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
161 goto exit;
162 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000163 des_iv, buf, buf );
164
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100165exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_des_free( &des_ctx );
167 mbedtls_zeroize( des_key, 8 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100168
169 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000170}
171
172/*
173 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
174 */
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100175static int pem_des3_decrypt( unsigned char des3_iv[8],
176 unsigned char *buf, size_t buflen,
177 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000180 unsigned char des3_key[24];
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100181 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200184
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100185 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
186 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000187
Andres AG51a7ae12017-02-22 16:23:26 +0000188 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
189 goto exit;
190 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000191 des3_iv, buf, buf );
192
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100193exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_des3_free( &des3_ctx );
195 mbedtls_zeroize( des3_key, 24 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100196
197 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000202/*
203 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
204 */
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100205static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
206 unsigned char *buf, size_t buflen,
207 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000210 unsigned char aes_key[32];
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100211 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200214
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100215 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
216 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000217
Andres AG51a7ae12017-02-22 16:23:26 +0000218 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
219 goto exit;
220 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000221 aes_iv, buf, buf );
222
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100223exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_aes_free( &aes_ctx );
225 mbedtls_zeroize( aes_key, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100226
227 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
232 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200235 const unsigned char *data, const unsigned char *pwd,
236 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000237{
Paul Bakker23986e52011-04-24 08:57:21 +0000238 int ret, enc;
239 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000240 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200241 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
243 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000244 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000246#else
247 ((void) pwd);
248 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
250 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000251
252 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000254
Paul Bakker3c2122f2013-06-24 19:03:14 +0200255 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000256
257 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000259
Paul Bakker3c2122f2013-06-24 19:03:14 +0200260 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000261
262 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000264
265 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200266 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000267 if( *s1 == '\r' ) s1++;
268 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200270
271 end = s2;
272 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200273 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200274 if( *end == '\r' ) end++;
275 if( *end == '\n' ) end++;
276 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000277
278 enc = 0;
279
Andres AG703990b2016-10-24 11:23:36 +0100280 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
283 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000284 enc++;
285
286 s1 += 22;
287 if( *s1 == '\r' ) s1++;
288 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000290
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100293 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000296
297 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100298 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000300
301 s1 += 16;
302 }
Andres AG703990b2016-10-24 11:23:36 +0100303 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000306
307 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100308 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000310
311 s1 += 16;
312 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100316 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000317 {
Andres AG703990b2016-10-24 11:23:36 +0100318 if( s2 - s1 < 22 )
319 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
320 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000322 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000324 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000326 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000328
329 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100330 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000332
333 s1 += 32;
334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( enc_alg == MBEDTLS_CIPHER_NONE )
338 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000339
340 if( *s1 == '\r' ) s1++;
341 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000343#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
345#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
346 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000347 }
348
Andres AG703990b2016-10-24 11:23:36 +0100349 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100350 return( MBEDTLS_ERR_PEM_INVALID_DATA );
351
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100352 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
355 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000356
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200357 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200358 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000359
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100360 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_free( buf );
363 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000364 }
Paul Bakkercff68422013-09-15 20:43:33 +0200365
Paul Bakker96743fc2011-02-12 14:30:57 +0000366 if( enc != 0 )
367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
369 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000370 if( pwd == NULL )
371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_free( buf );
373 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000374 }
375
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100376 ret = 0;
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_DES_C)
379 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100380 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100382 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_AES_C)
386 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100387 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100389 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100391 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000393
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100394 if( ret != 0 )
395 {
396 mbedtls_free( buf );
397 return( ret );
398 }
399
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200400 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200401 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
402 * length bytes (allow 4 to be sure) in all known use cases.
403 *
404 * Use that as heurisitic to try detecting password mismatchs.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200405 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200406 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000407 {
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
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_free( ctx->buf );
427 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_PEM_WRITE_C)
434int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200435 const unsigned char *der_data, size_t der_len,
436 unsigned char *buf, size_t buf_len, size_t *olen )
437{
438 int ret;
439 unsigned char *encode_buf, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100440 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200441
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100442 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200443 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
444
Paul Bakker77e23fb2013-09-15 20:03:26 +0200445 if( use_len + add_len > buf_len )
446 {
447 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200449 }
450
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200451 if( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200452 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200453
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100454 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200455 der_len ) ) != 0 )
456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200458 return( ret );
459 }
460
461 memcpy( p, header, strlen( header ) );
462 p += strlen( header );
463 c = encode_buf;
464
465 while( use_len )
466 {
467 len = ( use_len > 64 ) ? 64 : use_len;
468 memcpy( p, c, len );
469 use_len -= len;
470 p += len;
471 c += len;
472 *p++ = '\n';
473 }
474
475 memcpy( p, footer, strlen( footer ) );
476 p += strlen( footer );
477
478 *p++ = '\0';
479 *olen = p - buf;
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200482 return( 0 );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_PEM_WRITE_C */
485#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */