diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-08-09 16:50:54 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-08-09 16:50:54 +0700 |
commit | a66c5d53fe0a051caacffe5daaf5cead5b482c09 (patch) | |
tree | 36e45bce1e4de9d951b4ea17d467a045f2437da9 /crypto/src/asn1/DerEnumerated.cs | |
parent | Add IntValueExact and LongValueExact to BigInteger (diff) | |
download | BouncyCastle.NET-ed25519-a66c5d53fe0a051caacffe5daaf5cead5b482c09.tar.xz |
ASN.1 updates from bc-java
- Integer cannot have empty contents octets - Enumerated values can't be negative
Diffstat (limited to 'crypto/src/asn1/DerEnumerated.cs')
-rw-r--r-- | crypto/src/asn1/DerEnumerated.cs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/crypto/src/asn1/DerEnumerated.cs b/crypto/src/asn1/DerEnumerated.cs index 6690feceb..1344926bd 100644 --- a/crypto/src/asn1/DerEnumerated.cs +++ b/crypto/src/asn1/DerEnumerated.cs @@ -49,30 +49,28 @@ namespace Org.BouncyCastle.Asn1 return FromOctetString(((Asn1OctetString)o).GetOctets()); } - public DerEnumerated( - int val) + public DerEnumerated(int val) { + if (val < 0) + throw new ArgumentException("enumerated must be non-negative", "val"); + bytes = BigInteger.ValueOf(val).ToByteArray(); } - public DerEnumerated( - BigInteger val) + public DerEnumerated(BigInteger val) { + if (val.SignValue < 0) + throw new ArgumentException("enumerated must be non-negative", "val"); + bytes = val.ToByteArray(); } - public DerEnumerated( - byte[] bytes) + public DerEnumerated(byte[] bytes) { - if (bytes.Length > 1) - { - if ((bytes[0] == 0 && (bytes[1] & 0x80) == 0) - || (bytes[0] == (byte)0xff && (bytes[1] & 0x80) != 0)) - { - if (!DerInteger.AllowUnsafe()) - throw new ArgumentException("malformed enumerated"); - } - } + if (DerInteger.IsMalformed(bytes)) + throw new ArgumentException("malformed enumerated", "bytes"); + if (0 != (bytes[0] & 0x80)) + throw new ArgumentException("enumerated must be non-negative", "bytes"); this.bytes = Arrays.Clone(bytes); } |