Skip to content

Commit 3d0253f

Browse files
committed
FIx binary compatibility issues
1 parent 0e7c268 commit 3d0253f

8 files changed

+84
-91
lines changed

crypto/src/tls/DtlsRecordLayer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ public virtual int GetReceiveLimit()
240240
var cipher = m_readEpoch.Cipher;
241241

242242
int plaintextDecodeLimit;
243-
if (cipher is AbstractTlsCipher abstractTlsCipher)
243+
if (cipher is TlsCipherExt tlsCipherExt)
244244
{
245-
plaintextDecodeLimit = abstractTlsCipher.GetPlaintextDecodeLimit(ciphertextLimit);
245+
plaintextDecodeLimit = tlsCipherExt.GetPlaintextDecodeLimit(ciphertextLimit);
246246
}
247247
else
248248
{
@@ -259,9 +259,9 @@ public virtual int GetSendLimit()
259259
int ciphertextLimit = m_transport.GetSendLimit() - m_writeEpoch.RecordHeaderLengthWrite;
260260

261261
int plaintextEncodeLimit;
262-
if (cipher is AbstractTlsCipher abstractTlsCipher)
262+
if (cipher is TlsCipherExt tlsCipherExt)
263263
{
264-
plaintextEncodeLimit = abstractTlsCipher.GetPlaintextEncodeLimit(ciphertextLimit);
264+
plaintextEncodeLimit = tlsCipherExt.GetPlaintextEncodeLimit(ciphertextLimit);
265265
}
266266
else
267267
{

crypto/src/tls/RecordStream.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ internal RecordPreview PreviewRecordHeader(byte[] recordHeader)
153153
var cipher = m_readCipher;
154154

155155
int plaintextDecodeLimit;
156-
if (cipher is AbstractTlsCipher abstractTlsCipher)
156+
if (cipher is TlsCipherExt tlsCipherExt)
157157
{
158-
plaintextDecodeLimit = abstractTlsCipher.GetPlaintextDecodeLimit(length);
158+
plaintextDecodeLimit = tlsCipherExt.GetPlaintextDecodeLimit(length);
159159
}
160160
else
161161
{

crypto/src/tls/crypto/TlsCipher.cs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public interface TlsCipher
2323
/// <summary>Return the maximum size for the plaintext given ciphertextlimit bytes of ciphertext.</summary>
2424
/// <param name="ciphertextLimit">the maximum number of bytes of ciphertext.</param>
2525
/// <returns>the maximum size of the plaintext for ciphertextlimit bytes of input.</returns>
26+
// TODO[api] Remove
2627
int GetPlaintextLimit(int ciphertextLimit);
2728

2829
/// <summary>Encode the passed in plaintext using the current bulk cipher.</summary>

crypto/src/tls/crypto/impl/AbstractTlsCipher.cs

-56
This file was deleted.

crypto/src/tls/crypto/impl/TlsAeadCipher.cs

+17-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl
77
{
88
/// <summary>A generic TLS 1.2 AEAD cipher.</summary>
99
public class TlsAeadCipher
10-
: AbstractTlsCipher
10+
: TlsCipher, TlsCipherExt
1111
{
1212
public const int AEAD_CCM = 1;
1313
public const int AEAD_CHACHA20_POLY1305 = 2;
@@ -145,14 +145,14 @@ public TlsAeadCipher(TlsCryptoParameters cryptoParams, TlsAeadCipherImpl encrypt
145145
decryptCipher.Init(dummyNonce, macSize, null);
146146
}
147147

148-
public override int GetCiphertextDecodeLimit(int plaintextLimit)
148+
public virtual int GetCiphertextDecodeLimit(int plaintextLimit)
149149
{
150150
int innerPlaintextLimit = plaintextLimit + (m_decryptUseInnerPlaintext ? 1 : 0);
151151

152152
return innerPlaintextLimit + m_macSize + m_record_iv_length;
153153
}
154154

155-
public override int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
155+
public virtual int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
156156
{
157157
plaintextLimit = System.Math.Min(plaintextLength, plaintextLimit);
158158

@@ -161,21 +161,27 @@ public override int GetCiphertextEncodeLimit(int plaintextLength, int plaintextL
161161
return innerPlaintextLimit + m_macSize + m_record_iv_length;
162162
}
163163

164-
public override int GetPlaintextDecodeLimit(int ciphertextLimit)
164+
// TODO[api] Remove
165+
public virtual int GetPlaintextLimit(int ciphertextLimit)
166+
{
167+
return GetPlaintextEncodeLimit(ciphertextLimit);
168+
}
169+
170+
public virtual int GetPlaintextDecodeLimit(int ciphertextLimit)
165171
{
166172
int innerPlaintextLimit = ciphertextLimit - m_macSize - m_record_iv_length;
167173

168174
return innerPlaintextLimit - (m_decryptUseInnerPlaintext ? 1 : 0);
169175
}
170176

171-
public override int GetPlaintextEncodeLimit(int ciphertextLimit)
177+
public virtual int GetPlaintextEncodeLimit(int ciphertextLimit)
172178
{
173179
int innerPlaintextLimit = ciphertextLimit - m_macSize - m_record_iv_length;
174180

175181
return innerPlaintextLimit - (m_encryptUseInnerPlaintext ? 1 : 0);
176182
}
177183

178-
public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
184+
public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
179185
int headerAllocation, byte[] plaintext, int plaintextOffset, int plaintextLength)
180186
{
181187
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
@@ -257,7 +263,7 @@ public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, P
257263
}
258264

259265
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
260-
public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
266+
public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
261267
int headerAllocation, ReadOnlySpan<byte> plaintext)
262268
{
263269
byte[] nonce = new byte[m_encryptNonce.Length + m_record_iv_length];
@@ -334,7 +340,7 @@ public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, P
334340
}
335341
#endif
336342

337-
public override TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
343+
public virtual TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
338344
byte[] ciphertext, int ciphertextOffset, int ciphertextLength)
339345
{
340346
if (GetPlaintextDecodeLimit(ciphertextLength) < 0)
@@ -421,17 +427,17 @@ public override TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, P
421427
return new TlsDecodeResult(ciphertext, encryptionOffset, plaintextLength, contentType);
422428
}
423429

424-
public override void RekeyDecoder()
430+
public virtual void RekeyDecoder()
425431
{
426432
RekeyCipher(m_cryptoParams.SecurityParameters, m_decryptCipher, m_decryptNonce, !m_cryptoParams.IsServer);
427433
}
428434

429-
public override void RekeyEncoder()
435+
public virtual void RekeyEncoder()
430436
{
431437
RekeyCipher(m_cryptoParams.SecurityParameters, m_encryptCipher, m_encryptNonce, m_cryptoParams.IsServer);
432438
}
433439

434-
public override bool UsesOpaqueRecordType
440+
public virtual bool UsesOpaqueRecordType
435441
{
436442
get { return m_isTlsV13; }
437443
}

crypto/src/tls/crypto/impl/TlsBlockCipher.cs

+25-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl
88
{
99
/// <summary>A generic TLS 1.0-1.2 block cipher. This can be used for AES or 3DES for example.</summary>
1010
public class TlsBlockCipher
11-
: AbstractTlsCipher
11+
: TlsCipher, TlsCipherExt
1212
{
1313
protected readonly TlsCryptoParameters m_cryptoParams;
1414
protected readonly byte[] m_randomData;
@@ -155,7 +155,7 @@ public TlsBlockCipher(TlsCryptoParameters cryptoParams, TlsBlockCipherImpl encry
155155
}
156156
}
157157

158-
public override int GetCiphertextDecodeLimit(int plaintextLimit)
158+
public virtual int GetCiphertextDecodeLimit(int plaintextLimit)
159159
{
160160
int blockSize = m_decryptCipher.GetBlockSize();
161161
int macSize = m_readMac.Size;
@@ -165,7 +165,7 @@ public override int GetCiphertextDecodeLimit(int plaintextLimit)
165165
return GetCiphertextLength(blockSize, macSize, maxPadding, innerPlaintextLimit);
166166
}
167167

168-
public override int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
168+
public virtual int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
169169
{
170170
plaintextLimit = System.Math.Min(plaintextLength, plaintextLimit);
171171

@@ -177,7 +177,13 @@ public override int GetCiphertextEncodeLimit(int plaintextLength, int plaintextL
177177
return GetCiphertextLength(blockSize, macSize, maxPadding, innerPlaintextLimit);
178178
}
179179

180-
public override int GetPlaintextDecodeLimit(int ciphertextLimit)
180+
// TODO[api] Remove
181+
public virtual int GetPlaintextLimit(int ciphertextLimit)
182+
{
183+
return GetPlaintextEncodeLimit(ciphertextLimit);
184+
}
185+
186+
public virtual int GetPlaintextDecodeLimit(int ciphertextLimit)
181187
{
182188
int blockSize = m_decryptCipher.GetBlockSize();
183189
int macSize = m_readMac.Size;
@@ -187,7 +193,7 @@ public override int GetPlaintextDecodeLimit(int ciphertextLimit)
187193
return innerPlaintextLimit - (m_decryptUseInnerPlaintext ? 1 : 0);
188194
}
189195

190-
public override int GetPlaintextEncodeLimit(int ciphertextLimit)
196+
public virtual int GetPlaintextEncodeLimit(int ciphertextLimit)
191197
{
192198
int blockSize = m_encryptCipher.GetBlockSize();
193199
int macSize = m_writeMac.Size;
@@ -197,7 +203,7 @@ public override int GetPlaintextEncodeLimit(int ciphertextLimit)
197203
return innerPlaintextLimit - (m_encryptUseInnerPlaintext ? 1 : 0);
198204
}
199205

200-
public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
206+
public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
201207
int headerAllocation, byte[] plaintext, int offset, int len)
202208
{
203209
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
@@ -285,7 +291,7 @@ public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, P
285291
}
286292

287293
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
288-
public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
294+
public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
289295
int headerAllocation, ReadOnlySpan<byte> plaintext)
290296
{
291297
int blockSize = m_encryptCipher.GetBlockSize();
@@ -369,7 +375,7 @@ public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, P
369375
}
370376
#endif
371377

372-
public override TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
378+
public virtual TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
373379
byte[] ciphertext, int offset, int len)
374380
{
375381
int blockSize = m_decryptCipher.GetBlockSize();
@@ -475,7 +481,17 @@ public override TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, P
475481
return new TlsDecodeResult(ciphertext, offset, plaintextLength, contentType);
476482
}
477483

478-
public override bool UsesOpaqueRecordType
484+
public virtual void RekeyDecoder()
485+
{
486+
throw new TlsFatalAlert(AlertDescription.internal_error);
487+
}
488+
489+
public virtual void RekeyEncoder()
490+
{
491+
throw new TlsFatalAlert(AlertDescription.internal_error);
492+
}
493+
494+
public virtual bool UsesOpaqueRecordType
479495
{
480496
get { return false; }
481497
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Org.BouncyCastle.Tls.Crypto
2+
{
3+
// TODO[api] Merge into TlsCipher
4+
public interface TlsCipherExt
5+
{
6+
int GetPlaintextDecodeLimit(int ciphertextLimit);
7+
8+
int GetPlaintextEncodeLimit(int ciphertextLimit);
9+
}
10+
}

crypto/src/tls/crypto/impl/TlsNullCipher.cs

+25-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl
77
{
88
/// <summary>The NULL cipher.</summary>
99
public class TlsNullCipher
10-
: AbstractTlsCipher
10+
: TlsCipher, TlsCipherExt
1111
{
1212
protected readonly TlsCryptoParameters m_cryptoParams;
1313
protected readonly TlsSuiteHmac m_readMac, m_writeMac;
@@ -69,14 +69,14 @@ public TlsNullCipher(TlsCryptoParameters cryptoParams, TlsHmac clientMac, TlsHma
6969
}
7070
}
7171

72-
public override int GetCiphertextDecodeLimit(int plaintextLimit)
72+
public virtual int GetCiphertextDecodeLimit(int plaintextLimit)
7373
{
7474
int innerPlaintextLimit = plaintextLimit + (m_decryptUseInnerPlaintext ? 1 : 0);
7575

7676
return innerPlaintextLimit + m_readMac.Size;
7777
}
7878

79-
public override int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
79+
public virtual int GetCiphertextEncodeLimit(int plaintextLength, int plaintextLimit)
8080
{
8181
plaintextLimit = System.Math.Min(plaintextLength, plaintextLimit);
8282

@@ -85,21 +85,27 @@ public override int GetCiphertextEncodeLimit(int plaintextLength, int plaintextL
8585
return innerPlaintextLimit + m_writeMac.Size;
8686
}
8787

88-
public override int GetPlaintextDecodeLimit(int ciphertextLimit)
88+
// TODO[api] Remove
89+
public virtual int GetPlaintextLimit(int ciphertextLimit)
90+
{
91+
return GetPlaintextEncodeLimit(ciphertextLimit);
92+
}
93+
94+
public virtual int GetPlaintextDecodeLimit(int ciphertextLimit)
8995
{
9096
int innerPlaintextLimit = ciphertextLimit - m_readMac.Size;
9197

9298
return innerPlaintextLimit - (m_decryptUseInnerPlaintext ? 1 : 0);
9399
}
94100

95-
public override int GetPlaintextEncodeLimit(int ciphertextLimit)
101+
public virtual int GetPlaintextEncodeLimit(int ciphertextLimit)
96102
{
97103
int innerPlaintextLimit = ciphertextLimit - m_writeMac.Size;
98104

99105
return innerPlaintextLimit - (m_encryptUseInnerPlaintext ? 1 : 0);
100106
}
101107

102-
public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
108+
public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
103109
int headerAllocation, byte[] plaintext, int offset, int len)
104110
{
105111
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
@@ -129,7 +135,7 @@ public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, P
129135
}
130136

131137
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
132-
public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
138+
public virtual TlsEncodeResult EncodePlaintext(long seqNo, short contentType, ProtocolVersion recordVersion,
133139
int headerAllocation, ReadOnlySpan<byte> plaintext)
134140
{
135141
int macSize = m_writeMac.Size;
@@ -155,7 +161,7 @@ public override TlsEncodeResult EncodePlaintext(long seqNo, short contentType, P
155161
}
156162
#endif
157163

158-
public override TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
164+
public virtual TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, ProtocolVersion recordVersion,
159165
byte[] ciphertext, int offset, int len)
160166
{
161167
int macSize = m_readMac.Size;
@@ -196,7 +202,17 @@ public override TlsDecodeResult DecodeCiphertext(long seqNo, short recordType, P
196202
return new TlsDecodeResult(ciphertext, offset, plaintextLength, contentType);
197203
}
198204

199-
public override bool UsesOpaqueRecordType
205+
public virtual void RekeyDecoder()
206+
{
207+
throw new TlsFatalAlert(AlertDescription.internal_error);
208+
}
209+
210+
public virtual void RekeyEncoder()
211+
{
212+
throw new TlsFatalAlert(AlertDescription.internal_error);
213+
}
214+
215+
public virtual bool UsesOpaqueRecordType
200216
{
201217
get { return false; }
202218
}

0 commit comments

Comments
 (0)