pkcs12.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * PKCS#12 Personal Information Exchange Syntax
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * The PKCS #12 Personal Information Exchange Syntax Standard v1.1
  9. *
  10. * http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
  11. * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
  12. */
  13. #include "common.h"
  14. #if defined(MBEDTLS_PKCS12_C)
  15. #include "mbedtls/pkcs12.h"
  16. #include "mbedtls/asn1.h"
  17. #include "mbedtls/cipher.h"
  18. #include "mbedtls/platform_util.h"
  19. #include "mbedtls/error.h"
  20. #include <string.h>
  21. #if defined(MBEDTLS_ARC4_C)
  22. #include "mbedtls/arc4.h"
  23. #endif
  24. #if defined(MBEDTLS_DES_C)
  25. #include "mbedtls/des.h"
  26. #endif
  27. #if defined(MBEDTLS_ASN1_PARSE_C)
  28. static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
  29. mbedtls_asn1_buf *salt, int *iterations)
  30. {
  31. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  32. unsigned char **p = &params->p;
  33. const unsigned char *end = params->p + params->len;
  34. /*
  35. * pkcs-12PbeParams ::= SEQUENCE {
  36. * salt OCTET STRING,
  37. * iterations INTEGER
  38. * }
  39. *
  40. */
  41. if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
  42. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
  43. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
  44. }
  45. if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
  46. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
  47. }
  48. salt->p = *p;
  49. *p += salt->len;
  50. if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) {
  51. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
  52. }
  53. if (*p != end) {
  54. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
  55. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  56. }
  57. return 0;
  58. }
  59. #define PKCS12_MAX_PWDLEN 128
  60. static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
  61. const unsigned char *pwd, size_t pwdlen,
  62. unsigned char *key, size_t keylen,
  63. unsigned char *iv, size_t ivlen)
  64. {
  65. int ret, iterations = 0;
  66. mbedtls_asn1_buf salt;
  67. size_t i;
  68. unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
  69. if (pwdlen > PKCS12_MAX_PWDLEN) {
  70. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  71. }
  72. memset(&salt, 0, sizeof(mbedtls_asn1_buf));
  73. memset(&unipwd, 0, sizeof(unipwd));
  74. if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt,
  75. &iterations)) != 0) {
  76. return ret;
  77. }
  78. for (i = 0; i < pwdlen; i++) {
  79. unipwd[i * 2 + 1] = pwd[i];
  80. }
  81. if ((ret = mbedtls_pkcs12_derivation(key, keylen, unipwd, pwdlen * 2 + 2,
  82. salt.p, salt.len, md_type,
  83. MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) {
  84. return ret;
  85. }
  86. if (iv == NULL || ivlen == 0) {
  87. return 0;
  88. }
  89. if ((ret = mbedtls_pkcs12_derivation(iv, ivlen, unipwd, pwdlen * 2 + 2,
  90. salt.p, salt.len, md_type,
  91. MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) {
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. #undef PKCS12_MAX_PWDLEN
  97. int mbedtls_pkcs12_pbe_sha1_rc4_128(mbedtls_asn1_buf *pbe_params, int mode,
  98. const unsigned char *pwd, size_t pwdlen,
  99. const unsigned char *data, size_t len,
  100. unsigned char *output)
  101. {
  102. #if !defined(MBEDTLS_ARC4_C)
  103. ((void) pbe_params);
  104. ((void) mode);
  105. ((void) pwd);
  106. ((void) pwdlen);
  107. ((void) data);
  108. ((void) len);
  109. ((void) output);
  110. return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
  111. #else
  112. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  113. unsigned char key[16];
  114. mbedtls_arc4_context ctx;
  115. ((void) mode);
  116. mbedtls_arc4_init(&ctx);
  117. if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, MBEDTLS_MD_SHA1,
  118. pwd, pwdlen,
  119. key, 16, NULL, 0)) != 0) {
  120. return ret;
  121. }
  122. mbedtls_arc4_setup(&ctx, key, 16);
  123. if ((ret = mbedtls_arc4_crypt(&ctx, len, data, output)) != 0) {
  124. goto exit;
  125. }
  126. exit:
  127. mbedtls_platform_zeroize(key, sizeof(key));
  128. mbedtls_arc4_free(&ctx);
  129. return ret;
  130. #endif /* MBEDTLS_ARC4_C */
  131. }
  132. #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  133. int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
  134. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  135. const unsigned char *pwd, size_t pwdlen,
  136. const unsigned char *data, size_t len,
  137. unsigned char *output, size_t output_size,
  138. size_t *output_len);
  139. #endif
  140. int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
  141. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  142. const unsigned char *pwd, size_t pwdlen,
  143. const unsigned char *data, size_t len,
  144. unsigned char *output)
  145. {
  146. size_t output_len = 0;
  147. /* We assume caller of the function is providing a big enough output buffer
  148. * so we pass output_size as SIZE_MAX to pass checks, However, no guarantees
  149. * for the output size actually being correct.
  150. */
  151. return mbedtls_pkcs12_pbe_ext(pbe_params, mode, cipher_type, md_type,
  152. pwd, pwdlen, data, len, output, SIZE_MAX,
  153. &output_len);
  154. }
  155. int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
  156. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  157. const unsigned char *pwd, size_t pwdlen,
  158. const unsigned char *data, size_t len,
  159. unsigned char *output, size_t output_size,
  160. size_t *output_len)
  161. {
  162. int ret, keylen = 0;
  163. unsigned char key[32];
  164. unsigned char iv[16];
  165. const mbedtls_cipher_info_t *cipher_info;
  166. mbedtls_cipher_context_t cipher_ctx;
  167. size_t finish_olen = 0;
  168. unsigned int padlen = 0;
  169. if (pwd == NULL && pwdlen != 0) {
  170. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  171. }
  172. cipher_info = mbedtls_cipher_info_from_type(cipher_type);
  173. if (cipher_info == NULL) {
  174. return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
  175. }
  176. keylen = cipher_info->key_bitlen / 8;
  177. if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
  178. if (output_size < len) {
  179. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  180. }
  181. }
  182. if (mode == MBEDTLS_PKCS12_PBE_ENCRYPT) {
  183. padlen = cipher_info->block_size - (len % cipher_info->block_size);
  184. if (output_size < (len + padlen)) {
  185. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  186. }
  187. }
  188. if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen,
  189. key, keylen,
  190. iv, cipher_info->iv_size)) != 0) {
  191. return ret;
  192. }
  193. mbedtls_cipher_init(&cipher_ctx);
  194. if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
  195. goto exit;
  196. }
  197. if ((ret =
  198. mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
  199. (mbedtls_operation_t) mode)) != 0) {
  200. goto exit;
  201. }
  202. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  203. {
  204. /* PKCS12 uses CBC with PKCS7 padding */
  205. mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7;
  206. #if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  207. /* For historical reasons, when decrypting, this function works when
  208. * decrypting even when support for PKCS7 padding is disabled. In this
  209. * case, it ignores the padding, and so will never report a
  210. * password mismatch.
  211. */
  212. if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
  213. padding = MBEDTLS_PADDING_NONE;
  214. }
  215. #endif
  216. if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) {
  217. goto exit;
  218. }
  219. }
  220. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  221. if ((ret = mbedtls_cipher_set_iv(&cipher_ctx, iv, cipher_info->iv_size)) != 0) {
  222. goto exit;
  223. }
  224. if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) {
  225. goto exit;
  226. }
  227. if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len,
  228. output, output_len)) != 0) {
  229. goto exit;
  230. }
  231. if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + (*output_len), &finish_olen)) != 0) {
  232. ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
  233. }
  234. *output_len += finish_olen;
  235. exit:
  236. mbedtls_platform_zeroize(key, sizeof(key));
  237. mbedtls_platform_zeroize(iv, sizeof(iv));
  238. mbedtls_cipher_free(&cipher_ctx);
  239. return ret;
  240. }
  241. #endif /* MBEDTLS_ASN1_PARSE_C */
  242. static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
  243. const unsigned char *filler, size_t fill_len)
  244. {
  245. unsigned char *p = data;
  246. size_t use_len;
  247. if (filler != NULL && fill_len != 0) {
  248. while (data_len > 0) {
  249. use_len = (data_len > fill_len) ? fill_len : data_len;
  250. memcpy(p, filler, use_len);
  251. p += use_len;
  252. data_len -= use_len;
  253. }
  254. } else {
  255. /* If either of the above are not true then clearly there is nothing
  256. * that this function can do. The function should *not* be called
  257. * under either of those circumstances, as you could end up with an
  258. * incorrect output but for safety's sake, leaving the check in as
  259. * otherwise we could end up with memory corruption.*/
  260. }
  261. }
  262. int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
  263. const unsigned char *pwd, size_t pwdlen,
  264. const unsigned char *salt, size_t saltlen,
  265. mbedtls_md_type_t md_type, int id, int iterations)
  266. {
  267. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  268. unsigned int j;
  269. unsigned char diversifier[128];
  270. unsigned char salt_block[128], pwd_block[128], hash_block[128];
  271. unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
  272. unsigned char *p;
  273. unsigned char c;
  274. int use_password = 0;
  275. int use_salt = 0;
  276. size_t hlen, use_len, v, i;
  277. const mbedtls_md_info_t *md_info;
  278. mbedtls_md_context_t md_ctx;
  279. // This version only allows max of 64 bytes of password or salt
  280. if (datalen > 128 || pwdlen > 64 || saltlen > 64) {
  281. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  282. }
  283. if (pwd == NULL && pwdlen != 0) {
  284. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  285. }
  286. if (salt == NULL && saltlen != 0) {
  287. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  288. }
  289. use_password = (pwd && pwdlen != 0);
  290. use_salt = (salt && saltlen != 0);
  291. md_info = mbedtls_md_info_from_type(md_type);
  292. if (md_info == NULL) {
  293. return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
  294. }
  295. mbedtls_md_init(&md_ctx);
  296. if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
  297. return ret;
  298. }
  299. hlen = mbedtls_md_get_size(md_info);
  300. if (hlen <= 32) {
  301. v = 64;
  302. } else {
  303. v = 128;
  304. }
  305. memset(diversifier, (unsigned char) id, v);
  306. if (use_salt != 0) {
  307. pkcs12_fill_buffer(salt_block, v, salt, saltlen);
  308. }
  309. if (use_password != 0) {
  310. pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen);
  311. }
  312. p = data;
  313. while (datalen > 0) {
  314. // Calculate hash( diversifier || salt_block || pwd_block )
  315. if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
  316. goto exit;
  317. }
  318. if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) {
  319. goto exit;
  320. }
  321. if (use_salt != 0) {
  322. if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) {
  323. goto exit;
  324. }
  325. }
  326. if (use_password != 0) {
  327. if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) {
  328. goto exit;
  329. }
  330. }
  331. if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) {
  332. goto exit;
  333. }
  334. // Perform remaining ( iterations - 1 ) recursive hash calculations
  335. for (i = 1; i < (size_t) iterations; i++) {
  336. if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) != 0) {
  337. goto exit;
  338. }
  339. }
  340. use_len = (datalen > hlen) ? hlen : datalen;
  341. memcpy(p, hash_output, use_len);
  342. datalen -= use_len;
  343. p += use_len;
  344. if (datalen == 0) {
  345. break;
  346. }
  347. // Concatenating copies of hash_output into hash_block (B)
  348. pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
  349. // B += 1
  350. for (i = v; i > 0; i--) {
  351. if (++hash_block[i - 1] != 0) {
  352. break;
  353. }
  354. }
  355. if (use_salt != 0) {
  356. // salt_block += B
  357. c = 0;
  358. for (i = v; i > 0; i--) {
  359. j = salt_block[i - 1] + hash_block[i - 1] + c;
  360. c = MBEDTLS_BYTE_1(j);
  361. salt_block[i - 1] = MBEDTLS_BYTE_0(j);
  362. }
  363. }
  364. if (use_password != 0) {
  365. // pwd_block += B
  366. c = 0;
  367. for (i = v; i > 0; i--) {
  368. j = pwd_block[i - 1] + hash_block[i - 1] + c;
  369. c = MBEDTLS_BYTE_1(j);
  370. pwd_block[i - 1] = MBEDTLS_BYTE_0(j);
  371. }
  372. }
  373. }
  374. ret = 0;
  375. exit:
  376. mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
  377. mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
  378. mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
  379. mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
  380. mbedtls_md_free(&md_ctx);
  381. return ret;
  382. }
  383. #endif /* MBEDTLS_PKCS12_C */