Repository navigation
Expand file tree
/
Copy pathplugin_entry.cpp
More file actions
148 lines (130 loc) · 5.88 KB
/
Copy pathplugin_entry.cpp
File metadata and controls
148 lines (130 loc) · 5.88 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
// SPDX-License-Identifier: MIT
/// @file plugins/security/pkcs11/plugin_entry.cpp
/// @brief GoodNet plugin entry symbols for the PKCS#11 plugin.
///
/// v0.2: dual-expose — the same `.so` registers both
/// `gn.security.pkcs11` (transport-side, MVP shape) and
/// `gn.identity.pkcs11` (the identity-signer extension consumed by
/// `gn_core_install_identity_from_provider`). Both bind to the same
/// `Pkcs11Provider` so the long-lived `C_Initialize → C_OpenSession
/// → C_Login → C_FindObjects` chain only happens once.
#include "pkcs11_identity_ext.hpp"
#include "pkcs11_provider.hpp"
#include <sdk/abi.h>
#include <sdk/extensions/identity.h>
#include <sdk/host_api.h>
#include <sdk/plugin.h>
#include <sdk/security.h>
#include <sdk/types.h>
#include <cstdint>
#include <new>
namespace {
const gn_security_provider_vtable_t kVtable = gn::pkcs11::make_vtable();
/// Identity-side context — vtable first (kernel's `ctx = &vt`
/// convention), provider pointer second. Populated in `gn_plugin_init`
/// so the provider pointer is the freshly-constructed one. The
/// storage is `static` here so the kernel's `register_extension`
/// (which `@borrowed`s the vtable until `unregister_extension`) can
/// keep using it for the plugin's full lifetime.
gn::pkcs11::IdentitySignerCtx kIdentityCtx{};
const char* const kProvidesList[] = {
gn::pkcs11::kExtensionId,
gn::pkcs11::kIdentityExtensionId,
nullptr,
};
const gn_plugin_descriptor_t kDescriptor = {
/* name */ "goodnet_security_pkcs11",
/* version */ "0.2.0",
/* hot_reload_safe */ 0, /// PKCS#11 module owns process-global state
/* ext_requires */ nullptr,
/* ext_provides */ kProvidesList,
/* kind */ GN_PLUGIN_KIND_SECURITY,
/* _reserved */ {nullptr, nullptr, nullptr, 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 != nullptr) *major = GN_SDK_VERSION_MAJOR;
if (minor != nullptr) *minor = GN_SDK_VERSION_MINOR;
if (patch != nullptr) *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 == nullptr || out_self == nullptr) return GN_ERR_NULL_ARG;
auto* p = new (std::nothrow) gn::pkcs11::Pkcs11Provider();
if (p == nullptr) return GN_ERR_OUT_OF_MEMORY;
p->set_host(api, api->host_ctx);
/// Wire the identity-side vtable to the freshly-constructed
/// provider. Both extensions back the same on-token keypair —
/// `register_extension` runs later in `gn_plugin_register`.
gn::pkcs11::init_identity_vtable(kIdentityCtx, p);
*out_self = p;
/// `ready()` may be false here — for example when the operator
/// has not yet provisioned the token. Returning GN_OK lets the
/// plugin register itself so the kernel surfaces a clear
/// "provider available but unconfigured" state; the handshake
/// path returns GN_ERR_INVALID_STATE until provisioning lands.
return GN_OK;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_REGISTER_NAME(void* self) {
if (self == nullptr) return GN_ERR_NULL_ARG;
auto* p = static_cast<gn::pkcs11::Pkcs11Provider*>(self);
if (p->api() == nullptr || p->api()->register_security == nullptr) {
return GN_ERR_NOT_IMPLEMENTED;
}
const gn_result_t sec_rc = p->api()->register_security(
p->host_ctx(), gn::pkcs11::kProviderId, &kVtable, p);
if (sec_rc != GN_OK) return sec_rc;
/// Identity-side registration is additive — the dual-expose lets
/// the operator pick the new identity path
/// (`gn_core_install_identity_from_provider(core,
/// "gn.identity.pkcs11", "<CKA_LABEL>")`) without touching the
/// existing security-side wiring. The host MAY ship an older
/// kernel that does not yet expose `register_extension`; treat
/// that as a soft fallback rather than failing the whole plugin
/// init.
if (p->api()->register_extension != nullptr) {
const gn_result_t id_rc = p->api()->register_extension(
p->host_ctx(),
gn::pkcs11::kIdentityExtensionId,
GN_EXT_IDENTITY_SIGNER_VERSION,
&kIdentityCtx.vt);
if (id_rc != GN_OK) {
/// Roll back the security registration so the kernel
/// doesn't observe a half-registered plugin.
if (p->api()->unregister_security != nullptr) {
p->api()->unregister_security(p->host_ctx(),
gn::pkcs11::kProviderId);
}
return id_rc;
}
}
return GN_OK;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_UNREGISTER_NAME(void* self) {
if (self == nullptr) return GN_ERR_NULL_ARG;
auto* p = static_cast<gn::pkcs11::Pkcs11Provider*>(self);
if (p->api() == nullptr) return GN_OK;
/// Symmetric teardown — identity first, then security. Both are
/// idempotent in the kernel's registries, so order only matters
/// in that a dangling identity extension after security teardown
/// would still point at the soon-to-be-destroyed `Pkcs11Provider`.
if (p->api()->unregister_extension != nullptr) {
p->api()->unregister_extension(p->host_ctx(),
gn::pkcs11::kIdentityExtensionId);
}
if (p->api()->unregister_security == nullptr) {
return GN_OK;
}
return p->api()->unregister_security(p->host_ctx(),
gn::pkcs11::kProviderId);
}
GN_PLUGIN_EXPORT void GN_PLUGIN_SHUTDOWN_NAME(void* self) {
delete static_cast<gn::pkcs11::Pkcs11Provider*>(self);
}
GN_PLUGIN_EXPORT const gn_plugin_descriptor_t* GN_PLUGIN_DESCRIPTOR_NAME(void) {
return &kDescriptor;
}
} // extern "C"