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