diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-26 20:47:24 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-26 20:47:24 +0700 |
commit | eed964522f8e198a33267387942b1764018dfe1e (patch) | |
tree | c6bcead7e5e54c88845287d10bca6a1235e655e8 /crypto/src/openssl | |
parent | Cleanup in PQC code (diff) | |
download | BouncyCastle.NET-ed25519-eed964522f8e198a33267387942b1764018dfe1e.tar.xz |
Replace IX509Store API with new store/selector API
- overhaul Cms, Pkix, X509 APIs
Diffstat (limited to 'crypto/src/openssl')
-rw-r--r-- | crypto/src/openssl/MiscPemGenerator.cs | 50 | ||||
-rw-r--r-- | crypto/src/openssl/PEMReader.cs | 2 |
2 files changed, 25 insertions, 27 deletions
diff --git a/crypto/src/openssl/MiscPemGenerator.cs b/crypto/src/openssl/MiscPemGenerator.cs index d875f49c1..3db299569 100644 --- a/crypto/src/openssl/MiscPemGenerator.cs +++ b/crypto/src/openssl/MiscPemGenerator.cs @@ -26,10 +26,10 @@ namespace Org.BouncyCastle.OpenSsl public class MiscPemGenerator : PemObjectGenerator { - private object obj; - private string algorithm; - private char[] password; - private SecureRandom random; + private readonly object obj; + private readonly string algorithm; + private readonly char[] password; + private readonly SecureRandom random; public MiscPemGenerator(object obj) { @@ -53,48 +53,47 @@ namespace Org.BouncyCastle.OpenSsl if (obj == null) throw new ArgumentNullException("obj"); - if (obj is AsymmetricCipherKeyPair) + if (obj is AsymmetricCipherKeyPair keyPair) { - return CreatePemObject(((AsymmetricCipherKeyPair)obj).Private); + return CreatePemObject(keyPair.Private); } string type; byte[] encoding; - if (obj is PemObject) - return (PemObject)obj; + if (obj is PemObject pemObject) + return pemObject; - if (obj is PemObjectGenerator) - return ((PemObjectGenerator)obj).Generate(); + if (obj is PemObjectGenerator pemObjectGenerator) + return pemObjectGenerator.Generate(); - if (obj is X509Certificate) + if (obj is X509Certificate certificate) { // TODO Should we prefer "X509 CERTIFICATE" here? type = "CERTIFICATE"; try { - encoding = ((X509Certificate)obj).GetEncoded(); + encoding = certificate.GetEncoded(); } catch (CertificateEncodingException e) { throw new IOException("Cannot Encode object: " + e.ToString()); } } - else if (obj is X509Crl) + else if (obj is X509Crl crl) { type = "X509 CRL"; try { - encoding = ((X509Crl)obj).GetEncoded(); + encoding = crl.GetEncoded(); } catch (CrlException e) { throw new IOException("Cannot Encode object: " + e.ToString()); } } - else if (obj is AsymmetricKeyParameter) + else if (obj is AsymmetricKeyParameter akp) { - AsymmetricKeyParameter akp = (AsymmetricKeyParameter) obj; if (akp.IsPrivate) { encoding = EncodePrivateKey(akp, out type); @@ -106,20 +105,20 @@ namespace Org.BouncyCastle.OpenSsl encoding = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(akp).GetDerEncoded(); } } - else if (obj is IX509AttributeCertificate) + else if (obj is X509V2AttributeCertificate attrCert) { type = "ATTRIBUTE CERTIFICATE"; - encoding = ((X509V2AttributeCertificate)obj).GetEncoded(); + encoding = attrCert.GetEncoded(); } - else if (obj is Pkcs10CertificationRequest) + else if (obj is Pkcs10CertificationRequest certReq) { type = "CERTIFICATE REQUEST"; - encoding = ((Pkcs10CertificationRequest)obj).GetEncoded(); + encoding = certReq.GetEncoded(); } - else if (obj is Asn1.Cms.ContentInfo) + else if (obj is Asn1.Cms.ContentInfo contentInfo) { type = "PKCS7"; - encoding = ((Asn1.Cms.ContentInfo)obj).GetEncoded(); + encoding = contentInfo.GetEncoded(); } else { @@ -158,17 +157,16 @@ namespace Org.BouncyCastle.OpenSsl if (random == null) throw new ArgumentNullException("random"); - if (obj is AsymmetricCipherKeyPair) + if (obj is AsymmetricCipherKeyPair keyPair) { - return CreatePemObject(((AsymmetricCipherKeyPair)obj).Private, algorithm, password, random); + return CreatePemObject(keyPair.Private, algorithm, password, random); } string type = null; byte[] keyData = null; - if (obj is AsymmetricKeyParameter) + if (obj is AsymmetricKeyParameter akp) { - AsymmetricKeyParameter akp = (AsymmetricKeyParameter) obj; if (akp.IsPrivate) { keyData = EncodePrivateKey(akp, out type); diff --git a/crypto/src/openssl/PEMReader.cs b/crypto/src/openssl/PEMReader.cs index 65d3f5ad6..5b4e37035 100644 --- a/crypto/src/openssl/PEMReader.cs +++ b/crypto/src/openssl/PEMReader.cs @@ -199,7 +199,7 @@ namespace Org.BouncyCastle.OpenSsl * @return the X509 Attribute Certificate * @throws IOException if an I/O error occured */ - private IX509AttributeCertificate ReadAttributeCertificate(PemObject pemObject) + private X509V2AttributeCertificate ReadAttributeCertificate(PemObject pemObject) { return new X509V2AttributeCertificate(pemObject.Content); } |