pkcs12.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * PKCS#12 Personal Information Exchange Syntax
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
  25. *
  26. * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
  27. * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
  28. */
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "mbedtls/config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34. #if defined(MBEDTLS_PKCS12_C)
  35. #include "mbedtls/pkcs12.h"
  36. #include "mbedtls/asn1.h"
  37. #include "mbedtls/cipher.h"
  38. #include <string.h>
  39. #if defined(MBEDTLS_ARC4_C)
  40. #include "mbedtls/arc4.h"
  41. #endif
  42. #if defined(MBEDTLS_DES_C)
  43. #include "mbedtls/des.h"
  44. #endif
  45. /* Implementation that should never be optimized out by the compiler */
  46. static void mbedtls_zeroize( void *v, size_t n ) {
  47. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  48. }
  49. static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
  50. mbedtls_asn1_buf *salt, int *iterations )
  51. {
  52. int ret;
  53. unsigned char **p = &params->p;
  54. const unsigned char *end = params->p + params->len;
  55. /*
  56. * pkcs-12PbeParams ::= SEQUENCE {
  57. * salt OCTET STRING,
  58. * iterations INTEGER
  59. * }
  60. *
  61. */
  62. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  63. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  64. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  65. if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  66. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  67. salt->p = *p;
  68. *p += salt->len;
  69. if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
  70. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  71. if( *p != end )
  72. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  73. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  74. return( 0 );
  75. }
  76. #define PKCS12_MAX_PWDLEN 128
  77. static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
  78. const unsigned char *pwd, size_t pwdlen,
  79. unsigned char *key, size_t keylen,
  80. unsigned char *iv, size_t ivlen )
  81. {
  82. int ret, iterations = 0;
  83. mbedtls_asn1_buf salt;
  84. size_t i;
  85. unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
  86. if( pwdlen > PKCS12_MAX_PWDLEN )
  87. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  88. memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
  89. memset( &unipwd, 0, sizeof(unipwd) );
  90. if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
  91. &iterations ) ) != 0 )
  92. return( ret );
  93. for( i = 0; i < pwdlen; i++ )
  94. unipwd[i * 2 + 1] = pwd[i];
  95. if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
  96. salt.p, salt.len, md_type,
  97. MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
  98. {
  99. return( ret );
  100. }
  101. if( iv == NULL || ivlen == 0 )
  102. return( 0 );
  103. if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
  104. salt.p, salt.len, md_type,
  105. MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
  106. {
  107. return( ret );
  108. }
  109. return( 0 );
  110. }
  111. #undef PKCS12_MAX_PWDLEN
  112. int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
  113. const unsigned char *pwd, size_t pwdlen,
  114. const unsigned char *data, size_t len,
  115. unsigned char *output )
  116. {
  117. #if !defined(MBEDTLS_ARC4_C)
  118. ((void) pbe_params);
  119. ((void) mode);
  120. ((void) pwd);
  121. ((void) pwdlen);
  122. ((void) data);
  123. ((void) len);
  124. ((void) output);
  125. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  126. #else
  127. int ret;
  128. unsigned char key[16];
  129. mbedtls_arc4_context ctx;
  130. ((void) mode);
  131. mbedtls_arc4_init( &ctx );
  132. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
  133. pwd, pwdlen,
  134. key, 16, NULL, 0 ) ) != 0 )
  135. {
  136. return( ret );
  137. }
  138. mbedtls_arc4_setup( &ctx, key, 16 );
  139. if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
  140. goto exit;
  141. exit:
  142. mbedtls_zeroize( key, sizeof( key ) );
  143. mbedtls_arc4_free( &ctx );
  144. return( ret );
  145. #endif /* MBEDTLS_ARC4_C */
  146. }
  147. int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
  148. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  149. const unsigned char *pwd, size_t pwdlen,
  150. const unsigned char *data, size_t len,
  151. unsigned char *output )
  152. {
  153. int ret, keylen = 0;
  154. unsigned char key[32];
  155. unsigned char iv[16];
  156. const mbedtls_cipher_info_t *cipher_info;
  157. mbedtls_cipher_context_t cipher_ctx;
  158. size_t olen = 0;
  159. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  160. if( cipher_info == NULL )
  161. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  162. keylen = cipher_info->key_bitlen / 8;
  163. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
  164. key, keylen,
  165. iv, cipher_info->iv_size ) ) != 0 )
  166. {
  167. return( ret );
  168. }
  169. mbedtls_cipher_init( &cipher_ctx );
  170. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
  171. goto exit;
  172. if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
  173. goto exit;
  174. if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
  175. goto exit;
  176. if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
  177. goto exit;
  178. if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
  179. output, &olen ) ) != 0 )
  180. {
  181. goto exit;
  182. }
  183. if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
  184. ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
  185. exit:
  186. mbedtls_zeroize( key, sizeof( key ) );
  187. mbedtls_zeroize( iv, sizeof( iv ) );
  188. mbedtls_cipher_free( &cipher_ctx );
  189. return( ret );
  190. }
  191. static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
  192. const unsigned char *filler, size_t fill_len )
  193. {
  194. unsigned char *p = data;
  195. size_t use_len;
  196. while( data_len > 0 )
  197. {
  198. use_len = ( data_len > fill_len ) ? fill_len : data_len;
  199. memcpy( p, filler, use_len );
  200. p += use_len;
  201. data_len -= use_len;
  202. }
  203. }
  204. int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
  205. const unsigned char *pwd, size_t pwdlen,
  206. const unsigned char *salt, size_t saltlen,
  207. mbedtls_md_type_t md_type, int id, int iterations )
  208. {
  209. int ret;
  210. unsigned int j;
  211. unsigned char diversifier[128];
  212. unsigned char salt_block[128], pwd_block[128], hash_block[128];
  213. unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
  214. unsigned char *p;
  215. unsigned char c;
  216. size_t hlen, use_len, v, i;
  217. const mbedtls_md_info_t *md_info;
  218. mbedtls_md_context_t md_ctx;
  219. // This version only allows max of 64 bytes of password or salt
  220. if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
  221. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  222. md_info = mbedtls_md_info_from_type( md_type );
  223. if( md_info == NULL )
  224. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  225. mbedtls_md_init( &md_ctx );
  226. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  227. return( ret );
  228. hlen = mbedtls_md_get_size( md_info );
  229. if( hlen <= 32 )
  230. v = 64;
  231. else
  232. v = 128;
  233. memset( diversifier, (unsigned char) id, v );
  234. pkcs12_fill_buffer( salt_block, v, salt, saltlen );
  235. pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
  236. p = data;
  237. while( datalen > 0 )
  238. {
  239. // Calculate hash( diversifier || salt_block || pwd_block )
  240. if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
  241. goto exit;
  242. if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
  243. goto exit;
  244. if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
  245. goto exit;
  246. if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
  247. goto exit;
  248. if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
  249. goto exit;
  250. // Perform remaining ( iterations - 1 ) recursive hash calculations
  251. for( i = 1; i < (size_t) iterations; i++ )
  252. {
  253. if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
  254. goto exit;
  255. }
  256. use_len = ( datalen > hlen ) ? hlen : datalen;
  257. memcpy( p, hash_output, use_len );
  258. datalen -= use_len;
  259. p += use_len;
  260. if( datalen == 0 )
  261. break;
  262. // Concatenating copies of hash_output into hash_block (B)
  263. pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
  264. // B += 1
  265. for( i = v; i > 0; i-- )
  266. if( ++hash_block[i - 1] != 0 )
  267. break;
  268. // salt_block += B
  269. c = 0;
  270. for( i = v; i > 0; i-- )
  271. {
  272. j = salt_block[i - 1] + hash_block[i - 1] + c;
  273. c = (unsigned char) (j >> 8);
  274. salt_block[i - 1] = j & 0xFF;
  275. }
  276. // pwd_block += B
  277. c = 0;
  278. for( i = v; i > 0; i-- )
  279. {
  280. j = pwd_block[i - 1] + hash_block[i - 1] + c;
  281. c = (unsigned char) (j >> 8);
  282. pwd_block[i - 1] = j & 0xFF;
  283. }
  284. }
  285. ret = 0;
  286. exit:
  287. mbedtls_zeroize( salt_block, sizeof( salt_block ) );
  288. mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
  289. mbedtls_zeroize( hash_block, sizeof( hash_block ) );
  290. mbedtls_zeroize( hash_output, sizeof( hash_output ) );
  291. mbedtls_md_free( &md_ctx );
  292. return( ret );
  293. }
  294. #endif /* MBEDTLS_PKCS12_C */