diff options
author | David Hook <dgh@bouncycastle.org> | 2019-01-15 11:01:18 +1100 |
---|---|---|
committer | David Hook <dgh@bouncycastle.org> | 2019-01-15 11:01:18 +1100 |
commit | f25f7bed6807096d9a67d31f547398c1f6f213e4 (patch) | |
tree | e05afc98f495985870a7b4edbf8ab45f63a75e68 /crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs | |
parent | added alg constructor (diff) | |
download | BouncyCastle.NET-ed25519-f25f7bed6807096d9a67d31f547398c1f6f213e4.tar.xz |
first cut on Pkcs8
Diffstat (limited to 'crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs')
-rw-r--r-- | crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs new file mode 100644 index 000000000..4c4ae83eb --- /dev/null +++ b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfo.cs @@ -0,0 +1,106 @@ + +using Org.BouncyCastle.Asn1.Pkcs; +using Org.BouncyCastle.Asn1.X509; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Utilities.IO; +using System; +using System.IO; + +namespace Org.BouncyCastle.Pkcs +{ + /// <summary> + /// A holding class for a PKCS#8 encrypted private key info object that allows for its decryption. + /// </summary> + public class Pkcs8EncryptedPrivateKeyInfo + { + private EncryptedPrivateKeyInfo encryptedPrivateKeyInfo; + + private static EncryptedPrivateKeyInfo parseBytes(byte[] pkcs8Encoding) + { + try + { + return EncryptedPrivateKeyInfo.GetInstance(pkcs8Encoding); + } + + catch (ArgumentException e) + { + throw new PkcsIOException("malformed data: " + e.Message, e); + } + catch (Exception e) + { + throw new PkcsIOException("malformed data: " + e.Message, e); + } + } + + /// <summary> + /// Base constructor from a PKCS#8 EncryptedPrivateKeyInfo object. + /// </summary> + /// <param name="encryptedPrivateKeyInfo">A PKCS#8 EncryptedPrivateKeyInfo object.</param> + public Pkcs8EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfo encryptedPrivateKeyInfo) + { + this.encryptedPrivateKeyInfo = encryptedPrivateKeyInfo; + } + + /// <summary> + /// Base constructor from a BER encoding of a PKCS#8 EncryptedPrivateKeyInfo object. + /// </summary> + /// <param name="encryptedPrivateKeyInfo">A BER encoding of a PKCS#8 EncryptedPrivateKeyInfo objects.</param> + public Pkcs8EncryptedPrivateKeyInfo(byte[] encryptedPrivateKeyInfo) : this(parseBytes(encryptedPrivateKeyInfo)) + { + + } + + /// <summary> + /// Returns the underlying ASN.1 structure inside this object. + /// </summary> + /// <returns>Return the EncryptedPrivateKeyInfo structure in this object.</returns> + public EncryptedPrivateKeyInfo ToAsn1Structure() + { + return encryptedPrivateKeyInfo; + } + + /// <summary> + /// Returns a copy of the encrypted data in this structure. + /// </summary> + /// <returns>Return a copy of the encrypted data in this object.</returns> + public byte[] GetEncryptedData() + { + return encryptedPrivateKeyInfo.GetEncryptedData(); + } + + /// <summary> + /// Return a binary ASN.1 encoding of the EncryptedPrivateKeyInfo structure in this object. + /// </summary> + /// <returns>A byte array containing the encoded object.</returns> + public byte[] GetEncoded() + { + return encryptedPrivateKeyInfo.GetEncoded(); + } + + /// <summary> + /// Get a decryptor from the passed in provider and decrypt the encrypted private key info, returning the result. + /// </summary> + /// <param name="inputDecryptorProvider">A provider to query for decryptors for the object.</param> + /// <returns>The decrypted private key info structure.</returns> + public PrivateKeyInfo DecryptPrivateKeyInfo(IDecryptorBuilderProvider inputDecryptorProvider) + { + try + { + ICipherBuilder decryptorBuilder = inputDecryptorProvider.CreateDecryptorBuilder(encryptedPrivateKeyInfo.EncryptionAlgorithm); + + ICipher encIn = decryptorBuilder.BuildCipher(new MemoryInputStream(encryptedPrivateKeyInfo.GetEncryptedData())); + + using (Stream strm = encIn.Stream) + { + byte[] data = Streams.ReadAll(encIn.Stream); + + return PrivateKeyInfo.GetInstance(data); + } + } + catch (Exception e) + { + throw new PkcsException("unable to read encrypted data: " + e.Message, e); + } + } + } +} |