summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--crypto/src/asn1/DerEnumerated.cs70
-rw-r--r--crypto/src/asn1/DerInteger.cs82
-rw-r--r--crypto/src/asn1/cms/EnvelopedData.cs2
-rw-r--r--crypto/src/asn1/cms/SignedData.cs2
-rw-r--r--crypto/src/asn1/crmf/CertTemplate.cs2
-rw-r--r--crypto/src/asn1/crmf/PopoPrivKey.cs2
-rw-r--r--crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs2
-rw-r--r--crypto/src/asn1/icao/CscaMasterList.cs2
-rw-r--r--crypto/src/asn1/icao/DataGroupHash.cs2
-rw-r--r--crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs2
-rw-r--r--crypto/src/asn1/misc/CAST5CBCParameters.cs2
-rw-r--r--crypto/src/asn1/ocsp/OCSPResponseStatus.cs2
-rw-r--r--crypto/src/asn1/pkcs/EncryptedData.cs2
-rw-r--r--crypto/src/asn1/tsp/Accuracy.cs74
-rw-r--r--crypto/src/asn1/x509/CRLReason.cs7
-rw-r--r--crypto/src/asn1/x509/TBSCertList.cs2
-rw-r--r--crypto/src/asn1/x509/TBSCertificateStructure.cs2
-rw-r--r--crypto/src/asn1/x509/qualified/Iso4217CurrencyCode.cs4
-rw-r--r--crypto/src/asn1/x509/qualified/TypeOfBiometricData.cs4
-rw-r--r--crypto/src/asn1/x9/X9Curve.cs13
-rw-r--r--crypto/src/cms/CMSSignedData.cs2
-rw-r--r--crypto/src/cms/CMSSignedDataParser.cs2
-rw-r--r--crypto/src/cms/CMSSignedDataStreamGenerator.cs2
-rw-r--r--crypto/src/cms/SignerInformation.cs6
-rw-r--r--crypto/src/crmf/PKMacBuilder.cs4
-rw-r--r--crypto/src/ocsp/BasicOCSPResp.cs2
-rw-r--r--crypto/src/ocsp/OCSPReq.cs2
-rw-r--r--crypto/src/ocsp/OCSPResp.cs2
-rw-r--r--crypto/src/ocsp/RespData.cs2
-rw-r--r--crypto/src/ocsp/RevokedStatus.cs2
-rw-r--r--crypto/src/pkix/Rfc3280CertPathUtilities.cs8
-rw-r--r--crypto/src/tsp/GenTimeAccuracy.cs2
-rw-r--r--crypto/src/tsp/TimeStampRequest.cs2
-rw-r--r--crypto/src/x509/AttributeCertificateHolder.cs4
-rw-r--r--crypto/src/x509/X509V2AttributeCertificate.cs2
35 files changed, 201 insertions, 123 deletions
diff --git a/crypto/src/asn1/DerEnumerated.cs b/crypto/src/asn1/DerEnumerated.cs
index 1344926bd..8654a3bfd 100644
--- a/crypto/src/asn1/DerEnumerated.cs
+++ b/crypto/src/asn1/DerEnumerated.cs
@@ -9,6 +9,7 @@ namespace Org.BouncyCastle.Asn1
         : Asn1Object
     {
         private readonly byte[] bytes;
+        private readonly int start;
 
         /**
          * return an integer from the passed in object
@@ -54,7 +55,17 @@ namespace Org.BouncyCastle.Asn1
             if (val < 0)
                 throw new ArgumentException("enumerated must be non-negative", "val");
 
-            bytes = BigInteger.ValueOf(val).ToByteArray();
+            this.bytes = BigInteger.ValueOf(val).ToByteArray();
+            this.start = 0;
+        }
+
+        public DerEnumerated(long val)
+        {
+            if (val < 0L)
+                throw new ArgumentException("enumerated must be non-negative", "val");
+
+            this.bytes = BigInteger.ValueOf(val).ToByteArray();
+            this.start = 0;
         }
 
         public DerEnumerated(BigInteger val)
@@ -62,7 +73,8 @@ namespace Org.BouncyCastle.Asn1
             if (val.SignValue < 0)
                 throw new ArgumentException("enumerated must be non-negative", "val");
 
-            bytes = val.ToByteArray();
+            this.bytes = val.ToByteArray();
+            this.start = 0;
         }
 
         public DerEnumerated(byte[] bytes)
@@ -73,6 +85,7 @@ namespace Org.BouncyCastle.Asn1
                 throw new ArgumentException("enumerated must be non-negative", "bytes");
 
             this.bytes = Arrays.Clone(bytes);
+            this.start = DerInteger.SignBytesToSkip(bytes);
         }
 
         public BigInteger Value
@@ -80,17 +93,34 @@ namespace Org.BouncyCastle.Asn1
             get { return new BigInteger(bytes); }
         }
 
-        internal override void Encode(
-            DerOutputStream derOut)
+        public bool HasValue(BigInteger x)
+        {
+            return null != x
+                // Fast check to avoid allocation
+                && DerInteger.IntValue(bytes, start, DerInteger.SignExtSigned) == x.IntValue
+                && Value.Equals(x);
+        }
+
+        public int IntValueExact
+        {
+            get
+            {
+                int count = bytes.Length - start;
+                if (count > 4)
+                    throw new ArithmeticException("ASN.1 Enumerated out of int range");
+
+                return DerInteger.IntValue(bytes, start, DerInteger.SignExtSigned);
+            }
+        }
+
+        internal override void Encode(DerOutputStream derOut)
         {
             derOut.WriteEncoded(Asn1Tags.Enumerated, bytes);
         }
 
-        protected override bool Asn1Equals(
-            Asn1Object asn1Object)
+        protected override bool Asn1Equals(Asn1Object asn1Object)
         {
             DerEnumerated other = asn1Object as DerEnumerated;
-
             if (other == null)
                 return false;
 
@@ -106,27 +136,21 @@ namespace Org.BouncyCastle.Asn1
 
         internal static DerEnumerated FromOctetString(byte[] enc)
         {
+            if (enc.Length > 1)
+                return new DerEnumerated(enc);
             if (enc.Length == 0)
-            {
                 throw new ArgumentException("ENUMERATED has zero length", "enc");
-            }
 
-            if (enc.Length == 1)
+            int value = enc[0];
+            if (value >= cache.Length)
+                return new DerEnumerated(enc);
+
+            DerEnumerated possibleMatch = cache[value];
+            if (possibleMatch == null)
             {
-                int value = enc[0];
-                if (value < cache.Length)
-                {
-                    DerEnumerated cached = cache[value];
-                    if (cached != null)
-                    {
-                        return cached;
-                    }
-
-                    return cache[value] = new DerEnumerated(Arrays.Clone(enc));
-                }
+                cache[value] = possibleMatch = new DerEnumerated(enc);
             }
-
-            return new DerEnumerated(Arrays.Clone(enc));
+            return possibleMatch;
         }
     }
 }
diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs
index fec7b9420..3e19a07b6 100644
--- a/crypto/src/asn1/DerInteger.cs
+++ b/crypto/src/asn1/DerInteger.cs
@@ -16,7 +16,11 @@ namespace Org.BouncyCastle.Asn1
             return allowUnsafeValue != null && Platform.EqualsIgnoreCase("true", allowUnsafeValue);
         }
 
+        internal const int SignExtSigned = -1;
+        internal const int SignExtUnsigned = 0xFF;
+
         private readonly byte[] bytes;
+        private readonly int start;
 
         /**
          * return an integer from the passed in object
@@ -62,7 +66,14 @@ namespace Org.BouncyCastle.Asn1
 
 		public DerInteger(int value)
         {
-            bytes = BigInteger.ValueOf(value).ToByteArray();
+            this.bytes = BigInteger.ValueOf(value).ToByteArray();
+            this.start = 0;
+        }
+
+        public DerInteger(long value)
+        {
+            this.bytes = BigInteger.ValueOf(value).ToByteArray();
+            this.start = 0;
         }
 
 		public DerInteger(BigInteger value)
@@ -70,7 +81,8 @@ namespace Org.BouncyCastle.Asn1
             if (value == null)
                 throw new ArgumentNullException("value");
 
-			bytes = value.ToByteArray();
+			this.bytes = value.ToByteArray();
+            this.start = 0;
         }
 
         public DerInteger(byte[] bytes)
@@ -84,6 +96,7 @@ namespace Org.BouncyCastle.Asn1
                 throw new ArgumentException("malformed integer", "bytes");
 
             this.bytes = clone ? Arrays.Clone(bytes) : bytes;
+            this.start = SignBytesToSkip(bytes);
         }
 
         /**
@@ -100,8 +113,39 @@ namespace Org.BouncyCastle.Asn1
             get { return new BigInteger(bytes); }
         }
 
-        internal override void Encode(
-            DerOutputStream derOut)
+        public bool HasValue(BigInteger x)
+        {
+            return null != x
+                // Fast check to avoid allocation
+                && IntValue(bytes, start, SignExtSigned) == x.IntValue
+                && Value.Equals(x);
+        }
+
+        public int IntPositiveValueExact
+        {
+            get
+            {
+                int count = bytes.Length - start;
+                if (count > 4 || (count == 4 && 0 != (bytes[start] & 0x80)))
+                    throw new ArithmeticException("ASN.1 Integer out of positive int range");
+
+                return IntValue(bytes, start, SignExtUnsigned);
+            }
+        }
+
+        public int IntValueExact
+        {
+            get
+            {
+                int count = bytes.Length - start;
+                if (count > 4)
+                    throw new ArithmeticException("ASN.1 Integer out of int range");
+
+                return IntValue(bytes, start, SignExtSigned);
+            }
+        }
+
+        internal override void Encode(DerOutputStream derOut)
         {
             derOut.WriteEncoded(Asn1Tags.Integer, bytes);
         }
@@ -111,15 +155,13 @@ namespace Org.BouncyCastle.Asn1
 			return Arrays.GetHashCode(bytes);
         }
 
-		protected override bool Asn1Equals(
-			Asn1Object asn1Object)
+		protected override bool Asn1Equals(Asn1Object asn1Object)
 		{
 			DerInteger other = asn1Object as DerInteger;
-
 			if (other == null)
 				return false;
 
-			return Arrays.AreEqual(this.bytes, other.bytes);
+            return Arrays.AreEqual(this.bytes, other.bytes);
         }
 
 		public override string ToString()
@@ -127,6 +169,19 @@ namespace Org.BouncyCastle.Asn1
 			return Value.ToString();
 		}
 
+        internal static int IntValue(byte[] bytes, int start, int signExt)
+        {
+            int length = bytes.Length;
+            int pos = System.Math.Max(start, length - 4);
+
+            int val = (sbyte)bytes[pos] & signExt;
+            while (++pos < length)
+            {
+                val = (val << 8) | bytes[pos];
+            }
+            return val;
+        }
+
         /**
          * Apply the correct validation for an INTEGER primitive following the BER rules.
          *
@@ -145,5 +200,16 @@ namespace Org.BouncyCastle.Asn1
                 return (sbyte)bytes[0] == ((sbyte)bytes[1] >> 7) && !AllowUnsafe();
             }
         }
+
+        internal static int SignBytesToSkip(byte[] bytes)
+        {
+            int pos = 0, last = bytes.Length - 1;
+            while (pos < last
+                && (sbyte)bytes[pos] == ((sbyte)bytes[pos + 1] >> 7))
+            {
+                ++pos;
+            }
+            return pos;
+        }
     }
 }
diff --git a/crypto/src/asn1/cms/EnvelopedData.cs b/crypto/src/asn1/cms/EnvelopedData.cs
index 41dae548f..fbde00b28 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.Value.IntValue != 0)
+                if (ri.Version.IntValueExact != 0)
                 {
                     return 2;
                 }
diff --git a/crypto/src/asn1/cms/SignedData.cs b/crypto/src/asn1/cms/SignedData.cs
index 957b81cd8..dfc1e2829 100644
--- a/crypto/src/asn1/cms/SignedData.cs
+++ b/crypto/src/asn1/cms/SignedData.cs
@@ -150,7 +150,7 @@ namespace Org.BouncyCastle.Asn1.Cms
             {
                 SignerInfo s = SignerInfo.GetInstance(obj);
 
-                if (s.Version.Value.IntValue == 3)
+                if (s.Version.IntValueExact == 3)
                 {
                     return true;
                 }
diff --git a/crypto/src/asn1/crmf/CertTemplate.cs b/crypto/src/asn1/crmf/CertTemplate.cs
index 3de9f1d5a..f731ac12e 100644
--- a/crypto/src/asn1/crmf/CertTemplate.cs
+++ b/crypto/src/asn1/crmf/CertTemplate.cs
@@ -77,7 +77,7 @@ namespace Org.BouncyCastle.Asn1.Crmf
 
         public virtual int Version
         {
-            get { return version.Value.IntValue; }
+            get { return version.IntValueExact; }
         }
 
         public virtual DerInteger SerialNumber
diff --git a/crypto/src/asn1/crmf/PopoPrivKey.cs b/crypto/src/asn1/crmf/PopoPrivKey.cs
index 0cedc5127..2b38cb109 100644
--- a/crypto/src/asn1/crmf/PopoPrivKey.cs
+++ b/crypto/src/asn1/crmf/PopoPrivKey.cs
@@ -26,7 +26,7 @@ namespace Org.BouncyCastle.Asn1.Crmf
                 this.obj = DerBitString.GetInstance(obj, false);
                 break;
             case subsequentMessage:
-                this.obj = SubsequentMessage.ValueOf(DerInteger.GetInstance(obj, false).Value.IntValue);
+                this.obj = SubsequentMessage.ValueOf(DerInteger.GetInstance(obj, false).IntValueExact);
                 break;
             case dhMAC:
                 this.obj = DerBitString.GetInstance(obj, false);
diff --git a/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs b/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs
index b347f8dbd..ee6ed8c99 100644
--- a/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs
+++ b/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs
@@ -53,7 +53,7 @@ namespace Org.BouncyCastle.Asn1.CryptoPro
 			if (seq.Count != 4)
 				throw new ArgumentException("Wrong number of elements in sequence", "seq");
 
-			this.keySize = DerInteger.GetInstance(seq[0]).Value.IntValue;
+            this.keySize = DerInteger.GetInstance(seq[0]).IntValueExact;
 			this.p = DerInteger.GetInstance(seq[1]);
             this.q = DerInteger.GetInstance(seq[2]);
 			this.a = DerInteger.GetInstance(seq[3]);
diff --git a/crypto/src/asn1/icao/CscaMasterList.cs b/crypto/src/asn1/icao/CscaMasterList.cs
index 6890d8a2e..d03b3adb4 100644
--- a/crypto/src/asn1/icao/CscaMasterList.cs
+++ b/crypto/src/asn1/icao/CscaMasterList.cs
@@ -62,7 +62,7 @@ namespace Org.BouncyCastle.Asn1.Icao
 
 		public virtual int Version
 		{
-			get { return version.Value.IntValue; }
+            get { return version.IntValueExact; }
 		}
 
 		public X509CertificateStructure[] GetCertStructs()
diff --git a/crypto/src/asn1/icao/DataGroupHash.cs b/crypto/src/asn1/icao/DataGroupHash.cs
index e0d7eee7b..bf83718f3 100644
--- a/crypto/src/asn1/icao/DataGroupHash.cs
+++ b/crypto/src/asn1/icao/DataGroupHash.cs
@@ -70,7 +70,7 @@ namespace Org.BouncyCastle.Asn1.Icao
 
 		public int DataGroupNumber
 		{
-			get { return dataGroupNumber.Value.IntValue; }
+            get { return dataGroupNumber.IntValueExact; }
 		}
 
 		public Asn1OctetString DataGroupHashValue
diff --git a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs
index c4ebb2b72..b82c9373d 100644
--- a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs
+++ b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs
@@ -134,7 +134,7 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 				switch ((Choice) declaration.TagNo)
 				{
 					case Choice.NotYoungerThan:
-						return DerInteger.GetInstance(declaration, false).Value.IntValue;
+                        return DerInteger.GetInstance(declaration, false).IntValueExact;
 					default:
 						return -1;
 				}
diff --git a/crypto/src/asn1/misc/CAST5CBCParameters.cs b/crypto/src/asn1/misc/CAST5CBCParameters.cs
index 51fd6607a..7bd9f1ec6 100644
--- a/crypto/src/asn1/misc/CAST5CBCParameters.cs
+++ b/crypto/src/asn1/misc/CAST5CBCParameters.cs
@@ -52,7 +52,7 @@ namespace Org.BouncyCastle.Asn1.Misc
 
 		public int KeyLength
 		{
-			get { return keyLength.Value.IntValue; }
+            get { return keyLength.IntValueExact; }
 		}
 
 		/**
diff --git a/crypto/src/asn1/ocsp/OCSPResponseStatus.cs b/crypto/src/asn1/ocsp/OCSPResponseStatus.cs
index 653317e33..cf52c73e4 100644
--- a/crypto/src/asn1/ocsp/OCSPResponseStatus.cs
+++ b/crypto/src/asn1/ocsp/OCSPResponseStatus.cs
@@ -34,7 +34,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp
         }
 
 		public OcspResponseStatus(DerEnumerated value)
-			: base(value.Value.IntValue)
+            : base(value.IntValueExact)
         {
         }
     }
diff --git a/crypto/src/asn1/pkcs/EncryptedData.cs b/crypto/src/asn1/pkcs/EncryptedData.cs
index 7e95eb586..6a4c04f25 100644
--- a/crypto/src/asn1/pkcs/EncryptedData.cs
+++ b/crypto/src/asn1/pkcs/EncryptedData.cs
@@ -52,7 +52,7 @@ namespace Org.BouncyCastle.Asn1.Pkcs
 			if (seq.Count != 2)
 				throw new ArgumentException("Wrong number of elements in sequence", "seq");
 
-			int version = ((DerInteger) seq[0]).Value.IntValue;
+            int version = ((DerInteger)seq[0]).IntValueExact;
 			if (version != 0)
             {
                 throw new ArgumentException("sequence not version 0");
diff --git a/crypto/src/asn1/tsp/Accuracy.cs b/crypto/src/asn1/tsp/Accuracy.cs
index 9da8f1d68..31289db99 100644
--- a/crypto/src/asn1/tsp/Accuracy.cs
+++ b/crypto/src/asn1/tsp/Accuracy.cs
@@ -22,26 +22,22 @@ namespace Org.BouncyCastle.Asn1.Tsp
 			DerInteger millis,
 			DerInteger micros)
 		{
-			//Verifications
-			if (millis != null
-				&& (millis.Value.IntValue < MinMillis
-					|| millis.Value.IntValue > MaxMillis))
-			{
-				throw new ArgumentException(
-					"Invalid millis field : not in (1..999)");
-			}
-
-			if (micros != null
-				&& (micros.Value.IntValue < MinMicros
-					|| micros.Value.IntValue > MaxMicros))
-			{
-				throw new ArgumentException(
-					"Invalid micros field : not in (1..999)");
-			}
+            if (null != millis)
+            {
+                int millisValue = millis.IntValueExact;
+                if (millisValue < MinMillis || millisValue > MaxMillis)
+                    throw new ArgumentException("Invalid millis field : not in (1..999)");
+            }
+            if (null != micros)
+            {
+                int microsValue = micros.IntValueExact;
+                if (microsValue < MinMicros || microsValue > MaxMicros)
+                    throw new ArgumentException("Invalid micros field : not in (1..999)");
+            }
 
-			this.seconds = seconds;
-			this.millis = millis;
-			this.micros = micros;
+            this.seconds = seconds;
+            this.millis = millis;
+            this.micros = micros;
 		}
 
 		private Accuracy(
@@ -58,29 +54,23 @@ namespace Org.BouncyCastle.Asn1.Tsp
 				{
                     Asn1TaggedObject extra = (Asn1TaggedObject)seq[i];
 
-					switch (extra.TagNo)
-					{
-						case 0:
-							millis = DerInteger.GetInstance(extra, false);
-							if (millis.Value.IntValue < MinMillis
-								|| millis.Value.IntValue > MaxMillis)
-							{
-								throw new ArgumentException(
-									"Invalid millis field : not in (1..999).");
-							}
-							break;
-						case 1:
-							micros = DerInteger.GetInstance(extra, false);
-							if (micros.Value.IntValue < MinMicros
-								|| micros.Value.IntValue > MaxMicros)
-							{
-								throw new ArgumentException(
-									"Invalid micros field : not in (1..999).");
-							}
-							break;
-						default:
-							throw new ArgumentException("Invalig tag number");
-					}
+                    switch (extra.TagNo)
+                    {
+                    case 0:
+                        millis = DerInteger.GetInstance(extra, false);
+                        int millisValue = millis.IntValueExact;
+                        if (millisValue < MinMillis || millisValue > MaxMillis)
+                            throw new ArgumentException("Invalid millis field : not in (1..999)");
+                        break;
+                    case 1:
+                        micros = DerInteger.GetInstance(extra, false);
+                        int microsValue = micros.IntValueExact;
+                        if (microsValue < MinMicros || microsValue > MaxMicros)
+                            throw new ArgumentException("Invalid micros field : not in (1..999)");
+                        break;
+                    default:
+                        throw new ArgumentException("Invalid tag number");
+                    }
 				}
 			}
 		}
diff --git a/crypto/src/asn1/x509/CRLReason.cs b/crypto/src/asn1/x509/CRLReason.cs
index e8eb53a59..050ceb3bd 100644
--- a/crypto/src/asn1/x509/CRLReason.cs
+++ b/crypto/src/asn1/x509/CRLReason.cs
@@ -45,15 +45,14 @@ namespace Org.BouncyCastle.Asn1.X509
         {
         }
 
-		public CrlReason(
-			DerEnumerated reason)
-			: base(reason.Value.IntValue)
+		public CrlReason(DerEnumerated reason)
+			: base(reason.IntValueExact)
         {
         }
 
 		public override string ToString()
 		{
-			int reason = Value.IntValue;
+			int reason = IntValueExact;
 			string str = (reason < 0 || reason > 10) ? "Invalid" : ReasonString[reason];
 			return "CrlReason: " + str;
 		}    
diff --git a/crypto/src/asn1/x509/TBSCertList.cs b/crypto/src/asn1/x509/TBSCertList.cs
index 2b3646052..a427ba2ba 100644
--- a/crypto/src/asn1/x509/TBSCertList.cs
+++ b/crypto/src/asn1/x509/TBSCertList.cs
@@ -207,7 +207,7 @@ namespace Org.BouncyCastle.Asn1.X509
 
 		public int Version
         {
-            get { return version.Value.IntValue + 1; }
+            get { return version.IntValueExact + 1; }
         }
 
 		public DerInteger VersionNumber
diff --git a/crypto/src/asn1/x509/TBSCertificateStructure.cs b/crypto/src/asn1/x509/TBSCertificateStructure.cs
index 8f091087e..4e3c789e2 100644
--- a/crypto/src/asn1/x509/TBSCertificateStructure.cs
+++ b/crypto/src/asn1/x509/TBSCertificateStructure.cs
@@ -153,7 +153,7 @@ namespace Org.BouncyCastle.Asn1.X509
 
 		public int Version
 		{
-			get { return version.Value.IntValue + 1; }
+            get { return version.IntValueExact + 1; }
 		}
 
 		public DerInteger VersionNumber
diff --git a/crypto/src/asn1/x509/qualified/Iso4217CurrencyCode.cs b/crypto/src/asn1/x509/qualified/Iso4217CurrencyCode.cs
index 9ec88f5ed..a90a33ae2 100644
--- a/crypto/src/asn1/x509/qualified/Iso4217CurrencyCode.cs
+++ b/crypto/src/asn1/x509/qualified/Iso4217CurrencyCode.cs
@@ -35,7 +35,7 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified
 			if (obj is DerInteger)
             {
                 DerInteger numericobj = DerInteger.GetInstance(obj);
-                int numeric = numericobj.Value.IntValue;
+                int numeric = numericobj.IntValueExact;
                 return new Iso4217CurrencyCode(numeric);
             }
 
@@ -74,7 +74,7 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified
 
 		public string Alphabetic { get { return ((DerPrintableString) obj).GetString(); } }
 
-		public int Numeric { get { return ((DerInteger)obj).Value.IntValue; } }
+        public int Numeric { get { return ((DerInteger)obj).IntValueExact; } }
 
 		public override Asn1Object ToAsn1Object()
         {
diff --git a/crypto/src/asn1/x509/qualified/TypeOfBiometricData.cs b/crypto/src/asn1/x509/qualified/TypeOfBiometricData.cs
index 17b7841c3..a4e2e4555 100644
--- a/crypto/src/asn1/x509/qualified/TypeOfBiometricData.cs
+++ b/crypto/src/asn1/x509/qualified/TypeOfBiometricData.cs
@@ -35,7 +35,7 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified
 			if (obj is DerInteger)
             {
                 DerInteger predefinedBiometricTypeObj = DerInteger.GetInstance(obj);
-                int predefinedBiometricType = predefinedBiometricTypeObj.Value.IntValue;
+                int predefinedBiometricType = predefinedBiometricTypeObj.IntValueExact;
 
 				return new TypeOfBiometricData(predefinedBiometricType);
             }
@@ -75,7 +75,7 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified
 
 		public int PredefinedBiometricType
 		{
-			get { return ((DerInteger) obj).Value.IntValue; }
+            get { return ((DerInteger)obj).IntValueExact; }
 		}
 
 		public DerObjectIdentifier BiometricDataOid
diff --git a/crypto/src/asn1/x9/X9Curve.cs b/crypto/src/asn1/x9/X9Curve.cs
index eab94def8..0be9bf935 100644
--- a/crypto/src/asn1/x9/X9Curve.cs
+++ b/crypto/src/asn1/x9/X9Curve.cs
@@ -79,9 +79,8 @@ namespace Org.BouncyCastle.Asn1.X9
             {
                 // Characteristic two field
                 DerSequence parameters = (DerSequence)fieldID.Parameters;
-                int m = ((DerInteger)parameters[0]).Value.IntValue;
-                DerObjectIdentifier representation
-                    = (DerObjectIdentifier)parameters[1];
+                int m = ((DerInteger)parameters[0]).IntValueExact;
+                DerObjectIdentifier representation = (DerObjectIdentifier)parameters[1];
 
                 int k1 = 0;
                 int k2 = 0;
@@ -89,15 +88,15 @@ namespace Org.BouncyCastle.Asn1.X9
                 if (representation.Equals(X9ObjectIdentifiers.TPBasis)) 
                 {
                     // Trinomial basis representation
-                    k1 = ((DerInteger)parameters[2]).Value.IntValue;
+                    k1 = ((DerInteger)parameters[2]).IntValueExact;
                 }
                 else 
                 {
                     // Pentanomial basis representation
                     DerSequence pentanomial = (DerSequence) parameters[2];
-                    k1 = ((DerInteger) pentanomial[0]).Value.IntValue;
-                    k2 = ((DerInteger) pentanomial[1]).Value.IntValue;
-                    k3 = ((DerInteger) pentanomial[2]).Value.IntValue;
+                    k1 = ((DerInteger)pentanomial[0]).IntValueExact;
+                    k2 = ((DerInteger)pentanomial[1]).IntValueExact;
+                    k3 = ((DerInteger)pentanomial[2]).IntValueExact;
                 }
                 BigInteger A = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets());
                 BigInteger B = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets());
diff --git a/crypto/src/cms/CMSSignedData.cs b/crypto/src/cms/CMSSignedData.cs
index 237c1528e..979be6535 100644
--- a/crypto/src/cms/CMSSignedData.cs
+++ b/crypto/src/cms/CMSSignedData.cs
@@ -147,7 +147,7 @@ namespace Org.BouncyCastle.Cms
 		/// <summary>Return the version number for this object.</summary>
 		public int Version
 		{
-			get { return signedData.Version.Value.IntValue; }
+			get { return signedData.Version.IntValueExact; }
 		}
 
 		/**
diff --git a/crypto/src/cms/CMSSignedDataParser.cs b/crypto/src/cms/CMSSignedDataParser.cs
index fb51ab119..c25f0aad0 100644
--- a/crypto/src/cms/CMSSignedDataParser.cs
+++ b/crypto/src/cms/CMSSignedDataParser.cs
@@ -179,7 +179,7 @@ namespace Org.BouncyCastle.Cms
 		 */
 		public int Version
 		{
-			get { return _signedData.Version.Value.IntValue; }
+			get { return _signedData.Version.IntValueExact; }
 		}
 
 		public ISet DigestOids
diff --git a/crypto/src/cms/CMSSignedDataStreamGenerator.cs b/crypto/src/cms/CMSSignedDataStreamGenerator.cs
index 0a3e0c87e..f6bdacb06 100644
--- a/crypto/src/cms/CMSSignedDataStreamGenerator.cs
+++ b/crypto/src/cms/CMSSignedDataStreamGenerator.cs
@@ -732,7 +732,7 @@ namespace Org.BouncyCastle.Cms
 			{
 				SignerInfo s = SignerInfo.GetInstance(si.ToSignerInfo());
 
-				if (s.Version.Value.IntValue == 3)
+				if (s.Version.IntValueExact == 3)
 				{
 					return true;
 				}
diff --git a/crypto/src/cms/SignerInformation.cs b/crypto/src/cms/SignerInformation.cs
index c262806a8..3643c6fe3 100644
--- a/crypto/src/cms/SignerInformation.cs
+++ b/crypto/src/cms/SignerInformation.cs
@@ -129,7 +129,7 @@ namespace Org.BouncyCastle.Cms
 		*/
 		public int Version
 		{
-			get { return info.Version.Value.IntValue; }
+			get { return info.Version.IntValueExact; }
 		}
 
 		public AlgorithmIdentifier DigestAlgorithmID
@@ -349,8 +349,8 @@ namespace Org.BouncyCastle.Cms
 						throw new CmsException("RSASSA-PSS signature parameters specified unknown MGF");
 
                     IDigest pssDigest = DigestUtilities.GetDigest(pss.HashAlgorithm.Algorithm);
-					int saltLength = pss.SaltLength.Value.IntValue;
-					byte trailerField = (byte) pss.TrailerField.Value.IntValue;
+                    int saltLength = pss.SaltLength.IntValueExact;
+                    byte trailerField = (byte)pss.TrailerField.IntValueExact;
 
 					// RFC 4055 3.1
 					// The value MUST be 1, which represents the trailer field with hexadecimal value 0xBC
diff --git a/crypto/src/crmf/PKMacBuilder.cs b/crypto/src/crmf/PKMacBuilder.cs
index b74eb6d18..156936eac 100644
--- a/crypto/src/crmf/PKMacBuilder.cs
+++ b/crypto/src/crmf/PKMacBuilder.cs
@@ -184,7 +184,7 @@ namespace Org.BouncyCastle.Crmf
         /// <returns>this</returns>
         public PKMacBuilder SetParameters(PbmParameter parameters)
         {
-            CheckIterationCountCeiling(parameters.IterationCount.Value.IntValue);
+            CheckIterationCountCeiling(parameters.IterationCount.IntValueExact);
 
             this.parameters = parameters;
 
@@ -257,7 +257,7 @@ namespace Org.BouncyCastle.Crmf
 
             IDigest digest = provider.CreateDigest(parameters.Owf);
 
-            int iter = parameters.IterationCount.Value.IntValue;
+            int iter = parameters.IterationCount.IntValueExact;
 
             digest.BlockUpdate(K, 0, K.Length);
 
diff --git a/crypto/src/ocsp/BasicOCSPResp.cs b/crypto/src/ocsp/BasicOCSPResp.cs
index 63ab8921e..e79d556bd 100644
--- a/crypto/src/ocsp/BasicOCSPResp.cs
+++ b/crypto/src/ocsp/BasicOCSPResp.cs
@@ -54,7 +54,7 @@ namespace Org.BouncyCastle.Ocsp
 
 		public int Version
 		{
-			get { return data.Version.Value.IntValue + 1; }
+            get { return data.Version.IntValueExact + 1; }
 		}
 
 		public RespID ResponderId
diff --git a/crypto/src/ocsp/OCSPReq.cs b/crypto/src/ocsp/OCSPReq.cs
index 0cd95c6d6..5408f068f 100644
--- a/crypto/src/ocsp/OCSPReq.cs
+++ b/crypto/src/ocsp/OCSPReq.cs
@@ -103,7 +103,7 @@ namespace Org.BouncyCastle.Ocsp
 
 		public int Version
 		{
-			get { return req.TbsRequest.Version.Value.IntValue + 1; }
+            get { return req.TbsRequest.Version.IntValueExact + 1; }
 		}
 
 		public GeneralName RequestorName
diff --git a/crypto/src/ocsp/OCSPResp.cs b/crypto/src/ocsp/OCSPResp.cs
index dc99c6a9a..1e65b2f7a 100644
--- a/crypto/src/ocsp/OCSPResp.cs
+++ b/crypto/src/ocsp/OCSPResp.cs
@@ -43,7 +43,7 @@ namespace Org.BouncyCastle.Ocsp
 
 		public int Status
 		{
-			get { return this.resp.ResponseStatus.Value.IntValue; }
+            get { return this.resp.ResponseStatus.IntValueExact; }
 		}
 
 		public object GetResponseObject()
diff --git a/crypto/src/ocsp/RespData.cs b/crypto/src/ocsp/RespData.cs
index 105726ca7..7ec0e4b68 100644
--- a/crypto/src/ocsp/RespData.cs
+++ b/crypto/src/ocsp/RespData.cs
@@ -21,7 +21,7 @@ namespace Org.BouncyCastle.Ocsp
 
 		public int Version
 		{
-			get { return data.Version.Value.IntValue + 1; }
+            get { return data.Version.IntValueExact + 1; }
 		}
 
 		public RespID GetResponderId()
diff --git a/crypto/src/ocsp/RevokedStatus.cs b/crypto/src/ocsp/RevokedStatus.cs
index 6e5ad1b26..edbeb57da 100644
--- a/crypto/src/ocsp/RevokedStatus.cs
+++ b/crypto/src/ocsp/RevokedStatus.cs
@@ -51,7 +51,7 @@ namespace Org.BouncyCastle.Ocsp
 					throw new InvalidOperationException("attempt to get a reason where none is available");
 				}
 
-				return info.RevocationReason.Value.IntValue;
+                return info.RevocationReason.IntValueExact;
 			}
 		}
 	}
