p12_mutl.c 8.6 KB

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