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/credentials.h> 22 #include <grpc/grpc.h> 23 #include <grpc/grpc_security.h> 24 #include <grpc/grpc_security_constants.h> 25 #include <grpc/support/port_platform.h> 26 #include <stddef.h> 27 28 #include "absl/log/check.h" 29 #include "src/core/lib/channel/channel_args.h" 30 #include "src/core/lib/security/credentials/credentials.h" 31 #include "src/core/lib/security/security_connector/security_connector.h" 32 #include "src/core/lib/security/security_connector/ssl/ssl_security_connector.h" 33 #include "src/core/tsi/ssl_transport_security.h" 34 #include "src/core/util/ref_counted_ptr.h" 35 #include "src/core/util/unique_type_name.h" 36 #include "src/core/util/useful.h" 37 38 class grpc_ssl_credentials : public grpc_channel_credentials { 39 public: 40 grpc_ssl_credentials(const char* pem_root_certs, 41 grpc_ssl_pem_key_cert_pair* pem_key_cert_pair, 42 const grpc_ssl_verify_peer_options* verify_options); 43 44 ~grpc_ssl_credentials() override; 45 46 grpc_core::RefCountedPtr<grpc_channel_security_connector> 47 create_security_connector( 48 grpc_core::RefCountedPtr<grpc_call_credentials> call_creds, 49 const char* target, grpc_core::ChannelArgs* args) override; 50 51 static grpc_core::UniqueTypeName Type(); 52 type()53 grpc_core::UniqueTypeName type() const override { return Type(); } 54 55 // TODO(mattstev): Plumb to wrapped languages. Until then, setting the TLS 56 // version should be done for testing purposes only. 57 void set_min_tls_version(grpc_tls_version min_tls_version); 58 void set_max_tls_version(grpc_tls_version max_tls_version); 59 60 private: cmp_impl(const grpc_channel_credentials * other)61 int cmp_impl(const grpc_channel_credentials* other) const override { 62 // TODO(yashykt): Check if we can do something better here 63 return grpc_core::QsortCompare( 64 static_cast<const grpc_channel_credentials*>(this), other); 65 } 66 67 void build_config(const char* pem_root_certs, 68 grpc_ssl_pem_key_cert_pair* pem_key_cert_pair, 69 const grpc_ssl_verify_peer_options* verify_options); 70 71 // InitializeClientHandshakerFactory constructs a client handshaker factory 72 // that is stored on this credentials object. This handshaker factory will be 73 // used when creating handshakers using these credentials except in the case 74 // that there is a session cache. If a session cache is used, a new handshaker 75 // factory will be created and used that contains that session cache. 76 grpc_security_status InitializeClientHandshakerFactory( 77 const grpc_ssl_config* config, const char* pem_root_certs, 78 const tsi_ssl_root_certs_store* root_store, 79 tsi_ssl_session_cache* ssl_session_cache, 80 tsi_ssl_client_handshaker_factory** handshaker_factory); 81 82 grpc_ssl_config config_; 83 tsi_ssl_client_handshaker_factory* client_handshaker_factory_ = nullptr; 84 const tsi_ssl_root_certs_store* root_store_ = nullptr; 85 grpc_security_status client_handshaker_initialization_status_; 86 }; 87 88 struct grpc_ssl_server_certificate_config { 89 grpc_ssl_pem_key_cert_pair* pem_key_cert_pairs = nullptr; 90 size_t num_key_cert_pairs = 0; 91 char* pem_root_certs = nullptr; 92 }; 93 94 struct grpc_ssl_server_certificate_config_fetcher { 95 grpc_ssl_server_certificate_config_callback cb = nullptr; 96 void* user_data; 97 }; 98 99 class grpc_ssl_server_credentials final : public grpc_server_credentials { 100 public: 101 explicit grpc_ssl_server_credentials( 102 const grpc_ssl_server_credentials_options& options); 103 ~grpc_ssl_server_credentials() override; 104 105 grpc_core::RefCountedPtr<grpc_server_security_connector> 106 create_security_connector(const grpc_core::ChannelArgs& /* args */) override; 107 108 static grpc_core::UniqueTypeName Type(); 109 type()110 grpc_core::UniqueTypeName type() const override { return Type(); } 111 has_cert_config_fetcher()112 bool has_cert_config_fetcher() const { 113 return certificate_config_fetcher_.cb != nullptr; 114 } 115 FetchCertConfig(grpc_ssl_server_certificate_config ** config)116 grpc_ssl_certificate_config_reload_status FetchCertConfig( 117 grpc_ssl_server_certificate_config** config) { 118 DCHECK(has_cert_config_fetcher()); 119 return certificate_config_fetcher_.cb(certificate_config_fetcher_.user_data, 120 config); 121 } 122 123 // TODO(mattstev): Plumb to wrapped languages. Until then, setting the TLS 124 // version should be done for testing purposes only. 125 void set_min_tls_version(grpc_tls_version min_tls_version); 126 void set_max_tls_version(grpc_tls_version max_tls_version); 127 config()128 const grpc_ssl_server_config& config() const { return config_; } 129 130 private: 131 void build_config( 132 const char* pem_root_certs, 133 grpc_ssl_pem_key_cert_pair* pem_key_cert_pairs, size_t num_key_cert_pairs, 134 grpc_ssl_client_certificate_request_type client_certificate_request); 135 136 grpc_ssl_server_config config_; 137 grpc_ssl_server_certificate_config_fetcher certificate_config_fetcher_; 138 }; 139 140 tsi_ssl_pem_key_cert_pair* grpc_convert_grpc_to_tsi_cert_pairs( 141 const grpc_ssl_pem_key_cert_pair* pem_key_cert_pairs, 142 size_t num_key_cert_pairs); 143 144 #endif // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_SSL_SSL_CREDENTIALS_H 145