BcTlsCrypto.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Srp;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  14. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  15. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  16. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  17. {
  18. /**
  19. * Class for providing cryptographic services for TLS based on implementations in the BC light-weight API.
  20. * <p>
  21. * This class provides default implementations for everything. If you need to customise it, extend the class
  22. * and override the appropriate methods.
  23. * </p>
  24. */
  25. public class BcTlsCrypto
  26. : AbstractTlsCrypto
  27. {
  28. private readonly SecureRandom m_entropySource;
  29. public BcTlsCrypto()
  30. : this(CryptoServicesRegistrar.GetSecureRandom())
  31. {
  32. }
  33. public BcTlsCrypto(SecureRandom entropySource)
  34. {
  35. if (entropySource == null)
  36. throw new ArgumentNullException(nameof(entropySource));
  37. this.m_entropySource = entropySource;
  38. }
  39. internal virtual BcTlsSecret AdoptLocalSecret(byte[] data)
  40. {
  41. return new BcTlsSecret(this, data);
  42. }
  43. public override SecureRandom SecureRandom
  44. {
  45. get { return m_entropySource; }
  46. }
  47. public override TlsCertificate CreateCertificate(short type, byte[] encoding)
  48. {
  49. switch (type)
  50. {
  51. case CertificateType.X509:
  52. return new BcTlsCertificate(this, encoding);
  53. case CertificateType.RawPublicKey:
  54. return new BcTlsRawKeyCertificate(this, encoding);
  55. default:
  56. throw new TlsFatalAlert(AlertDescription.internal_error);
  57. }
  58. }
  59. public override TlsCipher CreateCipher(TlsCryptoParameters cryptoParams, int encryptionAlgorithm,
  60. int macAlgorithm)
  61. {
  62. switch (encryptionAlgorithm)
  63. {
  64. case EncryptionAlgorithm.AES_128_CBC:
  65. case EncryptionAlgorithm.ARIA_128_CBC:
  66. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  67. case EncryptionAlgorithm.SEED_CBC:
  68. case EncryptionAlgorithm.SM4_CBC:
  69. return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 16, macAlgorithm);
  70. case EncryptionAlgorithm.cls_3DES_EDE_CBC:
  71. return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 24, macAlgorithm);
  72. case EncryptionAlgorithm.AES_256_CBC:
  73. case EncryptionAlgorithm.ARIA_256_CBC:
  74. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  75. return CreateCipher_Cbc(cryptoParams, encryptionAlgorithm, 32, macAlgorithm);
  76. case EncryptionAlgorithm.AES_128_CCM:
  77. // NOTE: Ignores macAlgorithm
  78. return CreateCipher_Aes_Ccm(cryptoParams, 16, 16);
  79. case EncryptionAlgorithm.AES_128_CCM_8:
  80. // NOTE: Ignores macAlgorithm
  81. return CreateCipher_Aes_Ccm(cryptoParams, 16, 8);
  82. case EncryptionAlgorithm.AES_128_GCM:
  83. // NOTE: Ignores macAlgorithm
  84. return CreateCipher_Aes_Gcm(cryptoParams, 16, 16);
  85. case EncryptionAlgorithm.AES_256_CCM:
  86. // NOTE: Ignores macAlgorithm
  87. return CreateCipher_Aes_Ccm(cryptoParams, 32, 16);
  88. case EncryptionAlgorithm.AES_256_CCM_8:
  89. // NOTE: Ignores macAlgorithm
  90. return CreateCipher_Aes_Ccm(cryptoParams, 32, 8);
  91. case EncryptionAlgorithm.AES_256_GCM:
  92. // NOTE: Ignores macAlgorithm
  93. return CreateCipher_Aes_Gcm(cryptoParams, 32, 16);
  94. case EncryptionAlgorithm.ARIA_128_GCM:
  95. // NOTE: Ignores macAlgorithm
  96. return CreateCipher_Aria_Gcm(cryptoParams, 16, 16);
  97. case EncryptionAlgorithm.ARIA_256_GCM:
  98. // NOTE: Ignores macAlgorithm
  99. return CreateCipher_Aria_Gcm(cryptoParams, 32, 16);
  100. case EncryptionAlgorithm.CAMELLIA_128_GCM:
  101. // NOTE: Ignores macAlgorithm
  102. return CreateCipher_Camellia_Gcm(cryptoParams, 16, 16);
  103. case EncryptionAlgorithm.CAMELLIA_256_GCM:
  104. // NOTE: Ignores macAlgorithm
  105. return CreateCipher_Camellia_Gcm(cryptoParams, 32, 16);
  106. case EncryptionAlgorithm.CHACHA20_POLY1305:
  107. // NOTE: Ignores macAlgorithm
  108. return CreateChaCha20Poly1305(cryptoParams);
  109. case EncryptionAlgorithm.NULL:
  110. return CreateNullCipher(cryptoParams, macAlgorithm);
  111. case EncryptionAlgorithm.SM4_CCM:
  112. // NOTE: Ignores macAlgorithm
  113. return CreateCipher_SM4_Ccm(cryptoParams);
  114. case EncryptionAlgorithm.SM4_GCM:
  115. // NOTE: Ignores macAlgorithm
  116. return CreateCipher_SM4_Gcm(cryptoParams);
  117. case EncryptionAlgorithm.DES40_CBC:
  118. case EncryptionAlgorithm.DES_CBC:
  119. case EncryptionAlgorithm.IDEA_CBC:
  120. case EncryptionAlgorithm.RC2_CBC_40:
  121. case EncryptionAlgorithm.RC4_128:
  122. case EncryptionAlgorithm.RC4_40:
  123. default:
  124. throw new TlsFatalAlert(AlertDescription.internal_error);
  125. }
  126. }
  127. public override TlsDHDomain CreateDHDomain(TlsDHConfig dhConfig)
  128. {
  129. return new BcTlsDHDomain(this, dhConfig);
  130. }
  131. public override TlsECDomain CreateECDomain(TlsECConfig ecConfig)
  132. {
  133. switch (ecConfig.NamedGroup)
  134. {
  135. case NamedGroup.x25519:
  136. return new BcX25519Domain(this);
  137. case NamedGroup.x448:
  138. return new BcX448Domain(this);
  139. default:
  140. return new BcTlsECDomain(this, ecConfig);
  141. }
  142. }
  143. public override TlsNonceGenerator CreateNonceGenerator(byte[] additionalSeedMaterial)
  144. {
  145. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  146. return CreateNonceGenerator(Spans.FromNullableReadOnly(additionalSeedMaterial));
  147. #else
  148. int cryptoHashAlgorithm = CryptoHashAlgorithm.sha256;
  149. IDigest digest = CreateDigest(cryptoHashAlgorithm);
  150. int seedLength = TlsCryptoUtilities.GetHashOutputSize(cryptoHashAlgorithm);
  151. byte[] seed = new byte[seedLength];
  152. SecureRandom.NextBytes(seed);
  153. DigestRandomGenerator randomGenerator = new DigestRandomGenerator(digest);
  154. randomGenerator.AddSeedMaterial(additionalSeedMaterial);
  155. randomGenerator.AddSeedMaterial(seed);
  156. return new BcTlsNonceGenerator(randomGenerator);
  157. #endif
  158. }
  159. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  160. public override TlsNonceGenerator CreateNonceGenerator(ReadOnlySpan<byte> additionalSeedMaterial)
  161. {
  162. int cryptoHashAlgorithm = CryptoHashAlgorithm.sha256;
  163. IDigest digest = CreateDigest(cryptoHashAlgorithm);
  164. int seedLength = TlsCryptoUtilities.GetHashOutputSize(cryptoHashAlgorithm);
  165. Span<byte> seed = seedLength <= 128
  166. ? stackalloc byte[seedLength]
  167. : new byte[seedLength];
  168. SecureRandom.NextBytes(seed);
  169. DigestRandomGenerator randomGenerator = new DigestRandomGenerator(digest);
  170. randomGenerator.AddSeedMaterial(additionalSeedMaterial);
  171. randomGenerator.AddSeedMaterial(seed);
  172. return new BcTlsNonceGenerator(randomGenerator);
  173. }
  174. #endif
  175. public override bool HasAnyStreamVerifiers(IList<SignatureAndHashAlgorithm> signatureAndHashAlgorithms)
  176. {
  177. foreach (SignatureAndHashAlgorithm algorithm in signatureAndHashAlgorithms)
  178. {
  179. switch (SignatureScheme.From(algorithm))
  180. {
  181. case SignatureScheme.ed25519:
  182. case SignatureScheme.ed448:
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. public override bool HasAnyStreamVerifiersLegacy(short[] clientCertificateTypes)
  189. {
  190. return false;
  191. }
  192. public override bool HasCryptoHashAlgorithm(int cryptoHashAlgorithm)
  193. {
  194. switch (cryptoHashAlgorithm)
  195. {
  196. case CryptoHashAlgorithm.md5:
  197. case CryptoHashAlgorithm.sha1:
  198. case CryptoHashAlgorithm.sha224:
  199. case CryptoHashAlgorithm.sha256:
  200. case CryptoHashAlgorithm.sha384:
  201. case CryptoHashAlgorithm.sha512:
  202. case CryptoHashAlgorithm.sm3:
  203. return true;
  204. default:
  205. return false;
  206. }
  207. }
  208. public override bool HasCryptoSignatureAlgorithm(int cryptoSignatureAlgorithm)
  209. {
  210. switch (cryptoSignatureAlgorithm)
  211. {
  212. case CryptoSignatureAlgorithm.rsa:
  213. case CryptoSignatureAlgorithm.dsa:
  214. case CryptoSignatureAlgorithm.ecdsa:
  215. case CryptoSignatureAlgorithm.rsa_pss_rsae_sha256:
  216. case CryptoSignatureAlgorithm.rsa_pss_rsae_sha384:
  217. case CryptoSignatureAlgorithm.rsa_pss_rsae_sha512:
  218. case CryptoSignatureAlgorithm.ed25519:
  219. case CryptoSignatureAlgorithm.ed448:
  220. case CryptoSignatureAlgorithm.rsa_pss_pss_sha256:
  221. case CryptoSignatureAlgorithm.rsa_pss_pss_sha384:
  222. case CryptoSignatureAlgorithm.rsa_pss_pss_sha512:
  223. return true;
  224. // TODO[draft-smyshlyaev-tls12-gost-suites-10]
  225. case CryptoSignatureAlgorithm.gostr34102012_256:
  226. case CryptoSignatureAlgorithm.gostr34102012_512:
  227. // TODO[RFC 8998]
  228. case CryptoSignatureAlgorithm.sm2:
  229. default:
  230. return false;
  231. }
  232. }
  233. public override bool HasDHAgreement()
  234. {
  235. return true;
  236. }
  237. public override bool HasECDHAgreement()
  238. {
  239. return true;
  240. }
  241. public override bool HasEncryptionAlgorithm(int encryptionAlgorithm)
  242. {
  243. switch (encryptionAlgorithm)
  244. {
  245. case EncryptionAlgorithm.AES_128_CBC:
  246. case EncryptionAlgorithm.AES_128_CCM:
  247. case EncryptionAlgorithm.AES_128_CCM_8:
  248. case EncryptionAlgorithm.AES_128_GCM:
  249. case EncryptionAlgorithm.AES_256_CBC:
  250. case EncryptionAlgorithm.AES_256_CCM:
  251. case EncryptionAlgorithm.AES_256_CCM_8:
  252. case EncryptionAlgorithm.AES_256_GCM:
  253. case EncryptionAlgorithm.ARIA_128_CBC:
  254. case EncryptionAlgorithm.ARIA_128_GCM:
  255. case EncryptionAlgorithm.ARIA_256_CBC:
  256. case EncryptionAlgorithm.ARIA_256_GCM:
  257. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  258. case EncryptionAlgorithm.CAMELLIA_128_GCM:
  259. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  260. case EncryptionAlgorithm.CAMELLIA_256_GCM:
  261. case EncryptionAlgorithm.CHACHA20_POLY1305:
  262. case EncryptionAlgorithm.cls_3DES_EDE_CBC:
  263. case EncryptionAlgorithm.NULL:
  264. case EncryptionAlgorithm.SEED_CBC:
  265. case EncryptionAlgorithm.SM4_CBC:
  266. case EncryptionAlgorithm.SM4_CCM:
  267. case EncryptionAlgorithm.SM4_GCM:
  268. return true;
  269. case EncryptionAlgorithm.DES_CBC:
  270. case EncryptionAlgorithm.DES40_CBC:
  271. case EncryptionAlgorithm.IDEA_CBC:
  272. case EncryptionAlgorithm.RC2_CBC_40:
  273. case EncryptionAlgorithm.RC4_128:
  274. case EncryptionAlgorithm.RC4_40:
  275. default:
  276. return false;
  277. }
  278. }
  279. public override bool HasHkdfAlgorithm(int cryptoHashAlgorithm)
  280. {
  281. switch (cryptoHashAlgorithm)
  282. {
  283. case CryptoHashAlgorithm.sha256:
  284. case CryptoHashAlgorithm.sha384:
  285. case CryptoHashAlgorithm.sha512:
  286. case CryptoHashAlgorithm.sm3:
  287. return true;
  288. default:
  289. return false;
  290. }
  291. }
  292. public override bool HasMacAlgorithm(int macAlgorithm)
  293. {
  294. switch (macAlgorithm)
  295. {
  296. case MacAlgorithm.hmac_md5:
  297. case MacAlgorithm.hmac_sha1:
  298. case MacAlgorithm.hmac_sha256:
  299. case MacAlgorithm.hmac_sha384:
  300. case MacAlgorithm.hmac_sha512:
  301. return true;
  302. default:
  303. return false;
  304. }
  305. }
  306. public override bool HasNamedGroup(int namedGroup)
  307. {
  308. return NamedGroup.RefersToASpecificGroup(namedGroup);
  309. }
  310. public override bool HasRsaEncryption()
  311. {
  312. return true;
  313. }
  314. public override bool HasSignatureAlgorithm(short signatureAlgorithm)
  315. {
  316. switch (signatureAlgorithm)
  317. {
  318. case SignatureAlgorithm.rsa:
  319. case SignatureAlgorithm.dsa:
  320. case SignatureAlgorithm.ecdsa:
  321. case SignatureAlgorithm.ed25519:
  322. case SignatureAlgorithm.ed448:
  323. case SignatureAlgorithm.rsa_pss_rsae_sha256:
  324. case SignatureAlgorithm.rsa_pss_rsae_sha384:
  325. case SignatureAlgorithm.rsa_pss_rsae_sha512:
  326. case SignatureAlgorithm.rsa_pss_pss_sha256:
  327. case SignatureAlgorithm.rsa_pss_pss_sha384:
  328. case SignatureAlgorithm.rsa_pss_pss_sha512:
  329. case SignatureAlgorithm.ecdsa_brainpoolP256r1tls13_sha256:
  330. case SignatureAlgorithm.ecdsa_brainpoolP384r1tls13_sha384:
  331. case SignatureAlgorithm.ecdsa_brainpoolP512r1tls13_sha512:
  332. return true;
  333. // TODO[draft-smyshlyaev-tls12-gost-suites-10]
  334. case SignatureAlgorithm.gostr34102012_256:
  335. case SignatureAlgorithm.gostr34102012_512:
  336. // TODO[RFC 8998]
  337. //case SignatureAlgorithm.sm2:
  338. default:
  339. return false;
  340. }
  341. }
  342. public override bool HasSignatureAndHashAlgorithm(SignatureAndHashAlgorithm sigAndHashAlgorithm)
  343. {
  344. short signature = sigAndHashAlgorithm.Signature;
  345. switch (sigAndHashAlgorithm.Hash)
  346. {
  347. case HashAlgorithm.md5:
  348. return SignatureAlgorithm.rsa == signature && HasSignatureAlgorithm(signature);
  349. default:
  350. return HasSignatureAlgorithm(signature);
  351. }
  352. }
  353. public override bool HasSignatureScheme(int signatureScheme)
  354. {
  355. switch (signatureScheme)
  356. {
  357. case SignatureScheme.sm2sig_sm3:
  358. return false;
  359. default:
  360. {
  361. short signature = SignatureScheme.GetSignatureAlgorithm(signatureScheme);
  362. switch(SignatureScheme.GetCryptoHashAlgorithm(signatureScheme))
  363. {
  364. case CryptoHashAlgorithm.md5:
  365. return SignatureAlgorithm.rsa == signature && HasSignatureAlgorithm(signature);
  366. default:
  367. return HasSignatureAlgorithm(signature);
  368. }
  369. }
  370. }
  371. }
  372. public override bool HasSrpAuthentication()
  373. {
  374. return true;
  375. }
  376. public override TlsSecret CreateSecret(byte[] data)
  377. {
  378. try
  379. {
  380. return AdoptLocalSecret(Arrays.Clone(data));
  381. }
  382. finally
  383. {
  384. // TODO[tls-ops] Add this after checking all callers
  385. //if (data != null)
  386. //{
  387. // Array.Clear(data, 0, data.Length);
  388. //}
  389. }
  390. }
  391. public override TlsSecret GenerateRsaPreMasterSecret(ProtocolVersion version)
  392. {
  393. byte[] data = new byte[48];
  394. SecureRandom.NextBytes(data);
  395. TlsUtilities.WriteVersion(version, data, 0);
  396. return AdoptLocalSecret(data);
  397. }
  398. public virtual IDigest CloneDigest(int cryptoHashAlgorithm, IDigest digest)
  399. {
  400. switch (cryptoHashAlgorithm)
  401. {
  402. case CryptoHashAlgorithm.md5:
  403. return new MD5Digest((MD5Digest)digest);
  404. case CryptoHashAlgorithm.sha1:
  405. return new Sha1Digest((Sha1Digest)digest);
  406. case CryptoHashAlgorithm.sha224:
  407. return new Sha224Digest((Sha224Digest)digest);
  408. case CryptoHashAlgorithm.sha256:
  409. return new Sha256Digest((Sha256Digest)digest);
  410. case CryptoHashAlgorithm.sha384:
  411. return new Sha384Digest((Sha384Digest)digest);
  412. case CryptoHashAlgorithm.sha512:
  413. return new Sha512Digest((Sha512Digest)digest);
  414. case CryptoHashAlgorithm.sm3:
  415. return new SM3Digest((SM3Digest)digest);
  416. default:
  417. throw new ArgumentException("invalid CryptoHashAlgorithm: " + cryptoHashAlgorithm);
  418. }
  419. }
  420. public virtual IDigest CreateDigest(int cryptoHashAlgorithm)
  421. {
  422. switch (cryptoHashAlgorithm)
  423. {
  424. case CryptoHashAlgorithm.md5:
  425. return new MD5Digest();
  426. case CryptoHashAlgorithm.sha1:
  427. return new Sha1Digest();
  428. case CryptoHashAlgorithm.sha224:
  429. return new Sha224Digest();
  430. case CryptoHashAlgorithm.sha256:
  431. return new Sha256Digest();
  432. case CryptoHashAlgorithm.sha384:
  433. return new Sha384Digest();
  434. case CryptoHashAlgorithm.sha512:
  435. return new Sha512Digest();
  436. case CryptoHashAlgorithm.sm3:
  437. return new SM3Digest();
  438. default:
  439. throw new ArgumentException("invalid CryptoHashAlgorithm: " + cryptoHashAlgorithm);
  440. }
  441. }
  442. public override TlsHash CreateHash(int cryptoHashAlgorithm)
  443. {
  444. return new BcTlsHash(this, cryptoHashAlgorithm);
  445. }
  446. protected virtual IBlockCipher CreateBlockCipher(int encryptionAlgorithm)
  447. {
  448. switch (encryptionAlgorithm)
  449. {
  450. case EncryptionAlgorithm.cls_3DES_EDE_CBC:
  451. return CreateDesEdeEngine();
  452. case EncryptionAlgorithm.AES_128_CBC:
  453. case EncryptionAlgorithm.AES_256_CBC:
  454. return CreateAesEngine();
  455. case EncryptionAlgorithm.ARIA_128_CBC:
  456. case EncryptionAlgorithm.ARIA_256_CBC:
  457. return CreateAriaEngine();
  458. case EncryptionAlgorithm.CAMELLIA_128_CBC:
  459. case EncryptionAlgorithm.CAMELLIA_256_CBC:
  460. return CreateCamelliaEngine();
  461. case EncryptionAlgorithm.SEED_CBC:
  462. return CreateSeedEngine();
  463. case EncryptionAlgorithm.SM4_CBC:
  464. return CreateSM4Engine();
  465. default:
  466. throw new TlsFatalAlert(AlertDescription.internal_error);
  467. }
  468. }
  469. protected virtual IBlockCipher CreateCbcBlockCipher(IBlockCipher blockCipher)
  470. {
  471. return new CbcBlockCipher(blockCipher);
  472. }
  473. protected virtual IBlockCipher CreateCbcBlockCipher(int encryptionAlgorithm)
  474. {
  475. return CreateCbcBlockCipher(CreateBlockCipher(encryptionAlgorithm));
  476. }
  477. protected virtual TlsCipher CreateChaCha20Poly1305(TlsCryptoParameters cryptoParams)
  478. {
  479. BcChaCha20Poly1305 encrypt = new BcChaCha20Poly1305(true);
  480. BcChaCha20Poly1305 decrypt = new BcChaCha20Poly1305(false);
  481. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, 32, 16, TlsAeadCipher.AEAD_CHACHA20_POLY1305);
  482. }
  483. protected virtual TlsAeadCipher CreateCipher_Aes_Ccm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  484. int macSize)
  485. {
  486. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), true);
  487. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Aes_Ccm(), false);
  488. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_CCM);
  489. }
  490. protected virtual TlsAeadCipher CreateCipher_Aes_Gcm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  491. int macSize)
  492. {
  493. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Aes_Gcm(), true);
  494. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Aes_Gcm(), false);
  495. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_GCM);
  496. }
  497. protected virtual TlsAeadCipher CreateCipher_Aria_Gcm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  498. int macSize)
  499. {
  500. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Aria_Gcm(), true);
  501. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Aria_Gcm(), false);
  502. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_GCM);
  503. }
  504. protected virtual TlsAeadCipher CreateCipher_Camellia_Gcm(TlsCryptoParameters cryptoParams, int cipherKeySize,
  505. int macSize)
  506. {
  507. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Camellia_Gcm(), true);
  508. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_Camellia_Gcm(), false);
  509. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAeadCipher.AEAD_GCM);
  510. }
  511. protected virtual TlsCipher CreateCipher_Cbc(TlsCryptoParameters cryptoParams, int encryptionAlgorithm,
  512. int cipherKeySize, int macAlgorithm)
  513. {
  514. BcTlsBlockCipherImpl encrypt = new BcTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), true);
  515. BcTlsBlockCipherImpl decrypt = new BcTlsBlockCipherImpl(CreateCbcBlockCipher(encryptionAlgorithm), false);
  516. TlsHmac clientMac = CreateMac(cryptoParams, macAlgorithm);
  517. TlsHmac serverMac = CreateMac(cryptoParams, macAlgorithm);
  518. return new TlsBlockCipher(cryptoParams, encrypt, decrypt, clientMac, serverMac, cipherKeySize);
  519. }
  520. protected virtual TlsAeadCipher CreateCipher_SM4_Ccm(TlsCryptoParameters cryptoParams)
  521. {
  522. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_SM4_Ccm(), true);
  523. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_SM4_Ccm(), false);
  524. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAeadCipher.AEAD_CCM);
  525. }
  526. protected virtual TlsAeadCipher CreateCipher_SM4_Gcm(TlsCryptoParameters cryptoParams)
  527. {
  528. BcTlsAeadCipherImpl encrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_SM4_Gcm(), true);
  529. BcTlsAeadCipherImpl decrypt = new BcTlsAeadCipherImpl(CreateAeadCipher_SM4_Gcm(), false);
  530. return new TlsAeadCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAeadCipher.AEAD_GCM);
  531. }
  532. protected virtual TlsNullCipher CreateNullCipher(TlsCryptoParameters cryptoParams, int macAlgorithm)
  533. {
  534. return new TlsNullCipher(cryptoParams, CreateMac(cryptoParams, macAlgorithm),
  535. CreateMac(cryptoParams, macAlgorithm));
  536. }
  537. protected virtual IBlockCipher CreateAesEngine()
  538. {
  539. return AesUtilities.CreateEngine();
  540. }
  541. protected virtual IBlockCipher CreateAriaEngine()
  542. {
  543. return new AriaEngine();
  544. }
  545. protected virtual IBlockCipher CreateCamelliaEngine()
  546. {
  547. return new CamelliaEngine();
  548. }
  549. protected virtual IBlockCipher CreateDesEdeEngine()
  550. {
  551. return new DesEdeEngine();
  552. }
  553. protected virtual IBlockCipher CreateSeedEngine()
  554. {
  555. return new SeedEngine();
  556. }
  557. protected virtual IBlockCipher CreateSM4Engine()
  558. {
  559. return new SM4Engine();
  560. }
  561. protected virtual IAeadCipher CreateCcmMode(IBlockCipher engine)
  562. {
  563. return new CcmBlockCipher(engine);
  564. }
  565. protected virtual IAeadCipher CreateGcmMode(IBlockCipher engine)
  566. {
  567. // TODO Consider allowing custom configuration of multiplier
  568. return new GcmBlockCipher(engine);
  569. }
  570. protected virtual IAeadCipher CreateAeadCipher_Aes_Ccm()
  571. {
  572. return CreateCcmMode(CreateAesEngine());
  573. }
  574. protected virtual IAeadCipher CreateAeadCipher_Aes_Gcm()
  575. {
  576. return CreateGcmMode(CreateAesEngine());
  577. }
  578. protected virtual IAeadCipher CreateAeadCipher_Aria_Gcm()
  579. {
  580. return CreateGcmMode(CreateAriaEngine());
  581. }
  582. protected virtual IAeadCipher CreateAeadCipher_Camellia_Gcm()
  583. {
  584. return CreateGcmMode(CreateCamelliaEngine());
  585. }
  586. protected virtual IAeadCipher CreateAeadCipher_SM4_Ccm()
  587. {
  588. return CreateCcmMode(CreateSM4Engine());
  589. }
  590. protected virtual IAeadCipher CreateAeadCipher_SM4_Gcm()
  591. {
  592. return CreateGcmMode(CreateSM4Engine());
  593. }
  594. public override TlsHmac CreateHmac(int macAlgorithm)
  595. {
  596. switch (macAlgorithm)
  597. {
  598. case MacAlgorithm.hmac_md5:
  599. case MacAlgorithm.hmac_sha1:
  600. case MacAlgorithm.hmac_sha256:
  601. case MacAlgorithm.hmac_sha384:
  602. case MacAlgorithm.hmac_sha512:
  603. return CreateHmacForHash(TlsCryptoUtilities.GetHashForHmac(macAlgorithm));
  604. default:
  605. throw new ArgumentException("invalid MacAlgorithm: " + macAlgorithm);
  606. }
  607. }
  608. public override TlsHmac CreateHmacForHash(int cryptoHashAlgorithm)
  609. {
  610. return new BcTlsHmac(new HMac(CreateDigest(cryptoHashAlgorithm)));
  611. }
  612. protected virtual TlsHmac CreateHmac_Ssl(int macAlgorithm)
  613. {
  614. switch (macAlgorithm)
  615. {
  616. case MacAlgorithm.hmac_md5:
  617. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.md5));
  618. case MacAlgorithm.hmac_sha1:
  619. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha1));
  620. case MacAlgorithm.hmac_sha256:
  621. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha256));
  622. case MacAlgorithm.hmac_sha384:
  623. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha384));
  624. case MacAlgorithm.hmac_sha512:
  625. return new BcSsl3Hmac(CreateDigest(CryptoHashAlgorithm.sha512));
  626. default:
  627. throw new TlsFatalAlert(AlertDescription.internal_error);
  628. }
  629. }
  630. protected virtual TlsHmac CreateMac(TlsCryptoParameters cryptoParams, int macAlgorithm)
  631. {
  632. if (TlsImplUtilities.IsSsl(cryptoParams))
  633. {
  634. return CreateHmac_Ssl(macAlgorithm);
  635. }
  636. else
  637. {
  638. return CreateHmac(macAlgorithm);
  639. }
  640. }
  641. public override TlsSrp6Client CreateSrp6Client(TlsSrpConfig srpConfig)
  642. {
  643. BigInteger[] ng = srpConfig.GetExplicitNG();
  644. Srp6GroupParameters srpGroup = new Srp6GroupParameters(ng[0], ng[1]);
  645. Srp6Client srp6Client = new Srp6Client();
  646. srp6Client.Init(srpGroup, CreateDigest(CryptoHashAlgorithm.sha1), SecureRandom);
  647. return new BcTlsSrp6Client(srp6Client);
  648. }
  649. public override TlsSrp6Server CreateSrp6Server(TlsSrpConfig srpConfig, BigInteger srpVerifier)
  650. {
  651. BigInteger[] ng = srpConfig.GetExplicitNG();
  652. Srp6GroupParameters srpGroup = new Srp6GroupParameters(ng[0], ng[1]);
  653. Srp6Server srp6Server = new Srp6Server();
  654. srp6Server.Init(srpGroup, srpVerifier, CreateDigest(CryptoHashAlgorithm.sha1), SecureRandom);
  655. return new BcTlsSrp6Server(srp6Server);
  656. }
  657. public override TlsSrp6VerifierGenerator CreateSrp6VerifierGenerator(TlsSrpConfig srpConfig)
  658. {
  659. BigInteger[] ng = srpConfig.GetExplicitNG();
  660. Srp6VerifierGenerator srp6VerifierGenerator = new Srp6VerifierGenerator();
  661. srp6VerifierGenerator.Init(ng[0], ng[1], CreateDigest(CryptoHashAlgorithm.sha1));
  662. return new BcTlsSrp6VerifierGenerator(srp6VerifierGenerator);
  663. }
  664. public override TlsSecret HkdfInit(int cryptoHashAlgorithm)
  665. {
  666. return AdoptLocalSecret(new byte[TlsCryptoUtilities.GetHashOutputSize(cryptoHashAlgorithm)]);
  667. }
  668. }
  669. }
  670. #pragma warning restore
  671. #endif