From dc8b86c13c769e6de9a5c38e3c67921b034a2394 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 14 Jul 2023 15:07:49 +0700 Subject: Refactoring in Asn1 --- crypto/src/asn1/x509/AttCertIssuer.cs | 49 +++++++++++++++++----------------- crypto/src/asn1/x509/Holder.cs | 27 +++++++------------ crypto/src/asn1/x509/IetfAttrSyntax.cs | 4 +-- crypto/src/asn1/x509/Target.cs | 25 +++++++---------- crypto/src/asn1/x509/X509Extensions.cs | 28 +++++++++---------- 5 files changed, 58 insertions(+), 75 deletions(-) (limited to 'crypto/src/asn1/x509') diff --git a/crypto/src/asn1/x509/AttCertIssuer.cs b/crypto/src/asn1/x509/AttCertIssuer.cs index 407c4ae7a..9d74f631e 100644 --- a/crypto/src/asn1/x509/AttCertIssuer.cs +++ b/crypto/src/asn1/x509/AttCertIssuer.cs @@ -10,34 +10,33 @@ namespace Org.BouncyCastle.Asn1.X509 internal readonly Asn1Encodable obj; internal readonly Asn1Object choiceObj; - public static AttCertIssuer GetInstance( - object obj) - { - if (obj is AttCertIssuer) - { - return (AttCertIssuer)obj; - } - else if (obj is V2Form) - { - return new AttCertIssuer(V2Form.GetInstance(obj)); - } - else if (obj is GeneralNames) - { - return new AttCertIssuer((GeneralNames)obj); - } - else if (obj is Asn1TaggedObject) - { - return new AttCertIssuer(V2Form.GetInstance((Asn1TaggedObject)obj, false)); - } - else if (obj is Asn1Sequence) - { - return new AttCertIssuer(GeneralNames.GetInstance(obj)); - } + public static AttCertIssuer GetInstance(object obj) + { + if (obj is AttCertIssuer attrCertIssuer) + { + return attrCertIssuer; + } + else if (obj is V2Form v2Form) + { + return new AttCertIssuer(v2Form); + } + else if (obj is GeneralNames generalNames) + { + return new AttCertIssuer(generalNames); + } + else if (obj is Asn1TaggedObject taggedObject) + { + return new AttCertIssuer(V2Form.GetInstance(taggedObject, false)); + } + else if (obj is Asn1Sequence) + { + return new AttCertIssuer(GeneralNames.GetInstance(obj)); + } throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); - } + } - public static AttCertIssuer GetInstance( + public static AttCertIssuer GetInstance( Asn1TaggedObject obj, bool isExplicit) { diff --git a/crypto/src/asn1/x509/Holder.cs b/crypto/src/asn1/x509/Holder.cs index 90df75a0f..3da57f70f 100644 --- a/crypto/src/asn1/x509/Holder.cs +++ b/crypto/src/asn1/x509/Holder.cs @@ -42,33 +42,26 @@ namespace Org.BouncyCastle.Asn1.X509 internal readonly ObjectDigestInfo objectDigestInfo; private readonly int version; - public static Holder GetInstance( - object obj) + public static Holder GetInstance(object obj) { - if (obj is Holder) - { - return (Holder) obj; - } + if (obj is Holder holder) + return holder; - if (obj is Asn1Sequence) - { - return new Holder((Asn1Sequence) obj); - } + if (obj is Asn1Sequence sequence) + return new Holder(sequence); - if (obj is Asn1TaggedObject) - { - return new Holder((Asn1TaggedObject) obj); - } + if (obj is Asn1TaggedObject taggedObject) + return new Holder(taggedObject); throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); - } + } - /** + /** * Constructor for a holder for an v1 attribute certificate. * * @param tagObj The ASN.1 tagged holder object. */ - public Holder( + public Holder( Asn1TaggedObject tagObj) { switch (tagObj.TagNo) diff --git a/crypto/src/asn1/x509/IetfAttrSyntax.cs b/crypto/src/asn1/x509/IetfAttrSyntax.cs index 024c8dd68..a59877163 100644 --- a/crypto/src/asn1/x509/IetfAttrSyntax.cs +++ b/crypto/src/asn1/x509/IetfAttrSyntax.cs @@ -25,9 +25,9 @@ namespace Org.BouncyCastle.Asn1.X509 { int i = 0; - if (seq[0] is Asn1TaggedObject) + if (seq[0] is Asn1TaggedObject taggedObject) { - policyAuthority = GeneralNames.GetInstance(((Asn1TaggedObject)seq[0]), false); + policyAuthority = GeneralNames.GetInstance(taggedObject, false); i++; } else if (seq.Count == 2) diff --git a/crypto/src/asn1/x509/Target.cs b/crypto/src/asn1/x509/Target.cs index 7c4f9db7e..eff740143 100644 --- a/crypto/src/asn1/x509/Target.cs +++ b/crypto/src/asn1/x509/Target.cs @@ -32,7 +32,7 @@ namespace Org.BouncyCastle.Asn1.X509 private readonly GeneralName targetName; private readonly GeneralName targetGroup; - /** + /** * Creates an instance of a Target from the given object. *

* obj can be a Target or a {@link Asn1TaggedObject}

@@ -42,29 +42,24 @@ namespace Org.BouncyCastle.Asn1.X509 * @throws ArgumentException if the given object cannot be * interpreted as Target. */ - public static Target GetInstance( - object obj) - { - if (obj is Target) - { - return (Target) obj; - } + public static Target GetInstance(object obj) + { + if (obj is Target target) + return target; - if (obj is Asn1TaggedObject) - { - return new Target((Asn1TaggedObject) obj); - } + if (obj is Asn1TaggedObject taggedObject) + return new Target(taggedObject); throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); - } + } - /** + /** * Constructor from Asn1TaggedObject. * * @param tagObj The tagged object. * @throws ArgumentException if the encoding is wrong. */ - private Target( + private Target( Asn1TaggedObject tagObj) { switch ((Choice) tagObj.TagNo) diff --git a/crypto/src/asn1/x509/X509Extensions.cs b/crypto/src/asn1/x509/X509Extensions.cs index 62291a829..390bf46b7 100644 --- a/crypto/src/asn1/x509/X509Extensions.cs +++ b/crypto/src/asn1/x509/X509Extensions.cs @@ -203,28 +203,24 @@ namespace Org.BouncyCastle.Asn1.X509 return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); } - public static X509Extensions GetInstance( - object obj) + public static X509Extensions GetInstance(object obj) { - if (obj == null || obj is X509Extensions) - { - return (X509Extensions) obj; - } + if (obj == null) + return null; - if (obj is Asn1Sequence) - { - return new X509Extensions((Asn1Sequence) obj); - } + if (obj is X509Extensions x509Extensions) + return x509Extensions; - if (obj is Asn1TaggedObject) - { - return GetInstance(((Asn1TaggedObject) obj).GetObject()); - } + if (obj is Asn1Sequence sequence) + return new X509Extensions(sequence); + + if (obj is Asn1TaggedObject taggedObject) + return GetInstance(taggedObject.GetObject()); throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); - } + } - /** + /** * Constructor from Asn1Sequence. * * the extensions are a list of constructed sequences, either with (Oid, OctetString) or (Oid, Boolean, OctetString) -- cgit 1.4.1