node-tpm2 0.0.2 → 0.0.4-beta.0

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/README.md CHANGED
@@ -1,13 +1,12 @@
1
1
  # node-tpm2
2
2
 
3
- Native TPM 2.0 for Node. Zero tooling, no admin.
3
+ Native TPM 2.0 for Node. Zero tooling at install time — prebuilt napi-rs binaries, no tpm2-tss, no tpm2-tools.
4
4
 
5
- - Windows via TBS, Linux via `/dev/tpmrm0`.
6
- - Direct TBS command marshalling — no tpm2-tss, no tpm2-tools at install or runtime.
7
- - Ships as prebuilt native binaries via napi-rs platform packages.
8
-
9
- > **Status: pre-release.** `Tpm.isAvailable()` and `Tpm.info()` work on Windows and Linux.
10
- > `Tpm.open()` and attestation methods are not implemented yet.
5
+ | Platform | Backend | Attestation key |
6
+ |----------|---------|-----------------|
7
+ | Linux | `/dev/tpmrm0` | ECDSA P-256 wrapped blob |
8
+ | Windows | TBS + NCrypt PCP | RSA-2048 persisted PCP key |
9
+ | macOS | | Not supported (returns unavailable) |
11
10
 
12
11
  ## Install
13
12
 
@@ -15,34 +14,61 @@ Native TPM 2.0 for Node. Zero tooling, no admin.
15
14
  npm install node-tpm2
16
15
  ```
17
16
 
18
- npm resolves exactly one prebuilt native binary from `optionalDependencies` no build step,
19
- no tpm2-tools, no Rust. Requires platform packages published for your OS/arch.
17
+ Requires Node 20+. Resolves a prebuilt `.node` binary for your OS/arch via optional platform packages.
18
+
19
+ ## Quick start
20
+
21
+ ```javascript
22
+ import { Tpm } from 'node-tpm2';
23
+
24
+ if (!(await Tpm.isAvailable())) throw new Error('no TPM');
25
+
26
+ const { akBlob } = await Tpm.provisionAk();
27
+ const quote = await Tpm.quote({
28
+ akBlob,
29
+ pcrSelection: [0, 1, 7],
30
+ qualifyingData: Buffer.from('challenge-bytes'),
31
+ });
32
+ ```
33
+
34
+ See [docs/getting-started.md](./docs/getting-started.md) for API details and [docs/windows-pcp.md](./docs/windows-pcp.md) for Windows machine-scoped keys (privileged install → unprivileged quote).
35
+
36
+ ## Validate install (clean machine)
20
37
 
21
- ## Development
38
+ After `npm install`, run the npm smoke test — **not** the Rust probe:
39
+
40
+ ```bash
41
+ node node_modules/node-tpm2/examples/smoke-test.mjs runtime
42
+ ```
43
+
44
+ Windows fleet path (Admin/SYSTEM provision, then standard user quote):
45
+
46
+ ```bash
47
+ node node_modules/node-tpm2/examples/smoke-test.mjs provision-machine --key-name my-app-device-ak --out ak.blob.json
48
+ node node_modules/node-tpm2/examples/smoke-test.mjs quote --in ak.blob.json
49
+ ```
50
+
51
+ ## Development (this repo)
22
52
 
23
53
  ```bash
24
54
  git clone https://github.com/stacks0x/tpm2.git
25
55
  cd tpm2
26
56
  npm install
27
57
  npm run build
28
- node --input-type=module -e "
29
- import { Tpm } from './index.js';
30
- console.log('available', await Tpm.isAvailable());
31
- console.log('info', await Tpm.info());
32
- "
58
+ node examples/smoke-test.mjs runtime
33
59
  ```
34
60
 
35
- On Linux, your user needs read/write on `/dev/tpmrm0` (typically the `tss` group).
36
-
37
- ## Windows probe (direct TBS validation)
38
-
39
- Non-elevated PowerShell on Windows 11:
61
+ Rust probe (developers, not the npm artifact):
40
62
 
41
63
  ```powershell
