forked from cloudflare/privacypass-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpub_verif_token.ts
More file actions
449 lines (379 loc) · 14.7 KB
/
Copy pathpub_verif_token.ts
File metadata and controls
449 lines (379 loc) · 14.7 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright (c) 2023 Cloudflare, Inc.
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
import {
type BlindRSA,
type PartiallyBlindRSA,
type BlindRSAPlatformParams,
RSABSSA,
RSAPBSSA,
} from '@cloudflare/blindrsa-ts';
import { convertEncToRSASSAPSS, convertRSASSAPSSToEnc, joinAll } from './util.js';
import {
AuthenticatorInput,
Extensions,
Token,
TokenChallenge,
type TokenTypeEntry,
} from './auth_scheme/private_token.js';
export enum BlindRSAMode {
PSSZero = 0, // Corresponds to RSASSA.SHA384.PSSZero.Deterministic
PSS = 48, // Corresponds to RSASSA.SHA384.PSS.Deterministic
}
export import PartiallyBlindRSAMode = BlindRSAMode;
export interface BlindRSAExtraParams {
suite: Record<BlindRSAMode, (params?: BlindRSAPlatformParams) => BlindRSA>;
rsaParams: RsaHashedImportParams;
}
export interface PartiallyBlindRSAExtraParams {
suite: Record<PartiallyBlindRSAMode, (params?: BlindRSAPlatformParams) => PartiallyBlindRSA>;
rsaParams: RsaHashedImportParams;
}
const BLINDRSA_EXTRA_PARAMS: BlindRSAExtraParams = {
suite: {
[BlindRSAMode.PSSZero]: RSABSSA.SHA384.PSSZero.Deterministic,
[BlindRSAMode.PSS]: RSABSSA.SHA384.PSS.Deterministic,
},
rsaParams: {
name: 'RSA-PSS',
hash: 'SHA-384',
},
} as const;
const PARTIALLY_BLINDRSA_EXTRA_PARAMS: PartiallyBlindRSAExtraParams = {
suite: {
[PartiallyBlindRSAMode.PSSZero]: RSAPBSSA.SHA384.PSSZero.Deterministic,
[PartiallyBlindRSAMode.PSS]: RSAPBSSA.SHA384.PSS.Deterministic,
},
rsaParams: {
name: 'RSA-PSS',
hash: 'SHA-384',
},
} as const;
// Token Type Entry Update:
// - Token Type Blind RSA (2048-bit)
// - Token Type Partially Blind RSA (2048-bit)
//
// https://datatracker.ietf.org/doc/html/draft-ietf-privacypass-protocol-16#name-token-type-blind-rsa-2048-b',
// https://datatracker.ietf.org/doc/html/draft-hendrickson-privacypass-public-metadata-03#section-8.2
export const BLIND_RSA: Readonly<TokenTypeEntry> & BlindRSAExtraParams = {
value: 0x0002,
name: 'Blind RSA (2048)',
Nk: 256,
Nid: 32,
publicVerifiable: true,
publicMetadata: false,
privateMetadata: false,
...BLINDRSA_EXTRA_PARAMS,
} as const;
type BlindRSAType = typeof BLIND_RSA;
export const PARTIALLY_BLIND_RSA: Readonly<TokenTypeEntry> & PartiallyBlindRSAExtraParams = {
value: 0xda7a,
name: 'Partially Blind RSA (2048-bit)',
Nk: 256,
Nid: 32,
publicVerifiable: true,
publicMetadata: true,
privateMetadata: false,
...PARTIALLY_BLINDRSA_EXTRA_PARAMS,
} as const;
type PartiallyBlindRSAType = typeof PARTIALLY_BLIND_RSA;
function getCryptoKey(publicKey: Uint8Array): Promise<CryptoKey> {
// Converts a RSA-PSS key into a RSA Encryption key.
// Required because WebCrypto do not support importing keys with `RSASSA-PSS` OID,
// See https://github.com/w3c/webcrypto/pull/325
const spkiEncoded = convertRSASSAPSSToEnc(publicKey);
return crypto.subtle.importKey('spki', spkiEncoded, BLIND_RSA.rsaParams, true, ['verify']);
}
export async function getPublicKeyBytes(publicKey: CryptoKey): Promise<Uint8Array> {
return convertEncToRSASSAPSS(new Uint8Array(await crypto.subtle.exportKey('spki', publicKey)));
}
async function getTokenKeyID(publicKey: Uint8Array): Promise<Uint8Array> {
return new Uint8Array(await crypto.subtle.digest('SHA-256', publicKey));
}
export class TokenRequest {
// struct {
// uint16_t token_type = 0x0002 | 0xda7a; /* Type Blind RSA (2048-bit) */
// uint8_t truncated_token_key_id;
// uint8_t blinded_msg[Nk];
// } TokenRequest;
tokenType: number;
constructor(
public readonly truncatedTokenKeyId: number,
public readonly blindedMsg: Uint8Array,
tokenType: TokenTypeEntry,
) {
if (blindedMsg.length !== tokenType.Nk) {
throw new Error('invalid blinded message size');
}
this.tokenType = tokenType.value;
}
static deserialize(tokenType: TokenTypeEntry, bytes: Uint8Array): TokenRequest {
let offset = 0;
const input = new DataView(bytes.buffer);
const type = input.getUint16(offset);
offset += 2;
if (type !== tokenType.value) {
throw new Error('mismatch of token type');
}
const tokenKeyId = input.getUint8(offset);
offset += 1;
const len = tokenType.Nk;
const blindedMsg = new Uint8Array(input.buffer.slice(offset, offset + len));
offset += len;
return new TokenRequest(tokenKeyId, blindedMsg, tokenType);
}
serialize(): Uint8Array {
const output = new Array<ArrayBuffer>();
let b = new ArrayBuffer(2);
new DataView(b).setUint16(0, this.tokenType);
output.push(b);
b = new ArrayBuffer(1);
new DataView(b).setUint8(0, this.truncatedTokenKeyId);
output.push(b);
b = this.blindedMsg.buffer;
output.push(b);
return new Uint8Array(joinAll(output));
}
}
export class ExtendedTokenRequest {
// struct {
// TokenRequest request;
// Extensions extensions;
// } ExtendedTokenRequest;
constructor(
public readonly request: TokenRequest,
public readonly extensions: Extensions,
) {}
static deserialize(bytes: Uint8Array): ExtendedTokenRequest {
const request = TokenRequest.deserialize(PARTIALLY_BLIND_RSA, bytes);
const extensions = Extensions.deserialize(bytes.slice(3 + PARTIALLY_BLIND_RSA.Nk));
return new ExtendedTokenRequest(request, extensions);
}
serialize(): Uint8Array {
const output = new Array<ArrayBuffer>();
const request = this.request.serialize();
output.push(request.buffer);
const extensions = this.extensions.serialize();
output.push(extensions.buffer);
return new Uint8Array(joinAll(output));
}
}
export class TokenResponse {
// struct {
// uint8_t blind_sig[Nk];
// } TokenResponse;
constructor(public readonly blindSig: Uint8Array) {
if (blindSig.length !== BLIND_RSA.Nk) {
throw new Error(`blind signature has invalid size: ${blindSig.length}`);
}
}
static deserialize(bytes: Uint8Array): TokenResponse {
return new TokenResponse(bytes.slice(0, BLIND_RSA.Nk));
}
serialize(): Uint8Array {
return new Uint8Array(this.blindSig);
}
length(): number {
return this.blindSig.length;
}
}
abstract class PubliclyVerifiableIssuer {
private suite: (extensions?: Extensions) => Pick<BlindRSA, 'blindSign' | 'verify'>;
constructor(
public readonly mode: BlindRSAMode,
public readonly name: string,
private readonly privateKey: CryptoKey,
public readonly publicKey: CryptoKey,
public readonly params?: BlindRSAPlatformParams,
) {
this.suite = (extensions?: Extensions) => {
if (extensions === undefined) {
return BLIND_RSA.suite[this.mode](this.params);
} else {
const suite = PARTIALLY_BLIND_RSA.suite[this.mode](this.params);
const serializedExtensions = extensions.serialize();
return {
blindSign: (privateKey: CryptoKey, blindMsg: Uint8Array) =>
suite.blindSign(privateKey, blindMsg, serializedExtensions),
verify: (publicKey: CryptoKey, signature: Uint8Array, message: Uint8Array) =>
suite.verify(publicKey, signature, message, serializedExtensions),
};
}
};
}
protected async _issue(tokReq: TokenRequest, extensions?: Extensions): Promise<TokenResponse> {
const blindSig = await this.suite(extensions).blindSign(this.privateKey, tokReq.blindedMsg);
return new TokenResponse(blindSig);
}
async tokenKeyID(): Promise<Uint8Array> {
return getTokenKeyID(await getPublicKeyBytes(this.publicKey));
}
verify(token: Token): Promise<boolean> {
return this.suite().verify(
this.publicKey,
token.authenticator,
token.authInput.serialize(),
);
}
}
export class Issuer extends PubliclyVerifiableIssuer {
async issue(tokReq: TokenRequest): Promise<TokenResponse> {
return super._issue(tokReq);
}
static generateKey(
mode: BlindRSAMode,
algorithm: Pick<RsaHashedKeyGenParams, 'modulusLength' | 'publicExponent'>,
): Promise<CryptoKeyPair> {
const suite = BLIND_RSA.suite[mode as unknown as BlindRSAMode]();
return suite.generateKey(algorithm);
}
}
export class IssuerWithMetadata extends PubliclyVerifiableIssuer {
async issue(tokReq: ExtendedTokenRequest): Promise<TokenResponse> {
return super._issue(tokReq.request, tokReq.extensions);
}
static generateKey(
mode: PartiallyBlindRSAMode,
algorithm: Pick<RsaHashedKeyGenParams, 'modulusLength' | 'publicExponent'>,
generateSafePrimeSync?: (length: number) => bigint,
): Promise<CryptoKeyPair> {
const suite = PARTIALLY_BLIND_RSA.suite[mode as unknown as PartiallyBlindRSAMode]();
return suite.generateKey(algorithm, generateSafePrimeSync);
}
}
abstract class PubliclyVerifiableClient {
private finData?: {
pkIssuer: CryptoKey;
tokenInput: Uint8Array;
authInput: AuthenticatorInput;
inv: Uint8Array;
};
// given extensions are known when the constructor is called, extensions can be abstracted to provide the same signature as BlindRSA
private suite: Pick<BlindRSA, 'blind' | 'finalize'>;
private tokenType: BlindRSAType | PartiallyBlindRSAType;
constructor(
public readonly mode: BlindRSAMode,
public readonly extensions?: Extensions,
) {
if (this.extensions === undefined) {
this.tokenType = BLIND_RSA;
this.suite = BLIND_RSA.suite[this.mode]();
} else {
this.tokenType = PARTIALLY_BLIND_RSA;
const suite = PARTIALLY_BLIND_RSA.suite[this.mode]();
const extensions = this.extensions.serialize();
this.suite = {
blind: (publicKey: CryptoKey, msg: Uint8Array) =>
suite.blind(publicKey, msg, extensions),
finalize: (
publicKey: CryptoKey,
msg: Uint8Array,
blindSig: Uint8Array,
inv: Uint8Array,
) => suite.finalize(publicKey, msg, extensions, blindSig, inv),
};
}
}
protected async _createTokenRequest(
tokChl: TokenChallenge,
issuerPublicKey: Uint8Array,
): Promise<TokenRequest> {
const nonce = crypto.getRandomValues(new Uint8Array(32));
const challengeDigest = new Uint8Array(
await crypto.subtle.digest('SHA-256', tokChl.serialize()),
);
const tokenKeyId = await getTokenKeyID(issuerPublicKey);
const authInput = new AuthenticatorInput(
this.tokenType,
this.tokenType.value,
nonce,
challengeDigest,
tokenKeyId,
);
const tokenInput = authInput.serialize();
const pkIssuer = await getCryptoKey(issuerPublicKey);
const { blindedMsg, inv } = await this.suite.blind(pkIssuer, tokenInput);
// "truncated_token_key_id" is the least significant byte of the
// token_key_id in network byte order (in other words, the
// last 8 bits of token_key_id).
const truncatedTokenKeyId = tokenKeyId[tokenKeyId.length - 1];
const tokenRequest = new TokenRequest(truncatedTokenKeyId, blindedMsg, this.tokenType);
this.finData = { tokenInput, authInput, inv, pkIssuer };
return tokenRequest;
}
deserializeTokenResponse(bytes: Uint8Array): TokenResponse {
return TokenResponse.deserialize(bytes);
}
async finalize(tokRes: TokenResponse): Promise<Token> {
if (!this.finData) {
throw new Error('no token request was created yet');
}
const authenticator = await this.suite.finalize(
this.finData.pkIssuer,
this.finData.tokenInput,
tokRes.blindSig,
this.finData.inv,
);
const token = new Token(this.tokenType, this.finData.authInput, authenticator);
this.finData = undefined;
return token;
}
}
export class Client extends PubliclyVerifiableClient {
async createTokenRequest(
tokChl: TokenChallenge,
issuerPublicKey: Uint8Array,
): Promise<TokenRequest> {
return super._createTokenRequest(tokChl, issuerPublicKey);
}
}
export class ClientWithMetadata extends PubliclyVerifiableClient {
async createTokenRequest(
tokChl: TokenChallenge,
issuerPublicKey: Uint8Array,
): Promise<ExtendedTokenRequest> {
const tokenRequest = await super._createTokenRequest(tokChl, issuerPublicKey);
if (!this.extensions) {
throw new Error('no extensions available');
}
return new ExtendedTokenRequest(tokenRequest, this.extensions);
}
}
abstract class PubliclyVerifiableOrigin {
private tokenType: BlindRSAType | PartiallyBlindRSAType;
private suite: Pick<BlindRSA, 'verify'>;
constructor(
public readonly mode: BlindRSAMode,
public readonly originInfo?: string[],
public readonly extensions?: Extensions,
) {
if (this.extensions === undefined) {
this.suite = BLIND_RSA.suite[this.mode]();
this.tokenType = BLIND_RSA;
} else {
const suite = PARTIALLY_BLIND_RSA.suite[this.mode]();
const extensions = this.extensions.serialize();
this.suite = {
verify: (publicKey: CryptoKey, signature: Uint8Array, message: Uint8Array) =>
suite.verify(publicKey, signature, message, extensions),
};
this.tokenType = PARTIALLY_BLIND_RSA;
}
}
async verify(token: Token, publicKeyIssuer: CryptoKey): Promise<boolean> {
return this.suite.verify(publicKeyIssuer, token.authenticator, token.authInput.serialize());
}
createTokenChallenge(issuerName: string, redemptionContext: Uint8Array): TokenChallenge {
return new TokenChallenge(
this.tokenType.value,
issuerName,
redemptionContext,
this.originInfo,
);
}
}
export class Origin extends PubliclyVerifiableOrigin {}
export class OriginWithMetadata extends PubliclyVerifiableOrigin {
constructor(mode: PartiallyBlindRSAMode, extensions: Extensions, originInfo?: string[]) {
super(mode, originInfo, extensions);
}
}