proxygen
wangle::SSLUtil Class Reference

#include <SSLUtil.h>

Static Public Member Functions

static void getSSLCtxExIndex (int *pindex)
 
static void getRSAExIndex (int *pindex)
 
static int exDataStdStringDup (CRYPTO_EX_DATA *, ex_data_dup_from_arg_t, ex_data_dup_ptr_arg_t ptr, int, long, void *)
 
static void exDataStdStringFree (void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
 
static void getSSLSessionExStrIndex (int *pindex)
 
static std::string hexlify (const std::string &binary)
 
static const std::stringhexlify (const std::string &binary, std::string &hex)
 
static SSLResumeEnum getResumeState (folly::AsyncSSLSocket *sslSocket)
 
static std::unique_ptr< std::stringgetCommonName (const X509 *cert)
 
static std::unique_ptr< std::list< std::string > > getSubjectAltName (const X509 *cert)
 
static folly::ssl::X509UniquePtr getX509FromCertificate (const std::string &certificateData)
 

Private Types

using ex_data_dup_from_arg_t = CRYPTO_EX_DATA *
 
using ex_data_dup_ptr_arg_t = void *
 

Static Private Attributes

static std::mutex sIndexLock_
 

Detailed Description

Definition at line 65 of file SSLUtil.h.

Member Typedef Documentation

using wangle::SSLUtil::ex_data_dup_from_arg_t = CRYPTO_EX_DATA*
private

Definition at line 94 of file SSLUtil.h.

Definition at line 100 of file SSLUtil.h.

Member Function Documentation

static int wangle::SSLUtil::exDataStdStringDup ( CRYPTO_EX_DATA *  ,
ex_data_dup_from_arg_t  ,
ex_data_dup_ptr_arg_t  ptr,
int  ,
long  ,
void *   
)
inlinestatic

Definition at line 105 of file SSLUtil.h.

References dataPtr(), ptr, and string.

111  {
112  // TODO: With OpenSSL, ptr is passed in as a void* but is actually a void**
113  // So we need to convert it and then set to the duped data.
114  // see int_dup_ex_data in ex_data.c
115  // BoringSSL is saner and uses a void**
116  void** dataPtr = reinterpret_cast<void**>(ptr);
117  std::string* strData = reinterpret_cast<std::string*>(*dataPtr);
118  if (strData) {
119  *dataPtr = new std::string(*strData);
120  }
121  return 1;
122  }
void * ptr
const char * string
Definition: Conv.cpp:212
Container::value_type * dataPtr(Container &cont)
Definition: RangeTest.cpp:1082
static void wangle::SSLUtil::exDataStdStringFree ( void *  ,
void *  ptr,
CRYPTO_EX_DATA *  ,
int  ,
long  ,
void *   
)
inlinestatic

Definition at line 125 of file SSLUtil.h.

References ptr, and string.

131  {
132  if (ptr) {
133  auto strPtr = reinterpret_cast<std::string*>(ptr);
134  delete strPtr;
135  }
136  }
void * ptr
const char * string
Definition: Conv.cpp:212
std::unique_ptr< std::string > wangle::SSLUtil::getCommonName ( const X509 *  cert)
static

Get the Common Name from an X.509 certificate

Parameters
certcertificate to inspect
Returns
common name, or null if an error occurs

Definition at line 54 of file SSLUtil.cpp.

Referenced by wangle::SSLContextManager::addSSLContextConfig(), and wangle::SSLContextManager::insert().

54  {
55  X509_NAME* subject = X509_get_subject_name((X509*)cert);
56  if (!subject) {
57  return nullptr;
58  }
59  char cn[ub_common_name + 1];
60  int res = X509_NAME_get_text_by_NID(subject, NID_commonName,
61  cn, ub_common_name);
62  if (res <= 0) {
63  return nullptr;
64  } else {
65  cn[ub_common_name] = '\0';
66  return std::make_unique<std::string>(cn);
67  }
68 }
static void wangle::SSLUtil::getRSAExIndex ( int *  pindex)
inlinestatic

Definition at line 81 of file SSLUtil.h.

References g().

81  {
82  std::lock_guard<std::mutex> g(sIndexLock_);
83  if (*pindex < 0) {
84  *pindex = RSA_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr);
85  }
86  }
g_t g(f_t)
static std::mutex sIndexLock_
Definition: SSLUtil.h:67
static void wangle::SSLUtil::getSSLCtxExIndex ( int *  pindex)
inlinestatic

Ensures only one caller will allocate an ex_data index for a given static or global.

Definition at line 74 of file SSLUtil.h.

References g().

Referenced by wangle::SSLSessionCacheManager::SSLSessionCacheManager().

74  {
75  std::lock_guard<std::mutex> g(sIndexLock_);
76  if (*pindex < 0) {
77  *pindex = SSL_CTX_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr);
78  }
79  }
g_t g(f_t)
static std::mutex sIndexLock_
Definition: SSLUtil.h:67
static void wangle::SSLUtil::getSSLSessionExStrIndex ( int *  pindex)
inlinestatic

