Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Privacy Enhanced Mail (PEM) decoding |
| 3 | * |
Paul Bakker | 9255e83 | 2013-06-06 14:58:28 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
| 28 | #if defined(POLARSSL_PEM_C) |
| 29 | |
| 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 | |
| 37 | #include <stdlib.h> |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 38 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 39 | /* Implementation that should never be optimized out by the compiler */ |
| 40 | static void polarssl_zeroize( void *v, size_t n ) { |
| 41 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 42 | } |
| 43 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 44 | void pem_init( pem_context *ctx ) |
| 45 | { |
| 46 | memset( ctx, 0, sizeof( pem_context ) ); |
| 47 | } |
| 48 | |
| 49 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 50 | /* |
| 51 | * Read a 16-byte hex string and convert it to binary |
| 52 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 53 | static int pem_get_iv( const unsigned char *s, unsigned char *iv, size_t iv_len ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 54 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 55 | size_t i, j, k; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 56 | |
| 57 | memset( iv, 0, iv_len ); |
| 58 | |
| 59 | for( i = 0; i < iv_len * 2; i++, s++ ) |
| 60 | { |
| 61 | if( *s >= '0' && *s <= '9' ) j = *s - '0'; else |
| 62 | if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else |
| 63 | if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else |
| 64 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 65 | |
| 66 | k = ( ( i & 1 ) != 0 ) ? j : j << 4; |
| 67 | |
| 68 | iv[i >> 1] = (unsigned char)( iv[i >> 1] | k ); |
| 69 | } |
| 70 | |
| 71 | return( 0 ); |
| 72 | } |
| 73 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 74 | static void pem_pbkdf1( unsigned char *key, size_t keylen, |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 75 | unsigned char *iv, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 76 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 77 | { |
| 78 | md5_context md5_ctx; |
| 79 | unsigned char md5sum[16]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 80 | size_t use_len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * key[ 0..15] = MD5(pwd || IV) |
| 84 | */ |
| 85 | md5_starts( &md5_ctx ); |
| 86 | md5_update( &md5_ctx, pwd, pwdlen ); |
| 87 | md5_update( &md5_ctx, iv, 8 ); |
| 88 | md5_finish( &md5_ctx, md5sum ); |
| 89 | |
| 90 | if( keylen <= 16 ) |
| 91 | { |
| 92 | memcpy( key, md5sum, keylen ); |
| 93 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 94 | polarssl_zeroize( &md5_ctx, sizeof( md5_ctx ) ); |
| 95 | polarssl_zeroize( md5sum, 16 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | |
| 99 | memcpy( key, md5sum, 16 ); |
| 100 | |
| 101 | /* |
| 102 | * key[16..23] = MD5(key[ 0..15] || pwd || IV]) |
| 103 | */ |
| 104 | md5_starts( &md5_ctx ); |
| 105 | md5_update( &md5_ctx, md5sum, 16 ); |
| 106 | md5_update( &md5_ctx, pwd, pwdlen ); |
| 107 | md5_update( &md5_ctx, iv, 8 ); |
| 108 | md5_finish( &md5_ctx, md5sum ); |
| 109 | |
| 110 | use_len = 16; |
| 111 | if( keylen < 32 ) |
| 112 | use_len = keylen - 16; |
| 113 | |
| 114 | memcpy( key + 16, md5sum, use_len ); |
| 115 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 116 | polarssl_zeroize( &md5_ctx, sizeof( md5_ctx ) ); |
| 117 | polarssl_zeroize( md5sum, 16 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | #if defined(POLARSSL_DES_C) |
| 121 | /* |
| 122 | * Decrypt with DES-CBC, using PBKDF1 for key derivation |
| 123 | */ |
| 124 | static void pem_des_decrypt( unsigned char des_iv[8], |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 125 | unsigned char *buf, size_t buflen, |
| 126 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 127 | { |
| 128 | des_context des_ctx; |
| 129 | unsigned char des_key[8]; |
| 130 | |
| 131 | pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ); |
| 132 | |
| 133 | des_setkey_dec( &des_ctx, des_key ); |
| 134 | des_crypt_cbc( &des_ctx, DES_DECRYPT, buflen, |
| 135 | des_iv, buf, buf ); |
| 136 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 137 | polarssl_zeroize( &des_ctx, sizeof( des_ctx ) ); |
| 138 | polarssl_zeroize( des_key, 8 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Decrypt with 3DES-CBC, using PBKDF1 for key derivation |
| 143 | */ |
| 144 | static void pem_des3_decrypt( unsigned char des3_iv[8], |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 145 | unsigned char *buf, size_t buflen, |
| 146 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 147 | { |
| 148 | des3_context des3_ctx; |
| 149 | unsigned char des3_key[24]; |
| 150 | |
| 151 | pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ); |
| 152 | |
| 153 | des3_set3key_dec( &des3_ctx, des3_key ); |
| 154 | des3_crypt_cbc( &des3_ctx, DES_DECRYPT, buflen, |
| 155 | des3_iv, buf, buf ); |
| 156 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 157 | polarssl_zeroize( &des3_ctx, sizeof( des3_ctx ) ); |
| 158 | polarssl_zeroize( des3_key, 24 ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 159 | } |
| 160 | #endif /* POLARSSL_DES_C */ |
| 161 | |
| 162 | #if defined(POLARSSL_AES_C) |
| 163 | /* |
| 164 | * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation |
| 165 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 166 | static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen, |
| 167 | unsigned char *buf, size_t buflen, |
| 168 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 169 | { |
| 170 | aes_context aes_ctx; |
| 171 | unsigned char aes_key[32]; |
| 172 | |
| 173 | pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ); |
| 174 | |
| 175 | aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ); |
| 176 | aes_crypt_cbc( &aes_ctx, AES_DECRYPT, buflen, |
| 177 | aes_iv, buf, buf ); |
| 178 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 179 | polarssl_zeroize( &aes_ctx, sizeof( aes_ctx ) ); |
| 180 | polarssl_zeroize( aes_key, keylen ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 181 | } |
| 182 | #endif /* POLARSSL_AES_C */ |
| 183 | |
| 184 | #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */ |
| 185 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 186 | int pem_read_buffer( pem_context *ctx, char *header, char *footer, const unsigned char *data, const unsigned char *pwd, size_t pwdlen, size_t *use_len ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 187 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 188 | int ret, enc; |
| 189 | size_t len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 190 | unsigned char *buf; |
Paul Bakker | 9255e83 | 2013-06-06 14:58:28 +0200 | [diff] [blame] | 191 | const unsigned char *s1, *s2, *end; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 192 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 193 | unsigned char pem_iv[16]; |
| 194 | cipher_type_t enc_alg = POLARSSL_CIPHER_NONE; |
| 195 | #else |
| 196 | ((void) pwd); |
| 197 | ((void) pwdlen); |
| 198 | #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */ |
| 199 | |
| 200 | if( ctx == NULL ) |
Paul Bakker | 9255e83 | 2013-06-06 14:58:28 +0200 | [diff] [blame] | 201 | return( POLARSSL_ERR_PEM_BAD_INPUT_DATA ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 202 | |
Paul Bakker | eae09db | 2013-06-06 12:35:54 +0200 | [diff] [blame] | 203 | s1 = (unsigned char *) strstr( (const char *) data, header ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 204 | |
| 205 | if( s1 == NULL ) |
Paul Bakker | 9255e83 | 2013-06-06 14:58:28 +0200 | [diff] [blame] | 206 | return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 207 | |
Paul Bakker | eae09db | 2013-06-06 12:35:54 +0200 | [diff] [blame] | 208 | s2 = (unsigned char *) strstr( (const char *) data, footer ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 209 | |
| 210 | if( s2 == NULL || s2 <= s1 ) |
Paul Bakker | 9255e83 | 2013-06-06 14:58:28 +0200 | [diff] [blame] | 211 | return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 212 | |
| 213 | s1 += strlen( header ); |
| 214 | if( *s1 == '\r' ) s1++; |
| 215 | if( *s1 == '\n' ) s1++; |
Paul Bakker | 9255e83 | 2013-06-06 14:58:28 +0200 | [diff] [blame] | 216 | else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
| 217 | |
| 218 | end = s2; |
| 219 | end += strlen( footer ); |
| 220 | if( *end == '\r' ) end++; |
| 221 | if( *end == '\n' ) end++; |
| 222 | *use_len = end - data; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 223 | |
| 224 | enc = 0; |
| 225 | |
| 226 | if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) |
| 227 | { |
| 228 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 229 | enc++; |
| 230 | |
| 231 | s1 += 22; |
| 232 | if( *s1 == '\r' ) s1++; |
| 233 | if( *s1 == '\n' ) s1++; |
| 234 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 235 | |
| 236 | |
| 237 | #if defined(POLARSSL_DES_C) |
| 238 | if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 ) |
| 239 | { |
| 240 | enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC; |
| 241 | |
| 242 | s1 += 23; |
| 243 | if( pem_get_iv( s1, pem_iv, 8 ) != 0 ) |
| 244 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 245 | |
| 246 | s1 += 16; |
| 247 | } |
| 248 | else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 ) |
| 249 | { |
| 250 | enc_alg = POLARSSL_CIPHER_DES_CBC; |
| 251 | |
| 252 | s1 += 18; |
| 253 | if( pem_get_iv( s1, pem_iv, 8) != 0 ) |
| 254 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 255 | |
| 256 | s1 += 16; |
| 257 | } |
| 258 | #endif /* POLARSSL_DES_C */ |
| 259 | |
| 260 | #if defined(POLARSSL_AES_C) |
| 261 | if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) |
| 262 | { |
| 263 | if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) |
| 264 | enc_alg = POLARSSL_CIPHER_AES_128_CBC; |
| 265 | else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 ) |
| 266 | enc_alg = POLARSSL_CIPHER_AES_192_CBC; |
| 267 | else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 ) |
| 268 | enc_alg = POLARSSL_CIPHER_AES_256_CBC; |
| 269 | else |
| 270 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 271 | |
| 272 | s1 += 22; |
| 273 | if( pem_get_iv( s1, pem_iv, 16 ) != 0 ) |
| 274 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 275 | |
| 276 | s1 += 32; |
| 277 | } |
| 278 | #endif /* POLARSSL_AES_C */ |
| 279 | |
| 280 | if( enc_alg == POLARSSL_CIPHER_NONE ) |
| 281 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 282 | |
| 283 | if( *s1 == '\r' ) s1++; |
| 284 | if( *s1 == '\n' ) s1++; |
| 285 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 286 | #else |
| 287 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
| 288 | #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */ |
| 289 | } |
| 290 | |
| 291 | len = 0; |
| 292 | ret = base64_decode( NULL, &len, s1, s2 - s1 ); |
| 293 | |
| 294 | if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 295 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 296 | |
| 297 | if( ( buf = (unsigned char *) malloc( len ) ) == NULL ) |
| 298 | return( POLARSSL_ERR_PEM_MALLOC_FAILED ); |
| 299 | |
| 300 | if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 ) |
| 301 | { |
| 302 | free( buf ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 303 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if( enc != 0 ) |
| 307 | { |
| 308 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 309 | if( pwd == NULL ) |
| 310 | { |
| 311 | free( buf ); |
| 312 | return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED ); |
| 313 | } |
| 314 | |
| 315 | #if defined(POLARSSL_DES_C) |
| 316 | if( enc_alg == POLARSSL_CIPHER_DES_EDE3_CBC ) |
| 317 | pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
| 318 | else if( enc_alg == POLARSSL_CIPHER_DES_CBC ) |
| 319 | pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
| 320 | #endif /* POLARSSL_DES_C */ |
| 321 | |
| 322 | #if defined(POLARSSL_AES_C) |
| 323 | if( enc_alg == POLARSSL_CIPHER_AES_128_CBC ) |
| 324 | pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen ); |
| 325 | else if( enc_alg == POLARSSL_CIPHER_AES_192_CBC ) |
| 326 | pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen ); |
| 327 | else if( enc_alg == POLARSSL_CIPHER_AES_256_CBC ) |
| 328 | pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen ); |
| 329 | #endif /* POLARSSL_AES_C */ |
| 330 | |
| 331 | if( buf[0] != 0x30 || buf[1] != 0x82 || |
| 332 | buf[4] != 0x02 || buf[5] != 0x01 ) |
| 333 | { |
| 334 | free( buf ); |
| 335 | return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH ); |
| 336 | } |
| 337 | #else |
Paul Bakker | ff3a4b0 | 2013-06-17 15:56:12 +0200 | [diff] [blame] | 338 | free( buf ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 339 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
| 340 | #endif |
| 341 | } |
| 342 | |
| 343 | ctx->buf = buf; |
| 344 | ctx->buflen = len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 345 | |
| 346 | return( 0 ); |
| 347 | } |
| 348 | |
| 349 | void pem_free( pem_context *ctx ) |
| 350 | { |
| 351 | if( ctx->buf ) |
| 352 | free( ctx->buf ); |
| 353 | |
| 354 | if( ctx->info ) |
| 355 | free( ctx->info ); |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 356 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 357 | polarssl_zeroize( ctx, sizeof( pem_context ) ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | #endif |