pem.c 12.9 KB
Newer Older
1 2 3
/*
 *  Privacy Enhanced Mail (PEM) decoding
 *
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

23
#if !defined(MBEDTLS_CONFIG_FILE)
24
#include "mbedtls/config.h"
25
#else
26
#include MBEDTLS_CONFIG_FILE
27
#endif
28

29
#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
30

31 32 33 34 35 36
#include "mbedtls/pem.h"
#include "mbedtls/base64.h"
#include "mbedtls/des.h"
#include "mbedtls/aes.h"
#include "mbedtls/md5.h"
#include "mbedtls/cipher.h"
37

38 39
#include <string.h>

40
#if defined(MBEDTLS_PLATFORM_C)
41
#include "mbedtls/platform.h"
42
#else
43
#include <stdlib.h>
44
#define mbedtls_calloc    calloc
45
#define mbedtls_free       free
46 47
#endif

48
/* Implementation that should never be optimized out by the compiler */
49
static void mbedtls_zeroize( void *v, size_t n ) {
50 51 52
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

53 54
#if defined(MBEDTLS_PEM_PARSE_C)
void mbedtls_pem_init( mbedtls_pem_context *ctx )
55
{
56
    memset( ctx, 0, sizeof( mbedtls_pem_context ) );
57 58
}

59 60
#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) &&         \
    ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
61 62 63
/*
 * Read a 16-byte hex string and convert it to binary
 */
64 65
static int pem_get_iv( const unsigned char *s, unsigned char *iv,
                       size_t iv_len )
66
{
67
    size_t i, j, k;
68 69 70 71 72 73 74 75

    memset( iv, 0, iv_len );

    for( i = 0; i < iv_len * 2; i++, s++ )
    {
        if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
        if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
        if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
76
            return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
77 78 79 80 81 82 83 84 85

        k = ( ( i & 1 ) != 0 ) ? j : j << 4;

        iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
    }

    return( 0 );
}

86
static void pem_pbkdf1( unsigned char *key, size_t keylen,
87
                        unsigned char *iv,
88
                        const unsigned char *pwd, size_t pwdlen )
89
{
90
    mbedtls_md5_context md5_ctx;
91
    unsigned char md5sum[16];
92
    size_t use_len;
93

94
    mbedtls_md5_init( &md5_ctx );
95

96 97 98
    /*
     * key[ 0..15] = MD5(pwd || IV)
     */
99 100 101 102
    mbedtls_md5_starts( &md5_ctx );
    mbedtls_md5_update( &md5_ctx, pwd, pwdlen );
    mbedtls_md5_update( &md5_ctx, iv,  8 );
    mbedtls_md5_finish( &md5_ctx, md5sum );
103 104 105 106 107

    if( keylen <= 16 )
    {
        memcpy( key, md5sum, keylen );

108 109
        mbedtls_md5_free( &md5_ctx );
        mbedtls_zeroize( md5sum, 16 );
110 111 112 113 114 115 116 117
        return;
    }

    memcpy( key, md5sum, 16 );

    /*
     * key[16..23] = MD5(key[ 0..15] || pwd || IV])
     */
118 119 120 121 122
    mbedtls_md5_starts( &md5_ctx );
    mbedtls_md5_update( &md5_ctx, md5sum,  16 );
    mbedtls_md5_update( &md5_ctx, pwd, pwdlen );
    mbedtls_md5_update( &md5_ctx, iv,  8 );
    mbedtls_md5_finish( &md5_ctx, md5sum );
123 124 125 126 127 128 129

    use_len = 16;
    if( keylen < 32 )
        use_len = keylen - 16;

    memcpy( key + 16, md5sum, use_len );

130 131
    mbedtls_md5_free( &md5_ctx );
    mbedtls_zeroize( md5sum, 16 );
132 133
}

134
#if defined(MBEDTLS_DES_C)
135 136 137 138
/*
 * Decrypt with DES-CBC, using PBKDF1 for key derivation
 */
static void pem_des_decrypt( unsigned char des_iv[8],
139 140
                               unsigned char *buf, size_t buflen,
                               const unsigned char *pwd, size_t pwdlen )
141
{
142
    mbedtls_des_context des_ctx;
143 144
    unsigned char des_key[8];

145
    mbedtls_des_init( &des_ctx );
146

147 148
    pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen );

149 150
    mbedtls_des_setkey_dec( &des_ctx, des_key );
    mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, buflen,
151 152
                     des_iv, buf, buf );

