blob: 544f7c41ba304dddc47d2cec55b4cdd3d1af00a8 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
Rich Evansce2f2372015-02-06 13:57:42 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/pem.h"
27#include "mbedtls/base64.h"
28#include "mbedtls/des.h"
29#include "mbedtls/aes.h"
30#include "mbedtls/md5.h"
31#include "mbedtls/cipher.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020039#else
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020041#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020043#endif
44
Andres AGc0db5112016-12-07 15:05:53 +000045#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046void mbedtls_pem_init( mbedtls_pem_context *ctx )
Paul Bakker96743fc2011-02-12 14:30:57 +000047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 memset( ctx, 0, sizeof( mbedtls_pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +000049}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
52 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000053/*
54 * Read a 16-byte hex string and convert it to binary
55 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020056static int pem_get_iv( const unsigned char *s, unsigned char *iv,
57 size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000058{
Paul Bakker23986e52011-04-24 08:57:21 +000059 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000060
61 memset( iv, 0, iv_len );
62
63 for( i = 0; i < iv_len * 2; i++, s++ )
64 {
65 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
66 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
67 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +000069
70 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
71
72 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
73 }
74
75 return( 0 );
76}
77
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010078static int pem_pbkdf1( unsigned char *key, size_t keylen,
79 unsigned char *iv,
80 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_md5_context md5_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +000083 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000084 size_t use_len;
Janos Follath24eed8d2019-11-22 13:21:35 +000085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_md5_init( &md5_ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020088
Paul Bakker96743fc2011-02-12 14:30:57 +000089 /*
90 * key[ 0..15] = MD5(pwd || IV)
91 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010092 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010093 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010094 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010095 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010096 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010097 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010098 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +010099 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000100
101 if( keylen <= 16 )
102 {
103 memcpy( key, md5sum, keylen );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100104 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000105 }
106
107 memcpy( key, md5sum, 16 );
108
109 /*
110 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
111 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100112 if( ( ret = mbedtls_md5_starts_ret( &md5_ctx ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100113 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100114 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, md5sum, 16 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100115 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100116 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, pwd, pwdlen ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100117 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100118 if( ( ret = mbedtls_md5_update_ret( &md5_ctx, iv, 8 ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100119 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100120 if( ( ret = mbedtls_md5_finish_ret( &md5_ctx, md5sum ) ) != 0 )
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100121 goto exit;
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
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100129exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_md5_free( &md5_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500131 mbedtls_platform_zeroize( md5sum, 16 );
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100132
133 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000134}
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_DES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000137/*
138 * Decrypt with DES-CBC, using PBKDF1 for key derivation
139 */
Andres AG51a7ae12017-02-22 16:23:26 +0000140static int pem_des_decrypt( unsigned char des_iv[8],
141 unsigned char *buf, size_t buflen,
142 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_des_context des_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000145 unsigned char des_key[8];
Janos Follath24eed8d2019-11-22 13:21:35 +0000146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_des_init( &des_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200149
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100150 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
151 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000152
Andres AG51a7ae12017-02-22 16:23:26 +0000153 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
154 goto exit;
155 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000156 des_iv, buf, buf );
157
Andres AG51a7ae12017-02-22 16:23:26 +0000158exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_des_free( &des_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500160 mbedtls_platform_zeroize( des_key, 8 );
Andres AG51a7ae12017-02-22 16:23:26 +0000161
162 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000163}
164
165/*
166 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
167 */
Andres AG51a7ae12017-02-22 16:23:26 +0000168static int pem_des3_decrypt( unsigned char des3_iv[8],
169 unsigned char *buf, size_t buflen,
170 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_des3_context des3_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000173 unsigned char des3_key[24];
Janos Follath24eed8d2019-11-22 13:21:35 +0000174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_des3_init( &des3_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200177
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100178 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
179 goto exit;
Paul Bakker96743fc2011-02-12 14:30:57 +0000180
Andres AG51a7ae12017-02-22 16:23:26 +0000181 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
182 goto exit;
183 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000184 des3_iv, buf, buf );
185
Andres AG51a7ae12017-02-22 16:23:26 +0000186exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_des3_free( &des3_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500188 mbedtls_platform_zeroize( des3_key, 24 );
Andres AG51a7ae12017-02-22 16:23:26 +0000189
190 return( ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_AES_C)
Paul Bakker96743fc2011-02-12 14:30:57 +0000195/*
196 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
197 */
Andres AG51a7ae12017-02-22 16:23:26 +0000198static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
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_aes_context aes_ctx;
Paul Bakker96743fc2011-02-12 14:30:57 +0000203 unsigned char aes_key[32];
Janos Follath24eed8d2019-11-22 13:21:35 +0000204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker96743fc2011-02-12 14:30:57 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_aes_init( &aes_ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200207
Andres Amaya Garcia8d08c442017-06-29 11:16:38 +0100208 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_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_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
212 goto exit;
213 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
Paul Bakker96743fc2011-02-12 14:30:57 +0000214 aes_iv, buf, buf );
215
Andres AG51a7ae12017-02-22 16:23:26 +0000216exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_aes_free( &aes_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500218 mbedtls_platform_zeroize( aes_key, keylen );
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_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
225 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200228 const unsigned char *data, const unsigned char *pwd,
229 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000230{
Paul Bakker23986e52011-04-24 08:57:21 +0000231 int ret, enc;
232 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000233 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200234 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
236 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000237 unsigned char pem_iv[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
Paul Bakker96743fc2011-02-12 14:30:57 +0000239#else
240 ((void) pwd);
241 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
243 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000244
245 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000247
Paul Bakker3c2122f2013-06-24 19:03:14 +0200248 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000249
250 if( s1 == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000252
Paul Bakker3c2122f2013-06-24 19:03:14 +0200253 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000254
255 if( s2 == NULL || s2 <= s1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000257
258 s1 += strlen( header );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200259 if( *s1 == ' ' ) s1++;
Paul Bakker96743fc2011-02-12 14:30:57 +0000260 if( *s1 == '\r' ) s1++;
261 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker00b28602013-06-24 13:02:41 +0200263
264 end = s2;
265 end += strlen( footer );
Manuel Pégourié-Gonnard052d10c2015-07-31 11:09:59 +0200266 if( *end == ' ' ) end++;
Paul Bakker00b28602013-06-24 13:02:41 +0200267 if( *end == '\r' ) end++;
268 if( *end == '\n' ) end++;
269 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000270
271 enc = 0;
272
Andres AG703990b2016-10-24 11:23:36 +0100273 if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
276 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000277 enc++;
278
279 s1 += 22;
280 if( *s1 == '\r' ) s1++;
281 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000283
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_DES_C)
Andres AG703990b2016-10-24 11:23:36 +0100286 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000289
290 s1 += 23;
Andres AG703990b2016-10-24 11:23:36 +0100291 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000293
294 s1 += 16;
295 }
Andres AG703990b2016-10-24 11:23:36 +0100296 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 enc_alg = MBEDTLS_CIPHER_DES_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000299
300 s1 += 18;
Andres AG703990b2016-10-24 11:23:36 +0100301 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000303
304 s1 += 16;
305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_AES_C)
Andres AG703990b2016-10-24 11:23:36 +0100309 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000310 {
Andres AG703990b2016-10-24 11:23:36 +0100311 if( s2 - s1 < 22 )
312 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
313 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000315 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000317 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
Paul Bakker96743fc2011-02-12 14:30:57 +0000319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000321
322 s1 += 22;
Andres AG703990b2016-10-24 11:23:36 +0100323 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
Paul Bakker96743fc2011-02-12 14:30:57 +0000325
326 s1 += 32;
327 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* MBEDTLS_AES_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( enc_alg == MBEDTLS_CIPHER_NONE )
331 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
Paul Bakker96743fc2011-02-12 14:30:57 +0000332
333 if( *s1 == '\r' ) s1++;
334 if( *s1 == '\n' ) s1++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000336#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
338#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
339 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000340 }
341
Andres AG703990b2016-10-24 11:23:36 +0100342 if( s1 >= s2 )
Simon Butchera45aa132015-10-05 00:26:36 +0100343 return( MBEDTLS_ERR_PEM_INVALID_DATA );
344
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100345 ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
Paul Bakker96743fc2011-02-12 14:30:57 +0000346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
348 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000349
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200350 if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200351 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000352
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100353 if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000354 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500355 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_free( buf );
357 return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000358 }
Paul Bakkercff68422013-09-15 20:43:33 +0200359
Paul Bakker96743fc2011-02-12 14:30:57 +0000360 if( enc != 0 )
361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
363 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000364 if( pwd == NULL )
365 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500366 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_free( buf );
368 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
Paul Bakker96743fc2011-02-12 14:30:57 +0000369 }
370
Andres AG51a7ae12017-02-22 16:23:26 +0000371 ret = 0;
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_DES_C)
374 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000375 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000377 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#endif /* MBEDTLS_DES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_AES_C)
381 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000382 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000384 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
Andres AG51a7ae12017-02-22 16:23:26 +0000386 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_AES_C */
Paul Bakker96743fc2011-02-12 14:30:57 +0000388
Andres AG51a7ae12017-02-22 16:23:26 +0000389 if( ret != 0 )
390 {
391 mbedtls_free( buf );
392 return( ret );
393 }
394
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200395 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200396 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
397 * length bytes (allow 4 to be sure) in all known use cases.
398 *
ILUXONCHIK060fe372018-02-25 20:59:09 +0000399 * Use that as a heuristic to try to detect password mismatches.
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200400 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200401 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000402 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500403 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_free( buf );
405 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
Paul Bakker96743fc2011-02-12 14:30:57 +0000406 }
407#else
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500408 mbedtls_platform_zeroize( buf, len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_free( buf );
410 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
411#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
412 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000413 }
414
415 ctx->buf = buf;
416 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000417
418 return( 0 );
419}
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421void mbedtls_pem_free( mbedtls_pem_context *ctx )
Paul Bakkercff68422013-09-15 20:43:33 +0200422{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 if ( ctx->buf != NULL )
424 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500425 mbedtls_platform_zeroize( ctx->buf, ctx->buflen );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426 mbedtls_free( ctx->buf );
427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_free( ctx->info );
Paul Bakkercff68422013-09-15 20:43:33 +0200429
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500430 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) );
Paul Bakkercff68422013-09-15 20:43:33 +0200431}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakkercff68422013-09-15 20:43:33 +0200433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#if defined(MBEDTLS_PEM_WRITE_C)
435int mbedtls_pem_write_buffer( const char *header, const char *footer,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200436 const unsigned char *der_data, size_t der_len,
437 unsigned char *buf, size_t buf_len, size_t *olen )
438{
Janos Follath24eed8d2019-11-22 13:21:35 +0000439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres AG9cf1f962017-01-30 14:34:25 +0000440 unsigned char *encode_buf = NULL, *c, *p = buf;
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100441 size_t len = 0, use_len, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200442
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100443 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200444 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
445
Paul Bakker77e23fb2013-09-15 20:03:26 +0200446 if( use_len + add_len > buf_len )
447 {
448 *olen = use_len + add_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200450 }
451
Andres Amaya Garciaf1ee6352017-07-06 10:06:58 +0100452 if( use_len != 0 &&
453 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200454 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200455
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100456 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200457 der_len ) ) != 0 )
458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200460 return( ret );
461 }
462
463 memcpy( p, header, strlen( header ) );
464 p += strlen( header );
465 c = encode_buf;
466
467 while( use_len )
468 {
469 len = ( use_len > 64 ) ? 64 : use_len;
470 memcpy( p, c, len );
471 use_len -= len;
472 p += len;
473 c += len;
474 *p++ = '\n';
475 }
476
477 memcpy( p, footer, strlen( footer ) );
478 p += strlen( footer );
479
480 *p++ = '\0';
481 *olen = p - buf;
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_free( encode_buf );
Paul Bakker77e23fb2013-09-15 20:03:26 +0200484 return( 0 );
485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#endif /* MBEDTLS_PEM_WRITE_C */
487#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */