proxygen
fizz::DefaultCertificateVerifier Class Reference

#include <DefaultCertificateVerifier.h>

Inheritance diagram for fizz::DefaultCertificateVerifier:
fizz::CertificateVerifier

Public Types

using X509VerifyCallback = int(*)(int, X509_STORE_CTX *)
 

Public Member Functions

 DefaultCertificateVerifier (VerificationContext context)
 
 DefaultCertificateVerifier (VerificationContext context, folly::ssl::X509StoreUniquePtr &&store)
 
void verify (const std::vector< std::shared_ptr< const fizz::PeerCert >> &certs) const override
 
void setCustomVerifyCallback (X509VerifyCallback cb)
 
void setX509Store (folly::ssl::X509StoreUniquePtr &&store)
 
std::vector< ExtensiongetCertificateRequestExtensions () const override
 
- Public Member Functions inherited from fizz::CertificateVerifier
virtual ~CertificateVerifier ()=default
 

Static Public Member Functions

static X509_STORE * getDefaultX509Store ()
 
static std::unique_ptr< DefaultCertificateVerifiercreateFromCAFile (VerificationContext context, const std::string &caFile)
 

Private Member Functions

void createAuthorities ()
 

Private Attributes

CertificateAuthorities authorities_
 
VerificationContext context_
 
folly::ssl::X509StoreUniquePtr x509Store_
 
X509VerifyCallback customVerifyCallback_ {nullptr}
 

Detailed Description

Certificate verifier that verifies a certificate against a trusted certificate store

This does not perform any identity or hostname verification.

Definition at line 27 of file DefaultCertificateVerifier.h.

Member Typedef Documentation

using fizz::DefaultCertificateVerifier::X509VerifyCallback = int (*)(int, X509_STORE_CTX*)

Definition at line 29 of file DefaultCertificateVerifier.h.

Constructor & Destructor Documentation

fizz::DefaultCertificateVerifier::DefaultCertificateVerifier ( VerificationContext  context)
inlineexplicit

Definition at line 31 of file DefaultCertificateVerifier.h.

fizz::DefaultCertificateVerifier::DefaultCertificateVerifier ( VerificationContext  context,
folly::ssl::X509StoreUniquePtr &&  store 
)
inlineexplicit

Definition at line 36 of file DefaultCertificateVerifier.h.

References verify().

39  : context_(context), x509Store_(std::move(store)) {
41  }
context
Definition: CMakeCache.txt:563
folly::ssl::X509StoreUniquePtr x509Store_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567

Member Function Documentation

void fizz::DefaultCertificateVerifier::createAuthorities ( )
private

Definition at line 93 of file DefaultCertificateVerifier.cpp.

References fizz::CertificateAuthorities::authorities, folly::IOBuf::create(), fizz::DistinguishedName::encoded_name, i, folly::gen::move, folly::portability::ssl::STACK_OF(), folly::portability::ssl::X509_OBJECT_get0_X509(), and folly::portability::ssl::X509_OBJECT_get_type().

93  {
94  CertificateAuthorities auth;
95  X509_STORE* store = x509Store_ ? x509Store_.get() : getDefaultX509Store();
96  // X509_STORE stores CA certs as objects in this stack.
97  STACK_OF(X509_OBJECT)* entries = X509_STORE_get0_objects(store);
98 
99  for (int i = 0; i < sk_X509_OBJECT_num(entries); i++) {
100  X509_OBJECT* obj = sk_X509_OBJECT_value(entries, i);
101  if (X509_OBJECT_get_type(obj) == X509_LU_X509) {
102  auto certIssuer = X509_get_subject_name(X509_OBJECT_get0_X509(obj));
103  int dnLength = i2d_X509_NAME(certIssuer, nullptr);
104  if (dnLength < 0) {
105  throw std::runtime_error("Error computing DN length");
106  }
107  DistinguishedName dn;
108  dn.encoded_name = folly::IOBuf::create(dnLength);
109  auto dnData = dn.encoded_name->writableData();
110  dnLength = i2d_X509_NAME(certIssuer, &dnData);
111  if (dnLength < 0) {
112  throw std::runtime_error("Error encoding DN in DER format");
113  }
114  dn.encoded_name->append(dnLength);
115  auth.authorities.push_back(std::move(dn));
116  }
117  }
118  authorities_ = std::move(auth);
119 }
X509 * X509_OBJECT_get0_X509(const X509_OBJECT *obj)
Definition: OpenSSL.cpp:487
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
folly::ssl::X509StoreUniquePtr x509Store_
STACK_OF(X509_OBJECT)*X509_STORE_get0_objects(X509_STORE *store)
Definition: OpenSSL.cpp:305
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
int X509_OBJECT_get_type(const X509_OBJECT *obj)
Definition: OpenSSL.cpp:483
std::unique_ptr< DefaultCertificateVerifier > fizz::DefaultCertificateVerifier::createFromCAFile ( VerificationContext  context,
const std::string caFile 
)
static

Definition at line 21 of file DefaultCertificateVerifier.cpp.

References context, folly::gen::move, and folly::ssl::OpenSSLCertUtils::readStoreFromFile().

23  {
25  return std::make_unique<DefaultCertificateVerifier>(
26  context, std::move(store));
27 }
context
Definition: CMakeCache.txt:563
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static X509StoreUniquePtr readStoreFromFile(std::string caFile)
std::vector< Extension > fizz::DefaultCertificateVerifier::getCertificateRequestExtensions ( ) const
overridevirtual

