DigitallySigned.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  6. {
  7. public sealed class DigitallySigned
  8. {
  9. private readonly SignatureAndHashAlgorithm algorithm;
  10. private readonly byte[] signature;
  11. public DigitallySigned(SignatureAndHashAlgorithm algorithm, byte[] signature)
  12. {
  13. if (signature == null)
  14. throw new ArgumentNullException("signature");
  15. this.algorithm = algorithm;
  16. this.signature = signature;
  17. }
  18. /// <returns>a <see cref="SignatureAndHashAlgorithm"/> (or null before TLS 1.2).</returns>
  19. public SignatureAndHashAlgorithm Algorithm
  20. {
  21. get { return algorithm; }
  22. }
  23. public byte[] Signature
  24. {
  25. get { return signature; }
  26. }
  27. /// <summary>Encode this <see cref="DigitallySigned"/> to a <see cref="Stream"/>.</summary>
  28. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  29. /// <exception cref="IOException"/>
  30. public void Encode(Stream output)
  31. {
  32. if (algorithm != null)
  33. {
  34. algorithm.Encode(output);
  35. }
  36. TlsUtilities.WriteOpaque16(signature, output);
  37. }
  38. /// <summary>Parse a <see cref="DigitallySigned"/> from a <see cref="Stream"/>.</summary>
  39. /// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
  40. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  41. /// <returns>a <see cref="DigitallySigned"/> object.</returns>
  42. /// <exception cref="IOException"/>
  43. public static DigitallySigned Parse(TlsContext context, Stream input)
  44. {
  45. SignatureAndHashAlgorithm algorithm = null;
  46. if (TlsUtilities.IsTlsV12(context))
  47. {
  48. algorithm = SignatureAndHashAlgorithm.Parse(input);
  49. if (SignatureAlgorithm.anonymous == algorithm.Signature)
  50. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  51. }
  52. byte[] signature = TlsUtilities.ReadOpaque16(input);
  53. return new DigitallySigned(algorithm, signature);
  54. }
  55. }
  56. }
  57. #pragma warning restore
  58. #endif