proxygen
fizz::FizzUtil Class Reference

#include <FizzUtil.h>

Static Public Member Functions

static std::vector< folly::ssl::X509UniquePtrreadChainFile (const std::string &filename)
 
static folly::ssl::EvpPkeyUniquePtr readPrivateKey (const std::string &filename, const std::string &passwordFilename)
 
static std::vector< std::stringgetAlpnsFromNpnList (const std::list< folly::SSLContext::NextProtocolsItem > &list)
 
static folly::ssl::EvpPkeyUniquePtr decryptPrivateKey (const std::string &data, folly::PasswordInFile *pf)
 
template<class TicketCipher >
static std::unique_ptr< TicketCipher > createTicketCipher (const std::vector< std::string > &oldSecrets, const std::string &currentSecret, const std::vector< std::string > &newSecrets, std::chrono::seconds validity, folly::Optional< std::string > pskContext)
 

Detailed Description

Definition at line 22 of file FizzUtil.h.

Member Function Documentation

template<class TicketCipher >
static std::unique_ptr<TicketCipher> fizz::FizzUtil::createTicketCipher ( const std::vector< std::string > &  oldSecrets,
const std::string currentSecret,
const std::vector< std::string > &  newSecrets,
std::chrono::seconds  validity,
folly::Optional< std::string pskContext 
)
inlinestatic

Definition at line 43 of file FizzUtil.h.

References cipher, folly::Optional< Value >::hasValue(), folly::gen::move, and secret.

48  {
49  std::vector<folly::ByteRange> ticketSecrets;
50  if (!currentSecret.empty()) {
51  ticketSecrets.push_back(folly::StringPiece(currentSecret));
52  }
53  for (const auto& secret : oldSecrets) {
54  ticketSecrets.push_back(folly::StringPiece(secret));
55  }
56  for (const auto& secret : newSecrets) {
57  ticketSecrets.push_back(folly::StringPiece(secret));
58  }
59  std::unique_ptr<TicketCipher> cipher;
60  if (pskContext.hasValue()) {
61  cipher = std::make_unique<TicketCipher>(std::move(*pskContext));
62  } else {
63  cipher = std::make_unique<TicketCipher>();
64  }
65  cipher->setTicketSecrets(std::move(ticketSecrets));
66  cipher->setValidity(validity);
67  return cipher;
68  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CipherSuite cipher
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
static constexpr StringPiece secret
folly::ssl::EvpPkeyUniquePtr fizz::FizzUtil::decryptPrivateKey ( const std::string data,
folly::PasswordInFile pf 
)
static

Definition at line 73 of file FizzUtil.cpp.

References fizz::passwordCallback().

Referenced by readPrivateKey().

75  {
76  folly::ssl::BioUniquePtr keyBio(BIO_new_mem_buf(data.data(), data.size()));
77  if (!keyBio) {
78  throw std::runtime_error("couldn't create bio");
79  }
80 
82  if (pf) {
83  pkey.reset(
84  PEM_read_bio_PrivateKey(keyBio.get(), nullptr, passwordCallback, pf));
85  } else {
86  pkey.reset(
87  PEM_read_bio_PrivateKey(keyBio.get(), nullptr, nullptr, nullptr));
88  }
89 
90  if (!pkey) {
91  throw std::runtime_error("couldn't read private key");
92  }
93 
94  return pkey;
95 }
std::unique_ptr< BIO, BioDeleter > BioUniquePtr
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
static int passwordCallback(char *password, int size, int, void *data)
Definition: FizzUtil.cpp:19
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
std::vector< std::string > fizz::FizzUtil::getAlpnsFromNpnList ( const std::list< folly::SSLContext::NextProtocolsItem > &  list)
static

Definition at line 97 of file FizzUtil.cpp.

98  {
99  CHECK(!list.empty());
100  auto maxWeight = list.front().weight;
101  auto protoList = &list.front().protocols;
102  for (const auto& item : list) {
103  if (item.weight > maxWeight) {
104  protoList = &item.protocols;
105  }
106  }
107  return std::vector<std::string>(protoList->begin(), protoList->end());
108 }
std::vector< folly::ssl::X509UniquePtr > fizz::FizzUtil::readChainFile ( const std::string filename)
static

Definition at line 36 of file FizzUtil.cpp.

References folly::ssl::OpenSSLCertUtils::readCertsFromBuffer(), folly::readFile(), and string.

37  {
38  std::string certData;
39  if (!folly::readFile(filename.c_str(), certData)) {
40  throw std::runtime_error(
41  folly::to<std::string>("couldn't read cert file: ", filename));
42  }
43  auto certRange = folly::ByteRange(folly::StringPiece(certData));
45  if (certs.empty()) {
46  throw std::runtime_error(
47  folly::to<std::string>("couldn't read any cert from: ", filename));
48  }
49  return certs;
50 }
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
const char * string
Definition: Conv.cpp:212
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
static std::vector< X509UniquePtr > readCertsFromBuffer(ByteRange range)
folly::ssl::EvpPkeyUniquePtr fizz::FizzUtil::readPrivateKey ( const std::string filename,
const std::string passwordFilename 
)
static

Definition at line 52 of file FizzUtil.cpp.

References data, decryptPrivateKey(), folly::readFile(), folly::sformat(), and string.

54  {
56  folly::readFile(filename.c_str(), data);
57  try {
58  if (!passwordFilename.empty()) {
59  folly::PasswordInFile pf(passwordFilename);
60  return FizzUtil::decryptPrivateKey(data, &pf);
61  } else {
62  return FizzUtil::decryptPrivateKey(data, nullptr);
63  }
64  } catch (std::runtime_error& e) {
65  auto ex = folly::sformat(
66  "Failed to read private key from file: {}, password file: {}",
67  filename,
68  passwordFilename);
69  std::throw_with_nested(std::runtime_error(ex));
70  }
71 }
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
static folly::ssl::EvpPkeyUniquePtr decryptPrivateKey(const std::string &data, folly::PasswordInFile *pf)
Definition: FizzUtil.cpp:73
const char * string
Definition: Conv.cpp:212
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43

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