1 //
2 //
3 // Copyright 2020 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_SRC_CORE_LIB_SECURITY_CREDENTIALS_XDS_XDS_CREDENTIALS_H
20 #define GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_XDS_XDS_CREDENTIALS_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stddef.h>
25 
26 #include <functional>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "absl/status/status.h"
32 
33 #include <grpc/grpc.h>
34 #include <grpc/grpc_security.h>
35 
36 #include "src/core/ext/xds/xds_certificate_provider.h"
37 #include "src/core/lib/channel/channel_args.h"
38 #include "src/core/lib/gprpp/ref_counted_ptr.h"
39 #include "src/core/lib/gprpp/unique_type_name.h"
40 #include "src/core/lib/matchers/matchers.h"
41 #include "src/core/lib/security/credentials/credentials.h"
42 #include "src/core/lib/security/credentials/tls/grpc_tls_certificate_verifier.h"
43 #include "src/core/lib/security/security_connector/security_connector.h"
44 
45 namespace grpc_core {
46 
47 class XdsCertificateVerifier : public grpc_tls_certificate_verifier {
48  public:
49   XdsCertificateVerifier(
50       RefCountedPtr<XdsCertificateProvider> xds_certificate_provider,
51       std::string cluster_name);
52 
53   bool Verify(grpc_tls_custom_verification_check_request* request,
54               std::function<void(absl::Status)>,
55               absl::Status* sync_status) override;
56   void Cancel(grpc_tls_custom_verification_check_request*) override;
57 
58   UniqueTypeName type() const override;
59 
60  private:
61   int CompareImpl(const grpc_tls_certificate_verifier* other) const override;
62 
63   RefCountedPtr<XdsCertificateProvider> xds_certificate_provider_;
64   std::string cluster_name_;
65 };
66 
67 class XdsCredentials final : public grpc_channel_credentials {
68  public:
XdsCredentials(RefCountedPtr<grpc_channel_credentials> fallback_credentials)69   explicit XdsCredentials(
70       RefCountedPtr<grpc_channel_credentials> fallback_credentials)
71       : fallback_credentials_(std::move(fallback_credentials)) {}
72 
73   RefCountedPtr<grpc_channel_security_connector> create_security_connector(
74       RefCountedPtr<grpc_call_credentials> call_creds, const char* target_name,
75       ChannelArgs* args) override;
76 
77   static UniqueTypeName Type();
78 
type()79   UniqueTypeName type() const override { return Type(); }
80 
81  private:
cmp_impl(const grpc_channel_credentials * other)82   int cmp_impl(const grpc_channel_credentials* other) const override {
83     auto* o = static_cast<const XdsCredentials*>(other);
84     return fallback_credentials_->cmp(o->fallback_credentials_.get());
85   }
86 
87   RefCountedPtr<grpc_channel_credentials> fallback_credentials_;
88 };
89 
90 class XdsServerCredentials final : public grpc_server_credentials {
91  public:
XdsServerCredentials(RefCountedPtr<grpc_server_credentials> fallback_credentials)92   explicit XdsServerCredentials(
93       RefCountedPtr<grpc_server_credentials> fallback_credentials)
94       : fallback_credentials_(std::move(fallback_credentials)) {}
95 
96   RefCountedPtr<grpc_server_security_connector> create_security_connector(
97       const ChannelArgs& /* args */) override;
98 
99   static UniqueTypeName Type();
100 
type()101   UniqueTypeName type() const override { return Type(); }
102 
103  private:
104   RefCountedPtr<grpc_server_credentials> fallback_credentials_;
105 };
106 
107 bool TestOnlyXdsVerifySubjectAlternativeNames(
108     const char* const* subject_alternative_names,
109     size_t subject_alternative_names_size,
110     const std::vector<StringMatcher>& matchers);
111 
112 }  // namespace grpc_core
113 
114 #endif  // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_XDS_XDS_CREDENTIALS_H
115