21 template <
typename A,
typename B>
24 throw std::runtime_error(msg);
31 throw std::runtime_error(msg);
43 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
46 EC_GROUP_set_asn1_flag(grp.get(), OPENSSL_EC_NAMED_CURVE);
47 EC_GROUP_set_point_conversion_form(grp.get(), POINT_CONVERSION_UNCOMPRESSED);
49 throwIfNeq(EC_GROUP_check(grp.get(),
nullptr), 1,
"group check failed");
54 throwIfNeq(EC_KEY_set_group(ec.get(), grp.get()), 1,
"failed to set group");
56 throwIfNeq(EC_KEY_generate_key(ec.get()), 1,
"ec generation failed");
59 EVP_PKEY_set1_EC_KEY(pk.get(), ec.get()),
61 "private key assignment failed");
63 X509_set_version(crt.get(), 2);
64 static int serial = 0;
65 ASN1_INTEGER_set(X509_get_serialNumber(crt.get()), serial++);
66 X509_gmtime_adj(X509_get_notBefore(crt.get()), 0);
67 X509_gmtime_adj(X509_get_notAfter(crt.get()), 31536000);
70 X509_set_pubkey(crt.get(), pk.get()), 1,
"public key assignment failed");
72 X509_NAME*
name = X509_get_subject_name(crt.get());
73 const std::vector<std::pair<std::string, std::string>> entries{
74 {
"C",
"US"}, {
"O",
"Facebook, Inc."}, {
"CN", cn}};
75 for (
const auto& entry : entries) {
77 X509_NAME_add_entry_by_txt(
81 reinterpret_cast<const unsigned char*
>(entry.second.c_str()),
86 std::string(
"failed to set name entry: ") + entry.first);
91 X509V3_set_ctx_nodb(&ctx);
92 std::array<char, 8> constraint{
"CA:TRUE"};
94 nullptr, &ctx, NID_basic_constraints, constraint.data()));
97 X509_EXTENSION_set_critical(ext.get(), 1), 1,
"failed to set critical");
99 X509_add_ext(crt.get(), ext.get(), -1), 1,
"failed to add extension");
104 X509_set_issuer_name(
105 crt.get(), X509_get_subject_name(issuer->
cert.get())),
107 "failed to set issuer");
108 if (X509_sign(crt.get(), issuer->
key.get(), EVP_sha256()) == 0) {
109 throw std::runtime_error(
"failed to sign certificate");
113 X509_set_issuer_name(crt.get(),
name), 1,
"failed to set issuer");
114 if (X509_sign(crt.get(), pk.get(), EVP_sha256()) == 0) {
115 throw std::runtime_error(
"failed to self-sign certificate");
123 return std::make_shared<PeerCertImpl<KeyType::P256>>(
folly::ssl::EvpPkeyUniquePtr key
void throwIfNull(const A &a, const std::string &msg)
std::unique_ptr< X509, X509Deleter > X509UniquePtr
constexpr detail::Map< Move > move
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
std::unique_ptr< EC_KEY, EcKeyDeleter > EcKeyUniquePtr
std::unique_ptr< EC_GROUP, EcGroupDeleter > EcGroupUniquePtr
folly::ssl::X509UniquePtr cert
std::unique_ptr< X509_EXTENSION, X509ExtensionDeleter > X509ExtensionUniquePtr
std::shared_ptr< PeerCert > getPeerCert(const CertAndKey &cert)
CertAndKey createCert(std::string cn, bool ca, CertAndKey *issuer)
void throwIfNeq(const A &a, const B &b, const std::string &msg)