DigitallySigned.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.Crypto.Tls
  6. {
  7. public class DigitallySigned
  8. {
  9. protected readonly SignatureAndHashAlgorithm mAlgorithm;
  10. protected readonly byte[] mSignature;
  11. public DigitallySigned(SignatureAndHashAlgorithm algorithm, byte[] signature)
  12. {
  13. if (signature == null)
  14. throw new ArgumentNullException("signature");
  15. this.mAlgorithm = algorithm;
  16. this.mSignature = signature;
  17. }
  18. /**
  19. * @return a {@link SignatureAndHashAlgorithm} (or null before TLS 1.2).
  20. */
  21. public virtual SignatureAndHashAlgorithm Algorithm
  22. {
  23. get { return mAlgorithm; }
  24. }
  25. public virtual byte[] Signature
  26. {
  27. get { return mSignature; }
  28. }
  29. /**
  30. * Encode this {@link DigitallySigned} to a {@link Stream}.
  31. *
  32. * @param output
  33. * the {@link Stream} to encode to.
  34. * @throws IOException
  35. */
  36. public virtual void Encode(Stream output)
  37. {
  38. if (mAlgorithm != null)
  39. {
  40. mAlgorithm.Encode(output);
  41. }
  42. TlsUtilities.WriteOpaque16(mSignature, output);
  43. }
  44. /**
  45. * Parse a {@link DigitallySigned} from a {@link Stream}.
  46. *
  47. * @param context
  48. * the {@link TlsContext} of the current connection.
  49. * @param input
  50. * the {@link Stream} to parse from.
  51. * @return a {@link DigitallySigned} object.
  52. * @throws IOException
  53. */
  54. public static DigitallySigned Parse(TlsContext context, Stream input)
  55. {
  56. SignatureAndHashAlgorithm algorithm = null;
  57. if (TlsUtilities.IsTlsV12(context))
  58. {
  59. algorithm = SignatureAndHashAlgorithm.Parse(input);
  60. }
  61. byte[] signature = TlsUtilities.ReadOpaque16(input);
  62. return new DigitallySigned(algorithm, signature);
  63. }
  64. }
  65. }
  66. #pragma warning restore
  67. #endif