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