• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/security/security_connector/ssl_utils.h"
22 
23 #include <vector>
24 
25 #include "absl/strings/str_cat.h"
26 
27 #include <grpc/slice_buffer.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 
31 #include "src/core/ext/transport/chttp2/alpn/alpn.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/gpr/string.h"
34 #include "src/core/lib/gprpp/host_port.h"
35 #include "src/core/lib/gprpp/ref_counted_ptr.h"
36 #include "src/core/lib/iomgr/load_file.h"
37 #include "src/core/lib/security/context/security_context.h"
38 #include "src/core/lib/security/security_connector/load_system_roots.h"
39 #include "src/core/lib/security/security_connector/ssl_utils_config.h"
40 #include "src/core/tsi/ssl_transport_security.h"
41 
42 /* -- Constants. -- */
43 
44 #ifndef INSTALL_PREFIX
45 static const char* installed_roots_path = "/usr/share/grpc/roots.pem";
46 #else
47 static const char* installed_roots_path =
48     INSTALL_PREFIX "/share/grpc/roots.pem";
49 #endif
50 
51 #ifndef TSI_OPENSSL_ALPN_SUPPORT
52 #define TSI_OPENSSL_ALPN_SUPPORT 1
53 #endif
54 
55 /* -- Overridden default roots. -- */
56 
57 static grpc_ssl_roots_override_callback ssl_roots_override_cb = nullptr;
58 
grpc_set_ssl_roots_override_callback(grpc_ssl_roots_override_callback cb)59 void grpc_set_ssl_roots_override_callback(grpc_ssl_roots_override_callback cb) {
60   ssl_roots_override_cb = cb;
61 }
62 
63 /* -- Cipher suites. -- */
64 
65 static gpr_once cipher_suites_once = GPR_ONCE_INIT;
66 static const char* cipher_suites = nullptr;
67 
68 // All cipher suites for default are compliant with HTTP2.
69 GPR_GLOBAL_CONFIG_DEFINE_STRING(
70     grpc_ssl_cipher_suites,
71     "TLS_AES_128_GCM_SHA256:"
72     "TLS_AES_256_GCM_SHA384:"
73     "TLS_CHACHA20_POLY1305_SHA256:"
74     "ECDHE-ECDSA-AES128-GCM-SHA256:"
75     "ECDHE-ECDSA-AES256-GCM-SHA384:"
76     "ECDHE-RSA-AES128-GCM-SHA256:"
77     "ECDHE-RSA-AES256-GCM-SHA384",
78     "A colon separated list of cipher suites to use with OpenSSL")
79 
init_cipher_suites(void)80 static void init_cipher_suites(void) {
81   grpc_core::UniquePtr<char> value =
82       GPR_GLOBAL_CONFIG_GET(grpc_ssl_cipher_suites);
83   cipher_suites = value.release();
84 }
85 
86 /* --- Util --- */
87 
grpc_get_ssl_cipher_suites(void)88 const char* grpc_get_ssl_cipher_suites(void) {
89   gpr_once_init(&cipher_suites_once, init_cipher_suites);
90   return cipher_suites;
91 }
92 
grpc_tsi_security_level_string_to_enum(const char * security_level)93 grpc_security_level grpc_tsi_security_level_string_to_enum(
94     const char* security_level) {
95   if (strcmp(security_level, "TSI_INTEGRITY_ONLY") == 0) {
96     return GRPC_INTEGRITY_ONLY;
97   } else if (strcmp(security_level, "TSI_PRIVACY_AND_INTEGRITY") == 0) {
98     return GRPC_PRIVACY_AND_INTEGRITY;
99   }
100   return GRPC_SECURITY_NONE;
101 }
102 
grpc_security_level_to_string(grpc_security_level security_level)103 const char* grpc_security_level_to_string(grpc_security_level security_level) {
104   if (security_level == GRPC_PRIVACY_AND_INTEGRITY) {
105     return "GRPC_PRIVACY_AND_INTEGRITY";
106   } else if (security_level == GRPC_INTEGRITY_ONLY) {
107     return "GRPC_INTEGRITY_ONLY";
108   }
109   return "GRPC_SECURITY_NONE";
110 }
111 
grpc_check_security_level(grpc_security_level channel_level,grpc_security_level call_cred_level)112 bool grpc_check_security_level(grpc_security_level channel_level,
113                                grpc_security_level call_cred_level) {
114   return static_cast<int>(channel_level) >= static_cast<int>(call_cred_level);
115 }
116 
117 tsi_client_certificate_request_type
grpc_get_tsi_client_certificate_request_type(grpc_ssl_client_certificate_request_type grpc_request_type)118 grpc_get_tsi_client_certificate_request_type(
119     grpc_ssl_client_certificate_request_type grpc_request_type) {
120   switch (grpc_request_type) {
121     case GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE:
122       return TSI_DONT_REQUEST_CLIENT_CERTIFICATE;
123 
124     case GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY:
125       return TSI_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY;
126 
127     case GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY:
128       return TSI_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY;
129 
130     case GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY:
131       return TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY;
132 
133     case GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY:
134       return TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY;
135 
136     default:
137       return TSI_DONT_REQUEST_CLIENT_CERTIFICATE;
138   }
139 }
140 
grpc_get_tsi_tls_version(grpc_tls_version tls_version)141 tsi_tls_version grpc_get_tsi_tls_version(grpc_tls_version tls_version) {
142   switch (tls_version) {
143     case grpc_tls_version::TLS1_2:
144       return tsi_tls_version::TSI_TLS1_2;
145     case grpc_tls_version::TLS1_3:
146       return tsi_tls_version::TSI_TLS1_3;
147     default:
148       gpr_log(GPR_INFO, "Falling back to TLS 1.2.");
149       return tsi_tls_version::TSI_TLS1_2;
150   }
151 }
152 
grpc_ssl_check_alpn(const tsi_peer * peer)153 grpc_error* grpc_ssl_check_alpn(const tsi_peer* peer) {
154 #if TSI_OPENSSL_ALPN_SUPPORT
155   /* Check the ALPN if ALPN is supported. */
156   const tsi_peer_property* p =
157       tsi_peer_get_property_by_name(peer, TSI_SSL_ALPN_SELECTED_PROTOCOL);
158   if (p == nullptr) {
159     return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
160         "Cannot check peer: missing selected ALPN property.");
161   }
162   if (!grpc_chttp2_is_alpn_version_supported(p->value.data, p->value.length)) {
163     return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
164         "Cannot check peer: invalid ALPN value.");
165   }
166 #endif /* TSI_OPENSSL_ALPN_SUPPORT */
167   return GRPC_ERROR_NONE;
168 }
169 
grpc_ssl_check_peer_name(absl::string_view peer_name,const tsi_peer * peer)170 grpc_error* grpc_ssl_check_peer_name(absl::string_view peer_name,
171                                      const tsi_peer* peer) {
172   /* Check the peer name if specified. */
173   if (!peer_name.empty() && !grpc_ssl_host_matches_name(peer, peer_name)) {
174     return GRPC_ERROR_CREATE_FROM_COPIED_STRING(
175         absl::StrCat("Peer name ", peer_name, " is not in peer certificate")
176             .c_str());
177   }
178   return GRPC_ERROR_NONE;
179 }
180 
grpc_ssl_check_call_host(absl::string_view host,absl::string_view target_name,absl::string_view overridden_target_name,grpc_auth_context * auth_context,grpc_error ** error)181 bool grpc_ssl_check_call_host(absl::string_view host,
182                               absl::string_view target_name,
183                               absl::string_view overridden_target_name,
184                               grpc_auth_context* auth_context,
185                               grpc_error** error) {
186   grpc_security_status status = GRPC_SECURITY_ERROR;
187   tsi_peer peer = grpc_shallow_peer_from_ssl_auth_context(auth_context);
188   if (grpc_ssl_host_matches_name(&peer, host)) status = GRPC_SECURITY_OK;
189   /* If the target name was overridden, then the original target_name was
190    'checked' transitively during the previous peer check at the end of the
191    handshake. */
192   if (!overridden_target_name.empty() && host == target_name) {
193     status = GRPC_SECURITY_OK;
194   }
195   if (status != GRPC_SECURITY_OK) {
196     *error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
197         "call host does not match SSL server name");
198   }
199   grpc_shallow_peer_destruct(&peer);
200   return true;
201 }
202 
grpc_fill_alpn_protocol_strings(size_t * num_alpn_protocols)203 const char** grpc_fill_alpn_protocol_strings(size_t* num_alpn_protocols) {
204   GPR_ASSERT(num_alpn_protocols != nullptr);
205   *num_alpn_protocols = grpc_chttp2_num_alpn_versions();
206   const char** alpn_protocol_strings = static_cast<const char**>(
207       gpr_malloc(sizeof(const char*) * (*num_alpn_protocols)));
208   for (size_t i = 0; i < *num_alpn_protocols; i++) {
209     alpn_protocol_strings[i] = grpc_chttp2_get_alpn_version_index(i);
210   }
211   return alpn_protocol_strings;
212 }
213 
grpc_ssl_host_matches_name(const tsi_peer * peer,absl::string_view peer_name)214 int grpc_ssl_host_matches_name(const tsi_peer* peer,
215                                absl::string_view peer_name) {
216   absl::string_view allocated_name;
217   absl::string_view ignored_port;
218   grpc_core::SplitHostPort(peer_name, &allocated_name, &ignored_port);
219   if (allocated_name.empty()) return 0;
220 
221   // IPv6 zone-id should not be included in comparisons.
222   const size_t zone_id = allocated_name.find('%');
223   if (zone_id != absl::string_view::npos) {
224     allocated_name.remove_suffix(allocated_name.size() - zone_id);
225   }
226   return tsi_ssl_peer_matches_name(peer, allocated_name);
227 }
228 
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)229 int grpc_ssl_cmp_target_name(absl::string_view target_name,
230                              absl::string_view other_target_name,
231                              absl::string_view overridden_target_name,
232                              absl::string_view other_overridden_target_name) {
233   int c = target_name.compare(other_target_name);
234   if (c != 0) return c;
235   return overridden_target_name.compare(other_overridden_target_name);
236 }
237 
IsSpiffeId(absl::string_view uri)238 static bool IsSpiffeId(absl::string_view uri) {
239   // Return false without logging for a non-spiffe uri scheme.
240   if (!absl::StartsWith(uri, "spiffe://")) {
241     return false;
242   };
243   if (uri.size() > 2048) {
244     gpr_log(GPR_INFO, "Invalid SPIFFE ID: ID longer than 2048 bytes.");
245     return false;
246   }
247   std::vector<absl::string_view> splits = absl::StrSplit(uri, '/');
248   if (splits.size() < 4 || splits[3] == "") {
249     gpr_log(GPR_INFO, "Invalid SPIFFE ID: workload id is empty.");
250     return false;
251   }
252   if (splits[2].size() > 255) {
253     gpr_log(GPR_INFO, "Invalid SPIFFE ID: domain longer than 255 characters.");
254     return false;
255   }
256   return true;
257 }
258 
grpc_ssl_peer_to_auth_context(const tsi_peer * peer,const char * transport_security_type)259 grpc_core::RefCountedPtr<grpc_auth_context> grpc_ssl_peer_to_auth_context(
260     const tsi_peer* peer, const char* transport_security_type) {
261   size_t i;
262   const char* peer_identity_property_name = nullptr;
263 
264   /* The caller has checked the certificate type property. */
265   GPR_ASSERT(peer->property_count >= 1);
266   grpc_core::RefCountedPtr<grpc_auth_context> ctx =
267       grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
268   grpc_auth_context_add_cstring_property(
269       ctx.get(), GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME,
270       transport_security_type);
271   const char* spiffe_data = nullptr;
272   size_t spiffe_length = 0;
273   int uri_count = 0;
274   bool has_spiffe_id = false;
275   for (i = 0; i < peer->property_count; i++) {
276     const tsi_peer_property* prop = &peer->properties[i];
277     if (prop->name == nullptr) continue;
278     if (strcmp(prop->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) {
279       /* If there is no subject alt name, have the CN as the identity. */
280       if (peer_identity_property_name == nullptr) {
281         peer_identity_property_name = GRPC_X509_CN_PROPERTY_NAME;
282       }
283       grpc_auth_context_add_property(ctx.get(), GRPC_X509_CN_PROPERTY_NAME,
284                                      prop->value.data, prop->value.length);
285     } else if (strcmp(prop->name,
286                       TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) {
287       peer_identity_property_name = GRPC_X509_SAN_PROPERTY_NAME;
288       grpc_auth_context_add_property(ctx.get(), GRPC_X509_SAN_PROPERTY_NAME,
289                                      prop->value.data, prop->value.length);
290     } else if (strcmp(prop->name, TSI_X509_PEM_CERT_PROPERTY) == 0) {
291       grpc_auth_context_add_property(ctx.get(),
292                                      GRPC_X509_PEM_CERT_PROPERTY_NAME,
293                                      prop->value.data, prop->value.length);
294     } else if (strcmp(prop->name, TSI_X509_PEM_CERT_CHAIN_PROPERTY) == 0) {
295       grpc_auth_context_add_property(ctx.get(),
296                                      GRPC_X509_PEM_CERT_CHAIN_PROPERTY_NAME,
297                                      prop->value.data, prop->value.length);
298     } else if (strcmp(prop->name, TSI_SSL_SESSION_REUSED_PEER_PROPERTY) == 0) {
299       grpc_auth_context_add_property(ctx.get(),
300                                      GRPC_SSL_SESSION_REUSED_PROPERTY,
301                                      prop->value.data, prop->value.length);
302     } else if (strcmp(prop->name, TSI_SECURITY_LEVEL_PEER_PROPERTY) == 0) {
303       grpc_auth_context_add_property(
304           ctx.get(), GRPC_TRANSPORT_SECURITY_LEVEL_PROPERTY_NAME,
305           prop->value.data, prop->value.length);
306     } else if (strcmp(prop->name, TSI_X509_URI_PEER_PROPERTY) == 0) {
307       uri_count++;
308       absl::string_view spiffe_id(prop->value.data, prop->value.length);
309       if (IsSpiffeId(spiffe_id)) {
310         spiffe_data = prop->value.data;
311         spiffe_length = prop->value.length;
312         has_spiffe_id = true;
313       }
314     }
315   }
316   if (peer_identity_property_name != nullptr) {
317     GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(
318                    ctx.get(), peer_identity_property_name) == 1);
319   }
320   // A valid SPIFFE certificate can only have exact one URI SAN field.
321   if (has_spiffe_id) {
322     if (uri_count == 1) {
323       GPR_ASSERT(spiffe_length > 0);
324       GPR_ASSERT(spiffe_data != nullptr);
325       grpc_auth_context_add_property(ctx.get(),
326                                      GRPC_PEER_SPIFFE_ID_PROPERTY_NAME,
327                                      spiffe_data, spiffe_length);
328     } else {
329       gpr_log(GPR_INFO, "Invalid SPIFFE ID: multiple URI SANs.");
330     }
331   }
332   return ctx;
333 }
334 
add_shallow_auth_property_to_peer(tsi_peer * peer,const grpc_auth_property * prop,const char * tsi_prop_name)335 static void add_shallow_auth_property_to_peer(tsi_peer* peer,
336                                               const grpc_auth_property* prop,
337                                               const char* tsi_prop_name) {
338   tsi_peer_property* tsi_prop = &peer->properties[peer->property_count++];
339   tsi_prop->name = const_cast<char*>(tsi_prop_name);
340   tsi_prop->value.data = prop->value;
341   tsi_prop->value.length = prop->value_length;
342 }
343 
grpc_shallow_peer_from_ssl_auth_context(const grpc_auth_context * auth_context)344 tsi_peer grpc_shallow_peer_from_ssl_auth_context(
345     const grpc_auth_context* auth_context) {
346   size_t max_num_props = 0;
347   grpc_auth_property_iterator it;
348   const grpc_auth_property* prop;
349   tsi_peer peer;
350   memset(&peer, 0, sizeof(peer));
351 
352   it = grpc_auth_context_property_iterator(auth_context);
353   while (grpc_auth_property_iterator_next(&it) != nullptr) max_num_props++;
354 
355   if (max_num_props > 0) {
356     peer.properties = static_cast<tsi_peer_property*>(
357         gpr_malloc(max_num_props * sizeof(tsi_peer_property)));
358     it = grpc_auth_context_property_iterator(auth_context);
359     while ((prop = grpc_auth_property_iterator_next(&it)) != nullptr) {
360       if (strcmp(prop->name, GRPC_X509_SAN_PROPERTY_NAME) == 0) {
361         add_shallow_auth_property_to_peer(
362             &peer, prop, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY);
363       } else if (strcmp(prop->name, GRPC_X509_CN_PROPERTY_NAME) == 0) {
364         add_shallow_auth_property_to_peer(
365             &peer, prop, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY);
366       } else if (strcmp(prop->name, GRPC_X509_PEM_CERT_PROPERTY_NAME) == 0) {
367         add_shallow_auth_property_to_peer(&peer, prop,
368                                           TSI_X509_PEM_CERT_PROPERTY);
369       } else if (strcmp(prop->name,
370                         GRPC_TRANSPORT_SECURITY_LEVEL_PROPERTY_NAME) == 0) {
371         add_shallow_auth_property_to_peer(&peer, prop,
372                                           TSI_SECURITY_LEVEL_PEER_PROPERTY);
373       } else if (strcmp(prop->name, GRPC_X509_PEM_CERT_CHAIN_PROPERTY_NAME) ==
374                  0) {
375         add_shallow_auth_property_to_peer(&peer, prop,
376                                           TSI_X509_PEM_CERT_CHAIN_PROPERTY);
377       } else if (strcmp(prop->name, GRPC_PEER_SPIFFE_ID_PROPERTY_NAME) == 0) {
378         add_shallow_auth_property_to_peer(&peer, prop,
379                                           TSI_X509_URI_PEER_PROPERTY);
380       }
381     }
382   }
383   return peer;
384 }
385 
grpc_shallow_peer_destruct(tsi_peer * peer)386 void grpc_shallow_peer_destruct(tsi_peer* peer) {
387   if (peer->properties != nullptr) gpr_free(peer->properties);
388 }
389 
grpc_ssl_tsi_client_handshaker_factory_init(tsi_ssl_pem_key_cert_pair * pem_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_ssl_client_handshaker_factory ** handshaker_factory)390 grpc_security_status grpc_ssl_tsi_client_handshaker_factory_init(
391     tsi_ssl_pem_key_cert_pair* pem_key_cert_pair, const char* pem_root_certs,
392     bool skip_server_certificate_verification, tsi_tls_version min_tls_version,
393     tsi_tls_version max_tls_version, tsi_ssl_session_cache* ssl_session_cache,
394     tsi_ssl_client_handshaker_factory** handshaker_factory) {
395   const char* root_certs;
396   const tsi_ssl_root_certs_store* root_store;
397   if (pem_root_certs == nullptr) {
398     // Use default root certificates.
399     root_certs = grpc_core::DefaultSslRootStore::GetPemRootCerts();
400     if (root_certs == nullptr) {
401       gpr_log(GPR_ERROR, "Could not get default pem root certs.");
402       return GRPC_SECURITY_ERROR;
403     }
404     root_store = grpc_core::DefaultSslRootStore::GetRootStore();
405   } else {
406     root_certs = pem_root_certs;
407     root_store = nullptr;
408   }
409   bool has_key_cert_pair = pem_key_cert_pair != nullptr &&
410                            pem_key_cert_pair->private_key != nullptr &&
411                            pem_key_cert_pair->cert_chain != nullptr;
412   tsi_ssl_client_handshaker_options options;
413   GPR_DEBUG_ASSERT(root_certs != nullptr);
414   options.pem_root_certs = root_certs;
415   options.root_store = root_store;
416   options.alpn_protocols =
417       grpc_fill_alpn_protocol_strings(&options.num_alpn_protocols);
418   if (has_key_cert_pair) {
419     options.pem_key_cert_pair = pem_key_cert_pair;
420   }
421   options.cipher_suites = grpc_get_ssl_cipher_suites();
422   options.session_cache = ssl_session_cache;
423   options.skip_server_certificate_verification =
424       skip_server_certificate_verification;
425   options.min_tls_version = min_tls_version;
426   options.max_tls_version = max_tls_version;
427   const tsi_result result =
428       tsi_create_ssl_client_handshaker_factory_with_options(&options,
429                                                             handshaker_factory);
430   gpr_free(options.alpn_protocols);
431   if (result != TSI_OK) {
432     gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
433             tsi_result_to_string(result));
434     return GRPC_SECURITY_ERROR;
435   }
436   return GRPC_SECURITY_OK;
437 }
438 
grpc_ssl_tsi_server_handshaker_factory_init(tsi_ssl_pem_key_cert_pair * pem_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_ssl_server_handshaker_factory ** handshaker_factory)439 grpc_security_status grpc_ssl_tsi_server_handshaker_factory_init(
440     tsi_ssl_pem_key_cert_pair* pem_key_cert_pairs, size_t num_key_cert_pairs,
441     const char* pem_root_certs,
442     grpc_ssl_client_certificate_request_type client_certificate_request,
443     tsi_tls_version min_tls_version, tsi_tls_version max_tls_version,
444     tsi_ssl_server_handshaker_factory** handshaker_factory) {
445   size_t num_alpn_protocols = 0;
446   const char** alpn_protocol_strings =
447       grpc_fill_alpn_protocol_strings(&num_alpn_protocols);
448   tsi_ssl_server_handshaker_options options;
449   options.pem_key_cert_pairs = pem_key_cert_pairs;
450   options.num_key_cert_pairs = num_key_cert_pairs;
451   options.pem_client_root_certs = pem_root_certs;
452   options.client_certificate_request =
453       grpc_get_tsi_client_certificate_request_type(client_certificate_request);
454   options.cipher_suites = grpc_get_ssl_cipher_suites();
455   options.alpn_protocols = alpn_protocol_strings;
456   options.num_alpn_protocols = static_cast<uint16_t>(num_alpn_protocols);
457   options.min_tls_version = min_tls_version;
458   options.max_tls_version = max_tls_version;
459   const tsi_result result =
460       tsi_create_ssl_server_handshaker_factory_with_options(&options,
461                                                             handshaker_factory);
462   gpr_free(alpn_protocol_strings);
463   if (result != TSI_OK) {
464     gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
465             tsi_result_to_string(result));
466     return GRPC_SECURITY_ERROR;
467   }
468   return GRPC_SECURITY_OK;
469 }
470 
471 /* --- Ssl cache implementation. --- */
472 
grpc_ssl_session_cache_create_lru(size_t capacity)473 grpc_ssl_session_cache* grpc_ssl_session_cache_create_lru(size_t capacity) {
474   tsi_ssl_session_cache* cache = tsi_ssl_session_cache_create_lru(capacity);
475   return reinterpret_cast<grpc_ssl_session_cache*>(cache);
476 }
477 
grpc_ssl_session_cache_destroy(grpc_ssl_session_cache * cache)478 void grpc_ssl_session_cache_destroy(grpc_ssl_session_cache* cache) {
479   tsi_ssl_session_cache* tsi_cache =
480       reinterpret_cast<tsi_ssl_session_cache*>(cache);
481   tsi_ssl_session_cache_unref(tsi_cache);
482 }
483 
grpc_ssl_session_cache_arg_copy(void * p)484 static void* grpc_ssl_session_cache_arg_copy(void* p) {
485   tsi_ssl_session_cache* tsi_cache =
486       reinterpret_cast<tsi_ssl_session_cache*>(p);
487   // destroy call below will unref the pointer.
488   tsi_ssl_session_cache_ref(tsi_cache);
489   return p;
490 }
491 
grpc_ssl_session_cache_arg_destroy(void * p)492 static void grpc_ssl_session_cache_arg_destroy(void* p) {
493   tsi_ssl_session_cache* tsi_cache =
494       reinterpret_cast<tsi_ssl_session_cache*>(p);
495   tsi_ssl_session_cache_unref(tsi_cache);
496 }
497 
grpc_ssl_session_cache_arg_cmp(void * p,void * q)498 static int grpc_ssl_session_cache_arg_cmp(void* p, void* q) {
499   return GPR_ICMP(p, q);
500 }
501 
grpc_ssl_session_cache_create_channel_arg(grpc_ssl_session_cache * cache)502 grpc_arg grpc_ssl_session_cache_create_channel_arg(
503     grpc_ssl_session_cache* cache) {
504   static const grpc_arg_pointer_vtable vtable = {
505       grpc_ssl_session_cache_arg_copy,
506       grpc_ssl_session_cache_arg_destroy,
507       grpc_ssl_session_cache_arg_cmp,
508   };
509   return grpc_channel_arg_pointer_create(
510       const_cast<char*>(GRPC_SSL_SESSION_CACHE_ARG), cache, &vtable);
511 }
512 
513 /* --- Default SSL root store implementation. --- */
514 
515 namespace grpc_core {
516 
517 tsi_ssl_root_certs_store* DefaultSslRootStore::default_root_store_;
518 grpc_slice DefaultSslRootStore::default_pem_root_certs_;
519 
GetRootStore()520 const tsi_ssl_root_certs_store* DefaultSslRootStore::GetRootStore() {
521   InitRootStore();
522   return default_root_store_;
523 }
524 
GetPemRootCerts()525 const char* DefaultSslRootStore::GetPemRootCerts() {
526   InitRootStore();
527   return GRPC_SLICE_IS_EMPTY(default_pem_root_certs_)
528              ? nullptr
529              : reinterpret_cast<const char*>
530                    GRPC_SLICE_START_PTR(default_pem_root_certs_);
531 }
532 
ComputePemRootCerts()533 grpc_slice DefaultSslRootStore::ComputePemRootCerts() {
534   grpc_slice result = grpc_empty_slice();
535   const bool not_use_system_roots =
536       GPR_GLOBAL_CONFIG_GET(grpc_not_use_system_ssl_roots);
537   // First try to load the roots from the configuration.
538   grpc_core::UniquePtr<char> default_root_certs_path =
539       GPR_GLOBAL_CONFIG_GET(grpc_default_ssl_roots_file_path);
540   if (strlen(default_root_certs_path.get()) > 0) {
541     GRPC_LOG_IF_ERROR(
542         "load_file", grpc_load_file(default_root_certs_path.get(), 1, &result));
543   }
544   // Try overridden roots if needed.
545   grpc_ssl_roots_override_result ovrd_res = GRPC_SSL_ROOTS_OVERRIDE_FAIL;
546   if (GRPC_SLICE_IS_EMPTY(result) && ssl_roots_override_cb != nullptr) {
547     char* pem_root_certs = nullptr;
548     ovrd_res = ssl_roots_override_cb(&pem_root_certs);
549     if (ovrd_res == GRPC_SSL_ROOTS_OVERRIDE_OK) {
550       GPR_ASSERT(pem_root_certs != nullptr);
551       result = grpc_slice_from_copied_buffer(
552           pem_root_certs,
553           strlen(pem_root_certs) + 1);  // nullptr terminator.
554     }
555     gpr_free(pem_root_certs);
556   }
557   // Try loading roots from OS trust store if flag is enabled.
558   if (GRPC_SLICE_IS_EMPTY(result) && !not_use_system_roots) {
559     result = LoadSystemRootCerts();
560   }
561   // Fallback to roots manually shipped with gRPC.
562   if (GRPC_SLICE_IS_EMPTY(result) &&
563       ovrd_res != GRPC_SSL_ROOTS_OVERRIDE_FAIL_PERMANENTLY) {
564     GRPC_LOG_IF_ERROR("load_file",
565                       grpc_load_file(installed_roots_path, 1, &result));
566   }
567   return result;
568 }
569 
InitRootStore()570 void DefaultSslRootStore::InitRootStore() {
571   static gpr_once once = GPR_ONCE_INIT;
572   gpr_once_init(&once, DefaultSslRootStore::InitRootStoreOnce);
573 }
574 
InitRootStoreOnce()575 void DefaultSslRootStore::InitRootStoreOnce() {
576   default_pem_root_certs_ = ComputePemRootCerts();
577   if (!GRPC_SLICE_IS_EMPTY(default_pem_root_certs_)) {
578     default_root_store_ =
579         tsi_ssl_root_certs_store_create(reinterpret_cast<const char*>(
580             GRPC_SLICE_START_PTR(default_pem_root_certs_)));
581   }
582 }
583 
584 }  // namespace grpc_core
585