eng_init.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /* 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_debug(e, 0, 1);
  34. engine_ref_debug(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_debug(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. CRYPTO_THREAD_write_lock(global_engine_lock);
  61. if (!to_return)
  62. return 0;
  63. }
  64. REF_ASSERT_ISNT(e->funct_ref < 0);
  65. /* Release the structural reference too */
  66. if (!engine_free_util(e, 0)) {
  67. ENGINEerr(ENGINE_F_ENGINE_UNLOCKED_FINISH, ENGINE_R_FINISH_FAILED);
  68. return 0;
  69. }
  70. return to_return;
  71. }
  72. /* The API (locked) version of "init" */
  73. int ENGINE_init(ENGINE *e)
  74. {
  75. int ret;
  76. if (e == NULL) {
  77. ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);
  78. return 0;
  79. }
  80. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
  81. ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);
  82. return 0;
  83. }
  84. CRYPTO_THREAD_write_lock(global_engine_lock);
  85. ret = engine_unlocked_init(e);
  86. CRYPTO_THREAD_unlock(global_engine_lock);
  87. return ret;
  88. }
  89. /* The API (locked) version of "finish" */
  90. int ENGINE_finish(ENGINE *e)
  91. {
  92. int to_return = 1;
  93. if (e == NULL)
  94. return 1;
  95. CRYPTO_THREAD_write_lock(global_engine_lock);
  96. to_return = engine_unlocked_finish(e, 1);
  97. CRYPTO_THREAD_unlock(global_engine_lock);
  98. if (!to_return) {
  99. ENGINEerr(ENGINE_F_ENGINE_FINISH, ENGINE_R_FINISH_FAILED);
  100. return 0;
  101. }
  102. return to_return;
  103. }