OpenSSL.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2016-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 <cstdint>
  18. // This must come before the OpenSSL includes.
  19. #include <folly/portability/Windows.h>
  20. #include <folly/Portability.h>
  21. #include <openssl/opensslv.h>
  22. #include <openssl/asn1.h>
  23. #include <openssl/bio.h>
  24. #include <openssl/crypto.h>
  25. #include <openssl/dh.h>
  26. #include <openssl/err.h>
  27. #include <openssl/evp.h>
  28. #include <openssl/hmac.h>
  29. #include <openssl/rand.h>
  30. #include <openssl/rsa.h>
  31. #include <openssl/sha.h>
  32. #include <openssl/ssl.h>
  33. #include <openssl/tls1.h>
  34. #include <openssl/x509.h>
  35. #include <openssl/x509v3.h>
  36. #ifndef OPENSSL_NO_EC
  37. #include <openssl/ec.h>
  38. #include <openssl/ecdsa.h>
  39. #endif
  40. // BoringSSL doesn't have notion of versioning although it defines
  41. // OPENSSL_VERSION_NUMBER to maintain compatibility. The following variables are
  42. // intended to be specific to OpenSSL.
  43. #if !defined(OPENSSL_IS_BORINGSSL)
  44. #define FOLLY_OPENSSL_IS_100 \
  45. (OPENSSL_VERSION_NUMBER >= 0x10000003L && \
  46. OPENSSL_VERSION_NUMBER < 0x1000105fL)
  47. #define FOLLY_OPENSSL_IS_101 \
  48. (OPENSSL_VERSION_NUMBER >= 0x1000105fL && \
  49. OPENSSL_VERSION_NUMBER < 0x1000200fL)
  50. #define FOLLY_OPENSSL_IS_102 \
  51. (OPENSSL_VERSION_NUMBER >= 0x1000200fL && \
  52. OPENSSL_VERSION_NUMBER < 0x10100000L)
  53. #define FOLLY_OPENSSL_IS_110 (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  54. #endif
  55. #if !defined(OPENSSL_IS_BORINGSSL) && !FOLLY_OPENSSL_IS_100 && \
  56. !FOLLY_OPENSSL_IS_101 && !FOLLY_OPENSSL_IS_102 && !FOLLY_OPENSSL_IS_110
  57. #warning Compiling with unsupported OpenSSL version
  58. #endif
  59. // BoringSSL and OpenSSL 0.9.8f later with TLS extension support SNI.
  60. #if defined(OPENSSL_IS_BORINGSSL) || \
  61. (OPENSSL_VERSION_NUMBER >= 0x00908070L && !defined(OPENSSL_NO_TLSEXT))
  62. #define FOLLY_OPENSSL_HAS_SNI 1
  63. #else
  64. #define FOLLY_OPENSSL_HAS_SNI 0
  65. #endif
  66. // BoringSSL and OpenSSL 1.0.2 later with TLS extension support ALPN.
  67. #if defined(OPENSSL_IS_BORINGSSL) || \
  68. (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT))
  69. #define FOLLY_OPENSSL_HAS_ALPN 1
  70. #else
  71. #define FOLLY_OPENSSL_HAS_ALPN 0
  72. #endif
  73. // This attempts to "unify" the OpenSSL libcrypto/libssl APIs between
  74. // OpenSSL 1.0.2, 1.1.0 (and some earlier versions) and BoringSSL. The general
  75. // idea is to provide namespaced wrapper methods for versions which do not
  76. // which already exist in BoringSSL and 1.1.0, but there are few APIs such as
  77. // SSL_CTX_set1_sigalgs_list and so on which exist in 1.0.2 but were removed
  78. // in BoringSSL
  79. namespace folly {
  80. namespace portability {
  81. namespace ssl {
  82. #ifdef OPENSSL_IS_BORINGSSL
  83. int SSL_CTX_set1_sigalgs_list(SSL_CTX* ctx, const char* sigalgs_list);
  84. int TLS1_get_client_version(SSL* s);
  85. #endif
  86. #if FOLLY_OPENSSL_IS_100
  87. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER*);
  88. int TLS1_get_client_version(const SSL*);
  89. #endif
  90. #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101
  91. int X509_get_signature_nid(X509* cert);
  92. #endif
  93. #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_102
  94. int SSL_CTX_up_ref(SSL_CTX* session);
  95. int SSL_SESSION_up_ref(SSL_SESSION* session);
  96. int X509_up_ref(X509* x);
  97. int X509_STORE_up_ref(X509_STORE* v);
  98. int EVP_PKEY_up_ref(EVP_PKEY* evp);
  99. void RSA_get0_key(
  100. const RSA* r,
  101. const BIGNUM** n,
  102. const BIGNUM** e,
  103. const BIGNUM** d);
  104. RSA* EVP_PKEY_get0_RSA(EVP_PKEY* pkey);
  105. DSA* EVP_PKEY_get0_DSA(EVP_PKEY* pkey);
  106. DH* EVP_PKEY_get0_DH(EVP_PKEY* pkey);
  107. EC_KEY* EVP_PKEY_get0_EC_KEY(EVP_PKEY* pkey);
  108. #endif
  109. #if !FOLLY_OPENSSL_IS_110
  110. BIO_METHOD* BIO_meth_new(int type, const char* name);
  111. void BIO_meth_free(BIO_METHOD* biom);
  112. int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int));
  113. int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int));
  114. int BIO_meth_set_puts(BIO_METHOD* biom, int (*bputs)(BIO*, const char*));
  115. int BIO_meth_set_gets(BIO_METHOD* biom, int (*bgets)(BIO*, char*, int));
  116. int BIO_meth_set_ctrl(BIO_METHOD* biom, long (*ctrl)(BIO*, int, long, void*));
  117. int BIO_meth_set_create(BIO_METHOD* biom, int (*create)(BIO*));
  118. int BIO_meth_set_destroy(BIO_METHOD* biom, int (*destroy)(BIO*));
  119. void BIO_set_data(BIO* bio, void* ptr);
  120. void* BIO_get_data(BIO* bio);
  121. void BIO_set_init(BIO* bio, int init);
  122. void BIO_set_shutdown(BIO* bio, int shutdown);
  123. const SSL_METHOD* TLS_server_method(void);
  124. const SSL_METHOD* TLS_client_method(void);
  125. const char* SSL_SESSION_get0_hostname(const SSL_SESSION* s);
  126. unsigned char* ASN1_STRING_get0_data(const ASN1_STRING* x);
  127. EVP_MD_CTX* EVP_MD_CTX_new();
  128. void EVP_MD_CTX_free(EVP_MD_CTX* ctx);
  129. HMAC_CTX* HMAC_CTX_new();
  130. void HMAC_CTX_free(HMAC_CTX* ctx);
  131. unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s);
  132. int SSL_SESSION_has_ticket(const SSL_SESSION* s);
  133. int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g);
  134. void DH_get0_pqg(
  135. const DH* dh,
  136. const BIGNUM** p,
  137. const BIGNUM** q,
  138. const BIGNUM** g);
  139. void DH_get0_key(const DH* dh, const BIGNUM** pub_key, const BIGNUM** priv_key);
  140. void DSA_get0_pqg(
  141. const DSA* dsa,
  142. const BIGNUM** p,
  143. const BIGNUM** q,
  144. const BIGNUM** g);
  145. void DSA_get0_key(
  146. const DSA* dsa,
  147. const BIGNUM** pub_key,
  148. const BIGNUM** priv_key);
  149. STACK_OF(X509_OBJECT) * X509_STORE_get0_objects(X509_STORE* store);
  150. X509* X509_STORE_CTX_get0_cert(X509_STORE_CTX* ctx);
  151. STACK_OF(X509) * X509_STORE_CTX_get0_chain(X509_STORE_CTX* ctx);
  152. STACK_OF(X509) * X509_STORE_CTX_get0_untrusted(X509_STORE_CTX* ctx);
  153. bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d);
  154. void RSA_get0_factors(const RSA* r, const BIGNUM** p, const BIGNUM** q);
  155. void RSA_get0_crt_params(
  156. const RSA* r,
  157. const BIGNUM** dmp1,
  158. const BIGNUM** dmq1,
  159. const BIGNUM** iqmp);
  160. int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s);
  161. void ECDSA_SIG_get0(const ECDSA_SIG* sig, const BIGNUM** pr, const BIGNUM** ps);
  162. using OPENSSL_INIT_SETTINGS = void;
  163. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS* settings);
  164. void OPENSSL_cleanup();
  165. const ASN1_INTEGER* X509_REVOKED_get0_serialNumber(const X509_REVOKED* r);
  166. const ASN1_TIME* X509_REVOKED_get0_revocationDate(const X509_REVOKED* r);
  167. uint32_t X509_get_extension_flags(X509* x);
  168. uint32_t X509_get_key_usage(X509* x);
  169. uint32_t X509_get_extended_key_usage(X509* x);
  170. int X509_OBJECT_get_type(const X509_OBJECT* obj);
  171. X509* X509_OBJECT_get0_X509(const X509_OBJECT* obj);
  172. const ASN1_TIME* X509_CRL_get0_lastUpdate(const X509_CRL* crl);
  173. const ASN1_TIME* X509_CRL_get0_nextUpdate(const X509_CRL* crl);
  174. const X509_ALGOR* X509_get0_tbs_sigalg(const X509* x);
  175. #endif
  176. #if FOLLY_OPENSSL_IS_110
  177. // Note: this was a type and has been fixed upstream, so the next 1.1.0
  178. // minor version upgrade will need to remove this
  179. #define OPENSSL_lh_new OPENSSL_LH_new
  180. // OpenSSL v1.1.0 removed support for SSLv2, and also removed the define that
  181. // indicates it isn't supported.
  182. #define OPENSSL_NO_SSL2
  183. #endif
  184. } // namespace ssl
  185. } // namespace portability
  186. } // namespace folly
  187. FOLLY_PUSH_WARNING
  188. FOLLY_CLANG_DISABLE_WARNING("-Wheader-hygiene")
  189. /* using override */ using namespace folly::portability::ssl;
  190. FOLLY_POP_WARNING