mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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