diff options
-rw-r--r-- | crypto/src/asn1/DerEnumerated.cs | 6 | ||||
-rw-r--r-- | crypto/src/asn1/DerInteger.cs | 12 | ||||
-rw-r--r-- | crypto/src/asn1/cms/AuthEnvelopedData.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/cms/AuthEnvelopedDataParser.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/cms/EnvelopedData.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/cms/SignedData.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/icao/LDSSecurityObject.cs | 3 | ||||
-rw-r--r-- | crypto/src/asn1/pkcs/EncryptedData.cs | 6 | ||||
-rw-r--r-- | crypto/src/asn1/pkcs/Pfx.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/x509/AttributeCertificateInfo.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/x509/GeneralSubtree.cs | 2 | ||||
-rw-r--r-- | crypto/src/asn1/x509/TBSCertificateStructure.cs | 11 | ||||
-rw-r--r-- | crypto/src/asn1/x9/X9ECParameters.cs | 2 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/ASN1IntegerTest.cs | 38 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/CertificateTest.cs | 6 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/EnumeratedTest.cs | 12 | ||||
-rw-r--r-- | crypto/test/src/test/CertTest.cs | 21 |
17 files changed, 91 insertions, 40 deletions
diff --git a/crypto/src/asn1/DerEnumerated.cs b/crypto/src/asn1/DerEnumerated.cs index 8654a3bfd..eb0555745 100644 --- a/crypto/src/asn1/DerEnumerated.cs +++ b/crypto/src/asn1/DerEnumerated.cs @@ -93,6 +93,12 @@ namespace Org.BouncyCastle.Asn1 get { return new BigInteger(bytes); } } + public bool HasValue(int x) + { + return (bytes.Length - start) <= 4 + && DerInteger.IntValue(bytes, start, DerInteger.SignExtSigned) == x; + } + public bool HasValue(BigInteger x) { return null != x diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs index 4f7b68d35..67916538b 100644 --- a/crypto/src/asn1/DerInteger.cs +++ b/crypto/src/asn1/DerInteger.cs @@ -113,6 +113,18 @@ namespace Org.BouncyCastle.Asn1 get { return new BigInteger(bytes); } } + public bool HasValue(int x) + { + return (bytes.Length - start) <= 4 + && IntValue(bytes, start, SignExtSigned) == x; + } + + public bool HasValue(long x) + { + return (bytes.Length - start) <= 8 + && LongValue(bytes, start, SignExtSigned) == x; + } + public bool HasValue(BigInteger x) { return null != x diff --git a/crypto/src/asn1/cms/AuthEnvelopedData.cs b/crypto/src/asn1/cms/AuthEnvelopedData.cs index c6e1e3bb5..4ca86e9df 100644 --- a/crypto/src/asn1/cms/AuthEnvelopedData.cs +++ b/crypto/src/asn1/cms/AuthEnvelopedData.cs @@ -57,7 +57,7 @@ namespace Org.BouncyCastle.Asn1.Cms // "It MUST be set to 0." Asn1Object tmp = seq[index++].ToAsn1Object(); version = DerInteger.GetInstance(tmp); - if (version.IntValueExact != 0) + if (!version.HasValue(0)) throw new ArgumentException("AuthEnvelopedData version number must be 0"); tmp = seq[index++].ToAsn1Object(); diff --git a/crypto/src/asn1/cms/AuthEnvelopedDataParser.cs b/crypto/src/asn1/cms/AuthEnvelopedDataParser.cs index 52f0bf720..950040b26 100644 --- a/crypto/src/asn1/cms/AuthEnvelopedDataParser.cs +++ b/crypto/src/asn1/cms/AuthEnvelopedDataParser.cs @@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Asn1.Cms // "It MUST be set to 0." this.version = (DerInteger)seq.ReadObject(); - if (version.IntValueExact != 0) + if (!version.HasValue(0)) throw new Asn1ParsingException("AuthEnvelopedData version number must be 0"); } diff --git a/crypto/src/asn1/cms/EnvelopedData.cs b/crypto/src/asn1/cms/EnvelopedData.cs index fbde00b28..8897fe380 100644 --- a/crypto/src/asn1/cms/EnvelopedData.cs +++ b/crypto/src/asn1/cms/EnvelopedData.cs @@ -154,7 +154,7 @@ namespace Org.BouncyCastle.Asn1.Cms { RecipientInfo ri = RecipientInfo.GetInstance(o); - if (ri.Version.IntValueExact != 0) + if (!ri.Version.HasValue(0)) { return 2; } diff --git a/crypto/src/asn1/cms/SignedData.cs b/crypto/src/asn1/cms/SignedData.cs index 5eba8390d..1e97346e6 100644 --- a/crypto/src/asn1/cms/SignedData.cs +++ b/crypto/src/asn1/cms/SignedData.cs @@ -147,7 +147,7 @@ namespace Org.BouncyCastle.Asn1.Cms { SignerInfo s = SignerInfo.GetInstance(obj); - if (s.Version.IntValueExact == 3) + if (s.Version.HasValue(3)) { return true; } diff --git a/crypto/src/asn1/icao/LDSSecurityObject.cs b/crypto/src/asn1/icao/LDSSecurityObject.cs index cda4aaa37..5d7331a4f 100644 --- a/crypto/src/asn1/icao/LDSSecurityObject.cs +++ b/crypto/src/asn1/icao/LDSSecurityObject.cs @@ -1,7 +1,6 @@ using System; using System.Collections; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Math; @@ -62,7 +61,7 @@ namespace Org.BouncyCastle.Asn1.Icao e.MoveNext(); Asn1Sequence datagroupHashSeq = Asn1Sequence.GetInstance(e.Current); - if (version.Value.Equals(BigInteger.One)) + if (version.HasValue(1)) { e.MoveNext(); versionInfo = LdsVersionInfo.GetInstance(e.Current); diff --git a/crypto/src/asn1/pkcs/EncryptedData.cs b/crypto/src/asn1/pkcs/EncryptedData.cs index 6a4c04f25..cb04f343c 100644 --- a/crypto/src/asn1/pkcs/EncryptedData.cs +++ b/crypto/src/asn1/pkcs/EncryptedData.cs @@ -52,11 +52,9 @@ namespace Org.BouncyCastle.Asn1.Pkcs if (seq.Count != 2) throw new ArgumentException("Wrong number of elements in sequence", "seq"); - int version = ((DerInteger)seq[0]).IntValueExact; - if (version != 0) - { + DerInteger version = (DerInteger)seq[0]; + if (!version.HasValue(0)) throw new ArgumentException("sequence not version 0"); - } this.data = (Asn1Sequence) seq[1]; } diff --git a/crypto/src/asn1/pkcs/Pfx.cs b/crypto/src/asn1/pkcs/Pfx.cs index 3aec8ed0f..e73701342 100644 --- a/crypto/src/asn1/pkcs/Pfx.cs +++ b/crypto/src/asn1/pkcs/Pfx.cs @@ -28,7 +28,7 @@ namespace Org.BouncyCastle.Asn1.Pkcs Asn1Sequence seq) { DerInteger version = DerInteger.GetInstance(seq[0]); - if (version.IntValueExact != 3) + if (!version.HasValue(3)) throw new ArgumentException("wrong version for PFX PDU"); this.contentInfo = ContentInfo.GetInstance(seq[1]); diff --git a/crypto/src/asn1/x509/AttributeCertificateInfo.cs b/crypto/src/asn1/x509/AttributeCertificateInfo.cs index 29abaa6e4..d466bbd14 100644 --- a/crypto/src/asn1/x509/AttributeCertificateInfo.cs +++ b/crypto/src/asn1/x509/AttributeCertificateInfo.cs @@ -149,7 +149,7 @@ namespace Org.BouncyCastle.Asn1.X509 { Asn1EncodableVector v = new Asn1EncodableVector(9); - if (version.IntValueExact != 0) + if (!version.HasValue(0)) { v.Add(version); } diff --git a/crypto/src/asn1/x509/GeneralSubtree.cs b/crypto/src/asn1/x509/GeneralSubtree.cs index a42ec4643..7dbacd229 100644 --- a/crypto/src/asn1/x509/GeneralSubtree.cs +++ b/crypto/src/asn1/x509/GeneralSubtree.cs @@ -173,7 +173,7 @@ namespace Org.BouncyCastle.Asn1.X509 { Asn1EncodableVector v = new Asn1EncodableVector(baseName); - if (minimum != null && minimum.Value.SignValue != 0) + if (minimum != null && !minimum.HasValue(0)) { v.Add(new DerTaggedObject(false, 0, minimum)); } diff --git a/crypto/src/asn1/x509/TBSCertificateStructure.cs b/crypto/src/asn1/x509/TBSCertificateStructure.cs index e600685f0..bd08d8bb8 100644 --- a/crypto/src/asn1/x509/TBSCertificateStructure.cs +++ b/crypto/src/asn1/x509/TBSCertificateStructure.cs @@ -1,6 +1,5 @@ using System; -using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.X509 @@ -82,15 +81,15 @@ namespace Org.BouncyCastle.Asn1.X509 bool isV1 = false; bool isV2 = false; - if (version.Value.Equals(BigInteger.Zero)) + if (version.HasValue(0)) { isV1 = true; } - else if (version.Value.Equals(BigInteger.One)) + else if (version.HasValue(1)) { isV2 = true; } - else if (!version.Value.Equals(BigInteger.Two)) + else if (!version.HasValue(2)) { throw new ArgumentException("version number not recognised"); } @@ -220,7 +219,7 @@ namespace Org.BouncyCastle.Asn1.X509 Asn1EncodableVector v = new Asn1EncodableVector(); // DEFAULT Zero - if (!version.HasValue(BigIntegers.Zero)) + if (!version.HasValue(0)) { v.Add(new DerTaggedObject(true, 0, version)); } @@ -238,7 +237,7 @@ namespace Org.BouncyCastle.Asn1.X509 } else { - v.Add(new DerSequence()); + v.Add(DerSequence.Empty); } v.Add(subjectPublicKeyInfo); diff --git a/crypto/src/asn1/x9/X9ECParameters.cs b/crypto/src/asn1/x9/X9ECParameters.cs index e1b29ca13..aa84063b8 100644 --- a/crypto/src/asn1/x9/X9ECParameters.cs +++ b/crypto/src/asn1/x9/X9ECParameters.cs @@ -35,7 +35,7 @@ namespace Org.BouncyCastle.Asn1.X9 Asn1Sequence seq) { if (!(seq[0] is DerInteger) - || !((DerInteger)seq[0]).Value.Equals(BigInteger.One)) + || !((DerInteger)seq[0]).HasValue(1)) { throw new ArgumentException("bad version in X9ECParameters"); } diff --git a/crypto/test/src/asn1/test/ASN1IntegerTest.cs b/crypto/test/src/asn1/test/ASN1IntegerTest.cs index 689a60ac5..3d0c6fcd0 100644 --- a/crypto/test/src/asn1/test/ASN1IntegerTest.cs +++ b/crypto/test/src/asn1/test/ASN1IntegerTest.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using Org.BouncyCastle.Math; +using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; @@ -128,7 +129,7 @@ namespace Org.BouncyCastle.Asn1.Tests // byte[] rawInt = Hex.Decode("10"); DerInteger i = new DerInteger(rawInt); - IsEquals(i.Value.IntValue, 16); + CheckIntValue(i, 16); // // With property set. @@ -137,7 +138,7 @@ namespace Org.BouncyCastle.Asn1.Tests rawInt = Hex.Decode("10"); i = new DerInteger(rawInt); - IsEquals(i.Value.IntValue, 16); + CheckIntValue(i, 16); } public void DoTestValidEncodingMultiByte() @@ -149,7 +150,7 @@ namespace Org.BouncyCastle.Asn1.Tests // byte[] rawInt = Hex.Decode("10FF"); DerInteger i = new DerInteger(rawInt); - IsEquals(i.Value.IntValue, 4351); + CheckIntValue(i, 4351); // // With property set. @@ -158,7 +159,7 @@ namespace Org.BouncyCastle.Asn1.Tests rawInt = Hex.Decode("10FF"); i = new DerInteger(rawInt); - IsEquals(i.Value.IntValue, 4351); + CheckIntValue(i, 4351); } public void DoTestInvalidEncoding_00() @@ -203,8 +204,7 @@ namespace Org.BouncyCastle.Asn1.Tests try { byte[] rawInt = Hex.Decode("0000000010FF"); - DerInteger i = new DerInteger(rawInt); - IsEquals(i.Value.IntValue, 4351); + new DerInteger(rawInt); Fail("Expecting illegal argument exception."); } catch (ArgumentException e) @@ -283,7 +283,7 @@ namespace Org.BouncyCastle.Asn1.Tests SetAllowUnsafeProperty(true); byte[] rawInt = Hex.Decode("00000010FF000000"); DerInteger i = new DerInteger(rawInt); - IsEquals(72997666816L, i.Value.LongValue); + CheckLongValue(i, 72997666816L); } public void DoTestLooseValidEncoding_FF_32BAligned() @@ -294,7 +294,7 @@ namespace Org.BouncyCastle.Asn1.Tests SetAllowUnsafeProperty(true); byte[] rawInt = Hex.Decode("FFFFFF10FF000000"); DerInteger i = new DerInteger(rawInt); - IsEquals(-1026513960960L, i.Value.LongValue); + CheckLongValue(i, -1026513960960L); } public void DoTestLooseValidEncoding_FF_32BAligned_1not0() @@ -305,7 +305,7 @@ namespace Org.BouncyCastle.Asn1.Tests SetAllowUnsafeProperty(true); byte[] rawInt = Hex.Decode("FFFEFF10FF000000"); DerInteger i = new DerInteger(rawInt); - IsEquals(-282501490671616L, i.Value.LongValue); + CheckLongValue(i, -282501490671616L); } public void DoTestLooseValidEncoding_FF_32BAligned_2not0() @@ -316,7 +316,7 @@ namespace Org.BouncyCastle.Asn1.Tests SetAllowUnsafeProperty(true); byte[] rawInt = Hex.Decode("FFFFFE10FF000000"); DerInteger i = new DerInteger(rawInt); - IsEquals(-2126025588736L, i.Value.LongValue); + CheckLongValue(i, -2126025588736L); } public void DoTestOversizedEncoding() @@ -358,6 +358,24 @@ namespace Org.BouncyCastle.Asn1.Tests #endif } + private void CheckIntValue(DerInteger i, int n) + { + BigInteger val = i.Value; + IsEquals(val.IntValue, n); + IsEquals(val.IntValueExact, n); + IsEquals(i.IntValueExact, n); + IsTrue(i.HasValue(n)); + } + + private void CheckLongValue(DerInteger i, long n) + { + BigInteger val = i.Value; + IsEquals(val.LongValue, n); + IsEquals(val.LongValueExact, n); + IsEquals(i.LongValueExact, n); + IsTrue(i.HasValue(n)); + } + public static void Main( string[] args) { diff --git a/crypto/test/src/asn1/test/CertificateTest.cs b/crypto/test/src/asn1/test/CertificateTest.cs index c2593fad2..62ae6799c 100644 --- a/crypto/test/src/asn1/test/CertificateTest.cs +++ b/crypto/test/src/asn1/test/CertificateTest.cs @@ -360,8 +360,8 @@ namespace Org.BouncyCastle.Asn1.Tests AttributeCertificateInfo acInfo = obj.ACInfo; // Version - if (!(acInfo.Version.Equals(new DerInteger(1))) - && (!(acInfo.Version.Equals(new DerInteger(2))))) + DerInteger version = acInfo.Version; + if (!version.HasValue(1) && !version.HasValue(2)) { Fail("failed AC Version test for id " + id); } @@ -427,7 +427,7 @@ namespace Org.BouncyCastle.Asn1.Tests AttributeCertificateInfo acInfo = obj.ACInfo; // Version - if (acInfo.Version.IntValueExact != 0) + if (!acInfo.Version.HasValue(0)) { Fail("failed AC Version test for id " + id); } diff --git a/crypto/test/src/asn1/test/EnumeratedTest.cs b/crypto/test/src/asn1/test/EnumeratedTest.cs index 29e90326b..31ff133be 100644 --- a/crypto/test/src/asn1/test/EnumeratedTest.cs +++ b/crypto/test/src/asn1/test/EnumeratedTest.cs @@ -49,7 +49,8 @@ namespace Org.BouncyCastle.Asn1.Tests Assert.IsNotNull(enumerated, "ENUMERATED expected"); - Assert.AreEqual(1, enumerated.Value.IntValue, "Unexpected ENUMERATED value"); + Assert.AreEqual(1, enumerated.IntValueExact, "Unexpected ENUMERATED value"); + Assert.IsTrue(enumerated.HasValue(1), "Unexpected ENUMERATED value"); DerBoolean boolean = sequence[1] as DerBoolean; @@ -76,13 +77,15 @@ namespace Org.BouncyCastle.Asn1.Tests Assert.IsNotNull(enumerated1, "ENUMERATED expected"); - Assert.AreEqual(257, enumerated1.Value.IntValue, "Unexpected ENUMERATED value"); + Assert.AreEqual(257, enumerated1.IntValueExact, "Unexpected ENUMERATED value"); + Assert.IsTrue(enumerated1.HasValue(257), "Unexpected ENUMERATED value"); DerEnumerated enumerated2 = sequence[1] as DerEnumerated; Assert.IsNotNull(enumerated2, "ENUMERATED expected"); - Assert.AreEqual(514, enumerated2.Value.IntValue, "Unexpected ENUMERATED value"); + Assert.AreEqual(514, enumerated2.IntValueExact, "Unexpected ENUMERATED value"); + Assert.IsTrue(enumerated2.HasValue(514), "Unexpected ENUMERATED value"); } /// <summary> @@ -103,7 +106,8 @@ namespace Org.BouncyCastle.Asn1.Tests Assert.IsNotNull(enumerated, "ENUMERATED expected"); - Assert.AreEqual(65793, enumerated.Value.IntValue, "Unexpected ENUMERATED value"); + Assert.AreEqual(65793, enumerated.IntValueExact, "Unexpected ENUMERATED value"); + Assert.IsTrue(enumerated.HasValue(65793), "Unexpected ENUMERATED value"); DerObjectIdentifier objectId = sequence[1] as DerObjectIdentifier; diff --git a/crypto/test/src/test/CertTest.cs b/crypto/test/src/test/CertTest.cs index 46276a75b..d83b67f8c 100644 --- a/crypto/test/src/test/CertTest.cs +++ b/crypto/test/src/test/CertTest.cs @@ -1719,7 +1719,7 @@ namespace Org.BouncyCastle.Tests { DerEnumerated reasonCode = (DerEnumerated)X509ExtensionUtilities.FromExtensionValue(ext); - if (reasonCode.Value.IntValue != CrlReason.PrivilegeWithdrawn) + if (!reasonCode.HasValue(CrlReason.PrivilegeWithdrawn)) { Fail("CRL entry reasonCode wrong"); } @@ -1807,7 +1807,7 @@ namespace Org.BouncyCastle.Tests { DerEnumerated reasonCode = (DerEnumerated)X509ExtensionUtilities.FromExtensionValue(ext); - if (reasonCode.Value.IntValue != CrlReason.PrivilegeWithdrawn) + if (!reasonCode.HasValue(CrlReason.PrivilegeWithdrawn)) { Fail("CRL entry reasonCode wrong"); } @@ -1895,7 +1895,7 @@ namespace Org.BouncyCastle.Tests { DerEnumerated reasonCode = (DerEnumerated)X509ExtensionUtilities.FromExtensionValue(ext); - if (reasonCode.Value.IntValue != CrlReason.PrivilegeWithdrawn) + if (!reasonCode.HasValue(CrlReason.PrivilegeWithdrawn)) { Fail("CRL entry reasonCode wrong"); } @@ -1934,6 +1934,21 @@ namespace Org.BouncyCastle.Tests if (crlEnt.SerialNumber.IntValue == 1) { oneFound = true; + Asn1OctetString extn = entry.GetExtensionValue(X509Extensions.ReasonCode); + + if (extn != null) + { + DerEnumerated reasonCode = (DerEnumerated)X509ExtensionUtilities.FromExtensionValue(extn); + + if (!reasonCode.HasValue(CrlReason.PrivilegeWithdrawn)) + { + Fail("CRL entry reasonCode wrong"); + } + } + else + { + Fail("CRL entry reasonCode not found"); + } } else if (crlEnt.SerialNumber.IntValue == 2) { |