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