Debian Codegraph @

src/core/lib/security/context/security_context.h

raw
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_CONTEXT_SECURITY_CONTEXT_H
20 #define GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stddef.h>
25
26 #include <memory>
27 #include <utility>
28
29 #include "absl/strings/string_view.h"
30
31 #include <grpc/grpc_security.h>
32 #include <grpc/impl/codegen/grpc_types.h>
33 #include <grpc/support/alloc.h>
34
35 #include "src/core/lib/debug/trace.h"
36 #include "src/core/lib/gpr/useful.h"
37 #include "src/core/lib/gprpp/debug_location.h"
38 #include "src/core/lib/gprpp/ref_counted.h"
39 #include "src/core/lib/gprpp/ref_counted_ptr.h"
40 #include "src/core/lib/resource_quota/arena.h"
41 #include "src/core/lib/security/credentials/credentials.h" // IWYU pragma: keep
42
43 extern grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount;
44
45 /* --- grpc_auth_context ---
46
47 High level authentication context object. Can optionally be chained. */
48
49 /* Property names are always NULL terminated. */
50
51 struct grpc_auth_property_array {
52 grpc_auth_property* array = nullptr;
53 size_t count = 0;
54 size_t capacity = 0;
55 };
56
57 void grpc_auth_property_reset(grpc_auth_property* property);
58
59 #define GRPC_AUTH_CONTEXT_ARG "grpc.auth_context"
60
61 // This type is forward declared as a C struct and we cannot define it as a
62 // class. Otherwise, compiler will complain about type mismatch due to
63 // -Wmismatched-tags.
64 struct grpc_auth_context
65 : public grpc_core::RefCounted<grpc_auth_context,
66 grpc_core::NonPolymorphicRefCount> {
67 public:
68 // Base class for all extensions to inherit from.
69 class Extension {
70 public:
71 virtual ~Extension() = default;
72 };
73 explicit grpc_auth_context(
74 grpc_core::RefCountedPtr<grpc_auth_context> chained)
75 : grpc_core::RefCounted<grpc_auth_context,
76 grpc_core::NonPolymorphicRefCount>(
77 GRPC_TRACE_FLAG_ENABLED(grpc_trace_auth_context_refcount)
78 ? "auth_context_refcount"
79 : nullptr),
80 chained_(std::move(chained)) {
81 if (chained_ != nullptr) {
82 peer_identity_property_name_ = chained_->peer_identity_property_name_;
83 }
84 }
85
86 ~grpc_auth_context() {
87 chained_.reset(DEBUG_LOCATION, "chained");
88 if (properties_.array != nullptr) {
89 for (size_t i = 0; i < properties_.count; i++) {
90 grpc_auth_property_reset(&properties_.array[i]);
91 }
92 gpr_free(properties_.array);
93 }
94 }
95
96 static absl::string_view ChannelArgName() { return GRPC_AUTH_CONTEXT_ARG; }
97 static int ChannelArgsCompare(const grpc_auth_context* a,
98 const grpc_auth_context* b) {
99 return QsortCompare(a, b);
100 }
101
102 const grpc_auth_context* chained() const { return chained_.get(); }
103 const grpc_auth_property_array& properties() const { return properties_; }
104
105 bool is_authenticated() const {
106 return peer_identity_property_name_ != nullptr;
107 }
108 const char* peer_identity_property_name() const {
109 return peer_identity_property_name_;
110 }
111 void set_peer_identity_property_name(const char* name) {
112 peer_identity_property_name_ = name;
113 }
114 void set_extension(std::unique_ptr<Extension> extension) {
115 extension_ = std::move(extension);
116 }
117
118 void ensure_capacity();
119 void add_property(const char* name, const char* value, size_t value_length);
120 void add_cstring_property(const char* name, const char* value);
121
122 private:
123 grpc_core::RefCountedPtr<grpc_auth_context> chained_;
124 grpc_auth_property_array properties_;
125 const char* peer_identity_property_name_ = nullptr;
126 std::unique_ptr<Extension> extension_;
127 };
128
129 /* --- grpc_security_context_extension ---
130
131 Extension to the security context that may be set in a filter and accessed
132 later by a higher level method on a grpc_call object. */
133
134 struct grpc_security_context_extension {
135 void* instance = nullptr;
136 void (*destroy)(void*) = nullptr;
137 };
138
139 /* --- grpc_client_security_context ---
140
141 Internal client-side security context. */
142
143 struct grpc_client_security_context {
144 explicit grpc_client_security_context(
145 grpc_core::RefCountedPtr<grpc_call_credentials> creds)
146 : creds(std::move(creds)) {}
147 ~grpc_client_security_context();
148
149 grpc_core::RefCountedPtr<grpc_call_credentials> creds;
150 grpc_core::RefCountedPtr<grpc_auth_context> auth_context;
151 grpc_security_context_extension extension;
152 };
153
154 grpc_client_security_context* grpc_client_security_context_create(
155 grpc_core::Arena* arena, grpc_call_credentials* creds);
156 void grpc_client_security_context_destroy(void* ctx);
157
158 /* --- grpc_server_security_context ---
159
160 Internal server-side security context. */
161
162 struct grpc_server_security_context {
163 grpc_server_security_context() = default;
164 ~grpc_server_security_context();
165
166 grpc_core::RefCountedPtr<grpc_auth_context> auth_context;
167 grpc_security_context_extension extension;
168 };
169
170 grpc_server_security_context* grpc_server_security_context_create(
171 grpc_core::Arena* arena);
172 void grpc_server_security_context_destroy(void* ctx);
173
174 /* --- Channel args for auth context --- */
175
176 grpc_arg grpc_auth_context_to_arg(grpc_auth_context* c);
177 grpc_auth_context* grpc_auth_context_from_arg(const grpc_arg* arg);
178 grpc_auth_context* grpc_find_auth_context_in_args(
179 const grpc_channel_args* args);
180
181 #endif /* GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H */