42
- cargo run --bin tbs-probe -- all
64
+ cargo build --no-default-features --features probe-bin --bin tbs-probe
65
+ .\target\debug\tbs-probe.exe all
66
+ .\target\debug\tbs-probe.exe help
43
67
  ```
44
68
 
45
- See [spike/README.md](./spike/README.md) for probe details and RC discipline.
69
+ Linux: user needs access to `/dev/tpmrm0` (typically the `tss` group).
70
+
71
+ Maintainer-only planning docs (release checklists, specs) go in `docs/dev/` — that folder is gitignored and never published.
46
72
 
47
73
  ## License
48
74
 
package/api.js CHANGED
@@ -9,6 +9,50 @@ try {
9
9
  nativeLoadError = err;
10
10
  }
11
11
 
12
+ const TPM_UNAVAILABLE = {
13
+ code: 'TPM_UNAVAILABLE',
14
+ suggestion: 'Run npm run build, or install a published platform package.',
15
+ };
16
+
17
+ function parseNativeError(err) {
18
+ const msg = err?.message ?? String(err);
19
+ if (msg.startsWith('__tpm2__')) {
20
+ const rest = msg.slice('__tpm2__'.length);
21
+ const parts = rest.split('|');
22
+ const [code, message, suggestion, tpmRcStr] = parts;
23
+ const tpmRc = tpmRcStr ? Number.parseInt(tpmRcStr, 10) : undefined;
24
+ return new TpmError(
25
+ code,
26
+ message,
27
+ suggestion || undefined,
28
+ Number.isFinite(tpmRc) ? tpmRc : undefined,
29
+ );
30
+ }
31
+ return err;
32
+ }
33
+
34
+ function wrapNative(fn) {
35
+ return async (...args) => {
36
+ try {
37
+ return await fn(...args);
38
+ } catch (err) {
39
+ throw parseNativeError(err);
40
+ }
41
+ };
42
+ }
43
+
44
+ function requireNative(method) {
45
+ if (!native?.[method]) {
46
+ throw new TpmError(
47
+ TPM_UNAVAILABLE.code,
48
+ nativeLoadError
49
+ ? `Native backend not loaded: ${nativeLoadError.message}`
50
+ : 'Native backend not built for this platform.',
51
+ TPM_UNAVAILABLE.suggestion,
52
+ );
53
+ }
54
+ }
55
+
12
56
  export class TpmError extends Error {
13
57
  constructor(code, message, suggestion, tpmRc) {
14
58
  super(message);
@@ -19,6 +63,98 @@ export class TpmError extends Error {
19
63
  }
20
64
  }
21
65
 
66
+ function createAkHandle(akPublicDer, akBlob) {
67
+ return {
68
+ /** Wrapped TPM2B_PUBLIC + TPM2B_PRIVATE for persistence (no persistent TPM handle). */
69
+ export() {
70
+ return {
71
+ public: Buffer.from(akBlob.public),
72
+ private: Buffer.from(akBlob.private),
73
+ };
74
+ },
75
+
76
+ /** SPKI DER for the AK public area (from provisioning). */
77
+ get publicKeyDer() {
78
+ return Buffer.from(akPublicDer);
79
+ },
80
+
81
+ quote: wrapNative(async (opts) => {
82
+ requireNative('quote');
83
+ return native.quote({
84
+ akBlob,
85
+ pcrSelection: opts.pcrSelection,
86
+ qualifyingData: opts.qualifyingData,
87
+ bank: opts.bank,
88
+ });
89
+ }),
90
+
91
+ activateCredential: wrapNative(async (opts) => {
92
+ requireNative('activateCredential');
93
+ return native.activateCredential({
94
+ akBlob,
95
+ credentialBlob: opts.credentialBlob,
96
+ secret: opts.secret,
97
+ });
98
+ }),
99
+ };
100
+ }
101
+
102
+ function createTpmHandle() {
103
+ return {
104
+ async info() {
105
+ return Tpm.getFixedProperties();
106
+ },
107
+
108
+ pcr: {
109
+ /** Read SHA-256 PCR digests for the given indices. */
110
+ read: wrapNative(async (selection, bank = 'sha256') => {
111
+ requireNative('pcrRead');
112
+ return native.pcrRead(selection, bank);
113
+ }),
114
+ },
115
+
116
+ attest: {
117
+ /** EK certificate from NV index, or null if not provisioned. */
118
+ ekCertificate: wrapNative(async () => {
119
+ requireNative('readEkCertificate');
120
+ return native.readEkCertificate();
121
+ }),
122
+
123
+ /** Provision a transient AK; returns a handle with export/quote/activateCredential. */
124
+ provisionAk: wrapNative(async (opts) => {
125
+ requireNative('provisionAk');
126
+ const result = await native.provisionAk({
127
+ keyName: opts?.keyName,
128
+ scope: opts?.scope,
129
+ overwrite: opts?.overwrite,
130
+ });
131
+ return createAkHandle(result.akPublicDer, result.akBlob);
132
+ }),
133
+
134
+ /** Produce a quote from a wrapped AK blob (transient load, no persistent handle). */
135
+ quote: wrapNative(async (opts) => {
136
+ requireNative('quote');
137
+ return native.quote({
138
+ akBlob: opts.akBlob,
139
+ pcrSelection: opts.pcrSelection,
140
+ qualifyingData: opts.qualifyingData,
141
+ bank: opts.bank,
142
+ });
143
+ }),
144
+ },
145
+
146
+ /** Read a TPM object's public area as SPKI DER + name. */
147
+ readPublic: wrapNative(async (handle) => {
148
+ requireNative('readPublic');
149
+ return native.readPublic(handle);
150
+ }),
151
+
152
+ async [Symbol.asyncDispose]() {
153
+ // Transient handles are flushed inside each native operation.
154
+ },
155
+ };
156
+ }
157
+
22
158
  export const Tpm = {
23
159
  /** Probe for an accessible TPM. False on darwin / no TPM / no access. */
24
160
  async isAvailable() {
@@ -32,38 +168,78 @@ export const Tpm = {
32
168
  }
33
169
  },
34
170
 
35
- /** Open a TPM handle. Not implemented in v0.0.x. */
171
+ /** Open a TPM handle. Auto-detects transport (TBS / /dev/tpmrm0). */
36
172
  async open() {
37
- if (!native?.isAvailable) {
173
+ requireNative('isAvailable');
174
+ const available = await this.isAvailable();
175
+ if (!available) {
38
176
  throw new TpmError(
39
177
  'TPM_UNAVAILABLE',
40
- nativeLoadError
41
- ? `Native backend not loaded: ${nativeLoadError.message}`
42
- : 'Native backend not built for this platform.',
43
- 'Run npm run build, or install a published platform package.',
178
+ 'No accessible TPM on this platform.',
179
+ 'On Linux, ensure /dev/tpmrm0 is readable; on Windows, ensure the TPM is present.',
44
180
  );
45
181
  }
46
- throw new TpmError(
47
- 'NOT_IMPLEMENTED',
48
- 'Tpm.open() is not implemented yet; v0.0.x exposes isAvailable() and info() only.',
49
- 'See https://github.com/stacks0x/tpm2 for release progress.',
50
- );
182
+ return createTpmHandle();
51
183
  },
52
184
 
53
185
  /** Manufacturer, firmware, and virtual-TPM hint from GetCapability. */
54
- async getFixedProperties() {
55
- if (!native?.getFixedProperties) {
56
- throw new TpmError(
57
- 'TPM_UNAVAILABLE',
58
- 'Native backend not loaded.',
59
- 'Run npm run build, or install a published platform package.',
60
- );
61
- }
62
- return native.getFixedProperties();
63
- },
186
+ getFixedProperties: wrapNative(async () => {
187
+ requireNative('getFixedProperties');
188
+ const props = await native.getFixedProperties();
189
+ return {
190
+ manufacturer: props.manufacturer,
191
+ firmwareVersion: props.firmwareVersion,
192
+ isVirtual: props.isVirtual,
193
+ spec: props.spec,
194
+ };
195
+ }),
64
196
 
65
197
  /** Alias for getFixedProperties. */
66
198
  async info() {
67
199
  return this.getFixedProperties();
68
200
  },
201
+
202
+ /** Flat native binding: PCR read. Prefer `tpm.pcr.read` on an open handle. */
203
+ pcrRead: wrapNative(async (selection, bank) => {
204
+ requireNative('pcrRead');
205
+ return native.pcrRead(selection, bank);
206
+ }),
207
+
208
+ /** Flat native binding: ReadPublic. */
209
+ readPublic: wrapNative(async (handle) => {
210
+ requireNative('readPublic');
211
+ return native.readPublic(handle);
212
+ }),
213
+
214
+ /** Flat native binding: EK certificate NV read. */
215
+ readEkCertificate: wrapNative(async () => {
216
+ requireNative('readEkCertificate');
217
+ return native.readEkCertificate();
218
+ }),
219
+
220
+ /** Flat native binding: quote with wrapped AK blob. */
221
+ quote: wrapNative(async (opts) => {
222
+ requireNative('quote');
223
+ return native.quote(opts);
224
+ }),
225
+
226
+ /** Flat native binding: provision AK (returns akPublicDer + akBlob). */
227
+ provisionAk: wrapNative(async (opts) => {
228
+ requireNative('provisionAk');
229
+ const result = await native.provisionAk({
230
+ keyName: opts?.keyName,
231
+ scope: opts?.scope,
232
+ overwrite: opts?.overwrite,
233
+ });
234
+ return {
235
+ akPublicDer: result.akPublicDer,
236
+ akBlob: result.akBlob,
237
+ };
238
+ }),
239
+
240
+ /** Flat native binding: activate credential with wrapped AK blob. */
241
+ activateCredential: wrapNative(async (opts) => {
242
+ requireNative('activateCredential');
243
+ return native.activateCredential(opts);
244
+ }),
69
245
  };
@@ -0,0 +1,77 @@
1
+ # Getting started
2
+
3
+ node-tpm2 talks to the TPM 2.0 through OS-native paths — no tpm2-tools, no tpm2-tss, and no Rust toolchain at install time.
4
+
5
+ | Platform | Transport | Attestation key (AK) |
6
+ |----------|-----------|----------------------|
7
+ | Linux | `/dev/tpmrm0` | ECDSA P-256 wrapped TPM2B blob |
8
+ | Windows | TBS + NCrypt PCP | RSA-2048 persisted PCP key (`PCP1` / `PCP2` blob) |
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install node-tpm2
14
+ ```
15
+
16
+ Requires Node 20+. npm pulls a prebuilt native binary for your OS/arch from optional platform packages (`node-tpm2-windows-x64-msvc`, `node-tpm2-linux-x64-gnu`, etc.).
17
+
18
+ ## Quick check
19
+
20
+ ```javascript
21
+ import { Tpm } from 'node-tpm2';
22
+
23
+ console.log('available', await Tpm.isAvailable());
24
+ console.log('info', await Tpm.info());
25
+ ```
26
+
27
+ ## Provision and quote (development)
28
+
29
+ Works **without admin** on both Linux and Windows (user-scoped AK on Windows):
30
+
31
+ ```javascript
32
+ import { Tpm } from 'node-tpm2';
33
+
34
+ const { akPublicDer, akBlob } = await Tpm.provisionAk();
35
+ console.log('AK SPKI', akPublicDer.length, 'bytes');
36
+
37
+ const quote = await Tpm.quote({
38
+ akBlob,
39
+ pcrSelection: [0, 1, 7],
40
+ qualifyingData: Buffer.from('session-nonce-or-challenge'),
41
+ bank: 'sha256',
42
+ });
43
+ console.log('quote', quote.message.length, quote.signature.length);
44
+ ```
45
+
46
+ ## Windows: machine-scoped AK (cross-user)
47
+
48
+ For apps where a **privileged installer** creates the key and a **standard user** quotes at runtime, use a machine-scoped key with a stable name. See [windows-pcp.md](./windows-pcp.md).
49
+
50
+ ```javascript
51
+ // Run elevated or as SYSTEM (enrollment / install time only)
52
+ const { akBlob } = await Tpm.provisionAk({
53
+ keyName: 'my-app-device-ak',
54
+ scope: 'machine',
55
+ overwrite: true,
56
+ });
57
+ // Persist akBlob (and upload creation attestation to your verifier)
58
+
59
+ // Runtime — standard user, no admin
60
+ const quote = await Tpm.quote({
61
+ akBlob,
62
+ pcrSelection: [0, 1, 7],
63
+ qualifyingData: Buffer.from('challenge'),
64
+ });
65
+ ```
66
+
67
+ ## Linux permissions
68
+
69
+ Your user needs read/write on `/dev/tpmrm0` (commonly the `tss` group):
70
+
71
+ ```bash
72
+ sudo usermod -aG tss "$USER"
73
+ ```
74
+
75
+ ## Errors
76
+
77
+ Native failures surface as `TpmError` with `code`, `message`, optional `suggestion`, and `tpmRc` when the TPM returned an RC.
@@ -0,0 +1,67 @@
1
+ # Windows: Platform Crypto Provider (PCP)
2
+
3
+ On Windows, node-tpm2 uses the **Microsoft Platform Crypto Provider** for attestation keys — not raw TBS wrapped blobs. Linux continues to use ECDSA TBS-wrapped blobs; AK formats differ by design and verifiers should accept both.
4
+
5
+ ## Operations and privilege
6
+
7
+ | Operation | Standard user | Elevated admin | SYSTEM |
8
+ |-----------|---------------|----------------|--------|
9
+ | `Tpm.isAvailable()`, PCR read, `readPublic` | Yes | Yes | Yes |
10
+ | `provisionAk()` user scope (`PCP1`) | Yes | Yes | Yes |
11
+ | `quote()` | Yes | Yes | Yes |
12
+ | `provisionAk({ scope: 'machine' })` (`PCP2`) | No | Yes | Yes (production enroll) |
13
+ | `activateCredential()` (PCP) | No | Yes | Yes |
14
+
15
+ **Runtime apps** typically only need `quote()` with a blob/locator from enrollment. Activation is an enrollment-time proof-of-possession step.
16
+
17
+ ## AK blob formats
18
+
19
+ | Magic | Scope | Meaning |
20
+ |-------|-------|---------|
21
+ | `PCP1` | User | Dev / same-user flows; random key name if omitted |
22
+ | `PCP2` | Machine | Fleet / cross-user; stable `keyName` required |
23
+
24
+ Blob layout: magic + key name + PCP creation attestation fields (`public` holds metadata; `private` is empty).
25
+
26
+ ## Machine key provisioning
27
+
28
+ ```javascript
29
+ await Tpm.provisionAk({
30
+ keyName: 'my-app-device-ak', // your product prefix, not library-specific
31
+ scope: 'machine',
32
+ overwrite: true, // replaces existing key of same name
33
+ });
34
+ ```
35
+
36
+ Requirements:
37
+
38
+ - PCP must advertise **Security Descr Support** (probe: `tbs-probe pcp-capabilities`)
39
+ - Machine keys get a DACL granting Built-in Users read/sign so standard users can open and quote
40
+ - PCP ignores `NCRYPT_OVERWRITE_KEY_FLAG`; the library deletes an existing key before recreate when `overwrite: true`
41
+
42
+ ## Quote scheme
43
+
44
+ PCP identity keys are RSA-2048. Quotes use `TPM_ALG_NULL` (key default RSASSA), matching go-attestation. Linux uses explicit ECDSA+SHA256.
45
+
46
+ ## Validating with `tbs-probe` (Rust, developers only)
47
+
48
+ The npm module and `tbs-probe` share the same Rust core but are **different artifacts**. Validate Rust with the probe; validate the **npm package** with [examples/smoke-test.mjs](../examples/smoke-test.mjs) on a clean machine.
49
+
50
+ ```powershell
51
+ # Runtime path (standard PowerShell)
52
+ cargo run --no-default-features --features probe-bin --bin tbs-probe -- all
53
+
54
+ # SYSTEM provision + standard quote — see `tbs-probe help`
55
+ ```
56
+
57
+ ## Simulating SYSTEM locally
58
+
59
+ Intune/SCCM/GPO run enrollment as **SYSTEM**, not interactive admin. On a dev VM, use a one-shot scheduled task (no extra tools):
60
+
61
+ ```powershell
62
+ # Admin PowerShell — see tbs-probe help for full script
63
+ schtasks /Create /TN "my-app-system-provision" /RU SYSTEM ...
64
+ schtasks /Run /TN "my-app-system-provision"
65
+ ```
66
+
67
+ Then quote from standard PowerShell with the saved AK blob.
@@ -0,0 +1,178 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Validate the published npm native module (not tbs-probe).
4
+ *
5
+ * Usage:
6
+ * node smoke-test.mjs runtime
7
+ * node smoke-test.mjs provision-machine [--key-name NAME] [--out PATH]
8
+ * node smoke-test.mjs quote [--in PATH]
9
+ *
10
+ * Install on a clean machine:
11
+ * npm install node-tpm2@<version>
12
+ * node node_modules/node-tpm2/examples/smoke-test.mjs runtime
13
+ */
14
+
15
+ import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
16
+ import { dirname, resolve } from 'node:path';
17
+ import { fileURLToPath } from 'node:url';
18
+ import { Tpm } from '../index.js';
19
+
20
+ const __dirname = dirname(fileURLToPath(import.meta.url));
21
+
22
+ function usage() {
23
+ console.error(`Usage:
24
+ smoke-test.mjs runtime
25
+ smoke-test.mjs provision-machine [--key-name NAME] [--out PATH]
26
+ smoke-test.mjs quote [--in PATH]
27
+
28
+ Windows: provision-machine needs elevation or SYSTEM.
29
+ Runtime quote works unprivileged (user or machine AK blob).`);
30
+ process.exit(2);
31
+ }
32
+
33
+ function flagValue(args, name) {
34
+ for (let i = 0; i < args.length; i++) {
35
+ if (args[i] === name && args[i + 1]) return args[++i];
36
+ const prefix = `${name}=`;
37
+ if (args[i]?.startsWith(prefix)) return args[i].slice(prefix.length);
38
+ }
39
+ return undefined;
40
+ }
41
+
42
+ function saveBlob(path, akBlob) {
43
+ mkdirSync(dirname(resolve(path)), { recursive: true });
44
+ writeFileSync(
45
+ path,
46
+ JSON.stringify(
47
+ {
48
+ public: akBlob.public.toString('base64'),
49
+ private: akBlob.private.toString('base64'),
50
+ },
51
+ null,
52
+ 2,
53
+ ),
54
+ );
55
+ }
56
+
57
+ function loadBlob(path) {
58
+ const raw = JSON.parse(readFileSync(path, 'utf8'));
59
+ return {
60
+ public: Buffer.from(raw.public, 'base64'),
61
+ private: Buffer.from(raw.private, 'base64'),
62
+ };
63
+ }
64
+
65
+ function blobScope(akBlob) {
66
+ const head = akBlob.public.subarray(0, 4).toString('ascii');
67
+ if (head === 'PCP2') return 'machine';
68
+ if (head === 'PCP1') return 'user';
69
+ return 'linux-tpm2b';
70
+ }
71
+
72
+ async function assertAvailable() {
73
+ const ok = await Tpm.isAvailable();
74
+ if (!ok) {
75
+ console.error('FAIL: Tpm.isAvailable() returned false');
76
+ process.exit(1);
77
+ }
78
+ console.log('PASS Tpm.isAvailable()');
79
+ }
80
+
81
+ async function runRuntime() {
82
+ await assertAvailable();
83
+ const info = await Tpm.info();
84
+ console.log('PASS Tpm.info()', info);
85
+
86
+ const { akPublicDer, akBlob } = await Tpm.provisionAk();
87
+ console.log(`PASS Tpm.provisionAk() scope=${blobScope(akBlob)} SPKI=${akPublicDer.length}B`);
88
+
89
+ const qualifying = Buffer.from('node-tpm2-smoke-test-qualifying-data');
90
+ const quote = await Tpm.quote({
91
+ akBlob,
92
+ pcrSelection: [0, 1, 7],
93
+ qualifyingData: qualifying,
94
+ bank: 'sha256',
95
+ });
96
+ if (!quote.message.length || !quote.signature.length) {
97
+ console.error('FAIL: empty quote');
98
+ process.exit(1);
99
+ }
100
+ console.log(
101
+ `PASS Tpm.quote() message=${quote.message.length}B signature=${quote.signature.length}B`,
102
+ );
103
+ console.log('\nsmoke-test: runtime OK');
104
+ }
105
+
106
+ async function runProvisionMachine(args) {
107
+ await assertAvailable();
108
+ const keyName = flagValue(args, '--key-name') ?? 'my-app-device-ak';
109
+ const out =
110
+ flagValue(args, '--out') ??
111
+ resolve(process.env.PROGRAMDATA ?? 'C:\\ProgramData', 'node-tpm2-spike', 'ak.blob.json');
112
+
113
+ if (process.platform !== 'win32') {
114
+ console.error('FAIL: provision-machine is Windows PCP only');
115
+ process.exit(1);
116
+ }
117
+
118
+ const { akPublicDer, akBlob } = await Tpm.provisionAk({
119
+ keyName,
120
+ scope: 'machine',
121
+ overwrite: true,
122
+ });
123
+ if (blobScope(akBlob) !== 'machine') {
124
+ console.error(`FAIL: expected PCP2 machine blob, got scope=${blobScope(akBlob)}`);
125
+ process.exit(1);
126
+ }
127
+ saveBlob(out, akBlob);
128
+ console.log(`PASS Tpm.provisionAk(machine) keyName=${keyName} SPKI=${akPublicDer.length}B`);
129
+ console.log(` wrote ${out}`);
130
+ console.log('\nNEXT (standard user):');
131
+ console.log(` node examples/smoke-test.mjs quote --in ${out}`);
132
+ }
133
+
134
+ async function runQuote(args) {
135
+ await assertAvailable();
136
+ const input =
137
+ flagValue(args, '--in') ??
138
+ resolve(process.env.PROGRAMDATA ?? 'C:\\ProgramData', 'node-tpm2-spike', 'ak.blob.json');
139
+
140
+ const akBlob = loadBlob(input);
141
+ console.log(` loaded blob scope=${blobScope(akBlob)} from ${input}`);
142
+
143
+ const qualifying = Buffer.from('node-tpm2-smoke-test-qualifying-data');
144
+ const quote = await Tpm.quote({
145
+ akBlob,
146
+ pcrSelection: [0, 1, 7],
147
+ qualifyingData: qualifying,
148
+ bank: 'sha256',
149
+ });
150
+ console.log(
151
+ `PASS Tpm.quote() message=${quote.message.length}B signature=${quote.signature.length}B`,
152
+ );
153
+ console.log('\nsmoke-test: quote OK');
154
+ }
155
+
156
+ async function main() {
157
+ const [cmd, ...rest] = process.argv.slice(2);
158
+ switch (cmd) {
159
+ case 'runtime':
160
+ await runRuntime();
161
+ break;
162
+ case 'provision-machine':
163
+ await runProvisionMachine(rest);
164
+ break;
165
+ case 'quote':
166
+ await runQuote(rest);
167
+ break;
168
+ default:
169
+ usage();
170
+ }
171
+ }
172
+
173
+ main().catch((err) => {
174
+ console.error('FAIL:', err.message ?? err);
175
+ if (err.suggestion) console.error(' suggestion:', err.suggestion);
176
+ if (err.tpmRc != null) console.error(' tpmRc:', err.tpmRc);
177
+ process.exit(1);
178
+ });
package/index.d.ts CHANGED
@@ -5,19 +5,113 @@ export declare class TpmError extends Error {
5
5
  constructor(code: string, message: string, suggestion?: string, tpmRc?: number);
6
6
  }
