ToolX 1.11.1 [e3f477f]
A GUI debug tool chain for diagnosing GVA systems
Loading...
Searching...
No Matches
AstuteSignatureVerifier.h
Go to the documentation of this file.
1//
2// Copyright (c) 2026, Astute Systems PTY LTD
3//
4// Ed25519 signature verification using ASTUTE_KEY environment variable.
5// Verifies that license server responses are cryptographically signed
6// by the Astute Systems signing key.
7//
11
12#ifndef COMMON_ASTUTE_SIGNATURE_VERIFIER_H
13#define COMMON_ASTUTE_SIGNATURE_VERIFIER_H
14
15#include <QByteArray>
16#include <QDebug>
17#include <QString>
18
19#include <openssl/evp.h>
20#include <openssl/x509.h>
21
22#include <cstdlib>
23#include <memory>
24#include <string>
25
26namespace Astute {
27
43{
44public:
47 explicit SignatureVerifier(const QByteArray &base64PublicKey)
48 {
49 QByteArray derKey = QByteArray::fromBase64(base64PublicKey);
50 if (derKey.isEmpty()) {
51 qWarning() << "Signature verifier: Failed to decode base64 public key";
52 return;
53 }
54
55 const unsigned char *derData = reinterpret_cast<const unsigned char *>(derKey.constData());
56 m_pkey = d2i_PUBKEY(nullptr, &derData, derKey.size());
57 if (!m_pkey) {
58 qWarning() << "Signature verifier: Failed to parse Ed25519 public key from DER";
59 return;
60 }
61
62 // Verify it's an Ed25519 key
63 if (EVP_PKEY_id(m_pkey) != EVP_PKEY_ED25519) {
64 qWarning() << "Signature verifier: Key is not Ed25519 (type:" << EVP_PKEY_id(m_pkey)
65 << ")";
66 EVP_PKEY_free(m_pkey);
67 m_pkey = nullptr;
68 return;
69 }
70
71 m_valid = true;
72 }
73
75 {
76 if (m_pkey) {
77 EVP_PKEY_free(m_pkey);
78 }
79 }
80
81 // Non-copyable
82 SignatureVerifier(const SignatureVerifier &) = delete;
83 SignatureVerifier &operator=(const SignatureVerifier &) = delete;
84
85 // Movable
86 SignatureVerifier(SignatureVerifier &&other) noexcept
87 : m_pkey(other.m_pkey), m_valid(other.m_valid)
88 {
89 other.m_pkey = nullptr;
90 other.m_valid = false;
91 }
92
98 static std::unique_ptr<SignatureVerifier> create()
99 {
100 // 1. Try compile-time embedded key first
101#ifdef ASTUTE_SIGNING_KEY
102 {
103 constexpr const char *kEmbeddedKey = ASTUTE_SIGNING_KEY;
104 if (kEmbeddedKey[0] != '\0') {
105 auto verifier = std::make_unique<SignatureVerifier>(QByteArray(kEmbeddedKey));
106 if (verifier->isValid()) {
107 qInfo() << "Signature verifier: Using compiled-in signing key";
108 return verifier;
109 }
110 qWarning() << "Signature verifier: Compiled-in key is invalid";
111 }
112 }
113#endif
114 // 2. Fallback to environment variable
115 const char *keyEnv = std::getenv("ASTUTE_KEY");
116 if (keyEnv && keyEnv[0] != '\0') {
117 auto verifier = std::make_unique<SignatureVerifier>(QByteArray(keyEnv));
118 if (verifier->isValid()) {
119 qInfo() << "Signature verifier: Using ASTUTE_KEY from environment";
120 return verifier;
121 }
122 qWarning() << "Signature verifier: ASTUTE_KEY environment variable contains "
123 "invalid key";
124 }
125
126 qWarning() << "Signature verifier: No signing key available (not compiled in, "
127 "ASTUTE_KEY not set)";
128 return nullptr;
129 }
130
134 static std::unique_ptr<SignatureVerifier> fromEnvironment() { return create(); }
135
138 bool isValid() const { return m_valid; }
139
144 bool verify(const QByteArray &message, const QByteArray &signature) const
145 {
146 if (!m_valid) {
147 qWarning() << "Signature verifier: Cannot verify - verifier not initialised";
148 return false;
149 }
150
151 // Accept either raw 64-byte signature or base64-encoded
152 QByteArray rawSig = signature;
153 if (signature.size() != 64) {
154 rawSig = QByteArray::fromBase64(signature);
155 if (rawSig.size() != 64) {
156 qWarning() << "Signature verifier: Invalid signature length:" << rawSig.size()
157 << "(expected 64)";
158 return false;
159 }
160 }
161
162 EVP_MD_CTX *mdCtx = EVP_MD_CTX_new();
163 if (!mdCtx) {
164 qWarning() << "Signature verifier: Failed to create EVP_MD_CTX";
165 return false;
166 }
167
168 bool result = false;
169
170 if (EVP_DigestVerifyInit(mdCtx, nullptr, nullptr, nullptr, m_pkey) == 1) {
171 int rc = EVP_DigestVerify(mdCtx,
172 reinterpret_cast<const unsigned char *>(rawSig.constData()),
173 static_cast<size_t>(rawSig.size()),
174 reinterpret_cast<const unsigned char *>(message.constData()),
175 static_cast<size_t>(message.size()));
176 result = (rc == 1);
177 }
178
179 EVP_MD_CTX_free(mdCtx);
180 return result;
181 }
182
189 bool verifySignedPayload(const QByteArray &signedPayload) const
190 {
191 int dotIndex = signedPayload.lastIndexOf('.');
192 if (dotIndex < 0) {
193 qWarning() << "Signature verifier: No '.' separator found in signed payload";
194 return false;
195 }
196
197 QByteArray messagePart = signedPayload.left(dotIndex);
198 QByteArray signaturePart = signedPayload.mid(dotIndex + 1);
199
200 return verify(messagePart, signaturePart);
201 }
202
203private:
204 EVP_PKEY *m_pkey = nullptr;
205 bool m_valid = false;
206};
207
208} // namespace Astute
209
210#endif // COMMON_ASTUTE_SIGNATURE_VERIFIER_H
Verifies Ed25519 signatures using the ASTUTE_KEY public key.
Definition AstuteSignatureVerifier.h:43
bool verifySignedPayload(const QByteArray &signedPayload) const
Verify a signed payload in the format "message.signature".
Definition AstuteSignatureVerifier.h:189
static std::unique_ptr< SignatureVerifier > fromEnvironment()
Create a verifier from the ASTUTE_KEY environment variable (legacy).
Definition AstuteSignatureVerifier.h:134
static std::unique_ptr< SignatureVerifier > create()
Create a verifier using the compiled-in signing key.
Definition AstuteSignatureVerifier.h:98
bool isValid() const
Check if the verifier was initialised successfully.
Definition AstuteSignatureVerifier.h:138
bool verify(const QByteArray &message, const QByteArray &signature) const
Verify an Ed25519 signature over a message.
Definition AstuteSignatureVerifier.h:144
SignatureVerifier(const QByteArray &base64PublicKey)
Construct a verifier from a base64-encoded Ed25519 public key.
Definition AstuteSignatureVerifier.h:47