kdf_legacy_kmgmt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  10. * This implemments a dummy key manager for legacy KDFs that still support the
  11. * old way of performing a KDF via EVP_PKEY_derive(). New KDFs should not be
  12. * implemented this way. In reality there is no key data for such KDFs, so this
  13. * key manager does very little.
  14. */
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/err.h>
  18. #include "prov/implementations.h"
  19. #include "prov/providercommon.h"
  20. #include "prov/provider_ctx.h"
  21. #include "prov/kdfexchange.h"
  22. static OSSL_FUNC_keymgmt_new_fn kdf_newdata;
  23. static OSSL_FUNC_keymgmt_free_fn kdf_freedata;
  24. static OSSL_FUNC_keymgmt_has_fn kdf_has;
  25. KDF_DATA *ossl_kdf_data_new(void *provctx)
  26. {
  27. KDF_DATA *kdfdata;
  28. if (!ossl_prov_is_running())
  29. return NULL;
  30. kdfdata = OPENSSL_zalloc(sizeof(*kdfdata));
  31. if (kdfdata == NULL)
  32. return NULL;
  33. kdfdata->lock = CRYPTO_THREAD_lock_new();
  34. if (kdfdata->lock == NULL) {
  35. OPENSSL_free(kdfdata);
  36. return NULL;
  37. }
  38. kdfdata->libctx = PROV_LIBCTX_OF(provctx);
  39. kdfdata->refcnt = 1;
  40. return kdfdata;
  41. }
  42. void ossl_kdf_data_free(KDF_DATA *kdfdata)
  43. {
  44. int ref = 0;
  45. if (kdfdata == NULL)
  46. return;
  47. CRYPTO_DOWN_REF(&kdfdata->refcnt, &ref, kdfdata->lock);
  48. if (ref > 0)
  49. return;
  50. CRYPTO_THREAD_lock_free(kdfdata->lock);
  51. OPENSSL_free(kdfdata);
  52. }
  53. int ossl_kdf_data_up_ref(KDF_DATA *kdfdata)
  54. {
  55. int ref = 0;
  56. /* This is effectively doing a new operation on the KDF_DATA and should be
  57. * adequately guarded again modules' error states. However, both current
  58. * calls here are guarded propery in exchange/kdf_exch.c. Thus, it
  59. * could be removed here. The concern is that something in the future
  60. * might call this function without adequate guards. It's a cheap call,
  61. * it seems best to leave it even though it is currently redundant.
  62. */
  63. if (!ossl_prov_is_running())
  64. return 0;
  65. CRYPTO_UP_REF(&kdfdata->refcnt, &ref, kdfdata->lock);
  66. return 1;
  67. }
  68. static void *kdf_newdata(void *provctx)
  69. {
  70. return ossl_kdf_data_new(provctx);
  71. }
  72. static void kdf_freedata(void *kdfdata)
  73. {
  74. ossl_kdf_data_free(kdfdata);
  75. }
  76. static int kdf_has(const void *keydata, int selection)
  77. {
  78. return 1; /* nothing is missing */
  79. }
  80. const OSSL_DISPATCH ossl_kdf_keymgmt_functions[] = {
  81. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))kdf_newdata },
  82. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))kdf_freedata },
  83. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))kdf_has },
  84. { 0, NULL }
  85. };