DefaultTlsCipherFactory.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  8. {
  9. public class DefaultTlsCipherFactory
  10. : AbstractTlsCipherFactory
  11. {
  12. /// <exception cref="IOException"></exception>
  13. public override TlsCipher CreateCipher(TlsContext context, int encryptionAlgorithm, int macAlgorithm)
  14. {
  15. switch (encryptionAlgorithm)
  16. {
  17. case EncryptionAlgorithm.cls_3DES_EDE_CBC:
  18. return CreateDesEdeCipher(context, macAlgorithm);
  19. case EncryptionAlgorithm.AES_128_CBC:
  20. return CreateAESCipher(context, 16, macAlgorithm);
  21. case EncryptionAlgorithm.AES_128_CCM:
  22. // NOTE: Ignores macAlgorithm
  23. return CreateCipher_Aes_Ccm(context, 16, 16);
  24. case EncryptionAlgorithm.AES_128_CCM_8:
  25. // NOTE: Ignores macAlgorithm
  26. return CreateCipher_Aes_Ccm(context, 16, 8);
  27. case EncryptionAlgorithm.AES_128_GCM:
  28. // NOTE: Ignores macAlgorithm
  29. return CreateCipher_Aes_Gcm(context, 16, 16);
  30. case EncryptionAlgorithm.AES_128_OCB_TAGLEN96:
  31. // NOTE: Ignores macAlgorithm
  32. return CreateCipher_Aes_Ocb(context, 16, 12);
  33. case EncryptionAlgorithm.AES_256_CBC:
  34. return CreateAESCipher(context, 32, macAlgorithm);
  35. case EncryptionAlgorithm.AES_256_CCM:
  36. // NOTE: Ignores macAlgorithm
  37. return CreateCipher_Aes_Ccm(context, 32, 16);
  38. case EncryptionAlgorithm.AES_256_CCM_8:
  39. // NOTE: Ignores macAlgorithm
  40. return CreateCipher_Aes_Ccm(context, 32, 8);
  41. case EncryptionAlgorithm.AES_256_GCM:
  42. // NOTE: Ignores macAlgorithm
  43. return CreateCipher_Aes_Gcm(context, 32, 16);
  44. case EncryptionAlgorithm.AES_256_OCB_TAGLEN96:
  45. // NOTE: Ignores macAlgorithm
  46. return CreateCipher_Aes_Ocb(context, 32, 12);
  47. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  48. return CreateCamelliaCipher(context, 16, macAlgorithm);
  49. case EncryptionAlgorithm.CAMELLIA_128_GCM:
  50. // NOTE: Ignores macAlgorithm
  51. return CreateCipher_Camellia_Gcm(context, 16, 16);
  52. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  53. return CreateCamelliaCipher(context, 32, macAlgorithm);
  54. case EncryptionAlgorithm.CAMELLIA_256_GCM:
  55. // NOTE: Ignores macAlgorithm
  56. return CreateCipher_Camellia_Gcm(context, 32, 16);
  57. case EncryptionAlgorithm.CHACHA20_POLY1305:
  58. // NOTE: Ignores macAlgorithm
  59. return CreateChaCha20Poly1305(context);
  60. case EncryptionAlgorithm.NULL:
  61. return CreateNullCipher(context, macAlgorithm);
  62. case EncryptionAlgorithm.RC4_128:
  63. return CreateRC4Cipher(context, 16, macAlgorithm);
  64. case EncryptionAlgorithm.SEED_CBC:
  65. return CreateSeedCipher(context, macAlgorithm);
  66. default:
  67. throw new TlsFatalAlert(AlertDescription.internal_error);
  68. }
  69. }
  70. /// <exception cref="IOException"></exception>
  71. protected virtual TlsBlockCipher CreateAESCipher(TlsContext context, int cipherKeySize, int macAlgorithm)
  72. {
  73. return new TlsBlockCipher(context, CreateAesBlockCipher(), CreateAesBlockCipher(),
  74. CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), cipherKeySize);
  75. }
  76. /// <exception cref="IOException"></exception>
  77. protected virtual TlsBlockCipher CreateCamelliaCipher(TlsContext context, int cipherKeySize, int macAlgorithm)
  78. {
  79. return new TlsBlockCipher(context, CreateCamelliaBlockCipher(),
  80. CreateCamelliaBlockCipher(), CreateHMacDigest(macAlgorithm),
  81. CreateHMacDigest(macAlgorithm), cipherKeySize);
  82. }
  83. /// <exception cref="IOException"></exception>
  84. protected virtual TlsCipher CreateChaCha20Poly1305(TlsContext context)
  85. {
  86. return new Chacha20Poly1305(context);
  87. }
  88. /// <exception cref="IOException"></exception>
  89. protected virtual TlsAeadCipher CreateCipher_Aes_Ccm(TlsContext context, int cipherKeySize, int macSize)
  90. {
  91. return new TlsAeadCipher(context, CreateAeadBlockCipher_Aes_Ccm(),
  92. CreateAeadBlockCipher_Aes_Ccm(), cipherKeySize, macSize);
  93. }
  94. /// <exception cref="IOException"></exception>
  95. protected virtual TlsAeadCipher CreateCipher_Aes_Gcm(TlsContext context, int cipherKeySize, int macSize)
  96. {
  97. return new TlsAeadCipher(context, CreateAeadBlockCipher_Aes_Gcm(),
  98. CreateAeadBlockCipher_Aes_Gcm(), cipherKeySize, macSize);
  99. }
  100. /// <exception cref="IOException"></exception>
  101. protected virtual TlsAeadCipher CreateCipher_Aes_Ocb(TlsContext context, int cipherKeySize, int macSize)
  102. {
  103. return new TlsAeadCipher(context, CreateAeadBlockCipher_Aes_Ocb(),
  104. CreateAeadBlockCipher_Aes_Ocb(), cipherKeySize, macSize, TlsAeadCipher.NONCE_DRAFT_CHACHA20_POLY1305);
  105. }
  106. /// <exception cref="IOException"></exception>
  107. protected virtual TlsAeadCipher CreateCipher_Camellia_Gcm(TlsContext context, int cipherKeySize, int macSize)
  108. {
  109. return new TlsAeadCipher(context, CreateAeadBlockCipher_Camellia_Gcm(),
  110. CreateAeadBlockCipher_Camellia_Gcm(), cipherKeySize, macSize);
  111. }
  112. /// <exception cref="IOException"></exception>
  113. protected virtual TlsBlockCipher CreateDesEdeCipher(TlsContext context, int macAlgorithm)
  114. {
  115. return new TlsBlockCipher(context, CreateDesEdeBlockCipher(), CreateDesEdeBlockCipher(),
  116. CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), 24);
  117. }
  118. /// <exception cref="IOException"></exception>
  119. protected virtual TlsNullCipher CreateNullCipher(TlsContext context, int macAlgorithm)
  120. {
  121. return new TlsNullCipher(context, CreateHMacDigest(macAlgorithm),
  122. CreateHMacDigest(macAlgorithm));
  123. }
  124. /// <exception cref="IOException"></exception>
  125. protected virtual TlsStreamCipher CreateRC4Cipher(TlsContext context, int cipherKeySize, int macAlgorithm)
  126. {
  127. return new TlsStreamCipher(context, CreateRC4StreamCipher(), CreateRC4StreamCipher(),
  128. CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), cipherKeySize, false);
  129. }
  130. /// <exception cref="IOException"></exception>
  131. protected virtual TlsBlockCipher CreateSeedCipher(TlsContext context, int macAlgorithm)
  132. {
  133. return new TlsBlockCipher(context, CreateSeedBlockCipher(), CreateSeedBlockCipher(),
  134. CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), 16);
  135. }
  136. protected virtual IBlockCipher CreateAesEngine()
  137. {
  138. return new AesEngine();
  139. }
  140. protected virtual IBlockCipher CreateCamelliaEngine()
  141. {
  142. return new CamelliaEngine();
  143. }
  144. protected virtual IBlockCipher CreateAesBlockCipher()
  145. {
  146. return new CbcBlockCipher(CreateAesEngine());
  147. }
  148. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aes_Ccm()
  149. {
  150. return new CcmBlockCipher(CreateAesEngine());
  151. }
  152. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aes_Gcm()
  153. {
  154. // TODO Consider allowing custom configuration of multiplier
  155. return new GcmBlockCipher(CreateAesEngine());
  156. }
  157. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aes_Ocb()
  158. {
  159. return new OcbBlockCipher(CreateAesEngine(), CreateAesEngine());
  160. }
  161. protected virtual IAeadBlockCipher CreateAeadBlockCipher_Camellia_Gcm()
  162. {
  163. // TODO Consider allowing custom configuration of multiplier
  164. return new GcmBlockCipher(CreateCamelliaEngine());
  165. }
  166. protected virtual IBlockCipher CreateCamelliaBlockCipher()
  167. {
  168. return new CbcBlockCipher(CreateCamelliaEngine());
  169. }
  170. protected virtual IBlockCipher CreateDesEdeBlockCipher()
  171. {
  172. return new CbcBlockCipher(new DesEdeEngine());
  173. }
  174. protected virtual IStreamCipher CreateRC4StreamCipher()
  175. {
  176. return new RC4Engine();
  177. }
  178. protected virtual IBlockCipher CreateSeedBlockCipher()
  179. {
  180. return new CbcBlockCipher(new SeedEngine());
  181. }
  182. /// <exception cref="IOException"></exception>
  183. protected virtual IDigest CreateHMacDigest(int macAlgorithm)
  184. {
  185. switch (macAlgorithm)
  186. {
  187. case MacAlgorithm.cls_null:
  188. return null;
  189. case MacAlgorithm.hmac_md5:
  190. return TlsUtilities.CreateHash(HashAlgorithm.md5);
  191. case MacAlgorithm.hmac_sha1:
  192. return TlsUtilities.CreateHash(HashAlgorithm.sha1);
  193. case MacAlgorithm.hmac_sha256:
  194. return TlsUtilities.CreateHash(HashAlgorithm.sha256);
  195. case MacAlgorithm.hmac_sha384:
  196. return TlsUtilities.CreateHash(HashAlgorithm.sha384);
  197. case MacAlgorithm.hmac_sha512:
  198. return TlsUtilities.CreateHash(HashAlgorithm.sha512);
  199. default:
  200. throw new TlsFatalAlert(AlertDescription.internal_error);
  201. }
  202. }
  203. }
  204. }
  205. #pragma warning restore
  206. #endif