SSLOptions.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/container/Array.h>
  18. #include <folly/io/async/SSLContext.h>
  19. namespace folly {
  20. namespace ssl {
  21. namespace ssl_options_detail {
  22. void logDfatal(std::exception const&);
  23. } // namespace ssl_options_detail
  24. struct SSLCommonOptions {
  25. /**
  26. * The cipher list recommended for this options configuration.
  27. */
  28. static constexpr auto kCipherList = folly::make_array(
  29. "ECDHE-ECDSA-AES128-GCM-SHA256",
  30. "ECDHE-RSA-AES128-GCM-SHA256",
  31. "ECDHE-ECDSA-AES256-GCM-SHA384",
  32. "ECDHE-RSA-AES256-GCM-SHA384",
  33. "ECDHE-ECDSA-AES256-SHA",
  34. "ECDHE-RSA-AES256-SHA",
  35. "ECDHE-ECDSA-AES128-SHA",
  36. "ECDHE-RSA-AES128-SHA",
  37. "ECDHE-RSA-AES256-SHA384",
  38. "AES128-GCM-SHA256",
  39. "AES256-SHA",
  40. "AES128-SHA");
  41. /**
  42. * The list of signature algorithms recommended for this options
  43. * configuration.
  44. */
  45. static constexpr auto kSignatureAlgorithms = folly::make_array(
  46. "RSA+SHA512",
  47. "ECDSA+SHA512",
  48. "RSA+SHA384",
  49. "ECDSA+SHA384",
  50. "RSA+SHA256",
  51. "ECDSA+SHA256",
  52. "RSA+SHA1",
  53. "ECDSA+SHA1");
  54. /**
  55. * Set common parameters on a client SSL context, for example,
  56. * ciphers, signature algorithms, verification options, and client EC curves.
  57. * @param ctx The SSL Context to which to apply the options.
  58. */
  59. static void setClientOptions(SSLContext& ctx);
  60. };
  61. /**
  62. * Recommended SSL options for server-side scenario.
  63. */
  64. struct SSLServerOptions {
  65. /**
  66. * The list of ciphers recommended for server use.
  67. */
  68. static constexpr auto kCipherList = folly::make_array(
  69. "ECDHE-ECDSA-AES128-GCM-SHA256",
  70. "ECDHE-ECDSA-AES256-GCM-SHA384",
  71. "ECDHE-ECDSA-AES128-SHA",
  72. "ECDHE-ECDSA-AES256-SHA",
  73. "ECDHE-RSA-AES128-GCM-SHA256",
  74. "ECDHE-RSA-AES256-GCM-SHA384",
  75. "ECDHE-RSA-AES128-SHA",
  76. "ECDHE-RSA-AES256-SHA",
  77. "AES128-GCM-SHA256",
  78. "AES256-GCM-SHA384",
  79. "AES128-SHA",
  80. "AES256-SHA");
  81. };
  82. /**
  83. * Set the cipher suite of ctx to that in TSSLOptions, and print any runtime
  84. * error it catches.
  85. * @param ctx The SSLContext to apply the desired SSL options to.
  86. */
  87. template <typename TSSLOptions>
  88. void setCipherSuites(SSLContext& ctx) {
  89. try {
  90. ctx.setCipherList(TSSLOptions::kCipherList);
  91. } catch (std::runtime_error const& e) {
  92. ssl_options_detail::logDfatal(e);
  93. }
  94. }
  95. /**
  96. * Set the signature algorithm list of ctx to that in TSSLOptions, and print
  97. * any runtime errors it catche.
  98. * @param ctx The SSLContext to apply the desired SSL options to.
  99. */
  100. template <typename TSSLOptions>
  101. void setSignatureAlgorithms(SSLContext& ctx) {
  102. try {
  103. ctx.setSignatureAlgorithms(TSSLOptions::kSignatureAlgorithms);
  104. } catch (std::runtime_error const& e) {
  105. ssl_options_detail::logDfatal(e);
  106. }
  107. }
  108. } // namespace ssl
  109. } // namespace folly