crmf_pbm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /* could be combined with other MAC calculations in the library */
  113. int OSSL_CRMF_pbm_new(OSSL_LIB_CTX *libctx, const char *propq,
  114. const OSSL_CRMF_PBMPARAMETER *pbmp,
  115. const unsigned char *msg, size_t msglen,
  116. const unsigned char *sec, size_t seclen,
  117. unsigned char **out, size_t *outlen)
  118. {
  119. int mac_nid, hmac_md_nid = NID_undef;
  120. char mdname[OSSL_MAX_NAME_SIZE];
  121. char hmac_mdname[OSSL_MAX_NAME_SIZE];
  122. EVP_MD *owf = NULL;
  123. EVP_MD_CTX *ctx = NULL;
  124. unsigned char basekey[EVP_MAX_MD_SIZE];
  125. unsigned int bklen = EVP_MAX_MD_SIZE;
  126. int64_t iterations;
  127. unsigned char *mac_res = 0;
  128. int ok = 0;
  129. if (out == NULL || pbmp == NULL || pbmp->mac == NULL
  130. || pbmp->mac->algorithm == NULL || msg == NULL || sec == NULL) {
  131. ERR_raise(ERR_LIB_CRMF, CRMF_R_NULL_ARGUMENT);
  132. goto err;
  133. }
  134. if ((mac_res = OPENSSL_malloc(EVP_MAX_MD_SIZE)) == NULL)
  135. goto err;
  136. /*
  137. * owf identifies the hash algorithm and associated parameters used to
  138. * compute the key used in the MAC process. All implementations MUST
  139. * support SHA-1.
  140. */
  141. OBJ_obj2txt(mdname, sizeof(mdname), pbmp->owf->algorithm, 0);
  142. if ((owf = EVP_MD_fetch(libctx, mdname, propq)) == NULL) {
  143. ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_ALGORITHM);
  144. goto err;
  145. }
  146. if ((ctx = EVP_MD_CTX_new()) == NULL)
  147. goto err;
  148. /* compute the basekey of the salted secret */
  149. if (!EVP_DigestInit_ex(ctx, owf, NULL))
  150. goto err;
  151. /* first the secret */
  152. if (!EVP_DigestUpdate(ctx, sec, seclen))
  153. goto err;
  154. /* then the salt */
  155. if (!EVP_DigestUpdate(ctx, pbmp->salt->data, pbmp->salt->length))
  156. goto err;
  157. if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
  158. goto err;
  159. if (!ASN1_INTEGER_get_int64(&iterations, pbmp->iterationCount)
  160. || iterations < 100 /* min from RFC */
  161. || iterations > OSSL_CRMF_PBM_MAX_ITERATION_COUNT) {
  162. ERR_raise(ERR_LIB_CRMF, CRMF_R_BAD_PBM_ITERATIONCOUNT);
  163. goto err;
  164. }
  165. /* the first iteration was already done above */
  166. while (--iterations > 0) {
  167. if (!EVP_DigestInit_ex(ctx, owf, NULL))
  168. goto err;
  169. if (!EVP_DigestUpdate(ctx, basekey, bklen))
  170. goto err;
  171. if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
  172. goto err;
  173. }
  174. /*
  175. * mac identifies the algorithm and associated parameters of the MAC
  176. * function to be used. All implementations MUST support HMAC-SHA1 [HMAC].
  177. * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11].
  178. */
  179. mac_nid = OBJ_obj2nid(pbmp->mac->algorithm);
  180. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, mac_nid, NULL, &hmac_md_nid, NULL)
  181. || OBJ_obj2txt(hmac_mdname, sizeof(hmac_mdname),
  182. OBJ_nid2obj(hmac_md_nid), 0) <= 0) {
  183. ERR_raise(ERR_LIB_CRMF, CRMF_R_UNSUPPORTED_ALGORITHM);
  184. goto err;
  185. }
  186. /* could be generalized to allow non-HMAC: */
  187. if (EVP_Q_mac(libctx, "HMAC", propq, hmac_mdname, NULL, basekey, bklen,
  188. msg, msglen, mac_res, EVP_MAX_MD_SIZE, outlen) == NULL)
  189. goto err;
  190. ok = 1;
  191. err:
  192. OPENSSL_cleanse(basekey, bklen);
  193. EVP_MD_free(owf);
  194. EVP_MD_CTX_free(ctx);
  195. if (ok == 1) {
  196. *out = mac_res;
  197. return 1;
  198. }
  199. OPENSSL_free(mac_res);
  200. if (pbmp != NULL && pbmp->mac != NULL) {
  201. char buf[128];
  202. if (OBJ_obj2txt(buf, sizeof(buf), pbmp->mac->algorithm, 0))
  203. ERR_add_error_data(1, buf);
  204. }
  205. return 0;
  206. }