2
0

endecoder_common.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2020-2022 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.h>
  10. #include <openssl/buffer.h>
  11. #include "internal/asn1.h"
  12. #include "prov/bio.h"
  13. #include "endecoder_local.h"
  14. OSSL_FUNC_keymgmt_new_fn *
  15. ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
  16. {
  17. /* Pilfer the keymgmt dispatch table */
  18. for (; fns->function_id != 0; fns++)
  19. if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
  20. return OSSL_FUNC_keymgmt_new(fns);
  21. return NULL;
  22. }
  23. OSSL_FUNC_keymgmt_free_fn *
  24. ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
  25. {
  26. /* Pilfer the keymgmt dispatch table */
  27. for (; fns->function_id != 0; fns++)
  28. if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
  29. return OSSL_FUNC_keymgmt_free(fns);
  30. return NULL;
  31. }
  32. OSSL_FUNC_keymgmt_import_fn *
  33. ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
  34. {
  35. /* Pilfer the keymgmt dispatch table */
  36. for (; fns->function_id != 0; fns++)
  37. if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
  38. return OSSL_FUNC_keymgmt_import(fns);
  39. return NULL;
  40. }
  41. OSSL_FUNC_keymgmt_export_fn *
  42. ossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
  43. {
  44. /* Pilfer the keymgmt dispatch table */
  45. for (; fns->function_id != 0; fns++)
  46. if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT)
  47. return OSSL_FUNC_keymgmt_export(fns);
  48. return NULL;
  49. }
  50. void *ossl_prov_import_key(const OSSL_DISPATCH *fns, void *provctx,
  51. int selection, const OSSL_PARAM params[])
  52. {
  53. OSSL_FUNC_keymgmt_new_fn *kmgmt_new = ossl_prov_get_keymgmt_new(fns);
  54. OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
  55. OSSL_FUNC_keymgmt_import_fn *kmgmt_import =
  56. ossl_prov_get_keymgmt_import(fns);
  57. void *key = NULL;
  58. if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) {
  59. if ((key = kmgmt_new(provctx)) == NULL
  60. || !kmgmt_import(key, selection, params)) {
  61. kmgmt_free(key);
  62. key = NULL;
  63. }
  64. }
  65. return key;
  66. }
  67. void ossl_prov_free_key(const OSSL_DISPATCH *fns, void *key)
  68. {
  69. OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
  70. if (kmgmt_free != NULL)
  71. kmgmt_free(key);
  72. }
  73. int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data,
  74. long *len)
  75. {
  76. BUF_MEM *mem = NULL;
  77. BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
  78. int ok;
  79. if (in == NULL)
  80. return 0;
  81. ok = (asn1_d2i_read_bio(in, &mem) >= 0);
  82. if (ok) {
  83. *data = (unsigned char *)mem->data;
  84. *len = (long)mem->length;
  85. OPENSSL_free(mem);
  86. }
  87. BIO_free(in);
  88. return ok;
  89. }