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