diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-01-16 18:36:02 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-01-16 18:36:02 +0700 |
commit | c93162764c5097f65f2792c577f8a7658c6ee0dc (patch) | |
tree | 52bc090595bcb81e2db276c9023176deb02897b1 /crypto/src/openssl | |
parent | Refactoring in RsaCoreEngine (diff) | |
download | BouncyCastle.NET-ed25519-c93162764c5097f65f2792c577f8a7658c6ee0dc.tar.xz |
Refactoring in PEM classes
Diffstat (limited to 'crypto/src/openssl')
-rw-r--r-- | crypto/src/openssl/MiscPemGenerator.cs | 10 | ||||
-rw-r--r-- | crypto/src/openssl/PEMReader.cs | 30 | ||||
-rw-r--r-- | crypto/src/openssl/PEMWriter.cs | 30 |
3 files changed, 27 insertions, 43 deletions
diff --git a/crypto/src/openssl/MiscPemGenerator.cs b/crypto/src/openssl/MiscPemGenerator.cs index 0e918f793..fe2583063 100644 --- a/crypto/src/openssl/MiscPemGenerator.cs +++ b/crypto/src/openssl/MiscPemGenerator.cs @@ -32,15 +32,11 @@ namespace Org.BouncyCastle.OpenSsl private readonly SecureRandom random; public MiscPemGenerator(object obj) + : this(obj, null, null, null) { - this.obj = obj; } - public MiscPemGenerator( - object obj, - string algorithm, - char[] password, - SecureRandom random) + public MiscPemGenerator(object obj, string algorithm, char[] password, SecureRandom random) { this.obj = obj; this.algorithm = algorithm; @@ -54,9 +50,7 @@ namespace Org.BouncyCastle.OpenSsl throw new ArgumentNullException("obj"); if (obj is AsymmetricCipherKeyPair keyPair) - { return CreatePemObject(keyPair.Private); - } string type; byte[] encoding; diff --git a/crypto/src/openssl/PEMReader.cs b/crypto/src/openssl/PEMReader.cs index d2354dbe3..a2c4fd1d6 100644 --- a/crypto/src/openssl/PEMReader.cs +++ b/crypto/src/openssl/PEMReader.cs @@ -367,23 +367,17 @@ namespace Org.BouncyCastle.OpenSsl } } - // TODO Add an equivalent class for ECNamedCurveParameterSpec? - //private ECNamedCurveParameterSpec ReadECParameters( -// private X9ECParameters ReadECParameters(PemObject pemObject) -// { -// DerObjectIdentifier oid = (DerObjectIdentifier)Asn1Object.FromByteArray(pemObject.Content); -// -// //return ECNamedCurveTable.getParameterSpec(oid.Id); -// return GetCurveParameters(oid.Id); -// } - - private static X9ECParameters GetCurveParameters(string name) - { - X9ECParameters ecP = ECKeyPairGenerator.FindECCurveByName(name); - if (ecP == null) - throw new Exception("unknown curve name: " + name); - - return ecP; - } + //private X9ECParameters ReadECParameters(PemObject pemObject) + //{ + // DerObjectIdentifier oid = (DerObjectIdentifier)Asn1Object.FromByteArray(pemObject.Content); + + // //return ECNamedCurveTable.getParameterSpec(oid.Id); + // return GetCurveParameters(oid.Id); + //} + + //private static X9ECParameters GetCurveParameters(string name) + //{ + // return ECKeyPairGenerator.FindECCurveByName(name) ?? throw new Exception("unknown curve name: " + name); + //} } } diff --git a/crypto/src/openssl/PEMWriter.cs b/crypto/src/openssl/PEMWriter.cs index 043869cc3..58b6156d5 100644 --- a/crypto/src/openssl/PEMWriter.cs +++ b/crypto/src/openssl/PEMWriter.cs @@ -17,26 +17,22 @@ namespace Org.BouncyCastle.OpenSsl public void WriteObject(object obj) { - try - { - base.WriteObject(new MiscPemGenerator(obj)); - } - catch (PemGenerationException e) - { - if (e.InnerException is IOException inner) - throw inner; - - throw e; - } + WriteObject(obj, null, null, null); } - public void WriteObject( - object obj, - string algorithm, - char[] password, - SecureRandom random) + public void WriteObject(object obj, string algorithm, char[] password, SecureRandom random) { - base.WriteObject(new MiscPemGenerator(obj, algorithm, password, random)); + try + { + base.WriteObject(new MiscPemGenerator(obj, algorithm, password, random)); + } + catch (PemGenerationException e) + { + if (e.InnerException is IOException inner) + throw inner; + + throw e; + } } } } |