Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
license.hpp
Go to the documentation of this file.
1#pragma once
2
3// License key validation — plugin licensing.
4//
5// Format versions:
6// v1 — legacy: base64(json).base64(RSA_SHA256(json)). Asymmetric;
7// server holds an RSA private key, plugin embeds the public PEM.
8// Continues to validate for one release window for backward compat.
9// v2 — current: "v2." + base64url(IV || AES-256-GCM(json) || tag).
10// Symmetric; server + plugin share a 32-byte secret. Smaller keys,
11// no PEM parsing, and the GCM tag covers integrity (eliminates the
12// separate signature). HMAC-SHA256 is still available as a standalone
13// primitive for use cases (e.g. activation tokens) that need MAC
14// without payload encryption.
15
17#include <string>
18#include <string_view>
19#include <optional>
20#include <vector>
21#include <cstdint>
22
23namespace pulp::runtime {
24
28 V1 = 1,
29 V2 = 2,
30};
31
36
46
49 std::string product_id;
50 std::string user_email;
51 std::string machine_id; // Empty = any machine
52 int64_t issued_timestamp = 0;
53 int64_t expiry_timestamp = 0; // 0 = perpetual
54 std::string edition; // "standard", "pro", etc.
55};
56
61public:
62 LicenseValidator() = default;
63
66 void set_public_key(std::string_view pem);
67
70 void set_shared_secret(const uint8_t* secret_32, size_t secret_size);
71
74 LicenseStatus validate(std::string_view license_key) const;
75
77 std::optional<LicenseInfo> validate_and_parse(std::string_view license_key) const;
78
80 LicenseStatus validate_file(std::string_view path) const;
81
83 bool is_valid_for_machine(const LicenseInfo& info) const;
84
85private:
86 std::string public_key_pem_;
87 std::vector<uint8_t> shared_secret_;
88
89 std::optional<LicenseInfo> parse_payload(std::string_view json) const;
90 bool verify_signature(std::string_view payload, std::string_view signature_b64) const;
91
92 LicenseStatus validate_v1(std::string_view license_key) const;
93 LicenseStatus validate_v2(std::string_view license_key) const;
94};
95
100public:
101 LicenseGenerator() = default;
102
105 void set_private_key(std::string_view pem);
106
110 void set_shared_secret(const uint8_t* secret_32, size_t secret_size);
111
115 std::optional<std::string> generate(const LicenseInfo& info) const;
116
119 std::optional<std::string> generate_v1(const LicenseInfo& info) const;
120
122 std::optional<std::string> generate_v2(const LicenseInfo& info) const;
123
124private:
125 std::string private_key_pem_;
126 std::vector<uint8_t> shared_secret_;
127};
128
129// ── Format helpers and migration ────────────────────────────────────────
130
133std::optional<LicenseFormatVersion> detect_license_format(
134 std::string_view license_key);
135
146std::optional<std::string> migrate_v1_to_v2(
147 std::string_view v1_license_key,
148 std::string_view v1_public_key_pem,
149 const uint8_t* v2_secret_32,
150 size_t v2_secret_size);
151
161
168 static std::optional<std::string> activate(std::string_view server_url,
169 std::string_view serial_number,
170 std::string_view product_id,
171 const OnlineActivationOptions& options = {});
172
174 static bool deactivate(std::string_view server_url, std::string_view license_key,
175 const OnlineActivationOptions& options = {});
176
180 static LicenseStatus check_status(std::string_view server_url, std::string_view license_key,
181 const OnlineActivationOptions& options = {});
182
185 static bool server_url_allowed(std::string_view server_url,
186 const OnlineActivationOptions& options = {});
187
193 static LicenseStatus parse_status_response(std::string_view body);
194};
195
196} // namespace pulp::runtime
std::optional< std::string > generate_v1(const LicenseInfo &info) const
Force v1 (RSA-signed) license output.
void set_shared_secret(const uint8_t *secret_32, size_t secret_size)
Set the 32-byte symmetric secret for v2 AES-256-GCM license keys.
std::optional< std::string > generate_v2(const LicenseInfo &info) const
Force v2 (AES-256-GCM) license output. Used by migrate_v1_to_v2.
void set_private_key(std::string_view pem)
Set the RSA private key (PEM format) for signing v1 license keys.
std::optional< std::string > generate(const LicenseInfo &info) const
Generate a license key string in the current format (kCurrentLicenseFormat).
LicenseStatus validate(std::string_view license_key) const
Validate a license key string.
void set_public_key(std::string_view pem)
Set the RSA public key (PEM format) for v1 signature verification.
bool is_valid_for_machine(const LicenseInfo &info) const
Check if a license is valid for this machine.
std::optional< LicenseInfo > validate_and_parse(std::string_view license_key) const
Validate and extract license info.
void set_shared_secret(const uint8_t *secret_32, size_t secret_size)
Set the 32-byte symmetric secret used to decrypt v2 license keys.
LicenseStatus validate_file(std::string_view path) const
Validate a license key file.
Definition generated_event_source.hpp:33
std::optional< LicenseFormatVersion > detect_license_format(std::string_view license_key)
Detect the format version of a license key without parsing the payload.
std::optional< std::string > migrate_v1_to_v2(std::string_view v1_license_key, std::string_view v1_public_key_pem, const uint8_t *v2_secret_32, size_t v2_secret_size)
Re-encode a v1 license key as v2 using the supplied 32-byte secret.
LicenseStatus
License key status.
Definition license.hpp:38
@ Expired
Definition license.hpp:40
@ InvalidSignature
Definition license.hpp:41
@ NotFound
Definition license.hpp:44
@ Valid
Definition license.hpp:39
@ MachineIdMismatch
Definition license.hpp:43
@ InvalidFormat
Definition license.hpp:42
LicenseFormatVersion
License-key format version.
Definition license.hpp:27
@ V1
base64(json).base64(RSA_sig) — legacy
Definition license.hpp:28
@ V2
"v2." + base64url(IV || AES-256-GCM(json) || tag)
Definition license.hpp:29
constexpr LicenseFormatVersion kCurrentLicenseFormat
Latest format emitted by LicenseGenerator.
Definition license.hpp:34
Parsed license key data.
Definition license.hpp:48
int64_t issued_timestamp
Definition license.hpp:52
std::string user_email
Definition license.hpp:50
std::string product_id
Definition license.hpp:49
int64_t expiry_timestamp
Definition license.hpp:53
std::string edition
Definition license.hpp:54
std::string machine_id
Definition license.hpp:51
Transport policy for OnlineActivation.
Definition license.hpp:153
bool allow_insecure_http
Permit a plain http:// server_url.
Definition license.hpp:158
int timeout_seconds
Definition license.hpp:159
Online license activation flow.
Definition license.hpp:165
static LicenseStatus check_status(std::string_view server_url, std::string_view license_key, const OnlineActivationOptions &options={})
Check license status with the server.
static LicenseStatus parse_status_response(std::string_view body)
Exact parse of a /status response body: a JSON object whose status member is the string "valid" or "e...
static bool deactivate(std::string_view server_url, std::string_view license_key, const OnlineActivationOptions &options={})
Deactivate a license (free up a machine slot).
static std::optional< std::string > activate(std::string_view server_url, std::string_view serial_number, std::string_view product_id, const OnlineActivationOptions &options={})
Activate online with a serial number.
static bool server_url_allowed(std::string_view server_url, const OnlineActivationOptions &options={})
True when server_url is an https:// origin, or http:// and the options allow it.