A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.5 KiB

  1. /*
  2. * Copyright 1998-2001 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. #ifndef OSSL_TEST_SHIM_INCLUDE_OPENSSL_BASE_H
  10. #define OSSL_TEST_SHIM_INCLUDE_OPENSSL_BASE_H
  11. /* Needed for DH functions */
  12. #include "internal/deprecated.h"
  13. /* Needed for BORINGSSL_MAKE_DELETER */
  14. # include <openssl/bio.h>
  15. # include <openssl/evp.h>
  16. # include <openssl/dh.h>
  17. # include <openssl/x509.h>
  18. # include <openssl/ssl.h>
  19. # define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
  20. extern "C++" {
  21. #include <memory>
  22. namespace bssl {
  23. namespace internal {
  24. template <typename T>
  25. struct DeleterImpl {};
  26. template <typename T>
  27. struct Deleter {
  28. void operator()(T *ptr) {
  29. // Rather than specialize Deleter for each type, we specialize
  30. // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only
  31. // including base.h as long as the destructor is not emitted. This matches
  32. // std::unique_ptr's behavior on forward-declared types.
  33. //
  34. // DeleterImpl itself is specialized in the corresponding module's header
  35. // and must be included to release an object. If not included, the compiler
  36. // will error that DeleterImpl<T> does not have a method Free.
  37. DeleterImpl<T>::Free(ptr);
  38. }
  39. };
  40. template <typename T, typename CleanupRet, void (*init)(T *),
  41. CleanupRet (*cleanup)(T *)>
  42. class StackAllocated {
  43. public:
  44. StackAllocated() { init(&ctx_); }
  45. ~StackAllocated() { cleanup(&ctx_); }
  46. StackAllocated(const StackAllocated<T, CleanupRet, init, cleanup> &) = delete;
  47. T& operator=(const StackAllocated<T, CleanupRet, init, cleanup> &) = delete;
  48. T *get() { return &ctx_; }
  49. const T *get() const { return &ctx_; }
  50. void Reset() {
  51. cleanup(&ctx_);
  52. init(&ctx_);
  53. }
  54. private:
  55. T ctx_;
  56. };
  57. } // namespace internal
  58. #define BORINGSSL_MAKE_DELETER(type, deleter) \
  59. namespace internal { \
  60. template <> \
  61. struct DeleterImpl<type> { \
  62. static void Free(type *ptr) { deleter(ptr); } \
  63. }; \
  64. }
  65. // This makes a unique_ptr to STACK_OF(type) that owns all elements on the
  66. // stack, i.e. it uses sk_pop_free() to clean up.
  67. #define BORINGSSL_MAKE_STACK_DELETER(type, deleter) \
  68. namespace internal { \
  69. template <> \
  70. struct DeleterImpl<STACK_OF(type)> { \
  71. static void Free(STACK_OF(type) *ptr) { \
  72. sk_##type##_pop_free(ptr, deleter); \
  73. } \
  74. }; \
  75. }
  76. // Holds ownership of heap-allocated BoringSSL structures. Sample usage:
  77. // bssl::UniquePtr<BIO> rsa(RSA_new());
  78. // bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
  79. template <typename T>
  80. using UniquePtr = std::unique_ptr<T, internal::Deleter<T>>;
  81. BORINGSSL_MAKE_DELETER(BIO, BIO_free)
  82. BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
  83. BORINGSSL_MAKE_DELETER(DH, DH_free)
  84. BORINGSSL_MAKE_DELETER(X509, X509_free)
  85. BORINGSSL_MAKE_DELETER(SSL, SSL_free)
  86. BORINGSSL_MAKE_DELETER(SSL_CTX, SSL_CTX_free)
  87. BORINGSSL_MAKE_DELETER(SSL_SESSION, SSL_SESSION_free)
  88. } // namespace bssl
  89. } /* extern C++ */
  90. #endif /* OSSL_TEST_SHIM_INCLUDE_OPENSSL_BASE_H */