1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 #ifndef GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_SSL_SSL_CREDENTIALS_H
19 #define GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_SSL_SSL_CREDENTIALS_H
20 
21 #include <grpc/support/port_platform.h>
22 
23 #include <stddef.h>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/grpc_security.h>
27 #include <grpc/grpc_security_constants.h>
28 #include <grpc/support/log.h>
29 
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/gprpp/ref_counted_ptr.h"
33 #include "src/core/lib/gprpp/unique_type_name.h"
34 #include "src/core/lib/security/credentials/credentials.h"
35 #include "src/core/lib/security/security_connector/security_connector.h"
36 #include "src/core/lib/security/security_connector/ssl/ssl_security_connector.h"
37 #include "src/core/tsi/ssl_transport_security.h"
38 
39 class grpc_ssl_credentials : public grpc_channel_credentials {
40  public:
41   grpc_ssl_credentials(const char* pem_root_certs,
42                        grpc_ssl_pem_key_cert_pair* pem_key_cert_pair,
43                        const grpc_ssl_verify_peer_options* verify_options);
44 
45   ~grpc_ssl_credentials() override;
46 
47   grpc_core::RefCountedPtr<grpc_channel_security_connector>
48   create_security_connector(
49       grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
50       const char* target, grpc_core::ChannelArgs* args) override;
51 
52   static grpc_core::UniqueTypeName Type();
53 
type()54   grpc_core::UniqueTypeName type() const override { return Type(); }
55 
56   // TODO(mattstev): Plumb to wrapped languages. Until then, setting the TLS
57   // version should be done for testing purposes only.
58   void set_min_tls_version(grpc_tls_version min_tls_version);
59   void set_max_tls_version(grpc_tls_version max_tls_version);
60 
61  private:
cmp_impl(const grpc_channel_credentials * other)62   int cmp_impl(const grpc_channel_credentials* other) const override {
63     // TODO(yashykt): Check if we can do something better here
64     return grpc_core::QsortCompare(
65         static_cast<const grpc_channel_credentials*>(this), other);
66   }
67 
68   void build_config(const char* pem_root_certs,
69                     grpc_ssl_pem_key_cert_pair* pem_key_cert_pair,
70                     const grpc_ssl_verify_peer_options* verify_options);
71 
72   grpc_ssl_config config_;
73 };
74 
75 struct grpc_ssl_server_certificate_config {
76   grpc_ssl_pem_key_cert_pair* pem_key_cert_pairs = nullptr;
77   size_t num_key_cert_pairs = 0;
78   char* pem_root_certs = nullptr;
79 };
80 
81 struct grpc_ssl_server_certificate_config_fetcher {
82   grpc_ssl_server_certificate_config_callback cb = nullptr;
83   void* user_data;
84 };
85 
86 class grpc_ssl_server_credentials final : public grpc_server_credentials {
87  public:
88   explicit grpc_ssl_server_credentials(
89       const grpc_ssl_server_credentials_options& options);
90   ~grpc_ssl_server_credentials() override;
91 
92   grpc_core::RefCountedPtr<grpc_server_security_connector>
93   create_security_connector(const grpc_core::ChannelArgs& /* args */) override;
94 
95   static grpc_core::UniqueTypeName Type();
96 
type()97   grpc_core::UniqueTypeName type() const override { return Type(); }
98 
has_cert_config_fetcher()99   bool has_cert_config_fetcher() const {
100     return certificate_config_fetcher_.cb != nullptr;
101   }
102 
FetchCertConfig(grpc_ssl_server_certificate_config ** config)103   grpc_ssl_certificate_config_reload_status FetchCertConfig(
104       grpc_ssl_server_certificate_config** config) {
105     GPR_DEBUG_ASSERT(has_cert_config_fetcher());
106     return certificate_config_fetcher_.cb(certificate_config_fetcher_.user_data,
107                                           config);
108   }
109 
110   // TODO(mattstev): Plumb to wrapped languages. Until then, setting the TLS
111   // version should be done for testing purposes only.
112   void set_min_tls_version(grpc_tls_version min_tls_version);
113   void set_max_tls_version(grpc_tls_version max_tls_version);
114 
config()115   const grpc_ssl_server_config& config() const { return config_; }
116 
117  private:
118   void build_config(
119       const char* pem_root_certs,
120       grpc_ssl_pem_key_cert_pair* pem_key_cert_pairs, size_t num_key_cert_pairs,
121       grpc_ssl_client_certificate_request_type client_certificate_request);
122 
123   grpc_ssl_server_config config_;
124   grpc_ssl_server_certificate_config_fetcher certificate_config_fetcher_;
125 };
126 
127 tsi_ssl_pem_key_cert_pair* grpc_convert_grpc_to_tsi_cert_pairs(
128     const grpc_ssl_pem_key_cert_pair* pem_key_cert_pairs,
129     size_t num_key_cert_pairs);
130 
131 #endif  // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_SSL_SSL_CREDENTIALS_H
132