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