pkcs12.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. }
  57. if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
  58. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
  59. }
  60. salt->p = *p;
  61. *p += salt->len;
  62. if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) {
  63. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
  64. }
  65. if (*p != end) {
  66. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
  67. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  68. }
  69. return 0;
  70. }
  71. #define PKCS12_MAX_PWDLEN 128
  72. static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
  73. const unsigned char *pwd, size_t pwdlen,
  74. unsigned char *key, size_t keylen,
  75. unsigned char *iv, size_t ivlen)
  76. {
  77. int ret, iterations = 0;
  78. mbedtls_asn1_buf salt;
  79. size_t i;
  80. unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
  81. if (pwdlen > PKCS12_MAX_PWDLEN) {
  82. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  83. }
  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. }
  90. for (i = 0; i < pwdlen; i++) {
  91. unipwd[i * 2 + 1] = pwd[i];
  92. }
  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. return ret;
  97. }
  98. if (iv == NULL || ivlen == 0) {
  99. return 0;
  100. }
  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. return ret;
  105. }
  106. return 0;
  107. }
  108. #undef PKCS12_MAX_PWDLEN
  109. int mbedtls_pkcs12_pbe_sha1_rc4_128(mbedtls_asn1_buf *pbe_params, int mode,
  110. const unsigned char *pwd, size_t pwdlen,
  111. const unsigned char *data, size_t len,
  112. unsigned char *output)
  113. {
  114. #if !defined(MBEDTLS_ARC4_C)
  115. ((void) pbe_params);
  116. ((void) mode);
  117. ((void) pwd);
  118. ((void) pwdlen);
  119. ((void) data);
  120. ((void) len);
  121. ((void) output);
  122. return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
  123. #else
  124. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  125. unsigned char key[16];
  126. mbedtls_arc4_context ctx;
  127. ((void) mode);
  128. mbedtls_arc4_init(&ctx);
  129. if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, MBEDTLS_MD_SHA1,
  130. pwd, pwdlen,
  131. key, 16, NULL, 0)) != 0) {
  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. }
  138. exit:
  139. mbedtls_platform_zeroize(key, sizeof(key));
  140. mbedtls_arc4_free(&ctx);
  141. return ret;
  142. #endif /* MBEDTLS_ARC4_C */
  143. }
  144. int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
  145. mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
  146. const unsigned char *pwd, size_t pwdlen,
  147. const unsigned char *data, size_t len,
  148. unsigned char *output)
  149. {
  150. int ret, keylen = 0;
  151. unsigned char key[32];
  152. unsigned char iv[16];
  153. const mbedtls_cipher_info_t *cipher_info;
  154. mbedtls_cipher_context_t cipher_ctx;
  155. size_t olen = 0;
  156. if (pwd == NULL && pwdlen != 0) {
  157. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  158. }
  159. cipher_info = mbedtls_cipher_info_from_type(cipher_type);
  160. if (cipher_info == NULL) {
  161. return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
  162. }
  163. keylen = cipher_info->key_bitlen / 8;
  164. if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen,
  165. key, keylen,
  166. iv, cipher_info->iv_size)) != 0) {
  167. return ret;
  168. }
  169. mbedtls_cipher_init(&cipher_ctx);
  170. if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
  171. goto exit;
  172. }
  173. if ((ret =
  174. mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
  175. (mbedtls_operation_t) mode)) != 0) {
  176. goto exit;
  177. }
  178. if ((ret = mbedtls_cipher_set_iv(&cipher_ctx, iv, cipher_info->iv_size)) != 0) {
  179. goto exit;
  180. }
  181. if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) {
  182. goto exit;
  183. }
  184. if ((ret = mbedtls_cipher_update(&cipher_ctx, data, len,
  185. output, &olen)) != 0) {
  186. goto exit;
  187. }
  188. if ((ret = mbedtls_cipher_finish(&cipher_ctx, output + olen, &olen)) != 0) {
  189. ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
  190. }
  191. exit:
  192. mbedtls_platform_zeroize(key, sizeof(key));
  193. mbedtls_platform_zeroize(iv, sizeof(iv));
  194. mbedtls_cipher_free(&cipher_ctx);
  195. return ret;
  196. }
  197. #endif /* MBEDTLS_ASN1_PARSE_C */
  198. static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
  199. const unsigned char *filler, size_t fill_len)
  200. {
  201. unsigned char *p = data;
  202. size_t use_len;
  203. if (filler != NULL && fill_len != 0) {
  204. while (data_len > 0) {
  205. use_len = (data_len > fill_len) ? fill_len : data_len;
  206. memcpy(p, filler, use_len);
  207. p += use_len;
  208. data_len -= use_len;
  209. }
  210. } else {
  211. /* If either of the above are not true then clearly there is nothing
  212. * that this function can do. The function should *not* be called
  213. * under either of those circumstances, as you could end up with an
  214. * incorrect output but for safety's sake, leaving the check in as
  215. * otherwise we could end up with memory corruption.*/
  216. }
  217. }
  218. int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
  219. const unsigned char *pwd, size_t pwdlen,
  220. const unsigned char *salt, size_t saltlen,
  221. mbedtls_md_type_t md_type, int id, int iterations)
  222. {
  223. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  224. unsigned int j;
  225. unsigned char diversifier[128];
  226. unsigned char salt_block[128], pwd_block[128], hash_block[128];
  227. unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
  228. unsigned char *p;
  229. unsigned char c;
  230. int use_password = 0;
  231. int use_salt = 0;
  232. size_t hlen, use_len, v, i;
  233. const mbedtls_md_info_t *md_info;
  234. mbedtls_md_context_t md_ctx;
  235. // This version only allows max of 64 bytes of password or salt
  236. if (datalen > 128 || pwdlen > 64 || saltlen > 64) {
  237. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  238. }
  239. if (pwd == NULL && pwdlen != 0) {
  240. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  241. }
  242. if (salt == NULL && saltlen != 0) {
  243. return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
  244. }
  245. use_password = (pwd && pwdlen != 0);
  246. use_salt = (salt && saltlen != 0);
  247. md_info = mbedtls_md_info_from_type(md_type);
  248. if (md_info == NULL) {
  249. return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
  250. }
  251. mbedtls_md_init(&md_ctx);
  252. if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
  253. return ret;
  254. }
  255. hlen = mbedtls_md_get_size(md_info);
  256. if (hlen <= 32) {
  257. v = 64;
  258. } else {
  259. v = 128;
  260. }
  261. memset(diversifier, (unsigned char) id, v);
  262. if (use_salt != 0) {
  263. pkcs12_fill_buffer(salt_block, v, salt, saltlen);
  264. }
  265. if (use_password != 0) {
  266. pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen);
  267. }
  268. p = data;
  269. while (datalen > 0) {
  270. // Calculate hash( diversifier || salt_block || pwd_block )
  271. if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
  272. goto exit;
  273. }
  274. if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) {
  275. goto exit;
  276. }
  277. if (use_salt != 0) {
  278. if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) {
  279. goto exit;
  280. }
  281. }
  282. if (use_password != 0) {
  283. if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) {
  284. goto exit;
  285. }
  286. }
  287. if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) {
  288. goto exit;
  289. }
  290. // Perform remaining ( iterations - 1 ) recursive hash calculations
  291. for (i = 1; i < (size_t) iterations; i++) {
  292. if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output)) != 0) {
  293. goto exit;
  294. }
  295. }
  296. use_len = (datalen > hlen) ? hlen : datalen;
  297. memcpy(p, hash_output, use_len);
  298. datalen -= use_len;
  299. p += use_len;
  300. if (datalen == 0) {
  301. break;
  302. }
  303. // Concatenating copies of hash_output into hash_block (B)
  304. pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
  305. // B += 1
  306. for (i = v; i > 0; i--) {
  307. if (++hash_block[i - 1] != 0) {
  308. break;
  309. }
  310. }
  311. if (use_salt != 0) {
  312. // salt_block += B
  313. c = 0;
  314. for (i = v; i > 0; i--) {
  315. j = salt_block[i - 1] + hash_block[i - 1] + c;
  316. c = MBEDTLS_BYTE_1(j);
  317. salt_block[i - 1] = MBEDTLS_BYTE_0(j);
  318. }
  319. }
  320. if (use_password != 0) {
  321. // pwd_block += B
  322. c = 0;
  323. for (i = v; i > 0; i--) {
  324. j = pwd_block[i - 1] + hash_block[i - 1] + c;
  325. c = MBEDTLS_BYTE_1(j);
  326. pwd_block[i - 1] = MBEDTLS_BYTE_0(j);
  327. }
  328. }
  329. }
  330. ret = 0;
  331. exit:
  332. mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
  333. mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
  334. mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
  335. mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
  336. mbedtls_md_free(&md_ctx);
  337. return ret;
  338. }
  339. #endif /* MBEDTLS_PKCS12_C */