153 154
    mbedtls_des_free( &des_ctx );
    mbedtls_zeroize( des_key, 8 );
155 156 157 158 159 160
}

/*
 * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
 */
static void pem_des3_decrypt( unsigned char des3_iv[8],
161 162
                               unsigned char *buf, size_t buflen,
                               const unsigned char *pwd, size_t pwdlen )
163
{
164
    mbedtls_des3_context des3_ctx;
165 166
    unsigned char des3_key[24];

167
    mbedtls_des3_init( &des3_ctx );
168

169 170
    pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen );

171 172
    mbedtls_des3_set3key_dec( &des3_ctx, des3_key );
    mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
173 174
                     des3_iv, buf, buf );

175 176
    mbedtls_des3_free( &des3_ctx );
    mbedtls_zeroize( des3_key, 24 );
177
}
178
#endif /* MBEDTLS_DES_C */
179

180
#if defined(MBEDTLS_AES_C)
181 182 183
/*
 * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
 */
184 185 186
static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
                               unsigned char *buf, size_t buflen,
                               const unsigned char *pwd, size_t pwdlen )
187
{
188
    mbedtls_aes_context aes_ctx;
189 190
    unsigned char aes_key[32];

191
    mbedtls_aes_init( &aes_ctx );
192

193 194
    pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen );

195 196
    mbedtls_aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 );
    mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
197 198
                     aes_iv, buf, buf );

199 200
    mbedtls_aes_free( &aes_ctx );
    mbedtls_zeroize( aes_key, keylen );
201
}
202
#endif /* MBEDTLS_AES_C */
203

204 205
#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
          ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
206

207
int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
208 209
                     const unsigned char *data, const unsigned char *pwd,
                     size_t pwdlen, size_t *use_len )
210
{
211 212
    int ret, enc;
    size_t len;
213
    unsigned char *buf;
214
    const unsigned char *s1, *s2, *end;
215 216
#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) &&         \
    ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
217
    unsigned char pem_iv[16];
218
    mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
219 220 221
#else
    ((void) pwd);
    ((void) pwdlen);
222 223
#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
          ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
224 225

    if( ctx == NULL )
226
        return( MBEDTLS_ERR_PEM_BAD_INPUT_DATA );
227

228
    s1 = (unsigned char *) strstr( (const char *) data, header );
229 230

    if( s1 == NULL )
231
        return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
232

233
    s2 = (unsigned char *) strstr( (const char *) data, footer );
234 235

    if( s2 == NULL || s2 <= s1 )
236
        return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
237 238 239 240

    s1 += strlen( header );
    if( *s1 == '\r' ) s1++;
    if( *s1 == '\n' ) s1++;
241
    else return( MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
242 243 244 245 246 247

    end = s2;
    end += strlen( footer );
    if( *end == '\r' ) end++;
    if( *end == '\n' ) end++;
    *use_len = end - data;
248 249 250 251 252

    enc = 0;

    if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
    {
253 254
#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) &&         \
    ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
255 256 257 258 259
        enc++;

        s1 += 22;
        if( *s1 == '\r' ) s1++;
        if( *s1 == '\n' ) s1++;
260
        else return( MBEDTLS_ERR_PEM_INVALID_DATA );
261 262


263
#if defined(MBEDTLS_DES_C)
264 265
        if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
        {
266
            enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
267 268 269

            s1 += 23;
            if( pem_get_iv( s1, pem_iv, 8 ) != 0 )
270
                return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
271 272 273 274 275

            s1 += 16;
        }
        else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
        {
276
            enc_alg = MBEDTLS_CIPHER_DES_CBC;
277 278 279

            s1 += 18;
            if( pem_get_iv( s1, pem_iv, 8) != 0 )
280
                return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
281 282 283

            s1 += 16;
        }
284
#endif /* MBEDTLS_DES_C */
285

286
#if defined(MBEDTLS_AES_C)
287 288 289
        if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
        {
            if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
290
                enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
291
            else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
292
                enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
293
            else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
294
                enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
295
            else
296
                return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
297 298 299

            s1 += 22;
            if( pem_get_iv( s1, pem_iv, 16 ) != 0 )
300
                return( MBEDTLS_ERR_PEM_INVALID_ENC_IV );
301 302 303

            s1 += 32;
        }
304
#endif /* MBEDTLS_AES_C */
305

306 307
        if( enc_alg == MBEDTLS_CIPHER_NONE )
            return( MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG );
308 309 310

        if( *s1 == '\r' ) s1++;
        if( *s1 == '\n' ) s1++;
311
        else return( MBEDTLS_ERR_PEM_INVALID_DATA );
312
#else
313 314 315
        return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
          ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
316 317
    }

318
    ret = mbedtls_base64_decode( NULL, 0, &len, s1, s2 - s1 );
319

320 321
    if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
        return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
322

323
    if( ( buf = mbedtls_calloc( 1, len ) ) == NULL )
324
        return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
325

326
    if( ( ret = mbedtls_base64_decode( buf, len, &len, s1, s2 - s1 ) ) != 0 )
327
    {
328 329
        mbedtls_free( buf );
        return( MBEDTLS_ERR_PEM_INVALID_DATA + ret );
330
    }
331

332 333
    if( enc != 0 )
    {
334 335
#if defined(MBEDTLS_MD5_C) && defined(MBEDTLS_CIPHER_MODE_CBC) &&         \
    ( defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C) )
336 337
        if( pwd == NULL )
        {
338 339
            mbedtls_free( buf );
            return( MBEDTLS_ERR_PEM_PASSWORD_REQUIRED );
340 341
        }

342 343
#if defined(MBEDTLS_DES_C)
        if( enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC )
344
            pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
345
        else if( enc_alg == MBEDTLS_CIPHER_DES_CBC )
346
            pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
347
#endif /* MBEDTLS_DES_C */
348

349 350
#if defined(MBEDTLS_AES_C)
        if( enc_alg == MBEDTLS_CIPHER_AES_128_CBC )
351
            pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
352
        else if( enc_alg == MBEDTLS_CIPHER_AES_192_CBC )
353
            pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
354
        else if( enc_alg == MBEDTLS_CIPHER_AES_256_CBC )
355
            pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
356
#endif /* MBEDTLS_AES_C */
357

358
        /*
359 360 361 362
         * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
         * length bytes (allow 4 to be sure) in all known use cases.
         *
         * Use that as heurisitic to try detecting password mismatchs.
363
         */
364
        if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
365
        {
366 367
            mbedtls_free( buf );
            return( MBEDTLS_ERR_PEM_PASSWORD_MISMATCH );
368 369
        }
#else
370 371 372 373
        mbedtls_free( buf );
        return( MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE );
#endif /* MBEDTLS_MD5_C && MBEDTLS_CIPHER_MODE_CBC &&
          ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
374 375 376 377 378 379 380 381
    }

    ctx->buf = buf;
    ctx->buflen = len;

    return( 0 );
}

