blob: 813c4ec2e7bf89bd036f9146f5c063f220787457 [file] [log] [blame]
Paul Bakker96743fc2011-02-12 14:30:57 +00001/*
2 * Privacy Enhanced Mail (PEM) decoding
3 *
Paul Bakker00b28602013-06-24 13:02:41 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker96743fc2011-02-12 14:30:57 +00005 *
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 Bakker96743fc2011-02-12 14:30:57 +000038
39void 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 Bakker23986e52011-04-24 08:57:21 +000048static int pem_get_iv( const unsigned char *s, unsigned char *iv, size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000049{
Paul Bakker23986e52011-04-24 08:57:21 +000050 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000051
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 Bakker23986e52011-04-24 08:57:21 +000069static void pem_pbkdf1( unsigned char *key, size_t keylen,
Paul Bakker96743fc2011-02-12 14:30:57 +000070 unsigned char *iv,
Paul Bakker23986e52011-04-24 08:57:21 +000071 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000072{
73 md5_context md5_ctx;
74 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000075 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +000076
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 */
119static void pem_des_decrypt( unsigned char des_iv[8],
Paul Bakker23986e52011-04-24 08:57:21 +0000120 unsigned char *buf, size_t buflen,
121 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000122{
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 */
139static void pem_des3_decrypt( unsigned char des3_iv[8],
Paul Bakker23986e52011-04-24 08:57:21 +0000140 unsigned char *buf, size_t buflen,
141 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000142{
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 Bakker23986e52011-04-24 08:57:21 +0000161static 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 Bakker96743fc2011-02-12 14:30:57 +0000164{
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 Bakker23986e52011-04-24 08:57:21 +0000181int 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 Bakker96743fc2011-02-12 14:30:57 +0000182{
Paul Bakker23986e52011-04-24 08:57:21 +0000183 int ret, enc;
184 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000185 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200186 const unsigned char *s1, *s2, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +0000187#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 )
Paul Bakker00b28602013-06-24 13:02:41 +0200196 return( POLARSSL_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000197
Paul Bakker3c2122f2013-06-24 19:03:14 +0200198 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000199
200 if( s1 == NULL )
Paul Bakker00b28602013-06-24 13:02:41 +0200201 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000202
Paul Bakker3c2122f2013-06-24 19:03:14 +0200203 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000204
205 if( s2 == NULL || s2 <= s1 )
Paul Bakker00b28602013-06-24 13:02:41 +0200206 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000207
208 s1 += strlen( header );
209 if( *s1 == '\r' ) s1++;
210 if( *s1 == '\n' ) s1++;
Paul Bakker00b28602013-06-24 13:02:41 +0200211 else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
212
213 end = s2;
214 end += strlen( footer );
215 if( *end == '\r' ) end++;
216 if( *end == '\n' ) end++;
217 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000218
219 enc = 0;
220
221 if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
222 {
223#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
224 enc++;
225
226 s1 += 22;
227 if( *s1 == '\r' ) s1++;
228 if( *s1 == '\n' ) s1++;
229 else return( POLARSSL_ERR_PEM_INVALID_DATA );
230
231
232#if defined(POLARSSL_DES_C)
233 if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
234 {
235 enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC;
236
237 s1 += 23;
238 if( pem_get_iv( s1, pem_iv, 8 ) != 0 )
239 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
240
241 s1 += 16;
242 }
243 else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
244 {
245 enc_alg = POLARSSL_CIPHER_DES_CBC;
246
247 s1 += 18;
248 if( pem_get_iv( s1, pem_iv, 8) != 0 )
249 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
250
251 s1 += 16;
252 }
253#endif /* POLARSSL_DES_C */
254
255#if defined(POLARSSL_AES_C)
256 if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
257 {
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;
264 else
265 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
266
267 s1 += 22;
268 if( pem_get_iv( s1, pem_iv, 16 ) != 0 )
269 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
270
271 s1 += 32;
272 }
273#endif /* POLARSSL_AES_C */
274
275 if( enc_alg == POLARSSL_CIPHER_NONE )
276 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
277
278 if( *s1 == '\r' ) s1++;
279 if( *s1 == '\n' ) s1++;
280 else return( POLARSSL_ERR_PEM_INVALID_DATA );
281#else
282 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
283#endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
284 }
285
286 len = 0;
287 ret = base64_decode( NULL, &len, s1, s2 - s1 );
288
289 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker9d781402011-05-09 16:17:09 +0000290 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000291
292 if( ( buf = (unsigned char *) malloc( len ) ) == NULL )
293 return( POLARSSL_ERR_PEM_MALLOC_FAILED );
294
295 if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
296 {
297 free( buf );
Paul Bakker9d781402011-05-09 16:17:09 +0000298 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000299 }
300
301 if( enc != 0 )
302 {
303#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
304 if( pwd == NULL )
305 {
306 free( buf );
307 return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED );
308 }
309
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 */
316
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 */
325
326 if( buf[0] != 0x30 || buf[1] != 0x82 ||
327 buf[4] != 0x02 || buf[5] != 0x01 )
328 {
329 free( buf );
330 return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH );
331 }
332#else
333 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
334#endif
335 }
336
337 ctx->buf = buf;
338 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000339
340 return( 0 );
341}
342
343void pem_free( pem_context *ctx )
344{
345 if( ctx->buf )
346 free( ctx->buf );
347
348 if( ctx->info )
349 free( ctx->info );
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000350
351 memset( ctx, 0, sizeof( pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +0000352}
353
354#endif