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 <string.h>
  37. #if defined(MBEDTLS_ARC4_C)
  38. #include "mbedtls/arc4.h"
  39. #endif
  40. #if defined(MBEDTLS_DES_C)
  41. #include "mbedtls/des.h"
  42. #endif
  43. /* Implementation that should never be optimized out by the compiler */
  44. static void mbedtls_zeroize( void *v, size_t n ) {
  45. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  46. }
  47. static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
  48. mbedtls_asn1_buf *salt, int *iterations )
  49. {
  50. int ret;
  51. unsigned char **p = &params->p;
  52. const unsigned char *end = params->p + params->len;
  53. /*
  54. * pkcs-12PbeParams ::= SEQUENCE {
  55. * salt OCTET STRING,
  56. * iterations INTEGER
  57. * }
  58. *
  59. */
  60. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  61. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  62. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  63. if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  64. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  65. salt->p = *p;
  66. *p += salt->len;
  67. if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
  68. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
  69. if( *p != end )
  70. return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
  71. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  72. return( 0 );
  73. }
  74. #define PKCS12_MAX_PWDLEN 128
  75. static int pkcs12_pbe_derive_key_iv( mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
  76. const unsigned char *pwd, size_t pwdlen,
  77. unsigned char *key, size_t keylen,
  78. unsigned char *iv, size_t ivlen )
  79. {
  80. int ret, iterations = 0;
  81. mbedtls_asn1_buf salt;
  82. size_t i;
  83. unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
  84. if( pwdlen > PKCS12_MAX_PWDLEN )
  85. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  86. memset( &salt, 0, sizeof(mbedtls_asn1_buf) );
  87. memset( &unipwd, 0, sizeof(unipwd) );
  88. if( ( ret = pkcs12_parse_pbe_params( pbe_params, &salt,
  89. &iterations ) ) != 0 )
  90. return( ret );
  91. for( i = 0; i < pwdlen; i++ )
  92. unipwd[i * 2 + 1] = pwd[i];
  93. if( ( ret = mbedtls_pkcs12_derivation( key, keylen, unipwd, pwdlen * 2 + 2,
  94. salt.p, salt.len, md_type,
  95. MBEDTLS_PKCS12_DERIVE_KEY, iterations ) ) != 0 )
  96. {
  97. return( ret );
  98. }
  99. if( iv == NULL || ivlen == 0 )
  100. return( 0 );
  101. if( ( ret = mbedtls_pkcs12_derivation( iv, ivlen, unipwd, pwdlen * 2 + 2,
  102. salt.p, salt.len, md_type,
  103. MBEDTLS_PKCS12_DERIVE_IV, iterations ) ) != 0 )
  104. {
  105. return( ret );
  106. }
  107. return( 0 );
  108. }
  109. #undef PKCS12_MAX_PWDLEN
  110. int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
  111. const unsigned char *pwd, size_t pwdlen,
  112. const unsigned char *data, size_t len,
  113. unsigned char *output )
  114. {
  115. #if !defined(MBEDTLS_ARC4_C)
  116. ((void) pbe_params);
  117. ((void) mode);
  118. ((void) pwd);
  119. ((void) pwdlen);
  120. ((void) data);
  121. ((void) len);
  122. ((void) output);
  123. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  124. #else
  125. int ret;
  126. unsigned char key[16];
  127. mbedtls_arc4_context ctx;
  128. ((void) mode);
  129. mbedtls_arc4_init( &ctx );
  130. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, MBEDTLS_MD_SHA1,
  131. pwd, pwdlen,
  132. key, 16, NULL, 0 ) ) != 0 )
  133. {
  134. return( ret );
  135. }
  136. mbedtls_arc4_setup( &ctx, key, 16 );
  137. if( ( ret = mbedtls_arc4_crypt( &ctx, len, data, output ) ) != 0 )
  138. goto exit;
  139. exit:
  140. mbedtls_zeroize( key, sizeof( key ) );
  141. mbedtls_arc4_free( &ctx );
  142. return( ret );
  143. #endif /* MBEDTLS_ARC4_C */
  144. }
  145. int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
  146. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  147. const unsigned char *pwd, size_t pwdlen,
  148. const unsigned char *data, size_t len,
  149. unsigned char *output )
  150. {
  151. int ret, keylen = 0;
  152. unsigned char key[32];
  153. unsigned char iv[16];
  154. const mbedtls_cipher_info_t *cipher_info;
  155. mbedtls_cipher_context_t cipher_ctx;
  156. size_t olen = 0;
  157. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  158. if( cipher_info == NULL )
  159. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  160. keylen = cipher_info->key_bitlen / 8;
  161. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
  162. key, keylen,
  163. iv, cipher_info->iv_size ) ) != 0 )
  164. {
  165. return( ret );
  166. }
  167. mbedtls_cipher_init( &cipher_ctx );
  168. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
  169. goto exit;
  170. if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
  171. goto exit;
  172. if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
  173. goto exit;
  174. if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
  175. goto exit;
  176. if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
  177. output, &olen ) ) != 0 )
  178. {
  179. goto exit;
  180. }
  181. if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
  182. ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
  183. exit:
  184. mbedtls_zeroize( key, sizeof( key ) );
  185. mbedtls_zeroize( iv, sizeof( iv ) );
  186. mbedtls_cipher_free( &cipher_ctx );
  187. return( ret );
  188. }
  189. static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
  190. const unsigned char *filler, size_t fill_len )
  191. {
  192. unsigned char *p = data;
  193. size_t use_len;
  194. while( data_len > 0 )
  195. {
  196. use_len = ( data_len > fill_len ) ? fill_len : data_len;
  197. memcpy( p, filler, use_len );
  198. p += use_len;
  199. data_len -= use_len;
  200. }
  201. }
  202. int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
  203. const unsigned char *pwd, size_t pwdlen,
  204. const unsigned char *salt, size_t saltlen,
  205. mbedtls_md_type_t md_type, int id, int iterations )
  206. {
  207. int ret;
  208. unsigned int j;
  209. unsigned char diversifier[128];
  210. unsigned char salt_block[128], pwd_block[128], hash_block[128];
  211. unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
  212. unsigned char *p;
  213. unsigned char c;
  214. size_t hlen, use_len, v, i;
  215. const mbedtls_md_info_t *md_info;
  216. mbedtls_md_context_t md_ctx;
  217. // This version only allows max of 64 bytes of password or salt
  218. if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
  219. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  220. md_info = mbedtls_md_info_from_type( md_type );
  221. if( md_info == NULL )
  222. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  223. mbedtls_md_init( &md_ctx );
  224. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  225. return( ret );
  226. hlen = mbedtls_md_get_size( md_info );
  227. if( hlen <= 32 )
  228. v = 64;
  229. else
  230. v = 128;
  231. memset( diversifier, (unsigned char) id, v );
  232. pkcs12_fill_buffer( salt_block, v, salt, saltlen );
  233. pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
  234. p = data;
  235. while( datalen > 0 )
  236. {
  237. // Calculate hash( diversifier || salt_block || pwd_block )
  238. if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
  239. goto exit;
  240. if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
  241. goto exit;
  242. if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
  243. goto exit;
  244. if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
  245. goto exit;
  246. if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
  247. goto exit;
  248. // Perform remaining ( iterations - 1 ) recursive hash calculations
  249. for( i = 1; i < (size_t) iterations; i++ )
  250. {
  251. if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
  252. goto exit;
  253. }
  254. use_len = ( datalen > hlen ) ? hlen : datalen;
  255. memcpy( p, hash_output, use_len );
  256. datalen -= use_len;
  257. p += use_len;
  258. if( datalen == 0 )
  259. break;
  260. // Concatenating copies of hash_output into hash_block (B)
  261. pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
  262. // B += 1
  263. for( i = v; i > 0; i-- )
  264. if( ++hash_block[i - 1] != 0 )
  265. break;
  266. // salt_block += B
  267. c = 0;
  268. for( i = v; i > 0; i-- )
  269. {
  270. j = salt_block[i - 1] + hash_block[i - 1] + c;
  271. c = (unsigned char) (j >> 8);
  272. salt_block[i - 1] = j & 0xFF;
  273. }
  274. // pwd_block += B
  275. c = 0;
  276. for( i = v; i > 0; i-- )
  277. {
  278. j = pwd_block[i - 1] + hash_block[i - 1] + c;
  279. c = (unsigned char) (j >> 8);
  280. pwd_block[i - 1] = j & 0xFF;
  281. }
  282. }
  283. ret = 0;
  284. exit:
  285. mbedtls_zeroize( salt_block, sizeof( salt_block ) );
  286. mbedtls_zeroize( pwd_block, sizeof( pwd_block ) );
  287. mbedtls_zeroize( hash_block, sizeof( hash_block ) );
  288. mbedtls_zeroize( hash_output, sizeof( hash_output ) );
  289. mbedtls_md_free( &md_ctx );
  290. return( ret );
  291. }
  292. #endif /* MBEDTLS_PKCS12_C */