crmf_pbm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*-
  2. * Copyright 2007-2021 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 <openssl/hmac.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/params.h>
  22. #include <openssl/core_names.h>
  23. #include "internal/sizes.h"
  24. #include "crmf_local.h"
  25. /*-
  26. * creates and initializes OSSL_CRMF_PBMPARAMETER (section 4.4)
  27. * |slen| SHOULD be at least 8 (16 is common)
  28. * |owfnid| e.g., NID_sha256
  29. * |itercnt| MUST be >= 100 (e.g., 500) and <= OSSL_CRMF_PBM_MAX_ITERATION_COUNT
  30. * |macnid| e.g., NID_hmac_sha1
  31. * returns pointer to OSSL_CRMF_PBMPARAMETER on success, NULL on error
  32. */
  33. OSSL_CRMF_PBMPARAMETER *OSSL_CRMF_pbmp_new(OSSL_LIB_CTX *libctx, size_t slen,
  34. int owfnid, size_t itercnt,
  35. int macnid)
  36. {
  37. OSSL_CRMF_PBMPARAMETER *pbm = NULL;
  38. unsigned char *salt = NULL;
  39. if ((pbm = OSSL_CRMF_PBMPARAMETER_new()) == NULL)
  40. goto err;
  41. /*
  42. * salt contains a randomly generated value used in computing the key
  43. * of the MAC process. The salt SHOULD be at least 8 octets (64
  44. * bits) long.
  45. */
  46. if ((salt = OPENSSL_malloc(slen)) == NULL)
  47. goto err;
  48. if (RAND_bytes_ex(libctx, salt, slen, 0) <= 0) {
  49. ERR_raise(ERR_LIB_CRMF, CRMF_R_FAILURE_OBTAINING_RANDOM);
  50. goto err;
  51. }
  52. if (!ASN1_OCTET_STRING_set(pbm->salt, salt, (int)slen))
  53. goto err;
  54. /*
  55. * owf identifies the hash algorithm and associated parameters used to
  56. * compute the key used in the MAC process. All implementations MUST
  57. * support SHA-1.
  58. */
  59. if (!X509_ALGOR_set0(pbm->owf, OBJ_nid2obj(owfnid), V_ASN1_UNDEF, NULL)) {
  60. ERR_raise(ERR_LIB_CRMF, CRMF_R_SETTING_OWF_ALGOR_FAILURE);
  61. goto err;
  62. }
  63. /*
  64. * iterationCount identifies the number of times the hash is applied
  65. * during the key computation process. The iterationCount MUST be a
  66. * minimum of 100. Many people suggest using values as high as 1000
  67. * iterations as the minimum value. The trade off here is between
  68. * protection of the password from attacks and the time spent by the
  69. * server processing all of the different iterations in deriving
  70. * passwords. Hashing is generally considered a cheap operation but
  71. * this may not be true with all hash functions in the future.
  72. */
  73. if (itercnt < 100) {
  74. ERR_raise(ERR_LIB_CRMF, CRMF_R_ITERATIONCOUNT_BELOW_100);
  75. goto err;
  76. }
  77. if (itercnt > OSSL_CRMF_PBM_MAX_ITERATION_COUNT) {
  78. ERR_raise(ERR_LIB_CRMF, CRMF_R_BAD_PBM_ITERATIONCOUNT);
  79. goto err;
  80. }
  81. if (!ASN1_INTEGER_set(pbm->iterationCount, itercnt)) {
  82. ERR_raise(ERR_LIB_CRMF, CRMF_R_CRMFERROR);
  83. goto err;
  84. }
  85. /*
  86. * mac identifies the algorithm and associated parameters of the MAC
  87. * function to be used. All implementations MUST support HMAC-SHA1 [HMAC].
  88. * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11].
  89. */
  90. if (!X509_ALGOR_set0(pbm->mac, OBJ_nid2obj(macnid), V_ASN1_UNDEF, NULL)) {
  91. ERR_raise(ERR_LIB_CRMF, CRMF_R_SETTING_MAC_ALGOR_FAILURE);
  92. goto err;
  93. }
  94. OPENSSL_free(salt);
  95. return pbm;
  96. err:
  97. OPENSSL_free(salt);
  98. OSSL_CRMF_PBMPARAMETER_free(pbm);
  99. return NULL;
  100. }
  101. /*-
  102. * calculates the PBM based on the settings of the given OSSL_CRMF_PBMPARAMETER
  103. * |pbmp| identifies the algorithms, salt to use
  104. * |msg| message to apply the PBM for
  105. * |msglen| length of the message
  106. * |sec| key to use
  107. * |seclen| length of the key
  108. * |out| pointer to the computed mac, will be set on success
  109. * |outlen| if not NULL, will set variable to the length of the mac on success
  110. * returns 1 on success, 0 on error
  111. */
  112. int OSSL_CRMF_pbm_new(OSSL_LIB_CTX *libctx, const char *propq,
  113. const OSSL_CRMF_PBMPARAMETER *pbmp,
  114. const unsigned char *msg, size_t msglen,
  115. const unsigned char *sec, size_t seclen,
  116. unsigned char **out, size_t *outlen)
  117. {
  118. int mac_nid, hmac_md_nid = NID_undef;
  119. char mdname[OSSL_MAX_NAME_SIZE];
  120. char hmac_mdname[OSSL_MAX_NAME_SIZE];
  121. EVP_MD *owf = NULL;
  122. EVP_MD_CTX *ctx = NULL;
  123. unsigned char basekey[EVP_MAX_MD_SIZE];
  124. unsigned int bklen = EVP_MAX_MD_SIZE;
  125. int64_t iterations;
  126. unsigned char *mac_res = 0;
  127. int ok = 0;
  128. if (out == NULL || pbmp == NULL || pbmp->mac == NULL
  129. || pbmp->mac->algorithm == NULL || msg == NULL || sec == NULL) {
  130. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  131. goto err;
  132. }
  133. if ((mac_res = OPENSSL_malloc(EVP_MAX_MD_SIZE)) == NULL)
  134. goto err;
  135. /*
  136. * owf identifies the hash algorithm and associated parameters used to
  137. * compute the key used in the MAC process. All implementations MUST
  138. * support SHA-1.
  139. */
  140. OBJ_obj2txt(mdname, sizeof(mdname), pbmp->owf->algorithm, 0);
  141. if ((owf = EVP_MD_fetch(libctx, mdname, propq)) == NULL) {
  142. ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_ALGORITHM);
  143. goto err;
  144. }
  145. if ((ctx = EVP_MD_CTX_new()) == NULL)
  146. goto err;
  147. /* compute the basekey of the salted secret */
  148. if (!EVP_DigestInit_ex(ctx, owf, NULL))
  149. goto err;
  150. /* first the secret */
  151. if (!EVP_DigestUpdate(ctx, sec, seclen))
  152. goto err;
  153. /* then the salt */
  154. if (!EVP_DigestUpdate(ctx, pbmp->salt->data, pbmp->salt->length))
  155. goto err;
  156. if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
  157. goto err;
  158. if (!ASN1_INTEGER_get_int64(&iterations, pbmp->iterationCount)
  159. || iterations < 100 /* min from RFC */
  160. || iterations > OSSL_CRMF_PBM_MAX_ITERATION_COUNT) {
  161. ERR_raise(ERR_LIB_CRMF, CRMF_R_BAD_PBM_ITERATIONCOUNT);
  162. goto err;
  163. }
  164. /* the first iteration was already done above */
  165. while (--iterations > 0) {
  166. if (!EVP_DigestInit_ex(ctx, owf, NULL))
  167. goto err;
  168. if (!EVP_DigestUpdate(ctx, basekey, bklen))
  169. goto err;
  170. if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
  171. goto err;
  172. }
  173. /*
  174. * mac identifies the algorithm and associated parameters of the MAC
  175. * function to be used. All implementations MUST support HMAC-SHA1 [HMAC].
  176. * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11].
  177. */
  178. mac_nid = OBJ_obj2nid(pbmp->mac->algorithm);
  179. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, mac_nid, NULL, &hmac_md_nid, NULL)
  180. || OBJ_obj2txt(hmac_mdname, sizeof(hmac_mdname),
  181. OBJ_nid2obj(hmac_md_nid), 0) <= 0) {
  182. ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_ALGORITHM);
  183. goto err;
  184. }
  185. if (EVP_Q_mac(libctx, "HMAC", propq, hmac_mdname, NULL, basekey, bklen,
  186. msg, msglen, mac_res, EVP_MAX_MD_SIZE, outlen) == NULL)
  187. goto err;
  188. ok = 1;
  189. err:
  190. OPENSSL_cleanse(basekey, bklen);
  191. EVP_MD_free(owf);
  192. EVP_MD_CTX_free(ctx);
  193. if (ok == 1) {
  194. *out = mac_res;
  195. return 1;
  196. }
  197. OPENSSL_free(mac_res);
  198. if (pbmp != NULL && pbmp->mac != NULL) {
  199. char buf[128];
  200. if (OBJ_obj2txt(buf, sizeof(buf), pbmp->mac->algorithm, 0))
  201. ERR_add_error_data(1, buf);
  202. }
  203. return 0;
  204. }