DigitallySigned.cs 2.1 KB

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