tb_pkmeth.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2006-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. /* We need to use some deprecated APIs */
  10. #include "internal/deprecated.h"
  11. #include "eng_local.h"
  12. #include <openssl/evp.h>
  13. static ENGINE_TABLE *pkey_meth_table = NULL;
  14. void ENGINE_unregister_pkey_meths(ENGINE *e)
  15. {
  16. engine_table_unregister(&pkey_meth_table, e);
  17. }
  18. static void engine_unregister_all_pkey_meths(void)
  19. {
  20. engine_table_cleanup(&pkey_meth_table);
  21. }
  22. int ENGINE_register_pkey_meths(ENGINE *e)
  23. {
  24. if (e->pkey_meths) {
  25. const int *nids;
  26. int num_nids = e->pkey_meths(e, NULL, &nids, 0);
  27. if (num_nids > 0)
  28. return engine_table_register(&pkey_meth_table,
  29. engine_unregister_all_pkey_meths, e,
  30. nids, num_nids, 0);
  31. }
  32. return 1;
  33. }
  34. void ENGINE_register_all_pkey_meths(void)
  35. {
  36. ENGINE *e;
  37. for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
  38. ENGINE_register_pkey_meths(e);
  39. }
  40. int ENGINE_set_default_pkey_meths(ENGINE *e)
  41. {
  42. if (e->pkey_meths) {
  43. const int *nids;
  44. int num_nids = e->pkey_meths(e, NULL, &nids, 0);
  45. if (num_nids > 0)
  46. return engine_table_register(&pkey_meth_table,
  47. engine_unregister_all_pkey_meths, e,
  48. nids, num_nids, 1);
  49. }
  50. return 1;
  51. }
  52. /*
  53. * Exposed API function to get a functional reference from the implementation
  54. * table (ie. try to get a functional reference from the tabled structural
  55. * references) for a given pkey_meth 'nid'
  56. */
  57. ENGINE *ENGINE_get_pkey_meth_engine(int nid)
  58. {
  59. return ossl_engine_table_select(&pkey_meth_table, nid,
  60. OPENSSL_FILE, OPENSSL_LINE);
  61. }
  62. /* Obtains a pkey_meth implementation from an ENGINE functional reference */
  63. const EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)
  64. {
  65. EVP_PKEY_METHOD *ret;
  66. ENGINE_PKEY_METHS_PTR fn = ENGINE_get_pkey_meths(e);
  67. if (!fn || !fn(e, &ret, NULL, nid)) {
  68. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
  69. return NULL;
  70. }
  71. return ret;
  72. }
  73. /* Gets the pkey_meth callback from an ENGINE structure */
  74. ENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e)
  75. {
  76. return e->pkey_meths;
  77. }
  78. /* Sets the pkey_meth callback in an ENGINE structure */
  79. int ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)
  80. {
  81. e->pkey_meths = f;
  82. return 1;
  83. }
  84. /*
  85. * Internal function to free up EVP_PKEY_METHOD structures before an ENGINE
  86. * is destroyed
  87. */
  88. void engine_pkey_meths_free(ENGINE *e)
  89. {
  90. int i;
  91. EVP_PKEY_METHOD *pkm;
  92. if (e->pkey_meths) {
  93. const int *pknids;
  94. int npknids;
  95. npknids = e->pkey_meths(e, NULL, &pknids, 0);
  96. for (i = 0; i < npknids; i++) {
  97. if (e->pkey_meths(e, &pkm, NULL, pknids[i])) {
  98. EVP_PKEY_meth_free(pkm);
  99. }
  100. }
  101. }
  102. }