mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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