generic.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2025 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. #include <openssl/core_dispatch.h>
  10. #include <openssl/core_names.h>
  11. #include "crypto/types.h"
  12. #include "internal/skey.h"
  13. #include "prov/provider_ctx.h"
  14. #include "prov/providercommon.h"
  15. #include "prov/implementations.h"
  16. #include "skeymgmt_lcl.h"
  17. void generic_free(void *keydata)
  18. {
  19. PROV_SKEY *generic = keydata;
  20. if (generic == NULL)
  21. return;
  22. OPENSSL_free(generic->data);
  23. OPENSSL_free(generic);
  24. }
  25. void *generic_import(void *provctx, int selection, const OSSL_PARAM params[])
  26. {
  27. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  28. const OSSL_PARAM *raw_bytes;
  29. PROV_SKEY *generic = NULL;
  30. int ok = 0;
  31. if (!ossl_prov_is_running())
  32. return NULL;
  33. if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
  34. return NULL;
  35. raw_bytes = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES);
  36. if (raw_bytes == NULL)
  37. return NULL;
  38. generic = OPENSSL_zalloc(sizeof(PROV_SKEY));
  39. if (generic == NULL)
  40. return NULL;
  41. generic->libctx = libctx;
  42. generic->type = SKEY_TYPE_GENERIC;
  43. if ((generic->data = OPENSSL_memdup(raw_bytes->data, raw_bytes->data_size)) == NULL)
  44. goto end;
  45. generic->length = raw_bytes->data_size;
  46. ok = 1;
  47. end:
  48. if (ok == 0) {
  49. generic_free(generic);
  50. generic = NULL;
  51. }
  52. return generic;
  53. }
  54. int generic_export(void *keydata, int selection,
  55. OSSL_CALLBACK *param_callback, void *cbarg)
  56. {
  57. PROV_SKEY *gen = keydata;
  58. OSSL_PARAM params[2];
  59. if (!ossl_prov_is_running() || gen == NULL)
  60. return 0;
  61. /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */
  62. if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
  63. return 0;
  64. params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
  65. gen->data, gen->length);
  66. params[1] = OSSL_PARAM_construct_end();
  67. return param_callback(params, cbarg);
  68. }
  69. const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = {
  70. { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free },
  71. { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import },
  72. { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export },
  73. OSSL_DISPATCH_END
  74. };