From 97647d0d000d3742bc65e7063343797620ca5154 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 27 Mar 2024 11:50:59 +0700 Subject: Refactoring in Asn1.Mozilla --- crypto/src/asn1/mozilla/PublicKeyAndChallenge.cs | 105 +++++++++++------------ 1 file changed, 51 insertions(+), 54 deletions(-) (limited to 'crypto') diff --git a/crypto/src/asn1/mozilla/PublicKeyAndChallenge.cs b/crypto/src/asn1/mozilla/PublicKeyAndChallenge.cs index ff2a1199f..eafbfd72b 100644 --- a/crypto/src/asn1/mozilla/PublicKeyAndChallenge.cs +++ b/crypto/src/asn1/mozilla/PublicKeyAndChallenge.cs @@ -1,68 +1,65 @@ using System; using Org.BouncyCastle.Asn1.X509; -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Mozilla { - /** - * This is designed to parse - * the PublicKeyAndChallenge created by the KEYGEN tag included by - * Mozilla based browsers. - *
-	 *  PublicKeyAndChallenge ::= SEQUENCE {
-	 *    spki SubjectPublicKeyInfo,
-	 *    challenge IA5STRING
-	 *  }
-	 *
-	 *  
- */ - public class PublicKeyAndChallenge - : Asn1Encodable - { - private Asn1Sequence pkacSeq; - private SubjectPublicKeyInfo spki; - private DerIA5String challenge; + /// + /// For parsing the PublicKeyAndChallenge created by the KEYGEN tag included by Mozilla based browsers. + /// + /// + /// + /// PublicKeyAndChallenge ::= SEQUENCE + /// { + /// spki SubjectPublicKeyInfo, + /// challenge IA5STRING + /// } + /// + /// + public class PublicKeyAndChallenge + : Asn1Encodable + { + private readonly SubjectPublicKeyInfo m_spki; + private readonly DerIA5String m_challenge; - public static PublicKeyAndChallenge GetInstance( - object obj) - { - if (obj is PublicKeyAndChallenge) - { - return (PublicKeyAndChallenge) obj; - } + public static PublicKeyAndChallenge GetInstance(object obj) + { + if (obj == null) + return null; + if (obj is PublicKeyAndChallenge publicKeyAndChallenge) + return publicKeyAndChallenge; +#pragma warning disable CS0618 // Type or member is obsolete + return new PublicKeyAndChallenge(Asn1Sequence.GetInstance(obj)); +#pragma warning restore CS0618 // Type or member is obsolete + } - if (obj is Asn1Sequence) - { - return new PublicKeyAndChallenge((Asn1Sequence) obj); - } + public static PublicKeyAndChallenge GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { +#pragma warning disable CS0618 // Type or member is obsolete + return new PublicKeyAndChallenge(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); +#pragma warning restore CS0618 // Type or member is obsolete + } - throw new ArgumentException( - "unknown object in 'PublicKeyAndChallenge' factory : " - + Platform.GetTypeName(obj) + "."); - } + public PublicKeyAndChallenge(SubjectPublicKeyInfo spki, DerIA5String challenge) + { + m_spki = spki ?? throw new ArgumentNullException(nameof(spki)); + m_challenge = challenge ?? throw new ArgumentNullException(nameof(m_challenge)); + } - public PublicKeyAndChallenge( - Asn1Sequence seq) - { - pkacSeq = seq; - spki = SubjectPublicKeyInfo.GetInstance(seq[0]); - challenge = DerIA5String.GetInstance(seq[1]); - } + [Obsolete("Use 'GetInstance' instead")] + public PublicKeyAndChallenge(Asn1Sequence seq) + { + m_spki = SubjectPublicKeyInfo.GetInstance(seq[0]); + m_challenge = DerIA5String.GetInstance(seq[1]); + } - public override Asn1Object ToAsn1Object() - { - return pkacSeq; - } + public DerIA5String Challenge => m_challenge; - public SubjectPublicKeyInfo SubjectPublicKeyInfo - { - get { return spki; } - } + public SubjectPublicKeyInfo Spki => m_spki; - public DerIA5String Challenge - { - get { return challenge; } - } - } + [Obsolete("Use 'Spki' instead")] + public SubjectPublicKeyInfo SubjectPublicKeyInfo => m_spki; + + public override Asn1Object ToAsn1Object() => new DerSequence(m_spki, m_challenge); + } } -- cgit 1.4.1