2
0

crmf_pbm.c 7.7 KB

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