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