blob: d2d70abf53c8a5a884f1bb94731091755fa10bf5 [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 Bakkerb6c5d2e2013-06-25 16:25:17 +0200181int pem_read_buffer( pem_context *ctx, const char *header, const char *footer,
182 const unsigned char *data, const unsigned char *pwd,
183 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000184{
Paul Bakker23986e52011-04-24 08:57:21 +0000185 int ret, enc;
186 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000187 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200188 const unsigned char *s1, *s2, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +0000189#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
190 unsigned char pem_iv[16];
191 cipher_type_t enc_alg = POLARSSL_CIPHER_NONE;
192#else
193 ((void) pwd);
194 ((void) pwdlen);
195#endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
196
197 if( ctx == NULL )
Paul Bakker00b28602013-06-24 13:02:41 +0200198 return( POLARSSL_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000199
Paul Bakker3c2122f2013-06-24 19:03:14 +0200200 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000201
202 if( s1 == NULL )
Paul Bakker00b28602013-06-24 13:02:41 +0200203 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000204
Paul Bakker3c2122f2013-06-24 19:03:14 +0200205 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000206
207 if( s2 == NULL || s2 <= s1 )
Paul Bakker00b28602013-06-24 13:02:41 +0200208 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000209
210 s1 += strlen( header );
211 if( *s1 == '\r' ) s1++;
212 if( *s1 == '\n' ) s1++;
Paul Bakker00b28602013-06-24 13:02:41 +0200213 else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
214
215 end = s2;
216 end += strlen( footer );
217 if( *end == '\r' ) end++;
218 if( *end == '\n' ) end++;
219 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000220
221 enc = 0;
222
223 if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
224 {
225#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
226 enc++;
227
228 s1 += 22;
229 if( *s1 == '\r' ) s1++;
230 if( *s1 == '\n' ) s1++;
231 else return( POLARSSL_ERR_PEM_INVALID_DATA );
232
233
234#if defined(POLARSSL_DES_C)
235 if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
236 {
237 enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC;
238
239 s1 += 23;
240 if( pem_get_iv( s1, pem_iv, 8 ) != 0 )
241 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
242
243 s1 += 16;
244 }
245 else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
246 {
247 enc_alg = POLARSSL_CIPHER_DES_CBC;
248
249 s1 += 18;
250 if( pem_get_iv( s1, pem_iv, 8) != 0 )
251 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
252
253 s1 += 16;
254 }
255#endif /* POLARSSL_DES_C */
256
257#if defined(POLARSSL_AES_C)
258 if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
259 {
260 if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
261 enc_alg = POLARSSL_CIPHER_AES_128_CBC;
262 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
263 enc_alg = POLARSSL_CIPHER_AES_192_CBC;
264 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
265 enc_alg = POLARSSL_CIPHER_AES_256_CBC;
266 else
267 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
268
269 s1 += 22;
270 if( pem_get_iv( s1, pem_iv, 16 ) != 0 )
271 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
272
273 s1 += 32;
274 }
275#endif /* POLARSSL_AES_C */
276
277 if( enc_alg == POLARSSL_CIPHER_NONE )
278 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
279
280 if( *s1 == '\r' ) s1++;
281 if( *s1 == '\n' ) s1++;
282 else return( POLARSSL_ERR_PEM_INVALID_DATA );
283#else
284 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
285#endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
286 }
287
288 len = 0;
289 ret = base64_decode( NULL, &len, s1, s2 - s1 );
290
291 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker9d781402011-05-09 16:17:09 +0000292 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000293
294 if( ( buf = (unsigned char *) malloc( len ) ) == NULL )
295 return( POLARSSL_ERR_PEM_MALLOC_FAILED );
296
297 if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
298 {
299 free( buf );
Paul Bakker9d781402011-05-09 16:17:09 +0000300 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000301 }
302
303 if( enc != 0 )
304 {
305#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
306 if( pwd == NULL )
307 {
308 free( buf );
309 return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED );
310 }
311
312#if defined(POLARSSL_DES_C)
313 if( enc_alg == POLARSSL_CIPHER_DES_EDE3_CBC )
314 pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
315 else if( enc_alg == POLARSSL_CIPHER_DES_CBC )
316 pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
317#endif /* POLARSSL_DES_C */
318
319#if defined(POLARSSL_AES_C)
320 if( enc_alg == POLARSSL_CIPHER_AES_128_CBC )
321 pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
322 else if( enc_alg == POLARSSL_CIPHER_AES_192_CBC )
323 pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
324 else if( enc_alg == POLARSSL_CIPHER_AES_256_CBC )
325 pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
326#endif /* POLARSSL_AES_C */
327
328 if( buf[0] != 0x30 || buf[1] != 0x82 ||
329 buf[4] != 0x02 || buf[5] != 0x01 )
330 {
331 free( buf );
332 return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH );
333 }
334#else
Paul Bakkercf445ff2013-06-24 19:31:41 +0200335 free( buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000336 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
337#endif
338 }
339
340 ctx->buf = buf;
341 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000342
343 return( 0 );
344}
345
346void pem_free( pem_context *ctx )
347{
348 if( ctx->buf )
349 free( ctx->buf );
350
351 if( ctx->info )
352 free( ctx->info );
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000353
354 memset( ctx, 0, sizeof( pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +0000355}
356
357#endif