7
7
 
8
+ export declare type AkBlob = {
9
+ public: Buffer;
10
+ private: Buffer;
11
+ };
12
+
13
+ export declare type QuoteOptions = {
14
+ akBlob: AkBlob;
15
+ pcrSelection: number[];
16
+ qualifyingData: Buffer;
17
+ bank?: 'sha256';
18
+ };
19
+
20
+ export declare type QuoteResult = {
21
+ message: Buffer;
22
+ signature: Buffer;
23
+ };
24
+
25
+ export declare type ReadPublicResult = {
26
+ publicKeyDer: Buffer;
27
+ name: Buffer;
28
+ };
29
+
30
+ export declare type ProvisionAkOptions = {
31
+ /** Persisted PCP key name on Windows. Omitted = random dev name. */
32
+ keyName?: string;
33
+ /** Windows only: `user` (default) or `machine` (fleet enrollment). */
34
+ scope?: 'user' | 'machine';
35
+ /** Windows only: replace existing persisted key of the same name. */
36
+ overwrite?: boolean;
37
+ /** @deprecated Linux-only hint; Windows PCP always uses RSA identity AK. */
38
+ algorithm?: 'ecc' | 'rsa';
39
+ };
40
+
41
+ export declare type ProvisionAkResult = {
42
+ akPublicDer: Buffer;
43
+ akBlob: AkBlob;
44
+ };
45
+
46
+ export declare type ActivateCredentialOptions = {
47
+ credentialBlob: Buffer;
48
+ secret: Buffer;
49
+ };
50
+
51
+ export declare type ActivateCredentialFlatOptions = ActivateCredentialOptions & {
52
+ akBlob: AkBlob;
53
+ };
54
+
55
+ export declare interface AkHandle {
56
+ export(): AkBlob;
57
+ readonly publicKeyDer: Buffer;
58
+ quote(opts: Omit<QuoteOptions, 'akBlob'>): Promise<QuoteResult>;
59
+ activateCredential(opts: ActivateCredentialOptions): Promise<Buffer>;
60
+ }
61
+
62
+ export declare type TpmInfo = {
63
+ manufacturer: string;
64
+ firmwareVersion: string;
65
+ isVirtual: boolean;
66
+ spec: string;
67
+ };
68
+
69
+ export declare interface TpmHandle {
70
+ info(): Promise<TpmInfo>;
71
+ pcr: {
72
+ read(selection: number[], bank?: 'sha256'): Promise<Record<number, string>>;
73
+ };
74
+ attest: {
75
+ ekCertificate(): Promise<Buffer | null>;
76
+ provisionAk(opts?: ProvisionAkOptions): Promise<AkHandle>;
77
+ quote(opts: QuoteOptions): Promise<QuoteResult>;
78
+ };
79
+ readPublic(handle: string): Promise<ReadPublicResult>;
80
+ [Symbol.asyncDispose](): Promise<void>;
81
+ }
82
+
8
83
  export declare const Tpm: {
9
84
  isAvailable(): Promise<boolean>;
10
- open(): Promise<never>;
11
- getFixedProperties(): Promise<{
12
- manufacturer: string;
13
- firmwareVersion: string;
14
- isVirtual: boolean;
15
- spec: string;
16
- }>;
17
- info(): Promise<{
18
- manufacturer: string;
19
- firmwareVersion: string;
20
- isVirtual: boolean;
21
- spec: string;
22
- }>;
85
+ open(): Promise<TpmHandle>;
86
+ getFixedProperties(): Promise<TpmInfo>;
87
+ info(): Promise<TpmInfo>;
88
+ pcrRead(selection: number[], bank?: 'sha256'): Promise<Record<number, string>>;
89
+ readPublic(handle: string): Promise<ReadPublicResult>;
90
+ readEkCertificate(): Promise<Buffer | null>;
91
+ quote(opts: QuoteOptions): Promise<QuoteResult>;
92
+ provisionAk(opts?: ProvisionAkOptions): Promise<ProvisionAkResult>;
93
+ activateCredential(opts: ActivateCredentialFlatOptions): Promise<Buffer>;
23
94
  };
