proxygen
Factory.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 
9 #pragma once
10 
25 #include <fizz/record/Types.h>
26 
27 namespace fizz {
28 
32 class Factory {
33  public:
34  virtual ~Factory() = default;
35 
36  virtual std::unique_ptr<PlaintextReadRecordLayer>
38  return std::make_unique<PlaintextReadRecordLayer>();
39  }
40 
41  virtual std::unique_ptr<PlaintextWriteRecordLayer>
43  return std::make_unique<PlaintextWriteRecordLayer>();
44  }
45 
46  virtual std::unique_ptr<EncryptedReadRecordLayer>
48  return std::make_unique<EncryptedReadRecordLayer>(encryptionLevel);
49  }
50 
51  virtual std::unique_ptr<EncryptedWriteRecordLayer>
53  return std::make_unique<EncryptedWriteRecordLayer>(encryptionLevel);
54  }
55 
56  virtual std::unique_ptr<KeyScheduler> makeKeyScheduler(
57  CipherSuite cipher) const {
58  auto keyDer = makeKeyDeriver(cipher);
59  return std::make_unique<KeyScheduler>(std::move(keyDer));
60  }
61 
62  virtual std::unique_ptr<KeyDerivation> makeKeyDeriver(
63  CipherSuite cipher) const {
64  switch (cipher) {
68  return std::make_unique<KeyDerivationImpl<Sha256>>(getHkdfPrefix());
70  return std::make_unique<KeyDerivationImpl<Sha384>>(getHkdfPrefix());
71  default:
72  throw std::runtime_error("ks: not implemented");
73  }
74  }
75 
76  virtual std::unique_ptr<HandshakeContext> makeHandshakeContext(
77  CipherSuite cipher) const {
78  switch (cipher) {
82  return std::make_unique<HandshakeContextImpl<Sha256>>(getHkdfPrefix());
84  return std::make_unique<HandshakeContextImpl<Sha384>>(getHkdfPrefix());
85  default:
86  throw std::runtime_error("hs: not implemented");
87  }
88  }
89 
90  virtual std::unique_ptr<KeyExchange> makeKeyExchange(NamedGroup group) const {
91  switch (group) {
93  return std::make_unique<OpenSSLKeyExchange<P256>>();
95  return std::make_unique<OpenSSLKeyExchange<P384>>();
97  return std::make_unique<OpenSSLKeyExchange<P521>>();
98  case NamedGroup::x25519:
99  return std::make_unique<X25519KeyExchange>();
100  default:
101  throw std::runtime_error("ke: not implemented");
102  }
103  }
104 
105  virtual std::unique_ptr<Aead> makeAead(CipherSuite cipher) const {
106  switch (cipher) {
108  return std::make_unique<OpenSSLEVPCipher<ChaCha20Poly1305>>();
110  return std::make_unique<OpenSSLEVPCipher<AESGCM128>>();
112  return std::make_unique<OpenSSLEVPCipher<AESGCM256>>();
114  return std::make_unique<OpenSSLEVPCipher<AESOCB128>>();
115  default:
116  throw std::runtime_error("aead: not implemented");
117  }
118  }
119 
120  virtual Random makeRandom() const {
121  return RandomGenerator<Random().size()>().generateRandom();
122  }
123 
124  virtual uint32_t makeTicketAgeAdd() const {
125  return RandomNumGenerator<uint32_t>().generateRandom();
126  }
127 
128  virtual std::shared_ptr<PeerCert> makePeerCert(Buf certData) const {
129  return CertUtils::makePeerCert(std::move(certData));
130  }
131 
132  virtual std::string getHkdfPrefix() const {
133  return kHkdfLabelPrefix.str();
134  }
135 };
136 } // namespace fizz
virtual std::string getHkdfPrefix() const
Definition: Factory.h:132
std::string str() const
Definition: Range.h:591
virtual std::unique_ptr< PlaintextReadRecordLayer > makePlaintextReadRecordLayer() const
Definition: Factory.h:37
NamedGroup
Definition: Types.h:302
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
virtual ~Factory()=default
CipherSuite
Definition: Types.h:153
virtual std::unique_ptr< KeyScheduler > makeKeyScheduler(CipherSuite cipher) const
Definition: Factory.h:56
virtual std::unique_ptr< KeyExchange > makeKeyExchange(NamedGroup group) const
Definition: Factory.h:90
virtual std::unique_ptr< Aead > makeAead(CipherSuite cipher) const
Definition: Factory.h:105
CipherSuite cipher
virtual std::shared_ptr< PeerCert > makePeerCert(Buf certData) const
Definition: Factory.h:128
virtual std::unique_ptr< EncryptedReadRecordLayer > makeEncryptedReadRecordLayer(EncryptionLevel encryptionLevel) const
Definition: Factory.h:47
EncryptionLevel
Definition: Types.h:29
Definition: Actions.h:16
virtual std::unique_ptr< HandshakeContext > makeHandshakeContext(CipherSuite cipher) const
Definition: Factory.h:76
std::array< uint8_t, 32 > Random
Definition: Types.h:184
static std::unique_ptr< PeerCert > makePeerCert(Buf certData)
Definition: Certificate.cpp:87
Optional< NamedGroup > group
virtual std::unique_ptr< EncryptedWriteRecordLayer > makeEncryptedWriteRecordLayer(EncryptionLevel encryptionLevel) const
Definition: Factory.h:52
virtual uint32_t makeTicketAgeAdd() const
Definition: Factory.h:124
const char * string
Definition: Conv.cpp:212
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
virtual Random makeRandom() const
Definition: Factory.h:120
constexpr folly::StringPiece kHkdfLabelPrefix
Definition: Types.h:20
virtual std::unique_ptr< KeyDerivation > makeKeyDeriver(CipherSuite cipher) const
Definition: Factory.h:62
virtual std::unique_ptr< PlaintextWriteRecordLayer > makePlaintextWriteRecordLayer() const
Definition: Factory.h:42