p12_mutl.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 1999-2024 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. if (p12->authsafes->d.data == NULL) {
  91. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
  92. return 0;
  93. }
  94. salt = p12->mac->salt->data;
  95. saltlen = p12->mac->salt->length;
  96. if (p12->mac->iter == NULL)
  97. iter = 1;
  98. else
  99. iter = ASN1_INTEGER_get(p12->mac->iter);
  100. X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
  101. X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
  102. if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0)
  103. return 0;
  104. (void)ERR_set_mark();
  105. md = md_fetch = EVP_MD_fetch(p12->authsafes->ctx.libctx, md_name,
  106. p12->authsafes->ctx.propq);
  107. if (md == NULL)
  108. md = EVP_get_digestbynid(OBJ_obj2nid(macoid));
  109. if (md == NULL) {
  110. (void)ERR_clear_last_mark();
  111. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
  112. return 0;
  113. }
  114. (void)ERR_pop_to_mark();
  115. md_size = EVP_MD_get_size(md);
  116. md_nid = EVP_MD_get_type(md);
  117. if (md_size < 0)
  118. goto err;
  119. if ((md_nid == NID_id_GostR3411_94
  120. || md_nid == NID_id_GostR3411_2012_256
  121. || md_nid == NID_id_GostR3411_2012_512)
  122. && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) {
  123. md_size = TK26_MAC_KEY_LEN;
  124. if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
  125. md_size, key, md)) {
  126. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
  127. goto err;
  128. }
  129. } else {
  130. if (pkcs12_key_gen != NULL) {
  131. if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
  132. iter, md_size, key, md)) {
  133. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
  134. goto err;
  135. }
  136. } else {
  137. /* Default to UTF-8 password */
  138. if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
  139. iter, md_size, key, md,
  140. p12->authsafes->ctx.libctx,
  141. p12->authsafes->ctx.propq)) {
  142. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
  143. goto err;
  144. }
  145. }
  146. }
  147. if ((hmac = HMAC_CTX_new()) == NULL
  148. || !HMAC_Init_ex(hmac, key, md_size, md, NULL)
  149. || !HMAC_Update(hmac, p12->authsafes->d.data->data,
  150. p12->authsafes->d.data->length)
  151. || !HMAC_Final(hmac, mac, maclen)) {
  152. goto err;
  153. }
  154. ret = 1;
  155. err:
  156. OPENSSL_cleanse(key, sizeof(key));
  157. HMAC_CTX_free(hmac);
  158. EVP_MD_free(md_fetch);
  159. return ret;
  160. }
  161. int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
  162. unsigned char *mac, unsigned int *maclen)
  163. {
  164. return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
  165. }
  166. /* Verify the mac */
  167. int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
  168. {
  169. unsigned char mac[EVP_MAX_MD_SIZE];
  170. unsigned int maclen;
  171. const ASN1_OCTET_STRING *macoct;
  172. if (p12->mac == NULL) {
  173. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT);
  174. return 0;
  175. }
  176. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) {
  177. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
  178. return 0;
  179. }
  180. X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
  181. if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
  182. || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
  183. return 0;
  184. return 1;
  185. }
  186. /* Set a mac */
  187. int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
  188. unsigned char *salt, int saltlen, int iter,
  189. const EVP_MD *md_type)
  190. {
  191. unsigned char mac[EVP_MAX_MD_SIZE];
  192. unsigned int maclen;
  193. ASN1_OCTET_STRING *macoct;
  194. if (md_type == NULL)
  195. /* No need to do a fetch as the md_type is used only to get a NID */
  196. md_type = EVP_sha256();
  197. if (!iter)
  198. iter = PKCS12_DEFAULT_ITER;
  199. if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
  200. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR);
  201. return 0;
  202. }
  203. /*
  204. * Note that output mac is forced to UTF-8...
  205. */
  206. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) {
  207. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
  208. return 0;
  209. }
  210. X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
  211. if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
  212. ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR);
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. /* Set up a mac structure */
  218. int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
  219. const EVP_MD *md_type)
  220. {
  221. X509_ALGOR *macalg;
  222. PKCS12_MAC_DATA_free(p12->mac);
  223. p12->mac = NULL;
  224. if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
  225. return PKCS12_ERROR;
  226. if (iter > 1) {
  227. if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
  228. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  229. return 0;
  230. }
  231. if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
  232. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  233. return 0;
  234. }
  235. }
  236. if (saltlen == 0)
  237. saltlen = PKCS12_SALT_LEN;
  238. else if (saltlen < 0)
  239. return 0;
  240. if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL)
  241. return 0;
  242. p12->mac->salt->length = saltlen;
  243. if (salt == NULL) {
  244. if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data,
  245. (size_t)saltlen, 0) <= 0)
  246. return 0;
  247. } else {
  248. memcpy(p12->mac->salt->data, salt, saltlen);
  249. }
  250. X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
  251. if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_get_type(md_type)),
  252. V_ASN1_NULL, NULL)) {
  253. ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
  254. return 0;
  255. }
  256. return 1;
  257. }