crmf_pbm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*-
  2. * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. *
  11. * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb.
  12. */
  13. #include <string.h>
  14. #include <openssl/rand.h>
  15. #include <openssl/evp.h>
  16. #include "crmf_local.h"
  17. /* explicit #includes not strictly needed since implied by the above: */
  18. #include <openssl/asn1t.h>
  19. #include <openssl/crmf.h>
  20. #include <openssl/err.h>
  21. #include <openssl/evp.h>
  22. #include <openssl/params.h>
  23. #include <openssl/core_names.h>
  24. /*-
  25. * creates and initializes OSSL_CRMF_PBMPARAMETER (section 4.4)
  26. * |slen| SHOULD be > 8 (16 is common)
  27. * |owfnid| e.g., NID_sha256
  28. * |itercnt| MUST be > 100 (500 is common)
  29. * |macnid| e.g., NID_hmac_sha1
  30. * returns pointer to OSSL_CRMF_PBMPARAMETER on success, NULL on error
  31. */
  32. OSSL_CRMF_PBMPARAMETER *OSSL_CRMF_pbmp_new(size_t slen, int owfnid,
  33. int itercnt, int macnid)
  34. {
  35. OSSL_CRMF_PBMPARAMETER *pbm = NULL;
  36. unsigned char *salt = NULL;
  37. if ((pbm = OSSL_CRMF_PBMPARAMETER_new()) == NULL)
  38. goto err;
  39. /*
  40. * salt contains a randomly generated value used in computing the key
  41. * of the MAC process. The salt SHOULD be at least 8 octets (64
  42. * bits) long.
  43. */
  44. if ((salt = OPENSSL_malloc(slen)) == NULL)
  45. goto err;
  46. if (RAND_bytes(salt, (int)slen) <= 0) {
  47. CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_FAILURE_OBTAINING_RANDOM);
  48. goto err;
  49. }
  50. if (!ASN1_OCTET_STRING_set(pbm->salt, salt, (int)slen))
  51. goto err;
  52. /*
  53. * owf identifies the hash algorithm and associated parameters used to
  54. * compute the key used in the MAC process. All implementations MUST
  55. * support SHA-1.
  56. */
  57. if (!X509_ALGOR_set0(pbm->owf, OBJ_nid2obj(owfnid), V_ASN1_UNDEF, NULL)) {
  58. CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_SETTING_OWF_ALGOR_FAILURE);
  59. goto err;
  60. }
  61. /*
  62. * iterationCount identifies the number of times the hash is applied
  63. * during the key computation process. The iterationCount MUST be a
  64. * minimum of 100. Many people suggest using values as high as 1000
  65. * iterations as the minimum value. The trade off here is between
  66. * protection of the password from attacks and the time spent by the
  67. * server processing all of the different iterations in deriving
  68. * passwords. Hashing is generally considered a cheap operation but
  69. * this may not be true with all hash functions in the future.
  70. */
  71. if (itercnt < 100) {
  72. CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_ITERATIONCOUNT_BELOW_100);
  73. goto err;
  74. }
  75. if (!ASN1_INTEGER_set(pbm->iterationCount, itercnt)) {
  76. CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_CRMFERROR);
  77. goto err;
  78. }
  79. /*
  80. * mac identifies the algorithm and associated parameters of the MAC
  81. * function to be used. All implementations MUST support HMAC-SHA1 [HMAC].
  82. * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11].
  83. */
  84. if (!X509_ALGOR_set0(pbm->mac, OBJ_nid2obj(macnid), V_ASN1_UNDEF, NULL)) {
  85. CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_SETTING_MAC_ALGOR_FAILURE);
  86. goto err;
  87. }
  88. OPENSSL_free(salt);
  89. return pbm;
  90. err:
  91. OPENSSL_free(salt);
  92. OSSL_CRMF_PBMPARAMETER_free(pbm);
  93. return NULL;
  94. }
  95. /*-
  96. * calculates the PBM based on the settings of the given OSSL_CRMF_PBMPARAMETER
  97. * |pbmp| identifies the algorithms, salt to use
  98. * |msg| message to apply the PBM for
  99. * |msglen| length of the message
  100. * |sec| key to use
  101. * |seclen| length of the key
  102. * |mac| pointer to the computed mac, will be set on success
  103. * |maclen| if not NULL, will set variable to the length of the mac on success
  104. * returns 1 on success, 0 on error
  105. */
  106. int OSSL_CRMF_pbm_new(const OSSL_CRMF_PBMPARAMETER *pbmp,
  107. const unsigned char *msg, size_t msglen,
  108. const unsigned char *sec, size_t seclen,
  109. unsigned char **out, size_t *outlen)
  110. {
  111. int mac_nid, hmac_md_nid = NID_undef;
  112. const char *mdname = NULL;
  113. const EVP_MD *m = NULL;
  114. EVP_MD_CTX *ctx = NULL;
  115. unsigned char basekey[EVP_MAX_MD_SIZE];
  116. unsigned int bklen = EVP_MAX_MD_SIZE;
  117. int64_t iterations;
  118. unsigned char *mac_res = 0;
  119. int ok = 0;
  120. EVP_MAC *mac = NULL;
  121. EVP_MAC_CTX *mctx = NULL;
  122. OSSL_PARAM macparams[3] = {OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END};
  123. if (out == NULL || pbmp == NULL || pbmp->mac == NULL
  124. || pbmp->mac->algorithm == NULL || msg == NULL || sec == NULL) {
  125. CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_NULL_ARGUMENT);
  126. goto err;
  127. }
  128. if ((mac_res = OPENSSL_malloc(EVP_MAX_MD_SIZE)) == NULL)
  129. goto err;
  130. /*
  131. * owf identifies the hash algorithm and associated parameters used to
  132. * compute the key used in the MAC process. All implementations MUST
  133. * support SHA-1.
  134. */
  135. if ((m = EVP_get_digestbyobj(pbmp->owf->algorithm)) == NULL) {
  136. CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_UNSUPPORTED_ALGORITHM);
  137. goto err;
  138. }
  139. if ((ctx = EVP_MD_CTX_new()) == NULL)
  140. goto err;
  141. /* compute the basekey of the salted secret */
  142. if (!EVP_DigestInit_ex(ctx, m, NULL))
  143. goto err;
  144. /* first the secret */
  145. if (!EVP_DigestUpdate(ctx, sec, seclen))
  146. goto err;
  147. /* then the salt */
  148. if (!EVP_DigestUpdate(ctx, pbmp->salt->data, pbmp->salt->length))
  149. goto err;
  150. if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
  151. goto err;
  152. if (!ASN1_INTEGER_get_int64(&iterations, pbmp->iterationCount)
  153. || iterations < 100 /* min from RFC */
  154. || iterations > OSSL_CRMF_PBM_MAX_ITERATION_COUNT) {
  155. CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_BAD_PBM_ITERATIONCOUNT);
  156. goto err;
  157. }
  158. /* the first iteration was already done above */
  159. while (--iterations > 0) {
  160. if (!EVP_DigestInit_ex(ctx, m, NULL))
  161. goto err;
  162. if (!EVP_DigestUpdate(ctx, basekey, bklen))
  163. goto err;
  164. if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
  165. goto err;
  166. }
  167. /*
  168. * mac identifies the algorithm and associated parameters of the MAC
  169. * function to be used. All implementations MUST support HMAC-SHA1 [HMAC].
  170. * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11].
  171. */
  172. mac_nid = OBJ_obj2nid(pbmp->mac->algorithm);
  173. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, mac_nid, NULL, &hmac_md_nid, NULL)
  174. || (mdname = OBJ_nid2sn(hmac_md_nid)) == NULL) {
  175. CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_UNSUPPORTED_ALGORITHM);
  176. goto err;
  177. }
  178. macparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  179. (char *)mdname, 0);
  180. macparams[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  181. basekey, bklen);
  182. if ((mac = EVP_MAC_fetch(NULL, "HMAC", NULL)) == NULL
  183. || (mctx = EVP_MAC_new_ctx(mac)) == NULL
  184. || !EVP_MAC_set_ctx_params(mctx, macparams)
  185. || !EVP_MAC_init(mctx)
  186. || !EVP_MAC_update(mctx, msg, msglen)
  187. || !EVP_MAC_final(mctx, mac_res, outlen, EVP_MAX_MD_SIZE))
  188. goto err;
  189. ok = 1;
  190. err:
  191. /* cleanup */
  192. OPENSSL_cleanse(basekey, bklen);
  193. EVP_MAC_free_ctx(mctx);
  194. EVP_MAC_free(mac);
  195. EVP_MD_CTX_free(ctx);
  196. if (ok == 1) {
  197. *out = mac_res;
  198. return 1;
  199. }
  200. OPENSSL_free(mac_res);
  201. if (pbmp != NULL && pbmp->mac != NULL) {
  202. char buf[128];
  203. if (OBJ_obj2txt(buf, sizeof(buf), pbmp->mac->algorithm, 0))
  204. ERR_add_error_data(1, buf);
  205. }
  206. return 0;
  207. }