EncryptedValueBuilder.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Text;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  15. using BestHTTP.SecureProtocol.Org.BouncyCastle.Pkcs;
  16. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  17. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  18. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  19. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crmf
  20. {
  21. public class EncryptedValueBuilder
  22. {
  23. private readonly IKeyWrapper wrapper;
  24. private readonly ICipherBuilderWithKey encryptor;
  25. private readonly IEncryptedValuePadder padder;
  26. ///
  27. /// Create a builder that makes EncryptedValue structures.
  28. ///
  29. /// <param name="wrapper">wrapper a wrapper for key used to encrypt the actual data contained in the EncryptedValue.</param>
  30. /// <param name="encryptor">encryptor an output encryptor to encrypt the actual data contained in the EncryptedValue. </param>
  31. ///
  32. public EncryptedValueBuilder(IKeyWrapper wrapper, ICipherBuilderWithKey encryptor)
  33. : this(wrapper, encryptor, null)
  34. {
  35. }
  36. ///
  37. /// Create a builder that makes EncryptedValue structures with fixed length blocks padded using the passed in padder.
  38. ///
  39. /// <param name="wrapper">a wrapper for key used to encrypt the actual data contained in the EncryptedValue.</param>
  40. /// <param name="encryptor">encryptor an output encryptor to encrypt the actual data contained in the EncryptedValue.</param>
  41. /// <param name="padder">padder a padder to ensure that the EncryptedValue created will always be a constant length.</param>
  42. ///
  43. public EncryptedValueBuilder(IKeyWrapper wrapper, ICipherBuilderWithKey encryptor, IEncryptedValuePadder padder)
  44. {
  45. this.wrapper = wrapper;
  46. this.encryptor = encryptor;
  47. this.padder = padder;
  48. }
  49. ///
  50. /// Build an EncryptedValue structure containing the passed in pass phrase.
  51. ///
  52. /// <param name="revocationPassphrase">a revocation pass phrase.</param>
  53. ///<returns>an EncryptedValue containing the encrypted pass phrase.</returns>
  54. ///
  55. public EncryptedValue Build(char[] revocationPassphrase)
  56. {
  57. return EncryptData(PadData(Strings.ToUtf8ByteArray(revocationPassphrase)));
  58. }
  59. ///<summary>
  60. /// Build an EncryptedValue structure containing the certificate contained in
  61. /// the passed in holder.
  62. ///</summary>
  63. /// <param name="holder">a holder containing a certificate.</param>
  64. /// <returns>an EncryptedValue containing the encrypted certificate.</returns>
  65. /// <exception cref="CrmfException">on a failure to encrypt the data, or wrap the symmetric key for this value.</exception>
  66. ///
  67. public EncryptedValue Build(X509Certificate holder)
  68. {
  69. try
  70. {
  71. return EncryptData(PadData(holder.GetEncoded()));
  72. }
  73. catch (IOException e)
  74. {
  75. throw new CrmfException("cannot encode certificate: " + e.Message, e);
  76. }
  77. }
  78. ///<summary>
  79. /// Build an EncryptedValue structure containing the private key contained in
  80. /// the passed info structure.
  81. ///</summary>
  82. /// <param name="privateKeyInfo">a PKCS#8 private key info structure.</param>
  83. /// <returns>an EncryptedValue containing an EncryptedPrivateKeyInfo structure.</returns>
  84. /// <exception cref="CrmfException">on a failure to encrypt the data, or wrap the symmetric key for this value.</exception>
  85. ///
  86. public EncryptedValue Build(PrivateKeyInfo privateKeyInfo)
  87. {
  88. Pkcs8EncryptedPrivateKeyInfoBuilder encInfoBldr = new Pkcs8EncryptedPrivateKeyInfoBuilder(privateKeyInfo);
  89. AlgorithmIdentifier intendedAlg = privateKeyInfo.PrivateKeyAlgorithm;
  90. AlgorithmIdentifier symmAlg = (AlgorithmIdentifier)encryptor.AlgorithmDetails;
  91. DerBitString encSymmKey;
  92. try
  93. {
  94. Pkcs8EncryptedPrivateKeyInfo encInfo = encInfoBldr.Build(encryptor);
  95. encSymmKey = new DerBitString(wrapper.Wrap(((KeyParameter)encryptor.Key).GetKey()).Collect());
  96. AlgorithmIdentifier keyAlg = (AlgorithmIdentifier)wrapper.AlgorithmDetails;
  97. Asn1OctetString valueHint = null;
  98. return new EncryptedValue(intendedAlg, symmAlg, encSymmKey, keyAlg, valueHint, new DerBitString(encInfo.GetEncryptedData()));
  99. }
  100. catch (Exception e)
  101. {
  102. throw new CrmfException("cannot wrap key: " + e.Message, e);
  103. }
  104. }
  105. private EncryptedValue EncryptData(byte[] data)
  106. {
  107. MemoryOutputStream bOut = new MemoryOutputStream();
  108. Stream eOut = encryptor.BuildCipher(bOut).Stream;
  109. try
  110. {
  111. eOut.Write(data, 0, data.Length);
  112. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(eOut);
  113. }
  114. catch (IOException e)
  115. {
  116. throw new CrmfException("cannot process data: " + e.Message, e);
  117. }
  118. AlgorithmIdentifier intendedAlg = null;
  119. AlgorithmIdentifier symmAlg = (AlgorithmIdentifier)encryptor.AlgorithmDetails;
  120. DerBitString encSymmKey;
  121. try
  122. {
  123. encSymmKey = new DerBitString(wrapper.Wrap(((KeyParameter)encryptor.Key).GetKey()).Collect());
  124. }
  125. catch (Exception e)
  126. {
  127. throw new CrmfException("cannot wrap key: " + e.Message, e);
  128. }
  129. AlgorithmIdentifier keyAlg = (AlgorithmIdentifier)wrapper.AlgorithmDetails;
  130. Asn1OctetString valueHint = null;
  131. DerBitString encValue = new DerBitString(bOut.ToArray());
  132. return new EncryptedValue(intendedAlg, symmAlg, encSymmKey, keyAlg, valueHint, encValue);
  133. }
  134. private byte[] PadData(byte[] data)
  135. {
  136. if (padder != null)
  137. {
  138. return padder.GetPaddedData(data);
  139. }
  140. return data;
  141. }
  142. }
  143. }
  144. #pragma warning restore
  145. #endif