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