blob: 3e3e96df36ebd86bd12845e4b9e45b9401eecc95 [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
Paul Bakker6e339b52013-07-03 13:37:05 +020037#if defined(POLARSSL_MEMORY_C)
38#include "polarssl/memory.h"
39#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include <stdlib.h>
Paul Bakker96743fc2011-02-12 14:30:57 +000045
46void pem_init( pem_context *ctx )
47{
48 memset( ctx, 0, sizeof( pem_context ) );
49}
50
51#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
52/*
53 * Read a 16-byte hex string and convert it to binary
54 */
Paul Bakker23986e52011-04-24 08:57:21 +000055static int pem_get_iv( const unsigned char *s, unsigned char *iv, size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000056{
Paul Bakker23986e52011-04-24 08:57:21 +000057 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000058
59 memset( iv, 0, iv_len );
60
61 for( i = 0; i < iv_len * 2; i++, s++ )
62 {
63 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
64 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
65 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
66 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
67
68 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
69
70 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
71 }
72
73 return( 0 );
74}
75
Paul Bakker23986e52011-04-24 08:57:21 +000076static void pem_pbkdf1( unsigned char *key, size_t keylen,
Paul Bakker96743fc2011-02-12 14:30:57 +000077 unsigned char *iv,
Paul Bakker23986e52011-04-24 08:57:21 +000078 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000079{
80 md5_context md5_ctx;
81 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000082 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +000083
84 /*
85 * key[ 0..15] = MD5(pwd || IV)
86 */
87 md5_starts( &md5_ctx );
88 md5_update( &md5_ctx, pwd, pwdlen );
89 md5_update( &md5_ctx, iv, 8 );
90 md5_finish( &md5_ctx, md5sum );
91
92 if( keylen <= 16 )
93 {
94 memcpy( key, md5sum, keylen );
95
96 memset( &md5_ctx, 0, sizeof( md5_ctx ) );
97 memset( md5sum, 0, 16 );
98 return;
99 }
100
101 memcpy( key, md5sum, 16 );
102
103 /*
104 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
105 */
106 md5_starts( &md5_ctx );
107 md5_update( &md5_ctx, md5sum, 16 );
108 md5_update( &md5_ctx, pwd, pwdlen );
109 md5_update( &md5_ctx, iv, 8 );
110 md5_finish( &md5_ctx, md5sum );
111
112 use_len = 16;
113 if( keylen < 32 )
114 use_len = keylen - 16;
115
116 memcpy( key + 16, md5sum, use_len );
117
118 memset( &md5_ctx, 0, sizeof( md5_ctx ) );
119 memset( md5sum, 0, 16 );
120}
121
122#if defined(POLARSSL_DES_C)
123/*
124 * Decrypt with DES-CBC, using PBKDF1 for key derivation
125 */
126static void pem_des_decrypt( unsigned char des_iv[8],
Paul Bakker23986e52011-04-24 08:57:21 +0000127 unsigned char *buf, size_t buflen,
128 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000129{
130 des_context des_ctx;
131 unsigned char des_key[8];
132
133 pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen );
134
135 des_setkey_dec( &des_ctx, des_key );
136 des_crypt_cbc( &des_ctx, DES_DECRYPT, buflen,
137 des_iv, buf, buf );
138
139 memset( &des_ctx, 0, sizeof( des_ctx ) );
140 memset( des_key, 0, 8 );
141}
142
143/*
144 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
145 */
146static void pem_des3_decrypt( unsigned char des3_iv[8],
Paul Bakker23986e52011-04-24 08:57:21 +0000147 unsigned char *buf, size_t buflen,
148 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000149{
150 des3_context des3_ctx;
151 unsigned char des3_key[24];
152
153 pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen );
154
155 des3_set3key_dec( &des3_ctx, des3_key );
156 des3_crypt_cbc( &des3_ctx, DES_DECRYPT, buflen,
157 des3_iv, buf, buf );
158
159 memset( &des3_ctx, 0, sizeof( des3_ctx ) );
160 memset( des3_key, 0, 24 );
161}
162#endif /* POLARSSL_DES_C */
163
164#if defined(POLARSSL_AES_C)
165/*
166 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
167 */
Paul Bakker23986e52011-04-24 08:57:21 +0000168static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
169 unsigned char *buf, size_t buflen,
170 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000171{
172 aes_context aes_ctx;
173 unsigned char aes_key[32];
174
175 pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen );
176
177 aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 );
178 aes_crypt_cbc( &aes_ctx, AES_DECRYPT, buflen,
179 aes_iv, buf, buf );
180
181 memset( &aes_ctx, 0, sizeof( aes_ctx ) );
182 memset( aes_key, 0, keylen );
183}
184#endif /* POLARSSL_AES_C */
185
186#endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
187
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200188int pem_read_buffer( pem_context *ctx, const char *header, const char *footer,
189 const unsigned char *data, const unsigned char *pwd,
190 size_t pwdlen, size_t *use_len )
Paul Bakker96743fc2011-02-12 14:30:57 +0000191{
Paul Bakker23986e52011-04-24 08:57:21 +0000192 int ret, enc;
193 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000194 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200195 const unsigned char *s1, *s2, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +0000196#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
197 unsigned char pem_iv[16];
198 cipher_type_t enc_alg = POLARSSL_CIPHER_NONE;
199#else
200 ((void) pwd);
201 ((void) pwdlen);
202#endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
203
204 if( ctx == NULL )
Paul Bakker00b28602013-06-24 13:02:41 +0200205 return( POLARSSL_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000206
Paul Bakker3c2122f2013-06-24 19:03:14 +0200207 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000208
209 if( s1 == NULL )
Paul Bakker00b28602013-06-24 13:02:41 +0200210 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000211
Paul Bakker3c2122f2013-06-24 19:03:14 +0200212 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000213
214 if( s2 == NULL || s2 <= s1 )
Paul Bakker00b28602013-06-24 13:02:41 +0200215 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000216
217 s1 += strlen( header );
218 if( *s1 == '\r' ) s1++;
219 if( *s1 == '\n' ) s1++;
Paul Bakker00b28602013-06-24 13:02:41 +0200220 else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
221
222 end = s2;
223 end += strlen( footer );
224 if( *end == '\r' ) end++;
225 if( *end == '\n' ) end++;
226 *use_len = end - data;
Paul Bakker96743fc2011-02-12 14:30:57 +0000227
228 enc = 0;
229
230 if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
231 {
232#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
233 enc++;
234
235 s1 += 22;
236 if( *s1 == '\r' ) s1++;
237 if( *s1 == '\n' ) s1++;
238 else return( POLARSSL_ERR_PEM_INVALID_DATA );
239
240
241#if defined(POLARSSL_DES_C)
242 if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
243 {
244 enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC;
245
246 s1 += 23;
247 if( pem_get_iv( s1, pem_iv, 8 ) != 0 )
248 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
249
250 s1 += 16;
251 }
252 else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
253 {
254 enc_alg = POLARSSL_CIPHER_DES_CBC;
255
256 s1 += 18;
257 if( pem_get_iv( s1, pem_iv, 8) != 0 )
258 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
259
260 s1 += 16;
261 }
262#endif /* POLARSSL_DES_C */
263
264#if defined(POLARSSL_AES_C)
265 if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
266 {
267 if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
268 enc_alg = POLARSSL_CIPHER_AES_128_CBC;
269 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
270 enc_alg = POLARSSL_CIPHER_AES_192_CBC;
271 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
272 enc_alg = POLARSSL_CIPHER_AES_256_CBC;
273 else
274 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
275
276 s1 += 22;
277 if( pem_get_iv( s1, pem_iv, 16 ) != 0 )
278 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
279
280 s1 += 32;
281 }
282#endif /* POLARSSL_AES_C */
283
284 if( enc_alg == POLARSSL_CIPHER_NONE )
285 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
286
287 if( *s1 == '\r' ) s1++;
288 if( *s1 == '\n' ) s1++;
289 else return( POLARSSL_ERR_PEM_INVALID_DATA );
290#else
291 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
292#endif /* POLARSSL_MD5_C && (POLARSSL_AES_C || POLARSSL_DES_C) */
293 }
294
295 len = 0;
296 ret = base64_decode( NULL, &len, s1, s2 - s1 );
297
298 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker9d781402011-05-09 16:17:09 +0000299 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000300
Paul Bakker6e339b52013-07-03 13:37:05 +0200301 if( ( buf = (unsigned char *) polarssl_malloc( len ) ) == NULL )
Paul Bakker96743fc2011-02-12 14:30:57 +0000302 return( POLARSSL_ERR_PEM_MALLOC_FAILED );
303
304 if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
305 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200306 polarssl_free( buf );
Paul Bakker9d781402011-05-09 16:17:09 +0000307 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000308 }
309
310 if( enc != 0 )
311 {
312#if defined(POLARSSL_MD5_C) && (defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C))
313 if( pwd == NULL )
314 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200315 polarssl_free( buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000316 return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED );
317 }
318
319#if defined(POLARSSL_DES_C)
320 if( enc_alg == POLARSSL_CIPHER_DES_EDE3_CBC )
321 pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
322 else if( enc_alg == POLARSSL_CIPHER_DES_CBC )
323 pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
324#endif /* POLARSSL_DES_C */
325
326#if defined(POLARSSL_AES_C)
327 if( enc_alg == POLARSSL_CIPHER_AES_128_CBC )
328 pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
329 else if( enc_alg == POLARSSL_CIPHER_AES_192_CBC )
330 pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
331 else if( enc_alg == POLARSSL_CIPHER_AES_256_CBC )
332 pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
333#endif /* POLARSSL_AES_C */
334
Manuel Pégourié-Gonnardf8648d52013-07-03 21:01:35 +0200335 /*
336 * The result should look like RSAPrivateKey or ECPrivateKey
337 * We use the following heuristic:
338 * len must be more than 6
339 * byte 1 must be 0x30 (SEQUENCE tag)
340 * then allow for one to 3 length bytes
341 * then we must have 0x02 0x01 (INTEGER tag + length, for version)
342 * version must be less than 4 (leaves some room)
343 */
344 if( ! ( len > 6 && buf[0] == 0x30 && (
345 ( buf[1] <= 0x7f && /* 1 length byte */
346 buf[2] == 0x02 && buf[3] == 0x01 && buf[4] < 4 ) ||
347 ( buf[1] == 0x81 && /* 2 length bytes */
348 buf[3] == 0x02 && buf[4] == 0x01 && buf[5] < 4 ) ||
349 ( buf[1] == 0x82 && /* 2 length bytes */
350 buf[4] == 0x02 && buf[5] == 0x01 && buf[6] < 4 ) ) ) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000351 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200352 polarssl_free( buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000353 return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH );
354 }
355#else
Paul Bakker6e339b52013-07-03 13:37:05 +0200356 polarssl_free( buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000357 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
358#endif
359 }
360
361 ctx->buf = buf;
362 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000363
364 return( 0 );
365}
366
367void pem_free( pem_context *ctx )
368{
369 if( ctx->buf )
Paul Bakker6e339b52013-07-03 13:37:05 +0200370 polarssl_free( ctx->buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000371
372 if( ctx->info )
Paul Bakker6e339b52013-07-03 13:37:05 +0200373 polarssl_free( ctx->info );
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000374
375 memset( ctx, 0, sizeof( pem_context ) );
Paul Bakker96743fc2011-02-12 14:30:57 +0000376}
377
378#endif