Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/pem.c@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /* |
HannesTschofenig | 0:796d0f61a05b | 2 | * Privacy Enhanced Mail (PEM) decoding |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | |
HannesTschofenig | 0:796d0f61a05b | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 27 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 28 | #else |
HannesTschofenig | 0:796d0f61a05b | 29 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 30 | #endif |
HannesTschofenig | 0:796d0f61a05b | 31 | |
HannesTschofenig | 0:796d0f61a05b | 32 | #if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C) |
HannesTschofenig | 0:796d0f61a05b | 33 | #include "polarssl/pem.h" |
HannesTschofenig | 0:796d0f61a05b | 34 | #include "polarssl/base64.h" |
HannesTschofenig | 0:796d0f61a05b | 35 | #include "polarssl/des.h" |
HannesTschofenig | 0:796d0f61a05b | 36 | #include "polarssl/aes.h" |
HannesTschofenig | 0:796d0f61a05b | 37 | #include "polarssl/md5.h" |
HannesTschofenig | 0:796d0f61a05b | 38 | #include "polarssl/cipher.h" |
HannesTschofenig | 0:796d0f61a05b | 39 | |
HannesTschofenig | 0:796d0f61a05b | 40 | #if defined(POLARSSL_PLATFORM_C) |
HannesTschofenig | 0:796d0f61a05b | 41 | #include "polarssl/platform.h" |
HannesTschofenig | 0:796d0f61a05b | 42 | #else |
HannesTschofenig | 0:796d0f61a05b | 43 | #define polarssl_malloc malloc |
HannesTschofenig | 0:796d0f61a05b | 44 | #define polarssl_free free |
HannesTschofenig | 0:796d0f61a05b | 45 | #endif |
HannesTschofenig | 0:796d0f61a05b | 46 | |
HannesTschofenig | 0:796d0f61a05b | 47 | #include <stdlib.h> |
HannesTschofenig | 0:796d0f61a05b | 48 | |
HannesTschofenig | 0:796d0f61a05b | 49 | #if defined(POLARSSL_PEM_PARSE_C) |
HannesTschofenig | 0:796d0f61a05b | 50 | void pem_init( pem_context *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 51 | { |
HannesTschofenig | 0:796d0f61a05b | 52 | memset( ctx, 0, sizeof( pem_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 53 | } |
HannesTschofenig | 0:796d0f61a05b | 54 | |
HannesTschofenig | 0:796d0f61a05b | 55 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
HannesTschofenig | 0:796d0f61a05b | 56 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
HannesTschofenig | 0:796d0f61a05b | 57 | /* |
HannesTschofenig | 0:796d0f61a05b | 58 | * Read a 16-byte hex string and convert it to binary |
HannesTschofenig | 0:796d0f61a05b | 59 | */ |
HannesTschofenig | 0:796d0f61a05b | 60 | static int pem_get_iv( const unsigned char *s, unsigned char *iv, |
HannesTschofenig | 0:796d0f61a05b | 61 | size_t iv_len ) |
HannesTschofenig | 0:796d0f61a05b | 62 | { |
HannesTschofenig | 0:796d0f61a05b | 63 | size_t i, j, k; |
HannesTschofenig | 0:796d0f61a05b | 64 | |
HannesTschofenig | 0:796d0f61a05b | 65 | memset( iv, 0, iv_len ); |
HannesTschofenig | 0:796d0f61a05b | 66 | |
HannesTschofenig | 0:796d0f61a05b | 67 | for( i = 0; i < iv_len * 2; i++, s++ ) |
HannesTschofenig | 0:796d0f61a05b | 68 | { |
HannesTschofenig | 0:796d0f61a05b | 69 | if( *s >= '0' && *s <= '9' ) j = *s - '0'; else |
HannesTschofenig | 0:796d0f61a05b | 70 | if( *s >= 'A' && *s <= 'F' ) j = *s - '7'; else |
HannesTschofenig | 0:796d0f61a05b | 71 | if( *s >= 'a' && *s <= 'f' ) j = *s - 'W'; else |
HannesTschofenig | 0:796d0f61a05b | 72 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
HannesTschofenig | 0:796d0f61a05b | 73 | |
HannesTschofenig | 0:796d0f61a05b | 74 | k = ( ( i & 1 ) != 0 ) ? j : j << 4; |
HannesTschofenig | 0:796d0f61a05b | 75 | |
HannesTschofenig | 0:796d0f61a05b | 76 | iv[i >> 1] = (unsigned char)( iv[i >> 1] | k ); |
HannesTschofenig | 0:796d0f61a05b | 77 | } |
HannesTschofenig | 0:796d0f61a05b | 78 | |
HannesTschofenig | 0:796d0f61a05b | 79 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 80 | } |
HannesTschofenig | 0:796d0f61a05b | 81 | |
HannesTschofenig | 0:796d0f61a05b | 82 | static void pem_pbkdf1( unsigned char *key, size_t keylen, |
HannesTschofenig | 0:796d0f61a05b | 83 | unsigned char *iv, |
HannesTschofenig | 0:796d0f61a05b | 84 | const unsigned char *pwd, size_t pwdlen ) |
HannesTschofenig | 0:796d0f61a05b | 85 | { |
HannesTschofenig | 0:796d0f61a05b | 86 | md5_context md5_ctx; |
HannesTschofenig | 0:796d0f61a05b | 87 | unsigned char md5sum[16]; |
HannesTschofenig | 0:796d0f61a05b | 88 | size_t use_len; |
HannesTschofenig | 0:796d0f61a05b | 89 | |
HannesTschofenig | 0:796d0f61a05b | 90 | /* |
HannesTschofenig | 0:796d0f61a05b | 91 | * key[ 0..15] = MD5(pwd || IV) |
HannesTschofenig | 0:796d0f61a05b | 92 | */ |
HannesTschofenig | 0:796d0f61a05b | 93 | md5_starts( &md5_ctx ); |
HannesTschofenig | 0:796d0f61a05b | 94 | md5_update( &md5_ctx, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 95 | md5_update( &md5_ctx, iv, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 96 | md5_finish( &md5_ctx, md5sum ); |
HannesTschofenig | 0:796d0f61a05b | 97 | |
HannesTschofenig | 0:796d0f61a05b | 98 | if( keylen <= 16 ) |
HannesTschofenig | 0:796d0f61a05b | 99 | { |
HannesTschofenig | 0:796d0f61a05b | 100 | memcpy( key, md5sum, keylen ); |
HannesTschofenig | 0:796d0f61a05b | 101 | |
HannesTschofenig | 0:796d0f61a05b | 102 | memset( &md5_ctx, 0, sizeof( md5_ctx ) ); |
HannesTschofenig | 0:796d0f61a05b | 103 | memset( md5sum, 0, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 104 | return; |
HannesTschofenig | 0:796d0f61a05b | 105 | } |
HannesTschofenig | 0:796d0f61a05b | 106 | |
HannesTschofenig | 0:796d0f61a05b | 107 | memcpy( key, md5sum, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 108 | |
HannesTschofenig | 0:796d0f61a05b | 109 | /* |
HannesTschofenig | 0:796d0f61a05b | 110 | * key[16..23] = MD5(key[ 0..15] || pwd || IV]) |
HannesTschofenig | 0:796d0f61a05b | 111 | */ |
HannesTschofenig | 0:796d0f61a05b | 112 | md5_starts( &md5_ctx ); |
HannesTschofenig | 0:796d0f61a05b | 113 | md5_update( &md5_ctx, md5sum, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 114 | md5_update( &md5_ctx, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 115 | md5_update( &md5_ctx, iv, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 116 | md5_finish( &md5_ctx, md5sum ); |
HannesTschofenig | 0:796d0f61a05b | 117 | |
HannesTschofenig | 0:796d0f61a05b | 118 | use_len = 16; |
HannesTschofenig | 0:796d0f61a05b | 119 | if( keylen < 32 ) |
HannesTschofenig | 0:796d0f61a05b | 120 | use_len = keylen - 16; |
HannesTschofenig | 0:796d0f61a05b | 121 | |
HannesTschofenig | 0:796d0f61a05b | 122 | memcpy( key + 16, md5sum, use_len ); |
HannesTschofenig | 0:796d0f61a05b | 123 | |
HannesTschofenig | 0:796d0f61a05b | 124 | memset( &md5_ctx, 0, sizeof( md5_ctx ) ); |
HannesTschofenig | 0:796d0f61a05b | 125 | memset( md5sum, 0, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 126 | } |
HannesTschofenig | 0:796d0f61a05b | 127 | |
HannesTschofenig | 0:796d0f61a05b | 128 | #if defined(POLARSSL_DES_C) |
HannesTschofenig | 0:796d0f61a05b | 129 | /* |
HannesTschofenig | 0:796d0f61a05b | 130 | * Decrypt with DES-CBC, using PBKDF1 for key derivation |
HannesTschofenig | 0:796d0f61a05b | 131 | */ |
HannesTschofenig | 0:796d0f61a05b | 132 | static void pem_des_decrypt( unsigned char des_iv[8], |
HannesTschofenig | 0:796d0f61a05b | 133 | unsigned char *buf, size_t buflen, |
HannesTschofenig | 0:796d0f61a05b | 134 | const unsigned char *pwd, size_t pwdlen ) |
HannesTschofenig | 0:796d0f61a05b | 135 | { |
HannesTschofenig | 0:796d0f61a05b | 136 | des_context des_ctx; |
HannesTschofenig | 0:796d0f61a05b | 137 | unsigned char des_key[8]; |
HannesTschofenig | 0:796d0f61a05b | 138 | |
HannesTschofenig | 0:796d0f61a05b | 139 | pem_pbkdf1( des_key, 8, des_iv, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 140 | |
HannesTschofenig | 0:796d0f61a05b | 141 | des_setkey_dec( &des_ctx, des_key ); |
HannesTschofenig | 0:796d0f61a05b | 142 | des_crypt_cbc( &des_ctx, DES_DECRYPT, buflen, |
HannesTschofenig | 0:796d0f61a05b | 143 | des_iv, buf, buf ); |
HannesTschofenig | 0:796d0f61a05b | 144 | |
HannesTschofenig | 0:796d0f61a05b | 145 | memset( &des_ctx, 0, sizeof( des_ctx ) ); |
HannesTschofenig | 0:796d0f61a05b | 146 | memset( des_key, 0, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 147 | } |
HannesTschofenig | 0:796d0f61a05b | 148 | |
HannesTschofenig | 0:796d0f61a05b | 149 | /* |
HannesTschofenig | 0:796d0f61a05b | 150 | * Decrypt with 3DES-CBC, using PBKDF1 for key derivation |
HannesTschofenig | 0:796d0f61a05b | 151 | */ |
HannesTschofenig | 0:796d0f61a05b | 152 | static void pem_des3_decrypt( unsigned char des3_iv[8], |
HannesTschofenig | 0:796d0f61a05b | 153 | unsigned char *buf, size_t buflen, |
HannesTschofenig | 0:796d0f61a05b | 154 | const unsigned char *pwd, size_t pwdlen ) |
HannesTschofenig | 0:796d0f61a05b | 155 | { |
HannesTschofenig | 0:796d0f61a05b | 156 | des3_context des3_ctx; |
HannesTschofenig | 0:796d0f61a05b | 157 | unsigned char des3_key[24]; |
HannesTschofenig | 0:796d0f61a05b | 158 | |
HannesTschofenig | 0:796d0f61a05b | 159 | pem_pbkdf1( des3_key, 24, des3_iv, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 160 | |
HannesTschofenig | 0:796d0f61a05b | 161 | des3_set3key_dec( &des3_ctx, des3_key ); |
HannesTschofenig | 0:796d0f61a05b | 162 | des3_crypt_cbc( &des3_ctx, DES_DECRYPT, buflen, |
HannesTschofenig | 0:796d0f61a05b | 163 | des3_iv, buf, buf ); |
HannesTschofenig | 0:796d0f61a05b | 164 | |
HannesTschofenig | 0:796d0f61a05b | 165 | memset( &des3_ctx, 0, sizeof( des3_ctx ) ); |
HannesTschofenig | 0:796d0f61a05b | 166 | memset( des3_key, 0, 24 ); |
HannesTschofenig | 0:796d0f61a05b | 167 | } |
HannesTschofenig | 0:796d0f61a05b | 168 | #endif /* POLARSSL_DES_C */ |
HannesTschofenig | 0:796d0f61a05b | 169 | |
HannesTschofenig | 0:796d0f61a05b | 170 | #if defined(POLARSSL_AES_C) |
HannesTschofenig | 0:796d0f61a05b | 171 | /* |
HannesTschofenig | 0:796d0f61a05b | 172 | * Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation |
HannesTschofenig | 0:796d0f61a05b | 173 | */ |
HannesTschofenig | 0:796d0f61a05b | 174 | static void pem_aes_decrypt( unsigned char aes_iv[16], unsigned int keylen, |
HannesTschofenig | 0:796d0f61a05b | 175 | unsigned char *buf, size_t buflen, |
HannesTschofenig | 0:796d0f61a05b | 176 | const unsigned char *pwd, size_t pwdlen ) |
HannesTschofenig | 0:796d0f61a05b | 177 | { |
HannesTschofenig | 0:796d0f61a05b | 178 | aes_context aes_ctx; |
HannesTschofenig | 0:796d0f61a05b | 179 | unsigned char aes_key[32]; |
HannesTschofenig | 0:796d0f61a05b | 180 | |
HannesTschofenig | 0:796d0f61a05b | 181 | pem_pbkdf1( aes_key, keylen, aes_iv, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 182 | |
HannesTschofenig | 0:796d0f61a05b | 183 | aes_setkey_dec( &aes_ctx, aes_key, keylen * 8 ); |
HannesTschofenig | 0:796d0f61a05b | 184 | aes_crypt_cbc( &aes_ctx, AES_DECRYPT, buflen, |
HannesTschofenig | 0:796d0f61a05b | 185 | aes_iv, buf, buf ); |
HannesTschofenig | 0:796d0f61a05b | 186 | |
HannesTschofenig | 0:796d0f61a05b | 187 | memset( &aes_ctx, 0, sizeof( aes_ctx ) ); |
HannesTschofenig | 0:796d0f61a05b | 188 | memset( aes_key, 0, keylen ); |
HannesTschofenig | 0:796d0f61a05b | 189 | } |
HannesTschofenig | 0:796d0f61a05b | 190 | #endif /* POLARSSL_AES_C */ |
HannesTschofenig | 0:796d0f61a05b | 191 | |
HannesTschofenig | 0:796d0f61a05b | 192 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
HannesTschofenig | 0:796d0f61a05b | 193 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
HannesTschofenig | 0:796d0f61a05b | 194 | |
HannesTschofenig | 0:796d0f61a05b | 195 | int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, |
HannesTschofenig | 0:796d0f61a05b | 196 | const unsigned char *data, const unsigned char *pwd, |
HannesTschofenig | 0:796d0f61a05b | 197 | size_t pwdlen, size_t *use_len ) |
HannesTschofenig | 0:796d0f61a05b | 198 | { |
HannesTschofenig | 0:796d0f61a05b | 199 | int ret, enc; |
HannesTschofenig | 0:796d0f61a05b | 200 | size_t len; |
HannesTschofenig | 0:796d0f61a05b | 201 | unsigned char *buf; |
HannesTschofenig | 0:796d0f61a05b | 202 | const unsigned char *s1, *s2, *end; |
HannesTschofenig | 0:796d0f61a05b | 203 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
HannesTschofenig | 0:796d0f61a05b | 204 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
HannesTschofenig | 0:796d0f61a05b | 205 | unsigned char pem_iv[16]; |
HannesTschofenig | 0:796d0f61a05b | 206 | cipher_type_t enc_alg = POLARSSL_CIPHER_NONE; |
HannesTschofenig | 0:796d0f61a05b | 207 | #else |
HannesTschofenig | 0:796d0f61a05b | 208 | ((void) pwd); |
HannesTschofenig | 0:796d0f61a05b | 209 | ((void) pwdlen); |
HannesTschofenig | 0:796d0f61a05b | 210 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
HannesTschofenig | 0:796d0f61a05b | 211 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
HannesTschofenig | 0:796d0f61a05b | 212 | |
HannesTschofenig | 0:796d0f61a05b | 213 | if( ctx == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 214 | return( POLARSSL_ERR_PEM_BAD_INPUT_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 215 | |
HannesTschofenig | 0:796d0f61a05b | 216 | s1 = (unsigned char *) strstr( (const char *) data, header ); |
HannesTschofenig | 0:796d0f61a05b | 217 | |
HannesTschofenig | 0:796d0f61a05b | 218 | if( s1 == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 219 | return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
HannesTschofenig | 0:796d0f61a05b | 220 | |
HannesTschofenig | 0:796d0f61a05b | 221 | s2 = (unsigned char *) strstr( (const char *) data, footer ); |
HannesTschofenig | 0:796d0f61a05b | 222 | |
HannesTschofenig | 0:796d0f61a05b | 223 | if( s2 == NULL || s2 <= s1 ) |
HannesTschofenig | 0:796d0f61a05b | 224 | return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
HannesTschofenig | 0:796d0f61a05b | 225 | |
HannesTschofenig | 0:796d0f61a05b | 226 | s1 += strlen( header ); |
HannesTschofenig | 0:796d0f61a05b | 227 | if( *s1 == '\r' ) s1++; |
HannesTschofenig | 0:796d0f61a05b | 228 | if( *s1 == '\n' ) s1++; |
HannesTschofenig | 0:796d0f61a05b | 229 | else return( POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ); |
HannesTschofenig | 0:796d0f61a05b | 230 | |
HannesTschofenig | 0:796d0f61a05b | 231 | end = s2; |
HannesTschofenig | 0:796d0f61a05b | 232 | end += strlen( footer ); |
HannesTschofenig | 0:796d0f61a05b | 233 | if( *end == '\r' ) end++; |
HannesTschofenig | 0:796d0f61a05b | 234 | if( *end == '\n' ) end++; |
HannesTschofenig | 0:796d0f61a05b | 235 | *use_len = end - data; |
HannesTschofenig | 0:796d0f61a05b | 236 | |
HannesTschofenig | 0:796d0f61a05b | 237 | enc = 0; |
HannesTschofenig | 0:796d0f61a05b | 238 | |
HannesTschofenig | 0:796d0f61a05b | 239 | if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 240 | { |
HannesTschofenig | 0:796d0f61a05b | 241 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
HannesTschofenig | 0:796d0f61a05b | 242 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
HannesTschofenig | 0:796d0f61a05b | 243 | enc++; |
HannesTschofenig | 0:796d0f61a05b | 244 | |
HannesTschofenig | 0:796d0f61a05b | 245 | s1 += 22; |
HannesTschofenig | 0:796d0f61a05b | 246 | if( *s1 == '\r' ) s1++; |
HannesTschofenig | 0:796d0f61a05b | 247 | if( *s1 == '\n' ) s1++; |
HannesTschofenig | 0:796d0f61a05b | 248 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 249 | |
HannesTschofenig | 0:796d0f61a05b | 250 | |
HannesTschofenig | 0:796d0f61a05b | 251 | #if defined(POLARSSL_DES_C) |
HannesTschofenig | 0:796d0f61a05b | 252 | if( memcmp( s1, "DEK-Info: DES-EDE3-CBC,", 23 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 253 | { |
HannesTschofenig | 0:796d0f61a05b | 254 | enc_alg = POLARSSL_CIPHER_DES_EDE3_CBC; |
HannesTschofenig | 0:796d0f61a05b | 255 | |
HannesTschofenig | 0:796d0f61a05b | 256 | s1 += 23; |
HannesTschofenig | 0:796d0f61a05b | 257 | if( pem_get_iv( s1, pem_iv, 8 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 258 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
HannesTschofenig | 0:796d0f61a05b | 259 | |
HannesTschofenig | 0:796d0f61a05b | 260 | s1 += 16; |
HannesTschofenig | 0:796d0f61a05b | 261 | } |
HannesTschofenig | 0:796d0f61a05b | 262 | else if( memcmp( s1, "DEK-Info: DES-CBC,", 18 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 263 | { |
HannesTschofenig | 0:796d0f61a05b | 264 | enc_alg = POLARSSL_CIPHER_DES_CBC; |
HannesTschofenig | 0:796d0f61a05b | 265 | |
HannesTschofenig | 0:796d0f61a05b | 266 | s1 += 18; |
HannesTschofenig | 0:796d0f61a05b | 267 | if( pem_get_iv( s1, pem_iv, 8) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 268 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
HannesTschofenig | 0:796d0f61a05b | 269 | |
HannesTschofenig | 0:796d0f61a05b | 270 | s1 += 16; |
HannesTschofenig | 0:796d0f61a05b | 271 | } |
HannesTschofenig | 0:796d0f61a05b | 272 | #endif /* POLARSSL_DES_C */ |
HannesTschofenig | 0:796d0f61a05b | 273 | |
HannesTschofenig | 0:796d0f61a05b | 274 | #if defined(POLARSSL_AES_C) |
HannesTschofenig | 0:796d0f61a05b | 275 | if( memcmp( s1, "DEK-Info: AES-", 14 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 276 | { |
HannesTschofenig | 0:796d0f61a05b | 277 | if( memcmp( s1, "DEK-Info: AES-128-CBC,", 22 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 278 | enc_alg = POLARSSL_CIPHER_AES_128_CBC; |
HannesTschofenig | 0:796d0f61a05b | 279 | else if( memcmp( s1, "DEK-Info: AES-192-CBC,", 22 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 280 | enc_alg = POLARSSL_CIPHER_AES_192_CBC; |
HannesTschofenig | 0:796d0f61a05b | 281 | else if( memcmp( s1, "DEK-Info: AES-256-CBC,", 22 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 282 | enc_alg = POLARSSL_CIPHER_AES_256_CBC; |
HannesTschofenig | 0:796d0f61a05b | 283 | else |
HannesTschofenig | 0:796d0f61a05b | 284 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
HannesTschofenig | 0:796d0f61a05b | 285 | |
HannesTschofenig | 0:796d0f61a05b | 286 | s1 += 22; |
HannesTschofenig | 0:796d0f61a05b | 287 | if( pem_get_iv( s1, pem_iv, 16 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 288 | return( POLARSSL_ERR_PEM_INVALID_ENC_IV ); |
HannesTschofenig | 0:796d0f61a05b | 289 | |
HannesTschofenig | 0:796d0f61a05b | 290 | s1 += 32; |
HannesTschofenig | 0:796d0f61a05b | 291 | } |
HannesTschofenig | 0:796d0f61a05b | 292 | #endif /* POLARSSL_AES_C */ |
HannesTschofenig | 0:796d0f61a05b | 293 | |
HannesTschofenig | 0:796d0f61a05b | 294 | if( enc_alg == POLARSSL_CIPHER_NONE ) |
HannesTschofenig | 0:796d0f61a05b | 295 | return( POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG ); |
HannesTschofenig | 0:796d0f61a05b | 296 | |
HannesTschofenig | 0:796d0f61a05b | 297 | if( *s1 == '\r' ) s1++; |
HannesTschofenig | 0:796d0f61a05b | 298 | if( *s1 == '\n' ) s1++; |
HannesTschofenig | 0:796d0f61a05b | 299 | else return( POLARSSL_ERR_PEM_INVALID_DATA ); |
HannesTschofenig | 0:796d0f61a05b | 300 | #else |
HannesTschofenig | 0:796d0f61a05b | 301 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
HannesTschofenig | 0:796d0f61a05b | 302 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
HannesTschofenig | 0:796d0f61a05b | 303 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
HannesTschofenig | 0:796d0f61a05b | 304 | } |
HannesTschofenig | 0:796d0f61a05b | 305 | |
HannesTschofenig | 0:796d0f61a05b | 306 | len = 0; |
HannesTschofenig | 0:796d0f61a05b | 307 | ret = base64_decode( NULL, &len, s1, s2 - s1 ); |
HannesTschofenig | 0:796d0f61a05b | 308 | |
HannesTschofenig | 0:796d0f61a05b | 309 | if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER ) |
HannesTschofenig | 0:796d0f61a05b | 310 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
HannesTschofenig | 0:796d0f61a05b | 311 | |
HannesTschofenig | 0:796d0f61a05b | 312 | if( ( buf = (unsigned char *) polarssl_malloc( len ) ) == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 313 | return( POLARSSL_ERR_PEM_MALLOC_FAILED ); |
HannesTschofenig | 0:796d0f61a05b | 314 | |
HannesTschofenig | 0:796d0f61a05b | 315 | if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 316 | { |
HannesTschofenig | 0:796d0f61a05b | 317 | polarssl_free( buf ); |
HannesTschofenig | 0:796d0f61a05b | 318 | return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); |
HannesTschofenig | 0:796d0f61a05b | 319 | } |
HannesTschofenig | 0:796d0f61a05b | 320 | |
HannesTschofenig | 0:796d0f61a05b | 321 | if( enc != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 322 | { |
HannesTschofenig | 0:796d0f61a05b | 323 | #if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \ |
HannesTschofenig | 0:796d0f61a05b | 324 | ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) |
HannesTschofenig | 0:796d0f61a05b | 325 | if( pwd == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 326 | { |
HannesTschofenig | 0:796d0f61a05b | 327 | polarssl_free( buf ); |
HannesTschofenig | 0:796d0f61a05b | 328 | return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED ); |
HannesTschofenig | 0:796d0f61a05b | 329 | } |
HannesTschofenig | 0:796d0f61a05b | 330 | |
HannesTschofenig | 0:796d0f61a05b | 331 | #if defined(POLARSSL_DES_C) |
HannesTschofenig | 0:796d0f61a05b | 332 | if( enc_alg == POLARSSL_CIPHER_DES_EDE3_CBC ) |
HannesTschofenig | 0:796d0f61a05b | 333 | pem_des3_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 334 | else if( enc_alg == POLARSSL_CIPHER_DES_CBC ) |
HannesTschofenig | 0:796d0f61a05b | 335 | pem_des_decrypt( pem_iv, buf, len, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 336 | #endif /* POLARSSL_DES_C */ |
HannesTschofenig | 0:796d0f61a05b | 337 | |
HannesTschofenig | 0:796d0f61a05b | 338 | #if defined(POLARSSL_AES_C) |
HannesTschofenig | 0:796d0f61a05b | 339 | if( enc_alg == POLARSSL_CIPHER_AES_128_CBC ) |
HannesTschofenig | 0:796d0f61a05b | 340 | pem_aes_decrypt( pem_iv, 16, buf, len, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 341 | else if( enc_alg == POLARSSL_CIPHER_AES_192_CBC ) |
HannesTschofenig | 0:796d0f61a05b | 342 | pem_aes_decrypt( pem_iv, 24, buf, len, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 343 | else if( enc_alg == POLARSSL_CIPHER_AES_256_CBC ) |
HannesTschofenig | 0:796d0f61a05b | 344 | pem_aes_decrypt( pem_iv, 32, buf, len, pwd, pwdlen ); |
HannesTschofenig | 0:796d0f61a05b | 345 | #endif /* POLARSSL_AES_C */ |
HannesTschofenig | 0:796d0f61a05b | 346 | |
HannesTschofenig | 0:796d0f61a05b | 347 | /* |
HannesTschofenig | 0:796d0f61a05b | 348 | * The result will be ASN.1 starting with a SEQUENCE tag, with 1 to 3 |
HannesTschofenig | 0:796d0f61a05b | 349 | * length bytes (allow 4 to be sure) in all known use cases. |
HannesTschofenig | 0:796d0f61a05b | 350 | * |
HannesTschofenig | 0:796d0f61a05b | 351 | * Use that as heurisitic to try detecting password mismatchs. |
HannesTschofenig | 0:796d0f61a05b | 352 | */ |
HannesTschofenig | 0:796d0f61a05b | 353 | if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 ) |
HannesTschofenig | 0:796d0f61a05b | 354 | { |
HannesTschofenig | 0:796d0f61a05b | 355 | polarssl_free( buf ); |
HannesTschofenig | 0:796d0f61a05b | 356 | return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH ); |
HannesTschofenig | 0:796d0f61a05b | 357 | } |
HannesTschofenig | 0:796d0f61a05b | 358 | #else |
HannesTschofenig | 0:796d0f61a05b | 359 | polarssl_free( buf ); |
HannesTschofenig | 0:796d0f61a05b | 360 | return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); |
HannesTschofenig | 0:796d0f61a05b | 361 | #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && |
HannesTschofenig | 0:796d0f61a05b | 362 | ( POLARSSL_AES_C || POLARSSL_DES_C ) */ |
HannesTschofenig | 0:796d0f61a05b | 363 | } |
HannesTschofenig | 0:796d0f61a05b | 364 | |
HannesTschofenig | 0:796d0f61a05b | 365 | ctx->buf = buf; |
HannesTschofenig | 0:796d0f61a05b | 366 | ctx->buflen = len; |
HannesTschofenig | 0:796d0f61a05b | 367 | |
HannesTschofenig | 0:796d0f61a05b | 368 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 369 | } |
HannesTschofenig | 0:796d0f61a05b | 370 | |
HannesTschofenig | 0:796d0f61a05b | 371 | void pem_free( pem_context *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 372 | { |
HannesTschofenig | 0:796d0f61a05b | 373 | if( ctx->buf ) |
HannesTschofenig | 0:796d0f61a05b | 374 | polarssl_free( ctx->buf ); |
HannesTschofenig | 0:796d0f61a05b | 375 | |
HannesTschofenig | 0:796d0f61a05b | 376 | if( ctx->info ) |
HannesTschofenig | 0:796d0f61a05b | 377 | polarssl_free( ctx->info ); |
HannesTschofenig | 0:796d0f61a05b | 378 | |
HannesTschofenig | 0:796d0f61a05b | 379 | memset( ctx, 0, sizeof( pem_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 380 | } |
HannesTschofenig | 0:796d0f61a05b | 381 | #endif /* POLARSSL_PEM_PARSE_C */ |
HannesTschofenig | 0:796d0f61a05b | 382 | |
HannesTschofenig | 0:796d0f61a05b | 383 | #if defined(POLARSSL_PEM_WRITE_C) |
HannesTschofenig | 0:796d0f61a05b | 384 | int pem_write_buffer( const char *header, const char *footer, |
HannesTschofenig | 0:796d0f61a05b | 385 | const unsigned char *der_data, size_t der_len, |
HannesTschofenig | 0:796d0f61a05b | 386 | unsigned char *buf, size_t buf_len, size_t *olen ) |
HannesTschofenig | 0:796d0f61a05b | 387 | { |
HannesTschofenig | 0:796d0f61a05b | 388 | int ret; |
HannesTschofenig | 0:796d0f61a05b | 389 | unsigned char *encode_buf, *c, *p = buf; |
HannesTschofenig | 0:796d0f61a05b | 390 | size_t len = 0, use_len = 0, add_len = 0; |
HannesTschofenig | 0:796d0f61a05b | 391 | |
HannesTschofenig | 0:796d0f61a05b | 392 | base64_encode( NULL, &use_len, der_data, der_len ); |
HannesTschofenig | 0:796d0f61a05b | 393 | add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1; |
HannesTschofenig | 0:796d0f61a05b | 394 | |
HannesTschofenig | 0:796d0f61a05b | 395 | if( use_len + add_len > buf_len ) |
HannesTschofenig | 0:796d0f61a05b | 396 | { |
HannesTschofenig | 0:796d0f61a05b | 397 | *olen = use_len + add_len; |
HannesTschofenig | 0:796d0f61a05b | 398 | return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL ); |
HannesTschofenig | 0:796d0f61a05b | 399 | } |
HannesTschofenig | 0:796d0f61a05b | 400 | |
HannesTschofenig | 0:796d0f61a05b | 401 | if( ( encode_buf = polarssl_malloc( use_len ) ) == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 402 | return( POLARSSL_ERR_PEM_MALLOC_FAILED ); |
HannesTschofenig | 0:796d0f61a05b | 403 | |
HannesTschofenig | 0:796d0f61a05b | 404 | if( ( ret = base64_encode( encode_buf, &use_len, der_data, |
HannesTschofenig | 0:796d0f61a05b | 405 | der_len ) ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 406 | { |
HannesTschofenig | 0:796d0f61a05b | 407 | polarssl_free( encode_buf ); |
HannesTschofenig | 0:796d0f61a05b | 408 | return( ret ); |
HannesTschofenig | 0:796d0f61a05b | 409 | } |
HannesTschofenig | 0:796d0f61a05b | 410 | |
HannesTschofenig | 0:796d0f61a05b | 411 | memcpy( p, header, strlen( header ) ); |
HannesTschofenig | 0:796d0f61a05b | 412 | p += strlen( header ); |
HannesTschofenig | 0:796d0f61a05b | 413 | c = encode_buf; |
HannesTschofenig | 0:796d0f61a05b | 414 | |
HannesTschofenig | 0:796d0f61a05b | 415 | while( use_len ) |
HannesTschofenig | 0:796d0f61a05b | 416 | { |
HannesTschofenig | 0:796d0f61a05b | 417 | len = ( use_len > 64 ) ? 64 : use_len; |
HannesTschofenig | 0:796d0f61a05b | 418 | memcpy( p, c, len ); |
HannesTschofenig | 0:796d0f61a05b | 419 | use_len -= len; |
HannesTschofenig | 0:796d0f61a05b | 420 | p += len; |
HannesTschofenig | 0:796d0f61a05b | 421 | c += len; |
HannesTschofenig | 0:796d0f61a05b | 422 | *p++ = '\n'; |
HannesTschofenig | 0:796d0f61a05b | 423 | } |
HannesTschofenig | 0:796d0f61a05b | 424 | |
HannesTschofenig | 0:796d0f61a05b | 425 | memcpy( p, footer, strlen( footer ) ); |
HannesTschofenig | 0:796d0f61a05b | 426 | p += strlen( footer ); |
HannesTschofenig | 0:796d0f61a05b | 427 | |
HannesTschofenig | 0:796d0f61a05b | 428 | *p++ = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 429 | *olen = p - buf; |
HannesTschofenig | 0:796d0f61a05b | 430 | |
HannesTschofenig | 0:796d0f61a05b | 431 | polarssl_free( encode_buf ); |
HannesTschofenig | 0:796d0f61a05b | 432 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 433 | } |
HannesTschofenig | 0:796d0f61a05b | 434 | #endif /* POLARSSL_PEM_WRITE_C */ |
HannesTschofenig | 0:796d0f61a05b | 435 | #endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */ |
HannesTschofenig | 0:796d0f61a05b | 436 | |
HannesTschofenig | 0:796d0f61a05b | 437 |