proxygen
fizz::extensions::Validator Class Reference

#include <Validator.h>

Static Public Member Functions

static folly::Optional< TokenBindingIDvalidateTokenBinding (TokenBinding tokenBinding, const Buf &ekm, const TokenBindingKeyParameters &negotiatedParameters)
 

Static Private Member Functions

static void verify (const TokenBindingKeyParameters &keyParams, const Buf &key, const Buf &signature, const Buf &message)
 
static folly::ssl::EcdsaSigUniquePtr constructECDSASig (const Buf &signature)
 
static folly::ssl::EcKeyUniquePtr constructEcKeyFromBuf (const Buf &key)
 

Detailed Description

Definition at line 24 of file Validator.h.

Member Function Documentation

EcdsaSigUniquePtr Validator::constructECDSASig ( const Buf signature)
staticprivate

Definition at line 79 of file Validator.cpp.

References folly::IOBuf::create(), folly::portability::ssl::ECDSA_SIG_set0(), and s.

79  {
80  EcdsaSigUniquePtr ecdsaSignature(ECDSA_SIG_new());
81  if (!ecdsaSignature) {
82  throw std::runtime_error("Unable to allocate ecdsaSignature");
83  }
84  Cursor signatureReader(signature.get());
87  signatureReader.clone(*rBytes, TokenBindingUtils::kP256EcKeySize / 2);
88  signatureReader.clone(*sBytes, TokenBindingUtils::kP256EcKeySize / 2);
89  auto rRange = rBytes->coalesce();
90  auto sRange = sBytes->coalesce();
91  BIGNUMUniquePtr r(BN_new());
92  BIGNUMUniquePtr s(BN_new());
93  if (!BN_bin2bn(
94  rRange.data(), TokenBindingUtils::kP256EcKeySize / 2, r.get()) ||
95  !BN_bin2bn(
96  sRange.data(), TokenBindingUtils::kP256EcKeySize / 2, s.get())) {
97  throw std::runtime_error("unable to create bnum");
98  }
99 
100  // ecdsaSignature will clean up Bignum ptrs,
101  // so unique ptr needs to release them to avoid double delete
102  if (ECDSA_SIG_set0(ecdsaSignature.get(), r.release(), s.release()) != 1) {
103  throw std::runtime_error("unable to set bnum on ecdsa_sig");
104  }
105  return ecdsaSignature;
106 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
static constexpr uint8_t kP256EcKeySize
Definition: Utils.h:18
std::unique_ptr< BIGNUM, BIGNUMDeleter > BIGNUMUniquePtr
std::unique_ptr< ECDSA_SIG, EcdsaSigDeleter > EcdsaSigUniquePtr
int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
Definition: OpenSSL.cpp:405
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
static set< string > s
EcKeyUniquePtr Validator::constructEcKeyFromBuf ( const Buf key)
staticprivate

Definition at line 108 of file Validator.cpp.

References folly::IOBuf::create(), fizz::detail::decodeECPublicKey(), folly::io::detail::CursorBase< Derived, BufType >::readBE(), uint8_t, and folly::io::detail::Writable< Derived >::writeBE().

108  {
109  // EC_point_oct2point expects the format to match the one described here:
110  // https://tlswg.github.io/tls13-spec/draft-ietf-tls-tls13.html#ecdhe-param
112  Appender keyAppender(combinedKey.get(), 20);
113  keyAppender.writeBE<uint8_t>(POINT_CONVERSION_UNCOMPRESSED);
114 
115  // Key string from the token binding message has key size as the first byte,
116  // so we need to retrieve the key without the size byte,
117  // and add it to the buf from earlier
118  Cursor keyReader(key.get());
119  auto keyLen = keyReader.readBE<uint8_t>();
120  if (keyLen != TokenBindingUtils::kP256EcKeySize) {
121  throw std::runtime_error(
122  folly::to<std::string>("incorrect key size: ", keyLen));
123  }
124  keyAppender.push(keyReader, keyLen);
125  auto combinedRange = combinedKey->coalesce();
126 
127  auto evpKey =
128  fizz::detail::decodeECPublicKey(combinedRange, NID_X9_62_prime256v1);
129  EcKeyUniquePtr publicKey(EVP_PKEY_get1_EC_KEY(evpKey.get()));
130  if (!publicKey) {
131  throw std::runtime_error("Error getting EC_key");
132  }
133  return publicKey;
134 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
static constexpr uint8_t kP256EcKeySize
Definition: Utils.h:18
folly::ssl::EvpPkeyUniquePtr decodeECPublicKey(folly::ByteRange range, int curveNid)
std::unique_ptr< EC_KEY, EcKeyDeleter > EcKeyUniquePtr
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
void writeBE(T value)
Definition: Cursor.h:744
Optional< TokenBindingID > Validator::validateTokenBinding ( TokenBinding  tokenBinding,
const Buf ekm,
const TokenBindingKeyParameters negotiatedParameters 
)
static

Definition at line 22 of file Validator.cpp.

References fizz::extensions::TokenBindingID::key, fizz::extensions::TokenBindingID::key_parameters, folly::gen::move, folly::none, fizz::extensions::TokenBinding::signature, fizz::extensions::TokenBinding::tokenbinding_type, fizz::extensions::TokenBinding::tokenbindingid, fizz::toString(), and verify().

25  {
26  if (tokenBinding.tokenbindingid.key_parameters != negotiatedParameters) {
27  VLOG(2) << "sent parameters: "
28  << toString(tokenBinding.tokenbindingid.key_parameters)
29  << " don't match negotiated parameters: "
30  << toString(negotiatedParameters);
31  return folly::none;
32  }
33 
34  try {
36  tokenBinding.tokenbinding_type,
37  tokenBinding.tokenbindingid.key_parameters,
38  ekm);
39  verify(
40  tokenBinding.tokenbindingid.key_parameters,
41  tokenBinding.tokenbindingid.key,
42  tokenBinding.signature,
43  message);
44  return std::move(tokenBinding.tokenbindingid);
45  } catch (const std::exception& e) {
46  VLOG(1) << "Token Binding Verification Failed: " << e.what();
47  return folly::none;
48  }
49 }
Definition: test.c:42
static Buf constructMessage(const TokenBindingType &type, const TokenBindingKeyParameters &keyParams, const Buf &ekm)
Definition: Utils.h:20
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static void verify(const TokenBindingKeyParameters &keyParams, const Buf &key, const Buf &signature, const Buf &message)
Definition: Validator.cpp:51
StringPiece tokenBinding
std::string toString(TokenBindingProtocolVersion)
Definition: Types.cpp:67
constexpr None none
Definition: Optional.h:87
void Validator::verify ( const TokenBindingKeyParameters keyParams,
const Buf key,
const Buf signature,
const Buf message 
)
staticprivate

Definition at line 51 of file Validator.cpp.

References fizz::detail::getOpenSSLError(), and fizz::Sha< Sha256 >::hash().

55  {
56  if (keyParams == TokenBindingKeyParameters::ecdsap256) {
57  auto pkey = constructEcKeyFromBuf(key);
58  auto ecdsa = constructECDSASig(signature);
59 
60  std::array<uint8_t, fizz::Sha256::HashLen> hashedMessage;
62  *message,
63  folly::MutableByteRange(hashedMessage.data(), hashedMessage.size()));
64  if (ECDSA_do_verify(
65  hashedMessage.data(),
66  hashedMessage.size(),
67  ecdsa.get(),
68  pkey.get()) != 1) {
69  throw std::runtime_error(folly::to<std::string>(
70  "Verification failed: ", detail::getOpenSSLError()));
71  }
72  } else {
73  // rsa_pss and rsa_pkcs
74  throw std::runtime_error(
75  folly::to<std::string>("key params not implemented: ", keyParams));
76  }
77 }
Definition: test.c:42
static folly::ssl::EcKeyUniquePtr constructEcKeyFromBuf(const Buf &key)
Definition: Validator.cpp:108
static folly::ssl::EcdsaSigUniquePtr constructECDSASig(const Buf &signature)
Definition: Validator.cpp:79
std::string getOpenSSLError()
static void hash(const folly::IOBuf &in, folly::MutableByteRange out)

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