Repository navigation
Expand file tree
/
Copy pathnull.cpp
More file actions
251 lines (215 loc) · 8.77 KB
/
Copy pathnull.cpp
File metadata and controls
251 lines (215 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/// @file plugins/security/null/null.cpp
/// @brief Null security provider — pass-through encrypt / decrypt.
///
/// For loopback, IPC, intra-node, and the "I trust this link"
/// paths permitted by `security-trust.en.md` §4 with explicit opt-in.
/// Handshake is a single no-op step that returns "complete" right
/// away; encrypt / decrypt copy the input buffer to a fresh
/// allocation so the kernel's contract on plugin-allocated output
/// (paired with @ref free_fn) holds uniformly across providers.
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <new>
#include <sdk/abi.h>
#include <sdk/host_api.h>
#include <sdk/plugin.h>
#include <sdk/security.h>
namespace {
/// Provider-private state. Carries nothing beyond the api pointer
/// the plugin needs to register the vtable. Per-connection state
/// is also empty for this provider.
struct NullPlugin {
const host_api_t* api = nullptr;
void* host_ctx = nullptr;
};
constexpr const char* kProviderId = "null";
const char* null_provider_id(void* /*self*/) {
return kProviderId;
}
gn_result_t null_handshake_open(void* /*self*/,
gn_conn_id_t /*conn*/,
gn_trust_class_t /*trust*/,
gn_handshake_role_t /*role*/,
const std::uint8_t* /*local_static_sk*/,
const std::uint8_t* /*local_static_pk*/,
const std::uint8_t* /*remote_static_pk*/,
void** out_state) {
if (!out_state) return GN_ERR_NULL_ARG;
/// No per-connection state for null security.
*out_state = nullptr;
return GN_OK;
}
gn_result_t null_handshake_step(void* /*self*/,
void* /*state*/,
const std::uint8_t* /*incoming*/,
std::size_t /*incoming_size*/,
gn_secure_buffer_t* out_message) {
if (!out_message) return GN_ERR_NULL_ARG;
/// Nothing to send; handshake already complete.
out_message->bytes = nullptr;
out_message->size = 0;
out_message->free_user_data = nullptr;
out_message->free_fn = nullptr;
return GN_OK;
}
int null_handshake_complete(void* /*self*/, void* /*state*/) {
/// Always complete — no rounds to run.
return 1;
}
gn_result_t null_export_transport_keys(void* /*self*/,
void* /*state*/,
gn_handshake_keys_t* out_keys) {
if (!out_keys) return GN_ERR_NULL_ARG;
/// Zero keys for the no-encryption path. The kernel still
/// records identity-binding through other means.
std::memset(out_keys, 0, sizeof(*out_keys));
return GN_OK;
}
void null_free_buffer(void* /*user_data*/, std::uint8_t* p) {
std::free(p);
}
[[nodiscard]] gn_result_t null_copy_through(const std::uint8_t* in,
std::size_t in_size,
gn_secure_buffer_t* out) {
if (!out) return GN_ERR_NULL_ARG;
if (in_size == 0) {
out->bytes = nullptr;
out->size = 0;
out->free_user_data = nullptr;
out->free_fn = nullptr;
return GN_OK;
}
auto* heap = static_cast<std::uint8_t*>(std::malloc(in_size));
if (!heap) return GN_ERR_OUT_OF_MEMORY;
std::memcpy(heap, in, in_size);
out->bytes = heap;
out->size = in_size;
out->free_user_data = nullptr;
out->free_fn = &null_free_buffer;
return GN_OK;
}
gn_result_t null_encrypt(void* /*self*/,
void* /*state*/,
const std::uint8_t* plaintext,
std::size_t plaintext_size,
gn_secure_buffer_t* out) {
return null_copy_through(plaintext, plaintext_size, out);
}
gn_result_t null_decrypt(void* /*self*/,
void* /*state*/,
const std::uint8_t* ciphertext,
std::size_t ciphertext_size,
gn_secure_buffer_t* out) {
return null_copy_through(ciphertext, ciphertext_size, out);
}
gn_result_t null_rekey(void* /*self*/, void* /*state*/) {
/// No keys to rotate.
return GN_OK;
}
void null_handshake_close(void* /*self*/, void* /*state*/) {
/// State is always nullptr.
}
void null_destroy(void* self) {
delete static_cast<NullPlugin*>(self);
}
/// Null provider is a passthrough — no encryption, no authentication.
/// Permitted only on inherently trusted classes per
/// `security-trust.en.md` §4: AF_UNIX-style loopback and intra-process
/// pipes. Public-network connections must not reach this provider.
std::uint32_t null_allowed_trust_mask(void* /*self*/) {
return (1u << GN_TRUST_LOOPBACK) | (1u << GN_TRUST_INTRA_NODE);
}
gn_security_provider_vtable_t make_null_vtable() {
gn_security_provider_vtable_t v{};
v.api_size = sizeof(gn_security_provider_vtable_t);
v.provider_id = &null_provider_id;
v.handshake_open = &null_handshake_open;
v.handshake_step = &null_handshake_step;
v.handshake_complete = &null_handshake_complete;
v.export_transport_keys = &null_export_transport_keys;
v.encrypt = &null_encrypt;
v.decrypt = &null_decrypt;
v.rekey = &null_rekey;
v.handshake_close = &null_handshake_close;
v.destroy = &null_destroy;
v.allowed_trust_mask = &null_allowed_trust_mask;
v.provides_flags = [](void*) noexcept -> std::uint32_t { return 0; };
return v;
}
const gn_security_provider_vtable_t kVtable = make_null_vtable();
/// link-only provider — same pass-through crypto as null, but permitted
/// on `GN_TRUST_LINK_ENCRYPTED` connections (link-layer TLS + operator
/// opt-in via `security.allow_link_only: true`).
constexpr const char* kLinkOnlyProviderId = "link-only";
const char* link_only_provider_id(void* /*self*/) {
return kLinkOnlyProviderId;
}
std::uint32_t link_only_allowed_trust_mask(void* /*self*/) {
return 1u << GN_TRUST_LINK_ENCRYPTED;
}
gn_security_provider_vtable_t make_link_only_vtable() {
gn_security_provider_vtable_t v = make_null_vtable();
v.provider_id = &link_only_provider_id;
v.allowed_trust_mask = &link_only_allowed_trust_mask;
return v;
}
const gn_security_provider_vtable_t kLinkOnlyVtable = make_link_only_vtable();
const char* const kProvidesList[] = {
"gn.security.null",
"gn.security.link-only",
nullptr,
};
const gn_plugin_descriptor_t kDescriptor = {
.name = "goodnet_security_null",
.version = "0.1.0",
.hot_reload_safe = 1,
.ext_requires = nullptr,
.ext_provides = kProvidesList,
.kind = GN_PLUGIN_KIND_SECURITY,
.inject_targets = nullptr,
};
} // namespace
extern "C" {
GN_PLUGIN_EXPORT void GN_PLUGIN_SDK_VERSION_NAME(std::uint32_t* major,
std::uint32_t* minor,
std::uint32_t* patch) {
if (major) *major = GN_SDK_VERSION_MAJOR;
if (minor) *minor = GN_SDK_VERSION_MINOR;
if (patch) *patch = GN_SDK_VERSION_PATCH;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_INIT_NAME(const host_api_t* api,
void** out_self) {
if (!api || !out_self) return GN_ERR_NULL_ARG;
auto* p = new (std::nothrow) NullPlugin{};
if (!p) return GN_ERR_OUT_OF_MEMORY;
p->api = api;
p->host_ctx = api->host_ctx;
*out_self = p;
return GN_OK;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_REGISTER_NAME(void* self) {
if (!self) return GN_ERR_NULL_ARG;
auto* p = static_cast<NullPlugin*>(self);
if (!p->api || !p->api->register_security) return GN_ERR_NOT_IMPLEMENTED;
// link-only is opportunistic: hosts with single-slot security registries
// keep null (registered last); multi-slot hosts get both.
(void)p->api->register_security(
p->host_ctx, kLinkOnlyProviderId, &kLinkOnlyVtable, p);
return p->api->register_security(p->host_ctx, kProviderId, &kVtable, p);
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_UNREGISTER_NAME(void* self) {
if (!self) return GN_ERR_NULL_ARG;
auto* p = static_cast<NullPlugin*>(self);
if (!p->api || !p->api->unregister_security) return GN_OK;
(void)p->api->unregister_security(p->host_ctx, kLinkOnlyProviderId);
(void)p->api->unregister_security(p->host_ctx, kProviderId);
return GN_OK;
}
GN_PLUGIN_EXPORT void GN_PLUGIN_SHUTDOWN_NAME(void* self) {
delete static_cast<NullPlugin*>(self);
}
GN_PLUGIN_EXPORT const gn_plugin_descriptor_t* GN_PLUGIN_DESCRIPTOR_NAME(void) {
return &kDescriptor;
}
} // extern "C"