proxygen
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
16 
17 namespace fizz {
18 namespace server {
19 namespace test {
20 
22  public:
24  public:
25  virtual ~CallbackFactory() = default;
27  std::shared_ptr<AsyncFizzServer> server) = 0;
28  };
29 
30  FizzTestServer(folly::EventBase& evb, CallbackFactory* factory, int port = 0)
31  : factory_(factory), evb_(evb) {
32  auto certData =
33  fizz::test::createCert("fizz-test-selfsign", false, nullptr);
34  std::vector<folly::ssl::X509UniquePtr> certChain;
35  certChain.push_back(std::move(certData.cert));
36  auto fizzCert = std::make_unique<SelfCertImpl<KeyType::P256>>(
37  std::move(certData.key), std::move(certChain));
38  auto certManager = std::make_unique<CertManager>();
39  certManager->addCert(std::move(fizzCert), true);
40  ctx_ = std::make_shared<FizzServerContext>();
41  ctx_->setCertManager(std::move(certManager));
44  socket_->bind(port);
45  socket_->listen(100);
46  socket_->addAcceptCallback(this, &evb_);
47  socket_->startAccepting();
48  }
49 
50  void setFizzContext(std::shared_ptr<FizzServerContext> ctx) {
51  ctx_ = ctx;
52  }
53 
54  void acceptError(const std::exception& ex) noexcept override {
55  LOG(ERROR) << "Accept error: " << ex.what();
56  }
57 
59  int fd,
60  const folly::SocketAddress& /* clientAddr */) noexcept override {
61  auto sock = new folly::AsyncSocket(&evb_, fd);
62  std::shared_ptr<AsyncFizzServer> transport = AsyncFizzServer::UniquePtr(
64  auto callback = factory_->getCallback(transport);
65  transport->accept(callback);
66  }
67 
68  void setResumption(bool enable) {
69  if (enable) {
70  auto ticketCipher = std::make_shared<AeadTicketCipher<
73  HkdfImpl<Sha256>>>();
74  auto ticketSeed = RandomGenerator<32>().generateRandom();
75  ticketCipher->setTicketSecrets({{folly::range(ticketSeed)}});
76  ctx_->setTicketCipher(ticketCipher);
77  } else {
78  ctx_->setTicketCipher(nullptr);
79  }
80  }
81 
82  void setCertificate(std::unique_ptr<SelfCert> cert) {
83  auto certManager = std::make_unique<CertManager>();
84  certManager->addCert(std::move(cert), true);
85  ctx_->setCertManager(std::move(certManager));
86  }
87 
89  std::string path,
91  ctx_->setClientAuthMode(mode);
92  std::string certData;
93  CHECK(folly::readFile(path.c_str(), certData));
94  auto certRange = folly::ByteRange(folly::StringPiece(certData));
95 
96  auto clientAuthCerts =
98  ERR_clear_error();
99  folly::ssl::X509StoreUniquePtr store(X509_STORE_new());
100  for (auto& caCert : clientAuthCerts) {
101  if (X509_STORE_add_cert(store.get(), caCert.get()) != 1) {
102  auto err = ERR_get_error();
103  CHECK(
104  ERR_GET_LIB(err) == ERR_LIB_X509 &&
105  ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE)
106  << "Could not insert CA certificate into store: "
107  << std::string(ERR_error_string(err, nullptr));
108  }
109  }
110 
111  auto verifier = std::make_shared<DefaultCertificateVerifier>(
113  ctx_->setClientCertVerifier(std::move(verifier));
114  }
115 
117  ctx_->setClientAuthMode(ClientAuthMode::None);
118  ctx_->setClientCertVerifier(nullptr);
119  }
120 
121  void setAcceptEarlyData(bool enable) {
122  if (enable) {
123  ctx_->setEarlyDataSettings(
124  true,
125  {std::chrono::seconds(-10), std::chrono::seconds(10)},
126  std::make_shared<AllowAllReplayReplayCache>());
127  } else {
128  ctx_->setEarlyDataSettings(false, ClockSkewTolerance(), nullptr);
129  }
130  }
131 
132  void stopAccepting() {
133  socket_.reset();
134  }
135 
138  socket_->getAddress(&addr);
139  return addr;
140  }
141 
142  std::shared_ptr<FizzServerContext> getFizzContext() {
143  return ctx_;
144  }
145 
146  private:
148  std::shared_ptr<FizzServerContext> ctx_;
151 };
152 
153 } // namespace test
154 } // namespace server
155 } // namespace fizz
std::shared_ptr< FizzServerContext > getFizzContext()
Definition: Utils.h:142
folly::AsyncServerSocket::UniquePtr socket_
Definition: Utils.h:147
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
folly::EventBase & evb_
Definition: Utils.h:150
void setResumption(bool enable)
Definition: Utils.h:68
CallbackFactory * factory_
Definition: Utils.h:149
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
virtual AsyncFizzServer::HandshakeCallback * getCallback(std::shared_ptr< AsyncFizzServer > server)=0
requires E e noexcept(noexcept(s.error(std::move(e))))
folly::SocketAddress getAddress()
Definition: Utils.h:136
void connectionAccepted(int fd, const folly::SocketAddress &) noexceptoverride
Definition: Utils.h:58
folly::Optional< PskKeyExchangeMode > mode
std::unique_ptr< X509_STORE, X509StoreDeleter > X509StoreUniquePtr
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
std::shared_ptr< FizzServerContext > ctx_
Definition: Utils.h:148
Definition: Actions.h:16
void setFizzContext(std::shared_ptr< FizzServerContext > ctx)
Definition: Utils.h:50
void setAcceptEarlyData(bool enable)
Definition: Utils.h:121
void acceptError(const std::exception &ex) noexceptoverride
Definition: Utils.h:54
void setCertificate(std::unique_ptr< SelfCert > cert)
Definition: Utils.h:82
std::unique_ptr< AsyncServerSocket, Destructor > UniquePtr
std::unique_ptr< AsyncFizzServerT, folly::DelayedDestruction::Destructor > UniquePtr
CertAndKey createCert(std::string cn, bool ca, CertAndKey *issuer)
Definition: Utilities.h:35
const char * string
Definition: Conv.cpp:212
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
static std::vector< X509UniquePtr > readCertsFromBuffer(ByteRange range)
AsyncFizzServerT< ServerStateMachine > AsyncFizzServer
FizzTestServer(folly::EventBase &evb, CallbackFactory *factory, int port=0)
Definition: Utils.h:30
void enableClientAuthWithChain(std::string path, ClientAuthMode mode=ClientAuthMode::Optional)
Definition: Utils.h:88
ThreadPoolListHook * addr
std::unique_ptr< AsyncSocket, Destructor > UniquePtr
Definition: AsyncSocket.h:83