Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PKCS#12 Personal Information Exchange Syntax |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | /* |
| 23 | * The PKCS #12 Personal Information Exchange Syntax Standard v1.1 |
| 24 | * |
| 25 | * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf |
| 26 | * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn |
| 27 | */ |
| 28 | |
| 29 | #include "polarssl/config.h" |
| 30 | |
| 31 | #if defined(POLARSSL_PKCS12_C) |
| 32 | |
| 33 | #include "polarssl/pkcs12.h" |
| 34 | #include "polarssl/asn1.h" |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 35 | #include "polarssl/cipher.h" |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 36 | |
| 37 | #if defined(POLARSSL_ARC4_C) |
| 38 | #include "polarssl/arc4.h" |
| 39 | #endif |
| 40 | |
| 41 | #if defined(POLARSSL_DES_C) |
| 42 | #include "polarssl/des.h" |
| 43 | #endif |
| 44 | |
| 45 | static int pkcs12_parse_pbe_params( unsigned char **p, |
| 46 | const unsigned char *end, |
| 47 | asn1_buf *salt, int *iterations ) |
| 48 | { |
| 49 | int ret; |
| 50 | size_t len = 0; |
| 51 | |
| 52 | /* |
| 53 | * pkcs-12PbeParams ::= SEQUENCE { |
| 54 | * salt OCTET STRING, |
| 55 | * iterations INTEGER |
| 56 | * } |
| 57 | * |
| 58 | */ |
| 59 | if( ( ret = asn1_get_tag( p, end, &len, |
| 60 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 61 | { |
| 62 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret ); |
| 63 | } |
| 64 | |
| 65 | end = *p + len; |
| 66 | |
| 67 | if( ( ret = asn1_get_tag( p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 ) |
| 68 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret ); |
| 69 | |
| 70 | salt->p = *p; |
| 71 | *p += salt->len; |
| 72 | |
| 73 | if( ( ret = asn1_get_int( p, end, iterations ) ) != 0 ) |
| 74 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + ret ); |
| 75 | |
| 76 | if( *p != end ) |
| 77 | return( POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT + |
| 78 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 79 | |
| 80 | return( 0 ); |
| 81 | } |
| 82 | |
Manuel Pégourié-Gonnard | 73011bb | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 83 | #define PKCS12_MAX_PWDLEN 128 |
| 84 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 85 | static int pkcs12_pbe_derive_key_iv( asn1_buf *pbe_params, md_type_t md_type, |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 86 | const unsigned char *pwd, size_t pwdlen, |
| 87 | unsigned char *key, size_t keylen, |
| 88 | unsigned char *iv, size_t ivlen ) |
| 89 | { |
| 90 | int ret, iterations; |
| 91 | asn1_buf salt; |
| 92 | size_t i; |
| 93 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 73011bb | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 94 | unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2]; |
| 95 | |
| 96 | if( pwdlen > PKCS12_MAX_PWDLEN ) |
| 97 | return( POLARSSL_ERR_PKCS12_BAD_INPUT_DATA ); |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 98 | |
| 99 | memset(&salt, 0, sizeof(asn1_buf)); |
| 100 | memset(&unipwd, 0, sizeof(unipwd)); |
| 101 | |
| 102 | p = pbe_params->p; |
| 103 | end = p + pbe_params->len; |
| 104 | |
| 105 | if( ( ret = pkcs12_parse_pbe_params( &p, end, &salt, &iterations ) ) != 0 ) |
| 106 | return( ret ); |
| 107 | |
| 108 | for(i = 0; i < pwdlen; i++) |
| 109 | unipwd[i * 2 + 1] = pwd[i]; |
| 110 | |
| 111 | if( ( ret = pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 112 | salt.p, salt.len, md_type, |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 113 | PKCS12_DERIVE_KEY, iterations ) ) != 0 ) |
| 114 | { |
| 115 | return( ret ); |
| 116 | } |
| 117 | |
| 118 | if( iv == NULL || ivlen == 0 ) |
| 119 | return( 0 ); |
| 120 | |
| 121 | if( ( ret = pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2, |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 122 | salt.p, salt.len, md_type, |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 123 | PKCS12_DERIVE_IV, iterations ) ) != 0 ) |
| 124 | { |
| 125 | return( ret ); |
| 126 | } |
| 127 | return( 0 ); |
| 128 | } |
| 129 | |
Manuel Pégourié-Gonnard | 73011bb | 2015-09-28 18:34:48 +0200 | [diff] [blame] | 130 | #undef PKCS12_MAX_PWDLEN |
| 131 | |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 132 | int pkcs12_pbe_sha1_rc4_128( asn1_buf *pbe_params, int mode, |
| 133 | const unsigned char *pwd, size_t pwdlen, |
| 134 | const unsigned char *data, size_t len, |
| 135 | unsigned char *output ) |
| 136 | { |
| 137 | #if !defined(POLARSSL_ARC4_C) |
| 138 | ((void) pbe_params); |
| 139 | ((void) mode); |
| 140 | ((void) pwd); |
| 141 | ((void) pwdlen); |
| 142 | ((void) data); |
| 143 | ((void) len); |
| 144 | ((void) output); |
| 145 | return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 146 | #else |
| 147 | int ret; |
| 148 | unsigned char key[16]; |
| 149 | arc4_context ctx; |
| 150 | ((void) mode); |
| 151 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 152 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, POLARSSL_MD_SHA1, |
| 153 | pwd, pwdlen, |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 154 | key, 16, NULL, 0 ) ) != 0 ) |
| 155 | { |
| 156 | return( ret ); |
| 157 | } |
| 158 | |
| 159 | arc4_setup( &ctx, key, 16 ); |
| 160 | if( ( ret = arc4_crypt( &ctx, len, data, output ) ) != 0 ) |
| 161 | return( ret ); |
| 162 | |
| 163 | return( 0 ); |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 164 | #endif /* POLARSSL_ARC4_C */ |
Paul Bakker | 67812d3 | 2013-06-14 11:35:09 +0200 | [diff] [blame] | 165 | } |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 166 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 167 | int pkcs12_pbe( asn1_buf *pbe_params, int mode, |
| 168 | cipher_type_t cipher_type, md_type_t md_type, |
| 169 | const unsigned char *pwd, size_t pwdlen, |
| 170 | const unsigned char *data, size_t len, |
| 171 | unsigned char *output ) |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 172 | { |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 173 | int ret, keylen = 0; |
| 174 | unsigned char key[32]; |
| 175 | unsigned char iv[16]; |
| 176 | const cipher_info_t *cipher_info; |
| 177 | cipher_context_t cipher_ctx; |
| 178 | size_t olen = 0; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 179 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 180 | cipher_info = cipher_info_from_type( cipher_type ); |
| 181 | if( cipher_info == NULL ) |
| 182 | return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 183 | |
| 184 | keylen = cipher_info->key_length / 8; |
| 185 | |
| 186 | if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen, |
| 187 | key, keylen, |
| 188 | iv, cipher_info->iv_size ) ) != 0 ) |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 189 | { |
| 190 | return( ret ); |
| 191 | } |
| 192 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 193 | if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 ) |
| 194 | return( ret ); |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 195 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 196 | if( ( ret = cipher_setkey( &cipher_ctx, key, keylen, mode ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 197 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 198 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 199 | if( ( ret = cipher_reset( &cipher_ctx, iv ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 200 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 201 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 202 | if( ( ret = cipher_update( &cipher_ctx, data, len, |
| 203 | output, &olen ) ) != 0 ) |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 204 | { |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 205 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Paul Bakker | 14a222c | 2013-06-18 16:35:48 +0200 | [diff] [blame] | 208 | if( ( ret = cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 209 | { |
| 210 | ret = POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH; |
| 211 | goto cleanup; |
| 212 | } |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 213 | |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 214 | cleanup: |
| 215 | cipher_free_ctx( &cipher_ctx ); |
| 216 | |
| 217 | return( ret ); |
Paul Bakker | 67812d3 | 2013-06-14 11:35:09 +0200 | [diff] [blame] | 218 | } |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 219 | |
| 220 | static void pkcs12_fill_buffer( unsigned char *data, size_t data_len, |
| 221 | const unsigned char *filler, size_t fill_len ) |
| 222 | { |
| 223 | unsigned char *p = data; |
| 224 | size_t use_len; |
| 225 | |
| 226 | while( data_len > 0 ) |
| 227 | { |
| 228 | use_len = ( data_len > fill_len ) ? fill_len : data_len; |
| 229 | memcpy( p, filler, use_len ); |
| 230 | p += use_len; |
| 231 | data_len -= use_len; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | int pkcs12_derivation( unsigned char *data, size_t datalen, |
| 236 | const unsigned char *pwd, size_t pwdlen, |
| 237 | const unsigned char *salt, size_t saltlen, |
| 238 | md_type_t md_type, int id, int iterations ) |
| 239 | { |
| 240 | int ret, i; |
| 241 | unsigned int j; |
| 242 | |
| 243 | unsigned char diversifier[128]; |
| 244 | unsigned char salt_block[128], pwd_block[128], hash_block[128]; |
| 245 | unsigned char hash_output[POLARSSL_MD_MAX_SIZE]; |
| 246 | unsigned char *p; |
| 247 | unsigned char c; |
| 248 | |
| 249 | size_t hlen, use_len, v; |
| 250 | |
| 251 | const md_info_t *md_info; |
| 252 | md_context_t md_ctx; |
| 253 | |
| 254 | // This version only allows max of 64 bytes of password or salt |
| 255 | if( datalen > 128 || pwdlen > 64 || saltlen > 64 ) |
| 256 | return( POLARSSL_ERR_PKCS12_BAD_INPUT_DATA ); |
| 257 | |
| 258 | md_info = md_info_from_type( md_type ); |
| 259 | if( md_info == NULL ) |
| 260 | return( POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE ); |
| 261 | |
| 262 | if ( ( ret = md_init_ctx( &md_ctx, md_info ) ) != 0 ) |
| 263 | return( ret ); |
| 264 | hlen = md_get_size( md_info ); |
| 265 | |
| 266 | if( hlen <= 32 ) |
| 267 | v = 64; |
| 268 | else |
| 269 | v = 128; |
| 270 | |
| 271 | memset( diversifier, (unsigned char) id, v ); |
| 272 | |
| 273 | pkcs12_fill_buffer( salt_block, v, salt, saltlen ); |
| 274 | pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen ); |
| 275 | |
| 276 | p = data; |
| 277 | while( datalen > 0 ) |
| 278 | { |
| 279 | // Calculate hash( diversifier || salt_block || pwd_block ) |
| 280 | if( ( ret = md_starts( &md_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 281 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 282 | |
| 283 | if( ( ret = md_update( &md_ctx, diversifier, v ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 284 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 285 | |
| 286 | if( ( ret = md_update( &md_ctx, salt_block, v ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 287 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 288 | |
| 289 | if( ( ret = md_update( &md_ctx, pwd_block, v ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 290 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 291 | |
| 292 | if( ( ret = md_finish( &md_ctx, hash_output ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 293 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 294 | |
| 295 | // Perform remaining ( iterations - 1 ) recursive hash calculations |
| 296 | for( i = 1; i < iterations; i++ ) |
| 297 | { |
| 298 | if( ( ret = md( md_info, hash_output, hlen, hash_output ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 299 | goto cleanup; |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | use_len = ( datalen > hlen ) ? hlen : datalen; |
| 303 | memcpy( p, hash_output, use_len ); |
| 304 | datalen -= use_len; |
| 305 | p += use_len; |
| 306 | |
| 307 | if( datalen == 0 ) |
| 308 | break; |
| 309 | |
| 310 | // Concatenating copies of hash_output into hash_block (B) |
| 311 | pkcs12_fill_buffer( hash_block, v, hash_output, hlen ); |
| 312 | |
| 313 | // B += 1 |
| 314 | for( i = v; i > 0; i-- ) |
| 315 | if( ++hash_block[i - 1] != 0 ) |
| 316 | break; |
| 317 | |
| 318 | // salt_block += B |
| 319 | c = 0; |
| 320 | for( i = v; i > 0; i-- ) |
| 321 | { |
| 322 | j = salt_block[i - 1] + hash_block[i - 1] + c; |
| 323 | c = (unsigned char) (j >> 8); |
| 324 | salt_block[i - 1] = j & 0xFF; |
| 325 | } |
| 326 | |
| 327 | // pwd_block += B |
| 328 | c = 0; |
| 329 | for( i = v; i > 0; i-- ) |
| 330 | { |
| 331 | j = pwd_block[i - 1] + hash_block[i - 1] + c; |
| 332 | c = (unsigned char) (j >> 8); |
| 333 | pwd_block[i - 1] = j & 0xFF; |
| 334 | } |
| 335 | } |
| 336 | |
Manuel Pégourié-Gonnard | 1c022a6 | 2014-11-17 12:24:41 +0100 | [diff] [blame] | 337 | cleanup: |
| 338 | md_free_ctx( &md_ctx ); |
| 339 | |
| 340 | return( ret ); |
Paul Bakker | cf6e95d | 2013-06-12 13:18:15 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | #endif /* POLARSSL_PKCS12_C */ |