17 using namespace folly;
22 namespace extensions {
29 if (negotiatedParameters != TokenBindingKeyParameters::ecdsap256) {
30 throw std::runtime_error(folly::to<std::string>(
31 "key params not implemented: ", negotiatedParameters));
36 throw std::runtime_error(
"Unable to retrieve EC Key");
44 TokenBindingUtils::constructMessage(type, negotiatedParameters, ekm);
49 id.key = encodeEcKey(ecKey);
54 Buf TokenBindingConstructor::signWithEcKey(
57 std::array<uint8_t, fizz::Sha256::HashLen> hashedMessage;
63 ECDSA_do_sign(hashedMessage.data(), hashedMessage.size(), key.get()));
64 if (!ecSignature.get()) {
65 throw std::runtime_error(
"Unable to sign message with EC Key");
68 return encodeEcdsaSignature(ecSignature);
71 Buf TokenBindingConstructor::encodeEcdsaSignature(
75 ECDSA_SIG_get0(signature.get(), (
const BIGNUM**)&r, (
const BIGNUM**)&
s);
77 throw std::runtime_error(
"Unable to retrieve Bignum from ECDSA sig");
80 Buf encodedSignature =
82 addBignumToSignature(encodedSignature, r);
83 addBignumToSignature(encodedSignature, s);
84 return encodedSignature;
87 void TokenBindingConstructor::addBignumToSignature(
90 auto length = BN_num_bytes(bigNum);
91 if (length > TokenBindingUtils::kP256EcKeySize / 2) {
92 throw std::runtime_error(
"ECDSA sig bignum is of incorrect size");
96 size_t padding = (TokenBindingUtils::kP256EcKeySize / 2) - length;
97 std::memset(signature->writableTail(), 0x00, padding);
98 signature->append(padding);
100 auto lenActual = BN_bn2bin(bigNum, signature->writableTail());
101 signature->append(lenActual);
102 if (lenActual != length) {
103 throw std::runtime_error(
"bn2bin returned unexpected value");
109 if (ecKeyBuf->isChained() ||
110 ecKeyBuf->length() != TokenBindingUtils::kP256EcKeySize + 1) {
111 throw std::runtime_error(
"Incorrect encoded EC Key Length");
113 ecKeyBuf->writableData()[0] = TokenBindingUtils::kP256EcKeySize;
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
static std::unique_ptr< IOBuf > create(std::size_t capacity)
constexpr detail::Map< Move > move
—— Concurrent Priority Queue Implementation ——
TokenBindingType tokenbinding_type
TokenBindingKeyParameters
std::unique_ptr< ECDSA_SIG, EcdsaSigDeleter > EcdsaSigUniquePtr
std::unique_ptr< EC_KEY, EcKeyDeleter > EcKeyUniquePtr
static void hash(const folly::IOBuf &in, folly::MutableByteRange out)
TokenBindingKeyParameters key_parameters
TokenBindingID tokenbindingid
std::unique_ptr< folly::IOBuf > Buf
std::unique_ptr< folly::IOBuf > encodeECPublicKey(const folly::ssl::EvpPkeyUniquePtr &key)