pkcs12.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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_ERROR_ADD( 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_ERROR_ADD( 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_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) );
  62. if( *p != end )
  63. return( MBEDTLS_ERROR_ADD( 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. if( pwd == NULL && pwdlen != 0 )
  151. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  152. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  153. if( cipher_info == NULL )
  154. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  155. keylen = cipher_info->key_bitlen / 8;
  156. if( ( ret = pkcs12_pbe_derive_key_iv( pbe_params, md_type, pwd, pwdlen,
  157. key, keylen,
  158. iv, cipher_info->iv_size ) ) != 0 )
  159. {
  160. return( ret );
  161. }
  162. mbedtls_cipher_init( &cipher_ctx );
  163. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info ) ) != 0 )
  164. goto exit;
  165. if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
  166. goto exit;
  167. if( ( ret = mbedtls_cipher_set_iv( &cipher_ctx, iv, cipher_info->iv_size ) ) != 0 )
  168. goto exit;
  169. if( ( ret = mbedtls_cipher_reset( &cipher_ctx ) ) != 0 )
  170. goto exit;
  171. if( ( ret = mbedtls_cipher_update( &cipher_ctx, data, len,
  172. output, &olen ) ) != 0 )
  173. {
  174. goto exit;
  175. }
  176. if( ( ret = mbedtls_cipher_finish( &cipher_ctx, output + olen, &olen ) ) != 0 )
  177. ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
  178. exit:
  179. mbedtls_platform_zeroize( key, sizeof( key ) );
  180. mbedtls_platform_zeroize( iv, sizeof( iv ) );
  181. mbedtls_cipher_free( &cipher_ctx );
  182. return( ret );
  183. }
  184. #endif /* MBEDTLS_ASN1_PARSE_C */
  185. static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
  186. const unsigned char *filler, size_t fill_len )
  187. {
  188. unsigned char *p = data;
  189. size_t use_len;
  190. if( filler != NULL && fill_len != 0 )
  191. {
  192. while( data_len > 0 )
  193. {
  194. use_len = ( data_len > fill_len ) ? fill_len : data_len;
  195. memcpy( p, filler, use_len );
  196. p += use_len;
  197. data_len -= use_len;
  198. }
  199. }
  200. else
  201. {
  202. /* If either of the above are not true then clearly there is nothing
  203. * that this function can do. The function should *not* be called
  204. * under either of those circumstances, as you could end up with an
  205. * incorrect output but for safety's sake, leaving the check in as
  206. * otherwise we could end up with memory corruption.*/
  207. }
  208. }
  209. int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
  210. const unsigned char *pwd, size_t pwdlen,
  211. const unsigned char *salt, size_t saltlen,
  212. mbedtls_md_type_t md_type, int id, int iterations )
  213. {
  214. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  215. unsigned int j;
  216. unsigned char diversifier[128];
  217. unsigned char salt_block[128], pwd_block[128], hash_block[128];
  218. unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
  219. unsigned char *p;
  220. unsigned char c;
  221. int use_password = 0;
  222. int use_salt = 0;
  223. size_t hlen, use_len, v, i;
  224. const mbedtls_md_info_t *md_info;
  225. mbedtls_md_context_t md_ctx;
  226. // This version only allows max of 64 bytes of password or salt
  227. if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
  228. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  229. if( pwd == NULL && pwdlen != 0 )
  230. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  231. if( salt == NULL && saltlen != 0 )
  232. return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
  233. use_password = ( pwd && pwdlen != 0 );
  234. use_salt = ( salt && saltlen != 0 );
  235. md_info = mbedtls_md_info_from_type( md_type );
  236. if( md_info == NULL )
  237. return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
  238. mbedtls_md_init( &md_ctx );
  239. if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
  240. return( ret );
  241. hlen = mbedtls_md_get_size( md_info );
  242. if( hlen <= 32 )
  243. v = 64;
  244. else
  245. v = 128;
  246. memset( diversifier, (unsigned char) id, v );
  247. if( use_salt != 0 )
  248. {
  249. pkcs12_fill_buffer( salt_block, v, salt, saltlen );
  250. }
  251. if( use_password != 0 )
  252. {
  253. pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
  254. }
  255. p = data;
  256. while( datalen > 0 )
  257. {
  258. // Calculate hash( diversifier || salt_block || pwd_block )
  259. if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
  260. goto exit;
  261. if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
  262. goto exit;
  263. if( use_salt != 0 )
  264. {
  265. if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v )) != 0 )
  266. goto exit;
  267. }
  268. if( use_password != 0)
  269. {
  270. if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v )) != 0 )
  271. goto exit;
  272. }
  273. if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
  274. goto exit;
  275. // Perform remaining ( iterations - 1 ) recursive hash calculations
  276. for( i = 1; i < (size_t) iterations; i++ )
  277. {
  278. if( ( ret = mbedtls_md( md_info, hash_output, hlen, hash_output ) ) != 0 )
  279. goto exit;
  280. }
  281. use_len = ( datalen > hlen ) ? hlen : datalen;
  282. memcpy( p, hash_output, use_len );
  283. datalen -= use_len;
  284. p += use_len;
  285. if( datalen == 0 )
  286. break;
  287. // Concatenating copies of hash_output into hash_block (B)
  288. pkcs12_fill_buffer( hash_block, v, hash_output, hlen );
  289. // B += 1
  290. for( i = v; i > 0; i-- )
  291. if( ++hash_block[i - 1] != 0 )
  292. break;
  293. if( use_salt != 0 )
  294. {
  295. // salt_block += B
  296. c = 0;
  297. for( i = v; i > 0; i-- )
  298. {
  299. j = salt_block[i - 1] + hash_block[i - 1] + c;
  300. c = MBEDTLS_BYTE_1( j );
  301. salt_block[i - 1] = MBEDTLS_BYTE_0( j );
  302. }
  303. }
  304. if( use_password != 0 )
  305. {
  306. // pwd_block += B
  307. c = 0;
  308. for( i = v; i > 0; i-- )
  309. {
  310. j = pwd_block[i - 1] + hash_block[i - 1] + c;
  311. c = MBEDTLS_BYTE_1( j );
  312. pwd_block[i - 1] = MBEDTLS_BYTE_0( j );
  313. }
  314. }
  315. }
  316. ret = 0;
  317. exit:
  318. mbedtls_platform_zeroize( salt_block, sizeof( salt_block ) );
  319. mbedtls_platform_zeroize( pwd_block, sizeof( pwd_block ) );
  320. mbedtls_platform_zeroize( hash_block, sizeof( hash_block ) );
  321. mbedtls_platform_zeroize( hash_output, sizeof( hash_output ) );
  322. mbedtls_md_free( &md_ctx );
  323. return( ret );
  324. }
  325. #endif /* MBEDTLS_PKCS12_C */