SignatureAndHashAlgorithm.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /// <summary>RFC 5246 7.4.1.4.1</summary>
  8. public sealed class SignatureAndHashAlgorithm
  9. {
  10. public static readonly SignatureAndHashAlgorithm ecdsa_brainpoolP256r1tls13_sha256 =
  11. Create(SignatureScheme.ecdsa_brainpoolP256r1tls13_sha256);
  12. public static readonly SignatureAndHashAlgorithm ecdsa_brainpoolP384r1tls13_sha384 =
  13. Create(SignatureScheme.ecdsa_brainpoolP384r1tls13_sha384);
  14. public static readonly SignatureAndHashAlgorithm ecdsa_brainpoolP512r1tls13_sha512 =
  15. Create(SignatureScheme.ecdsa_brainpoolP512r1tls13_sha512);
  16. public static readonly SignatureAndHashAlgorithm ed25519 =
  17. Create(SignatureScheme.ed25519);
  18. public static readonly SignatureAndHashAlgorithm ed448 =
  19. Create(SignatureScheme.ed448);
  20. public static readonly SignatureAndHashAlgorithm gostr34102012_256 =
  21. Create(HashAlgorithm.Intrinsic, SignatureAlgorithm.gostr34102012_256);
  22. public static readonly SignatureAndHashAlgorithm gostr34102012_512 =
  23. Create(HashAlgorithm.Intrinsic, SignatureAlgorithm.gostr34102012_512);
  24. public static readonly SignatureAndHashAlgorithm rsa_pss_rsae_sha256 =
  25. Create(SignatureScheme.rsa_pss_rsae_sha256);
  26. public static readonly SignatureAndHashAlgorithm rsa_pss_rsae_sha384 =
  27. Create(SignatureScheme.rsa_pss_rsae_sha384);
  28. public static readonly SignatureAndHashAlgorithm rsa_pss_rsae_sha512 =
  29. Create(SignatureScheme.rsa_pss_rsae_sha512);
  30. public static readonly SignatureAndHashAlgorithm rsa_pss_pss_sha256 =
  31. Create(SignatureScheme.rsa_pss_pss_sha256);
  32. public static readonly SignatureAndHashAlgorithm rsa_pss_pss_sha384 =
  33. Create(SignatureScheme.rsa_pss_pss_sha384);
  34. public static readonly SignatureAndHashAlgorithm rsa_pss_pss_sha512 =
  35. Create(SignatureScheme.rsa_pss_pss_sha512);
  36. public static SignatureAndHashAlgorithm GetInstance(short hashAlgorithm, short signatureAlgorithm)
  37. {
  38. switch (hashAlgorithm)
  39. {
  40. case HashAlgorithm.Intrinsic:
  41. return GetInstanceIntrinsic(signatureAlgorithm);
  42. default:
  43. return Create(hashAlgorithm, signatureAlgorithm);
  44. }
  45. }
  46. private static SignatureAndHashAlgorithm GetInstanceIntrinsic(short signatureAlgorithm)
  47. {
  48. switch (signatureAlgorithm)
  49. {
  50. case SignatureAlgorithm.ed25519:
  51. return ed25519;
  52. case SignatureAlgorithm.ed448:
  53. return ed448;
  54. case SignatureAlgorithm.gostr34102012_256:
  55. return gostr34102012_256;
  56. case SignatureAlgorithm.gostr34102012_512:
  57. return gostr34102012_512;
  58. case SignatureAlgorithm.rsa_pss_rsae_sha256:
  59. return rsa_pss_rsae_sha256;
  60. case SignatureAlgorithm.rsa_pss_rsae_sha384:
  61. return rsa_pss_rsae_sha384;
  62. case SignatureAlgorithm.rsa_pss_rsae_sha512:
  63. return rsa_pss_rsae_sha512;
  64. case SignatureAlgorithm.rsa_pss_pss_sha256:
  65. return rsa_pss_pss_sha256;
  66. case SignatureAlgorithm.rsa_pss_pss_sha384:
  67. return rsa_pss_pss_sha384;
  68. case SignatureAlgorithm.rsa_pss_pss_sha512:
  69. return rsa_pss_pss_sha512;
  70. case SignatureAlgorithm.ecdsa_brainpoolP256r1tls13_sha256:
  71. return ecdsa_brainpoolP256r1tls13_sha256;
  72. case SignatureAlgorithm.ecdsa_brainpoolP384r1tls13_sha384:
  73. return ecdsa_brainpoolP384r1tls13_sha384;
  74. case SignatureAlgorithm.ecdsa_brainpoolP512r1tls13_sha512:
  75. return ecdsa_brainpoolP512r1tls13_sha512;
  76. default:
  77. return Create(HashAlgorithm.Intrinsic, signatureAlgorithm);
  78. }
  79. }
  80. private static SignatureAndHashAlgorithm Create(int signatureScheme)
  81. {
  82. short hashAlgorithm = SignatureScheme.GetHashAlgorithm(signatureScheme);
  83. short signatureAlgorithm = SignatureScheme.GetSignatureAlgorithm(signatureScheme);
  84. return Create(hashAlgorithm, signatureAlgorithm);
  85. }
  86. private static SignatureAndHashAlgorithm Create(short hashAlgorithm, short signatureAlgorithm)
  87. {
  88. return new SignatureAndHashAlgorithm(hashAlgorithm, signatureAlgorithm);
  89. }
  90. private readonly short m_hash;
  91. private readonly short m_signature;
  92. /// <param name="hash"><see cref="HashAlgorithm"/></param>
  93. /// <param name="signature"><see cref="SignatureAlgorithm"/></param>
  94. public SignatureAndHashAlgorithm(short hash, short signature)
  95. {
  96. /*
  97. * TODO]tls] The TlsUtils methods are inlined here to avoid circular static initialization
  98. * b/w these classes. We should refactor parts of TlsUtils into separate classes. e.g. the
  99. * TLS low-level encoding methods, and/or the SigAndHash registry and methods.
  100. */
  101. //if (!TlsUtilities.IsValidUint8(hash))
  102. if ((hash & 0xFF) != hash)
  103. throw new ArgumentException("should be a uint8", "hash");
  104. //if (!TlsUtilities.IsValidUint8(signature))
  105. if ((signature & 0xFF) != signature)
  106. throw new ArgumentException("should be a uint8", "signature");
  107. this.m_hash = hash;
  108. this.m_signature = signature;
  109. }
  110. /// <returns><see cref="HashAlgorithm"/></returns>
  111. public short Hash
  112. {
  113. get { return m_hash; }
  114. }
  115. /// <returns><see cref="SignatureAlgorithm"/></returns>
  116. public short Signature
  117. {
  118. get { return m_signature; }
  119. }
  120. /// <summary>Encode this <see cref="SignatureAndHashAlgorithm"/> to a <see cref="Stream"/>.</summary>
  121. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  122. /// <exception cref="IOException"/>
  123. public void Encode(Stream output)
  124. {
  125. TlsUtilities.WriteUint8(Hash, output);
  126. TlsUtilities.WriteUint8(Signature, output);
  127. }
  128. /// <summary>Parse a <see cref="SignatureAndHashAlgorithm"/> from a <see cref="Stream"/>.</summary>
  129. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  130. /// <returns>a <see cref="SignatureAndHashAlgorithm"/> object.</returns>
  131. /// <exception cref="IOException"/>
  132. public static SignatureAndHashAlgorithm Parse(Stream input)
  133. {
  134. short hash = TlsUtilities.ReadUint8(input);
  135. short signature = TlsUtilities.ReadUint8(input);
  136. return GetInstance(hash, signature);
  137. }
  138. public override bool Equals(object obj)
  139. {
  140. if (!(obj is SignatureAndHashAlgorithm))
  141. return false;
  142. SignatureAndHashAlgorithm other = (SignatureAndHashAlgorithm)obj;
  143. return other.Hash == Hash && other.Signature == Signature;
  144. }
  145. public override int GetHashCode()
  146. {
  147. return ((int)Hash << 16) | (int)Signature;
  148. }
  149. public override string ToString()
  150. {
  151. return "{" + HashAlgorithm.GetText(Hash) + "," + SignatureAlgorithm.GetText(Signature) + "}";
  152. }
  153. }
  154. }
  155. #pragma warning restore
  156. #endif