11 #include <openssl/err.h> 19 throw std::runtime_error(
"Wrong key type");
21 if (EC_KEY_check_key(ecKey.get()) != 1) {
22 throw std::runtime_error(
"Private key not valid");
26 throw std::runtime_error(
"Failed to create curve");
28 auto keyGroup = EC_KEY_get0_group(ecKey.get());
29 if (EC_GROUP_cmp(keyGroup, curve.get(),
nullptr) != 0) {
30 throw std::runtime_error(
"Invalid group");
37 folly::ssl::EvpPkeyCtxUniquePtr ctx(EVP_PKEY_CTX_new(key.get(),
nullptr));
38 if (EVP_PKEY_derive_init(ctx.get()) != 1) {
39 throw std::runtime_error(
"Initializing derive context failed");
42 if (EVP_PKEY_derive_set_peer(ctx.get(), peerKey.get()) != 1) {
43 throw std::runtime_error(
"Error setting peer key");
46 if (EVP_PKEY_derive(ctx.get(),
nullptr, &secretLen) != 1) {
47 throw std::runtime_error(
"Error deriving key");
51 if (EVP_PKEY_derive(ctx.get(), buf->writableData(), &secretLen) != 1) {
52 throw std::runtime_error(
"Error deriving key");
54 buf->append(secretLen);
61 if (!ecParamKey || !params) {
62 throw std::runtime_error(
"Error initializing params");
64 if (EVP_PKEY_set1_EC_KEY(params.get(), ecParamKey.get()) != 1) {
65 throw std::runtime_error(
"Error setting ec key for params");
67 folly::ssl::EvpPkeyCtxUniquePtr kctx(EVP_PKEY_CTX_new(params.get(),
nullptr));
69 throw std::runtime_error(
"Error creating kctx");
71 if (EVP_PKEY_keygen_init(kctx.get()) != 1) {
72 throw std::runtime_error(
"Error initializing ctx");
74 EVP_PKEY* pkey =
nullptr;
75 if (EVP_PKEY_keygen(kctx.get(), &pkey) != 1) {
76 throw std::runtime_error(
"Error generating key");
90 if (!curve || !peerKey) {
91 throw std::runtime_error(
"Error initializing peer key");
95 throw std::runtime_error(
"Error initializing point");
97 if (EC_POINT_oct2point(
98 curve.get(), point.get(), range.
data(), range.
size(),
nullptr) != 1) {
99 throw std::runtime_error(
"Error decoding peer key");
101 if (EC_POINT_is_on_curve(curve.get(), point.get(),
nullptr) != 1) {
102 throw std::runtime_error(
"Peer key is not on curve");
104 if (!EC_KEY_set_public_key(peerKey.get(), point.get())) {
105 throw std::runtime_error(
"Error setting public key");
108 if (EVP_PKEY_assign_EC_KEY(peerPkey.get(), peerKey.release()) != 1) {
109 throw std::runtime_error(
"Error assigning EC key");
118 throw std::runtime_error(
"Wrong key type");
125 auto point = EC_KEY_get0_public_key(ecKey.get());
126 auto group = EC_KEY_get0_group(ecKey.get());
128 size_t len = EC_POINT_point2oct(
129 group, point, POINT_CONVERSION_UNCOMPRESSED,
nullptr, 0,
nullptr);
133 len = EC_POINT_point2oct(
136 POINT_CONVERSION_UNCOMPRESSED,
141 throw std::runtime_error(
"Failed to encode key");
148 auto err = ERR_get_error();
153 ERR_error_string_n(ERR_get_error(), errMsg,
sizeof(errMsg));
static std::unique_ptr< IOBuf > create(std::size_t capacity)
std::unique_ptr< EC_POINT, EcPointDeleter > EcPointUniquePtr
constexpr size_type size() const
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
folly::ssl::EvpPkeyUniquePtr decodeECPublicKey(folly::ByteRange range, int curveNid)
Gen range(Value begin, Value end)
std::unique_ptr< EC_KEY, EcKeyDeleter > EcKeyUniquePtr
constexpr Params params[]
std::unique_ptr< EC_GROUP, EcGroupDeleter > EcGroupUniquePtr
void validateECKey(const folly::ssl::EvpPkeyUniquePtr &key, int curveNid)
constexpr Iter data() const
std::string getOpenSSLError()
std::unique_ptr< folly::IOBuf > generateEvpSharedSecret(const folly::ssl::EvpPkeyUniquePtr &key, const folly::ssl::EvpPkeyUniquePtr &peerKey)
Optional< NamedGroup > group
std::unique_ptr< folly::IOBuf > encodeECPublicKey(const folly::ssl::EvpPkeyUniquePtr &key)
folly::ssl::EvpPkeyUniquePtr generateECKeyPair(int curveNid)