diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-01 16:04:26 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-07-01 16:04:26 +0700 |
commit | 7667960b24e63b23518408337c69d5f36c5dfd94 (patch) | |
tree | 5a4d0d42d37cd2413b3ec7b68eccfb16f3d4a858 /crypto | |
parent | Rework EdDSA precomputations (diff) | |
download | BouncyCastle.NET-ed25519-7667960b24e63b23518408337c69d5f36c5dfd94.tar.xz |
Custom serialization
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/src/math/BigInteger.cs | 15 | ||||
-rw-r--r-- | crypto/src/pkix/PkixCertPathValidatorException.cs | 9 | ||||
-rw-r--r-- | crypto/src/tls/TlsFatalAlert.cs | 16 | ||||
-rw-r--r-- | crypto/src/tls/TlsFatalAlertReceived.cs | 14 | ||||
-rw-r--r-- | crypto/src/tsp/TSPValidationException.cs | 16 | ||||
-rw-r--r-- | crypto/test/src/math/test/BigIntegerTest.cs | 22 |
6 files changed, 78 insertions, 14 deletions
diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs index 98d1fcb1d..50229e2db 100644 --- a/crypto/src/math/BigInteger.cs +++ b/crypto/src/math/BigInteger.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.Runtime.Serialization; using System.Text; using Org.BouncyCastle.Security; @@ -9,9 +10,7 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Math { -#if !PORTABLE [Serializable] -#endif public class BigInteger { // The first few odd primes @@ -239,10 +238,22 @@ namespace Org.BouncyCastle.Math private int[] magnitude; // array of ints with [0] being the most significant private int sign; // -1 means -ve; +1 means +ve; 0 means 0; + + [NonSerialized] private int nBits = -1; // cache BitCount() value + [NonSerialized] private int nBitLength = -1; // cache BitLength() value + [NonSerialized] private int mQuote = 0; // -m^(-1) mod b, b = 2^32 (see Montgomery mult.), 0 when uninitialised + [OnDeserialized] + private void OnDeserialized(StreamingContext context) + { + this.nBits = -1; + this.nBitLength = -1; + this.mQuote = 0; + } + private static int GetByteLength( int nBits) { diff --git a/crypto/src/pkix/PkixCertPathValidatorException.cs b/crypto/src/pkix/PkixCertPathValidatorException.cs index effb60569..3c9dbe349 100644 --- a/crypto/src/pkix/PkixCertPathValidatorException.cs +++ b/crypto/src/pkix/PkixCertPathValidatorException.cs @@ -33,7 +33,7 @@ namespace Org.BouncyCastle.Pkix public class PkixCertPathValidatorException : GeneralSecurityException { - private readonly int m_index = -1; + protected readonly int m_index = -1; public PkixCertPathValidatorException() : base() @@ -69,6 +69,13 @@ namespace Org.BouncyCastle.Pkix protected PkixCertPathValidatorException(SerializationInfo info, StreamingContext context) : base(info, context) { + m_index = info.GetInt32("index"); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("index", m_index); } /// <summary> eturns the index of the certificate in the certification path that caused the exception to be diff --git a/crypto/src/tls/TlsFatalAlert.cs b/crypto/src/tls/TlsFatalAlert.cs index 511fa9fb1..feca84820 100644 --- a/crypto/src/tls/TlsFatalAlert.cs +++ b/crypto/src/tls/TlsFatalAlert.cs @@ -17,10 +17,10 @@ namespace Org.BouncyCastle.Tls return msg; } - protected readonly short m_alertDescription; + protected readonly byte m_alertDescription; public TlsFatalAlert(short alertDescription) - : this(alertDescription, (string)null) + : this(alertDescription, null, null) { } @@ -37,12 +37,22 @@ namespace Org.BouncyCastle.Tls public TlsFatalAlert(short alertDescription, string detailMessage, Exception alertCause) : base(GetMessage(alertDescription, detailMessage), alertCause) { - this.m_alertDescription = alertDescription; + if (!TlsUtilities.IsValidUint8(alertDescription)) + throw new ArgumentOutOfRangeException(nameof(alertDescription)); + + m_alertDescription = (byte)alertDescription; } protected TlsFatalAlert(SerializationInfo info, StreamingContext context) : base(info, context) { + m_alertDescription = info.GetByte("alertDescription"); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("alertDescription", m_alertDescription); } public virtual short AlertDescription diff --git a/crypto/src/tls/TlsFatalAlertReceived.cs b/crypto/src/tls/TlsFatalAlertReceived.cs index adf71d5b9..4e1a62481 100644 --- a/crypto/src/tls/TlsFatalAlertReceived.cs +++ b/crypto/src/tls/TlsFatalAlertReceived.cs @@ -7,17 +7,27 @@ namespace Org.BouncyCastle.Tls public class TlsFatalAlertReceived : TlsException { - protected readonly short m_alertDescription; + protected readonly byte m_alertDescription; public TlsFatalAlertReceived(short alertDescription) : base(Tls.AlertDescription.GetText(alertDescription)) { - this.m_alertDescription = alertDescription; + if (!TlsUtilities.IsValidUint8(alertDescription)) + throw new ArgumentOutOfRangeException(nameof(alertDescription)); + + m_alertDescription = (byte)alertDescription; } protected TlsFatalAlertReceived(SerializationInfo info, StreamingContext context) : base(info, context) { + m_alertDescription = info.GetByte("alertDescription"); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("alertDescription", m_alertDescription); } public virtual short AlertDescription diff --git a/crypto/src/tsp/TSPValidationException.cs b/crypto/src/tsp/TSPValidationException.cs index eb5181f2e..ca693fa38 100644 --- a/crypto/src/tsp/TSPValidationException.cs +++ b/crypto/src/tsp/TSPValidationException.cs @@ -13,29 +13,35 @@ namespace Org.BouncyCastle.Tsp public class TspValidationException : TspException { - private int failureCode; + protected readonly int m_failureCode; public TspValidationException(string message) - : base(message) + : this(message, -1) { - this.failureCode = -1; } public TspValidationException(string message, int failureCode) : base(message) { - this.failureCode = failureCode; + m_failureCode = failureCode; } protected TspValidationException(SerializationInfo info, StreamingContext context) : base(info, context) { + m_failureCode = info.GetInt32("failureCode"); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("failureCode", m_failureCode); } /// <returns>The failure code associated with this exception, if one is set.</returns> public int FailureCode { - get { return failureCode; } + get { return m_failureCode; } } } } diff --git a/crypto/test/src/math/test/BigIntegerTest.cs b/crypto/test/src/math/test/BigIntegerTest.cs index 8196cc6ec..f5973c197 100644 --- a/crypto/test/src/math/test/BigIntegerTest.cs +++ b/crypto/test/src/math/test/BigIntegerTest.cs @@ -1,8 +1,9 @@ using System; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; using NUnit.Framework; -using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; @@ -765,6 +766,25 @@ namespace Org.BouncyCastle.Math.Tests } [Test] + public void TestSerialization() + { + using (var buf = new MemoryStream()) + { + BigInteger x = new BigInteger(128, random); + object y; + + var formatter = new BinaryFormatter(); + formatter.Serialize(buf, x); + + buf.Position = 0; + y = formatter.Deserialize(buf); + + Assert.AreEqual(buf.Length, buf.Position); + Assert.AreEqual(x, y); + } + } + + [Test] public void TestSetBit() { Assert.AreEqual(one, zero.SetBit(0)); |