382
void mbedtls_pem_free( mbedtls_pem_context *ctx )
383
{
384 385
    mbedtls_free( ctx->buf );
    mbedtls_free( ctx->info );
386

387
    mbedtls_zeroize( ctx, sizeof( mbedtls_pem_context ) );
388
}
389
#endif /* MBEDTLS_PEM_PARSE_C */
390

391 392
#if defined(MBEDTLS_PEM_WRITE_C)
int mbedtls_pem_write_buffer( const char *header, const char *footer,
393 394 395 396 397
                      const unsigned char *der_data, size_t der_len,
                      unsigned char *buf, size_t buf_len, size_t *olen )
{
    int ret;
    unsigned char *encode_buf, *c, *p = buf;
398
    size_t len = 0, use_len, add_len = 0;
399

400
    mbedtls_base64_encode( NULL, 0, &use_len, der_data, der_len );
401 402
    add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;

403 404 405
    if( use_len + add_len > buf_len )
    {
        *olen = use_len + add_len;
406
        return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
407 408
    }

409
    if( ( encode_buf = mbedtls_calloc( 1, use_len ) ) == NULL )
410
        return( MBEDTLS_ERR_PEM_ALLOC_FAILED );
411

412
    if( ( ret = mbedtls_base64_encode( encode_buf, use_len, &use_len, der_data,
413 414
                               der_len ) ) != 0 )
    {
415
        mbedtls_free( encode_buf );
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        return( ret );
    }

    memcpy( p, header, strlen( header ) );
    p += strlen( header );
    c = encode_buf;

    while( use_len )
    {
        len = ( use_len > 64 ) ? 64 : use_len;
        memcpy( p, c, len );
        use_len -= len;
        p += len;
        c += len;
        *p++ = '\n';
    }

    memcpy( p, footer, strlen( footer ) );
    p += strlen( footer );

    *p++ = '\0';
    *olen = p - buf;

439
    mbedtls_free( encode_buf );
440 441
    return( 0 );
}
442 443
#endif /* MBEDTLS_PEM_WRITE_C */
#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */