ProtectedPkiMessage.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crmf;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cmp
  12. {
  13. /// <summary>
  14. /// Wrapper for a PKIMessage with protection attached to it.
  15. /// </summary>
  16. public class ProtectedPkiMessage
  17. {
  18. private readonly PkiMessage pkiMessage;
  19. /// <summary>
  20. /// Wrap a general message.
  21. /// </summary>
  22. /// <exception cref="ArgumentException">If the general message does not have protection.</exception>
  23. /// <param name="pkiMessage">The General message</param>
  24. public ProtectedPkiMessage(GeneralPkiMessage pkiMessage)
  25. {
  26. if (!pkiMessage.HasProtection)
  27. throw new ArgumentException("pki message not protected");
  28. this.pkiMessage = pkiMessage.ToAsn1Structure();
  29. }
  30. /// <summary>
  31. /// Wrap a PKI message.
  32. /// </summary>
  33. /// <exception cref="ArgumentException">If the PKI message does not have protection.</exception>
  34. /// <param name="pkiMessage">The PKI message</param>
  35. public ProtectedPkiMessage(PkiMessage pkiMessage)
  36. {
  37. if (null == pkiMessage.Header.ProtectionAlg)
  38. throw new ArgumentException("pki message not protected");
  39. this.pkiMessage = pkiMessage;
  40. }
  41. /// <summary>
  42. /// Message header
  43. /// </summary>
  44. public PkiHeader Header
  45. {
  46. get { return pkiMessage.Header; }
  47. }
  48. /// <summary>
  49. /// Message Body
  50. /// </summary>
  51. public PkiBody Body
  52. {
  53. get { return pkiMessage.Body; }
  54. }
  55. /// <summary>
  56. /// Return the underlying ASN.1 structure contained in this object.
  57. /// </summary>
  58. /// <returns>PKI Message structure</returns>
  59. public PkiMessage ToAsn1Message()
  60. {
  61. return pkiMessage;
  62. }
  63. /// <summary>
  64. /// Determine whether the message is protected by a password based MAC. Use verify(PKMACBuilder, char[])
  65. /// to verify the message if this method returns true.
  66. /// </summary>
  67. /// <returns>true if protection MAC PBE based, false otherwise.</returns>
  68. public bool HasPasswordBasedMacProtected
  69. {
  70. get { return Header.ProtectionAlg.Algorithm.Equals(CmpObjectIdentifiers.passwordBasedMac); }
  71. }
  72. /// <summary>
  73. /// Return the extra certificates associated with this message.
  74. /// </summary>
  75. /// <returns>an array of extra certificates, zero length if none present.</returns>
  76. public X509Certificate[] GetCertificates()
  77. {
  78. CmpCertificate[] certs = pkiMessage.GetExtraCerts();
  79. if (null == certs)
  80. return new X509Certificate[0];
  81. X509Certificate[] res = new X509Certificate[certs.Length];
  82. for (int t = 0; t < certs.Length; t++)
  83. {
  84. res[t] = new X509Certificate(X509CertificateStructure.GetInstance(certs[t].GetEncoded()));
  85. }
  86. return res;
  87. }
  88. /// <summary>
  89. /// Verify a message with a public key based signature attached.
  90. /// </summary>
  91. /// <param name="verifierFactory">a factory of signature verifiers.</param>
  92. /// <returns>true if the provider is able to create a verifier that validates the signature, false otherwise.</returns>
  93. public bool Verify(IVerifierFactory verifierFactory)
  94. {
  95. IStreamCalculator streamCalculator = verifierFactory.CreateCalculator();
  96. IVerifier result = (IVerifier)Process(streamCalculator);
  97. return result.IsVerified(pkiMessage.Protection.GetBytes());
  98. }
  99. private object Process(IStreamCalculator streamCalculator)
  100. {
  101. Asn1EncodableVector avec = new Asn1EncodableVector();
  102. avec.Add(pkiMessage.Header);
  103. avec.Add(pkiMessage.Body);
  104. byte[] enc = new DerSequence(avec).GetDerEncoded();
  105. streamCalculator.Stream.Write(enc, 0, enc.Length);
  106. streamCalculator.Stream.Flush();
  107. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(streamCalculator.Stream);
  108. return streamCalculator.GetResult();
  109. }
  110. /// <summary>
  111. /// Verify a message with password based MAC protection.
  112. /// </summary>
  113. /// <param name="pkMacBuilder">MAC builder that can be used to construct the appropriate MacCalculator</param>
  114. /// <param name="password">the MAC password</param>
  115. /// <returns>true if the passed in password and MAC builder verify the message, false otherwise.</returns>
  116. /// <exception cref="InvalidOperationException">if algorithm not MAC based, or an exception is thrown verifying the MAC.</exception>
  117. public bool Verify(PKMacBuilder pkMacBuilder, char[] password)
  118. {
  119. if (!CmpObjectIdentifiers.passwordBasedMac.Equals(pkiMessage.Header.ProtectionAlg.Algorithm))
  120. throw new InvalidOperationException("protection algorithm is not mac based");
  121. PbmParameter parameter = PbmParameter.GetInstance(pkiMessage.Header.ProtectionAlg.Parameters);
  122. pkMacBuilder.SetParameters(parameter);
  123. IBlockResult result = (IBlockResult)Process(pkMacBuilder.Build(password).CreateCalculator());
  124. return Arrays.ConstantTimeAreEqual(result.Collect(), this.pkiMessage.Protection.GetBytes());
  125. }
  126. }
  127. }
  128. #pragma warning restore
  129. #endif