pkcs12.c 11 KB

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