proxygen
Signature-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
10 #include <folly/io/IOBuf.h>
11 #include <folly/lang/Assume.h>
12 
13 namespace fizz {
14 
15 namespace detail {
16 
17 std::unique_ptr<folly::IOBuf> ecSign(
19  const folly::ssl::EvpPkeyUniquePtr& pkey,
20  int hashNid);
21 
22 void ecVerify(
24  folly::ByteRange signature,
25  const folly::ssl::EvpPkeyUniquePtr& pkey,
26  int hashNid);
27 
28 std::unique_ptr<folly::IOBuf> rsaPssSign(
30  const folly::ssl::EvpPkeyUniquePtr& pkey,
31  int hashNid);
32 
33 void rsaPssVerify(
35  folly::ByteRange signature,
36  const folly::ssl::EvpPkeyUniquePtr& pkey,
37  int hashNid);
38 } // namespace detail
39 
40 template <SignatureScheme Scheme>
41 struct SigAlg {};
42 
43 template <>
45  static constexpr int HashNid = NID_sha256;
46  static constexpr KeyType type = KeyType::RSA;
47 };
48 
49 template <>
51  static constexpr int HashNid = NID_sha256;
52  static constexpr KeyType type = KeyType::P256;
53 };
54 
55 template <>
57  static constexpr int HashNid = NID_sha384;
58  static constexpr KeyType type = KeyType::P384;
59 };
60 
61 template <>
63  static constexpr int HashNid = NID_sha512;
64  static constexpr KeyType type = KeyType::P521;
65 };
66 
67 template <KeyType Type>
68 template <SignatureScheme Scheme>
69 inline std::unique_ptr<folly::IOBuf> OpenSSLSignature<Type>::sign(
70  folly::ByteRange data) const {
71  static_assert(
72  SigAlg<Scheme>::type == Type, "Called with mismatched type and scheme");
73  switch (Type) {
74  case KeyType::P256:
75  case KeyType::P384:
76  case KeyType::P521:
77  return detail::ecSign(data, pkey_, SigAlg<Scheme>::HashNid);
78  case KeyType::RSA:
79  return detail::rsaPssSign(data, pkey_, SigAlg<Scheme>::HashNid);
80  }
82 }
83 
84 template <KeyType Type>
85 template <SignatureScheme Scheme>
88  folly::ByteRange signature) const {
89  switch (Type) {
90  case KeyType::P256:
91  case KeyType::P384:
92  case KeyType::P521:
93  return detail::ecVerify(data, signature, pkey_, SigAlg<Scheme>::HashNid);
94  case KeyType::RSA:
95  return detail::rsaPssVerify(
96  data, signature, pkey_, SigAlg<Scheme>::HashNid);
97  }
99 }
100 
101 template <>
104  detail::validateECKey(pkey, NID_X9_62_prime256v1);
105  pkey_ = std::move(pkey);
106 }
107 
108 template <>
111  detail::validateECKey(pkey, NID_secp384r1);
112  pkey_ = std::move(pkey);
113 }
114 
115 template <>
118  detail::validateECKey(pkey, NID_secp521r1);
119  pkey_ = std::move(pkey);
120 }
121 
122 template <>
125  if (EVP_PKEY_id(pkey.get()) != EVP_PKEY_RSA) {
126  throw std::runtime_error("key not rsa");
127  }
128  pkey_ = std::move(pkey);
129 }
130 } // namespace fizz
PskType type
void rsaPssVerify(folly::ByteRange data, folly::ByteRange signature, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)
Definition: Signature.cpp:123
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
SignatureScheme
Definition: Types.h:257
std::unique_ptr< folly::IOBuf > sign(folly::ByteRange data) const
Definition: Signature-inl.h:69
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
KeyType
Definition: Signature.h:17
FOLLY_ALWAYS_INLINE void assume_unreachable()
Definition: Assume.h:59
void validateECKey(const folly::ssl::EvpPkeyUniquePtr &key, int curveNid)
void verify(folly::ByteRange data, folly::ByteRange signature) const
Definition: Signature-inl.h:86
std::unique_ptr< folly::IOBuf > ecSign(folly::ByteRange data, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)
Definition: Signature.cpp:30
Definition: Actions.h:16
void ecVerify(folly::ByteRange data, folly::ByteRange signature, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)
Definition: Signature.cpp:59
void setKey(folly::ssl::EvpPkeyUniquePtr pkey)
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
std::unique_ptr< folly::IOBuf > rsaPssSign(folly::ByteRange data, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)
Definition: Signature.cpp:85