eng_init.c 3.3 KB

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