proxygen
fizz::PlaintextReadRecordLayer Class Reference

#include <PlaintextRecordLayer.h>

Inheritance diagram for fizz::PlaintextReadRecordLayer:
fizz::ReadRecordLayer fizz::MockPlaintextReadRecordLayer fizz::test::ConcreteReadRecordLayer testing::StrictMock< fizz::test::ConcreteReadRecordLayer > testing::StrictMock< fizz::test::ConcreteReadRecordLayer > testing::StrictMock< fizz::test::ConcreteReadRecordLayer >

Public Member Functions

 ~PlaintextReadRecordLayer () override=default
 
virtual void setSkipEncryptedRecords (bool enabled)
 
folly::Optional< TLSMessageread (folly::IOBufQueue &buf) override
 
folly::Optional< ProtocolVersiongetReceivedRecordVersion () const
 
EncryptionLevel getEncryptionLevel () const override
 
- Public Member Functions inherited from fizz::ReadRecordLayer
virtual ~ReadRecordLayer ()=default
 
virtual folly::Optional< ParamreadEvent (folly::IOBufQueue &socketBuf)
 
virtual bool hasUnparsedHandshakeData () const
 

Private Attributes

bool skipEncryptedRecords_ {false}
 
folly::Optional< ProtocolVersionreceivedRecordVersion_
 

Additional Inherited Members

- Static Public Member Functions inherited from fizz::ReadRecordLayer
static folly::Optional< ParamdecodeHandshakeMessage (folly::IOBufQueue &buf)
 

Detailed Description

Definition at line 15 of file PlaintextRecordLayer.h.

Constructor & Destructor Documentation

fizz::PlaintextReadRecordLayer::~PlaintextReadRecordLayer ( )
overridedefault

Member Function Documentation

EncryptionLevel fizz::PlaintextReadRecordLayer::getEncryptionLevel ( ) const
overridevirtual

Returns the current encryption level of the data that the read record layer can process.

Implements fizz::ReadRecordLayer.

Definition at line 97 of file PlaintextRecordLayer.cpp.

References fizz::Plaintext.

Referenced by getReceivedRecordVersion().

folly::Optional<ProtocolVersion> fizz::PlaintextReadRecordLayer::getReceivedRecordVersion ( ) const
inline

Get the record protocol version of the most recent received record. Should only be used for logging.

Definition at line 32 of file PlaintextRecordLayer.h.

References getEncryptionLevel(), and receivedRecordVersion_.

32  {
34  }
folly::Optional< ProtocolVersion > receivedRecordVersion_
folly::Optional< TLSMessage > fizz::PlaintextReadRecordLayer::read ( folly::IOBufQueue buf)
overridevirtual

Reads a fragment from the record layer. Returns an empty optional if insuficient data available. Throws if data malformed. On success, advances buf the amount read.

Implements fizz::ReadRecordLayer.

Definition at line 23 of file PlaintextRecordLayer.cpp.

References fizz::alert, fizz::application_data, folly::IOBufQueue::chainLength(), fizz::change_cipher_spec, folly::IOBufQueue::empty(), fizz::TLSMessage::fragment, folly::IOBufQueue::front(), fizz::handshake, folly::hexlify(), fizz::illegal_parameter, folly::gen::move, folly::none, receivedRecordVersion_, skipEncryptedRecords_, folly::IOBufQueue::splitAtMost(), folly::IOBufQueue::trimStart(), fizz::TLSMessage::type, and uint16_t.

Referenced by setSkipEncryptedRecords().

24  {
25  while (true) {
26  folly::io::Cursor cursor(buf.front());
27 
28  if (buf.empty() || !cursor.canAdvance(kPlaintextHeaderSize)) {
29  return folly::none;
30  }
31 
32  TLSMessage msg;
33  msg.type = static_cast<ContentType>(cursor.readBE<ContentTypeType>());
34 
36  if (msg.type == ContentType::application_data) {
37  cursor.skip(sizeof(ProtocolVersion));
38  auto length = cursor.readBE<uint16_t>();
39  if (buf.chainLength() < (cursor - buf.front()) + length) {
40  return folly::none;
41  }
42  length +=
43  sizeof(ContentType) + sizeof(ProtocolVersion) + sizeof(uint16_t);
44  buf.trimStart(length);
45  continue;
46  } else if (msg.type != ContentType::change_cipher_spec) {
47  skipEncryptedRecords_ = false;
48  }
49  }
50 
51  switch (msg.type) {
53  case ContentType::alert:
54  break;
56  break;
57  default:
58  throw std::runtime_error(folly::to<std::string>(
59  "received plaintext content type ",
60  static_cast<ContentTypeType>(msg.type),
61  ", header: ",
62  folly::hexlify(buf.splitAtMost(10)->coalesce())));
63  }
64 
66  static_cast<ProtocolVersion>(cursor.readBE<ProtocolVersionType>());
67 
68  auto length = cursor.readBE<uint16_t>();
69  if (length > kMaxPlaintextRecordSize) {
70  throw std::runtime_error("received too long plaintext record");
71  }
72  if (length == 0) {
73  throw std::runtime_error("received empty plaintext record");
74  }
75  if (buf.chainLength() < (cursor - buf.front()) + length) {
76  return folly::none;
77  }
78 
79  cursor.clone(msg.fragment, length);
80 
81  buf.trimStart(cursor - buf.front());
82 
83  if (msg.type == ContentType::change_cipher_spec) {
84  msg.fragment->coalesce();
85  if (msg.fragment->length() == 1 && *msg.fragment->data() == 0x01) {
86  continue;
87  } else {
88  throw FizzException(
89  "received ccs", AlertDescription::illegal_parameter);
90  }
91  }
92 
93  return std::move(msg);
94  }
95 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
size_t chainLength() const
Definition: IOBufQueue.h:492
std::unique_ptr< folly::IOBuf > splitAtMost(size_t n)
Definition: IOBufQueue.h:428
bool empty() const
Definition: IOBufQueue.h:503
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
ProtocolVersion
Definition: Types.h:24
constexpr uint16_t kMaxPlaintextRecordSize
static constexpr size_t kPlaintextHeaderSize
folly::Optional< ProtocolVersion > receivedRecordVersion_
typename std::underlying_type< ProtocolVersion >::type ProtocolVersionType
void trimStart(size_t amount)
Definition: IOBufQueue.cpp:255
bool hexlify(const InputString &input, OutputString &output, bool append_output)
Definition: String-inl.h:596
typename std::underlying_type< ContentType >::type ContentTypeType
ContentType
Definition: Types.h:46
constexpr None none
Definition: Optional.h:87
virtual void fizz::PlaintextReadRecordLayer::setSkipEncryptedRecords ( bool  enabled)
inlinevirtual

Skip over received encrypted records until a plaintext record is received.

Definition at line 22 of file PlaintextRecordLayer.h.

References read(), and skipEncryptedRecords_.

22  {
23  skipEncryptedRecords_ = enabled;
24  }

Member Data Documentation

folly::Optional<ProtocolVersion> fizz::PlaintextReadRecordLayer::receivedRecordVersion_
private

Definition at line 41 of file PlaintextRecordLayer.h.

Referenced by getReceivedRecordVersion(), and read().

bool fizz::PlaintextReadRecordLayer::skipEncryptedRecords_ {false}
private

Definition at line 39 of file PlaintextRecordLayer.h.

Referenced by read(), and setSkipEncryptedRecords().


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