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