engine.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 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. /*
  10. * Here is a set of wrappers for the ENGINE API, which are no-ops when the
  11. * ENGINE API is disabled / removed.
  12. * We need to suppress deprecation warnings to make this work.
  13. */
  14. #define OPENSSL_SUPPRESS_DEPRECATED
  15. #include <string.h> /* strcmp */
  16. #include <openssl/types.h> /* Ensure we have the ENGINE type, regardless */
  17. #ifndef OPENSSL_NO_ENGINE
  18. # include <openssl/engine.h>
  19. #endif
  20. #include "apps.h"
  21. #ifndef OPENSSL_NO_ENGINE
  22. /* Try to load an engine in a shareable library */
  23. static ENGINE *try_load_engine(const char *engine)
  24. {
  25. ENGINE *e = NULL;
  26. if ((e = ENGINE_by_id("dynamic")) != NULL) {
  27. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
  28. || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) {
  29. ENGINE_free(e);
  30. e = NULL;
  31. }
  32. }
  33. return e;
  34. }
  35. #endif
  36. ENGINE *setup_engine_methods(const char *id, unsigned int methods, int debug)
  37. {
  38. ENGINE *e = NULL;
  39. #ifndef OPENSSL_NO_ENGINE
  40. if (id != NULL) {
  41. if (strcmp(id, "auto") == 0) {
  42. BIO_printf(bio_err, "Enabling auto ENGINE support\n");
  43. ENGINE_register_all_complete();
  44. return NULL;
  45. }
  46. if ((e = ENGINE_by_id(id)) == NULL
  47. && (e = try_load_engine(id)) == NULL) {
  48. BIO_printf(bio_err, "Invalid engine \"%s\"\n", id);
  49. ERR_print_errors(bio_err);
  50. return NULL;
  51. }
  52. if (debug)
  53. (void)ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, 0, bio_err, 0);
  54. if (!ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0,
  55. (void *)get_ui_method(), 0, 1)
  56. || !ENGINE_set_default(e, methods)) {
  57. BIO_printf(bio_err, "Cannot use engine \"%s\"\n", ENGINE_get_id(e));
  58. ERR_print_errors(bio_err);
  59. ENGINE_free(e);
  60. return NULL;
  61. }
  62. BIO_printf(bio_err, "Engine \"%s\" set.\n", ENGINE_get_id(e));
  63. }
  64. #endif
  65. return e;
  66. }
  67. void release_engine(ENGINE *e)
  68. {
  69. #ifndef OPENSSL_NO_ENGINE
  70. /* Free our "structural" reference. */
  71. ENGINE_free(e);
  72. #endif
  73. }
  74. int init_engine(ENGINE *e)
  75. {
  76. int rv = 1;
  77. #ifndef OPENSSL_NO_ENGINE
  78. rv = ENGINE_init(e);
  79. #endif
  80. return rv;
  81. }
  82. int finish_engine(ENGINE *e)
  83. {
  84. int rv = 1;
  85. #ifndef OPENSSL_NO_ENGINE
  86. rv = ENGINE_finish(e);
  87. #endif
  88. return rv;
  89. }
  90. EVP_PKEY *load_engine_private_key(ENGINE *e, const char *keyid,
  91. const char *pass, const char *desc)
  92. {
  93. EVP_PKEY *rv = NULL;
  94. #ifndef OPENSSL_NO_ENGINE
  95. if (init_engine(e)) {
  96. PW_CB_DATA cb_data;
  97. cb_data.password = pass;
  98. cb_data.prompt_info = keyid;
  99. rv = ENGINE_load_private_key(e, keyid,
  100. (UI_METHOD *)get_ui_method(), &cb_data);
  101. finish_engine(e);
  102. }
  103. #else
  104. BIO_printf(bio_err, "Engines not supported for loading %s\n", desc);
  105. #endif
  106. return rv;
  107. }
  108. EVP_PKEY *load_engine_public_key(ENGINE *e, const char *keyid,
  109. const char *pass, const char *desc)
  110. {
  111. EVP_PKEY *rv = NULL;
  112. #ifndef OPENSSL_NO_ENGINE
  113. if (init_engine(e)) {
  114. PW_CB_DATA cb_data;
  115. cb_data.password = pass;
  116. cb_data.prompt_info = keyid;
  117. rv = ENGINE_load_public_key(e, keyid,
  118. (UI_METHOD *)get_ui_method(), &cb_data);
  119. finish_engine(e);
  120. }
  121. #else
  122. BIO_printf(bio_err, "Engines not supported for loading %s\n", desc);
  123. #endif
  124. return rv;
  125. }