From 9da0bed8b5a6f78fac535980f3650e512fe427a7 Mon Sep 17 00:00:00 2001 From: "ARTECH\\sgrampone" Date: Thu, 10 Sep 2020 15:16:39 -0300 Subject: [PATCH] Adds FromBase64 & ToBase64 to PrivateKey --- .../SecurityAPICommons/Commons/PrivateKey.cs | 9 +-- .../Keys/PrivateKeyManager.cs | 66 ++++++++++++++++++- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/dotnet/dotnetframework/SecurityAPICommons/Commons/PrivateKey.cs b/dotnet/dotnetframework/SecurityAPICommons/Commons/PrivateKey.cs index 52c591f..977aa9f 100644 --- a/dotnet/dotnetframework/SecurityAPICommons/Commons/PrivateKey.cs +++ b/dotnet/dotnetframework/SecurityAPICommons/Commons/PrivateKey.cs @@ -1,9 +1,10 @@ using System.Security; namespace SecurityAPICommons.Commons { - [SecuritySafeCritical] - public class PrivateKey : Key - { + [SecuritySafeCritical] + public class PrivateKey : Key + { - } + + } } diff --git a/dotnet/dotnetframework/SecurityAPICommons/Keys/PrivateKeyManager.cs b/dotnet/dotnetframework/SecurityAPICommons/Keys/PrivateKeyManager.cs index bfeadea..9a0d7db 100644 --- a/dotnet/dotnetframework/SecurityAPICommons/Keys/PrivateKeyManager.cs +++ b/dotnet/dotnetframework/SecurityAPICommons/Keys/PrivateKeyManager.cs @@ -17,6 +17,7 @@ using Org.BouncyCastle.Asn1.Nist; using SecurityAPICommons.Commons; using SecurityAPICommons.Utils; +using Org.BouncyCastle.Utilities.Encoders; namespace SecurityAPICommons.Keys { @@ -67,9 +68,68 @@ public bool LoadPKCS12(String privateKeyPath, String alias, String password) return true; } - /******** EXTERNAL OBJECT PUBLIC METHODS - END ********/ - - [SecuritySafeCritical] + [SecuritySafeCritical] + public bool FromBase64(string base64) + { + bool res; + try + { + res = ReadBase64(base64); + } + catch (IOException e) + { + this.error.setError("PK0015", e.Message); + return false; + } + this.hasPrivateKey = res; + return res; + } + + [SecuritySafeCritical] + public string ToBase64() + { + if (this.hasPrivateKey) + { + //PrivateKey priKey = getPrivateKeyXML(); + //return Base64.toBase64String(priKey.getEncoded()); + string encoded = ""; + try + { + encoded = Base64.ToBase64String(this.privateKeyInfo.GetEncoded()); + } + catch (Exception e) + { + this.error.setError("PK0017", e.Message); + return ""; + } + return encoded; + } + this.error.setError("PK0016", "No private key loaded"); + return ""; + + + } + + /******** EXTERNAL OBJECT PUBLIC METHODS - END ********/ + + private bool ReadBase64(string base64) + { + byte[] keybytes = Base64.Decode(base64); + Asn1InputStream istream = new Asn1InputStream(keybytes); + Asn1Sequence seq = (Asn1Sequence)istream.ReadObject(); + this.privateKeyInfo = PrivateKeyInfo.GetInstance(seq); + istream.Close(); + if (this.privateKeyInfo == null) + + { + this.error.setError("PK015", "Could not read private key from base64 string"); + return false; + } + this.privateKeyAlgorithm = this.privateKeyInfo.PrivateKeyAlgorithm.Algorithm.Id;//this.privateKeyInfo.GetPrivateKeyAlgorithm().getAlgorithm().getId(); // 1.2.840.113549.1.1.1 + return true; + } + + [SecuritySafeCritical] public AsymmetricAlgorithm getPrivateKeyForXML() {