94dc8ed188f4968f0d35e84546d96940a72fb434
[folly.git] / folly / io / async / SSLOptions.h
1 /*
2  * Copyright 2004-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
17 #pragma once
18
19 #include <folly/Array.h>
20 #include <folly/io/async/SSLContext.h>
21
22 namespace folly {
23 namespace ssl {
24
25 namespace ssl_options_detail {
26 void logDfatal(std::exception const&);
27 }
28
29 struct SSLCommonOptions {
30   /**
31    * The cipher list recommended for this options configuration.
32    */
33   static constexpr auto kCipherList = folly::make_array(
34       "ECDHE-ECDSA-AES128-GCM-SHA256",
35       "ECDHE-RSA-AES128-GCM-SHA256",
36       "ECDHE-ECDSA-AES256-GCM-SHA384",
37       "ECDHE-RSA-AES256-GCM-SHA384",
38       "ECDHE-ECDSA-AES256-SHA",
39       "ECDHE-RSA-AES256-SHA",
40       "ECDHE-ECDSA-AES128-SHA",
41       "ECDHE-RSA-AES128-SHA",
42       "ECDHE-RSA-AES256-SHA384",
43       "AES128-GCM-SHA256",
44       "AES256-SHA",
45       "AES128-SHA");
46
47   /**
48    * The list of signature algorithms recommended for this options
49    * configuration.
50    */
51   static constexpr auto kSignatureAlgorithms = folly::make_array(
52       "RSA+SHA512",
53       "ECDSA+SHA512",
54       "RSA+SHA384",
55       "ECDSA+SHA384",
56       "RSA+SHA256",
57       "ECDSA+SHA256",
58       "RSA+SHA1",
59       "ECDSA+SHA1");
60
61   /**
62    * Set common parameters on a client SSL context, for example,
63    * ciphers, signature algorithms, verification options, and client EC curves.
64    * @param ctx The SSL Context to which to apply the options.
65    */
66   static void setClientOptions(SSLContext& ctx);
67 };
68
69 /**
70  * Set the cipher suite of ctx to that in TSSLOptions, and print any runtime
71  * error it catches.
72  * @param ctx The SSLContext to apply the desired SSL options to.
73  */
74 template <typename TSSLOptions>
75 void setCipherSuites(SSLContext& ctx) {
76   try {
77     ctx.setCipherList(TSSLOptions::kCipherList);
78   } catch (std::runtime_error const& e) {
79     ssl_options_detail::logDfatal(e);
80   }
81 }
82
83 /**
84  * Set the signature algorithm list of ctx to that in TSSLOptions, and print
85  * any runtime errors it catche.
86  * @param ctx The SSLContext to apply the desired SSL options to.
87  */
88 template <typename TSSLOptions>
89 void setSignatureAlgorithms(SSLContext& ctx) {
90   try {
91     ctx.setSignatureAlgorithms(TSSLOptions::kSignatureAlgorithms);
92   } catch (std::runtime_error const& e) {
93     ssl_options_detail::logDfatal(e);
94   }
95 }
96
97 } // namespace ssl
98 } // namespace folly