eng_init.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2001-2023 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 engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include "internal/e_os.h"
  12. #include "eng_local.h"
  13. /*
  14. * Initialise an engine type for use (or up its functional reference count if
  15. * it's already in use). This version is only used internally.
  16. */
  17. int engine_unlocked_init(ENGINE *e)
  18. {
  19. int to_return = 1;
  20. if ((e->funct_ref == 0) && e->init)
  21. /*
  22. * This is the first functional reference and the engine requires
  23. * initialisation so we do it now.
  24. */
  25. to_return = e->init(e);
  26. if (to_return) {
  27. int ref;
  28. /*
  29. * OK, we return a functional reference which is also a structural
  30. * reference.
  31. */
  32. if (!CRYPTO_UP_REF(&e->struct_ref, &ref)) {
  33. e->finish(e);
  34. return 0;
  35. }
  36. e->funct_ref++;
  37. ENGINE_REF_PRINT(e, 0, 1);
  38. ENGINE_REF_PRINT(e, 1, 1);
  39. }
  40. return to_return;
  41. }
  42. /*
  43. * Free a functional reference to an engine type. This version is only used
  44. * internally.
  45. */
  46. int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)
  47. {
  48. int to_return = 1;
  49. /*
  50. * Reduce the functional reference count here so if it's the terminating
  51. * case, we can release the lock safely and call the finish() handler
  52. * without risk of a race. We get a race if we leave the count until
  53. * after and something else is calling "finish" at the same time -
  54. * there's a chance that both threads will together take the count from 2
  55. * to 0 without either calling finish().
  56. */
  57. e->funct_ref--;
  58. ENGINE_REF_PRINT(e, 1, -1);
  59. if ((e->funct_ref == 0) && e->finish) {
  60. if (unlock_for_handlers)
  61. CRYPTO_THREAD_unlock(global_engine_lock);
  62. to_return = e->finish(e);
  63. if (unlock_for_handlers)
  64. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  65. return 0;
  66. if (!to_return)
  67. return 0;
  68. }
  69. REF_ASSERT_ISNT(e->funct_ref < 0);
  70. /* Release the structural reference too */
  71. if (!engine_free_util(e, 0)) {
  72. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
  73. return 0;
  74. }
  75. return to_return;
  76. }
  77. /* The API (locked) version of "init" */
  78. int ENGINE_init(ENGINE *e)
  79. {
  80. int ret;
  81. if (e == NULL) {
  82. ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
  83. return 0;
  84. }
  85. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
  86. /* Maybe this should be raised in do_engine_lock_init() */
  87. ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB);
  88. return 0;
  89. }
  90. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  91. return 0;
  92. ret = engine_unlocked_init(e);
  93. CRYPTO_THREAD_unlock(global_engine_lock);
  94. return ret;
  95. }
  96. /* The API (locked) version of "finish" */
  97. int ENGINE_finish(ENGINE *e)
  98. {
  99. int to_return = 1;
  100. if (e == NULL)
  101. return 1;
  102. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  103. return 0;
  104. to_return = engine_unlocked_finish(e, 1);
  105. CRYPTO_THREAD_unlock(global_engine_lock);
  106. if (!to_return) {
  107. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FINISH_FAILED);
  108. return 0;
  109. }
  110. return to_return;
  111. }