Pkcs8EncryptedPrivateKeyInfo.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs
  11. {
  12. /// <summary>
  13. /// A holding class for a PKCS#8 encrypted private key info object that allows for its decryption.
  14. /// </summary>
  15. public class Pkcs8EncryptedPrivateKeyInfo
  16. {
  17. private EncryptedPrivateKeyInfo encryptedPrivateKeyInfo;
  18. private static EncryptedPrivateKeyInfo parseBytes(byte[] pkcs8Encoding)
  19. {
  20. try
  21. {
  22. return EncryptedPrivateKeyInfo.GetInstance(pkcs8Encoding);
  23. }
  24. catch (ArgumentException e)
  25. {
  26. throw new PkcsIOException("malformed data: " + e.Message, e);
  27. }
  28. catch (Exception e)
  29. {
  30. throw new PkcsIOException("malformed data: " + e.Message, e);
  31. }
  32. }
  33. /// <summary>
  34. /// Base constructor from a PKCS#8 EncryptedPrivateKeyInfo object.
  35. /// </summary>
  36. /// <param name="encryptedPrivateKeyInfo">A PKCS#8 EncryptedPrivateKeyInfo object.</param>
  37. public Pkcs8EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfo encryptedPrivateKeyInfo)
  38. {
  39. this.encryptedPrivateKeyInfo = encryptedPrivateKeyInfo;
  40. }
  41. /// <summary>
  42. /// Base constructor from a BER encoding of a PKCS#8 EncryptedPrivateKeyInfo object.
  43. /// </summary>
  44. /// <param name="encryptedPrivateKeyInfo">A BER encoding of a PKCS#8 EncryptedPrivateKeyInfo objects.</param>
  45. public Pkcs8EncryptedPrivateKeyInfo(byte[] encryptedPrivateKeyInfo) : this(parseBytes(encryptedPrivateKeyInfo))
  46. {
  47. }
  48. /// <summary>
  49. /// Returns the underlying ASN.1 structure inside this object.
  50. /// </summary>
  51. /// <returns>Return the EncryptedPrivateKeyInfo structure in this object.</returns>
  52. public EncryptedPrivateKeyInfo ToAsn1Structure()
  53. {
  54. return encryptedPrivateKeyInfo;
  55. }
  56. /// <summary>
  57. /// Returns a copy of the encrypted data in this structure.
  58. /// </summary>
  59. /// <returns>Return a copy of the encrypted data in this object.</returns>
  60. public byte[] GetEncryptedData()
  61. {
  62. return encryptedPrivateKeyInfo.GetEncryptedData();
  63. }
  64. /// <summary>
  65. /// Return a binary ASN.1 encoding of the EncryptedPrivateKeyInfo structure in this object.
  66. /// </summary>
  67. /// <returns>A byte array containing the encoded object.</returns>
  68. public byte[] GetEncoded()
  69. {
  70. return encryptedPrivateKeyInfo.GetEncoded();
  71. }
  72. /// <summary>
  73. /// Get a decryptor from the passed in provider and decrypt the encrypted private key info, returning the result.
  74. /// </summary>
  75. /// <param name="inputDecryptorProvider">A provider to query for decryptors for the object.</param>
  76. /// <returns>The decrypted private key info structure.</returns>
  77. public PrivateKeyInfo DecryptPrivateKeyInfo(IDecryptorBuilderProvider inputDecryptorProvider)
  78. {
  79. try
  80. {
  81. ICipherBuilder decryptorBuilder = inputDecryptorProvider.CreateDecryptorBuilder(encryptedPrivateKeyInfo.EncryptionAlgorithm);
  82. ICipher encIn = decryptorBuilder.BuildCipher(new MemoryInputStream(encryptedPrivateKeyInfo.GetEncryptedData()));
  83. Stream strm = encIn.Stream;
  84. byte[] data = Streams.ReadAll(encIn.Stream);
  85. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(strm);
  86. return PrivateKeyInfo.GetInstance(data);
  87. }
  88. catch (Exception e)
  89. {
  90. throw new PkcsException("unable to read encrypted data: " + e.Message, e);
  91. }
  92. }
  93. }
  94. }
  95. #pragma warning restore
  96. #endif