eng_init.c 3.1 KB

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