Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Privacy Enhanced Mail (PEM) decoding |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C) |
Rich Evans | ce2f237 | 2015-02-06 13:57:42 +0000 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/pem.h" |
| 25 | #include "mbedtls/base64.h" |
| 26 | #include "mbedtls/des.h" |
| 27 | #include "mbedtls/aes.h" |
| 28 | #include "mbedtls/md5.h" |
| 29 | #include "mbedtls/cipher.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 30 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 31 | #include "mbedtls/error.h" |
Przemek Stekiel | bc3906c | 2022-08-19 09:16:36 +0200 | [diff] [blame] | 32 | #include "hash_info.h" |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 33 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 38 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 40 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #define mbedtls_free free |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 45 | #include "psa/crypto.h" |
| 46 | #endif |
| 47 | |
Przemek Stekiel | 829e97d | 2022-08-09 14:58:35 +0200 | [diff] [blame] | 48 | #include "legacy_or_psa.h" |
| 49 | |
Manuel Pégourié-Gonnard | 1dc3725 | 2022-09-15 11:10:26 +0200 | [diff] [blame^] | 50 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \ |
| 51 | defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Przemek Stekiel | 4092ff9 | 2022-08-11 08:49:21 +0200 | [diff] [blame] | 52 | ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) ) |
| 53 | #define PEM_RFC1421 |
Manuel Pégourié-Gonnard | 1dc3725 | 2022-09-15 11:10:26 +0200 | [diff] [blame^] | 54 | #endif /* MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA && |
| 55 | MBEDTLS_CIPHER_MODE_CBC && |
Przemek Stekiel | 4092ff9 | 2022-08-11 08:49:21 +0200 | [diff] [blame] | 56 | ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ |
| 57 | |
Andres AG | c0db511 | 2016-12-07 15:05:53 +0000 | [diff] [blame] | 58 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | void mbedtls_pem_init( mbedtls_pem_context *ctx ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 60 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | memset( ctx, 0, sizeof( mbedtls_pem_context ) ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 64 | #if defined(PEM_RFC1421) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 65 | /* |
| 66 | * Read a 16-byte hex string and convert it to binary |
| 67 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 68 | static int pem_get_iv( const unsigned char *s, unsigned char *iv, |
| 69 | size_t iv_len ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 70 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 71 | size_t i, j, k; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 72 | |
| 73 | memset( iv, 0, iv_len ); |
| 74 | |
| 75 | for( i = 0; i < iv_len * 2; i++, s++ ) |
| 76 | { |
| 77 | if( *s >= '0' && *s <= '9' ) j = *s - '0'; else |
| 78 | if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else |
| 79 | if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 81 | |
| 82 | k = ( ( i & 1 ) != 0 ) ? j : j << 4; |
| 83 | |
| 84 | iv[i >> 1] = (unsigned char)( iv[i >> 1] | k ); |
| 85 | } |
| 86 | |
| 87 | return( 0 ); |
| 88 | } |
| 89 | |
Przemek Stekiel | be92bee | 2022-08-04 10:38:34 +0200 | [diff] [blame] | 90 | #if defined(MBEDTLS_MD5_C) |
| 91 | static int pem_pbkdf1( unsigned char *key, size_t keylen, |
| 92 | unsigned char *iv, |
| 93 | const unsigned char *pwd, size_t pwdlen ) |
| 94 | { |
| 95 | mbedtls_md5_context md5_ctx; |
| 96 | unsigned char md5sum[16]; |
| 97 | size_t use_len; |
| 98 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 99 | |
| 100 | mbedtls_md5_init( &md5_ctx ); |
| 101 | |
| 102 | /* |
| 103 | * key[ 0..15] = MD5(pwd || IV) |
| 104 | */ |
| 105 | if( ( ret = mbedtls_md5_starts( &md5_ctx ) ) != 0 ) |
| 106 | goto exit; |
| 107 | if( ( ret = mbedtls_md5_update( &md5_ctx, pwd, pwdlen ) ) != 0 ) |
| 108 | goto exit; |
| 109 | if( ( ret = mbedtls_md5_update( &md5_ctx, iv, 8 ) ) != 0 ) |
| 110 | goto exit; |
| 111 | if( ( ret = mbedtls_md5_finish( &md5_ctx, md5sum ) ) != 0 ) |
| 112 | goto exit; |
| 113 | |
| 114 | if( keylen <= 16 ) |
| 115 | { |
| 116 | memcpy( key, md5sum, keylen ); |
| 117 | goto exit; |
| 118 | } |
| 119 | |
| 120 | memcpy( key, md5sum, 16 ); |
| 121 | |
| 122 | /* |
| 123 | * key[16..23] = MD5(key[ 0..15] || pwd || IV]) |
| 124 | */ |
| 125 | if( ( ret = mbedtls_md5_starts( &md5_ctx ) ) != 0 ) |
| 126 | goto exit; |
| 127 | if( ( ret = mbedtls_md5_update( &md5_ctx, md5sum, 16 ) ) != 0 ) |
| 128 | goto exit; |
| 129 | if( ( ret = mbedtls_md5_update( &md5_ctx, pwd, pwdlen ) ) != 0 ) |
| 130 | goto exit; |
| 131 | if( ( ret = mbedtls_md5_update( &md5_ctx, iv, 8 ) ) != 0 ) |
| 132 | goto exit; |
| 133 | if( ( ret = mbedtls_md5_finish( &md5_ctx, md5sum ) ) != 0 ) |
| 134 | goto exit; |
| 135 | |
| 136 | use_len = 16; |
| 137 | if( keylen < 32 ) |
| 138 | use_len = keylen - 16; |
| 139 | |
| 140 | memcpy( key + 16, md5sum, use_len ); |
| 141 | |
| 142 | exit: |
| 143 | mbedtls_md5_free( &md5_ctx ); |
| 144 | mbedtls_platform_zeroize( md5sum, 16 ); |
| 145 | |
| 146 | return( ret ); |
| 147 | } |
| 148 | #else |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 149 | static int pem_pbkdf1( unsigned char *key, size_t keylen, |
| 150 | unsigned char *iv, |
| 151 | const unsigned char *pwd, size_t pwdlen ) |
| 152 | { |
| 153 | unsigned char md5sum[16]; |
| 154 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 155 | size_t output_length = 0; |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 156 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 157 | |
| 158 | |
Przemek Stekiel | bc3906c | 2022-08-19 09:16:36 +0200 | [diff] [blame] | 159 | if( ( status = psa_hash_setup( &operation, PSA_ALG_MD5 ) ) != PSA_SUCCESS ) |
| 160 | goto exit; |
| 161 | |
| 162 | if( ( status = psa_hash_update( &operation, pwd, pwdlen ) ) != PSA_SUCCESS ) |
| 163 | goto exit; |
| 164 | |
| 165 | if( ( status = psa_hash_update( &operation, iv, 8 ) ) != PSA_SUCCESS ) |
| 166 | goto exit; |
| 167 | |
| 168 | if( ( status = psa_hash_finish( &operation, md5sum, |
| 169 | PSA_HASH_LENGTH( PSA_ALG_MD5 ), |
| 170 | &output_length ) ) != PSA_SUCCESS ) |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 171 | { |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 172 | goto exit; |
| 173 | } |
Przemek Stekiel | bc3906c | 2022-08-19 09:16:36 +0200 | [diff] [blame] | 174 | |
| 175 | if( ( status = psa_hash_abort( &operation ) ) != PSA_SUCCESS ) |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 176 | goto exit; |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * key[ 0..15] = MD5(pwd || IV) |
| 180 | */ |
| 181 | if( keylen <= 16 ) |
| 182 | { |
| 183 | memcpy( key, md5sum, keylen ); |
| 184 | goto exit; |
| 185 | } |
| 186 | |
| 187 | memcpy( key, md5sum, 16 ); |
| 188 | |
| 189 | /* |
| 190 | * key[16..23] = MD5(key[ 0..15] || pwd || IV]) |
| 191 | */ |
Przemek Stekiel | bc3906c | 2022-08-19 09:16:36 +0200 | [diff] [blame] | 192 | if( ( status = psa_hash_setup( &operation, PSA_ALG_MD5 ) ) != PSA_SUCCESS ) |
| 193 | goto exit; |
| 194 | |
| 195 | if( ( status = psa_hash_update( &operation, md5sum, 16 ) ) != PSA_SUCCESS ) |
| 196 | goto exit; |
| 197 | |
| 198 | if( ( status = psa_hash_update( &operation, pwd, pwdlen ) ) != PSA_SUCCESS ) |
| 199 | goto exit; |
| 200 | |
| 201 | if( ( status = psa_hash_update( &operation, iv, 8 ) ) != PSA_SUCCESS ) |
| 202 | goto exit; |
| 203 | |
| 204 | if( ( status = psa_hash_finish( &operation, md5sum, |
| 205 | PSA_HASH_LENGTH( PSA_ALG_MD5 ), |
| 206 | &output_length ) ) != PSA_SUCCESS ) |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 207 | { |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 208 | goto exit; |
| 209 | } |
Przemek Stekiel | bc3906c | 2022-08-19 09:16:36 +0200 | [diff] [blame] | 210 | |
| 211 | if( ( status = psa_hash_abort( &operation ) ) != PSA_SUCCESS ) |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 212 | goto exit; |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 213 | |
| 214 | size_t use_len = 16; |
| 215 | if( keylen < 32 ) |
| 216 | use_len = keylen - 16; |
| 217 | |
| 218 | memcpy( key + 16, md5sum, use_len ); |
| 219 | |
| 220 | exit: |
| 221 | mbedtls_platform_zeroize( md5sum, 16 ); |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 222 | |
Przemek Stekiel | bc3906c | 2022-08-19 09:16:36 +0200 | [diff] [blame] | 223 | return( mbedtls_md_error_from_psa ( status ) ); |
Przemek Stekiel | a68d08f | 2022-08-04 08:42:06 +0200 | [diff] [blame] | 224 | } |
Przemek Stekiel | d23a4ef | 2022-08-18 11:56:54 +0200 | [diff] [blame] | 225 | #endif /* MBEDTLS_MD5_C */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #if defined(MBEDTLS_DES_C) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 228 | /* |
| 229 | * Decrypt with DES-CBC, using PBKDF1 for key derivation |
| 230 | */ |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 231 | static int pem_des_decrypt( unsigned char des_iv[8], |
| 232 | unsigned char *buf, size_t buflen, |
| 233 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 234 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | mbedtls_des_context des_ctx; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 236 | unsigned char des_key[8]; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 237 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | mbedtls_des_init( &des_ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 240 | |
Andres Amaya Garcia | 8d08c44 | 2017-06-29 11:16:38 +0100 | [diff] [blame] | 241 | if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 ) |
| 242 | goto exit; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 243 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 244 | if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 ) |
| 245 | goto exit; |
| 246 | ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen, |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 247 | des_iv, buf, buf ); |
| 248 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 249 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_des_free( &des_ctx ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 251 | mbedtls_platform_zeroize( des_key, 8 ); |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 252 | |
| 253 | return( ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Decrypt with 3DES-CBC, using PBKDF1 for key derivation |
| 258 | */ |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 259 | static int pem_des3_decrypt( unsigned char des3_iv[8], |
| 260 | unsigned char *buf, size_t buflen, |
| 261 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 262 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | mbedtls_des3_context des3_ctx; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 264 | unsigned char des3_key[24]; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 265 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 266 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | mbedtls_des3_init( &des3_ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 268 | |
Andres Amaya Garcia | 8d08c44 | 2017-06-29 11:16:38 +0100 | [diff] [blame] | 269 | if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 ) |
| 270 | goto exit; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 271 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 272 | if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 ) |
| 273 | goto exit; |
| 274 | ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen, |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 275 | des3_iv, buf, buf ); |
| 276 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 277 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | mbedtls_des3_free( &des3_ctx ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 279 | mbedtls_platform_zeroize( des3_key, 24 ); |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 280 | |
| 281 | return( ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 282 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | #endif /* MBEDTLS_DES_C */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | #if defined(MBEDTLS_AES_C) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 286 | /* |
| 287 | * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation |
| 288 | */ |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 289 | static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen, |
| 290 | unsigned char *buf, size_t buflen, |
| 291 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | mbedtls_aes_context aes_ctx; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 294 | unsigned char aes_key[32]; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 295 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | mbedtls_aes_init( &aes_ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 298 | |
Andres Amaya Garcia | 8d08c44 | 2017-06-29 11:16:38 +0100 | [diff] [blame] | 299 | if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 ) |
| 300 | goto exit; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 301 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 302 | if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 ) |
| 303 | goto exit; |
| 304 | ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen, |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 305 | aes_iv, buf, buf ); |
| 306 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 307 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_aes_free( &aes_ctx ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 309 | mbedtls_platform_zeroize( aes_key, keylen ); |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 310 | |
| 311 | return( ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 312 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 313 | #endif /* MBEDTLS_AES_C */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 314 | |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 315 | #endif /* PEM_RFC1421 */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 318 | const unsigned char *data, const unsigned char *pwd, |
| 319 | size_t pwdlen, size_t *use_len ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 320 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 321 | int ret, enc; |
| 322 | size_t len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 323 | unsigned char *buf; |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 324 | const unsigned char *s1, *s2, *end; |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 325 | #if defined(PEM_RFC1421) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 326 | unsigned char pem_iv[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 328 | #else |
| 329 | ((void) pwd); |
| 330 | ((void) pwdlen); |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 331 | #endif /* PEM_RFC1421 */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 332 | |
| 333 | if( ctx == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 335 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 336 | s1 = (unsigned char *) strstr( (const char *) data, header ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 337 | |
| 338 | if( s1 == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 340 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 341 | s2 = (unsigned char *) strstr( (const char *) data, footer ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 342 | |
| 343 | if( s2 == NULL || s2 <= s1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 345 | |
| 346 | s1 += strlen( header ); |
Manuel Pégourié-Gonnard | 052d10c | 2015-07-31 11:09:59 +0200 | [diff] [blame] | 347 | if( *s1 == ' ' ) s1++; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 348 | if( *s1 == '\r' ) s1++; |
| 349 | if( *s1 == '\n' ) s1++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 351 | |
| 352 | end = s2; |
| 353 | end += strlen( footer ); |
Manuel Pégourié-Gonnard | 052d10c | 2015-07-31 11:09:59 +0200 | [diff] [blame] | 354 | if( *end == ' ' ) end++; |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 355 | if( *end == '\r' ) end++; |
| 356 | if( *end == '\n' ) end++; |
| 357 | *use_len = end - data; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 358 | |
| 359 | enc = 0; |
| 360 | |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 361 | if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 362 | { |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 363 | #if defined(PEM_RFC1421) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 364 | enc++; |
| 365 | |
| 366 | s1 += 22; |
| 367 | if( *s1 == '\r' ) s1++; |
| 368 | if( *s1 == '\n' ) s1++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 369 | else return( MBEDTLS_ERR_PEM_INVALID_DATA ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 370 | |
| 371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 372 | #if defined(MBEDTLS_DES_C) |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 373 | if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 374 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 376 | |
| 377 | s1 += 23; |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 378 | if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 380 | |
| 381 | s1 += 16; |
| 382 | } |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 383 | else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 384 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | enc_alg = MBEDTLS_CIPHER_DES_CBC; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 386 | |
| 387 | s1 += 18; |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 388 | if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 390 | |
| 391 | s1 += 16; |
| 392 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | #endif /* MBEDTLS_DES_C */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | #if defined(MBEDTLS_AES_C) |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 396 | if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 397 | { |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 398 | if( s2 - s1 < 22 ) |
| 399 | return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 400 | else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | enc_alg = MBEDTLS_CIPHER_AES_128_CBC; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 402 | else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 403 | enc_alg = MBEDTLS_CIPHER_AES_192_CBC; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 404 | else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | enc_alg = MBEDTLS_CIPHER_AES_256_CBC; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 406 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 408 | |
| 409 | s1 += 22; |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 410 | if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | return( MBEDTLS_ERR_PEM_INVALID_ENC_IV ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 412 | |
| 413 | s1 += 32; |
| 414 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | #endif /* MBEDTLS_AES_C */ |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | if( enc_alg == MBEDTLS_CIPHER_NONE ) |
| 418 | return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 419 | |
| 420 | if( *s1 == '\r' ) s1++; |
| 421 | if( *s1 == '\n' ) s1++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | else return( MBEDTLS_ERR_PEM_INVALID_DATA ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 423 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE ); |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 425 | #endif /* PEM_RFC1421 */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Andres AG | 703990b | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 428 | if( s1 >= s2 ) |
Simon Butcher | a45aa13 | 2015-10-05 00:26:36 +0100 | [diff] [blame] | 429 | return( MBEDTLS_ERR_PEM_INVALID_DATA ); |
| 430 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 431 | ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER ) |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 434 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 435 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 436 | if( ( buf = mbedtls_calloc( 1, len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 437 | return( MBEDTLS_ERR_PEM_ALLOC_FAILED ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 438 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 439 | if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 440 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 441 | mbedtls_platform_zeroize( buf, len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | mbedtls_free( buf ); |
Chris Jones | 9f7a693 | 2021-04-14 12:12:09 +0100 | [diff] [blame] | 443 | return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 444 | } |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 445 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 446 | if( enc != 0 ) |
| 447 | { |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 448 | #if defined(PEM_RFC1421) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 449 | if( pwd == NULL ) |
| 450 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 451 | mbedtls_platform_zeroize( buf, len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | mbedtls_free( buf ); |
| 453 | return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 456 | ret = 0; |
| 457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | #if defined(MBEDTLS_DES_C) |
| 459 | if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC ) |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 460 | ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 461 | else if( enc_alg == MBEDTLS_CIPHER_DES_CBC ) |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 462 | ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | #endif /* MBEDTLS_DES_C */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | #if defined(MBEDTLS_AES_C) |
| 466 | if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC ) |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 467 | ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 468 | else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC ) |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 469 | ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC ) |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 471 | ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | #endif /* MBEDTLS_AES_C */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 473 | |
Andres AG | 51a7ae1 | 2017-02-22 16:23:26 +0000 | [diff] [blame] | 474 | if( ret != 0 ) |
| 475 | { |
| 476 | mbedtls_free( buf ); |
| 477 | return( ret ); |
| 478 | } |
| 479 | |
Manuel Pégourié-Gonnard | f8648d5 | 2013-07-03 21:01:35 +0200 | [diff] [blame] | 480 | /* |
Manuel Pégourié-Gonnard | 7d4e5b7 | 2013-07-09 16:35:23 +0200 | [diff] [blame] | 481 | * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3 |
| 482 | * length bytes (allow 4 to be sure) in all known use cases. |
| 483 | * |
ILUXONCHIK | 060fe37 | 2018-02-25 20:59:09 +0000 | [diff] [blame] | 484 | * Use that as a heuristic to try to detect password mismatches. |
Manuel Pégourié-Gonnard | f8648d5 | 2013-07-03 21:01:35 +0200 | [diff] [blame] | 485 | */ |
Manuel Pégourié-Gonnard | 7d4e5b7 | 2013-07-09 16:35:23 +0200 | [diff] [blame] | 486 | if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 487 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 488 | mbedtls_platform_zeroize( buf, len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 489 | mbedtls_free( buf ); |
| 490 | return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 491 | } |
| 492 | #else |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 493 | mbedtls_platform_zeroize( buf, len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 494 | mbedtls_free( buf ); |
| 495 | return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE ); |
Przemek Stekiel | 0cd6f08 | 2022-08-18 12:38:30 +0200 | [diff] [blame] | 496 | #endif /* PEM_RFC1421 */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | ctx->buf = buf; |
| 500 | ctx->buflen = len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 501 | |
| 502 | return( 0 ); |
| 503 | } |
| 504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 505 | void mbedtls_pem_free( mbedtls_pem_context *ctx ) |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 506 | { |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 507 | if ( ctx->buf != NULL ) |
| 508 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 509 | mbedtls_platform_zeroize( ctx->buf, ctx->buflen ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 510 | mbedtls_free( ctx->buf ); |
| 511 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | mbedtls_free( ctx->info ); |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 513 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 514 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) ); |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 515 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 516 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | #if defined(MBEDTLS_PEM_WRITE_C) |
| 519 | int mbedtls_pem_write_buffer( const char *header, const char *footer, |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 520 | const unsigned char *der_data, size_t der_len, |
| 521 | unsigned char *buf, size_t buf_len, size_t *olen ) |
| 522 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 523 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andres AG | 9cf1f96 | 2017-01-30 14:34:25 +0000 | [diff] [blame] | 524 | unsigned char *encode_buf = NULL, *c, *p = buf; |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 525 | size_t len = 0, use_len, add_len = 0; |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 526 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 527 | mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len ); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 528 | add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1; |
| 529 | |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 530 | if( use_len + add_len > buf_len ) |
| 531 | { |
| 532 | *olen = use_len + add_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 533 | return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Andres Amaya Garcia | f1ee635 | 2017-07-06 10:06:58 +0100 | [diff] [blame] | 536 | if( use_len != 0 && |
| 537 | ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 538 | return( MBEDTLS_ERR_PEM_ALLOC_FAILED ); |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 539 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 540 | if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data, |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 541 | der_len ) ) != 0 ) |
| 542 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 543 | mbedtls_free( encode_buf ); |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 544 | return( ret ); |
| 545 | } |
| 546 | |
| 547 | memcpy( p, header, strlen( header ) ); |
| 548 | p += strlen( header ); |
| 549 | c = encode_buf; |
| 550 | |
| 551 | while( use_len ) |
| 552 | { |
| 553 | len = ( use_len > 64 ) ? 64 : use_len; |
| 554 | memcpy( p, c, len ); |
| 555 | use_len -= len; |
| 556 | p += len; |
| 557 | c += len; |
| 558 | *p++ = '\n'; |
| 559 | } |
| 560 | |
| 561 | memcpy( p, footer, strlen( footer ) ); |
| 562 | p += strlen( footer ); |
| 563 | |
| 564 | *p++ = '\0'; |
| 565 | *olen = p - buf; |
| 566 | |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 567 | /* Clean any remaining data previously written to the buffer */ |
| 568 | memset( buf + *olen, 0, buf_len - *olen ); |
| 569 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 570 | mbedtls_free( encode_buf ); |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 571 | return( 0 ); |
| 572 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | #endif /* MBEDTLS_PEM_WRITE_C */ |
| 574 | #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */ |
Paul Elliott | 557b8d6 | 2020-11-19 09:46:56 +0000 | [diff] [blame] | 575 | |