proxygen
fizz::CertUtils Class Reference

#include <Certificate.h>

Static Public Member Functions

static Buf prepareSignData (CertificateVerifyContext context, folly::ByteRange toBeSigned)
 
static CertificateMsg getCertMessage (const std::vector< folly::ssl::X509UniquePtr > &certs, Buf certificateRequestContext)
 
template<KeyType T>
static std::vector< SignatureSchemegetSigSchemes ()
 
static std::unique_ptr< PeerCertmakePeerCert (Buf certData)
 
static std::unique_ptr< SelfCertmakeSelfCert (std::string certData, std::string keyData, const std::vector< std::shared_ptr< CertificateCompressor >> &compressors={})
 
static std::unique_ptr< SelfCertmakeSelfCert (std::string certData, std::string encryptedKeyData, std::string password, const std::vector< std::shared_ptr< CertificateCompressor >> &compressors={})
 
static std::unique_ptr< SelfCertmakeSelfCert (std::vector< folly::ssl::X509UniquePtr > certs, folly::ssl::EvpPkeyUniquePtr key, const std::vector< std::shared_ptr< CertificateCompressor >> &compressors={})
 
static CompressedCertificate cloneCompressedCert (const CompressedCertificate &src)
 

Detailed Description

Definition at line 79 of file Certificate.h.

Member Function Documentation

CompressedCertificate fizz::CertUtils::cloneCompressedCert ( const CompressedCertificate src)
static

Clones a compressed cert by copying the relevant fields and cloning the underlying data IOBuf.

Definition at line 201 of file Certificate.cpp.

References fizz::CompressedCertificate::algorithm, fizz::CompressedCertificate::compressed_certificate_message, and fizz::CompressedCertificate::uncompressed_length.

Referenced by fizz::SelfCertImpl< T >::getCompressedCert().

202  {
204  ret.algorithm = src.algorithm;
205  ret.compressed_certificate_message =
206  src.compressed_certificate_message->clone();
207  ret.uncompressed_length = src.uncompressed_length;
208  return ret;
209 }
CertificateMsg fizz::CertUtils::getCertMessage ( const std::vector< folly::ssl::X509UniquePtr > &  certs,
Buf  certificateRequestContext 
)
static

Definition at line 59 of file Certificate.cpp.

References fizz::CertificateEntry::cert_data, fizz::CertificateMsg::certificate_list, fizz::CertificateMsg::certificate_request_context, folly::IOBuf::create(), dataPtr(), and folly::gen::move.

Referenced by fizz::SelfCertImpl< T >::getCertMessage().

