proxygen
RecordLayer.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 
11 #include <fizz/protocol/Params.h>
12 #include <fizz/record/Types.h>
13 #include <folly/Optional.h>
14 #include <folly/io/IOBufQueue.h>
15 
16 namespace fizz {
17 
18 struct TLSContent {
22 };
23 
25  public:
26  virtual ~ReadRecordLayer() = default;
27 
34 
39  virtual folly::Optional<Param> readEvent(folly::IOBufQueue& socketBuf);
40 
44  virtual bool hasUnparsedHandshakeData() const;
45 
50  virtual EncryptionLevel getEncryptionLevel() const = 0;
51 
52  static folly::Optional<Param> decodeHandshakeMessage(folly::IOBufQueue& buf);
53 
54  private:
55  folly::IOBufQueue unparsedHandshakeData_{
57 };
58 
60  public:
61  virtual ~WriteRecordLayer() = default;
62 
63  virtual TLSContent write(TLSMessage&& msg) const = 0;
64 
67  }
68 
69  TLSContent writeAppData(std::unique_ptr<folly::IOBuf>&& appData) const {
71  }
72 
73  template <typename... Args>
74  TLSContent writeHandshake(Buf&& encodedHandshakeMsg, Args&&... args) const {
75  TLSMessage msg{ContentType::handshake, std::move(encodedHandshakeMsg)};
76  addMessage(msg.fragment, std::forward<Args>(args)...);
77  return write(std::move(msg));
78  }
79 
81  auto realVersion = getRealDraftVersion(version);
82  if (realVersion == ProtocolVersion::tls_1_3_21 ||
83  realVersion == ProtocolVersion::tls_1_3_20) {
84  recordVersion_ = ProtocolVersion::tls_1_0;
85  } else {
86  recordVersion_ = ProtocolVersion::tls_1_2;
87  }
88 
89  if (realVersion == ProtocolVersion::tls_1_3_23 ||
90  realVersion == ProtocolVersion::tls_1_3_22 ||
91  realVersion == ProtocolVersion::tls_1_3_21 ||
92  realVersion == ProtocolVersion::tls_1_3_20) {
93  useAdditionalData_ = false;
94  } else {
95  useAdditionalData_ = true;
96  }
97  }
98 
103  virtual EncryptionLevel getEncryptionLevel() const = 0;
104 
105  protected:
106  mutable ProtocolVersion recordVersion_{ProtocolVersion::tls_1_2};
107  mutable bool useAdditionalData_{true};
108 
109  private:
110  template <typename... Args>
111  static void addMessage(Buf& buf, Buf&& add, Args&&... args) {
112  buf->prependChain(std::move(add));
113  addMessage(buf, std::forward<Args>(args)...);
114  }
115 
116  static void addMessage(Buf& /*buf*/) {}
117 };
118 } // namespace fizz
TLSContent writeHandshake(Buf &&encodedHandshakeMsg, Args &&...args) const
Definition: RecordLayer.h:74
void setProtocolVersion(ProtocolVersion version) const
Definition: RecordLayer.h:80
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
TLSContent writeAlert(Alert &&alert) const
Definition: RecordLayer.h:65
auto add
Definition: BaseTest.cpp:70
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
ProtocolVersion
Definition: Types.h:24
ProtocolVersion version
EncryptionLevel
Definition: Types.h:29
static void addMessage(Buf &buf, Buf &&add, Args &&...args)
Definition: RecordLayer.h:111
static Options cacheChainLength()
Definition: IOBufQueue.h:83
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
EncryptionLevel encryptionLevel
Definition: RecordLayer.h:21
Definition: Actions.h:16
Buf encode(TokenBindingMessage &&message)
Definition: Types.cpp:124
ContentType contentType
Definition: RecordLayer.h:20
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
ProtocolVersion getRealDraftVersion(ProtocolVersion version)
Definition: Types.cpp:16
static void addMessage(Buf &)
Definition: RecordLayer.h:116
TLSContent writeAppData(std::unique_ptr< folly::IOBuf > &&appData) const
Definition: RecordLayer.h:69
ContentType
Definition: Types.h:46