Skip to content

Commit

Permalink
create pure virtual Factory interface
Browse files Browse the repository at this point in the history
Summary:
`fizz::Factory` is used by other parts of Fizz to allow customization of various components that Fizz may need to construct while servicing a handshake. However, `fizz::Factory` as it currently stands is not a pure inteface -- the current implementation tightly couples OpenSSL (e.g. `makeAead`).

Introduce a new class `IFactory` that is meant to represent the interface of Factory.

Later diffs will merge OpenSSLFactory and Factory together.

Reviewed By: mingtaoy

Differential Revision: D50714194

fbshipit-source-id: afef019d4943d284c7b8410cdf30f6d6d0365d61
  • Loading branch information
Zale Young authored and facebook-github-bot committed Oct 31, 2023
1 parent 7e124c6 commit 5c92af0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 20 deletions.
40 changes: 20 additions & 20 deletions fizz/protocol/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <fizz/crypto/exchange/X25519.h>
#include <fizz/protocol/Certificate.h>
#include <fizz/protocol/HandshakeContext.h>
#include <fizz/protocol/IFactory.h>
#include <fizz/protocol/KeyScheduler.h>
#include <fizz/record/EncryptedRecordLayer.h>
#include <fizz/record/PlaintextRecordLayer.h>
Expand All @@ -34,47 +35,44 @@ namespace fizz {
/**
* This class instantiates various objects to facilitate testing.
*/
class Factory {
class Factory : public IFactory {
public:
enum class KeyExchangeMode { Server, Client };

virtual ~Factory() = default;

virtual std::unique_ptr<PlaintextReadRecordLayer>
makePlaintextReadRecordLayer() const {
makePlaintextReadRecordLayer() const override {
return std::make_unique<PlaintextReadRecordLayer>();
}

virtual std::unique_ptr<PlaintextWriteRecordLayer>
makePlaintextWriteRecordLayer() const {
makePlaintextWriteRecordLayer() const override {
return std::make_unique<PlaintextWriteRecordLayer>();
}

virtual std::unique_ptr<EncryptedReadRecordLayer>
makeEncryptedReadRecordLayer(EncryptionLevel encryptionLevel) const {
makeEncryptedReadRecordLayer(EncryptionLevel encryptionLevel) const override {
return std::make_unique<EncryptedReadRecordLayer>(encryptionLevel);
}

virtual std::unique_ptr<EncryptedWriteRecordLayer>
makeEncryptedWriteRecordLayer(EncryptionLevel encryptionLevel) const {
makeEncryptedWriteRecordLayer(
EncryptionLevel encryptionLevel) const override {
return std::make_unique<EncryptedWriteRecordLayer>(encryptionLevel);
}

virtual std::unique_ptr<KeyScheduler> makeKeyScheduler(
CipherSuite cipher) const {
CipherSuite cipher) const override {
auto keyDer = makeKeyDeriver(cipher);
return std::make_unique<KeyScheduler>(std::move(keyDer));
}

virtual std::unique_ptr<KeyDerivation> makeKeyDeriver(
CipherSuite cipher) const = 0;
CipherSuite cipher) const override = 0;

virtual std::unique_ptr<HandshakeContext> makeHandshakeContext(
CipherSuite cipher) const = 0;
CipherSuite cipher) const override = 0;

virtual std::unique_ptr<KeyExchange> makeKeyExchange(
NamedGroup group,
KeyExchangeMode mode) const {
KeyExchangeMode mode) const override {
(void)mode;
switch (group) {
case NamedGroup::secp256r1:
Expand All @@ -90,7 +88,7 @@ class Factory {
}
}

virtual std::unique_ptr<Aead> makeAead(CipherSuite cipher) const {
virtual std::unique_ptr<Aead> makeAead(CipherSuite cipher) const override {
switch (cipher) {
case CipherSuite::TLS_CHACHA20_POLY1305_SHA256:
return OpenSSLEVPCipher::makeCipher<ChaCha20Poly1305>();
Expand All @@ -111,29 +109,31 @@ class Factory {
}
}

virtual Random makeRandom() const {
virtual Random makeRandom() const override {
return RandomGenerator<Random().size()>().generateRandom();
}

virtual uint32_t makeTicketAgeAdd() const {
virtual uint32_t makeTicketAgeAdd() const override {
return RandomNumGenerator<uint32_t>().generateRandom();
}

virtual std::unique_ptr<folly::IOBuf> makeRandomBytes(size_t count) const {
virtual std::unique_ptr<folly::IOBuf> makeRandomBytes(
size_t count) const override {
return RandomBufGenerator(count).generateRandom();
}

virtual std::shared_ptr<PeerCert> makePeerCert(
CertificateEntry certEntry,
bool /*leaf*/) const {
bool /*leaf*/) const override {
return CertUtils::makePeerCert(std::move(certEntry.cert_data));
}

virtual std::shared_ptr<Cert> makeIdentityOnlyCert(std::string ident) const {
virtual std::shared_ptr<Cert> makeIdentityOnlyCert(
std::string ident) const override {
return std::make_shared<IdentityCert>(std::move(ident));
}

virtual std::string getHkdfPrefix() const {
virtual std::string getHkdfPrefix() const override {
return kHkdfLabelPrefix.str();
}
};
Expand Down
84 changes: 84 additions & 0 deletions fizz/protocol/IFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <memory>
#include <string>

#include <fizz/protocol/Types.h>
#include <fizz/record/Types.h>
#include <folly/io/async/AsyncTransportCertificate.h>

namespace fizz {

class Aead;
class EncryptedReadRecordLayer;
class EncryptedWriteRecordLayer;
class HandshakeContext;
class KeyDerivation;
class KeyExchange;
class KeyScheduler;
class PeerCert;
class PlaintextReadRecordLayer;
class PlaintextWriteRecordLayer;

/**
* This class instantiates various objects to facilitate testing.
*/
class IFactory {
public:
enum class KeyExchangeMode { Server, Client };

virtual ~IFactory() = default;

virtual std::unique_ptr<PlaintextReadRecordLayer>
makePlaintextReadRecordLayer() const = 0;

virtual std::unique_ptr<PlaintextWriteRecordLayer>
makePlaintextWriteRecordLayer() const = 0;

virtual std::unique_ptr<EncryptedReadRecordLayer>
makeEncryptedReadRecordLayer(EncryptionLevel encryptionLevel) const = 0;

virtual std::unique_ptr<EncryptedWriteRecordLayer>
makeEncryptedWriteRecordLayer(EncryptionLevel encryptionLevel) const = 0;

virtual std::unique_ptr<KeyScheduler> makeKeyScheduler(
CipherSuite cipher) const = 0;

virtual std::unique_ptr<KeyDerivation> makeKeyDeriver(
CipherSuite cipher) const = 0;

virtual std::unique_ptr<HandshakeContext> makeHandshakeContext(
CipherSuite cipher) const = 0;

virtual std::unique_ptr<KeyExchange> makeKeyExchange(
NamedGroup group,
KeyExchangeMode mode) const = 0;

[[nodiscard]] virtual std::unique_ptr<Aead> makeAead(
CipherSuite cipher) const = 0;

[[nodiscard]] virtual Random makeRandom() const = 0;

[[nodiscard]] virtual uint32_t makeTicketAgeAdd() const = 0;

[[nodiscard]] virtual std::unique_ptr<folly::IOBuf> makeRandomBytes(
size_t count) const = 0;

virtual std::shared_ptr<PeerCert> makePeerCert(
CertificateEntry certEntry,
bool /*leaf*/) const = 0;

[[nodiscard]] virtual std::shared_ptr<folly::AsyncTransportCertificate>
makeIdentityOnlyCert(std::string ident) const = 0;

[[nodiscard]] virtual std::string getHkdfPrefix() const = 0;
};
} // namespace fizz

0 comments on commit 5c92af0

Please sign in to comment.