@codeo-engineering/licenserail-embedded 0.0.0-stage → 0.1.1
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.
- package/LICENSE +21 -0
- package/README.md +53 -2
- package/dist/cjs/client.js +115 -0
- package/dist/cjs/encoding.js +95 -0
- package/dist/cjs/evaluate.js +47 -0
- package/dist/cjs/fingerprint.js +50 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/p256.js +117 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/protocol.js +104 -0
- package/dist/cjs/sha256.js +60 -0
- package/dist/cjs/token.js +63 -0
- package/dist/esm/client.d.ts +60 -0
- package/dist/esm/client.js +112 -0
- package/dist/esm/encoding.d.ts +8 -0
- package/dist/esm/encoding.js +89 -0
- package/dist/esm/evaluate.d.ts +21 -0
- package/dist/esm/evaluate.js +41 -0
- package/dist/esm/fingerprint.d.ts +18 -0
- package/dist/esm/fingerprint.js +43 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/p256.d.ts +8 -0
- package/dist/esm/p256.js +113 -0
- package/dist/esm/protocol.d.ts +81 -0
- package/dist/esm/protocol.js +95 -0
- package/dist/esm/sha256.d.ts +2 -0
- package/dist/esm/sha256.js +57 -0
- package/dist/esm/token.d.ts +46 -0
- package/dist/esm/token.js +58 -0
- package/package.json +29 -4
package/dist/esm/p256.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { base64Decode } from './encoding.js';
|
|
2
|
+
import { sha256 } from './sha256.js';
|
|
3
|
+
// ECDSA P-256 / SHA-256 signature verification with BigInt (ES2020), for hosts without crypto.subtle.
|
|
4
|
+
// Verify-only: no secret data is involved, so constant-time arithmetic is not needed. Checked against the Wycheproof
|
|
5
|
+
// ecdsa_secp256r1_sha256_p1363 vectors (test/wycheproof.test.mjs).
|
|
6
|
+
const P = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn;
|
|
7
|
+
const N = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
|
|
8
|
+
const B = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604bn;
|
|
9
|
+
const G = {
|
|
10
|
+
x: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
|
|
11
|
+
y: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
|
|
12
|
+
z: 1n,
|
|
13
|
+
};
|
|
14
|
+
const INFINITY = { x: 0n, y: 1n, z: 0n };
|
|
15
|
+
function toInt(bytes) {
|
|
16
|
+
let r = 0n;
|
|
17
|
+
for (let i = 0; i < bytes.length; i++)
|
|
18
|
+
r = (r << 8n) | BigInt(bytes[i]);
|
|
19
|
+
return r;
|
|
20
|
+
}
|
|
21
|
+
function mod(a, m) {
|
|
22
|
+
const r = a % m;
|
|
23
|
+
return r < 0n ? r + m : r;
|
|
24
|
+
}
|
|
25
|
+
function pow(a, e, m) {
|
|
26
|
+
let r = 1n;
|
|
27
|
+
a = mod(a, m);
|
|
28
|
+
while (e > 0n) {
|
|
29
|
+
if (e & 1n)
|
|
30
|
+
r = (r * a) % m;
|
|
31
|
+
a = (a * a) % m;
|
|
32
|
+
e >>= 1n;
|
|
33
|
+
}
|
|
34
|
+
return r;
|
|
35
|
+
}
|
|
36
|
+
const invert = (a, m) => pow(a, m - 2n, m); // m is prime
|
|
37
|
+
// Curve a = -3.
|
|
38
|
+
function double(p) {
|
|
39
|
+
if (p.z === 0n || p.y === 0n)
|
|
40
|
+
return INFINITY;
|
|
41
|
+
const delta = (p.z * p.z) % P, gamma = (p.y * p.y) % P, beta = (p.x * gamma) % P;
|
|
42
|
+
const alpha = (3n * mod(p.x - delta, P) * (p.x + delta)) % P;
|
|
43
|
+
const x = mod(alpha * alpha - 8n * beta, P);
|
|
44
|
+
const z = mod((p.y + p.z) * (p.y + p.z) - gamma - delta, P);
|
|
45
|
+
const y = mod(alpha * (4n * beta - x) - 8n * gamma * gamma, P);
|
|
46
|
+
return { x, y, z };
|
|
47
|
+
}
|
|
48
|
+
function add(p, q) {
|
|
49
|
+
if (p.z === 0n)
|
|
50
|
+
return q;
|
|
51
|
+
if (q.z === 0n)
|
|
52
|
+
return p;
|
|
53
|
+
const z1z1 = (p.z * p.z) % P, z2z2 = (q.z * q.z) % P;
|
|
54
|
+
const u1 = (p.x * z2z2) % P, u2 = (q.x * z1z1) % P;
|
|
55
|
+
const s1 = (((p.y * q.z) % P) * z2z2) % P, s2 = (((q.y * p.z) % P) * z1z1) % P;
|
|
56
|
+
const h = mod(u2 - u1, P), r = mod(s2 - s1, P);
|
|
57
|
+
if (h === 0n)
|
|
58
|
+
return r === 0n ? double(p) : INFINITY;
|
|
59
|
+
const hh = (h * h) % P, hhh = (hh * h) % P, v = (u1 * hh) % P;
|
|
60
|
+
const x = mod(r * r - hhh - 2n * v, P);
|
|
61
|
+
const y = mod(r * (v - x) - s1 * hhh, P);
|
|
62
|
+
return { x, y, z: (((p.z * q.z) % P) * h) % P };
|
|
63
|
+
}
|
|
64
|
+
/** u1·G + u2·Q in one pass (Shamir's trick). */
|
|
65
|
+
function mulAdd(u1, u2, q) {
|
|
66
|
+
const gq = add(G, q);
|
|
67
|
+
let r = INFINITY;
|
|
68
|
+
for (let i = 255; i >= 0; i--) {
|
|
69
|
+
r = double(r);
|
|
70
|
+
const b1 = (u1 >> BigInt(i)) & 1n, b2 = (u2 >> BigInt(i)) & 1n;
|
|
71
|
+
if (b1 && b2)
|
|
72
|
+
r = add(r, gq);
|
|
73
|
+
else if (b1)
|
|
74
|
+
r = add(r, G);
|
|
75
|
+
else if (b2)
|
|
76
|
+
r = add(r, q);
|
|
77
|
+
}
|
|
78
|
+
return r;
|
|
79
|
+
}
|
|
80
|
+
// An uncompressed P-256 SubjectPublicKeyInfo is always 91 bytes: this fixed header, 0x04, X (32), Y (32).
|
|
81
|
+
const SPKI_HEADER = [0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
|
|
82
|
+
0x03, 0x01, 0x07, 0x03, 0x42, 0x00];
|
|
83
|
+
/** The public point of a PEM (SPKI) P-256 key, or null if it is anything else. */
|
|
84
|
+
export function parsePublicKey(pem) {
|
|
85
|
+
const der = base64Decode(pem.replace(/-----(BEGIN|END) PUBLIC KEY-----/g, ''));
|
|
86
|
+
if (!der || der.length !== 91 || der[26] !== 0x04)
|
|
87
|
+
return null;
|
|
88
|
+
for (let i = 0; i < SPKI_HEADER.length; i++)
|
|
89
|
+
if (der[i] !== SPKI_HEADER[i])
|
|
90
|
+
return null;
|
|
91
|
+
return { x: der.slice(27, 59), y: der.slice(59, 91) };
|
|
92
|
+
}
|
|
93
|
+
/** Verifies an IEEE P1363 (r || s, 64 bytes) ECDSA P-256 SHA-256 signature over `data`. */
|
|
94
|
+
export function verifyP256(publicKeyPem, data, signature) {
|
|
95
|
+
const key = parsePublicKey(publicKeyPem);
|
|
96
|
+
if (!key || signature.length !== 64)
|
|
97
|
+
return false;
|
|
98
|
+
const qx = toInt(key.x), qy = toInt(key.y);
|
|
99
|
+
if (qx >= P || qy >= P || mod(qy * qy - (qx * qx * qx - 3n * qx + B), P) !== 0n)
|
|
100
|
+
return false; // on the curve
|
|
101
|
+
const sig = Array.prototype.slice.call(signature);
|
|
102
|
+
const r = toInt(sig.slice(0, 32)), s = toInt(sig.slice(32, 64));
|
|
103
|
+
if (r < 1n || r >= N || s < 1n || s >= N)
|
|
104
|
+
return false;
|
|
105
|
+
const e = toInt(sha256(data)); // a 256-bit hash needs no truncation for P-256
|
|
106
|
+
const w = invert(s, N);
|
|
107
|
+
const point = mulAdd((e * w) % N, (r * w) % N, { x: qx, y: qy, z: 1n });
|
|
108
|
+
if (point.z === 0n)
|
|
109
|
+
return false;
|
|
110
|
+
const zi = invert(point.z, P);
|
|
111
|
+
const x = (((point.x * zi) % P) * zi) % P;
|
|
112
|
+
return x % N === r;
|
|
113
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Fingerprint, LicensePayload } from './token.js';
|
|
2
|
+
export interface HttpRequestDescription {
|
|
3
|
+
method: 'POST';
|
|
4
|
+
url: string;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
/** JSON text. */
|
|
7
|
+
body: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Target {
|
|
10
|
+
/** e.g. https://api.licenserail.com */
|
|
11
|
+
apiUrl: string;
|
|
12
|
+
product: string;
|
|
13
|
+
fingerprint: Fingerprint;
|
|
14
|
+
}
|
|
15
|
+
/** Header that makes the server rate-limit per installation instead of per IP (apps inside a host share its IPs). */
|
|
16
|
+
export declare const INSTALLATION_HEADER = "LicenseRail-Installation";
|
|
17
|
+
export interface ActivateOptions extends Target {
|
|
18
|
+
/**
|
|
19
|
+
* The license key. Omit it when the host adds `Authorization: Bearer <key>` itself (YouTrack: the key is a secret setting
|
|
20
|
+
* that only `connection.bearerAuth(ctx.settings.licenseKey)` can send).
|
|
21
|
+
*/
|
|
22
|
+
licenseKey?: string;
|
|
23
|
+
/** Send the key as `Authorization: Bearer` instead of in the body (default true). */
|
|
24
|
+
keyInHeader?: boolean;
|
|
25
|
+
machineName?: string;
|
|
26
|
+
appVersion?: string;
|
|
27
|
+
/** The host's current number of users (checked against the license's host user limit). */
|
|
28
|
+
hostUsers?: number;
|
|
29
|
+
/** Floating license: check out a lease instead of activating. */
|
|
30
|
+
floating?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare function activateRequest(o: ActivateOptions): HttpRequestDescription;
|
|
33
|
+
export interface RefreshOptions extends Target {
|
|
34
|
+
token: string;
|
|
35
|
+
appVersion?: string;
|
|
36
|
+
hostUsers?: number;
|
|
37
|
+
anomalies?: string[];
|
|
38
|
+
floating?: boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare function refreshRequest(o: RefreshOptions): HttpRequestDescription;
|
|
41
|
+
export declare function deactivateRequest(o: Target & {
|
|
42
|
+
token: string;
|
|
43
|
+
floating?: boolean;
|
|
44
|
+
}): HttpRequestDescription;
|
|
45
|
+
/**
|
|
46
|
+
* The host installation a trial is for. The server asks it for its id before a trial starts (YouTrack: the uuid that
|
|
47
|
+
* /api/config returns at baseUrls), so it must be reachable from the internet; otherwise the vendor gives a trial key.
|
|
48
|
+
*/
|
|
49
|
+
export interface TrialHost {
|
|
50
|
+
instanceId: string;
|
|
51
|
+
baseUrls?: string[];
|
|
52
|
+
}
|
|
53
|
+
export interface TrialOptions extends Target {
|
|
54
|
+
/** The environment's signing key id (public; shown with the public key in the dashboard). */
|
|
55
|
+
keyId: string;
|
|
56
|
+
/** Required for apps inside a host (fingerprint made with installationFingerprint from the same instance id). */
|
|
57
|
+
host?: TrialHost;
|
|
58
|
+
machineName?: string;
|
|
59
|
+
appVersion?: string;
|
|
60
|
+
hostUsers?: number;
|
|
61
|
+
}
|
|
62
|
+
export declare function trialRequest(o: TrialOptions): HttpRequestDescription;
|
|
63
|
+
/** Codes on which the local license must be removed; on all others (network errors, 402, 429, 5xx) the app keeps it. */
|
|
64
|
+
export declare const REVOKING_CODES: readonly string[];
|
|
65
|
+
export type ClientResponse = {
|
|
66
|
+
ok: true;
|
|
67
|
+
token: string;
|
|
68
|
+
} | {
|
|
69
|
+
ok: true;
|
|
70
|
+
token: null;
|
|
71
|
+
} | {
|
|
72
|
+
ok: false;
|
|
73
|
+
code: string;
|
|
74
|
+
message: string;
|
|
75
|
+
revokesLocalLicense: boolean;
|
|
76
|
+
retryLater: boolean;
|
|
77
|
+
};
|
|
78
|
+
/** Reads a client API response. `status` 0 means the request failed before any response (network error, timeout). */
|
|
79
|
+
export declare function readResponse(status: number, bodyText: string | null | undefined): ClientResponse;
|
|
80
|
+
/** When the token wants to be refreshed (null for trials, which are never refreshed). */
|
|
81
|
+
export declare function refreshDueAt(payload: LicensePayload): Date | null;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** Header that makes the server rate-limit per installation instead of per IP (apps inside a host share its IPs). */
|
|
2
|
+
export const INSTALLATION_HEADER = 'LicenseRail-Installation';
|
|
3
|
+
function url(apiUrl, path) {
|
|
4
|
+
return apiUrl.replace(/\/+$/, '') + '/api/client/v2' + path;
|
|
5
|
+
}
|
|
6
|
+
function headers(fingerprint) {
|
|
7
|
+
const h = { 'Content-Type': 'application/json' };
|
|
8
|
+
if (fingerprint.os)
|
|
9
|
+
h[INSTALLATION_HEADER] = `${fingerprint.os}:${fingerprint.machineId}`.slice(0, 128);
|
|
10
|
+
return h;
|
|
11
|
+
}
|
|
12
|
+
function clean(o) {
|
|
13
|
+
const out = {};
|
|
14
|
+
for (const k of Object.keys(o))
|
|
15
|
+
if (o[k] !== undefined && o[k] !== null)
|
|
16
|
+
out[k] = o[k];
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
export function activateRequest(o) {
|
|
20
|
+
const h = headers(o.fingerprint);
|
|
21
|
+
const inHeader = o.keyInHeader !== false;
|
|
22
|
+
if (o.licenseKey && inHeader)
|
|
23
|
+
h.Authorization = `Bearer ${o.licenseKey}`;
|
|
24
|
+
return {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
url: url(o.apiUrl, o.floating ? '/floating/checkout' : '/activate'),
|
|
27
|
+
headers: h,
|
|
28
|
+
body: JSON.stringify(clean({
|
|
29
|
+
product: o.product, fingerprint: o.fingerprint, licenseKey: inHeader ? undefined : o.licenseKey,
|
|
30
|
+
machineName: o.machineName, appVersion: o.appVersion, hostUsers: o.hostUsers,
|
|
31
|
+
})),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function refreshRequest(o) {
|
|
35
|
+
return {
|
|
36
|
+
method: 'POST',
|
|
37
|
+
url: url(o.apiUrl, o.floating ? '/floating/heartbeat' : '/refresh'),
|
|
38
|
+
headers: headers(o.fingerprint),
|
|
39
|
+
// appVersion is a required field of the refresh request (null allowed).
|
|
40
|
+
body: JSON.stringify({ ...clean({ token: o.token, fingerprint: o.fingerprint, anomalies: o.anomalies, hostUsers: o.hostUsers }), appVersion: o.appVersion ?? null }),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function deactivateRequest(o) {
|
|
44
|
+
return {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
url: url(o.apiUrl, o.floating ? '/floating/release' : '/deactivate'),
|
|
47
|
+
headers: headers(o.fingerprint),
|
|
48
|
+
body: JSON.stringify({ token: o.token, fingerprint: o.fingerprint }),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export function trialRequest(o) {
|
|
52
|
+
return {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
url: url(o.apiUrl, '/trial'),
|
|
55
|
+
headers: headers(o.fingerprint),
|
|
56
|
+
body: JSON.stringify(clean({
|
|
57
|
+
keyId: o.keyId, product: o.product, fingerprint: o.fingerprint, machineName: o.machineName, appVersion: o.appVersion, hostUsers: o.hostUsers,
|
|
58
|
+
host: o.host,
|
|
59
|
+
})),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** Codes on which the local license must be removed; on all others (network errors, 402, 429, 5xx) the app keeps it. */
|
|
63
|
+
export const REVOKING_CODES = [
|
|
64
|
+
'license_not_found', 'license_suspended', 'license_expired', 'activation_revoked', 'wrong_machine', 'invalid_token', 'license_revoked',
|
|
65
|
+
];
|
|
66
|
+
/** Reads a client API response. `status` 0 means the request failed before any response (network error, timeout). */
|
|
67
|
+
export function readResponse(status, bodyText) {
|
|
68
|
+
let body = null;
|
|
69
|
+
try {
|
|
70
|
+
body = bodyText ? JSON.parse(bodyText) : null;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
body = null;
|
|
74
|
+
}
|
|
75
|
+
if (status >= 200 && status < 300) {
|
|
76
|
+
if (status === 204)
|
|
77
|
+
return { ok: true, token: null };
|
|
78
|
+
// Anything else without a token isn't our server's answer (a captive portal, a proxy): an error, so nothing is removed.
|
|
79
|
+
if (body && typeof body.token === 'string')
|
|
80
|
+
return { ok: true, token: body.token };
|
|
81
|
+
}
|
|
82
|
+
const code = body && typeof body.code === 'string' ? body.code : status === 0 ? 'network_error' : `http_${status}`;
|
|
83
|
+
const message = body && typeof body.message === 'string' ? body.message : status === 0 ? 'The license server could not be reached.' : 'Unexpected response from the license server.';
|
|
84
|
+
return {
|
|
85
|
+
ok: false, code, message,
|
|
86
|
+
revokesLocalLicense: REVOKING_CODES.indexOf(code) >= 0,
|
|
87
|
+
retryLater: status === 0 || status === 429 || status === 402 || status >= 500,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** When the token wants to be refreshed (null for trials, which are never refreshed). */
|
|
91
|
+
export function refreshDueAt(payload) {
|
|
92
|
+
if (payload.trial === true)
|
|
93
|
+
return null;
|
|
94
|
+
return new Date(payload.refreshAfter);
|
|
95
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// SHA-256 (FIPS 180-4) in plain JavaScript, for hosts without crypto.subtle or node:crypto.
|
|
2
|
+
const K = [
|
|
3
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be,
|
|
4
|
+
0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa,
|
|
5
|
+
0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85,
|
|
6
|
+
0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
|
7
|
+
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
|
|
8
|
+
0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
9
|
+
];
|
|
10
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
11
|
+
export function sha256(bytes) {
|
|
12
|
+
const m = Array.prototype.slice.call(bytes);
|
|
13
|
+
const bitLength = bytes.length * 8;
|
|
14
|
+
m.push(0x80);
|
|
15
|
+
while (m.length % 64 !== 56)
|
|
16
|
+
m.push(0);
|
|
17
|
+
const hi = Math.floor(bitLength / 0x100000000), lo = bitLength >>> 0;
|
|
18
|
+
m.push(hi >>> 24, (hi >>> 16) & 255, (hi >>> 8) & 255, hi & 255, lo >>> 24, (lo >>> 16) & 255, (lo >>> 8) & 255, lo & 255);
|
|
19
|
+
const h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
20
|
+
const w = new Array(64);
|
|
21
|
+
for (let off = 0; off < m.length; off += 64) {
|
|
22
|
+
for (let t = 0; t < 64; t++) {
|
|
23
|
+
if (t < 16)
|
|
24
|
+
w[t] = (m[off + 4 * t] << 24) | (m[off + 4 * t + 1] << 16) | (m[off + 4 * t + 2] << 8) | m[off + 4 * t + 3];
|
|
25
|
+
else {
|
|
26
|
+
const s0 = rotr(w[t - 15], 7) ^ rotr(w[t - 15], 18) ^ (w[t - 15] >>> 3);
|
|
27
|
+
const s1 = rotr(w[t - 2], 17) ^ rotr(w[t - 2], 19) ^ (w[t - 2] >>> 10);
|
|
28
|
+
w[t] = (w[t - 16] + s0 + w[t - 7] + s1) | 0;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
let [a, b, c, d, e, f, g, k] = h;
|
|
32
|
+
for (let t = 0; t < 64; t++) {
|
|
33
|
+
const t1 = (k + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[t] + w[t]) | 0;
|
|
34
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
|
|
35
|
+
k = g;
|
|
36
|
+
g = f;
|
|
37
|
+
f = e;
|
|
38
|
+
e = (d + t1) | 0;
|
|
39
|
+
d = c;
|
|
40
|
+
c = b;
|
|
41
|
+
b = a;
|
|
42
|
+
a = (t1 + t2) | 0;
|
|
43
|
+
}
|
|
44
|
+
h[0] = (h[0] + a) | 0;
|
|
45
|
+
h[1] = (h[1] + b) | 0;
|
|
46
|
+
h[2] = (h[2] + c) | 0;
|
|
47
|
+
h[3] = (h[3] + d) | 0;
|
|
48
|
+
h[4] = (h[4] + e) | 0;
|
|
49
|
+
h[5] = (h[5] + f) | 0;
|
|
50
|
+
h[6] = (h[6] + g) | 0;
|
|
51
|
+
h[7] = (h[7] + k) | 0;
|
|
52
|
+
}
|
|
53
|
+
const out = [];
|
|
54
|
+
for (let i = 0; i < 8; i++)
|
|
55
|
+
out.push(h[i] >>> 24, (h[i] >>> 16) & 255, (h[i] >>> 8) & 255, h[i] & 255);
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export interface Fingerprint {
|
|
2
|
+
machineId: string;
|
|
3
|
+
diskId: string;
|
|
4
|
+
hostName: string;
|
|
5
|
+
boardId: string;
|
|
6
|
+
/** Operating system, or the host platform for installations (e.g. "youtrack"). */
|
|
7
|
+
os?: string | null;
|
|
8
|
+
}
|
|
9
|
+
/** The signed license token's payload (token-spec.md §2). Unknown future fields are kept. */
|
|
10
|
+
export interface LicensePayload {
|
|
11
|
+
licenseId: string;
|
|
12
|
+
activationId: string;
|
|
13
|
+
licenseKey: string;
|
|
14
|
+
product: string;
|
|
15
|
+
customer: string;
|
|
16
|
+
kind: 'full' | 'trial' | string;
|
|
17
|
+
expiresAt: string | null;
|
|
18
|
+
issuedAt: string;
|
|
19
|
+
refreshAfter: string;
|
|
20
|
+
offlineUntil: string;
|
|
21
|
+
fingerprint: Fingerprint;
|
|
22
|
+
suspended: boolean;
|
|
23
|
+
environmentId?: string;
|
|
24
|
+
test?: boolean;
|
|
25
|
+
entitlements?: string[];
|
|
26
|
+
metadata?: Record<string, unknown>;
|
|
27
|
+
floating?: boolean;
|
|
28
|
+
leaseExpiresAt?: string;
|
|
29
|
+
/** A keyless trial: valid until `expiresAt`, never refreshed. */
|
|
30
|
+
trial?: boolean;
|
|
31
|
+
/** Apps inside a host: the most users the host may have and what happens above it. */
|
|
32
|
+
maxHostUsers?: number;
|
|
33
|
+
hostUserPolicy?: 'block' | 'warn' | 'report' | string;
|
|
34
|
+
/** The server saw more host users than allowed and the policy is "warn": show a warning. */
|
|
35
|
+
hostUsersExceeded?: boolean;
|
|
36
|
+
[field: string]: unknown;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Verifies a token with the environment's public key (PEM) using the bundled P-256 code, synchronously.
|
|
40
|
+
* Returns the payload, or null when the token is malformed or the signature doesn't match.
|
|
41
|
+
*/
|
|
42
|
+
export declare function verifyToken(token: string, publicKeyPem: string): LicensePayload | null;
|
|
43
|
+
/** Like {@link verifyToken}, but uses WebCrypto (`crypto.subtle`) when the host has it. */
|
|
44
|
+
export declare function verifyTokenAsync(token: string, publicKeyPem: string): Promise<LicensePayload | null>;
|
|
45
|
+
/** Reads a token's payload WITHOUT checking the signature (for display only; never trust it). */
|
|
46
|
+
export declare function decodeTokenUnverified(token: string): LicensePayload | null;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { base64Decode, utf8Decode, utf8Encode } from './encoding.js';
|
|
2
|
+
import { parsePublicKey, verifyP256 } from './p256.js';
|
|
3
|
+
function split(token) {
|
|
4
|
+
if (!token || typeof token !== 'string')
|
|
5
|
+
return null;
|
|
6
|
+
const parts = token.trim().split('.');
|
|
7
|
+
if (parts.length !== 2 || !parts[0] || !parts[1])
|
|
8
|
+
return null;
|
|
9
|
+
const signature = base64Decode(parts[1]);
|
|
10
|
+
const body = base64Decode(parts[0]);
|
|
11
|
+
if (!signature || signature.length !== 64 || !body)
|
|
12
|
+
return null;
|
|
13
|
+
try {
|
|
14
|
+
const payload = JSON.parse(utf8Decode(body));
|
|
15
|
+
if (!payload || typeof payload !== 'object' || !payload.fingerprint)
|
|
16
|
+
return null;
|
|
17
|
+
// The signature covers the ASCII bytes of the base64url payload as it appears in the token.
|
|
18
|
+
return { signed: utf8Encode(parts[0]), signature, payload };
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Verifies a token with the environment's public key (PEM) using the bundled P-256 code, synchronously.
|
|
26
|
+
* Returns the payload, or null when the token is malformed or the signature doesn't match.
|
|
27
|
+
*/
|
|
28
|
+
export function verifyToken(token, publicKeyPem) {
|
|
29
|
+
const parts = split(token);
|
|
30
|
+
return parts && verifyP256(publicKeyPem, parts.signed, parts.signature) ? parts.payload : null;
|
|
31
|
+
}
|
|
32
|
+
function subtle() {
|
|
33
|
+
const c = globalThis.crypto;
|
|
34
|
+
return c && c.subtle && typeof c.subtle.verify === 'function' ? c.subtle : null;
|
|
35
|
+
}
|
|
36
|
+
/** Like {@link verifyToken}, but uses WebCrypto (`crypto.subtle`) when the host has it. */
|
|
37
|
+
export async function verifyTokenAsync(token, publicKeyPem) {
|
|
38
|
+
const s = subtle();
|
|
39
|
+
if (!s || typeof Uint8Array === 'undefined')
|
|
40
|
+
return verifyToken(token, publicKeyPem);
|
|
41
|
+
const parts = split(token);
|
|
42
|
+
const key = parsePublicKey(publicKeyPem);
|
|
43
|
+
if (!parts || !key)
|
|
44
|
+
return null;
|
|
45
|
+
try {
|
|
46
|
+
const der = base64Decode(publicKeyPem.replace(/-----(BEGIN|END) PUBLIC KEY-----/g, ''));
|
|
47
|
+
const cryptoKey = await s.importKey('spki', new Uint8Array(der).buffer, { name: 'ECDSA', namedCurve: 'P-256' }, false, ['verify']);
|
|
48
|
+
const ok = await s.verify({ name: 'ECDSA', hash: 'SHA-256' }, cryptoKey, new Uint8Array(parts.signature).buffer, new Uint8Array(parts.signed).buffer);
|
|
49
|
+
return ok ? parts.payload : null;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return verifyToken(token, publicKeyPem);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Reads a token's payload WITHOUT checking the signature (for display only; never trust it). */
|
|
56
|
+
export function decodeTokenUnverified(token) {
|
|
57
|
+
return split(token)?.payload ?? null;
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeo-engineering/licenserail-embedded",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "LicenseRail client core for apps inside a host platform (YouTrack apps, browser extensions, web apps): offline ES256 verification without WebCrypto, installation identity, activation, trials. No dependencies.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": { "access": "public" },
|
|
7
|
+
"author": "Codeo Solutions Limited",
|
|
8
|
+
"homepage": "https://licenserail.com",
|
|
9
|
+
"repository": { "type": "git", "url": "git+https://github.com/codeo-engineering/licenserail-sdks.git", "directory": "embedded" },
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "dist/cjs/index.js",
|
|
12
|
+
"module": "dist/esm/index.js",
|
|
13
|
+
"types": "dist/esm/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/esm/index.d.ts",
|
|
17
|
+
"import": "./dist/esm/index.js",
|
|
18
|
+
"require": "./dist/cjs/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": ["dist"],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node scripts/cjs-package.mjs",
|
|
25
|
+
"test": "npm run build && node --test test/"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.11.0",
|
|
29
|
+
"typescript": "^5.6.0"
|
|
30
|
+
}
|
|
31
|
+
}
|