p12_mutl.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright 1999-2020 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_type;
  77. HMAC_CTX *hmac = NULL;
  78. unsigned char key[EVP_MAX_MD_SIZE], *salt;
  79. int saltlen, iter;
  80. int md_size = 0;
  81. int md_type_nid;
  82. const X509_ALGOR *macalg;
  83. const ASN1_OBJECT *macoid;
  84. if (pkcs12_key_gen == NULL)
  85. pkcs12_key_gen = PKCS12_key_gen_utf8;
  86. if (!PKCS7_type_is_data(p12->authsafes)) {
  87. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, 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 ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
  99. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
  100. return 0;
  101. }
  102. md_size = EVP_MD_size(md_type);
  103. md_type_nid = EVP_MD_type(md_type);
  104. if (md_size < 0)
  105. return 0;
  106. if ((md_type_nid == NID_id_GostR3411_94
  107. || md_type_nid == NID_id_GostR3411_2012_256
  108. || md_type_nid == NID_id_GostR3411_2012_512)
  109. && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) {
  110. md_size = TK26_MAC_KEY_LEN;
  111. if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
  112. md_size, key, md_type)) {
  113. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
  114. goto err;
  115. }
  116. } else
  117. if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
  118. iter, md_size, key, md_type)) {
  119. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
  120. goto err;
  121. }
  122. if ((hmac = HMAC_CTX_new()) == NULL
  123. || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
  124. || !HMAC_Update(hmac, p12->authsafes->d.data->data,
  125. p12->authsafes->d.data->length)
  126. || !HMAC_Final(hmac, mac, maclen)) {
  127. goto err;
  128. }
  129. ret = 1;
  130. err:
  131. OPENSSL_cleanse(key, sizeof(key));
  132. HMAC_CTX_free(hmac);
  133. return ret;
  134. }
  135. int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
  136. unsigned char *mac, unsigned int *maclen)
  137. {
  138. return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
  139. }
  140. /* Verify the mac */
  141. int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
  142. {
  143. unsigned char mac[EVP_MAX_MD_SIZE];
  144. unsigned int maclen;
  145. const ASN1_OCTET_STRING *macoct;
  146. if (p12->mac == NULL) {
  147. PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
  148. return 0;
  149. }
  150. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
  151. PKCS12_key_gen_utf8)) {
  152. PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
  153. return 0;
  154. }
  155. X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
  156. if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
  157. || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
  158. return 0;
  159. return 1;
  160. }
  161. /* Set a mac */
  162. int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
  163. unsigned char *salt, int saltlen, int iter,
  164. const EVP_MD *md_type)
  165. {
  166. unsigned char mac[EVP_MAX_MD_SIZE];
  167. unsigned int maclen;
  168. ASN1_OCTET_STRING *macoct;
  169. if (!md_type)
  170. md_type = EVP_sha1();
  171. if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
  172. PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_SETUP_ERROR);
  173. return 0;
  174. }
  175. /*
  176. * Note that output mac is forced to UTF-8...
  177. */
  178. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
  179. PKCS12_key_gen_utf8)) {
  180. PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
  181. return 0;
  182. }
  183. X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
  184. if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
  185. PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. /* Set up a mac structure */
  191. int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
  192. const EVP_MD *md_type)
  193. {
  194. X509_ALGOR *macalg;
  195. PKCS12_MAC_DATA_free(p12->mac);
  196. p12->mac = NULL;
  197. if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
  198. return PKCS12_ERROR;
  199. if (iter > 1) {
  200. if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
  201. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  202. return 0;
  203. }
  204. if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
  205. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  206. return 0;
  207. }
  208. }
  209. if (!saltlen)
  210. saltlen = PKCS12_SALT_LEN;
  211. if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
  212. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  213. return 0;
  214. }
  215. p12->mac->salt->length = saltlen;
  216. if (!salt) {
  217. if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
  218. return 0;
  219. } else
  220. memcpy(p12->mac->salt->data, salt, saltlen);
  221. X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
  222. if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
  223. V_ASN1_NULL, NULL)) {
  224. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  225. return 0;
  226. }
  227. return 1;
  228. }