|
17 | 17 | using Org.BouncyCastle.Asn1.Nist; |
18 | 18 | using SecurityAPICommons.Commons; |
19 | 19 | using SecurityAPICommons.Utils; |
| 20 | +using Org.BouncyCastle.Utilities.Encoders; |
20 | 21 |
|
21 | 22 | namespace SecurityAPICommons.Keys |
22 | 23 | { |
@@ -67,9 +68,68 @@ public bool LoadPKCS12(String privateKeyPath, String alias, String password) |
67 | 68 | return true; |
68 | 69 | } |
69 | 70 |
|
70 | | - /******** EXTERNAL OBJECT PUBLIC METHODS - END ********/ |
71 | | - |
72 | | - [SecuritySafeCritical] |
| 71 | + [SecuritySafeCritical] |
| 72 | + public bool FromBase64(string base64) |
| 73 | + { |
| 74 | + bool res; |
| 75 | + try |
| 76 | + { |
| 77 | + res = ReadBase64(base64); |
| 78 | + } |
| 79 | + catch (IOException e) |
| 80 | + { |
| 81 | + this.error.setError("PK0015", e.Message); |
| 82 | + return false; |
| 83 | + } |
| 84 | + this.hasPrivateKey = res; |
| 85 | + return res; |
| 86 | + } |
| 87 | + |
| 88 | + [SecuritySafeCritical] |
| 89 | + public string ToBase64() |
| 90 | + { |
| 91 | + if (this.hasPrivateKey) |
| 92 | + { |
| 93 | + //PrivateKey priKey = getPrivateKeyXML(); |
| 94 | + //return Base64.toBase64String(priKey.getEncoded()); |
| 95 | + string encoded = ""; |
| 96 | + try |
| 97 | + { |
| 98 | + encoded = Base64.ToBase64String(this.privateKeyInfo.GetEncoded()); |
| 99 | + } |
| 100 | + catch (Exception e) |
| 101 | + { |
| 102 | + this.error.setError("PK0017", e.Message); |
| 103 | + return ""; |
| 104 | + } |
| 105 | + return encoded; |
| 106 | + } |
| 107 | + this.error.setError("PK0016", "No private key loaded"); |
| 108 | + return ""; |
| 109 | + |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + /******** EXTERNAL OBJECT PUBLIC METHODS - END ********/ |
| 114 | + |
| 115 | + private bool ReadBase64(string base64) |
| 116 | + { |
| 117 | + byte[] keybytes = Base64.Decode(base64); |
| 118 | + Asn1InputStream istream = new Asn1InputStream(keybytes); |
| 119 | + Asn1Sequence seq = (Asn1Sequence)istream.ReadObject(); |
| 120 | + this.privateKeyInfo = PrivateKeyInfo.GetInstance(seq); |
| 121 | + istream.Close(); |
| 122 | + if (this.privateKeyInfo == null) |
| 123 | + |
| 124 | + { |
| 125 | + this.error.setError("PK015", "Could not read private key from base64 string"); |
| 126 | + return false; |
| 127 | + } |
| 128 | + this.privateKeyAlgorithm = this.privateKeyInfo.PrivateKeyAlgorithm.Algorithm.Id;//this.privateKeyInfo.GetPrivateKeyAlgorithm().getAlgorithm().getId(); // 1.2.840.113549.1.1.1 |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + [SecuritySafeCritical] |
73 | 133 | public AsymmetricAlgorithm getPrivateKeyForXML() |
74 | 134 | { |
75 | 135 |
|
|
0 commit comments