From f82c115316bcbf98fae46cb6fd90a8e5a57fdef2 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 30 Jan 2023 18:54:08 +0700 Subject: Misc. cleanup after bc-fips-csharp updates --- crypto/src/asn1/x509/DistributionPoint.cs | 92 +++++++++--------------- crypto/src/asn1/x509/DistributionPointName.cs | 82 ++++++++------------- crypto/src/asn1/x509/GeneralNames.cs | 45 +++++------- crypto/src/asn1/x509/IssuingDistributionPoint.cs | 70 ++++++++---------- 4 files changed, 110 insertions(+), 179 deletions(-) (limited to 'crypto/src/asn1/x509') diff --git a/crypto/src/asn1/x509/DistributionPoint.cs b/crypto/src/asn1/x509/DistributionPoint.cs index f35586016..077c9321e 100644 --- a/crypto/src/asn1/x509/DistributionPoint.cs +++ b/crypto/src/asn1/x509/DistributionPoint.cs @@ -1,8 +1,5 @@ -using System; using System.Text; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.X509 { /** @@ -18,35 +15,25 @@ namespace Org.BouncyCastle.Asn1.X509 public class DistributionPoint : Asn1Encodable { - internal readonly DistributionPointName distributionPoint; - internal readonly ReasonFlags reasons; - internal readonly GeneralNames cRLIssuer; - - public static DistributionPoint GetInstance( - Asn1TaggedObject obj, - bool explicitly) + public static DistributionPoint GetInstance(object obj) { - return GetInstance(Asn1Sequence.GetInstance(obj, explicitly)); + if (obj == null) + return null; + if (obj is DistributionPoint distributionPoint) + return distributionPoint; + return new DistributionPoint(Asn1Sequence.GetInstance(obj)); } - public static DistributionPoint GetInstance( - object obj) + public static DistributionPoint GetInstance(Asn1TaggedObject obj, bool explicitly) { - if(obj == null || obj is DistributionPoint) - { - return (DistributionPoint) obj; - } - - if(obj is Asn1Sequence) - { - return new DistributionPoint((Asn1Sequence) obj); - } - - throw new ArgumentException("Invalid DistributionPoint: " + Platform.GetTypeName(obj)); + return GetInstance(Asn1Sequence.GetInstance(obj, explicitly)); } - private DistributionPoint( - Asn1Sequence seq) + private readonly DistributionPointName m_distributionPoint; + private readonly ReasonFlags m_reasons; + private readonly GeneralNames m_crlIssuer; + + private DistributionPoint(Asn1Sequence seq) { for (int i = 0; i != seq.Count; i++) { @@ -55,52 +42,41 @@ namespace Org.BouncyCastle.Asn1.X509 switch (t.TagNo) { case 0: - distributionPoint = DistributionPointName.GetInstance(t, true); + m_distributionPoint = DistributionPointName.GetInstance(t, true); break; case 1: - reasons = new ReasonFlags(DerBitString.GetInstance(t, false)); + m_reasons = new ReasonFlags(DerBitString.GetInstance(t, false)); break; case 2: - cRLIssuer = GeneralNames.GetInstance(t, false); + m_crlIssuer = GeneralNames.GetInstance(t, false); break; } } } - public DistributionPoint( - DistributionPointName distributionPointName, - ReasonFlags reasons, - GeneralNames crlIssuer) + public DistributionPoint(DistributionPointName distributionPointName, ReasonFlags reasons, + GeneralNames crlIssuer) { - this.distributionPoint = distributionPointName; - this.reasons = reasons; - this.cRLIssuer = crlIssuer; + m_distributionPoint = distributionPointName; + m_reasons = reasons; + m_crlIssuer = crlIssuer; } - public DistributionPointName DistributionPointName - { - get { return distributionPoint; } - } + public DistributionPointName DistributionPointName => m_distributionPoint; - public ReasonFlags Reasons - { - get { return reasons; } - } + public ReasonFlags Reasons => m_reasons; - public GeneralNames CrlIssuer - { - get { return cRLIssuer; } - } + public GeneralNames CrlIssuer => m_crlIssuer; public override Asn1Object ToAsn1Object() { - Asn1EncodableVector v = new Asn1EncodableVector(); + Asn1EncodableVector v = new Asn1EncodableVector(3); // As this is a CHOICE it must be explicitly tagged - v.AddOptionalTagged(true, 0, distributionPoint); + v.AddOptionalTagged(true, 0, m_distributionPoint); - v.AddOptionalTagged(false, 1, reasons); - v.AddOptionalTagged(false, 2, cRLIssuer); + v.AddOptionalTagged(false, 1, m_reasons); + v.AddOptionalTagged(false, 2, m_crlIssuer); return new DerSequence(v); } @@ -108,17 +84,17 @@ namespace Org.BouncyCastle.Asn1.X509 { StringBuilder buf = new StringBuilder(); buf.AppendLine("DistributionPoint: ["); - if (distributionPoint != null) + if (m_distributionPoint != null) { - AppendObject(buf, "distributionPoint", distributionPoint.ToString()); + AppendObject(buf, "distributionPoint", m_distributionPoint.ToString()); } - if (reasons != null) + if (m_reasons != null) { - AppendObject(buf, "reasons", reasons.ToString()); + AppendObject(buf, "reasons", m_reasons.ToString()); } - if (cRLIssuer != null) + if (m_crlIssuer != null) { - AppendObject(buf, "cRLIssuer", cRLIssuer.ToString()); + AppendObject(buf, "cRLIssuer", m_crlIssuer.ToString()); } buf.AppendLine("]"); return buf.ToString(); diff --git a/crypto/src/asn1/x509/DistributionPointName.cs b/crypto/src/asn1/x509/DistributionPointName.cs index bca54fa06..bdb7219be 100644 --- a/crypto/src/asn1/x509/DistributionPointName.cs +++ b/crypto/src/asn1/x509/DistributionPointName.cs @@ -1,8 +1,5 @@ -using System; using System.Text; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.X509 { /** @@ -17,90 +14,71 @@ namespace Org.BouncyCastle.Asn1.X509 public class DistributionPointName : Asn1Encodable, IAsn1Choice { - internal readonly Asn1Encodable name; - internal readonly int type; - - public const int FullName = 0; - public const int NameRelativeToCrlIssuer = 1; + public const int FullName = 0; + public const int NameRelativeToCrlIssuer = 1; - public static DistributionPointName GetInstance( - Asn1TaggedObject obj, - bool explicitly) + public static DistributionPointName GetInstance(object obj) { - return GetInstance(Asn1TaggedObject.GetInstance(obj, true)); - } - - public static DistributionPointName GetInstance( - object obj) - { - if (obj == null || obj is DistributionPointName) - { - return (DistributionPointName) obj; - } - - if (obj is Asn1TaggedObject) - { - return new DistributionPointName((Asn1TaggedObject) obj); - } - - throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); + if (obj == null) + return null; + if (obj is DistributionPointName distributionPointName) + return distributionPointName; + return new DistributionPointName(Asn1TaggedObject.GetInstance(obj)); } - public DistributionPointName( - int type, - Asn1Encodable name) + public static DistributionPointName GetInstance(Asn1TaggedObject obj, bool explicitly) { - this.type = type; - this.name = name; + return GetInstance(Asn1TaggedObject.GetInstance(obj, true)); } - public DistributionPointName( - GeneralNames name) - : this(FullName, name) - { - } + private readonly Asn1Encodable m_name; + private readonly int m_type; - public int PointType + public DistributionPointName(GeneralNames name) + : this(FullName, name) { - get { return type; } } - public Asn1Encodable Name + public DistributionPointName(int type, Asn1Encodable name) { - get { return name; } + m_type = type; + m_name = name; } - public DistributionPointName( - Asn1TaggedObject obj) + public int PointType => m_type; + + public Asn1Encodable Name => m_name; + + public DistributionPointName(Asn1TaggedObject obj) { - this.type = obj.TagNo; + m_type = obj.TagNo; - if (type == FullName) + if (m_type == FullName) { - this.name = GeneralNames.GetInstance(obj, false); + m_name = GeneralNames.GetInstance(obj, false); } else { - this.name = Asn1Set.GetInstance(obj, false); + m_name = Asn1Set.GetInstance(obj, false); } } public override Asn1Object ToAsn1Object() { - return new DerTaggedObject(false, type, name); + return new DerTaggedObject(false, m_type, m_name); } public override string ToString() { StringBuilder buf = new StringBuilder(); buf.AppendLine("DistributionPointName: ["); - if (type == FullName) + if (m_type == FullName) { - AppendObject(buf, "fullName", name.ToString()); + AppendObject(buf, "fullName", m_name.ToString()); } else { - AppendObject(buf, "nameRelativeToCRLIssuer", name.ToString()); + AppendObject(buf, "nameRelativeToCRLIssuer", m_name.ToString()); } buf.AppendLine("]"); return buf.ToString(); diff --git a/crypto/src/asn1/x509/GeneralNames.cs b/crypto/src/asn1/x509/GeneralNames.cs index acf263f84..3937b3279 100644 --- a/crypto/src/asn1/x509/GeneralNames.cs +++ b/crypto/src/asn1/x509/GeneralNames.cs @@ -1,24 +1,16 @@ -using System; using System.Text; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.X509 { public class GeneralNames : Asn1Encodable { - private static GeneralName[] Copy(GeneralName[] names) - { - return (GeneralName[])names.Clone(); - } - public static GeneralNames GetInstance(object obj) { - if (obj is GeneralNames) - return (GeneralNames)obj; if (obj == null) return null; + if (obj is GeneralNames generalNames) + return generalNames; return new GeneralNames(Asn1Sequence.GetInstance(obj)); } @@ -32,36 +24,33 @@ namespace Org.BouncyCastle.Asn1.X509 return GetInstance(X509Extensions.GetExtensionParsedValue(extensions, extOid)); } - private readonly GeneralName[] names; + private static GeneralName[] Copy(GeneralName[] names) + { + return (GeneralName[])names.Clone(); + } + + private readonly GeneralName[] m_names; /// Construct a GeneralNames object containing one GeneralName. /// The name to be contained. - public GeneralNames( - GeneralName name) + public GeneralNames(GeneralName name) { - names = new GeneralName[]{ name }; + m_names = new GeneralName[]{ name }; } - public GeneralNames( - GeneralName[] names) + public GeneralNames(GeneralName[] names) { - this.names = Copy(names); + m_names = Copy(names); } - private GeneralNames( - Asn1Sequence seq) + private GeneralNames(Asn1Sequence seq) { - this.names = new GeneralName[seq.Count]; - - for (int i = 0; i != seq.Count; i++) - { - names[i] = GeneralName.GetInstance(seq[i]); - } + m_names = seq.MapElements(GeneralName.GetInstance); } public GeneralName[] GetNames() { - return Copy(names); + return Copy(m_names); } /** @@ -72,14 +61,14 @@ namespace Org.BouncyCastle.Asn1.X509 */ public override Asn1Object ToAsn1Object() { - return new DerSequence(names); + return new DerSequence(m_names); } public override string ToString() { StringBuilder buf = new StringBuilder(); buf.AppendLine("GeneralNames:"); - foreach (GeneralName name in names) + foreach (GeneralName name in m_names) { buf.Append(" ") .Append(name) diff --git a/crypto/src/asn1/x509/IssuingDistributionPoint.cs b/crypto/src/asn1/x509/IssuingDistributionPoint.cs index a05efffa4..0287fd8f5 100644 --- a/crypto/src/asn1/x509/IssuingDistributionPoint.cs +++ b/crypto/src/asn1/x509/IssuingDistributionPoint.cs @@ -1,8 +1,6 @@ using System; using System.Text; -using Org.BouncyCastle.Utilities; - namespace Org.BouncyCastle.Asn1.X509 { /** @@ -28,27 +26,18 @@ namespace Org.BouncyCastle.Asn1.X509 private readonly Asn1Sequence seq; - public static IssuingDistributionPoint GetInstance( - Asn1TaggedObject obj, - bool explicitly) + public static IssuingDistributionPoint GetInstance(Asn1TaggedObject obj, bool explicitly) { return GetInstance(Asn1Sequence.GetInstance(obj, explicitly)); } - public static IssuingDistributionPoint GetInstance( - object obj) + public static IssuingDistributionPoint GetInstance(object obj) { - if (obj == null || obj is IssuingDistributionPoint) - { - return (IssuingDistributionPoint) obj; - } - - if (obj is Asn1Sequence) - { - return new IssuingDistributionPoint((Asn1Sequence) obj); - } - - throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); + if (obj == null) + return null; + if (obj is IssuingDistributionPoint issuingDistributionPoint) + return issuingDistributionPoint; + return new IssuingDistributionPoint(Asn1Sequence.GetInstance(obj)); } /** @@ -113,8 +102,7 @@ namespace Org.BouncyCastle.Asn1.X509 /** * Constructor from Asn1Sequence */ - private IssuingDistributionPoint( - Asn1Sequence seq) + private IssuingDistributionPoint(Asn1Sequence seq) { this.seq = seq; @@ -124,27 +112,27 @@ namespace Org.BouncyCastle.Asn1.X509 switch (o.TagNo) { - case 0: - // CHOICE so explicit - _distributionPoint = DistributionPointName.GetInstance(o, true); - break; - case 1: - _onlyContainsUserCerts = DerBoolean.GetInstance(o, false).IsTrue; - break; - case 2: - _onlyContainsCACerts = DerBoolean.GetInstance(o, false).IsTrue; - break; - case 3: - _onlySomeReasons = new ReasonFlags(ReasonFlags.GetInstance(o, false)); - break; - case 4: - _indirectCRL = DerBoolean.GetInstance(o, false).IsTrue; - break; - case 5: - _onlyContainsAttributeCerts = DerBoolean.GetInstance(o, false).IsTrue; - break; - default: - throw new ArgumentException("unknown tag in IssuingDistributionPoint"); + case 0: + // CHOICE so explicit + _distributionPoint = DistributionPointName.GetInstance(o, true); + break; + case 1: + _onlyContainsUserCerts = DerBoolean.GetInstance(o, false).IsTrue; + break; + case 2: + _onlyContainsCACerts = DerBoolean.GetInstance(o, false).IsTrue; + break; + case 3: + _onlySomeReasons = new ReasonFlags(ReasonFlags.GetInstance(o, false)); + break; + case 4: + _indirectCRL = DerBoolean.GetInstance(o, false).IsTrue; + break; + case 5: + _onlyContainsAttributeCerts = DerBoolean.GetInstance(o, false).IsTrue; + break; + default: + throw new ArgumentException("unknown tag in IssuingDistributionPoint"); } } } -- cgit 1.4.1