ssl_utils.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 
19 #ifndef GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_SSL_UTILS_H
20 #define GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_SSL_UTILS_H
21 
23 
24 #include <stddef.h>
25 
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include "absl/status/status.h"
31 #include "absl/strings/string_view.h"
32 
33 #include <grpc/grpc_security.h>
35 #include <grpc/slice.h>
36 
43 
44 /* --- Util --- */
45 
46 /* Check ALPN information returned from SSL handshakes. */
48 
49 /* Check peer name information returned from SSL handshakes. */
51  const tsi_peer* peer);
52 /* Compare targer_name information extracted from SSL security connectors. */
54  absl::string_view other_target_name,
55  absl::string_view overridden_target_name,
56  absl::string_view other_overridden_target_name);
57 
58 namespace grpc_core {
59 /* Check the host that will be set for a call is acceptable.*/
61  absl::string_view target_name,
62  absl::string_view overridden_target_name,
63  grpc_auth_context* auth_context);
64 } // namespace grpc_core
65 
66 /* Return HTTP2-compliant cipher suites that gRPC accepts by default. */
67 const char* grpc_get_ssl_cipher_suites(void);
68 
69 /* Map from grpc_ssl_client_certificate_request_type to
70  * tsi_client_certificate_request_type. */
74 
75 /* Map grpc_tls_version to tsi_tls_version. */
77 
78 /* Return an array of strings containing alpn protocols. */
79 const char** grpc_fill_alpn_protocol_strings(size_t* num_alpn_protocols);
80 
81 /* Initialize TSI SSL server/client handshaker factory. */
83  tsi_ssl_pem_key_cert_pair* key_cert_pair, const char* pem_root_certs,
84  bool skip_server_certificate_verification, tsi_tls_version min_tls_version,
85  tsi_tls_version max_tls_version, tsi_ssl_session_cache* ssl_session_cache,
87  const char* crl_directory,
88  tsi_ssl_client_handshaker_factory** handshaker_factory);
89 
91  tsi_ssl_pem_key_cert_pair* key_cert_pairs, size_t num_key_cert_pairs,
92  const char* pem_root_certs,
93  grpc_ssl_client_certificate_request_type client_certificate_request,
94  tsi_tls_version min_tls_version, tsi_tls_version max_tls_version,
96  const char* crl_directory,
97  tsi_ssl_server_handshaker_factory** handshaker_factory);
98 
99 /* Free the memory occupied by key cert pairs. */
101  size_t num_key_cert_pairs);
102 /* Exposed for testing only. */
104  const tsi_peer* peer, const char* transport_security_type);
106  const grpc_auth_context* auth_context);
108 int grpc_ssl_host_matches_name(const tsi_peer* peer,
109  absl::string_view peer_name);
110 
111 /* --- Default SSL Root Store. --- */
112 namespace grpc_core {
113 
114 // The class implements default SSL root store.
116  public:
117  // Gets the default SSL root store. Returns nullptr if not found.
118  static const tsi_ssl_root_certs_store* GetRootStore();
119 
120  // Gets the default PEM root certificate.
121  static const char* GetPemRootCerts();
122 
123  protected:
124  // Returns default PEM root certificates in nullptr terminated grpc_slice.
125  // This function is protected instead of private, so that it can be tested.
127 
128  private:
129  // Construct me not!
131 
132  // Initialization of default SSL root store.
133  static void InitRootStore();
134 
135  // One-time initialization of default SSL root store.
136  static void InitRootStoreOnce();
137 
138  // SSL root store in tsi_ssl_root_certs_store object.
140 
141  // Default PEM root certificates.
143 };
144 
146  public:
149 
150  // Movable.
151  PemKeyCertPair(PemKeyCertPair&& other) noexcept {
152  private_key_ = std::move(other.private_key_);
153  cert_chain_ = std::move(other.cert_chain_);
154  }
156  private_key_ = std::move(other.private_key_);
157  cert_chain_ = std::move(other.cert_chain_);
158  return *this;
159  }
160 
161  // Copyable.
163  : private_key_(other.private_key()), cert_chain_(other.cert_chain()) {}
165  private_key_ = other.private_key();
166  cert_chain_ = other.cert_chain();
167  return *this;
168  }
169 
170  bool operator==(const PemKeyCertPair& other) const {
171  return this->private_key() == other.private_key() &&
172  this->cert_chain() == other.cert_chain();
173  }
174 
175  const std::string& private_key() const { return private_key_; }
176  const std::string& cert_chain() const { return cert_chain_; }
177 
178  private:
181 };
182 
183 using PemKeyCertPairList = std::vector<PemKeyCertPair>;
184 
185 } // namespace grpc_core
186 
187 #endif // GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_SSL_UTILS_H
grpc_core::PemKeyCertPair::private_key_
std::string private_key_
Definition: ssl_utils.h:179
grpc_fill_alpn_protocol_strings
const char ** grpc_fill_alpn_protocol_strings(size_t *num_alpn_protocols)
Definition: ssl_utils.cc:205
grpc_auth_context
Definition: security_context.h:63
grpc_get_tsi_client_certificate_request_type
tsi_client_certificate_request_type grpc_get_tsi_client_certificate_request_type(grpc_ssl_client_certificate_request_type grpc_request_type)
Definition: ssl_utils.cc:106
grpc_ssl_tsi_client_handshaker_factory_init
grpc_security_status grpc_ssl_tsi_client_handshaker_factory_init(tsi_ssl_pem_key_cert_pair *key_cert_pair, const char *pem_root_certs, bool skip_server_certificate_verification, tsi_tls_version min_tls_version, tsi_tls_version max_tls_version, tsi_ssl_session_cache *ssl_session_cache, tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger *tls_session_key_logger, const char *crl_directory, tsi_ssl_client_handshaker_factory **handshaker_factory)
Definition: ssl_utils.cc:422
pem_root_certs
static char * pem_root_certs
Definition: rb_channel_credentials.c:38
slice.h
tsi_ssl_pem_key_cert_pair
Definition: ssl_transport_security.h:101
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::PemKeyCertPair::PemKeyCertPair
PemKeyCertPair(PemKeyCertPair &&other) noexcept
Definition: ssl_utils.h:151
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
tsi_ssl_client_handshaker_factory
Definition: ssl_transport_security.cc:93
grpc_get_ssl_cipher_suites
const char * grpc_get_ssl_cipher_suites(void)
Definition: ssl_utils.cc:100
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_ssl_check_alpn
grpc_error_handle grpc_ssl_check_alpn(const tsi_peer *peer)
Definition: ssl_utils.cc:141
grpc_core::DefaultSslRootStore::GetPemRootCerts
static const char * GetPemRootCerts()
Definition: ssl_utils.cc:568
grpc_core::PemKeyCertPair::operator=
PemKeyCertPair & operator=(const PemKeyCertPair &other)
Definition: ssl_utils.h:164
grpc_security.h
ssl_transport_security.h
grpc_ssl_host_matches_name
int grpc_ssl_host_matches_name(const tsi_peer *peer, absl::string_view peer_name)
Definition: ssl_utils.cc:216
grpc_core::DefaultSslRootStore::InitRootStore
static void InitRootStore()
Definition: ssl_utils.cc:613
ssl_key_logging.h
grpc_core::DefaultSslRootStore::GetRootStore
static const tsi_ssl_root_certs_store * GetRootStore()
Definition: ssl_utils.cc:563
grpc_core::PemKeyCertPair::cert_chain_
std::string cert_chain_
Definition: ssl_utils.h:180
grpc_core::RefCountedPtr< grpc_auth_context >
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
tsi_client_certificate_request_type
tsi_client_certificate_request_type
Definition: transport_security_interface.h:60
grpc_ssl_tsi_server_handshaker_factory_init
grpc_security_status grpc_ssl_tsi_server_handshaker_factory_init(tsi_ssl_pem_key_cert_pair *key_cert_pairs, size_t num_key_cert_pairs, const char *pem_root_certs, grpc_ssl_client_certificate_request_type client_certificate_request, tsi_tls_version min_tls_version, tsi_tls_version max_tls_version, tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger *tls_session_key_logger, const char *crl_directory, tsi_ssl_server_handshaker_factory **handshaker_factory)
Definition: ssl_utils.cc:478
grpc_core::DefaultSslRootStore::default_root_store_
static tsi_ssl_root_certs_store * default_root_store_
Definition: ssl_utils.h:139
tsi_tls_version
tsi_tls_version
Definition: transport_security_interface.h:89
security_connector.h
grpc_ssl_check_peer_name
grpc_error_handle grpc_ssl_check_peer_name(absl::string_view peer_name, const tsi_peer *peer)
Definition: ssl_utils.cc:158
grpc_core::SslCheckCallHost
absl::Status SslCheckCallHost(absl::string_view host, absl::string_view target_name, absl::string_view overridden_target_name, grpc_auth_context *auth_context)
Definition: ssl_utils.cc:180
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::PemKeyCertPair::cert_chain
const std::string & cert_chain() const
Definition: ssl_utils.h:176
grpc_ssl_client_certificate_request_type
grpc_ssl_client_certificate_request_type
Definition: grpc_security_constants.h:79
grpc_tls_version
grpc_tls_version
Definition: grpc_security_constants.h:146
error.h
grpc_core::PemKeyCertPair
Definition: ssl_utils.h:145
grpc_core::DefaultSslRootStore::default_pem_root_certs_
static grpc_slice default_pem_root_certs_
Definition: ssl_utils.h:142
grpc_core::PemKeyCertPair::private_key
const std::string & private_key() const
Definition: ssl_utils.h:175
transport_security_interface.h
grpc_core::PemKeyCertPair::PemKeyCertPair
PemKeyCertPair(const PemKeyCertPair &other)
Definition: ssl_utils.h:162
tsi_ssl_server_handshaker_factory
Definition: ssl_transport_security.cc:102
grpc_shallow_peer_destruct
void grpc_shallow_peer_destruct(tsi_peer *peer)
Definition: ssl_utils.cc:418
grpc_ssl_cmp_target_name
int grpc_ssl_cmp_target_name(absl::string_view target_name, absl::string_view other_target_name, absl::string_view overridden_target_name, absl::string_view other_overridden_target_name)
Definition: ssl_utils.cc:231
tsi_peer
Definition: transport_security_interface.h:238
grpc_core::DefaultSslRootStore::ComputePemRootCerts
static grpc_slice ComputePemRootCerts()
Definition: ssl_utils.cc:576
grpc_core::DefaultSslRootStore::InitRootStoreOnce
static void InitRootStoreOnce()
Definition: ssl_utils.cc:618
tsi_ssl_session_cache
struct tsi_ssl_session_cache tsi_ssl_session_cache
Definition: ssl_transport_security.h:68
tsi_ssl_root_certs_store
Definition: ssl_transport_security.cc:84
grpc_core::PemKeyCertPairList
std::vector< PemKeyCertPair > PemKeyCertPairList
Definition: ssl_utils.h:183
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
private_key
Definition: hrss.c:1885
grpc_shallow_peer_from_ssl_auth_context
tsi_peer grpc_shallow_peer_from_ssl_auth_context(const grpc_auth_context *auth_context)
Definition: ssl_utils.cc:361
grpc_core::PemKeyCertPair::operator==
bool operator==(const PemKeyCertPair &other) const
Definition: ssl_utils.h:170
tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger
Definition: ssl_key_logging.h:46
grpc_security_constants.h
ref_counted_ptr.h
grpc_ssl_peer_to_auth_context
grpc_core::RefCountedPtr< grpc_auth_context > grpc_ssl_peer_to_auth_context(const tsi_peer *peer, const char *transport_security_type)
Definition: ssl_utils.cc:261
grpc_core::PemKeyCertPair::PemKeyCertPair
PemKeyCertPair(absl::string_view private_key, absl::string_view cert_chain)
Definition: ssl_utils.h:147
grpc_error
Definition: error_internal.h:42
grpc_tsi_ssl_pem_key_cert_pairs_destroy
void grpc_tsi_ssl_pem_key_cert_pairs_destroy(tsi_ssl_pem_key_cert_pair *kp, size_t num_key_cert_pairs)
Definition: ssl_utils.cc:168
grpc_security_status
grpc_security_status
Definition: security_connector.h:52
grpc_core::PemKeyCertPair::operator=
PemKeyCertPair & operator=(PemKeyCertPair &&other) noexcept
Definition: ssl_utils.h:155
grpc_core::DefaultSslRootStore
Definition: ssl_utils.h:115
grpc_get_tsi_tls_version
tsi_tls_version grpc_get_tsi_tls_version(grpc_tls_version tls_version)
Definition: ssl_utils.cc:129
grpc_core::DefaultSslRootStore::DefaultSslRootStore
DefaultSslRootStore()
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:21