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/src | |
parent | Rework EdDSA precomputations (diff) | |
download | BouncyCastle.NET-ed25519-7667960b24e63b23518408337c69d5f36c5dfd94.tar.xz |
Custom serialization
Diffstat (limited to 'crypto/src')
-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 |
5 files changed, 57 insertions, 13 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; } } } } |