@xkwdstore/agent 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Shared Ed25519 key codec (Defect 2).
5
+ *
6
+ * The canonical representation across the entire system is:
7
+ * - Transmit the raw 32-byte Ed25519 public key as base64url.
8
+ * - Validate decoded length is exactly 32.
9
+ * - Wrap with the canonical Ed25519 SPKI DER prefix before calling
10
+ * crypto.createPublicKey.
11
+ * - Calculate the fingerprint from the raw 32 bytes: SHA-256(publicKey).
12
+ * - Reject non-canonical base64url values.
13
+ * - Reject malformed or oversized inputs.
14
+ *
15
+ * The Ed25519 SPKI DER prefix (12 bytes, RFC 8410):
16
+ * 30 2a 30 05 06 03 2b 65 70 03 21 00
17
+ *
18
+ * Used by: enrollment route, device registry, WebSocket challenge verifier,
19
+ * CLI key generation, and tests.
20
+ */
21
+
22
+ const crypto = require('crypto');
23
+
24
+ // Ed25519 SPKI DER prefix (12 bytes)
25
+ const SPKI_PREFIX = Buffer.from('302a300506032b6570032100', 'hex');
26
+
27
+ // Strict base64url alphabet (RFC 4648 Section 5, no padding)
28
+ const BASE64URL_RE = /^[A-Za-z0-9_-]+$/;
29
+
30
+ /**
31
+ * Decode a base64url-encoded raw Ed25519 public key.
32
+ *
33
+ * @param {string} publicKeyB64Url — base64url-encoded 32-byte raw key
34
+ * @returns {Buffer} raw 32-byte public key
35
+ * @throws {Error} if the input is malformed, wrong length, or not canonical base64url
36
+ */
37
+ function decodePublicKey(publicKeyB64Url) {
38
+ if (typeof publicKeyB64Url !== 'string' || publicKeyB64Url.length === 0) {
39
+ throw new Error('ED25519_KEY_EMPTY');
40
+ }
41
+ if (!BASE64URL_RE.test(publicKeyB64Url)) {
42
+ throw new Error('ED25519_KEY_NOT_BASE64URL');
43
+ }
44
+
45
+ const raw = Buffer.from(publicKeyB64Url, 'base64url');
46
+ if (raw.length !== 32) {
47
+ throw new Error('ED25519_KEY_WRONG_LENGTH');
48
+ }
49
+
50
+ return raw;
51
+ }
52
+
53
+ /**
54
+ * Validate a base64url-encoded raw Ed25519 public key.
55
+ * @returns {boolean} true if valid
56
+ */
57
+ function isValidPublicKey(publicKeyB64Url) {
58
+ try {
59
+ decodePublicKey(publicKeyB64Url);
60
+ return true;
61
+ } catch {
62
+ return false;
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Wrap a raw 32-byte Ed25519 public key in a DER SPKI structure.
68
+ * @param {Buffer} rawKey — exactly 32 bytes
69
+ * @returns {Buffer} DER SPKI (44 bytes)
70
+ */
71
+ function wrapSpki(rawKey) {
72
+ if (!Buffer.isBuffer(rawKey) || rawKey.length !== 32) {
73
+ throw new Error('ED25519_KEY_WRONG_LENGTH');
74
+ }
75
+ return Buffer.concat([SPKI_PREFIX, rawKey]);
76
+ }
77
+
78
+ /**
79
+ * Create a Node.js KeyObject from a base64url raw Ed25519 public key.
80
+ * @param {string} publicKeyB64Url
81
+ * @returns {crypto.KeyObject}
82
+ */
83
+ function createPublicKeyFromB64Url(publicKeyB64Url) {
84
+ const raw = decodePublicKey(publicKeyB64Url);
85
+ const spki = wrapSpki(raw);
86
+ return crypto.createPublicKey({ key: spki, format: 'der', type: 'spki' });
87
+ }
88
+
89
+ /**
90
+ * Compute the SHA-256 fingerprint of a base64url raw Ed25519 public key.
91
+ * @param {string} publicKeyB64Url
92
+ * @returns {string} 64-char hex fingerprint
93
+ */
94
+ function computeFingerprint(publicKeyB64Url) {
95
+ const raw = decodePublicKey(publicKeyB64Url);
96
+ return crypto.createHash('sha256').update(raw).digest('hex');
97
+ }
98
+
99
+ /**
100
+ * Verify that a claimed fingerprint matches the public key.
101
+ * @param {string} publicKeyB64Url
102
+ * @param {string} claimedFingerprint — 64-char hex
103
+ * @returns {boolean}
104
+ */
105
+ function verifyFingerprint(publicKeyB64Url, claimedFingerprint) {
106
+ try {
107
+ const computed = computeFingerprint(publicKeyB64Url);
108
+ // Constant-time comparison
109
+ return crypto.timingSafeEqual(
110
+ Buffer.from(computed, 'hex'),
111
+ Buffer.from(claimedFingerprint, 'hex'),
112
+ );
113
+ } catch {
114
+ return false;
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Verify an Ed25519 signature over a message.
120
+ *
121
+ * @param {string} publicKeyB64Url — base64url raw 32-byte key
122
+ * @param {Buffer} message — message bytes
123
+ * @param {string} signatureB64Url — base64url signature
124
+ * @returns {boolean}
125
+ */
126
+ function verifySignature(publicKeyB64Url, message, signatureB64Url) {
127
+ try {
128
+ const pubKey = createPublicKeyFromB64Url(publicKeyB64Url);
129
+ const signature = Buffer.from(signatureB64Url, 'base64url');
130
+ return crypto.verify(null, message, pubKey, signature);
131
+ } catch {
132
+ return false;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Export a raw 32-byte Ed25519 public key from a Node.js KeyObject.
138
+ * @param {crypto.KeyObject} publicKey
139
+ * @returns {string} base64url-encoded raw 32 bytes
140
+ */
141
+ function exportPublicKeyB64Url(publicKey) {
142
+ const der = publicKey.export({ type: 'spki', format: 'der' });
143
+ // The raw key is the last 32 bytes of the SPKI DER
144
+ const raw = der.slice(-32);
145
+ return raw.toString('base64url');
146
+ }
147
+
148
+ module.exports = {
149
+ decodePublicKey,
150
+ isValidPublicKey,
151
+ wrapSpki,
152
+ createPublicKeyFromB64Url,
153
+ computeFingerprint,
154
+ verifyFingerprint,
155
+ verifySignature,
156
+ exportPublicKeyB64Url,
157
+ SPKI_PREFIX,
158
+ };