Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Privacy Enhanced Mail (PEM) decoding |
| 3 | * |
| 4 | * Copyright (C) 2006-2010, Brainspark B.V. |
| 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 | |
| 39 | void pem_init( pem_context *ctx ) |
| 40 | { |
| 41 | memset( ctx, 0, sizeof( pem_context ) ); |
| 42 | } |
| 43 | |
| 44 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 45 | /* |
| 46 | * Read a 16-byte hex string and convert it to binary |
| 47 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 48 | 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] | 49 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 50 | size_t i, j, k; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 51 | |
| 52 | memset( iv, 0, iv_len ); |
| 53 | |
| 54 | for( i = 0; i < iv_len * 2; i++, s++ ) |
| 55 | { |
| 56 | if( *s >= '0' && *s <= '9' ) j = *s - '0'; else |
| 57 | if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else |
| 58 | if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else |
| 59 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 60 | |
| 61 | k = ( ( i & 1 ) != 0 ) ? j : j << 4; |
| 62 | |
| 63 | iv[i >> 1] = (unsigned char)( iv[i >> 1] | k ); |
| 64 | } |
| 65 | |
| 66 | return( 0 ); |
| 67 | } |
| 68 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 69 | static void pem_pbkdf1( unsigned char *key, size_t keylen, |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 70 | unsigned char *iv, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 71 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 72 | { |
| 73 | md5_context md5_ctx; |
| 74 | unsigned char md5sum[16]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 75 | size_t use_len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * key[ 0..15] = MD5(pwd || IV) |
| 79 | */ |
| 80 | md5_starts( &md5_ctx ); |
| 81 | md5_update( &md5_ctx, pwd, pwdlen ); |
| 82 | md5_update( &md5_ctx, iv, 8 ); |
| 83 | md5_finish( &md5_ctx, md5sum ); |
| 84 | |
| 85 | if( keylen <= 16 ) |
| 86 | { |
| 87 | memcpy( key, md5sum, keylen ); |
| 88 | |
| 89 | memset( &md5_ctx, 0, sizeof( md5_ctx ) ); |
| 90 | memset( md5sum, 0, 16 ); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | memcpy( key, md5sum, 16 ); |
| 95 | |
| 96 | /* |
| 97 | * key[16..23] = MD5(key[ 0..15] || pwd || IV]) |
| 98 | */ |
| 99 | md5_starts( &md5_ctx ); |
| 100 | md5_update( &md5_ctx, md5sum, 16 ); |
| 101 | md5_update( &md5_ctx, pwd, pwdlen ); |
| 102 | md5_update( &md5_ctx, iv, 8 ); |
| 103 | md5_finish( &md5_ctx, md5sum ); |
| 104 | |
| 105 | use_len = 16; |
| 106 | if( keylen < 32 ) |
| 107 | use_len = keylen - 16; |
| 108 | |
| 109 | memcpy( key + 16, md5sum, use_len ); |
| 110 | |
| 111 | memset( &md5_ctx, 0, sizeof( md5_ctx ) ); |
| 112 | memset( md5sum, 0, 16 ); |
| 113 | } |
| 114 | |
| 115 | #if defined(POLARSSL_DES_C) |
| 116 | /* |
| 117 | * Decrypt with DES-CBC, using PBKDF1 for key derivation |
| 118 | */ |
| 119 | static void pem_des_decrypt( unsigned char des_iv[8], |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 120 | unsigned char *buf, size_t buflen, |
| 121 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 122 | { |
| 123 | des_context des_ctx; |
| 124 | unsigned char des_key[8]; |
| 125 | |
| 126 | pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ); |
| 127 | |
| 128 | des_setkey_dec( &des_ctx, des_key ); |
| 129 | des_crypt_cbc( &des_ctx, DES_DECRYPT, buflen, |
| 130 | des_iv, buf, buf ); |
| 131 | |
| 132 | memset( &des_ctx, 0, sizeof( des_ctx ) ); |
| 133 | memset( des_key, 0, 8 ); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Decrypt with 3DES-CBC, using PBKDF1 for key derivation |
| 138 | */ |
| 139 | static void pem_des3_decrypt( unsigned char des3_iv[8], |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 140 | unsigned char *buf, size_t buflen, |
| 141 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 142 | { |
| 143 | des3_context des3_ctx; |
| 144 | unsigned char des3_key[24]; |
| 145 | |
| 146 | pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ); |
| 147 | |
| 148 | des3_set3key_dec( &des3_ctx, des3_key ); |
| 149 | des3_crypt_cbc( &des3_ctx, DES_DECRYPT, buflen, |
| 150 | des3_iv, buf, buf ); |
| 151 | |
| 152 | memset( &des3_ctx, 0, sizeof( des3_ctx ) ); |
| 153 | memset( des3_key, 0, 24 ); |
| 154 | } |
| 155 | #endif /* POLARSSL_DES_C */ |
| 156 | |
| 157 | #if defined(POLARSSL_AES_C) |
| 158 | /* |
| 159 | * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation |
| 160 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 161 | static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen, |
| 162 | unsigned char *buf, size_t buflen, |
| 163 | const unsigned char *pwd, size_t pwdlen ) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 164 | { |
| 165 | aes_context aes_ctx; |
| 166 | unsigned char aes_key[32]; |
| 167 | |
| 168 | pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ); |
| 169 | |
| 170 | aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ); |
| 171 | aes_crypt_cbc( &aes_ctx, AES_DECRYPT, buflen, |
| 172 | aes_iv, buf, buf ); |
| 173 | |
| 174 | memset( &aes_ctx, 0, sizeof( aes_ctx ) ); |
| 175 | memset( aes_key, 0, keylen ); |
| 176 | } |
| 177 | #endif /* POLARSSL_AES_C */ |
| 178 | |
| 179 | #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */ |
| 180 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 181 | 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] | 182 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 183 | int ret, enc; |
| 184 | size_t len; |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 185 | unsigned char *buf; |
| 186 | unsigned char *s1, *s2; |
| 187 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 188 | unsigned char pem_iv[16]; |
| 189 | cipher_type_t enc_alg = POLARSSL_CIPHER_NONE; |
| 190 | #else |
| 191 | ((void) pwd); |
| 192 | ((void) pwdlen); |
| 193 | #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */ |
| 194 | |
| 195 | if( ctx == NULL ) |
| 196 | return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 197 | |
| 198 | s1 = (unsigned char *) strstr( (char *) data, header ); |
| 199 | |
| 200 | if( s1 == NULL ) |
| 201 | return( POLARSSL_ERR_PEM_NO_HEADER_PRESENT ); |
| 202 | |
| 203 | s2 = (unsigned char *) strstr( (char *) data, footer ); |
| 204 | |
| 205 | if( s2 == NULL || s2 <= s1 ) |
| 206 | return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 207 | |
| 208 | s1 += strlen( header ); |
| 209 | if( *s1 == '\r' ) s1++; |
| 210 | if( *s1 == '\n' ) s1++; |
| 211 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 212 | |
| 213 | enc = 0; |
| 214 | |
| 215 | if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) |
| 216 | { |
| 217 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 218 | enc++; |
| 219 | |
| 220 | s1 += 22; |
| 221 | if( *s1 == '\r' ) s1++; |
| 222 | if( *s1 == '\n' ) s1++; |
| 223 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 224 | |
| 225 | |
| 226 | #if defined(POLARSSL_DES_C) |
| 227 | if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 ) |
| 228 | { |
| 229 | enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC; |
| 230 | |
| 231 | s1 += 23; |
| 232 | if( pem_get_iv( s1, pem_iv, 8 ) != 0 ) |
| 233 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 234 | |
| 235 | s1 += 16; |
| 236 | } |
| 237 | else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 ) |
| 238 | { |
| 239 | enc_alg = POLARSSL_CIPHER_DES_CBC; |
| 240 | |
| 241 | s1 += 18; |
| 242 | if( pem_get_iv( s1, pem_iv, 8) != 0 ) |
| 243 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 244 | |
| 245 | s1 += 16; |
| 246 | } |
| 247 | #endif /* POLARSSL_DES_C */ |
| 248 | |
| 249 | #if defined(POLARSSL_AES_C) |
| 250 | if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) |
| 251 | { |
| 252 | if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) |
| 253 | enc_alg = POLARSSL_CIPHER_AES_128_CBC; |
| 254 | else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 ) |
| 255 | enc_alg = POLARSSL_CIPHER_AES_192_CBC; |
| 256 | else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 ) |
| 257 | enc_alg = POLARSSL_CIPHER_AES_256_CBC; |
| 258 | else |
| 259 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 260 | |
| 261 | s1 += 22; |
| 262 | if( pem_get_iv( s1, pem_iv, 16 ) != 0 ) |
| 263 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
| 264 | |
| 265 | s1 += 32; |
| 266 | } |
| 267 | #endif /* POLARSSL_AES_C */ |
| 268 | |
| 269 | if( enc_alg == POLARSSL_CIPHER_NONE ) |
| 270 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
| 271 | |
| 272 | if( *s1 == '\r' ) s1++; |
| 273 | if( *s1 == '\n' ) s1++; |
| 274 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
| 275 | #else |
| 276 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
| 277 | #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */ |
| 278 | } |
| 279 | |
| 280 | len = 0; |
| 281 | ret = base64_decode( NULL, &len, s1, s2 - s1 ); |
| 282 | |
| 283 | if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 284 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 285 | |
| 286 | if( ( buf = (unsigned char *) malloc( len ) ) == NULL ) |
| 287 | return( POLARSSL_ERR_PEM_MALLOC_FAILED ); |
| 288 | |
| 289 | if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 ) |
| 290 | { |
| 291 | free( buf ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 292 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if( enc != 0 ) |
| 296 | { |
| 297 | #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C)) |
| 298 | if( pwd == NULL ) |
| 299 | { |
| 300 | free( buf ); |
| 301 | return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED ); |
| 302 | } |
| 303 | |
| 304 | #if defined(POLARSSL_DES_C) |
| 305 | if( enc_alg == POLARSSL_CIPHER_DES_EDE3_CBC ) |
| 306 | pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
| 307 | else if( enc_alg == POLARSSL_CIPHER_DES_CBC ) |
| 308 | pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
| 309 | #endif /* POLARSSL_DES_C */ |
| 310 | |
| 311 | #if defined(POLARSSL_AES_C) |
| 312 | if( enc_alg == POLARSSL_CIPHER_AES_128_CBC ) |
| 313 | pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen ); |
| 314 | else if( enc_alg == POLARSSL_CIPHER_AES_192_CBC ) |
| 315 | pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen ); |
| 316 | else if( enc_alg == POLARSSL_CIPHER_AES_256_CBC ) |
| 317 | pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen ); |
| 318 | #endif /* POLARSSL_AES_C */ |
| 319 | |
| 320 | if( buf[0] != 0x30 || buf[1] != 0x82 || |
| 321 | buf[4] != 0x02 || buf[5] != 0x01 ) |
| 322 | { |
| 323 | free( buf ); |
| 324 | return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH ); |
| 325 | } |
| 326 | #else |
| 327 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
| 328 | #endif |
| 329 | } |
| 330 | |
| 331 | ctx->buf = buf; |
| 332 | ctx->buflen = len; |
| 333 | s2 += strlen( footer ); |
| 334 | if( *s2 == '\r' ) s2++; |
| 335 | if( *s2 == '\n' ) s2++; |
| 336 | *use_len = s2 - data; |
| 337 | |
| 338 | return( 0 ); |
| 339 | } |
| 340 | |
| 341 | void pem_free( pem_context *ctx ) |
| 342 | { |
| 343 | if( ctx->buf ) |
| 344 | free( ctx->buf ); |
| 345 | |
| 346 | if( ctx->info ) |
| 347 | free( ctx->info ); |
| 348 | } |
| 349 | |
| 350 | #endif |