Returns a vector of extensions to send in a certificate request.

Implements fizz::CertificateVerifier.

Definition at line 140 of file DefaultCertificateVerifier.cpp.

References fizz::encodeExtension().

140  {
141  std::vector<Extension> exts;
142  exts.push_back(encodeExtension(authorities_));
143  return exts;
144 }
Extension encodeExtension(const TokenBindingParameters &params)
Definition: Types.cpp:113
X509_STORE * fizz::DefaultCertificateVerifier::getDefaultX509Store ( )
static

Definition at line 121 of file DefaultCertificateVerifier.cpp.

121  {
122  static folly::ssl::X509StoreUniquePtr defaultStore([]() {
123  X509_STORE* store = X509_STORE_new();
124 
125  if (!store) {
126  throw std::bad_alloc();
127  }
128 
129  if (X509_STORE_set_default_paths(store) != 1) {
130  throw std::runtime_error("failed to set default paths");
131  }
132 
133  return store;
134  }());
135 
136  return defaultStore.get();
137 }
std::unique_ptr< X509_STORE, X509StoreDeleter > X509StoreUniquePtr
void fizz::DefaultCertificateVerifier::setCustomVerifyCallback ( X509VerifyCallback  cb)
inline

Definition at line 46 of file DefaultCertificateVerifier.h.

46  {
48  }
void fizz::DefaultCertificateVerifier::setX509Store ( folly::ssl::X509StoreUniquePtr &&  store)
inline

Definition at line 50 of file DefaultCertificateVerifier.h.

References context, folly::gen::move, and string.

50  {
51  x509Store_ = std::move(store);
53  }
folly::ssl::X509StoreUniquePtr x509Store_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void fizz::DefaultCertificateVerifier::verify ( const std::vector< std::shared_ptr< const fizz::PeerCert >> &  certs) const
overridevirtual

Verifies the certificates in certs. The peer has been already proven possession of the first certificate in certs. Throws on error or if verification fails.

Implements fizz::CertificateVerifier.

Definition at line 29 of file DefaultCertificateVerifier.cpp.

References i, fizz::Server, and string.

30  {
31  if (certs.empty()) {
32  throw std::runtime_error("no certificates to verify");
33  }
34 
35  auto leafCert = certs.front()->getX509();
36 
37  auto certChainStack = std::unique_ptr<STACK_OF(X509), STACK_OF_X509_deleter>(
38  sk_X509_new_null());
39  if (!certChainStack) {
40  throw std::bad_alloc();
41  }
42 
43  for (size_t i = 1; i < certs.size(); i++) {
44  sk_X509_push(certChainStack.get(), certs[i]->getX509().get());
45  }
46 
47  auto ctx = folly::ssl::X509StoreCtxUniquePtr(X509_STORE_CTX_new());
48  if (!ctx) {
49  throw std::bad_alloc();
50  }
51 
52  if (X509_STORE_CTX_init(
53  ctx.get(),
55  leafCert.get(),
56  certChainStack.get()) != 1) {
57  throw std::runtime_error("failed to initialize store context");
58  }
59 
60  if (X509_STORE_CTX_set_default(
61  ctx.get(),
62  context_ == VerificationContext::Server ? "ssl_client"
63  : "ssl_server") != 1) {
64  throw std::runtime_error("failed to set default verification method");
65  }
66 
68  X509_STORE_CTX_set_verify_cb(ctx.get(), customVerifyCallback_);
69  }
70 
71  folly::ssl::X509VerifyParam param(X509_VERIFY_PARAM_new());
72  if (!param) {
73  throw std::bad_alloc();
74  }
75 
76  if (X509_VERIFY_PARAM_set_flags(param.get(), X509_V_FLAG_X509_STRICT) != 1) {
77  throw std::runtime_error("failed to set strict certificate checking");
78  }
79 
80  if (X509_VERIFY_PARAM_set1(
81  X509_STORE_CTX_get0_param(ctx.get()), param.get()) != 1) {
82  throw std::runtime_error("failed to apply verification parameters");
83  }
84 
85  if (X509_verify_cert(ctx.get()) != 1) {
86  const auto errorInt = X509_STORE_CTX_get_error(ctx.get());
87  std::string errorText =
88  std::string(X509_verify_cert_error_string(errorInt));
89  throw std::runtime_error("certificate verification failed: " + errorText);
90  }
91 }
folly::ssl::X509StoreUniquePtr x509Store_
const char * string
Definition: Conv.cpp:212
std::unique_ptr< X509_STORE_CTX, X509StoreCtxDeleter > X509StoreCtxUniquePtr
std::unique_ptr< X509_VERIFY_PARAM, X509VerifyParamDeleter > X509VerifyParam

Member Data Documentation

CertificateAuthorities fizz::DefaultCertificateVerifier::authorities_
private

Definition at line 66 of file DefaultCertificateVerifier.h.

VerificationContext fizz::DefaultCertificateVerifier::context_
private

Definition at line 67 of file DefaultCertificateVerifier.h.

X509VerifyCallback fizz::DefaultCertificateVerifier::customVerifyCallback_ {nullptr}
private

Definition at line 69 of file DefaultCertificateVerifier.h.

folly::ssl::X509StoreUniquePtr fizz::DefaultCertificateVerifier::x509Store_
private

Definition at line 68 of file DefaultCertificateVerifier.h.


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