diff --git a/crypto/src/pkix/Rfc3280CertPathUtilities.cs b/crypto/src/pkix/Rfc3280CertPathUtilities.cs
index cdb69b7e0..c703194a4 100644
--- a/crypto/src/pkix/Rfc3280CertPathUtilities.cs
+++ b/crypto/src/pkix/Rfc3280CertPathUtilities.cs
@@ -1620,7 +1620,7 @@ namespace Org.BouncyCastle.Pkix
 						Asn1TaggedObject constraint = Asn1TaggedObject.GetInstance(policyConstraints.Current);
 						if (constraint.TagNo == 0)
 						{
-							tmpInt = DerInteger.GetInstance(constraint, false).Value.IntValue;
+                            tmpInt = DerInteger.GetInstance(constraint, false).IntValueExact;
 							if (tmpInt < explicitPolicy)
 							{
 								return tmpInt;
@@ -1675,7 +1675,7 @@ namespace Org.BouncyCastle.Pkix
 						Asn1TaggedObject constraint = Asn1TaggedObject.GetInstance(policyConstraints.Current);
 						if (constraint.TagNo == 1)
 						{
-							tmpInt = DerInteger.GetInstance(constraint, false).Value.IntValue;
+                            tmpInt = DerInteger.GetInstance(constraint, false).IntValueExact;
 							if (tmpInt < policyMapping)
 							{
 								return tmpInt;
@@ -1789,7 +1789,7 @@ namespace Org.BouncyCastle.Pkix
 
 			if (iap != null)
 			{
-				int _inhibitAnyPolicy = iap.Value.IntValue;
+                int _inhibitAnyPolicy = iap.IntValueExact;
 
 				if (_inhibitAnyPolicy < inhibitAnyPolicy)
 					return _inhibitAnyPolicy;
@@ -2062,7 +2062,7 @@ namespace Org.BouncyCastle.Pkix
 						case 0:
 							try
 							{
-								tmpInt = DerInteger.GetInstance(constraint, false).Value.IntValue;
+                                tmpInt = DerInteger.GetInstance(constraint, false).IntValueExact;
 							}
 							catch (Exception e)
 							{
diff --git a/crypto/src/tsp/GenTimeAccuracy.cs b/crypto/src/tsp/GenTimeAccuracy.cs
index 8a2f29989..400bf6f9e 100644
--- a/crypto/src/tsp/GenTimeAccuracy.cs
+++ b/crypto/src/tsp/GenTimeAccuracy.cs
@@ -22,7 +22,7 @@ namespace Org.BouncyCastle.Tsp
 		private int GetTimeComponent(
 			DerInteger time)
 		{
-			return time == null ? 0 : time.Value.IntValue;
+            return time == null ? 0 : time.IntValueExact;
 		}
 
 		public override string ToString()
diff --git a/crypto/src/tsp/TimeStampRequest.cs b/crypto/src/tsp/TimeStampRequest.cs
index 0b41adef7..f5c6a09e6 100644
--- a/crypto/src/tsp/TimeStampRequest.cs
+++ b/crypto/src/tsp/TimeStampRequest.cs
@@ -72,7 +72,7 @@ namespace Org.BouncyCastle.Tsp
 
 		public int Version
 		{
-			get { return req.Version.Value.IntValue; }
+            get { return req.Version.IntValueExact; }
 		}
 
 		public string MessageImprintAlgOid
diff --git a/crypto/src/x509/AttributeCertificateHolder.cs b/crypto/src/x509/AttributeCertificateHolder.cs
index 04460cd59..8ea90c24d 100644
--- a/crypto/src/x509/AttributeCertificateHolder.cs
+++ b/crypto/src/x509/AttributeCertificateHolder.cs
@@ -128,8 +128,8 @@ namespace Org.BouncyCastle.X509
 				ObjectDigestInfo odi = holder.ObjectDigestInfo;
 
 				return odi == null
-					?	-1
-					:	odi.DigestedObjectType.Value.IntValue;
+					?   -1
+                    :   odi.DigestedObjectType.IntValueExact;
 			}
 		}
 
diff --git a/crypto/src/x509/X509V2AttributeCertificate.cs b/crypto/src/x509/X509V2AttributeCertificate.cs
index c41b31239..1ceba101e 100644
--- a/crypto/src/x509/X509V2AttributeCertificate.cs
+++ b/crypto/src/x509/X509V2AttributeCertificate.cs
@@ -67,7 +67,7 @@ namespace Org.BouncyCastle.X509
 
 		public virtual int Version
 		{
-			get { return cert.ACInfo.Version.Value.IntValue + 1; }
+            get { return cert.ACInfo.Version.IntValueExact + 1; }
 		}
 
 		public virtual BigInteger SerialNumber