tb_pkmeth.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2006-2020 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 engine_table_select(&pkey_meth_table, nid);
  60. }
  61. /* Obtains a pkey_meth implementation from an ENGINE functional reference */
  62. const EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid)
  63. {
  64. EVP_PKEY_METHOD *ret;
  65. ENGINE_PKEY_METHS_PTR fn = ENGINE_get_pkey_meths(e);
  66. if (!fn || !fn(e, &ret, NULL, nid)) {
  67. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
  68. return NULL;
  69. }
  70. return ret;
  71. }
  72. /* Gets the pkey_meth callback from an ENGINE structure */
  73. ENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e)
  74. {
  75. return e->pkey_meths;
  76. }
  77. /* Sets the pkey_meth callback in an ENGINE structure */
  78. int ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f)
  79. {
  80. e->pkey_meths = f;
  81. return 1;
  82. }
  83. /*
  84. * Internal function to free up EVP_PKEY_METHOD structures before an ENGINE
  85. * is destroyed
  86. */
  87. void engine_pkey_meths_free(ENGINE *e)
  88. {
  89. int i;
  90. EVP_PKEY_METHOD *pkm;
  91. if (e->pkey_meths) {
  92. const int *pknids;
  93. int npknids;
  94. npknids = e->pkey_meths(e, NULL, &pknids, 0);
  95. for (i = 0; i < npknids; i++) {
  96. if (e->pkey_meths(e, &pkm, NULL, pknids[i])) {
  97. EVP_PKEY_meth_free(pkm);
  98. }
  99. }
  100. }
  101. }