From d4978555b33327c750d59dc2cf2aa9874d122964 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 7 Feb 2023 17:19:28 +0700 Subject: Misc. refactoring --- crypto/src/asn1/x509/OtherName.cs | 27 +++++++++++------------ crypto/src/crypto/engines/DesEdeEngine.cs | 11 +++++---- crypto/src/crypto/engines/RC4Engine.cs | 8 +++---- crypto/src/pkix/PkixCertPathValidatorUtilities.cs | 2 +- crypto/src/x509/X509Crl.cs | 10 +-------- crypto/test/src/tls/test/NetworkStream.cs | 2 +- crypto/test/src/tls/test/PipedStream.cs | 2 +- 7 files changed, 25 insertions(+), 37 deletions(-) diff --git a/crypto/src/asn1/x509/OtherName.cs b/crypto/src/asn1/x509/OtherName.cs index 3e6a61499..bd80fe9ec 100644 --- a/crypto/src/asn1/x509/OtherName.cs +++ b/crypto/src/asn1/x509/OtherName.cs @@ -13,9 +13,6 @@ namespace Org.BouncyCastle.Asn1.X509 public class OtherName : Asn1Encodable { - private readonly DerObjectIdentifier typeID; - private readonly Asn1Encodable value; - /** * OtherName factory method. * @param obj the object used to construct an instance of @@ -29,13 +26,21 @@ namespace Org.BouncyCastle.Asn1.X509 */ public static OtherName GetInstance(object obj) { - if (obj is OtherName) - return (OtherName)obj; if (obj == null) return null; + if (obj is OtherName otherName) + return otherName; return new OtherName(Asn1Sequence.GetInstance(obj)); } + public static OtherName GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } + + private readonly DerObjectIdentifier typeID; + private readonly Asn1Encodable value; + /** * Base constructor. * @param typeID the type of the other name. @@ -50,18 +55,12 @@ namespace Org.BouncyCastle.Asn1.X509 private OtherName(Asn1Sequence seq) { this.typeID = DerObjectIdentifier.GetInstance(seq[0]); - this.value = DerTaggedObject.GetInstance(seq[1]).GetObject(); // explicitly tagged + this.value = Asn1Utilities.GetExplicitContextBaseObject(Asn1TaggedObject.GetInstance(seq[1]), tagNo: 0); } - public virtual DerObjectIdentifier TypeID - { - get { return typeID; } - } + public virtual DerObjectIdentifier TypeID => typeID; - public Asn1Encodable Value - { - get { return value; } - } + public Asn1Encodable Value => value; public override Asn1Object ToAsn1Object() { diff --git a/crypto/src/crypto/engines/DesEdeEngine.cs b/crypto/src/crypto/engines/DesEdeEngine.cs index a9185d295..58b9d5090 100644 --- a/crypto/src/crypto/engines/DesEdeEngine.cs +++ b/crypto/src/crypto/engines/DesEdeEngine.cs @@ -21,14 +21,13 @@ namespace Org.BouncyCastle.Crypto.Engines * @exception ArgumentException if the parameters argument is * inappropriate. */ - public override void Init( - bool forEncryption, - ICipherParameters parameters) + public override void Init(bool forEncryption, ICipherParameters parameters) { - if (!(parameters is KeyParameter)) - throw new ArgumentException("invalid parameter passed to DESede init - " + Platform.GetTypeName(parameters)); + if (!(parameters is KeyParameter keyParameter)) + throw new ArgumentException( + "invalid parameter passed to DESede init - " + Platform.GetTypeName(parameters)); - byte[] keyMaster = ((KeyParameter)parameters).GetKey(); + byte[] keyMaster = keyParameter.GetKey(); if (keyMaster.Length != 24 && keyMaster.Length != 16) throw new ArgumentException("key size must be 16 or 24 bytes."); diff --git a/crypto/src/crypto/engines/RC4Engine.cs b/crypto/src/crypto/engines/RC4Engine.cs index fe9594e10..5b2ffb238 100644 --- a/crypto/src/crypto/engines/RC4Engine.cs +++ b/crypto/src/crypto/engines/RC4Engine.cs @@ -28,18 +28,16 @@ namespace Org.BouncyCastle.Crypto.Engines * @exception ArgumentException if the parameters argument is * inappropriate. */ - public virtual void Init( - bool forEncryption, - ICipherParameters parameters) + public virtual void Init(bool forEncryption, ICipherParameters parameters) { - if (parameters is KeyParameter) + if (parameters is KeyParameter keyParameter) { /* * RC4 encryption and decryption is completely * symmetrical, so the 'forEncryption' is * irrelevant. */ - workingKey = ((KeyParameter)parameters).GetKey(); + workingKey = keyParameter.GetKey(); SetKey(workingKey); return; diff --git a/crypto/src/pkix/PkixCertPathValidatorUtilities.cs b/crypto/src/pkix/PkixCertPathValidatorUtilities.cs index 23c5e4205..7128ab833 100644 --- a/crypto/src/pkix/PkixCertPathValidatorUtilities.cs +++ b/crypto/src/pkix/PkixCertPathValidatorUtilities.cs @@ -720,7 +720,7 @@ namespace Org.BouncyCastle.Pkix // if (dp.getDistributionPoint() != null) // { // // look for nameRelativeToCRLIssuer - // if (dp.getDistributionPoint().getType() == DistributionPointName.NAME_RELATIVE_TO_CRL_ISSUER) + // if (dp.getDistributionPoint().Type == DistributionPointName.NAME_RELATIVE_TO_CRL_ISSUER) // { // // append fragment to issuer, only one // // issuer can be there, if this is given diff --git a/crypto/src/x509/X509Crl.cs b/crypto/src/x509/X509Crl.cs index a3f08a0ed..3780d277a 100644 --- a/crypto/src/x509/X509Crl.cs +++ b/crypto/src/x509/X509Crl.cs @@ -404,15 +404,7 @@ namespace Org.BouncyCastle.X509 * @return true if the given certificate is on this CRL, * false otherwise. */ -// public bool IsRevoked( -// Certificate cert) -// { -// if (!cert.getType().Equals("X.509")) -// { -// throw new RuntimeException("X.509 CRL used with non X.509 Cert"); -// } - public virtual bool IsRevoked( - X509Certificate cert) + public virtual bool IsRevoked(X509Certificate cert) { CrlEntry[] certs = c.GetRevokedCertificates(); diff --git a/crypto/test/src/tls/test/NetworkStream.cs b/crypto/test/src/tls/test/NetworkStream.cs index ed1b21014..013c27a12 100644 --- a/crypto/test/src/tls/test/NetworkStream.cs +++ b/crypto/test/src/tls/test/NetworkStream.cs @@ -97,7 +97,7 @@ namespace Org.BouncyCastle.Tls.Tests lock (this) { if (m_closed) - throw new ObjectDisposedException(this.GetType().Name); + throw new ObjectDisposedException(GetType().FullName); } } } diff --git a/crypto/test/src/tls/test/PipedStream.cs b/crypto/test/src/tls/test/PipedStream.cs index 95c3de465..82cd352af 100644 --- a/crypto/test/src/tls/test/PipedStream.cs +++ b/crypto/test/src/tls/test/PipedStream.cs @@ -126,7 +126,7 @@ namespace Org.BouncyCastle.Tls.Tests private void CheckOpen() { if (m_closed) - throw new ObjectDisposedException(this.GetType().Name); + throw new ObjectDisposedException(GetType().FullName); } private void WaitForData() -- cgit 1.4.1