proxygen
EncryptedRecordLayer.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 
12 
13 #include <fizz/crypto/aead/Aead.h>
14 
15 namespace fizz {
16 
17 constexpr uint16_t kMaxPlaintextRecordSize = 0x4000; // 16k
19 
21  public:
22  ~EncryptedReadRecordLayer() override = default;
23 
24  explicit EncryptedReadRecordLayer(EncryptionLevel encryptionLevel);
25 
27 
28  virtual void setAead(
29  folly::ByteRange /* baseSecret */,
30  std::unique_ptr<Aead> aead) {
31  if (seqNum_ != 0) {
32  throw std::runtime_error("aead set after read");
33  }
34  aead_ = std::move(aead);
35  }
36 
37  virtual void setSkipFailedDecryption(bool enabled) {
38  skipFailedDecryption_ = enabled;
39  }
40 
42  auto realVersion = getRealDraftVersion(version);
43  if (realVersion == ProtocolVersion::tls_1_3_23 ||
44  realVersion == ProtocolVersion::tls_1_3_22 ||
45  realVersion == ProtocolVersion::tls_1_3_21 ||
46  realVersion == ProtocolVersion::tls_1_3_20) {
47  useAdditionalData_ = false;
48  } else {
49  useAdditionalData_ = true;
50  }
51  }
52 
53  EncryptionLevel getEncryptionLevel() const override;
54 
55  private:
57 
59  std::unique_ptr<Aead> aead_;
60  bool skipFailedDecryption_{false};
61 
62  bool useAdditionalData_{true};
63 
64  mutable uint64_t seqNum_{0};
65 };
66 
68  public:
69  ~EncryptedWriteRecordLayer() override = default;
70 
71  explicit EncryptedWriteRecordLayer(EncryptionLevel encryptionLevel);
72 
73  TLSContent write(TLSMessage&& msg) const override;
74 
75  virtual void setAead(
76  folly::ByteRange /* baseSecret */,
77  std::unique_ptr<Aead> aead) {
78  if (seqNum_ != 0) {
79  throw std::runtime_error("aead set after write");
80  }
81  aead_ = std::move(aead);
82  }
83 
85  CHECK_GT(size, 0);
86  DCHECK_LE(size, kMaxPlaintextRecordSize);
87  maxRecord_ = size;
88  }
89 
91  CHECK_GT(size, 0);
92  DCHECK_LE(size, kMaxPlaintextRecordSize);
93  desiredMinRecord_ = size;
94  }
95 
96  EncryptionLevel getEncryptionLevel() const override;
97 
98  private:
99  Buf getBufToEncrypt(folly::IOBufQueue& queue) const;
100 
101  std::unique_ptr<Aead> aead_;
102 
103  uint16_t maxRecord_{kMaxPlaintextRecordSize};
104  uint16_t desiredMinRecord_{kMinSuggestedRecordSize};
105 
106  mutable uint64_t seqNum_{0};
108 };
109 } // namespace fizz
EncryptedReadRecordLayer(EncryptionLevel encryptionLevel)
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
folly::Optional< Buf > getDecryptedBuf(folly::IOBufQueue &buf)
~EncryptedReadRecordLayer() override=default
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
virtual void setSkipFailedDecryption(bool enabled)
ProtocolVersion
Definition: Types.h:24
ProtocolVersion version
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
EncryptionLevel
Definition: Types.h:29
virtual void setAead(folly::ByteRange, std::unique_ptr< Aead > aead)
constexpr uint16_t kMaxPlaintextRecordSize
Definition: Actions.h:16
EncryptionLevel getEncryptionLevel() const override
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
ProtocolVersion getRealDraftVersion(ProtocolVersion version)
Definition: Types.cpp:16
void setProtocolVersion(ProtocolVersion version)
folly::Optional< TLSMessage > read(folly::IOBufQueue &buf) override
constexpr uint16_t kMinSuggestedRecordSize
virtual void setAead(folly::ByteRange, std::unique_ptr< Aead > aead)