Init.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <map>
  18. #include <folly/ssl/OpenSSLLockTypes.h>
  19. namespace folly {
  20. namespace ssl {
  21. /**
  22. * Initializes openssl. This should be invoked once, during the start of an
  23. * application. Subsequent calls to this function are no-ops.
  24. *
  25. * For OpenSSL < 1.1.0, any lock types should be set with setLockTypes prior to
  26. * the call to folly::ssl::init()
  27. */
  28. void init();
  29. /**
  30. * Cleans up openssl. This should be invoked at most once during the lifetime
  31. * of the application. OpenSSL >= 1.1.0 users do not need to manually invoke
  32. * this method, as OpenSSL will automatically cleanup itself during the exit
  33. * of the application.
  34. */
  35. void cleanup();
  36. /**
  37. * Mark openssl as initialized without actually performing any initialization.
  38. * Please use this only if you are using a library which requires that it must
  39. * make its own calls to SSL_library_init() and related functions.
  40. */
  41. void markInitialized();
  42. /**
  43. * Set preferences for how to treat locks in OpenSSL. This must be
  44. * called before folly::ssl::init(), otherwise the defaults will be used.
  45. *
  46. * OpenSSL has a lock for each module rather than for each object or
  47. * data that needs locking. Some locks protect only refcounts, and
  48. * might be better as spinlocks rather than mutexes. Other locks
  49. * may be totally unnecessary if the objects being protected are not
  50. * shared between threads in the application.
  51. *
  52. * For a list of OpenSSL lock types, refer to crypto/crypto.h.
  53. *
  54. * By default, all locks are initialized as mutexes. OpenSSL's lock usage
  55. * may change from version to version and you should know what you are doing
  56. * before disabling any locks entirely.
  57. *
  58. * In newer versions of OpenSSL (>= 1.1.0), OpenSSL manages its own locks,
  59. * and this function is a no-op.
  60. *
  61. * Example: if you don't share SSL sessions between threads in your
  62. * application, you may be able to do this
  63. *
  64. * setSSLLockTypes({{
  65. * CRYPTO_LOCK_SSL_SESSION,
  66. * SSLContext::SSLLockType::LOCK_NONE
  67. * }})
  68. */
  69. void setLockTypes(LockTypeMapping inLockTypes);
  70. /**
  71. * Set the lock types and initialize OpenSSL in an atomic fashion. This
  72. * aborts if the library has already been initialized.
  73. */
  74. void setLockTypesAndInit(LockTypeMapping lockTypes);
  75. bool isLockDisabled(int lockId);
  76. void randomize();
  77. } // namespace ssl
  78. } // namespace folly