CipherSuite.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <LibTLS/Extensions.h>
  9. namespace TLS {
  10. // Defined in RFC 5246 section 7.4.1.4.1
  11. struct SignatureAndHashAlgorithm {
  12. HashAlgorithm hash;
  13. SignatureAlgorithm signature;
  14. };
  15. enum class KeyExchangeAlgorithm {
  16. Invalid,
  17. // Defined in RFC 5246 section 7.4.2 / RFC 4279 section 4
  18. RSA_PSK,
  19. // Defined in RFC 5246 section 7.4.3
  20. DHE_DSS,
  21. DHE_RSA,
  22. DH_anon,
  23. RSA,
  24. DH_DSS,
  25. DH_RSA,
  26. // Defined in RFC 4492 section 2
  27. ECDHE_RSA,
  28. ECDH_ECDSA,
  29. ECDH_RSA,
  30. ECDHE_ECDSA,
  31. ECDH_anon,
  32. };
  33. // Defined in RFC 5246 section 7.4.1.4.1
  34. constexpr SignatureAlgorithm signature_for_key_exchange_algorithm(KeyExchangeAlgorithm algorithm)
  35. {
  36. switch (algorithm) {
  37. case KeyExchangeAlgorithm::RSA:
  38. case KeyExchangeAlgorithm::DHE_RSA:
  39. case KeyExchangeAlgorithm::DH_RSA:
  40. case KeyExchangeAlgorithm::RSA_PSK:
  41. case KeyExchangeAlgorithm::ECDH_RSA:
  42. case KeyExchangeAlgorithm::ECDHE_RSA:
  43. return SignatureAlgorithm::RSA;
  44. case KeyExchangeAlgorithm::DHE_DSS:
  45. case KeyExchangeAlgorithm::DH_DSS:
  46. return SignatureAlgorithm::DSA;
  47. case KeyExchangeAlgorithm::ECDH_ECDSA:
  48. case KeyExchangeAlgorithm::ECDHE_ECDSA:
  49. return SignatureAlgorithm::ECDSA;
  50. case KeyExchangeAlgorithm::DH_anon:
  51. case KeyExchangeAlgorithm::ECDH_anon:
  52. default:
  53. return SignatureAlgorithm::ANONYMOUS;
  54. }
  55. }
  56. enum class CipherAlgorithm {
  57. Invalid,
  58. AES_128_CBC,
  59. AES_128_GCM,
  60. AES_128_CCM,
  61. AES_128_CCM_8,
  62. AES_256_CBC,
  63. AES_256_GCM,
  64. };
  65. constexpr size_t cipher_key_size(CipherAlgorithm algorithm)
  66. {
  67. switch (algorithm) {
  68. case CipherAlgorithm::AES_128_CBC:
  69. case CipherAlgorithm::AES_128_GCM:
  70. case CipherAlgorithm::AES_128_CCM:
  71. case CipherAlgorithm::AES_128_CCM_8:
  72. return 128;
  73. case CipherAlgorithm::AES_256_CBC:
  74. case CipherAlgorithm::AES_256_GCM:
  75. return 256;
  76. case CipherAlgorithm::Invalid:
  77. default:
  78. return 0;
  79. }
  80. }
  81. }