p12_mutl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/crypto.h>
  12. #include <openssl/hmac.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/pkcs12.h>
  15. #include "p12_lcl.h"
  16. int PKCS12_mac_present(const PKCS12 *p12)
  17. {
  18. return p12->mac ? 1 : 0;
  19. }
  20. void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac,
  21. const X509_ALGOR **pmacalg,
  22. const ASN1_OCTET_STRING **psalt,
  23. const ASN1_INTEGER **piter,
  24. const PKCS12 *p12)
  25. {
  26. if (p12->mac) {
  27. X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac);
  28. if (psalt)
  29. *psalt = p12->mac->salt;
  30. if (piter)
  31. *piter = p12->mac->iter;
  32. } else {
  33. if (pmac)
  34. *pmac = NULL;
  35. if (pmacalg)
  36. *pmacalg = NULL;
  37. if (psalt)
  38. *psalt = NULL;
  39. if (piter)
  40. *piter = NULL;
  41. }
  42. }
  43. #define TK26_MAC_KEY_LEN 32
  44. static int pkcs12_gen_gost_mac_key(const char *pass, int passlen,
  45. const unsigned char *salt, int saltlen,
  46. int iter, int keylen, unsigned char *key,
  47. const EVP_MD *digest)
  48. {
  49. unsigned char out[96];
  50. if (keylen != TK26_MAC_KEY_LEN) {
  51. return 0;
  52. }
  53. if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter,
  54. digest, sizeof(out), out)) {
  55. return 0;
  56. }
  57. memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN);
  58. OPENSSL_cleanse(out, sizeof(out));
  59. return 1;
  60. }
  61. /* Generate a MAC */
  62. static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
  63. unsigned char *mac, unsigned int *maclen,
  64. int (*pkcs12_key_gen)(const char *pass, int passlen,
  65. unsigned char *salt, int slen,
  66. int id, int iter, int n,
  67. unsigned char *out,
  68. const EVP_MD *md_type))
  69. {
  70. int ret = 0;
  71. const EVP_MD *md_type;
  72. HMAC_CTX *hmac = NULL;
  73. unsigned char key[EVP_MAX_MD_SIZE], *salt;
  74. int saltlen, iter;
  75. int md_size = 0;
  76. int md_type_nid;
  77. const X509_ALGOR *macalg;
  78. const ASN1_OBJECT *macoid;
  79. if (pkcs12_key_gen == NULL)
  80. pkcs12_key_gen = PKCS12_key_gen_utf8;
  81. if (!PKCS7_type_is_data(p12->authsafes)) {
  82. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
  83. return 0;
  84. }
  85. salt = p12->mac->salt->data;
  86. saltlen = p12->mac->salt->length;
  87. if (!p12->mac->iter)
  88. iter = 1;
  89. else
  90. iter = ASN1_INTEGER_get(p12->mac->iter);
  91. X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
  92. X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
  93. if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
  94. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
  95. return 0;
  96. }
  97. md_size = EVP_MD_size(md_type);
  98. md_type_nid = EVP_MD_type(md_type);
  99. if (md_size < 0)
  100. return 0;
  101. if ((md_type_nid == NID_id_GostR3411_94
  102. || md_type_nid == NID_id_GostR3411_2012_256
  103. || md_type_nid == NID_id_GostR3411_2012_512)
  104. && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) {
  105. md_size = TK26_MAC_KEY_LEN;
  106. if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
  107. md_size, key, md_type)) {
  108. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
  109. goto err;
  110. }
  111. } else
  112. if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
  113. iter, md_size, key, md_type)) {
  114. PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
  115. goto err;
  116. }
  117. if ((hmac = HMAC_CTX_new()) == NULL
  118. || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
  119. || !HMAC_Update(hmac, p12->authsafes->d.data->data,
  120. p12->authsafes->d.data->length)
  121. || !HMAC_Final(hmac, mac, maclen)) {
  122. goto err;
  123. }
  124. ret = 1;
  125. err:
  126. OPENSSL_cleanse(key, sizeof(key));
  127. HMAC_CTX_free(hmac);
  128. return ret;
  129. }
  130. int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
  131. unsigned char *mac, unsigned int *maclen)
  132. {
  133. return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL);
  134. }
  135. /* Verify the mac */
  136. int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
  137. {
  138. unsigned char mac[EVP_MAX_MD_SIZE];
  139. unsigned int maclen;
  140. const ASN1_OCTET_STRING *macoct;
  141. if (p12->mac == NULL) {
  142. PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
  143. return 0;
  144. }
  145. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
  146. PKCS12_key_gen_utf8)) {
  147. PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
  148. return 0;
  149. }
  150. X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
  151. if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
  152. || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
  153. return 0;
  154. return 1;
  155. }
  156. /* Set a mac */
  157. int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
  158. unsigned char *salt, int saltlen, int iter,
  159. const EVP_MD *md_type)
  160. {
  161. unsigned char mac[EVP_MAX_MD_SIZE];
  162. unsigned int maclen;
  163. ASN1_OCTET_STRING *macoct;
  164. if (!md_type)
  165. md_type = EVP_sha1();
  166. if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
  167. PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_SETUP_ERROR);
  168. return 0;
  169. }
  170. /*
  171. * Note that output mac is forced to UTF-8...
  172. */
  173. if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
  174. PKCS12_key_gen_utf8)) {
  175. PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
  176. return 0;
  177. }
  178. X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
  179. if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
  180. PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. /* Set up a mac structure */
  186. int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
  187. const EVP_MD *md_type)
  188. {
  189. X509_ALGOR *macalg;
  190. PKCS12_MAC_DATA_free(p12->mac);
  191. p12->mac = NULL;
  192. if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
  193. return PKCS12_ERROR;
  194. if (iter > 1) {
  195. if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
  196. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  197. return 0;
  198. }
  199. if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
  200. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  201. return 0;
  202. }
  203. }
  204. if (!saltlen)
  205. saltlen = PKCS12_SALT_LEN;
  206. if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) {
  207. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  208. return 0;
  209. }
  210. p12->mac->salt->length = saltlen;
  211. if (!salt) {
  212. if (RAND_bytes(p12->mac->salt->data, saltlen) <= 0)
  213. return 0;
  214. } else
  215. memcpy(p12->mac->salt->data, salt, saltlen);
  216. X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
  217. if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
  218. V_ASN1_NULL, NULL)) {
  219. PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
  220. return 0;
  221. }
  222. return 1;
  223. }