95
+
96
+ export declare function pcrRead(
97
+ selection: number[],
98
+ bank?: 'sha256',
99
+ ): Promise<Record<number, string>>;
100
+
101
+ export declare function readPublic(handle: string): Promise<ReadPublicResult>;
102
+
103
+ export declare function readEkCertificate(): Promise<Buffer | null>;
104
+
105
+ export declare function quote(opts: QuoteOptions): Promise<QuoteResult>;
106
+
107
+ export declare function provisionAk(
108
+ opts?: ProvisionAkOptions,
109
+ ): Promise<ProvisionAkResult>;
110
+
111
+ export declare function activateCredential(
112
+ opts: ActivateCredentialFlatOptions,
113
+ ): Promise<Buffer>;
114
+
115
+ export declare function getFixedProperties(): Promise<TpmInfo>;
116
+
117
+ export declare function isAvailable(): Promise<boolean>;
package/native.cjs CHANGED
@@ -77,8 +77,8 @@ function requireNative() {
77
77
  try {
78
78
  const binding = require('node-tpm2-android-arm64')
79
79
  const bindingPackageVersion = require('node-tpm2-android-arm64/package.json').version
80
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
80
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
82
82
  }
83
83
  return binding
84
84
  } catch (e) {
@@ -93,8 +93,8 @@ function requireNative() {
93
93
  try {
94
94
  const binding = require('node-tpm2-android-arm-eabi')
95
95
  const bindingPackageVersion = require('node-tpm2-android-arm-eabi/package.json').version
96
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
96
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
98
98
  }
99
99
  return binding
100
100
  } catch (e) {
@@ -114,8 +114,8 @@ function requireNative() {
114
114
  try {
115
115
  const binding = require('node-tpm2-win32-x64-gnu')
116
116
  const bindingPackageVersion = require('node-tpm2-win32-x64-gnu/package.json').version
117
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
117
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
119
119
  }
120
120
  return binding
121
121
  } catch (e) {
@@ -128,10 +128,10 @@ function requireNative() {
128
128
  loadErrors.push(e)
129
129
  }
130
130
  try {
131
- const binding = require('node-tpm2-win32-x64-msvc')
132
- const bindingPackageVersion = require('node-tpm2-win32-x64-msvc/package.json').version
133
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
131
+ const binding = require('node-tpm2-windows-x64-msvc')
132
+ const bindingPackageVersion = require('node-tpm2-windows-x64-msvc/package.json').version
133
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
135
135
  }
136
136
  return binding
137
137
  } catch (e) {
@@ -147,8 +147,8 @@ function requireNative() {
147
147
  try {
148
148
  const binding = require('node-tpm2-win32-ia32-msvc')
149
149
  const bindingPackageVersion = require('node-tpm2-win32-ia32-msvc/package.json').version
150
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
152
  }
153
153
  return binding
154
154
  } catch (e) {
@@ -161,10 +161,10 @@ function requireNative() {
161
161
  loadErrors.push(e)
162
162
  }
163
163
  try {
164
- const binding = require('node-tpm2-win32-arm64-msvc')
165
- const bindingPackageVersion = require('node-tpm2-win32-arm64-msvc/package.json').version
166
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
164
+ const binding = require('node-tpm2-windows-arm64-msvc')
165
+ const bindingPackageVersion = require('node-tpm2-windows-arm64-msvc/package.json').version
166
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
168
168
  }
169
169
  return binding
170
170
  } catch (e) {
@@ -182,8 +182,8 @@ function requireNative() {
182
182
  try {
183
183
  const binding = require('node-tpm2-darwin-universal')
184
184
  const bindingPackageVersion = require('node-tpm2-darwin-universal/package.json').version
185
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
187
187
  }
188
188
  return binding
189
189
  } catch (e) {
@@ -198,8 +198,8 @@ function requireNative() {
198
198
  try {
199
199
  const binding = require('node-tpm2-darwin-x64')
200
200
  const bindingPackageVersion = require('node-tpm2-darwin-x64/package.json').version
201
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
201
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
203
203
  }
204
204
  return binding
205
205
  } catch (e) {
@@ -214,8 +214,8 @@ function requireNative() {
214
214
  try {
215
215
  const binding = require('node-tpm2-darwin-arm64')
216
216
  const bindingPackageVersion = require('node-tpm2-darwin-arm64/package.json').version
217
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
217
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
219
219
  }
220
220
  return binding
221
221
  } catch (e) {
@@ -234,8 +234,8 @@ function requireNative() {
234
234
  try {
235
235
  const binding = require('node-tpm2-freebsd-x64')
236
236
  const bindingPackageVersion = require('node-tpm2-freebsd-x64/package.json').version
237
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
237
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
239
239
  }
240
240
  return binding
241
241
  } catch (e) {
@@ -250,8 +250,8 @@ function requireNative() {
250
250
  try {
251
251
  const binding = require('node-tpm2-freebsd-arm64')
252
252
  const bindingPackageVersion = require('node-tpm2-freebsd-arm64/package.json').version
253
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
253
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
255
255
  }
256
256
  return binding
257
257
  } catch (e) {
@@ -271,8 +271,8 @@ function requireNative() {
271
271
  try {
272
272
  const binding = require('node-tpm2-linux-x64-musl')
273
273
  const bindingPackageVersion = require('node-tpm2-linux-x64-musl/package.json').version
274
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
274
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
276
276
  }
277
277
  return binding
278
278
  } catch (e) {
@@ -287,8 +287,8 @@ function requireNative() {
287
287
  try {
288
288
  const binding = require('node-tpm2-linux-x64-gnu')
289
289
  const bindingPackageVersion = require('node-tpm2-linux-x64-gnu/package.json').version
290
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
290
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
292
292
  }
293
293
  return binding
294
294
  } catch (e) {
@@ -305,8 +305,8 @@ function requireNative() {
305
305
  try {
306
306
  const binding = require('node-tpm2-linux-arm64-musl')
307
307
  const bindingPackageVersion = require('node-tpm2-linux-arm64-musl/package.json').version
308
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
308
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
310
310
  }
311
311
  return binding
312
312
  } catch (e) {
@@ -321,8 +321,8 @@ function requireNative() {
321
321
  try {
322
322
  const binding = require('node-tpm2-linux-arm64-gnu')
323
323
  const bindingPackageVersion = require('node-tpm2-linux-arm64-gnu/package.json').version
324
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
324
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
326
326
  }
327
327
  return binding
328
328
  } catch (e) {
@@ -339,8 +339,8 @@ function requireNative() {
339
339
  try {
340
340
  const binding = require('node-tpm2-linux-arm-musleabihf')
341
341
  const bindingPackageVersion = require('node-tpm2-linux-arm-musleabihf/package.json').version
342
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
342
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
344
344
  }
345
345
  return binding
346
346
  } catch (e) {
@@ -355,8 +355,8 @@ function requireNative() {
355
355
  try {
356
356
  const binding = require('node-tpm2-linux-arm-gnueabihf')
357
357
  const bindingPackageVersion = require('node-tpm2-linux-arm-gnueabihf/package.json').version
358
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
358
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
360
360
  }
361
361
  return binding
362
362
  } catch (e) {
@@ -373,8 +373,8 @@ function requireNative() {
373
373
  try {
374
374
  const binding = require('node-tpm2-linux-loong64-musl')
375
375
  const bindingPackageVersion = require('node-tpm2-linux-loong64-musl/package.json').version
376
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
376
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
378
378
  }
379
379
  return binding
380
380
  } catch (e) {
@@ -389,8 +389,8 @@ function requireNative() {
389
389
  try {
390
390
  const binding = require('node-tpm2-linux-loong64-gnu')
391
391
  const bindingPackageVersion = require('node-tpm2-linux-loong64-gnu/package.json').version
392
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
392
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
394
394
  }
395
395
  return binding
396
396
  } catch (e) {
@@ -407,8 +407,8 @@ function requireNative() {
407
407
  try {
408
408
  const binding = require('node-tpm2-linux-riscv64-musl')
409
409
  const bindingPackageVersion = require('node-tpm2-linux-riscv64-musl/package.json').version
410
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
410
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
412
412
  }
413
413
  return binding
414
414
  } catch (e) {
@@ -423,8 +423,8 @@ function requireNative() {
423
423
  try {
424
424
  const binding = require('node-tpm2-linux-riscv64-gnu')
425
425
  const bindingPackageVersion = require('node-tpm2-linux-riscv64-gnu/package.json').version
426
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
426
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
428
428
  }
429
429
  return binding
430
430
  } catch (e) {
@@ -440,8 +440,8 @@ function requireNative() {
440
440
  try {
441
441
  const binding = require('node-tpm2-linux-ppc64-gnu')
442
442
  const bindingPackageVersion = require('node-tpm2-linux-ppc64-gnu/package.json').version
443
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
445
  }
446
446
  return binding
447
447
  } catch (e) {
@@ -456,8 +456,8 @@ function requireNative() {
456
456
  try {
457
457
  const binding = require('node-tpm2-linux-s390x-gnu')
458
458
  const bindingPackageVersion = require('node-tpm2-linux-s390x-gnu/package.json').version
459
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
461
  }
462
462
  return binding
463
463
  } catch (e) {
@@ -476,8 +476,8 @@ function requireNative() {
476
476
  try {
477
477
  const binding = require('node-tpm2-openharmony-arm64')
478
478
  const bindingPackageVersion = require('node-tpm2-openharmony-arm64/package.json').version
479
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
479
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
481
481
  }
482
482
  return binding
483
483
  } catch (e) {
@@ -492,8 +492,8 @@ function requireNative() {
492
492
  try {
493
493
  const binding = require('node-tpm2-openharmony-x64')
494
494
  const bindingPackageVersion = require('node-tpm2-openharmony-x64/package.json').version
495
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
495
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
497
497
  }
498
498
  return binding
499
499
  } catch (e) {
@@ -508,8 +508,8 @@ function requireNative() {
508
508
  try {
509
509
  const binding = require('node-tpm2-openharmony-arm')
510
510
  const bindingPackageVersion = require('node-tpm2-openharmony-arm/package.json').version
511
- if (bindingPackageVersion !== '0.0.2' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
- throw new Error(`Native binding package version mismatch, expected 0.0.2 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
511
+ if (bindingPackageVersion !== '0.0.4-beta.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
+ throw new Error(`Native binding package version mismatch, expected 0.0.4-beta.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
@@ -587,5 +587,11 @@ if (!nativeBinding) {
587
587
  }
588
588
 
589
589
  module.exports = nativeBinding
590
+ module.exports.activateCredential = nativeBinding.activateCredential
590
591
  module.exports.getFixedProperties = nativeBinding.getFixedProperties
591
592
  module.exports.isAvailable = nativeBinding.isAvailable
593
+ module.exports.pcrRead = nativeBinding.pcrRead
594
+ module.exports.provisionAk = nativeBinding.provisionAk
595
+ module.exports.quote = nativeBinding.quote
596
+ module.exports.readEkCertificate = nativeBinding.readEkCertificate
597
+ module.exports.readPublic = nativeBinding.readPublic
package/native.d.ts CHANGED
@@ -1,5 +1,18 @@
1
1
  /* auto-generated by NAPI-RS */
2
2
  /* eslint-disable */
3
+ export declare function activateCredential(opts: ActivateCredentialOptionsJs): Promise<Buffer>
4
+
5
+ export interface ActivateCredentialOptionsJs {
6
+ akBlob: AkBlobJs
7
+ credentialBlob: Buffer
8
+ secret: Buffer
9
+ }
10
+
11
+ export interface AkBlobJs {
12
+ public: Buffer
13
+ private: Buffer
14
+ }
15
+
3
16
  export interface FixedPropertiesJs {
4
17
  manufacturer: string
5
18
  firmwareVersion: string
@@ -10,3 +23,43 @@ export interface FixedPropertiesJs {
10
23
  export declare function getFixedProperties(): Promise<FixedPropertiesJs>
11
24
 
12
25
  export declare function isAvailable(): Promise<boolean>
26
+
27
+ export declare function pcrRead(selection: Array<number>, bank?: string | undefined | null): Promise<Record<string, string>>
28
+
29
+ export declare function provisionAk(opts?: ProvisionAkOptionsJs | undefined | null): Promise<ProvisionAkJs>
30
+
31
+ export interface ProvisionAkJs {
32
+ akPublicDer: Buffer
33
+ akBlob: AkBlobJs
34
+ }
35
+
36
+ export interface ProvisionAkOptionsJs {
37
+ keyName?: string
38
+ /** Windows only: `"user"` (default) or `"machine"`. */
39
+ scope?: string
40
+ /** Windows only: replace existing persisted key of the same name. */
41
+ overwrite?: boolean
42
+ }
43
+
44
+ export declare function quote(opts: QuoteOptionsJs): Promise<QuoteJs>
45
+
46
+ export interface QuoteJs {
47
+ message: Buffer
48
+ signature: Buffer
49
+ }
50
+
51
+ export interface QuoteOptionsJs {
52
+ akBlob: AkBlobJs
53
+ pcrSelection: Array<number>
54
+ qualifyingData: Buffer
55
+ bank?: string
56
+ }
57
+
58
+ export declare function readEkCertificate(): Promise<Buffer | null>
59
+
60
+ export declare function readPublic(handle: string): Promise<ReadPublicJs>
61
+
62
+ export interface ReadPublicJs {
63
+ publicKeyDer: Buffer
64
+ name: Buffer
65
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-tpm2",
3
- "version": "0.0.2",
3
+ "version": "0.0.4-beta.0",
4
4
  "description": "Native TPM 2.0 for Node. Zero tooling, no admin. Windows (TBS) and Linux (/dev/tpmrm0). Pre-release.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -38,7 +38,10 @@
38
38
  "index.d.ts",
39
39
  "api.js",
40
40
  "native.cjs",
41
- "native.d.ts"
41
+ "native.d.ts",
42
+ "docs/getting-started.md",
43
+ "docs/windows-pcp.md",
44
+ "examples"
42
45
  ],
43
46
  "engines": {
44
47
  "node": ">=20"
@@ -56,26 +59,32 @@
56
59
  "access": "public"
57
60
  },
58
61
  "scripts": {
59
- "build": "napi build --platform --release --js native.cjs --dts native.d.ts",
60
- "build:debug": "napi build --platform --js native.cjs --dts native.d.ts",
62
+ "build": "napi build --platform --release --js native.cjs --dts native.d.ts && node scripts/patch-windows-npm-packages.mjs",
63
+ "build:debug": "napi build --platform --js native.cjs --dts native.d.ts && node scripts/patch-windows-npm-packages.mjs",
61
64
  "build:esapi": "napi build --platform --release --features esapi --js native.cjs --dts native.d.ts",
62
- "tbs-probe": "cargo run --bin tbs-probe",
65
+ "tbs-probe": "cargo run --bin tbs-probe --no-default-features --features probe-bin --",
66
+ "smoke-test": "node examples/smoke-test.mjs runtime",
67
+ "smoke-test:runtime": "node examples/smoke-test.mjs runtime",
63
68
  "spike": "cargo run --features esapi --bin spike --",
64
69
  "spike:all": "cargo run --features esapi --bin spike -- all",
65
70
  "artifacts": "napi artifacts",
66
71
  "create-npm-dirs": "napi create-npm-dirs",
67
- "prepublishOnly": "napi prepublish -t npm --skip-optional-publish"
72
+ "patch-windows-npm": "node scripts/patch-windows-npm-packages.mjs",
73
+ "prepublishOnly": "napi prepublish -t npm --skip-optional-publish",
74
+ "publish:beta": "node scripts/publish-beta.mjs"
68
75
  },
69
76
  "devDependencies": {
70
77
  "@napi-rs/cli": "^3.7.2"
71
78
  },
72
79
  "optionalDependencies": {
73
- "node-tpm2-win32-x64-msvc": "0.0.2",
74
- "node-tpm2-win32-arm64-msvc": "0.0.2",
75
- "node-tpm2-linux-x64-gnu": "0.0.2",
76
- "node-tpm2-linux-arm64-gnu": "0.0.2",
77
- "node-tpm2-linux-x64-musl": "0.0.2",
78
- "node-tpm2-linux-arm64-musl": "0.0.2",
79
- "node-tpm2-darwin-arm64": "0.0.2"
80
+ "node-tpm2-windows-x64-msvc": "0.0.4-beta.0",
81
+ "node-tpm2-windows-arm64-msvc": "0.0.4-beta.0",
82
+ "node-tpm2-linux-x64-gnu": "0.0.4-beta.0",
83
+ "node-tpm2-linux-arm64-gnu": "0.0.4-beta.0",
84
+ "node-tpm2-linux-x64-musl": "0.0.4-beta.0",
85
+ "node-tpm2-linux-arm64-musl": "0.0.4-beta.0",
86
+ "node-tpm2-darwin-arm64": "0.0.4-beta.0",
87
+ "node-tpm2-win32-x64-msvc": "0.0.4-beta.0",
88
+ "node-tpm2-win32-arm64-msvc": "0.0.4-beta.0"
80
89
  }
81
90
  }