pkcs12.c 11 KB

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