61  {
62  // compose the cert entry list
63  std::vector<CertificateEntry> entries;
64  for (const auto& cert : certs) {
65  CertificateEntry entry;
66  int len = i2d_X509(cert.get(), nullptr);
67  if (len < 0) {
68  throw std::runtime_error("Error computing length");
69  }
70  entry.cert_data = folly::IOBuf::create(len);
71  auto dataPtr = entry.cert_data->writableData();
72  len = i2d_X509(cert.get(), &dataPtr);
73  if (len < 0) {
74  throw std::runtime_error("Error converting cert to DER");
75  }
76  entry.cert_data->append(len);
77  // TODO: add any extensions.
78  entries.push_back(std::move(entry));
79  }
80 
81  CertificateMsg msg;
82  msg.certificate_request_context = std::move(certificateRequestContext);
83  msg.certificate_list = std::move(entries);
84  return msg;
85 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Container::value_type * dataPtr(Container &cont)
Definition: RangeTest.cpp:1082
template<KeyType T>
static std::vector<SignatureScheme> fizz::CertUtils::getSigSchemes ( )
static
std::unique_ptr< PeerCert > fizz::CertUtils::makePeerCert ( Buf  certData)
static

Create a PeerCert from the ASN1 encoded certData.

Definition at line 87 of file Certificate.cpp.

References b, folly::test::begin(), makeSelfCert(), folly::gen::move, folly::gen::range(), folly::ssl::OpenSSLCertUtils::readCertsFromBuffer(), and string.

Referenced by fizz::Factory::makePeerCert(), and fizz::server::readClientCertificate().

87  {
88  if (certData->empty()) {
89  throw std::runtime_error("empty peer cert");
90  }
91 
92  auto range = certData->coalesce();
93  const unsigned char* begin = range.data();
94  folly::ssl::X509UniquePtr cert(d2i_X509(nullptr, &begin, range.size()));
95  if (!cert) {
96  throw std::runtime_error("could not read cert");
97  }
98  if (begin != range.data() + range.size()) {
99  VLOG(1) << "Did not read to end of certificate";
100  }
101 
102  folly::ssl::EvpPkeyUniquePtr pubKey(X509_get_pubkey(cert.get()));
103  if (!pubKey) {
104  throw std::runtime_error("couldn't get pubkey from peer cert");
105  }
106  if (EVP_PKEY_id(pubKey.get()) == EVP_PKEY_RSA) {
107  return std::make_unique<PeerCertImpl<KeyType::RSA>>(std::move(cert));
108  } else if (EVP_PKEY_id(pubKey.get()) == EVP_PKEY_EC) {
109  switch (getCurveName(pubKey.get())) {
110  case NID_X9_62_prime256v1:
111  return std::make_unique<PeerCertImpl<KeyType::P256>>(std::move(cert));
112  case NID_secp384r1:
113  return std::make_unique<PeerCertImpl<KeyType::P384>>(std::move(cert));
114  case NID_secp521r1:
115  return std::make_unique<PeerCertImpl<KeyType::P521>>(std::move(cert));
116  default:
117  break;
118  }
119  }
120  throw std::runtime_error("unknown peer cert type");
121 }
std::unique_ptr< X509, X509Deleter > X509UniquePtr
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
Gen range(Value begin, Value end)
Definition: Base.h:467
std::unique_ptr< SelfCert > fizz::CertUtils::makeSelfCert ( std::string  certData,
std::string  keyData,
const std::vector< std::shared_ptr< CertificateCompressor >> &  compressors = {} 
)
static

Creates a SelfCert using the supplied certificate/key file data and compressors. Throws std::runtime_error on error.

Definition at line 154 of file Certificate.cpp.

References folly::gen::move.

Referenced by fizz::tool::fizzClientCommand(), fizz::tool::fizzServerCommand(), and makePeerCert().

157  {
158  return selfCertFromDataInternal(
159  std::move(certData), std::move(keyData), nullptr, compressors);
160 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< SelfCert > fizz::CertUtils::makeSelfCert ( std::string  certData,
std::string  encryptedKeyData,
std::string  password,
const std::vector< std::shared_ptr< CertificateCompressor >> &  compressors = {} 
)
static

Creates a SelfCert using the supplied certificate, encrypted key data, and password. Throws std::runtime_error on error.

Definition at line 162 of file Certificate.cpp.

References folly::gen::move.

166  {
167  return selfCertFromDataInternal(
168  std::move(certData), std::move(encryptedKeyData), &password[0], compressors);
169 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< SelfCert > fizz::CertUtils::makeSelfCert ( std::vector< folly::ssl::X509UniquePtr certs,
folly::ssl::EvpPkeyUniquePtr  key,
const std::vector< std::shared_ptr< CertificateCompressor >> &  compressors = {} 
)
static

Definition at line 171 of file Certificate.cpp.

References folly::gen::move.

174  {
175  folly::ssl::EvpPkeyUniquePtr pubKey(X509_get_pubkey(certs.front().get()));
176  if (!pubKey) {
177  throw std::runtime_error("Failed to read public key");
178  }
179 
180  if (EVP_PKEY_id(pubKey.get()) == EVP_PKEY_RSA) {
181  return std::make_unique<SelfCertImpl<KeyType::RSA>>(
182  std::move(key), std::move(certs), compressors);
183  } else if (EVP_PKEY_id(pubKey.get()) == EVP_PKEY_EC) {
184  switch (getCurveName(pubKey.get())) {
185  case NID_X9_62_prime256v1:
186  return std::make_unique<SelfCertImpl<KeyType::P256>>(
187  std::move(key), std::move(certs), compressors);
188  case NID_secp384r1:
189  return std::make_unique<SelfCertImpl<KeyType::P384>>(
190  std::move(key), std::move(certs), compressors);
191  case NID_secp521r1:
192  return std::make_unique<SelfCertImpl<KeyType::P521>>(
193  std::move(key), std::move(certs), compressors);
194  default:
195  break;
196  }
197  }
198  throw std::runtime_error("unknown self cert type");
199 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
Buf fizz::CertUtils::prepareSignData ( CertificateVerifyContext  context,
folly::ByteRange  toBeSigned 
)
static

Adds the appropriate context data to prepare toBeSigned for a signature scheme's signing function.

Definition at line 23 of file Certificate.cpp.

References fizz::Client, folly::IOBuf::create(), folly::Range< Iter >::data(), fizz::test::label, fizz::Server, folly::Range< Iter >::size(), and uint8_t.

Referenced by fizz::JavaCryptoPeerCert::verify().

25  {
26  static constexpr folly::StringPiece kServerLabel =
27  "TLS 1.3, server CertificateVerify";
28  static constexpr folly::StringPiece kClientLabel =
29  "TLS 1.3, client CertificateVerify";
30  static constexpr folly::StringPiece kAuthLabel = "Exported Authenticator";
31  static constexpr size_t kSigPrefixLen = 64;
32  static constexpr uint8_t kSigPrefix = 32;
33 
36  label = kServerLabel;
38  label = kClientLabel;
39  } else {
40  label = kAuthLabel;
41  }
42 
43  size_t sigDataLen = kSigPrefixLen + label.size() + 1 + toBeSigned.size();
44  auto buf = folly::IOBuf::create(sigDataLen);
45  buf->append(sigDataLen);
46 
47  // Place bytes in the right order.
48  size_t offset = 0;
49  memset(buf->writableData(), kSigPrefix, kSigPrefixLen);
50  offset += kSigPrefixLen;
51  memcpy(buf->writableData() + offset, label.data(), label.size());
52  offset += label.size();
53  memset(buf->writableData() + offset, 0, 1);
54  offset += 1;
55  memcpy(buf->writableData() + offset, toBeSigned.data(), toBeSigned.size());
56  return buf;
57 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
context
Definition: CMakeCache.txt:563
constexpr size_type size() const
Definition: Range.h:431
constexpr Iter data() const
Definition: Range.h:446
StringPiece label

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