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