diff options
Diffstat (limited to 'crypto/src/asn1/x509/qualified/SemanticsInformation.cs')
-rw-r--r-- | crypto/src/asn1/x509/qualified/SemanticsInformation.cs | 105 |
1 files changed, 40 insertions, 65 deletions
diff --git a/crypto/src/asn1/x509/qualified/SemanticsInformation.cs b/crypto/src/asn1/x509/qualified/SemanticsInformation.cs index 36a15edec..c82e5f1a1 100644 --- a/crypto/src/asn1/x509/qualified/SemanticsInformation.cs +++ b/crypto/src/asn1/x509/qualified/SemanticsInformation.cs @@ -1,7 +1,5 @@ using System; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.X509.Qualified { /** @@ -21,92 +19,69 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified public class SemanticsInformation : Asn1Encodable { - private readonly DerObjectIdentifier semanticsIdentifier; - private readonly GeneralName[] nameRegistrationAuthorities; - - public static SemanticsInformation GetInstance( - object obj) + public static SemanticsInformation GetInstance(object obj) { - if (obj == null || obj is SemanticsInformation) - { - return (SemanticsInformation) obj; - } + if (obj == null) + return null; + if (obj is SemanticsInformation semanticsInformation) + return semanticsInformation; + return new SemanticsInformation(Asn1Sequence.GetInstance(obj)); + } - if (obj is Asn1Sequence) - { - return new SemanticsInformation(Asn1Sequence.GetInstance(obj)); - } + public static SemanticsInformation GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new SemanticsInformation(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); - throw new ArgumentException("unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj"); - } + public static SemanticsInformation GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new SemanticsInformation(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); - public SemanticsInformation(Asn1Sequence seq) + private readonly DerObjectIdentifier m_semanticsIdentifier; + private readonly GeneralName[] m_nameRegistrationAuthorities; + + public SemanticsInformation(Asn1Sequence seq) { - if (seq.Count < 1) - throw new ArgumentException("no objects in SemanticsInformation"); + int count = seq.Count, pos = 0; - var e = seq.GetEnumerator(); - e.MoveNext(); - var obj = e.Current; - if (obj is DerObjectIdentifier oid) - { - semanticsIdentifier = oid; - if (e.MoveNext()) - { - obj = e.Current; - } - else - { - obj = null; - } - } + // NOTE: At least one of 'semanticsIdentifier' or 'nameRegistrationAuthorities' must be present + if (count < 1 || count > 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - if (obj != null) - { - this.nameRegistrationAuthorities = Asn1Sequence.GetInstance(obj).MapElements(GeneralName.GetInstance); - } - } + m_semanticsIdentifier = Asn1Utilities.ReadOptional(seq, ref pos, DerObjectIdentifier.GetOptional); + m_nameRegistrationAuthorities = Asn1Utilities.ReadOptional(seq, ref pos, Asn1Sequence.GetOptional) + ?.MapElements(GeneralName.GetInstance); - public SemanticsInformation( - DerObjectIdentifier semanticsIdentifier, - GeneralName[] generalNames) - { - this.semanticsIdentifier = semanticsIdentifier; - this.nameRegistrationAuthorities = generalNames; + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); } - public SemanticsInformation( - DerObjectIdentifier semanticsIdentifier) + public SemanticsInformation(DerObjectIdentifier semanticsIdentifier) + : this(semanticsIdentifier, null) { - this.semanticsIdentifier = semanticsIdentifier; } - public SemanticsInformation( - GeneralName[] generalNames) + // TODO[api] Rename parameter + public SemanticsInformation(GeneralName[] generalNames) + : this(null, generalNames) { - this.nameRegistrationAuthorities = generalNames; } - public DerObjectIdentifier SemanticsIdentifier + public SemanticsInformation(DerObjectIdentifier semanticsIdentifier, GeneralName[] generalNames) { - get { return semanticsIdentifier; } - } + if (semanticsIdentifier == null && generalNames == null) + throw new ArgumentException("At least one option must be present"); - public GeneralName[] GetNameRegistrationAuthorities() - { - return nameRegistrationAuthorities; + m_semanticsIdentifier = semanticsIdentifier; + m_nameRegistrationAuthorities = generalNames; } + public DerObjectIdentifier SemanticsIdentifier => m_semanticsIdentifier; + + public GeneralName[] GetNameRegistrationAuthorities() => m_nameRegistrationAuthorities; + public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(2); - v.AddOptional(semanticsIdentifier); - - if (null != nameRegistrationAuthorities) - { - v.Add(new DerSequence(nameRegistrationAuthorities)); - } - + v.AddOptional(m_semanticsIdentifier); + v.AddOptional(DerSequence.FromElementsOptional(m_nameRegistrationAuthorities)); return new DerSequence(v); } } |