23 : encryptionLevel_(encryptionLevel) {}
30 if (buf.
empty() || !cursor.canAdvance(kEncryptedHeaderSize)) {
34 std::array<uint8_t, kEncryptedHeaderSize> ad;
36 adCursor.
pull(ad.data(), ad.size());
43 auto length = cursor.readBE<
uint16_t>();
45 throw std::runtime_error(
"received 0 length encrypted record");
47 if (length > kMaxEncryptedRecordSize) {
48 throw std::runtime_error(
"received too long encrypted record");
55 auto alert = decode<Alert>(cursor);
56 throw std::runtime_error(folly::to<std::string>(
57 "received plaintext alert in encrypted record: ",
61 std::unique_ptr<folly::IOBuf> encrypted;
62 cursor.clone(encrypted, length);
67 if (encrypted->
length() == 1 && *encrypted->
data() == 0x01) {
77 throw std::runtime_error(
"max read seq num");
80 auto decryptAttempt =
aead_->tryDecrypt(
85 return decryptAttempt;
90 return aead_->decrypt(
110 while (!*(paddingCursor -= 1).
data()) {
123 throw std::runtime_error(folly::to<std::string>(
124 "received encrypted content type ",
125 static_cast<ContentTypeType>(msg.
type)));
132 throw std::runtime_error(
"received empty fragment");
150 std::unique_ptr<folly::IOBuf> outBuf;
151 std::array<uint8_t, kEncryptedHeaderSize> headerBuf;
153 aead_->setEncryptedBufferHeadroom(kEncryptedHeaderSize);
154 while (!queue.
empty()) {
159 if (!dataBuf->isShared() &&
160 dataBuf->prev()->tailroom() >=
sizeof(
ContentType)) {
163 appender.
writeBE(static_cast<ContentTypeType>(msg.type));
169 appender.
writeBE(static_cast<ContentTypeType>(msg.type));
170 dataBuf->prependChain(
std::move(encryptedFooter));
174 throw std::runtime_error(
"max write seq num");
185 auto ciphertextLength =
186 dataBuf->computeChainDataLength() +
aead_->getCipherOverhead();
189 auto cipherText =
aead_->encrypt(
192 std::unique_ptr<folly::IOBuf> record;
193 if (!cipherText->isShared() &&
196 cipherText->prepend(kEncryptedHeaderSize);
197 memcpy(cipherText->writableData(), header.data(), header.length());
EncryptionLevel getEncryptionLevel() const override
const folly::IOBuf * front() const
folly::StringPiece toString(StateEnum state)
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
size_t chainLength() const
EncryptedReadRecordLayer(EncryptionLevel encryptionLevel)
std::unique_ptr< folly::IOBuf > splitAtMost(size_t n)
static std::unique_ptr< IOBuf > create(std::size_t capacity)
std::unique_ptr< Aead > aead_
folly::Optional< Buf > getDecryptedBuf(folly::IOBufQueue &buf)
static constexpr uint16_t kMaxEncryptedRecordSize
constexpr detail::Map< Move > move
const uint8_t * data() const
std::unique_ptr< folly::IOBuf > move()
void pull(void *buf, size_t len)
bool skipFailedDecryption_
ProtocolVersion recordVersion_
std::size_t length() const
constexpr Range< Iter > range(Iter first, Iter last)
EncryptionLevel encryptionLevel
EncryptionLevel getEncryptionLevel() const override
EncryptedWriteRecordLayer(EncryptionLevel encryptionLevel)
TLSContent write(TLSMessage &&msg) const override
static constexpr size_t kEncryptedHeaderSize
Buf getBufToEncrypt(folly::IOBufQueue &queue) const
static IOBuf wrapBufferAsValue(const void *buf, std::size_t capacity) noexcept
void prependChain(std::unique_ptr< IOBuf > &&iobuf)
typename std::underlying_type< ProtocolVersion >::type ProtocolVersionType
uint16_t desiredMinRecord_
EncryptionLevel encryptionLevel_
std::unique_ptr< folly::IOBuf > Buf
void trimStart(size_t amount)
folly::Optional< TLSMessage > read(folly::IOBufQueue &buf) override
EncryptionLevel encryptionLevel_
std::unique_ptr< Aead > aead_
typename std::underlying_type< ContentType >::type ContentTypeType
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
void trimEnd(size_t amount)
static constexpr uint64_t data[1]
std::unique_ptr< folly::IOBuf > pop_front()