p12_mutl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * HMAC low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/crypto.h>
  17. #include <openssl/hmac.h>
  18. #include <openssl/rand.h>
  19. #include <openssl/pkcs12.h>
  20. #include "p12_local.h"
  21. int PKCS12_mac_present(const PKCS12 *p12)
  22. {
  23. return p12->mac ? 1 : 0;
  24. }
  25. void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac,
  26. const X509_ALGOR **pmacalg,
  27. const ASN1_OCTET_STRING **psalt,
  28. const ASN1_INTEGER **piter,
  29. const PKCS12 *p12)
  30. {
  31. if (p12->mac) {
  32. X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
  33. if (psalt)
  34. *psalt = p12->mac->salt;
  35. if (piter)
  36. *piter = p12->mac->iter;
  37. } else {
  38. if (pmac)
  39. *pmac = NULL;
  40. if (pmacalg)
  41. *pmacalg = NULL;
  42. if (psalt)
  43. *psalt = NULL;
  44. if (piter)
  45. *piter = NULL;
  46. }
  47. }
  48. #define TK26_MAC_KEY_LEN 32
  49. static int pkcs12_gen_gost_mac_key(const char *pass, int passlen,
  50. const unsigned char *salt, int saltlen,
  51. int iter, int keylen, unsigned char *key,
  52. const EVP_MD *digest)
  53. {
  54. unsigned char out[96];
  55. if (keylen != TK26_MAC_KEY_LEN) {
  56. return 0;
  57. }
  58. if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter,
  59. digest, sizeof(out), out)) {
  60. return 0;
  61. }
  62. memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN);
  63. OPENSSL_cleanse(out, sizeof(out));
  64. return 1;
  65. }
  66. /* Generate a MAC */
  67. static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
  68. unsigned char *mac, unsigned int *maclen,
  69. int (*pkcs12_key_gen)(const char *pass, int passlen,
  70. unsigned char *salt, int slen,
  71. int id, int iter, int n,
  72. unsigned char *out,
  73. const EVP_MD *md_type))
  74. {
  75. int ret = 0;
  76. const EVP_MD *md;
  77. EVP_MD *md_fetch;
  78. HMAC_CTX *hmac = NULL;
  79. unsigned char key[EVP_MAX_MD_SIZE], *salt;
  80. int saltlen, iter;
  81. char md_name[80];
  82. int md_size = 0;
  83. int md_nid;
  84. const X509_ALGOR *macalg;
  85. const ASN1_OBJECT *macoid;
  86. if (!PKCS7_type_is_data(p12->authsafes)) {
  87. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
  88. return 0;
  89. }
  90. salt = p12->mac->salt->data;
  91. saltlen = p12->mac->salt->length;
  92. if (p12->mac->iter == NULL)
  93. iter = 1;
  94. else
  95. iter = ASN1_INTEGER_get(p12->mac->iter);
  96. X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
  97. X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
  98. if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0)
  99. return 0;
  100. (void)ERR_set_mark();
  101. md = md_fetch = EVP_MD_fetch(p12->authsafes->ctx.libctx, md_name,
  102. p12->authsafes->ctx.propq);
  103. if (md == NULL)
  104. md = EVP_get_digestbynid(OBJ_obj2nid(macoid));
  105. if (md == NULL) {
  106. (void)ERR_clear_last_mark();
  107. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
  108. return 0;
  109. }
  110. (void)ERR_pop_to_mark();
  111. md_size = EVP_MD_get_size(md);
  112. md_nid = EVP_MD_get_type(md);
  113. if (md_size < 0)
  114. goto err;
  115. if ((md_nid == NID_id_GostR3411_94
  116. || md_nid == NID_id_GostR3411_2012_256
  117. || md_nid == NID_id_GostR3411_2012_512)
  118. && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) {
  119. md_size = TK26_MAC_KEY_LEN;
  120. if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
  121. md_size, key, md)) {
  122. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
  123. goto err;
  124. }
  125. } else {
  126. if (pkcs12_key_gen != NULL) {
  127. if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
  128. iter, md_size, key, md)) {
  129. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
  130. goto err;
  131. }
  132. } else {
  133. /* Default to UTF-8 password */
  134. if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
  135. iter, md_size, key, md,
  136. p12->authsafes->ctx.libctx,
  137. p12->authsafes->ctx.propq)) {
  138. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
  139. goto err;
  140. }
  141. }
  142. }
  143. if ((hmac = HMAC_CTX_new()) == NULL
  144. || !HMAC_Init_ex(hmac, key, md_size, md, NULL)
  145. || !HMAC_Update(hmac, p12->authsafes->d.data->data,
  146. p12->authsafes->d.data->length)
  147. || !HMAC_Final(hmac, mac, maclen)) {
  148. goto err;
  149. }
  150. ret = 1;
  151. err:
  152. OPENSSL_cleanse(key, sizeof(key));
  153. HMAC_CTX_free(hmac);
  154. EVP_MD_free(md_fetch);
  155. return ret;
  156. }
  157. int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
  158. unsigned char *mac, unsigned int *maclen)
  159. {
  160. return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
  161. }
  162. /* Verify the mac */
  163. int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
  164. {
  165. unsigned char mac[EVP_MAX_MD_SIZE];
  166. unsigned int maclen;
  167. const ASN1_OCTET_STRING *macoct;
  168. if (p12->mac == NULL) {
  169. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT);
  170. return 0;
  171. }
  172. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) {
  173. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
  174. return 0;
  175. }
  176. X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
  177. if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
  178. || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
  179. return 0;
  180. return 1;
  181. }
  182. /* Set a mac */
  183. int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
  184. unsigned char *salt, int saltlen, int iter,
  185. const EVP_MD *md_type)
  186. {
  187. unsigned char mac[EVP_MAX_MD_SIZE];
  188. unsigned int maclen;
  189. ASN1_OCTET_STRING *macoct;
  190. if (md_type == NULL)
  191. /* No need to do a fetch as the md_type is used only to get a NID */
  192. md_type = EVP_sha256();
  193. if (!iter)
  194. iter = PKCS12_DEFAULT_ITER;
  195. if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
  196. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR);
  197. return 0;
  198. }
  199. /*
  200. * Note that output mac is forced to UTF-8...
  201. */
  202. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) {
  203. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
  204. return 0;
  205. }
  206. X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
  207. if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
  208. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR);
  209. return 0;
  210. }
  211. return 1;
  212. }
  213. /* Set up a mac structure */
  214. int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
  215. const EVP_MD *md_type)
  216. {
  217. X509_ALGOR *macalg;
  218. PKCS12_MAC_DATA_free(p12->mac);
  219. p12->mac = NULL;
  220. if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
  221. return PKCS12_ERROR;
  222. if (iter > 1) {
  223. if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
  224. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  225. return 0;
  226. }
  227. if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
  228. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  229. return 0;
  230. }
  231. }
  232. if (saltlen == 0)
  233. saltlen = PKCS12_SALT_LEN;
  234. else if (saltlen < 0)
  235. return 0;
  236. if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL)
  237. return 0;
  238. p12->mac->salt->length = saltlen;
  239. if (salt == NULL) {
  240. if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data,
  241. (size_t)saltlen, 0) <= 0)
  242. return 0;
  243. } else {
  244. memcpy(p12->mac->salt->data, salt, saltlen);
  245. }
  246. X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
  247. if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_get_type(md_type)),
  248. V_ASN1_NULL, NULL)) {
  249. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  250. return 0;
  251. }
  252. return 1;
  253. }