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