diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-18 13:46:18 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-18 13:46:18 +0700 |
commit | 4fccb8f490eefe7181558b2c3376ab23c33632ee (patch) | |
tree | fe8c1877bb5567cb4823823af77e5c8a38423db8 /crypto/src/asn1/DerBoolean.cs | |
parent | ASN.1: Staged encoding (diff) | |
download | BouncyCastle.NET-ed25519-4fccb8f490eefe7181558b2c3376ab23c33632ee.tar.xz |
ASN.1: Port of bc-java TYPE instances
- we use Meta.Instance here due to syntax restrictions - also reworked some ASN.1 string types
Diffstat (limited to 'crypto/src/asn1/DerBoolean.cs')
-rw-r--r-- | crypto/src/asn1/DerBoolean.cs | 81 |
1 files changed, 49 insertions, 32 deletions
diff --git a/crypto/src/asn1/DerBoolean.cs b/crypto/src/asn1/DerBoolean.cs index 29e2d8bd9..ad578ae80 100644 --- a/crypto/src/asn1/DerBoolean.cs +++ b/crypto/src/asn1/DerBoolean.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using Org.BouncyCastle.Utilities; @@ -7,7 +8,17 @@ namespace Org.BouncyCastle.Asn1 public class DerBoolean : Asn1Object { - private readonly byte value; + internal class Meta : Asn1UniversalType + { + internal static readonly Asn1UniversalType Instance = new Meta(); + + private Meta() : base(typeof(DerBoolean), Asn1Tags.Boolean) {} + + internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString) + { + return CreatePrimitive(octetString.GetOctets()); + } + } public static readonly DerBoolean False = new DerBoolean(false); public static readonly DerBoolean True = new DerBoolean(true); @@ -17,49 +28,57 @@ namespace Org.BouncyCastle.Asn1 * * @exception ArgumentException if the object cannot be converted. */ - public static DerBoolean GetInstance( - object obj) + public static DerBoolean GetInstance(object obj) { if (obj == null || obj is DerBoolean) { - return (DerBoolean) obj; + return (DerBoolean)obj; + } + else if (obj is IAsn1Convertible) + { + Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); + if (asn1Object is DerBoolean) + return (DerBoolean)asn1Object; + } + else if (obj is byte[]) + { + try + { + return (DerBoolean)Meta.Instance.FromByteArray((byte[])obj); + } + catch (IOException e) + { + throw new ArgumentException("failed to construct boolean from byte[]: " + e.Message); + } } throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj)); } - /** - * return a DerBoolean from the passed in bool. - */ - public static DerBoolean GetInstance( - bool value) + public static DerBoolean GetInstance(bool value) { return value ? True : False; } + public static DerBoolean GetInstance(int value) + { + return value != 0 ? True : False; + } + /** * return a Boolean from a tagged object. * - * @param obj the tagged object holding the object we want - * @param explicitly true if the object is meant to be explicitly - * tagged false otherwise. - * @exception ArgumentException if the tagged object cannot - * be converted. + * @param taggedObject the tagged object holding the object we want + * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise. + * @exception ArgumentException if the tagged object cannot be converted. */ - public static DerBoolean GetInstance( - Asn1TaggedObject obj, - bool isExplicit) + public static DerBoolean GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { - Asn1Object o = obj.GetObject(); - - if (isExplicit || o is DerBoolean) - { - return GetInstance(o); - } - - return FromOctetString(((Asn1OctetString)o).GetOctets()); + return (DerBoolean)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); } + private readonly byte value; + public DerBoolean( byte[] val) { @@ -112,16 +131,14 @@ namespace Org.BouncyCastle.Asn1 return IsTrue ? "TRUE" : "FALSE"; } - internal static DerBoolean FromOctetString(byte[] value) + internal static DerBoolean CreatePrimitive(byte[] contents) { - if (value.Length != 1) - { - throw new ArgumentException("BOOLEAN value should have 1 byte in it", "value"); - } + if (contents.Length != 1) + throw new ArgumentException("BOOLEAN value should have 1 byte in it", "contents"); - byte b = value[0]; + byte b = contents[0]; - return b == 0 ? False : b == 0xFF ? True : new DerBoolean(value); + return b == 0 ? False : b == 0xFF ? True : new DerBoolean(contents); } private byte[] GetContents(int encoding) |