1/*
2 * Privacy Enhanced Mail (PEM) decoding
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
23
24#include "mbedtls/pem.h"
25#include "mbedtls/base64.h"
26#include "mbedtls/des.h"
27#ifdef MBEDTLS_AES_C
28#include "mbedtls/aes.h"
29#endif
30#ifdef MBEDTLS_MD5_C
31#include "mbedtls/md5.h"
32#endif
33#include "mbedtls/cipher.h"
34#include "mbedtls/platform_util.h"
35#include "mbedtls/error.h"
36
37#include <string.h>
38
39#if defined(MBEDTLS_PLATFORM_C)
40#include "mbedtls/platform.h"
41#else
42#include <stdlib.h>
43#define mbedtls_calloc calloc
44#define mbedtls_free free
45#endif
46
47#if defined(MBEDTLS_PEM_PARSE_C)
48void mbedtls_pem_init( mbedtls_pem_context *ctx )
49{
50 memset( s: ctx, c: 0, n: sizeof( mbedtls_pem_context ) );
51}
52
53#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
54 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
55/*
56 * Read a 16-byte hex string and convert it to binary
57 */
58static int pem_get_iv( const unsigned char *s, unsigned char *iv,
59 size_t iv_len )
60{
61 size_t i, j, k;
62
63 memset( iv, 0, iv_len );
64
65 for( i = 0; i < iv_len * 2; i++, s++ )
66 {
67 if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
68 if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
69 if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
70 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
71
72 k = ( ( i & 1 ) != 0 ) ? j : j << 4;
73
74 iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
75 }
76
77 return( 0 );
78}
79
80static int pem_pbkdf1( unsigned char *key, size_t keylen,
81 unsigned char *iv,
82 const unsigned char *pwd, size_t pwdlen )
83{
84 mbedtls_md5_context md5_ctx;
85 unsigned char md5sum[16];
86 size_t use_len;
87 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
88
89 mbedtls_md5_init( &md5_ctx );
90
91 /*
92 * key[ 0..15] = MD5(pwd || IV)
93 */
94 if( ( ret = mbedtls_md5_starts( &md5_ctx ) ) != 0 )
95 goto exit;
96 if( ( ret = mbedtls_md5_update( &md5_ctx, pwd, pwdlen ) ) != 0 )
97 goto exit;
98 if( ( ret = mbedtls_md5_update( &md5_ctx, iv, 8 ) ) != 0 )
99 goto exit;
100 if( ( ret = mbedtls_md5_finish( &md5_ctx, md5sum ) ) != 0 )
101 goto exit;
102
103 if( keylen <= 16 )
104 {
105 memcpy( key, md5sum, keylen );
106 goto exit;
107 }
108
109 memcpy( key, md5sum, 16 );
110
111 /*
112 * key[16..23] = MD5(key[ 0..15] || pwd || IV])
113 */
114 if( ( ret = mbedtls_md5_starts( &md5_ctx ) ) != 0 )
115 goto exit;
116 if( ( ret = mbedtls_md5_update( &md5_ctx, md5sum, 16 ) ) != 0 )
117 goto exit;
118 if( ( ret = mbedtls_md5_update( &md5_ctx, pwd, pwdlen ) ) != 0 )
119 goto exit;
120 if( ( ret = mbedtls_md5_update( &md5_ctx, iv, 8 ) ) != 0 )
121 goto exit;
122 if( ( ret = mbedtls_md5_finish( &md5_ctx, md5sum ) ) != 0 )
123 goto exit;
124
125 use_len = 16;
126 if( keylen < 32 )
127 use_len = keylen - 16;
128
129 memcpy( key + 16, md5sum, use_len );
130
131exit:
132 mbedtls_md5_free( &md5_ctx );
133 mbedtls_platform_zeroize( md5sum, 16 );
134
135 return( ret );
136}
137
138#if defined(MBEDTLS_DES_C)
139/*
140 * Decrypt with DES-CBC, using PBKDF1 for key derivation
141 */
142static int pem_des_decrypt( unsigned char des_iv[8],
143 unsigned char *buf, size_t buflen,
144 const unsigned char *pwd, size_t pwdlen )
145{
146 mbedtls_des_context des_ctx;
147 unsigned char des_key[8];
148 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
149
150 mbedtls_des_init( &des_ctx );
151
152 if( ( ret = pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ) ) != 0 )
153 goto exit;
154
155 if( ( ret = mbedtls_des_setkey_dec( &des_ctx, des_key ) ) != 0 )
156 goto exit;
157 ret = mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
158 des_iv, buf, buf );
159
160exit:
161 mbedtls_des_free( &des_ctx );
162 mbedtls_platform_zeroize( des_key, 8 );
163
164 return( ret );
165}
166
167/*
168 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
169 */
170static int pem_des3_decrypt( unsigned char des3_iv[8],
171 unsigned char *buf, size_t buflen,
172 const unsigned char *pwd, size_t pwdlen )
173{
174 mbedtls_des3_context des3_ctx;
175 unsigned char des3_key[24];
176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
177
178 mbedtls_des3_init( &des3_ctx );
179
180 if( ( ret = pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ) ) != 0 )
181 goto exit;
182
183 if( ( ret = mbedtls_des3_set3key_dec( &des3_ctx, des3_key ) ) != 0 )
184 goto exit;
185 ret = mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
186 des3_iv, buf, buf );
187
188exit:
189 mbedtls_des3_free( &des3_ctx );
190 mbedtls_platform_zeroize( des3_key, 24 );
191
192 return( ret );
193}
194#endif /* MBEDTLS_DES_C */
195
196#if defined(MBEDTLS_AES_C)
197/*
198 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
199 */
200static int pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
201 unsigned char *buf, size_t buflen,
202 const unsigned char *pwd, size_t pwdlen )
203{
204 mbedtls_aes_context aes_ctx;
205 unsigned char aes_key[32];
206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
207
208 mbedtls_aes_init( &aes_ctx );
209
210 if( ( ret = pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ) ) != 0 )
211 goto exit;
212
213 if( ( ret = mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ) ) != 0 )
214 goto exit;
215 ret = mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
216 aes_iv, buf, buf );
217
218exit:
219 mbedtls_aes_free( &aes_ctx );
220 mbedtls_platform_zeroize( aes_key, keylen );
221
222 return( ret );
223}
224#endif /* MBEDTLS_AES_C */
225
226#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
227 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
228
229int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
230 const unsigned char *data, const unsigned char *pwd,
231 size_t pwdlen, size_t *use_len )
232{
233 int ret, enc;
234 size_t len;
235 unsigned char *buf;
236 const unsigned char *s1, *s2, *end;
237#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
238 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
239 unsigned char pem_iv[16];
240 mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
241#else
242 ((void) pwd);
243 ((void) pwdlen);
244#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
245 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
246
247 if( ctx == NULL )
248 return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
249
250 s1 = (unsigned char *) strstr( haystack: (const char *) data, needle: header );
251
252 if( s1 == NULL )
253 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
254
255 s2 = (unsigned char *) strstr( haystack: (const char *) data, needle: footer );
256
257 if( s2 == NULL || s2 <= s1 )
258 return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
259
260 s1 += strlen( s: header );
261 if( *s1 == ' ' ) s1++;
262 if( *s1 == '\r' ) s1++;
263 if( *s1 == '\n' ) s1++;
264 else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
265
266 end = s2;
267 end += strlen( s: footer );
268 if( *end == ' ' ) end++;
269 if( *end == '\r' ) end++;
270 if( *end == '\n' ) end++;
271 *use_len = end - data;
272
273 enc = 0;
274
275 if( s2 - s1 >= 22 && memcmp( s1: s1, s2: "Proc-Type: 4,ENCRYPTED", n: 22 ) == 0 )
276 {
277#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
278 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
279 enc++;
280
281 s1 += 22;
282 if( *s1 == '\r' ) s1++;
283 if( *s1 == '\n' ) s1++;
284 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
285
286
287#if defined(MBEDTLS_DES_C)
288 if( s2 - s1 >= 23 && memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
289 {
290 enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
291
292 s1 += 23;
293 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8 ) != 0 )
294 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
295
296 s1 += 16;
297 }
298 else if( s2 - s1 >= 18 && memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
299 {
300 enc_alg = MBEDTLS_CIPHER_DES_CBC;
301
302 s1 += 18;
303 if( s2 - s1 < 16 || pem_get_iv( s1, pem_iv, 8) != 0 )
304 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
305
306 s1 += 16;
307 }
308#endif /* MBEDTLS_DES_C */
309
310#if defined(MBEDTLS_AES_C)
311 if( s2 - s1 >= 14 && memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
312 {
313 if( s2 - s1 < 22 )
314 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
315 else if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
316 enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
317 else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
318 enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
319 else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
320 enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
321 else
322 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
323
324 s1 += 22;
325 if( s2 - s1 < 32 || pem_get_iv( s1, pem_iv, 16 ) != 0 )
326 return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
327
328 s1 += 32;
329 }
330#endif /* MBEDTLS_AES_C */
331
332 if( enc_alg == MBEDTLS_CIPHER_NONE )
333 return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
334
335 if( *s1 == '\r' ) s1++;
336 if( *s1 == '\n' ) s1++;
337 else return( MBEDTLS_ERR_PEM_INVALID_DATA );
338#else
339 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
340#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
341 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
342 }
343
344 if( s1 >= s2 )
345 return( MBEDTLS_ERR_PEM_INVALID_DATA );
346
347 ret = mbedtls_base64_decode( NULL, dlen: 0, olen: &len, src: s1, slen: s2 - s1 );
348
349 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
350 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) );
351
352 if( ( buf = (unsigned char *) mbedtls_calloc( nmemb: 1, size: len ) ) == NULL )
353 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
354
355 if( ( ret = mbedtls_base64_decode( dst: buf, dlen: len, olen: &len, src: s1, slen: s2 - s1 ) ) != 0 )
356 {
357 mbedtls_platform_zeroize( buf, len );
358 mbedtls_free( ptr: buf );
359 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PEM_INVALID_DATA, ret ) );
360 }
361
362 if( enc != 0 )
363 {
364#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
365 ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
366 if( pwd == NULL )
367 {
368 mbedtls_platform_zeroize( buf, len );
369 mbedtls_free( buf );
370 return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
371 }
372
373 ret = 0;
374
375#if defined(MBEDTLS_DES_C)
376 if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
377 ret = pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
378 else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
379 ret = pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
380#endif /* MBEDTLS_DES_C */
381
382#if defined(MBEDTLS_AES_C)
383 if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
384 ret = pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
385 else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
386 ret = pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
387 else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
388 ret = pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
389#endif /* MBEDTLS_AES_C */
390
391 if( ret != 0 )
392 {
393 mbedtls_free( buf );
394 return( ret );
395 }
396
397 /*
398 * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
399 * length bytes (allow 4 to be sure) in all known use cases.
400 *
401 * Use that as a heuristic to try to detect password mismatches.
402 */
403 if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
404 {
405 mbedtls_platform_zeroize( buf, len );
406 mbedtls_free( buf );
407 return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
408 }
409#else
410 mbedtls_platform_zeroize( buf, len );
411 mbedtls_free( ptr: buf );
412 return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
413#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
414 ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
415 }
416
417 ctx->buf = buf;
418 ctx->buflen = len;
419
420 return( 0 );
421}
422
423void mbedtls_pem_free( mbedtls_pem_context *ctx )
424{
425 if ( ctx->buf != NULL )
426 {
427 mbedtls_platform_zeroize( buf: ctx->buf, len: ctx->buflen );
428 mbedtls_free( ptr: ctx->buf );
429 }
430 mbedtls_free( ptr: ctx->info );
431
432 mbedtls_platform_zeroize( buf: ctx, len: sizeof( mbedtls_pem_context ) );
433}
434#endif /* MBEDTLS_PEM_PARSE_C */
435
436#if defined(MBEDTLS_PEM_WRITE_C)
437int mbedtls_pem_write_buffer( const char *header, const char *footer,
438 const unsigned char *der_data, size_t der_len,
439 unsigned char *buf, size_t buf_len, size_t *olen )
440{
441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
442 unsigned char *encode_buf = NULL, *c, *p = buf;
443 size_t len = 0, use_len, add_len = 0;
444
445 mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
446 add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
447
448 if( use_len + add_len > buf_len )
449 {
450 *olen = use_len + add_len;
451 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
452 }
453
454 if( use_len != 0 &&
455 ( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL ) )
456 return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
457
458 if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
459 der_len ) ) != 0 )
460 {
461 mbedtls_free( encode_buf );
462 return( ret );
463 }
464
465 memcpy( p, header, strlen( header ) );
466 p += strlen( header );
467 c = encode_buf;
468
469 while( use_len )
470 {
471 len = ( use_len > 64 ) ? 64 : use_len;
472 memcpy( p, c, len );
473 use_len -= len;
474 p += len;
475 c += len;
476 *p++ = '\n';
477 }
478
479 memcpy( p, footer, strlen( footer ) );
480 p += strlen( footer );
481
482 *p++ = '\0';
483 *olen = p - buf;
484
485 /* Clean any remaining data previously written to the buffer */
486 memset( buf + *olen, 0, buf_len - *olen );
487
488 mbedtls_free( encode_buf );
489 return( 0 );
490}
491#endif /* MBEDTLS_PEM_WRITE_C */
492#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
493
494