blob: 1cc23ba8dbb2171bcffbea83531aad0b5efc2ed8 [file] [log] [blame]
Paul Bakker96743fc2011-02-12 14:30:57 +00001/*
2 * Privacy Enhanced Mail (PEM) decoding
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, 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
Paul Bakkercff68422013-09-15 20:43:33 +020028#if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C)
Paul Bakker96743fc2011-02-12 14:30:57 +000029#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 Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020038#else
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakker96743fc2011-02-12 14:30:57 +000043#include <stdlib.h>
Paul Bakker96743fc2011-02-12 14:30:57 +000044
Paul Bakkercff68422013-09-15 20:43:33 +020045#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker96743fc2011-02-12 14:30:57 +000046void pem_init( pem_context *ctx )
47{
48 memset( ctx, 0, sizeof( pem_context ) );
49}
50
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020051#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
52 ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +000053/*
54 * Read a 16-byte hex string and convert it to binary
55 */
Paul Bakker23986e52011-04-24 08:57:21 +000056static int pem_get_iv( const unsigned char *s, unsigned char *iv, size_t iv_len )
Paul Bakker96743fc2011-02-12 14:30:57 +000057{
Paul Bakker23986e52011-04-24 08:57:21 +000058 size_t i, j, k;
Paul Bakker96743fc2011-02-12 14:30:57 +000059
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 Bakker23986e52011-04-24 08:57:21 +000077static void pem_pbkdf1( unsigned char *key, size_t keylen,
Paul Bakker96743fc2011-02-12 14:30:57 +000078 unsigned char *iv,
Paul Bakker23986e52011-04-24 08:57:21 +000079 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +000080{
81 md5_context md5_ctx;
82 unsigned char md5sum[16];
Paul Bakker23986e52011-04-24 08:57:21 +000083 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +000084
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 */
127static void pem_des_decrypt( unsigned char des_iv[8],
Paul Bakker23986e52011-04-24 08:57:21 +0000128 unsigned char *buf, size_t buflen,
129 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000130{
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 */
147static void pem_des3_decrypt( unsigned char des3_iv[8],
Paul Bakker23986e52011-04-24 08:57:21 +0000148 unsigned char *buf, size_t buflen,
149 const unsigned char *pwd, size_t pwdlen )
Paul Bakker96743fc2011-02-12 14:30:57 +0000150{
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 Bakker23986e52011-04-24 08:57:21 +0000169static 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 Bakker96743fc2011-02-12 14:30:57 +0000172{
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é-Gonnard92cb1d32013-09-13 16:24:20 +0200187#endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
188 ( POLARSSL_AES_C || POLARSSL_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000189
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200190int 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 Bakker96743fc2011-02-12 14:30:57 +0000193{
Paul Bakker23986e52011-04-24 08:57:21 +0000194 int ret, enc;
195 size_t len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000196 unsigned char *buf;
Paul Bakker00b28602013-06-24 13:02:41 +0200197 const unsigned char *s1, *s2, *end;
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200198#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
199 ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000200 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é-Gonnard92cb1d32013-09-13 16:24:20 +0200205#endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
206 ( POLARSSL_AES_C || POLARSSL_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000207
208 if( ctx == NULL )
Paul Bakker00b28602013-06-24 13:02:41 +0200209 return( POLARSSL_ERR_PEM_BAD_INPUT_DATA );
Paul Bakker96743fc2011-02-12 14:30:57 +0000210
Paul Bakker3c2122f2013-06-24 19:03:14 +0200211 s1 = (unsigned char *) strstr( (const char *) data, header );
Paul Bakker96743fc2011-02-12 14:30:57 +0000212
213 if( s1 == NULL )
Paul Bakker00b28602013-06-24 13:02:41 +0200214 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000215
Paul Bakker3c2122f2013-06-24 19:03:14 +0200216 s2 = (unsigned char *) strstr( (const char *) data, footer );
Paul Bakker96743fc2011-02-12 14:30:57 +0000217
218 if( s2 == NULL || s2 <= s1 )
Paul Bakker00b28602013-06-24 13:02:41 +0200219 return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
Paul Bakker96743fc2011-02-12 14:30:57 +0000220
221 s1 += strlen( header );
222 if( *s1 == '\r' ) s1++;
223 if( *s1 == '\n' ) s1++;
Paul Bakker00b28602013-06-24 13:02:41 +0200224 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 Bakker96743fc2011-02-12 14:30:57 +0000231
232 enc = 0;
233
234 if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
235 {
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200236#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
237 ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000238 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 Bakkercff68422013-09-15 20:43:33 +0200288
Paul Bakker96743fc2011-02-12 14:30:57 +0000289 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é-Gonnard92cb1d32013-09-13 16:24:20 +0200297#endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
298 ( POLARSSL_AES_C || POLARSSL_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000299 }
300
301 len = 0;
302 ret = base64_decode( NULL, &len, s1, s2 - s1 );
303
304 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker9d781402011-05-09 16:17:09 +0000305 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000306
Paul Bakker6e339b52013-07-03 13:37:05 +0200307 if( ( buf = (unsigned char *) polarssl_malloc( len ) ) == NULL )
Paul Bakker96743fc2011-02-12 14:30:57 +0000308 return( POLARSSL_ERR_PEM_MALLOC_FAILED );
309
310 if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
311 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200312 polarssl_free( buf );
Paul Bakker9d781402011-05-09 16:17:09 +0000313 return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
Paul Bakker96743fc2011-02-12 14:30:57 +0000314 }
Paul Bakkercff68422013-09-15 20:43:33 +0200315
Paul Bakker96743fc2011-02-12 14:30:57 +0000316 if( enc != 0 )
317 {
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200318#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
319 ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
Paul Bakker96743fc2011-02-12 14:30:57 +0000320 if( pwd == NULL )
321 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200322 polarssl_free( buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000323 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é-Gonnardf8648d52013-07-03 21:01:35 +0200342 /*
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200343 * 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é-Gonnardf8648d52013-07-03 21:01:35 +0200347 */
Manuel Pégourié-Gonnard7d4e5b72013-07-09 16:35:23 +0200348 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
Paul Bakker96743fc2011-02-12 14:30:57 +0000349 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200350 polarssl_free( buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000351 return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH );
352 }
353#else
Paul Bakker6e339b52013-07-03 13:37:05 +0200354 polarssl_free( buf );
Paul Bakker96743fc2011-02-12 14:30:57 +0000355 return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200356#endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
357 ( POLARSSL_AES_C || POLARSSL_DES_C ) */
Paul Bakker96743fc2011-02-12 14:30:57 +0000358 }
359
360 ctx->buf = buf;
361 ctx->buflen = len;
Paul Bakker96743fc2011-02-12 14:30:57 +0000362
363 return( 0 );
364}
365
Paul Bakkercff68422013-09-15 20:43:33 +0200366void 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 Bakker77e23fb2013-09-15 20:03:26 +0200379int 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 Bakker16300582014-04-11 13:28:43 +0200385 size_t len = 0, use_len = 0, add_len = 0;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200386
387 base64_encode( NULL, &use_len, der_data, der_len );
Paul Bakker16300582014-04-11 13:28:43 +0200388 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
389
Paul Bakker77e23fb2013-09-15 20:03:26 +0200390 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 Bakkercff68422013-09-15 20:43:33 +0200429#endif /* POLARSSL_PEM_WRITE_C */
430#endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */