Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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