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