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