blob: 3bf4ca5b8c9f640d288398b48b212f1a508b7dd8 [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
Bence Szépkútif744bd72020-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útif744bd72020-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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker96743fc2011-02-12 14:30:57 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker96743fc2011-02-12 14:30:57 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
Rich Evansce2f2372015-02-06 13:57:42 +000056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/pem.h"
58#include "mbedtls/base64.h"
59#include "mbedtls/des.h"
60#include "mbedtls/aes.h"
61#include "mbedtls/md5.h"
62#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050063#include "mbedtls/platform_util.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000064
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <string.h>
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020069#else
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020071#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020073#endif
74
Andres AGc0db5112016-12-07 15:05:53 +000075#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000079}
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
82 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000083/*
84 * Read a 16-byte hex string and convert it to binary
85 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020086static int pem_get_iv( const unsigned char *s, unsigned char *iv,
87 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000088{
Paul Bakker23986e52011-04-24 08:57:21 +000089 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000090
91 memset( iv, 0, iv_len );
92
93 for( i = 0; i < iv_len * 2; i++, s++ )
94 {
95 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
96 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
97 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +000099
100 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
101
102 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
103 }
104
105 return( 0 );
106}
107
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100108static int pem_pbkdf1( unsigned char *key, size_t keylen,
109 unsigned char *iv,
110 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000113 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +0000114 size_t use_len;
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100115 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200118
Paul Bakker96743fc2011-02-12 14:30:57 +0000119 /*
120 * key[ 0..15] = MD5(pwd || IV)
121 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100122 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100123 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100124 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 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, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100127 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100128 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100129 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000130
131 if( keylen <= 16 )
132 {
133 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100134 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000135 }
136
137 memcpy( key, md5sum, 16 );
138
139 /*
140 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
141 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100142 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100143 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100144 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 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, pwd, pwdlen ) ) != 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, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100149 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100150 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100151 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000152
153 use_len = 16;
154 if( keylen < 32 )
155 use_len = keylen - 16;
156
157 memcpy( key + 16, md5sum, use_len );
158
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100159exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_md5_free( &md5_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500161 mbedtls_platform_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100162
163 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000164}
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000167/*
168 * Decrypt with DES-CBC, using PBKDF1 for key derivation
169 */
Andres AG51a7ae12017-02-22 16:23:26 +0000170static int pem_des_decrypt( unsigned char des_iv[8],
171 unsigned char *buf, size_t buflen,
172 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000175 unsigned char des_key[8];
Andres AG51a7ae12017-02-22 16:23:26 +0000176 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200179
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100180 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
181 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000182
Andres AG51a7ae12017-02-22 16:23:26 +0000183 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
184 goto exit;
185 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000186 des_iv, buf, buf );
187
Andres AG51a7ae12017-02-22 16:23:26 +0000188exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_des_free( &des_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500190 mbedtls_platform_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000191
192 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000193}
194
195/*
196 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
197 */
Andres AG51a7ae12017-02-22 16:23:26 +0000198static int pem_des3_decrypt( unsigned char des3_iv[8],
199 unsigned char *buf, size_t buflen,
200 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000203 unsigned char des3_key[24];
Andres AG51a7ae12017-02-22 16:23:26 +0000204 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200207
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100208 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
209 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000210
Andres AG51a7ae12017-02-22 16:23:26 +0000211 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
212 goto exit;
213 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000214 des3_iv, buf, buf );
215
Andres AG51a7ae12017-02-22 16:23:26 +0000216exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_des3_free( &des3_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500218 mbedtls_platform_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000219
220 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000225/*
226 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
227 */
Andres AG51a7ae12017-02-22 16:23:26 +0000228static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
229 unsigned char *buf, size_t buflen,
230 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000233 unsigned char aes_key[32];
Andres AG51a7ae12017-02-22 16:23:26 +0000234 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200237
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100238 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
239 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000240
Andres AG51a7ae12017-02-22 16:23:26 +0000241 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
242 goto exit;
243 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000244 aes_iv, buf, buf );
245
Andres AG51a7ae12017-02-22 16:23:26 +0000246exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_aes_free( &aes_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500248 mbedtls_platform_zeroize( aes_key, keylen );
Andres AG51a7ae12017-02-22 16:23:26 +0000249
250 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000251}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
255 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258 const unsigned char *data, const unsigned char *pwd,
259 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000260{
Paul Bakker23986e52011-04-24 08:57:21 +0000261 int ret, enc;
262 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000263 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200264 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
266 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000267 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000269#else
270 ((void) pwd);
271 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
273 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000274
275 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000277
Paul Bakker3c2122f2013-06-24 19:03:14 +0200278 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000279
280 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000282
Paul Bakker3c2122f2013-06-24 19:03:14 +0200283 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000284
285 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000287
288 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200289 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000290 if( *s1 == '\r' ) s1++;
291 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200293
294 end = s2;
295 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200296 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200297 if( *end == '\r' ) end++;
298 if( *end == '\n' ) end++;
299 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000300
301 enc = 0;
302
Andres AG703990b2016-10-24 11:23:36 +0100303 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
306 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000307 enc++;
308
309 s1 += 22;
310 if( *s1 == '\r' ) s1++;
311 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000313
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100316 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000319
320 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100321 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000323
324 s1 += 16;
325 }
Andres AG703990b2016-10-24 11:23:36 +0100326 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000329
330 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100331 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000333
334 s1 += 16;
335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100339 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000340 {
Andres AG703990b2016-10-24 11:23:36 +0100341 if( s2 - s1 < 22 )
342 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
343 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000345 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000347 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000349 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000351
352 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100353 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000355
356 s1 += 32;
357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( enc_alg == MBEDTLS_CIPHER_NONE )
361 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000362
363 if( *s1 == '\r' ) s1++;
364 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000366#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
368#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
369 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000370 }
371
Andres AG703990b2016-10-24 11:23:36 +0100372 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100373 return( MBEDTLS_ERR_PEM_INVALID_DATA );
374
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100375 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
378 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000379
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200380 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200381 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000382
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100383 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000384 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500385 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_free( buf );
387 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000388 }
Paul Bakkercff68422013-09-15 20:43:33 +0200389
Paul Bakker96743fc2011-02-12 14:30:57 +0000390 if( enc != 0 )
391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
393 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000394 if( pwd == NULL )
395 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500396 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_free( buf );
398 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000399 }
400
Andres AG51a7ae12017-02-22 16:23:26 +0000401 ret = 0;
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_DES_C)
404 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000405 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000407 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_AES_C)
411 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000412 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000414 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000416 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000418
Andres AG51a7ae12017-02-22 16:23:26 +0000419 if( ret != 0 )
420 {
421 mbedtls_free( buf );
422 return( ret );
423 }
424
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200425 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200426 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
427 * length bytes (allow 4 to be sure) in all known use cases.
428 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000429 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200430 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200431 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000432 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500433 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_free( buf );
435 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000436 }
437#else
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500438 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_free( buf );
440 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
441#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
442 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000443 }
444
445 ctx->buf = buf;
446 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000447
448 return( 0 );
449}
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200452{
irwir2239a862018-06-12 18:25:09 +0300453 if ( ctx->buf != NULL )
454 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500455 mbedtls_platform_zeroize( ctx->buf, ctx->buflen );
irwir2239a862018-06-12 18:25:09 +0300456 mbedtls_free( ctx->buf );
457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200459
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500460 mbedtls_platform_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200514 return( 0 );
515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#endif /* MBEDTLS_PEM_WRITE_C */
517#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */