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