Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Privacy Enhanced Mail (PEM) decoding |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 28 | |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 29 | #if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C) |
Rich Evans | ce2f237 | 2015-02-06 13:57:42 +0000 | [diff] [blame] | 30 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 31 | #include "polarssl/pem.h" |
| 32 | #include "polarssl/base64.h" |
| 33 | #include "polarssl/des.h" |
| 34 | #include "polarssl/aes.h" |
| 35 | #include "polarssl/md5.h" |
| 36 | #include "polarssl/cipher.h" |
| 37 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 38 | #include <string.h> |
| 39 | |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 40 | #if defined(POLARSSL_PLATFORM_C) |
| 41 | #include "polarssl/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> |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 44 | #define polarssl_malloc malloc |
| 45 | #define polarssl_free free |
| 46 | #endif |
| 47 | |
Andres AG | f0a401f | 2016-12-07 16:08:04 +0000 | [diff] [blame] | 48 | #if defined(POLARSSL_PEM_PARSE_C) |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 49 | /* Implementation that should never be optimized out by the compiler */ |
| 50 | static void polarssl_zeroize( void *v, size_t n ) { |
| 51 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 52 | } |
| 53 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 54 | void pem_init( pem_context *ctx ) |
| 55 | { |
| 56 | memset( ctx, 0, sizeof( pem_context ) ); |
| 57 | } |
| 58 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 59 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
| 60 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Read a 16-byte hex string and convert it to binary |
| 63 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 64 | static int pem_get_iv( const unsigned char *s, unsigned char *iv, |
| 65 | size_t iv_len ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 66 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 67 | size_t i, j, k; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 68 | |
| 69 | memset( iv, 0, iv_len ); |
| 70 | |
| 71 | for( i = 0; i < iv_len * 2; i++, s++ ) |
| 72 | { |
| 73 | if( *s >= '0' && *s <= '9' ) j = *s - '0'; else |
| 74 | if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else |
| 75 | if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else |
| 76 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 77 | |
| 78 | k = ( ( i & 1 ) != 0 ) ? j : j << 4; |
| 79 | |
| 80 | iv[i >> 1] = (unsigned char)( iv[i >> 1] | k ); |
| 81 | } |
| 82 | |
| 83 | return( 0 ); |
| 84 | } |
| 85 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 86 | static void pem_pbkdf1( unsigned char *key, size_t keylen, |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 87 | unsigned char *iv, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 88 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 89 | { |
| 90 | md5_context md5_ctx; |
| 91 | unsigned char md5sum[16]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 92 | size_t use_len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 93 | |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 94 | md5_init( &md5_ctx ); |
| 95 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 96 | /* |
| 97 | * key[ 0..15] = MD5(pwd || IV) |
| 98 | */ |
| 99 | md5_starts( &md5_ctx ); |
| 100 | md5_update( &md5_ctx, pwd, pwdlen ); |
| 101 | md5_update( &md5_ctx, iv, 8 ); |
| 102 | md5_finish( &md5_ctx, md5sum ); |
| 103 | |
| 104 | if( keylen <= 16 ) |
| 105 | { |
| 106 | memcpy( key, md5sum, keylen ); |
| 107 | |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 108 | md5_free( &md5_ctx ); |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 109 | polarssl_zeroize( md5sum, 16 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | memcpy( key, md5sum, 16 ); |
| 114 | |
| 115 | /* |
| 116 | * key[16..23] = MD5(key[ 0..15] || pwd || IV]) |
| 117 | */ |
| 118 | md5_starts( &md5_ctx ); |
| 119 | md5_update( &md5_ctx, md5sum, 16 ); |
| 120 | md5_update( &md5_ctx, pwd, pwdlen ); |
| 121 | md5_update( &md5_ctx, iv, 8 ); |
| 122 | md5_finish( &md5_ctx, md5sum ); |
| 123 | |
| 124 | use_len = 16; |
| 125 | if( keylen < 32 ) |
| 126 | use_len = keylen - 16; |
| 127 | |
| 128 | memcpy( key + 16, md5sum, use_len ); |
| 129 | |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 130 | md5_free( &md5_ctx ); |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 131 | polarssl_zeroize( md5sum, 16 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | #if defined(POLARSSL_DES_C) |
| 135 | /* |
| 136 | * Decrypt with DES-CBC, using PBKDF1 for key derivation |
| 137 | */ |
| 138 | static void pem_des_decrypt( unsigned char des_iv[8], |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 139 | unsigned char *buf, size_t buflen, |
| 140 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 141 | { |
| 142 | des_context des_ctx; |
| 143 | unsigned char des_key[8]; |
| 144 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 145 | des_init( &des_ctx ); |
| 146 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 147 | pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ); |
| 148 | |
| 149 | des_setkey_dec( &des_ctx, des_key ); |
| 150 | des_crypt_cbc( &des_ctx, DES_DECRYPT, buflen, |
| 151 | des_iv, buf, buf ); |
| 152 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 153 | des_free( &des_ctx ); |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 154 | polarssl_zeroize( des_key, 8 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Decrypt with 3DES-CBC, using PBKDF1 for key derivation |
| 159 | */ |
| 160 | static void pem_des3_decrypt( unsigned char des3_iv[8], |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 161 | unsigned char *buf, size_t buflen, |
| 162 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 163 | { |
| 164 | des3_context des3_ctx; |
| 165 | unsigned char des3_key[24]; |
| 166 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 167 | des3_init( &des3_ctx ); |
| 168 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 169 | pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ); |
| 170 | |
| 171 | des3_set3key_dec( &des3_ctx, des3_key ); |
| 172 | des3_crypt_cbc( &des3_ctx, DES_DECRYPT, buflen, |
| 173 | des3_iv, buf, buf ); |
| 174 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 175 | des3_free( &des3_ctx ); |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 176 | polarssl_zeroize( des3_key, 24 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 177 | } |
| 178 | #endif /* POLARSSL_DES_C */ |
| 179 | |
| 180 | #if defined(POLARSSL_AES_C) |
| 181 | /* |
| 182 | * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation |
| 183 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 184 | static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen, |
| 185 | unsigned char *buf, size_t buflen, |
| 186 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 187 | { |
| 188 | aes_context aes_ctx; |
| 189 | unsigned char aes_key[32]; |
| 190 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 191 | aes_init( &aes_ctx ); |
| 192 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 193 | pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ); |
| 194 | |
| 195 | aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ); |
| 196 | aes_crypt_cbc( &aes_ctx, AES_DECRYPT, buflen, |
| 197 | aes_iv, buf, buf ); |
| 198 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 199 | aes_free( &aes_ctx ); |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 200 | polarssl_zeroize( aes_key, keylen ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 201 | } |
| 202 | #endif /* POLARSSL_AES_C */ |
| 203 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 204 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
| 205 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 206 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 207 | int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, |
| 208 | const unsigned char *data, const unsigned char *pwd, |
| 209 | size_t pwdlen, size_t *use_len ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 210 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 211 | int ret, enc; |
| 212 | size_t len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 213 | unsigned char *buf; |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 214 | const unsigned char *s1, *s2, *end; |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 215 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
| 216 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 217 | unsigned char pem_iv[16]; |
| 218 | cipher_type_t enc_alg = POLARSSL_CIPHER_NONE; |
| 219 | #else |
| 220 | ((void) pwd); |
| 221 | ((void) pwdlen); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 222 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
| 223 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 224 | |
| 225 | if( ctx == NULL ) |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 226 | return( POLARSSL_ERR_PEM_BAD_INPUT_DATA ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 227 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 228 | s1 = (unsigned char *) strstr( (const char *) data, header ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 229 | |
| 230 | if( s1 == NULL ) |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 231 | return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 232 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 233 | s2 = (unsigned char *) strstr( (const char *) data, footer ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 234 | |
| 235 | if( s2 == NULL || s2 <= s1 ) |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 236 | return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 237 | |
| 238 | s1 += strlen( header ); |
Manuel Pégourié-Gonnard | b5d77d3 | 2015-07-31 11:09:59 +0200 | [diff] [blame] | 239 | if( *s1 == ' ' ) s1++; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 240 | if( *s1 == '\r' ) s1++; |
| 241 | if( *s1 == '\n' ) s1++; |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 242 | else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
| 243 | |
| 244 | end = s2; |
| 245 | end += strlen( footer ); |
Manuel Pégourié-Gonnard | b5d77d3 | 2015-07-31 11:09:59 +0200 | [diff] [blame] | 246 | if( *end == ' ' ) end++; |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 247 | if( *end == '\r' ) end++; |
| 248 | if( *end == '\n' ) end++; |
| 249 | *use_len = end - data; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 250 | |
| 251 | enc = 0; |
| 252 | |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 253 | if( s2 - s1 >= 22 && memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 254 | { |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 255 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
| 256 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 257 | enc++; |
| 258 | |
| 259 | s1 += 22; |
| 260 | if( *s1 == '\r' ) s1++; |
| 261 | if( *s1 == '\n' ) s1++; |
| 262 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 263 | |
| 264 | |
| 265 | #if defined(POLARSSL_DES_C) |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 266 | 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] | 267 | { |
| 268 | enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC; |
| 269 | |
| 270 | s1 += 23; |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 271 | if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 272 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 273 | |
| 274 | s1 += 16; |
| 275 | } |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 276 | 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] | 277 | { |
| 278 | enc_alg = POLARSSL_CIPHER_DES_CBC; |
| 279 | |
| 280 | s1 += 18; |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 281 | if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 282 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 283 | |
| 284 | s1 += 16; |
| 285 | } |
| 286 | #endif /* POLARSSL_DES_C */ |
| 287 | |
| 288 | #if defined(POLARSSL_AES_C) |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 289 | if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 290 | { |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 291 | if( s2 - s1 < 22 ) |
| 292 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 293 | else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 294 | enc_alg = POLARSSL_CIPHER_AES_128_CBC; |
| 295 | else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 ) |
| 296 | enc_alg = POLARSSL_CIPHER_AES_192_CBC; |
| 297 | else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 ) |
| 298 | enc_alg = POLARSSL_CIPHER_AES_256_CBC; |
| 299 | else |
| 300 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 301 | |
| 302 | s1 += 22; |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 303 | if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 304 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 305 | |
| 306 | s1 += 32; |
| 307 | } |
| 308 | #endif /* POLARSSL_AES_C */ |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 309 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 310 | if( enc_alg == POLARSSL_CIPHER_NONE ) |
| 311 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 312 | |
| 313 | if( *s1 == '\r' ) s1++; |
| 314 | if( *s1 == '\n' ) s1++; |
| 315 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 316 | #else |
| 317 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 318 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
| 319 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Andres AG | de6079a | 2016-10-24 11:23:36 +0100 | [diff] [blame] | 322 | if( s1 >= s2 ) |
Manuel Pégourié-Gonnard | 9bf29be | 2015-09-28 18:27:15 +0200 | [diff] [blame] | 323 | return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 324 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 325 | len = 0; |
| 326 | ret = base64_decode( NULL, &len, s1, s2 - s1 ); |
| 327 | |
| 328 | if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 329 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 330 | |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 331 | if( ( buf = polarssl_malloc( len ) ) == NULL ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 332 | return( POLARSSL_ERR_PEM_MALLOC_FAILED ); |
| 333 | |
| 334 | if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 ) |
| 335 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 336 | polarssl_free( buf ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 337 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 338 | } |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 339 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 340 | if( enc != 0 ) |
| 341 | { |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 342 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
| 343 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 344 | if( pwd == NULL ) |
| 345 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 346 | polarssl_free( buf ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 347 | return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED ); |
| 348 | } |
| 349 | |
| 350 | #if defined(POLARSSL_DES_C) |
| 351 | if( enc_alg == POLARSSL_CIPHER_DES_EDE3_CBC ) |
| 352 | pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
| 353 | else if( enc_alg == POLARSSL_CIPHER_DES_CBC ) |
| 354 | pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
| 355 | #endif /* POLARSSL_DES_C */ |
| 356 | |
| 357 | #if defined(POLARSSL_AES_C) |
| 358 | if( enc_alg == POLARSSL_CIPHER_AES_128_CBC ) |
| 359 | pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen ); |
| 360 | else if( enc_alg == POLARSSL_CIPHER_AES_192_CBC ) |
| 361 | pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen ); |
| 362 | else if( enc_alg == POLARSSL_CIPHER_AES_256_CBC ) |
| 363 | pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen ); |
| 364 | #endif /* POLARSSL_AES_C */ |
| 365 | |
Manuel Pégourié-Gonnard | f8648d5 | 2013-07-03 21:01:35 +0200 | [diff] [blame] | 366 | /* |
Manuel Pégourié-Gonnard | 7d4e5b7 | 2013-07-09 16:35:23 +0200 | [diff] [blame] | 367 | * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3 |
| 368 | * length bytes (allow 4 to be sure) in all known use cases. |
| 369 | * |
| 370 | * Use that as heurisitic to try detecting password mismatchs. |
Manuel Pégourié-Gonnard | f8648d5 | 2013-07-03 21:01:35 +0200 | [diff] [blame] | 371 | */ |
Manuel Pégourié-Gonnard | 7d4e5b7 | 2013-07-09 16:35:23 +0200 | [diff] [blame] | 372 | if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 373 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 374 | polarssl_free( buf ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 375 | return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH ); |
| 376 | } |
| 377 | #else |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 378 | polarssl_free( buf ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 379 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 380 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
| 381 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | ctx->buf = buf; |
| 385 | ctx->buflen = len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 386 | |
| 387 | return( 0 ); |
| 388 | } |
| 389 | |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 390 | void pem_free( pem_context *ctx ) |
| 391 | { |
Ron Eldor | 27ce0b5 | 2017-09-05 15:34:35 +0300 | [diff] [blame^] | 392 | if ( ctx->buf != NULL ) |
| 393 | polarssl_zeroize( ctx->buf, ctx->buflen ); |
Paul Bakker | 14b16c6 | 2014-05-28 11:33:54 +0200 | [diff] [blame] | 394 | polarssl_free( ctx->buf ); |
| 395 | polarssl_free( ctx->info ); |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 396 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 397 | polarssl_zeroize( ctx, sizeof( pem_context ) ); |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 398 | } |
| 399 | #endif /* POLARSSL_PEM_PARSE_C */ |
| 400 | |
| 401 | #if defined(POLARSSL_PEM_WRITE_C) |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 402 | int pem_write_buffer( const char *header, const char *footer, |
| 403 | const unsigned char *der_data, size_t der_len, |
| 404 | unsigned char *buf, size_t buf_len, size_t *olen ) |
| 405 | { |
| 406 | int ret; |
| 407 | unsigned char *encode_buf, *c, *p = buf; |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 408 | size_t len = 0, use_len = 0, add_len = 0; |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 409 | |
| 410 | base64_encode( NULL, &use_len, der_data, der_len ); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 411 | add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1; |
| 412 | |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 413 | if( use_len + add_len > buf_len ) |
| 414 | { |
| 415 | *olen = use_len + add_len; |
| 416 | return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL ); |
| 417 | } |
| 418 | |
| 419 | if( ( encode_buf = polarssl_malloc( use_len ) ) == NULL ) |
| 420 | return( POLARSSL_ERR_PEM_MALLOC_FAILED ); |
| 421 | |
| 422 | if( ( ret = base64_encode( encode_buf, &use_len, der_data, |
| 423 | der_len ) ) != 0 ) |
| 424 | { |
| 425 | polarssl_free( encode_buf ); |
| 426 | return( ret ); |
| 427 | } |
| 428 | |
| 429 | memcpy( p, header, strlen( header ) ); |
| 430 | p += strlen( header ); |
| 431 | c = encode_buf; |
| 432 | |
| 433 | while( use_len ) |
| 434 | { |
| 435 | len = ( use_len > 64 ) ? 64 : use_len; |
| 436 | memcpy( p, c, len ); |
| 437 | use_len -= len; |
| 438 | p += len; |
| 439 | c += len; |
| 440 | *p++ = '\n'; |
| 441 | } |
| 442 | |
| 443 | memcpy( p, footer, strlen( footer ) ); |
| 444 | p += strlen( footer ); |
| 445 | |
| 446 | *p++ = '\0'; |
| 447 | *olen = p - buf; |
| 448 | |
| 449 | polarssl_free( encode_buf ); |
| 450 | return( 0 ); |
| 451 | } |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 452 | #endif /* POLARSSL_PEM_WRITE_C */ |
| 453 | #endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */ |