proxygen
FizzConfigUtil.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-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 
18 
20 #include <folly/Format.h>
21 
22 using fizz::CertUtils;
24 using fizz::FizzUtil;
29 
30 namespace wangle {
31 
32 namespace {
33 std::unique_ptr<fizz::server::CertManager> createCertManager(
34  const ServerSocketConfig& config) {
35  auto certMgr = std::make_unique<fizz::server::CertManager>();
36  auto loadedCert = false;
37  for (const auto& sslConfig : config.sslContextConfigs) {
38  for (const auto& cert : sslConfig.certificates) {
39  try {
40  auto x509Chain = FizzUtil::readChainFile(cert.certPath);
41  auto pkey = FizzUtil::readPrivateKey(cert.keyPath, cert.passwordPath);
42  auto selfCert =
43  CertUtils::makeSelfCert(std::move(x509Chain), std::move(pkey));
44  certMgr->addCert(std::move(selfCert), sslConfig.isDefault);
45  loadedCert = true;
46  } catch (const std::runtime_error& ex) {
47  auto msg = folly::sformat(
48  "Failed to load cert or key at key path {}, cert path {}",
49  cert.keyPath,
50  cert.certPath);
51  if (config.strictSSL) {
52  throw std::runtime_error(ex.what() + msg);
53  } else {
54  LOG(ERROR) << msg << ex.what();
55  }
56  }
57  }
58  }
59  if (!loadedCert) {
60  return nullptr;
61  }
62  return certMgr;
63 }
64 } // namespace
65 
66 std::shared_ptr<fizz::server::FizzServerContext>
68  const ServerSocketConfig& config,
69  std::unique_ptr<fizz::server::CertManager> certMgr) {
70  if (config.sslContextConfigs.empty()) {
71  return nullptr;
72  }
73  if (!certMgr) {
74  certMgr = createCertManager(config);
75  if (!certMgr) {
76  return nullptr;
77  }
78  }
79 
80  auto ctx = std::make_shared<fizz::server::FizzServerContext>();
81  ctx->setSupportedVersions({ProtocolVersion::tls_1_3,
82  ProtocolVersion::tls_1_3_28,
83  ProtocolVersion::tls_1_3_26});
84  ctx->setVersionFallbackEnabled(true);
85  ctx->setCertManager(std::move(certMgr));
86 
87  // Fizz does not yet support randomized next protocols so we use the highest
88  // weighted list on the first context.
89  const auto& list = config.sslContextConfigs.front().nextProtocols;
90  if (!list.empty()) {
91  ctx->setSupportedAlpns(FizzUtil::getAlpnsFromNpnList(list));
92  }
93 
94  auto verify = config.sslContextConfigs.front().clientVerification;
95  switch (verify) {
96  case folly::SSLContext::SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT:
97  ctx->setClientAuthMode(ClientAuthMode::Required);
98  break;
99  case folly::SSLContext::SSLVerifyPeerEnum::VERIFY:
100  ctx->setClientAuthMode(ClientAuthMode::Optional);
101  break;
102  default:
103  ctx->setClientAuthMode(ClientAuthMode::None);
104  }
105 
106  auto caFile = config.sslContextConfigs.front().clientCAFile;
107  if (!caFile.empty()) {
108  auto verifier = DefaultCertificateVerifier::createFromCAFile(
109  VerificationContext::Server, caFile);
110  ctx->setClientCertVerifier(std::move(verifier));
111  }
112 
113  return ctx;
114 }
115 
116 } // namespace wangle
void verify(int extras)
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static std::shared_ptr< fizz::server::FizzServerContext > createFizzContext(const wangle::ServerSocketConfig &config, std::unique_ptr< fizz::server::CertManager > certMgr=nullptr)
ProtocolVersion
Definition: Types.h:24
AHArrayT::Config config
Encoder::MutableCompressedList list
PskKeyExchangeMode
Definition: Types.h:163
std::vector< SSLContextConfig > sslContextConfigs