From 2446ee5e5a03f864ab8fde48bc9c4c1f133f9271 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 4 Oct 2022 12:04:27 +0700 Subject: Refactoring --- crypto/src/cms/CMSEnvelopedHelper.cs | 16 ++++++++-------- crypto/src/cms/CMSSignedDataStreamGenerator.cs | 4 +--- crypto/src/cms/CMSSignedHelper.cs | 7 +++---- crypto/src/cms/KeyAgreeRecipientInformation.cs | 4 +--- crypto/src/cms/RecipientInformation.cs | 6 +++--- crypto/src/cms/SignerInformation.cs | 10 ++-------- 6 files changed, 18 insertions(+), 29 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/cms/CMSEnvelopedHelper.cs b/crypto/src/cms/CMSEnvelopedHelper.cs index 91f5ab910..86baa3642 100644 --- a/crypto/src/cms/CMSEnvelopedHelper.cs +++ b/crypto/src/cms/CMSEnvelopedHelper.cs @@ -114,21 +114,21 @@ namespace Org.BouncyCastle.Cms CmsSecureReadable secureReadable) { Asn1Encodable recipInfo = info.Info; - if (recipInfo is KeyTransRecipientInfo) + if (recipInfo is KeyTransRecipientInfo keyTransRecipientInfo) { - infos.Add(new KeyTransRecipientInformation((KeyTransRecipientInfo)recipInfo, secureReadable)); + infos.Add(new KeyTransRecipientInformation(keyTransRecipientInfo, secureReadable)); } - else if (recipInfo is KekRecipientInfo) + else if (recipInfo is KekRecipientInfo kekRecipientInfo) { - infos.Add(new KekRecipientInformation((KekRecipientInfo)recipInfo, secureReadable)); + infos.Add(new KekRecipientInformation(kekRecipientInfo, secureReadable)); } - else if (recipInfo is KeyAgreeRecipientInfo) + else if (recipInfo is KeyAgreeRecipientInfo keyAgreeRecipientInfo) { - KeyAgreeRecipientInformation.ReadRecipientInfo(infos, (KeyAgreeRecipientInfo)recipInfo, secureReadable); + KeyAgreeRecipientInformation.ReadRecipientInfo(infos, keyAgreeRecipientInfo, secureReadable); } - else if (recipInfo is PasswordRecipientInfo) + else if (recipInfo is PasswordRecipientInfo passwordRecipientInfo) { - infos.Add(new PasswordRecipientInformation((PasswordRecipientInfo)recipInfo, secureReadable)); + infos.Add(new PasswordRecipientInformation(passwordRecipientInfo, secureReadable)); } } diff --git a/crypto/src/cms/CMSSignedDataStreamGenerator.cs b/crypto/src/cms/CMSSignedDataStreamGenerator.cs index 69427c246..96200fc8c 100644 --- a/crypto/src/cms/CMSSignedDataStreamGenerator.cs +++ b/crypto/src/cms/CMSSignedDataStreamGenerator.cs @@ -660,10 +660,8 @@ namespace Org.BouncyCastle.Cms { foreach (object obj in _certs) { - if (obj is Asn1TaggedObject) + if (obj is Asn1TaggedObject tagged) { - Asn1TaggedObject tagged = (Asn1TaggedObject) obj; - if (tagged.TagNo == 1) { attrCertV1Found = true; diff --git a/crypto/src/cms/CMSSignedHelper.cs b/crypto/src/cms/CMSSignedHelper.cs index 0aced112e..0ab7790d1 100644 --- a/crypto/src/cms/CMSSignedHelper.cs +++ b/crypto/src/cms/CMSSignedHelper.cs @@ -210,9 +210,9 @@ namespace Org.BouncyCastle.Cms { string encOID = null; - if (key is RsaKeyParameters) + if (key is RsaKeyParameters rsaKeyParameters) { - if (!((RsaKeyParameters)key).IsPrivate) + if (!rsaKeyParameters.IsPrivate) throw new ArgumentException("Expected RSA private key"); encOID = CmsSignedGenerator.EncryptionRsa; @@ -244,9 +244,8 @@ namespace Org.BouncyCastle.Cms throw new ArgumentException("can't mix DSA with anything but SHA1/SHA2"); } } - else if (key is ECPrivateKeyParameters) + else if (key is ECPrivateKeyParameters ecPrivKey) { - ECPrivateKeyParameters ecPrivKey = (ECPrivateKeyParameters)key; string algName = ecPrivKey.AlgorithmName; if (algName == "ECGOST3410") diff --git a/crypto/src/cms/KeyAgreeRecipientInformation.cs b/crypto/src/cms/KeyAgreeRecipientInformation.cs index 8843ede0d..398082810 100644 --- a/crypto/src/cms/KeyAgreeRecipientInformation.cs +++ b/crypto/src/cms/KeyAgreeRecipientInformation.cs @@ -208,11 +208,9 @@ namespace Org.BouncyCastle.Cms public override CmsTypedStream GetContentStream( ICipherParameters key) { - if (!(key is AsymmetricKeyParameter)) + if (!(key is AsymmetricKeyParameter receiverPrivateKey)) throw new ArgumentException("KeyAgreement requires asymmetric key", "key"); - AsymmetricKeyParameter receiverPrivateKey = (AsymmetricKeyParameter) key; - if (!receiverPrivateKey.IsPrivate) throw new ArgumentException("Expected private key", "key"); diff --git a/crypto/src/cms/RecipientInformation.cs b/crypto/src/cms/RecipientInformation.cs index 272b841f2..978962711 100644 --- a/crypto/src/cms/RecipientInformation.cs +++ b/crypto/src/cms/RecipientInformation.cs @@ -112,15 +112,15 @@ namespace Org.BouncyCastle.Cms if (resultMac == null) { object cryptoObject = secureReadable.CryptoObject; - if (cryptoObject is IMac) + if (cryptoObject is IMac mac) { - resultMac = MacUtilities.DoFinal((IMac)cryptoObject); + resultMac = MacUtilities.DoFinal(mac); } } return Arrays.Clone(resultMac); } - + public abstract CmsTypedStream GetContentStream(ICipherParameters key); } } diff --git a/crypto/src/cms/SignerInformation.cs b/crypto/src/cms/SignerInformation.cs index 551d9b737..99d300121 100644 --- a/crypto/src/cms/SignerInformation.cs +++ b/crypto/src/cms/SignerInformation.cs @@ -433,11 +433,9 @@ namespace Org.BouncyCastle.Cms if (isCounterSignature) throw new CmsException("[For counter signatures,] the signedAttributes field MUST NOT contain a content-type attribute"); - if (!(validContentType is DerObjectIdentifier)) + if (!(validContentType is DerObjectIdentifier signedContentType)) throw new CmsException("content-type attribute value not of ASN.1 type 'OBJECT IDENTIFIER'"); - DerObjectIdentifier signedContentType = (DerObjectIdentifier)validContentType; - if (!signedContentType.Equals(contentType)) throw new CmsException("content-type attribute value does not match eContentType"); } @@ -454,12 +452,8 @@ namespace Org.BouncyCastle.Cms } else { - if (!(validMessageDigest is Asn1OctetString)) - { + if (!(validMessageDigest is Asn1OctetString signedMessageDigest)) throw new CmsException("message-digest attribute value not of ASN.1 type 'OCTET STRING'"); - } - - Asn1OctetString signedMessageDigest = (Asn1OctetString)validMessageDigest; if (!Arrays.AreEqual(resultDigest, signedMessageDigest.GetOctets())) throw new CmsException("message-digest attribute value does not match calculated value"); -- cgit 1.4.1