forked from cloudflare/privacypass-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpub_verif_metadata.example.ts
More file actions
86 lines (75 loc) · 3.57 KB
/
Copy pathpub_verif_metadata.example.ts
File metadata and controls
86 lines (75 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2024 Cloudflare, Inc.
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
import { generatePrimeSync } from 'node:crypto';
import { Extension, Extensions, TOKEN_TYPES, publicVerif } from '../src/index.js';
type PartiallyBlindRSAMode = publicVerif.PartiallyBlindRSAMode;
const {
PartiallyBlindRSAMode,
ClientWithMetadata,
IssuerWithMetadata,
OriginWithMetadata,
getPublicKeyBytes,
} = publicVerif;
async function setup(mode: PartiallyBlindRSAMode, extensions: Extensions) {
// [ Issuer ] creates a key pair.
const keys = await IssuerWithMetadata.generateKey(
mode,
{
modulusLength: 2048,
publicExponent: Uint8Array.from([1, 0, 1]),
},
(length: number) => generatePrimeSync(length, { safe: true, bigint: true }),
);
const issuer = new IssuerWithMetadata(mode, 'issuer.com', keys.privateKey, keys.publicKey);
const pkIssuer = await getPublicKeyBytes(issuer.publicKey);
// [ Client ] creates a state.
const client = new ClientWithMetadata(mode, extensions);
// [ Origin ] creates a state.
const origin = new OriginWithMetadata(mode, extensions, [
'origin.example.com',
'origin2.example.com',
]);
return { issuer, client, origin, pkIssuer };
}
const TEST_EXTENSION_TYPE = 0xacdc;
function createTestExtension(info = new Uint8Array([TEST_EXTENSION_TYPE])) {
return new Extension(TEST_EXTENSION_TYPE, info);
}
async function rsaVariant(mode: PartiallyBlindRSAMode): Promise<void> {
// Protocol Setup
//
// [ Everybody ] agree to use Public Verifiable Tokens with Metadata.
const extensions = new Extensions([createTestExtension(new Uint8Array([1, 2, 3]))]);
const { issuer, client, origin, pkIssuer } = await setup(mode, extensions);
// Online Protocol
//
// +--------+ +--------+ +----------+ +--------+
// | Origin | | Client | | Attester | | Issuer |
// +---+----+ +---+----+ +----+-----+ +---+----+
// | | | |
// |<----- Request ------+ | |
const redemptionContext = crypto.getRandomValues(new Uint8Array(32));
const tokChl = origin.createTokenChallenge(issuer.name, redemptionContext);
// +-- TokenChallenge -->| | |
// | |<== Attestation ==>| |
// | | | |
const tokReq = await client.createTokenRequest(tokChl, pkIssuer);
// | +----- ExtendedTokenRequest --->|
// | | | |
const tokRes = await issuer.issue(tokReq);
// | |<-------- TokenResponse -------+
// | | | |
const token = await client.finalize(tokRes);
// |<-- Request+Token ---+ | |
// | | | |
const isValid = await origin.verify(token, issuer.publicKey);
console.log('Public-Verifiable With Metadata tokens');
console.log(
` Suite: ${TOKEN_TYPES.PARTIALLY_BLIND_RSA.suite[mode as unknown as PartiallyBlindRSAMode]()}`,
);
console.log(` Valid token: ${isValid}`);
}
export async function publicVerifiableWithMetadataTokens() {
await rsaVariant(PartiallyBlindRSAMode.PSS);
await rsaVariant(PartiallyBlindRSAMode.PSSZero);
}