From 75ef73619db51c97a242b0e9c165838854534694 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 19 May 2022 16:38:12 +0700 Subject: Add previewing of pending output records --- crypto/src/tls/ByteQueue.cs | 8 ++++++++ crypto/src/tls/TlsProtocol.cs | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/crypto/src/tls/ByteQueue.cs b/crypto/src/tls/ByteQueue.cs index e39e797dd..45bec8be4 100644 --- a/crypto/src/tls/ByteQueue.cs +++ b/crypto/src/tls/ByteQueue.cs @@ -149,6 +149,14 @@ namespace Org.BouncyCastle.Tls return TlsUtilities.ReadInt32(m_databuf, m_skipped); } + public int ReadUint16(int skip) + { + if (m_available < skip + 2) + throw new InvalidOperationException("Not enough data to read"); + + return TlsUtilities.ReadUint16(m_databuf, m_skipped + skip); + } + /// Remove some bytes from our data from the beginning. /// How many bytes to remove. public void RemoveData(int i) diff --git a/crypto/src/tls/TlsProtocol.cs b/crypto/src/tls/TlsProtocol.cs index 57b452f60..c30c253e1 100644 --- a/crypto/src/tls/TlsProtocol.cs +++ b/crypto/src/tls/TlsProtocol.cs @@ -1074,6 +1074,28 @@ namespace Org.BouncyCastle.Tls return SafePreviewRecordHeader(recordHeader); } + public virtual int PreviewOutputRecord() + { + if (m_blocking) + throw new InvalidOperationException("Cannot use PreviewOutputRecord() in blocking mode!"); + + ByteQueue buffer = m_outputBuffer.Buffer; + int available = buffer.Available; + if (available < 1) + return 0; + + if (available >= RecordFormat.FragmentOffset) + { + int length = buffer.ReadUint16(RecordFormat.LengthOffset); + int recordSize = RecordFormat.FragmentOffset + length; + + if (available >= recordSize) + return recordSize; + } + + throw new InvalidOperationException("Can only use PreviewOutputRecord() for record-aligned output."); + } + /// public virtual RecordPreview PreviewOutputRecord(int applicationDataSize) { -- cgit 1.4.1