Definition at line 138 of file SSLUtil.h.

References g().

138  {
139  std::lock_guard<std::mutex> g(sIndexLock_);
140  if (*pindex < 0) {
141  *pindex = SSL_SESSION_get_ex_new_index(
142  0,
143  nullptr,
144  nullptr,
147  }
148  }
static void exDataStdStringFree(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: SSLUtil.h:125
static int exDataStdStringDup(CRYPTO_EX_DATA *, ex_data_dup_from_arg_t, ex_data_dup_ptr_arg_t ptr, int, long, void *)
Definition: SSLUtil.h:105
g_t g(f_t)
static std::mutex sIndexLock_
Definition: SSLUtil.h:67
std::unique_ptr< std::list< std::string > > wangle::SSLUtil::getSubjectAltName ( const X509 *  cert)
static

Get the Subject Alternative Name value(s) from an X.509 certificate

Parameters
certcertificate to inspect
Returns
set of zero or more alternative names, or null if an error occurs

Definition at line 70 of file SSLUtil.cpp.

References folly::portability::ssl::ASN1_STRING_get0_data(), count, folly::gen::guard(), i, folly::makeGuard(), max, name, and s.

Referenced by wangle::SSLContextManager::addSSLContextConfig(), and wangle::SSLContextManager::insert().

71  {
72 #ifdef OPENSSL_GE_101
73  auto nameList = std::make_unique<std::list<std::string>>();
74  GENERAL_NAMES* names = (GENERAL_NAMES*)X509_get_ext_d2i(
75  (X509*)cert, NID_subject_alt_name, nullptr, nullptr);
76  if (names) {
77  auto guard = folly::makeGuard([names] { GENERAL_NAMES_free(names); });
78  size_t count = sk_GENERAL_NAME_num(names);
79  CHECK(count < std::numeric_limits<int>::max());
80  for (int i = 0; i < (int)count; ++i) {
81  GENERAL_NAME* generalName = sk_GENERAL_NAME_value(names, i);
82  if (generalName->type == GEN_DNS) {
83  ASN1_STRING* s = generalName->d.dNSName;
84  const char* name = (const char*)ASN1_STRING_get0_data(s);
85  // I can't find any docs on what a negative return value here
86  // would mean, so I'm going to ignore it.
87  auto len = ASN1_STRING_length(s);
88  DCHECK(len >= 0);
89  if (size_t(len) != strlen(name)) {
90  // Null byte(s) in the name; return an error rather than depending on
91  // the caller to safely handle this case.
92  return nullptr;
93  }
94  nameList->emplace_back(name);
95  }
96  }
97  }
98  return nameList;
99 #else
100  return nullptr;
101 #endif
102 }
LogLevel max
Definition: LogLevel.cpp:31
unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
Definition: OpenSSL.cpp:199
const char * name
Definition: http_parser.c:437
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
int * count
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
Definition: ScopeGuard.h:184
static set< string > s
folly::ssl::X509UniquePtr wangle::SSLUtil::getX509FromCertificate ( const std::string certificateData)
static

Parse an X509 out of a certificate buffer (usually read from the cert file)

Parameters
certificateDataBuffer containing certificate data from file
Returns
unique_ptr to X509 (may be null)

Definition at line 104 of file SSLUtil.cpp.

105  {
106  // BIO_new_mem_buf creates a bio pointing to a read-only buffer. However,
107  // older versions of OpenSSL fail to mark the first argument `const`.
109  BIO_new_mem_buf((void*)certificateData.data(), certificateData.length()));
110  if (!bio) {
111  throw std::runtime_error("Cannot create mem BIO");
112  }
113 
114  auto x509 = folly::ssl::X509UniquePtr(
115  PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
116  if (!x509) {
117  throw std::runtime_error("Cannot read X509 from PEM bio");
118  }
119  return x509;
120 }
std::unique_ptr< X509, X509Deleter > X509UniquePtr
std::unique_ptr< BIO, BioDeleter > BioUniquePtr
static std::string wangle::SSLUtil::hexlify ( const std::string binary)
inlinestatic

Definition at line 150 of file SSLUtil.h.

References string.

Referenced by wangle::SSLSessionCacheManager::getSession(), wangle::SSLSessionCacheManager::newSession(), wangle::LocalSSLSessionCache::pruneSessionCallback(), wangle::SSLSessionCacheManager::removeSession(), and wangle::ServerSocketConfig::ServerSocketConfig().

150  {
151  std::string hex;
152  folly::hexlify<std::string, std::string>(binary, hex);
153 
154  return hex;
155  }
const char * string
Definition: Conv.cpp:212
static const std::string& wangle::SSLUtil::hexlify ( const std::string binary,
std::string hex 
)
inlinestatic

Definition at line 157 of file SSLUtil.h.

References string.

158  {
159  folly::hexlify<std::string, std::string>(binary, hex);
160 
161  return hex;
162  }

Member Data Documentation

std::mutex wangle::SSLUtil::sIndexLock_
staticprivate

Definition at line 67 of file SSLUtil.h.


The documentation for this class was generated from the following files: