TlsExtensionsUtilities.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Utilities;
  6. namespace Org.BouncyCastle.Crypto.Tls
  7. {
  8. public abstract class TlsExtensionsUtilities
  9. {
  10. public static IDictionary EnsureExtensionsInitialised(IDictionary extensions)
  11. {
  12. return extensions == null ? Org.BouncyCastle.Utilities.Platform.CreateHashtable() : extensions;
  13. }
  14. public static void AddEncryptThenMacExtension(IDictionary extensions)
  15. {
  16. extensions[ExtensionType.encrypt_then_mac] = CreateEncryptThenMacExtension();
  17. }
  18. public static void AddExtendedMasterSecretExtension(IDictionary extensions)
  19. {
  20. extensions[ExtensionType.extended_master_secret] = CreateExtendedMasterSecretExtension();
  21. }
  22. /// <exception cref="IOException"></exception>
  23. public static void AddHeartbeatExtension(IDictionary extensions, HeartbeatExtension heartbeatExtension)
  24. {
  25. extensions[ExtensionType.heartbeat] = CreateHeartbeatExtension(heartbeatExtension);
  26. }
  27. /// <exception cref="IOException"></exception>
  28. public static void AddMaxFragmentLengthExtension(IDictionary extensions, byte maxFragmentLength)
  29. {
  30. extensions[ExtensionType.max_fragment_length] = CreateMaxFragmentLengthExtension(maxFragmentLength);
  31. }
  32. /// <exception cref="IOException"></exception>
  33. public static void AddPaddingExtension(IDictionary extensions, int dataLength)
  34. {
  35. extensions[ExtensionType.padding] = CreatePaddingExtension(dataLength);
  36. }
  37. /// <exception cref="IOException"></exception>
  38. public static void AddServerNameExtension(IDictionary extensions, ServerNameList serverNameList)
  39. {
  40. extensions[ExtensionType.server_name] = CreateServerNameExtension(serverNameList);
  41. }
  42. /// <exception cref="IOException"></exception>
  43. public static void AddStatusRequestExtension(IDictionary extensions, CertificateStatusRequest statusRequest)
  44. {
  45. extensions[ExtensionType.status_request] = CreateStatusRequestExtension(statusRequest);
  46. }
  47. public static void AddTruncatedHMacExtension(IDictionary extensions)
  48. {
  49. extensions[ExtensionType.truncated_hmac] = CreateTruncatedHMacExtension();
  50. }
  51. /// <exception cref="IOException"></exception>
  52. public static HeartbeatExtension GetHeartbeatExtension(IDictionary extensions)
  53. {
  54. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.heartbeat);
  55. return extensionData == null ? null : ReadHeartbeatExtension(extensionData);
  56. }
  57. /// <exception cref="IOException"></exception>
  58. public static short GetMaxFragmentLengthExtension(IDictionary extensions)
  59. {
  60. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.max_fragment_length);
  61. return extensionData == null ? (short)-1 : (short)ReadMaxFragmentLengthExtension(extensionData);
  62. }
  63. /// <exception cref="IOException"></exception>
  64. public static int GetPaddingExtension(IDictionary extensions)
  65. {
  66. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.padding);
  67. return extensionData == null ? -1 : ReadPaddingExtension(extensionData);
  68. }
  69. /// <exception cref="IOException"></exception>
  70. public static ServerNameList GetServerNameExtension(IDictionary extensions)
  71. {
  72. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.server_name);
  73. return extensionData == null ? null : ReadServerNameExtension(extensionData);
  74. }
  75. /// <exception cref="IOException"></exception>
  76. public static CertificateStatusRequest GetStatusRequestExtension(IDictionary extensions)
  77. {
  78. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.status_request);
  79. return extensionData == null ? null : ReadStatusRequestExtension(extensionData);
  80. }
  81. /// <exception cref="IOException"></exception>
  82. public static bool HasEncryptThenMacExtension(IDictionary extensions)
  83. {
  84. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.encrypt_then_mac);
  85. return extensionData == null ? false : ReadEncryptThenMacExtension(extensionData);
  86. }
  87. /// <exception cref="IOException"></exception>
  88. public static bool HasExtendedMasterSecretExtension(IDictionary extensions)
  89. {
  90. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.extended_master_secret);
  91. return extensionData == null ? false : ReadExtendedMasterSecretExtension(extensionData);
  92. }
  93. /// <exception cref="IOException"></exception>
  94. public static bool HasTruncatedHMacExtension(IDictionary extensions)
  95. {
  96. byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.truncated_hmac);
  97. return extensionData == null ? false : ReadTruncatedHMacExtension(extensionData);
  98. }
  99. public static byte[] CreateEmptyExtensionData()
  100. {
  101. return TlsUtilities.EmptyBytes;
  102. }
  103. public static byte[] CreateEncryptThenMacExtension()
  104. {
  105. return CreateEmptyExtensionData();
  106. }
  107. public static byte[] CreateExtendedMasterSecretExtension()
  108. {
  109. return CreateEmptyExtensionData();
  110. }
  111. /// <exception cref="IOException"></exception>
  112. public static byte[] CreateHeartbeatExtension(HeartbeatExtension heartbeatExtension)
  113. {
  114. if (heartbeatExtension == null)
  115. throw new TlsFatalAlert(AlertDescription.internal_error);
  116. MemoryStream buf = new MemoryStream();
  117. heartbeatExtension.Encode(buf);
  118. return buf.ToArray();
  119. }
  120. /// <exception cref="IOException"></exception>
  121. public static byte[] CreateMaxFragmentLengthExtension(byte maxFragmentLength)
  122. {
  123. return new byte[]{ maxFragmentLength };
  124. }
  125. /// <exception cref="IOException"></exception>
  126. public static byte[] CreatePaddingExtension(int dataLength)
  127. {
  128. TlsUtilities.CheckUint16(dataLength);
  129. return new byte[dataLength];
  130. }
  131. /// <exception cref="IOException"></exception>
  132. public static byte[] CreateServerNameExtension(ServerNameList serverNameList)
  133. {
  134. if (serverNameList == null)
  135. throw new TlsFatalAlert(AlertDescription.internal_error);
  136. MemoryStream buf = new MemoryStream();
  137. serverNameList.Encode(buf);
  138. return buf.ToArray();
  139. }
  140. /// <exception cref="IOException"></exception>
  141. public static byte[] CreateStatusRequestExtension(CertificateStatusRequest statusRequest)
  142. {
  143. if (statusRequest == null)
  144. {
  145. throw new TlsFatalAlert(AlertDescription.internal_error);
  146. }
  147. MemoryStream buf = new MemoryStream();
  148. statusRequest.Encode(buf);
  149. return buf.ToArray();
  150. }
  151. public static byte[] CreateTruncatedHMacExtension()
  152. {
  153. return CreateEmptyExtensionData();
  154. }
  155. /// <exception cref="IOException"></exception>
  156. private static bool ReadEmptyExtensionData(byte[] extensionData)
  157. {
  158. if (extensionData == null)
  159. throw new ArgumentNullException("extensionData");
  160. if (extensionData.Length != 0)
  161. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  162. return true;
  163. }
  164. /// <exception cref="IOException"></exception>
  165. public static bool ReadEncryptThenMacExtension(byte[] extensionData)
  166. {
  167. return ReadEmptyExtensionData(extensionData);
  168. }
  169. /// <exception cref="IOException"></exception>
  170. public static bool ReadExtendedMasterSecretExtension(byte[] extensionData)
  171. {
  172. return ReadEmptyExtensionData(extensionData);
  173. }
  174. /// <exception cref="IOException"></exception>
  175. public static HeartbeatExtension ReadHeartbeatExtension(byte[] extensionData)
  176. {
  177. if (extensionData == null)
  178. throw new ArgumentNullException("extensionData");
  179. MemoryStream buf = new MemoryStream(extensionData, false);
  180. HeartbeatExtension heartbeatExtension = HeartbeatExtension.Parse(buf);
  181. TlsProtocol.AssertEmpty(buf);
  182. return heartbeatExtension;
  183. }
  184. /// <exception cref="IOException"></exception>
  185. public static short ReadMaxFragmentLengthExtension(byte[] extensionData)
  186. {
  187. if (extensionData == null)
  188. throw new ArgumentNullException("extensionData");
  189. if (extensionData.Length != 1)
  190. throw new TlsFatalAlert(AlertDescription.decode_error);
  191. return extensionData[0];
  192. }
  193. /// <exception cref="IOException"></exception>
  194. public static int ReadPaddingExtension(byte[] extensionData)
  195. {
  196. if (extensionData == null)
  197. throw new ArgumentNullException("extensionData");
  198. for (int i = 0; i < extensionData.Length; ++i)
  199. {
  200. if (extensionData[i] != 0)
  201. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  202. }
  203. return extensionData.Length;
  204. }
  205. /// <exception cref="IOException"></exception>
  206. public static ServerNameList ReadServerNameExtension(byte[] extensionData)
  207. {
  208. if (extensionData == null)
  209. throw new ArgumentNullException("extensionData");
  210. MemoryStream buf = new MemoryStream(extensionData, false);
  211. ServerNameList serverNameList = ServerNameList.Parse(buf);
  212. TlsProtocol.AssertEmpty(buf);
  213. return serverNameList;
  214. }
  215. /// <exception cref="IOException"></exception>
  216. public static CertificateStatusRequest ReadStatusRequestExtension(byte[] extensionData)
  217. {
  218. if (extensionData == null)
  219. throw new ArgumentNullException("extensionData");
  220. MemoryStream buf = new MemoryStream(extensionData, false);
  221. CertificateStatusRequest statusRequest = CertificateStatusRequest.Parse(buf);
  222. TlsProtocol.AssertEmpty(buf);
  223. return statusRequest;
  224. }
  225. /// <exception cref="IOException"></exception>
  226. public static bool ReadTruncatedHMacExtension(byte[] extensionData)
  227. {
  228. return ReadEmptyExtensionData(extensionData);
  229. }
  230. }
  231. }
  232. #endif