blob: 6ff4b01a4db1c991cf81efa05c989e03a873b338 [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úti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker96743fc2011-02-12 14:30:57 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
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"
Paul Bakker96743fc2011-02-12 14:30:57 +000063
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <string.h>
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020068#else
Rich Evans00ab4702015-02-06 13:43:58 +000069#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020070#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020072#endif
73
Andres AGc0db5112016-12-07 15:05:53 +000074#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker34617722014-06-13 17:20:13 +020075/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020077 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
78}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000083}
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
86 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000087/*
88 * Read a 16-byte hex string and convert it to binary
89 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020090static int pem_get_iv( const unsigned char *s, unsigned char *iv,
91 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000092{
Paul Bakker23986e52011-04-24 08:57:21 +000093 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000094
95 memset( iv, 0, iv_len );
96
97 for( i = 0; i < iv_len * 2; i++, s++ )
98 {
99 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
100 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
101 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000103
104 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
105
106 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
107 }
108
109 return( 0 );
110}
111
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100112static int pem_pbkdf1( unsigned char *key, size_t keylen,
113 unsigned char *iv,
114 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000117 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +0000118 size_t use_len;
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100119 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200122
Paul Bakker96743fc2011-02-12 14:30:57 +0000123 /*
124 * key[ 0..15] = MD5(pwd || IV)
125 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100126 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100127 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100128 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100129 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100130 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100131 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100132 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100133 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000134
135 if( keylen <= 16 )
136 {
137 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100138 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000139 }
140
141 memcpy( key, md5sum, 16 );
142
143 /*
144 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
145 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100146 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 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, md5sum, 16 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100149 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100150 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100151 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100152 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100153 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100154 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100155 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000156
157 use_len = 16;
158 if( keylen < 32 )
159 use_len = keylen - 16;
160
161 memcpy( key + 16, md5sum, use_len );
162
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100163exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_md5_free( &md5_ctx );
165 mbedtls_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100166
167 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000168}
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000171/*
172 * Decrypt with DES-CBC, using PBKDF1 for key derivation
173 */
Andres AG51a7ae12017-02-22 16:23:26 +0000174static int pem_des_decrypt( unsigned char des_iv[8],
175 unsigned char *buf, size_t buflen,
176 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000179 unsigned char des_key[8];
Andres AG51a7ae12017-02-22 16:23:26 +0000180 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200183
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100184 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
185 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000186
Andres AG51a7ae12017-02-22 16:23:26 +0000187 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
188 goto exit;
189 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000190 des_iv, buf, buf );
191
Andres AG51a7ae12017-02-22 16:23:26 +0000192exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_des_free( &des_ctx );
194 mbedtls_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000195
196 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000197}
198
199/*
200 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
201 */
Andres AG51a7ae12017-02-22 16:23:26 +0000202static int pem_des3_decrypt( unsigned char des3_iv[8],
203 unsigned char *buf, size_t buflen,
204 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000207 unsigned char des3_key[24];
Andres AG51a7ae12017-02-22 16:23:26 +0000208 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200211
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100212 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
213 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000214
Andres AG51a7ae12017-02-22 16:23:26 +0000215 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
216 goto exit;
217 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000218 des3_iv, buf, buf );
219
Andres AG51a7ae12017-02-22 16:23:26 +0000220exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_des3_free( &des3_ctx );
222 mbedtls_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000223
224 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000229/*
230 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
231 */
Andres AG51a7ae12017-02-22 16:23:26 +0000232static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
233 unsigned char *buf, size_t buflen,
234 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000237 unsigned char aes_key[32];
Andres AG51a7ae12017-02-22 16:23:26 +0000238 int ret;
Paul Bakker96743fc2011-02-12 14:30:57 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200241
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100242 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
243 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000244
Andres AG51a7ae12017-02-22 16:23:26 +0000245 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
246 goto exit;
247 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000248 aes_iv, buf, buf );
249
Andres AG51a7ae12017-02-22 16:23:26 +0000250exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_aes_free( &aes_ctx );
252 mbedtls_zeroize( aes_key, keylen );
Andres AG51a7ae12017-02-22 16:23:26 +0000253
254 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
259 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200262 const unsigned char *data, const unsigned char *pwd,
263 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000264{
Paul Bakker23986e52011-04-24 08:57:21 +0000265 int ret, enc;
266 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000267 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200268 const unsigned char *s1, *s2, *end;
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 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000273#else
274 ((void) pwd);
275 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
277 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000278
279 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000281
Paul Bakker3c2122f2013-06-24 19:03:14 +0200282 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000283
284 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000286
Paul Bakker3c2122f2013-06-24 19:03:14 +0200287 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000288
289 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000291
292 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200293 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000294 if( *s1 == '\r' ) s1++;
295 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200297
298 end = s2;
299 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200300 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200301 if( *end == '\r' ) end++;
302 if( *end == '\n' ) end++;
303 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000304
305 enc = 0;
306
Andres AG703990b2016-10-24 11:23:36 +0100307 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
310 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000311 enc++;
312
313 s1 += 22;
314 if( *s1 == '\r' ) s1++;
315 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000317
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100320 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000323
324 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100325 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000327
328 s1 += 16;
329 }
Andres AG703990b2016-10-24 11:23:36 +0100330 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000333
334 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100335 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000337
338 s1 += 16;
339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100343 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000344 {
Andres AG703990b2016-10-24 11:23:36 +0100345 if( s2 - s1 < 22 )
346 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
347 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000349 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000351 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000355
356 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100357 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000359
360 s1 += 32;
361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( enc_alg == MBEDTLS_CIPHER_NONE )
365 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000366
367 if( *s1 == '\r' ) s1++;
368 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000370#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
372#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
373 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000374 }
375
Andres AG703990b2016-10-24 11:23:36 +0100376 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100377 return( MBEDTLS_ERR_PEM_INVALID_DATA );
378
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100379 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
382 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000383
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200384 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200385 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000386
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100387 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000388 {
Andres Amaya Garcia92d46f02017-07-07 10:46:51 +0100389 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_free( buf );
391 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000392 }
Paul Bakkercff68422013-09-15 20:43:33 +0200393
Paul Bakker96743fc2011-02-12 14:30:57 +0000394 if( enc != 0 )
395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
397 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000398 if( pwd == NULL )
399 {
Andres Amaya Garcia03d70502017-06-26 11:44:54 +0100400 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_free( buf );
402 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000403 }
404
Andres AG51a7ae12017-02-22 16:23:26 +0000405 ret = 0;
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_DES_C)
408 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000409 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000411 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#if defined(MBEDTLS_AES_C)
415 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000416 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000418 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000420 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000422
Andres AG51a7ae12017-02-22 16:23:26 +0000423 if( ret != 0 )
424 {
425 mbedtls_free( buf );
426 return( ret );
427 }
428
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200429 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200430 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
431 * length bytes (allow 4 to be sure) in all known use cases.
432 *
433 * Use that as heurisitic to try detecting password mismatchs.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200434 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200435 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000436 {
Andres Amaya Garcia03d70502017-06-26 11:44:54 +0100437 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_free( buf );
439 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000440 }
441#else
Andres Amaya Garcia03d70502017-06-26 11:44:54 +0100442 mbedtls_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_free( buf );
444 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
445#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
446 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000447 }
448
449 ctx->buf = buf;
450 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000451
452 return( 0 );
453}
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200456{
Ron Eldor65112b12017-09-06 17:09:41 +0300457 if( ctx->buf != NULL )
Ron Eldor9d84b4c2017-09-05 17:17:31 +0300458 mbedtls_zeroize( ctx->buf, ctx->buflen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_free( ctx->buf );
460 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_PEM_WRITE_C)
467int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200468 const unsigned char *der_data, size_t der_len,
469 unsigned char *buf, size_t buf_len, size_t *olen )
470{
471 int ret;
Andres AG9cf1f962017-01-30 14:34:25 +0000472 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100473 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200474
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100475 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200476 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
477
Paul Bakker77e23fb2013-09-15 20:03:26 +0200478 if( use_len + add_len > buf_len )
479 {
480 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200482 }
483
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100484 if( use_len != 0 &&
485 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200486 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200487
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100488 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200489 der_len ) ) != 0 )
490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200492 return( ret );
493 }
494
495 memcpy( p, header, strlen( header ) );
496 p += strlen( header );
497 c = encode_buf;
498
499 while( use_len )
500 {
501 len = ( use_len > 64 ) ? 64 : use_len;
502 memcpy( p, c, len );
503 use_len -= len;
504 p += len;
505 c += len;
506 *p++ = '\n';
507 }
508
509 memcpy( p, footer, strlen( footer ) );
510 p += strlen( footer );
511
512 *p++ = '\0';
513 *olen = p - buf;
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200516 return( 0 );
517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_PEM_WRITE_C */
519#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */