2
0

tb_pkmeth.c 3.0 KB

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