mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pem.c Source File

pem.c

00001 /*
00002  *  Privacy Enhanced Mail (PEM) decoding
00003  *
00004  *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
00005  *
00006  *  This file is part of mbed TLS (https://tls.mbed.org)
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License along
00019  *  with this program; if not, write to the Free Software Foundation, Inc.,
00020  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021  */
00022 
00023 #if !defined(POLARSSL_CONFIG_FILE)
00024 #include "polarssl/config.h"
00025 #else
00026 #include POLARSSL_CONFIG_FILE
00027 #endif
00028 
00029 #if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C)
00030 
00031 #include "polarssl/pem.h"
00032 #include "polarssl/base64.h"
00033 #include "polarssl/des.h"
00034 #include "polarssl/aes.h"
00035 #include "polarssl/md5.h"
00036 #include "polarssl/cipher.h"
00037 
00038 #include <string.h>
00039 
00040 #if defined(POLARSSL_PLATFORM_C)
00041 #include "polarssl/platform.h"
00042 #else
00043 #include <stdlib.h>
00044 #define polarssl_malloc     malloc
00045 #define polarssl_free       free
00046 #endif
00047 
00048 /* Implementation that should never be optimized out by the compiler */
00049 static void polarssl_zeroize( void *v, size_t n ) {
00050     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
00051 }
00052 
00053 #if defined(POLARSSL_PEM_PARSE_C)
00054 void pem_init( pem_context *ctx )
00055 {
00056     memset( ctx, 0, sizeof( pem_context ) );
00057 }
00058 
00059 #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) &&         \
00060     ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
00061 /*
00062  * Read a 16-byte hex string and convert it to binary
00063  */
00064 static int pem_get_iv( const unsigned char *s, unsigned char *iv,
00065                        size_t iv_len )
00066 {
00067     size_t i, j, k;
00068 
00069     memset( iv, 0, iv_len );
00070 
00071     for( i = 0; i < iv_len * 2; i++, s++ )
00072     {
00073         if( *s >= '0' && *s <= '9' ) j = *s - '0'; else
00074         if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else
00075         if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else
00076             return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
00077 
00078         k = ( ( i & 1 ) != 0 ) ? j : j << 4;
00079 
00080         iv[i >> 1] = (unsigned char)( iv[i >> 1] | k );
00081     }
00082 
00083     return( 0 );
00084 }
00085 
00086 static void pem_pbkdf1( unsigned char *key, size_t keylen,
00087                         unsigned char *iv,
00088                         const unsigned char *pwd, size_t pwdlen )
00089 {
00090     md5_context md5_ctx;
00091     unsigned char md5sum[16];
00092     size_t use_len;
00093 
00094     md5_init( &md5_ctx );
00095 
00096     /*
00097      * key[ 0..15] = MD5(pwd || IV)
00098      */
00099     md5_starts( &md5_ctx );
00100     md5_update( &md5_ctx, pwd, pwdlen );
00101     md5_update( &md5_ctx, iv,  8 );
00102     md5_finish( &md5_ctx, md5sum );
00103 
00104     if( keylen <= 16 )
00105     {
00106         memcpy( key, md5sum, keylen );
00107 
00108         md5_free( &md5_ctx );
00109         polarssl_zeroize( md5sum, 16 );
00110         return;
00111     }
00112 
00113     memcpy( key, md5sum, 16 );
00114 
00115     /*
00116      * key[16..23] = MD5(key[ 0..15] || pwd || IV])
00117      */
00118     md5_starts( &md5_ctx );
00119     md5_update( &md5_ctx, md5sum,  16 );
00120     md5_update( &md5_ctx, pwd, pwdlen );
00121     md5_update( &md5_ctx, iv,  8 );
00122     md5_finish( &md5_ctx, md5sum );
00123 
00124     use_len = 16;
00125     if( keylen < 32 )
00126         use_len = keylen - 16;
00127 
00128     memcpy( key + 16, md5sum, use_len );
00129 
00130     md5_free( &md5_ctx );
00131     polarssl_zeroize( md5sum, 16 );
00132 }
00133 
00134 #if defined(POLARSSL_DES_C)
00135 /*
00136  * Decrypt with DES-CBC, using PBKDF1 for key derivation
00137  */
00138 static void pem_des_decrypt( unsigned char des_iv[8],
00139                                unsigned char *buf, size_t buflen,
00140                                const unsigned char *pwd, size_t pwdlen )
00141 {
00142     des_context des_ctx;
00143     unsigned char des_key[8];
00144 
00145     des_init( &des_ctx );
00146 
00147     pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen );
00148 
00149     des_setkey_dec( &des_ctx, des_key );
00150     des_crypt_cbc( &des_ctx, DES_DECRYPT, buflen,
00151                      des_iv, buf, buf );
00152 
00153     des_free( &des_ctx );
00154     polarssl_zeroize( des_key, 8 );
00155 }
00156 
00157 /*
00158  * Decrypt with 3DES-CBC, using PBKDF1 for key derivation
00159  */
00160 static void pem_des3_decrypt( unsigned char des3_iv[8],
00161                                unsigned char *buf, size_t buflen,
00162                                const unsigned char *pwd, size_t pwdlen )
00163 {
00164     des3_context des3_ctx;
00165     unsigned char des3_key[24];
00166 
00167     des3_init( &des3_ctx );
00168 
00169     pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen );
00170 
00171     des3_set3key_dec( &des3_ctx, des3_key );
00172     des3_crypt_cbc( &des3_ctx, DES_DECRYPT, buflen,
00173                      des3_iv, buf, buf );
00174 
00175     des3_free( &des3_ctx );
00176     polarssl_zeroize( des3_key, 24 );
00177 }
00178 #endif /* POLARSSL_DES_C */
00179 
00180 #if defined(POLARSSL_AES_C)
00181 /*
00182  * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
00183  */
00184 static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen,
00185                                unsigned char *buf, size_t buflen,
00186                                const unsigned char *pwd, size_t pwdlen )
00187 {
00188     aes_context aes_ctx;
00189     unsigned char aes_key[32];
00190 
00191     aes_init( &aes_ctx );
00192 
00193     pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen );
00194 
00195     aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 );
00196     aes_crypt_cbc( &aes_ctx, AES_DECRYPT, buflen,
00197                      aes_iv, buf, buf );
00198 
00199     aes_free( &aes_ctx );
00200     polarssl_zeroize( aes_key, keylen );
00201 }
00202 #endif /* POLARSSL_AES_C */
00203 
00204 #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
00205           ( POLARSSL_AES_C || POLARSSL_DES_C ) */
00206 
00207 int pem_read_buffer( pem_context *ctx, const char *header, const char *footer,
00208                      const unsigned char *data, const unsigned char *pwd,
00209                      size_t pwdlen, size_t *use_len )
00210 {
00211     int ret, enc;
00212     size_t len;
00213     unsigned char *buf;
00214     const unsigned char *s1, *s2, *end;
00215 #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) &&         \
00216     ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
00217     unsigned char pem_iv[16];
00218     cipher_type_t enc_alg = POLARSSL_CIPHER_NONE;
00219 #else
00220     ((void) pwd);
00221     ((void) pwdlen);
00222 #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
00223           ( POLARSSL_AES_C || POLARSSL_DES_C ) */
00224 
00225     if( ctx == NULL )
00226         return( POLARSSL_ERR_PEM_BAD_INPUT_DATA );
00227 
00228     s1 = (unsigned char *) strstr( (const char *) data, header );
00229 
00230     if( s1 == NULL )
00231         return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
00232 
00233     s2 = (unsigned char *) strstr( (const char *) data, footer );
00234 
00235     if( s2 == NULL || s2 <= s1 )
00236         return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
00237 
00238     s1 += strlen( header );
00239     if( *s1 == '\r' ) s1++;
00240     if( *s1 == '\n' ) s1++;
00241     else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT );
00242 
00243     end = s2;
00244     end += strlen( footer );
00245     if( *end == '\r' ) end++;
00246     if( *end == '\n' ) end++;
00247     *use_len = end - data;
00248 
00249     enc = 0;
00250 
00251     if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
00252     {
00253 #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) &&         \
00254     ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
00255         enc++;
00256 
00257         s1 += 22;
00258         if( *s1 == '\r' ) s1++;
00259         if( *s1 == '\n' ) s1++;
00260         else return( POLARSSL_ERR_PEM_INVALID_DATA );
00261 
00262 
00263 #if defined(POLARSSL_DES_C)
00264         if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 )
00265         {
00266             enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC;
00267 
00268             s1 += 23;
00269             if( pem_get_iv( s1, pem_iv, 8 ) != 0 )
00270                 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
00271 
00272             s1 += 16;
00273         }
00274         else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 )
00275         {
00276             enc_alg = POLARSSL_CIPHER_DES_CBC;
00277 
00278             s1 += 18;
00279             if( pem_get_iv( s1, pem_iv, 8) != 0 )
00280                 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
00281 
00282             s1 += 16;
00283         }
00284 #endif /* POLARSSL_DES_C */
00285 
00286 #if defined(POLARSSL_AES_C)
00287         if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 )
00288         {
00289             if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 )
00290                 enc_alg = POLARSSL_CIPHER_AES_128_CBC;
00291             else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 )
00292                 enc_alg = POLARSSL_CIPHER_AES_192_CBC;
00293             else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 )
00294                 enc_alg = POLARSSL_CIPHER_AES_256_CBC;
00295             else
00296                 return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
00297 
00298             s1 += 22;
00299             if( pem_get_iv( s1, pem_iv, 16 ) != 0 )
00300                 return( POLARSSL_ERR_PEM_INVALID_ENC_IV );
00301 
00302             s1 += 32;
00303         }
00304 #endif /* POLARSSL_AES_C */
00305 
00306         if( enc_alg == POLARSSL_CIPHER_NONE )
00307             return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG );
00308 
00309         if( *s1 == '\r' ) s1++;
00310         if( *s1 == '\n' ) s1++;
00311         else return( POLARSSL_ERR_PEM_INVALID_DATA );
00312 #else
00313         return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
00314 #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
00315           ( POLARSSL_AES_C || POLARSSL_DES_C ) */
00316     }
00317 
00318     len = 0;
00319     ret = base64_decode( NULL, &len, s1, s2 - s1 );
00320 
00321     if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
00322         return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
00323 
00324     if( ( buf = polarssl_malloc( len ) ) == NULL )
00325         return( POLARSSL_ERR_PEM_MALLOC_FAILED );
00326 
00327     if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
00328     {
00329         polarssl_free( buf );
00330         return( POLARSSL_ERR_PEM_INVALID_DATA + ret );
00331     }
00332 
00333     if( enc != 0 )
00334     {
00335 #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) &&         \
00336     ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) )
00337         if( pwd == NULL )
00338         {
00339             polarssl_free( buf );
00340             return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED );
00341         }
00342 
00343 #if defined(POLARSSL_DES_C)
00344         if( enc_alg == POLARSSL_CIPHER_DES_EDE3_CBC )
00345             pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen );
00346         else if( enc_alg == POLARSSL_CIPHER_DES_CBC )
00347             pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen );
00348 #endif /* POLARSSL_DES_C */
00349 
00350 #if defined(POLARSSL_AES_C)
00351         if( enc_alg == POLARSSL_CIPHER_AES_128_CBC )
00352             pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen );
00353         else if( enc_alg == POLARSSL_CIPHER_AES_192_CBC )
00354             pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen );
00355         else if( enc_alg == POLARSSL_CIPHER_AES_256_CBC )
00356             pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen );
00357 #endif /* POLARSSL_AES_C */
00358 
00359         /*
00360          * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3
00361          * length bytes (allow 4 to be sure) in all known use cases.
00362          *
00363          * Use that as heurisitic to try detecting password mismatchs.
00364          */
00365         if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 )
00366         {
00367             polarssl_free( buf );
00368             return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH );
00369         }
00370 #else
00371         polarssl_free( buf );
00372         return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE );
00373 #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC &&
00374           ( POLARSSL_AES_C || POLARSSL_DES_C ) */
00375     }
00376 
00377     ctx->buf  = buf;
00378     ctx->buflen  = len;
00379 
00380     return( 0 );
00381 }
00382 
00383 void pem_free( pem_context *ctx )
00384 {
00385     polarssl_free( ctx->buf  );
00386     polarssl_free( ctx->info  );
00387 
00388     polarssl_zeroize( ctx, sizeof( pem_context ) );
00389 }
00390 #endif /* POLARSSL_PEM_PARSE_C */
00391 
00392 #if defined(POLARSSL_PEM_WRITE_C)
00393 int pem_write_buffer( const char *header, const char *footer,
00394                       const unsigned char *der_data, size_t der_len,
00395                       unsigned char *buf, size_t buf_len, size_t *olen )
00396 {
00397     int ret;
00398     unsigned char *encode_buf, *c, *p = buf;
00399     size_t len = 0, use_len = 0, add_len = 0;
00400 
00401     base64_encode( NULL, &use_len, der_data, der_len );
00402     add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
00403 
00404     if( use_len + add_len > buf_len )
00405     {
00406         *olen = use_len + add_len;
00407         return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
00408     }
00409 
00410     if( ( encode_buf = polarssl_malloc( use_len ) ) == NULL )
00411         return( POLARSSL_ERR_PEM_MALLOC_FAILED );
00412 
00413     if( ( ret = base64_encode( encode_buf, &use_len, der_data,
00414                                der_len ) ) != 0 )
00415     {
00416         polarssl_free( encode_buf );
00417         return( ret );
00418     }
00419 
00420     memcpy( p, header, strlen( header ) );
00421     p += strlen( header );
00422     c = encode_buf;
00423 
00424     while( use_len )
00425     {
00426         len = ( use_len > 64 ) ? 64 : use_len;
00427         memcpy( p, c, len );
00428         use_len -= len;
00429         p += len;
00430         c += len;
00431         *p++ = '\n';
00432     }
00433 
00434     memcpy( p, footer, strlen( footer ) );
00435     p += strlen( footer );
00436 
00437     *p++ = '\0';
00438     *olen = p - buf;
00439 
00440     polarssl_free( encode_buf );
00441     return( 0 );
00442 }
00443 #endif /* POLARSSL_PEM_WRITE_C */
00444 #endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */
00445