2 * Privacy Enhanced Mail (PEM) decoding
4 * Copyright (C) 2006-2013, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
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.
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.
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.
28 #if defined(POLARSSL_PEM_C)
39 void pem_init( pem_context
*ctx
)
41 memset( ctx
, 0, sizeof( pem_context
) );
44 #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
46 * Read a 16-byte hex string and convert it to binary
48 static int pem_get_iv( const unsigned char *s
, unsigned char *iv
, size_t iv_len
)
52 memset( iv
, 0, iv_len
);
54 for( i
= 0; i
< iv_len
* 2; i
++, s
++ )
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
);
61 k
= ( ( i
& 1 ) != 0 ) ? j
: j
<< 4;
63 iv
[i
>> 1] = (unsigned char)( iv
[i
>> 1] | k
);
69 static void pem_pbkdf1( unsigned char *key
, size_t keylen
,
71 const unsigned char *pwd
, size_t pwdlen
)
74 unsigned char md5sum
[16];
78 * key[ 0..15] = MD5(pwd || IV)
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
);
87 memcpy( key
, md5sum
, keylen
);
89 memset( &md5_ctx
, 0, sizeof( md5_ctx
) );
90 memset( md5sum
, 0, 16 );
94 memcpy( key
, md5sum
, 16 );
97 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
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
);
107 use_len
= keylen
- 16;
109 memcpy( key
+ 16, md5sum
, use_len
);
111 memset( &md5_ctx
, 0, sizeof( md5_ctx
) );
112 memset( md5sum
, 0, 16 );
115 #if defined(POLARSSL_DES_C)
117 * Decrypt with DES-CBC, using PBKDF1 for key derivation
119 static void pem_des_decrypt( unsigned char des_iv
[8],
120 unsigned char *buf
, size_t buflen
,
121 const unsigned char *pwd
, size_t pwdlen
)
124 unsigned char des_key
[8];
126 pem_pbkdf1( des_key
, 8, des_iv
, pwd
, pwdlen
);
128 des_setkey_dec( &des_ctx
, des_key
);
129 des_crypt_cbc( &des_ctx
, DES_DECRYPT
, buflen
,
132 memset( &des_ctx
, 0, sizeof( des_ctx
) );
133 memset( des_key
, 0, 8 );
137 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
139 static void pem_des3_decrypt( unsigned char des3_iv
[8],
140 unsigned char *buf
, size_t buflen
,
141 const unsigned char *pwd
, size_t pwdlen
)
143 des3_context des3_ctx
;
144 unsigned char des3_key
[24];
146 pem_pbkdf1( des3_key
, 24, des3_iv
, pwd
, pwdlen
);
148 des3_set3key_dec( &des3_ctx
, des3_key
);
149 des3_crypt_cbc( &des3_ctx
, DES_DECRYPT
, buflen
,
152 memset( &des3_ctx
, 0, sizeof( des3_ctx
) );
153 memset( des3_key
, 0, 24 );
155 #endif /* POLARSSL_DES_C */
157 #if defined(POLARSSL_AES_C)
159 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
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
)
166 unsigned char aes_key
[32];
168 pem_pbkdf1( aes_key
, keylen
, aes_iv
, pwd
, pwdlen
);
170 aes_setkey_dec( &aes_ctx
, aes_key
, keylen
* 8 );
171 aes_crypt_cbc( &aes_ctx
, AES_DECRYPT
, buflen
,
174 memset( &aes_ctx
, 0, sizeof( aes_ctx
) );
175 memset( aes_key
, 0, keylen
);
177 #endif /* POLARSSL_AES_C */
179 #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
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
)
186 const unsigned char *s1
, *s2
, *end
;
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
;
193 #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
196 return( POLARSSL_ERR_PEM_BAD_INPUT_DATA
);
198 s1
= (unsigned char *) strstr( (const char *) data
, header
);
201 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
);
203 s2
= (unsigned char *) strstr( (const char *) data
, footer
);
205 if( s2
== NULL
|| s2
<= s1
)
206 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
);
208 s1
+= strlen( header
);
209 if( *s1
== '\r' ) s1
++;
210 if( *s1
== '\n' ) s1
++;
211 else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
);
214 end
+= strlen( footer
);
215 if( *end
== '\r' ) end
++;
216 if( *end
== '\n' ) end
++;
217 *use_len
= end
- data
;
221 if( memcmp( s1
, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
223 #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
227 if( *s1
== '\r' ) s1
++;
228 if( *s1
== '\n' ) s1
++;
229 else return( POLARSSL_ERR_PEM_INVALID_DATA
);
232 #if defined(POLARSSL_DES_C)
233 if( memcmp( s1
, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
235 enc_alg
= POLARSSL_CIPHER_DES_EDE3_CBC
;
238 if( pem_get_iv( s1
, pem_iv
, 8 ) != 0 )
239 return( POLARSSL_ERR_PEM_INVALID_ENC_IV
);
243 else if( memcmp( s1
, "DEK-Info: DES-CBC,", 18 ) == 0 )
245 enc_alg
= POLARSSL_CIPHER_DES_CBC
;
248 if( pem_get_iv( s1
, pem_iv
, 8) != 0 )
249 return( POLARSSL_ERR_PEM_INVALID_ENC_IV
);
253 #endif /* POLARSSL_DES_C */
255 #if defined(POLARSSL_AES_C)
256 if( memcmp( s1
, "DEK-Info: AES-", 14 ) == 0 )
258 if( memcmp( s1
, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
259 enc_alg
= POLARSSL_CIPHER_AES_128_CBC
;
260 else if( memcmp( s1
, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
261 enc_alg
= POLARSSL_CIPHER_AES_192_CBC
;
262 else if( memcmp( s1
, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
263 enc_alg
= POLARSSL_CIPHER_AES_256_CBC
;
265 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG
);
268 if( pem_get_iv( s1
, pem_iv
, 16 ) != 0 )
269 return( POLARSSL_ERR_PEM_INVALID_ENC_IV
);
273 #endif /* POLARSSL_AES_C */
275 if( enc_alg
== POLARSSL_CIPHER_NONE
)
276 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG
);
278 if( *s1
== '\r' ) s1
++;
279 if( *s1
== '\n' ) s1
++;
280 else return( POLARSSL_ERR_PEM_INVALID_DATA
);
282 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE
);
283 #endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
287 ret
= base64_decode( NULL
, &len
, s1
, s2
- s1
);
289 if( ret
== POLARSSL_ERR_BASE64_INVALID_CHARACTER
)
290 return( POLARSSL_ERR_PEM_INVALID_DATA
+ ret
);
292 if( ( buf
= (unsigned char *) malloc( len
) ) == NULL
)
293 return( POLARSSL_ERR_PEM_MALLOC_FAILED
);
295 if( ( ret
= base64_decode( buf
, &len
, s1
, s2
- s1
) ) != 0 )
298 return( POLARSSL_ERR_PEM_INVALID_DATA
+ ret
);
303 #if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
307 return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED
);
310 #if defined(POLARSSL_DES_C)
311 if( enc_alg
== POLARSSL_CIPHER_DES_EDE3_CBC
)
312 pem_des3_decrypt( pem_iv
, buf
, len
, pwd
, pwdlen
);
313 else if( enc_alg
== POLARSSL_CIPHER_DES_CBC
)
314 pem_des_decrypt( pem_iv
, buf
, len
, pwd
, pwdlen
);
315 #endif /* POLARSSL_DES_C */
317 #if defined(POLARSSL_AES_C)
318 if( enc_alg
== POLARSSL_CIPHER_AES_128_CBC
)
319 pem_aes_decrypt( pem_iv
, 16, buf
, len
, pwd
, pwdlen
);
320 else if( enc_alg
== POLARSSL_CIPHER_AES_192_CBC
)
321 pem_aes_decrypt( pem_iv
, 24, buf
, len
, pwd
, pwdlen
);
322 else if( enc_alg
== POLARSSL_CIPHER_AES_256_CBC
)
323 pem_aes_decrypt( pem_iv
, 32, buf
, len
, pwd
, pwdlen
);
324 #endif /* POLARSSL_AES_C */
326 if( buf
[0] != 0x30 || buf
[1] != 0x82 ||
327 buf
[4] != 0x02 || buf
[5] != 0x01 )
330 return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH
);
334 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE
);
344 void pem_free( pem_context
*ctx
)
352 memset( ctx
, 0, sizeof( pem_context
) );