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