forked from WICG/webpackage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrity-block-signer_test.js
More file actions
300 lines (260 loc) · 10 KB
/
Copy pathintegrity-block-signer_test.js
File metadata and controls
300 lines (260 loc) · 10 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
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import url from 'url';
import * as cborg from 'cborg';
import * as constants from '../lib/utils/constants.js';
import * as utils from '../lib/utils/utils.js';
import * as wbnSign from '../lib/wbn-sign.js';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const TEST_WEB_BUNDLE_HASH =
'95f8713d382ffefb8f1e4f464e39a2bf18280c8b26434d2fcfc08d7d710c8919ace5a652e25e66f9292cda424f20e4b53bf613bf9488140272f56a455393f7e6';
const EMPTY_INTEGRITY_BLOCK_HEX = '8448f09f968bf09f93a64432620000a080';
const TEST_ED25519_PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIB8nP5PpWU7HiILHSfh5PYzb5GAcIfHZ+bw6tcd/LZXh
-----END PRIVATE KEY-----`;
const TEST_ECDSA_P256_PRIVATE_KEY = `
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIG6HAXvoG+dOP20rbyPuGC21od4DAZCKBkPy/1902xPnoAoGCCqGSM49
AwEHoUQDQgAEHIIHO9B+7XJoXTXf3aTWC7aoK1PW4Db5Z8gSGXIkHlLrucUI4lyx
DttYYhi36vrg5nR6zrfdhe7+8F1MoTvLuw==
-----END EC PRIVATE KEY-----`;
const TEST_ED25519_WEB_BUNDLE_ID =
'4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic';
const TEST_ECDSA_P256_WEB_BUNDLE_ID =
'amoiebz32b7o24tilu257xne2yf3nkblkploanxzm7ebeglseqpfeaacai';
const IWA_SCHEME = 'isolated-app://';
describe('Web Bundle ID', () => {
const ed25519PrivateKey = wbnSign.parsePemKey(TEST_ED25519_PRIVATE_KEY);
const ecdsaP256PrivateKey = wbnSign.parsePemKey(TEST_ECDSA_P256_PRIVATE_KEY);
const testKeys = [
ed25519PrivateKey,
crypto.createPublicKey(ed25519PrivateKey),
ecdsaP256PrivateKey,
crypto.createPublicKey(ecdsaP256PrivateKey),
];
testKeys.forEach((key, index) => {
it(`calculates the ID and isolated web app origin correctly with key #${index}.`, () => {
const expectedWebBundleId = (() => {
switch (utils.getSignatureType(key)) {
case constants.SignatureType.Ed25519:
return TEST_ED25519_WEB_BUNDLE_ID;
case constants.SignatureType.EcdsaP256SHA256:
return TEST_ECDSA_P256_WEB_BUNDLE_ID;
}
})();
expect(expectedWebBundleId).toEqual(
new wbnSign.WebBundleId(key).serialize()
);
expect(`${IWA_SCHEME}${expectedWebBundleId}/`).toEqual(
new wbnSign.WebBundleId(key).serializeWithIsolatedWebAppOrigin()
);
});
});
});
describe('Integrity Block Signer', () => {
function initSignerWithTestWebBundleAndKeys(
privateKeys,
webBundleId = undefined
) {
const file = path.resolve(__dirname, 'testdata/unsigned.wbn');
const contents = fs.readFileSync(file);
const signer = new wbnSign.IntegrityBlockSigner(
contents,
/*webBundleId=*/ webBundleId ||
new wbnSign.WebBundleId(privateKeys[0]).serialize(),
privateKeys.map(
(privateKey) => new wbnSign.NodeCryptoSigningStrategy(privateKey)
)
);
return signer;
}
function createTestSuffix(publicKey) {
return constants.SignatureType[utils.getSignatureType(publicKey)];
}
it('accepts only selected key types.', () => {
for (const validKey of [
{ keyType: 'ed25519' },
{ keyType: 'ec', options: { namedCurve: 'prime256v1' } },
]) {
const keypairValid = crypto.generateKeyPairSync(
validKey.keyType,
validKey.options
);
expect(() =>
initSignerWithTestWebBundleAndKeys([keypairValid.privateKey])
).not.toThrowError();
}
for (const invalidKey of [
{ keyType: 'rsa', options: { modulusLength: 2048 } },
{ keyType: 'dsa', options: { modulusLength: 1024, divisorLength: 224 } },
{ keyType: 'ec', options: { namedCurve: 'secp256k1' } },
{ keyType: 'ed448' },
{ keyType: 'x25519' },
{ keyType: 'x448' },
]) {
const keypairInvalid = crypto.generateKeyPairSync(
invalidKey.keyType,
invalidKey.options
);
expect(() =>
initSignerWithTestWebBundleAndKeys([keypairInvalid.privateKey])
).toThrowError();
}
});
it('encodes an empty integrity block CBOR correctly.', () => {
const integrityBlock = new wbnSign.IntegrityBlock();
const cbor = integrityBlock.toCbor();
expect(cbor).toEqual(
Uint8Array.from(Buffer.from(EMPTY_INTEGRITY_BLOCK_HEX, 'hex'))
);
const decoded = cborg.decode(cbor);
expect(decoded.length).toEqual(4);
expect(decoded[0]).toEqual(constants.INTEGRITY_BLOCK_MAGIC);
expect(decoded[1]).toEqual(constants.VERSION_B2);
expect(decoded[2]).toEqual({});
expect(decoded[3]).toEqual([]);
});
it('calculates the hash of the web bundle correctly.', () => {
const keypair = crypto.generateKeyPairSync('ed25519');
const signer = initSignerWithTestWebBundleAndKeys([keypair.privateKey]);
expect(signer.calcWebBundleHash()).toEqual(
Uint8Array.from(Buffer.from(TEST_WEB_BUNDLE_HASH, 'hex'))
);
});
[
crypto.generateKeyPairSync('ed25519'),
crypto.generateKeyPairSync('ec', { namedCurve: 'prime256v1' }),
].forEach((keypair) => {
it(`generates the dataToBeSigned correctly with ${createTestSuffix(
keypair.publicKey
)}.`, () => {
const signer = initSignerWithTestWebBundleAndKeys([keypair.privateKey]);
const rawPubKey = wbnSign.getRawPublicKey(keypair.publicKey);
const dataToBeSigned = utils.generateDataToBeSigned(
signer.calcWebBundleHash(),
new wbnSign.IntegrityBlock().toCbor(),
cborg.encode({
[utils.getPublicKeyAttributeName(keypair.publicKey)]: rawPubKey,
})
);
const attributesCborHex = (() => {
switch (utils.getSignatureType(keypair.publicKey)) {
case constants.SignatureType.Ed25519:
return (
/*52*/ '0000000000000034' +
'a170656432353531395075626c69634b65795820' +
Buffer.from(rawPubKey).toString('hex')
);
case constants.SignatureType.EcdsaP256SHA256:
return (
/*62*/ '000000000000003e' +
'a178186563647361503235365348413235365075626c69634b65795821' +
Buffer.from(rawPubKey).toString('hex')
);
}
})();
const hexHashString =
/*64*/ '0000000000000040' +
TEST_WEB_BUNDLE_HASH +
/*17*/ '0000000000000011' +
EMPTY_INTEGRITY_BLOCK_HEX +
attributesCborHex;
expect(dataToBeSigned).toEqual(
Uint8Array.from(Buffer.from(hexHashString, 'hex'))
);
});
});
[
crypto.generateKeyPairSync('ed25519'),
crypto.generateKeyPairSync('ec', { namedCurve: 'prime256v1' }),
].forEach((keypair) => {
it(`generates a valid signature with ${createTestSuffix(
keypair.publicKey
)}.`, async () => {
const signer = initSignerWithTestWebBundleAndKeys([keypair.privateKey]);
const rawPubKey = wbnSign.getRawPublicKey(keypair.publicKey);
const sigAttr = {
[utils.getPublicKeyAttributeName(keypair.publicKey)]: rawPubKey,
};
const webBundleId = new wbnSign.WebBundleId(
keypair.privateKey
).serialize();
const ibWithoutSignatures = new wbnSign.IntegrityBlock();
ibWithoutSignatures.setWebBundleId(webBundleId);
const dataToBeSigned = utils.generateDataToBeSigned(
signer.calcWebBundleHash(),
ibWithoutSignatures.toCbor(),
cborg.encode(sigAttr)
);
const ib = cborg.decode((await signer.sign()).integrityBlock);
expect(ib.length).toEqual(4);
const [magic, version, attributes, signatureStack] = ib;
expect(magic).toEqual(constants.INTEGRITY_BLOCK_MAGIC);
expect(version).toEqual(constants.VERSION_B2);
expect(attributes).toEqual({
webBundleId: webBundleId,
});
expect(signatureStack.length).toEqual(1);
expect(signatureStack[0].length).toEqual(2);
const [signatureAttributes, signature] = signatureStack[0];
expect(signatureAttributes).toEqual(sigAttr);
// For ECDSA P-256 keys the algorithm is implicitly selected as SHA-256.
expect(
crypto.verify(
/*algorithm=*/ undefined,
dataToBeSigned,
keypair.publicKey,
signature
)
).toBeTruthy();
});
});
it(`generates valid signatures with multiple keys`, async () => {
const keyPairs = [
crypto.generateKeyPairSync('ed25519'),
crypto.generateKeyPairSync('ec', { namedCurve: 'prime256v1' }),
crypto.generateKeyPairSync('ed25519'),
crypto.generateKeyPairSync('ec', { namedCurve: 'prime256v1' }),
];
const webBundleId = 'aaa';
const signer = initSignerWithTestWebBundleAndKeys(
keyPairs.map((keyPair) => keyPair.privateKey),
webBundleId
);
const ibData = (await signer.sign()).integrityBlock;
const ib = cborg.decode(ibData);
expect(ib.length).toEqual(4);
const [magic, version, ibAttributes, signatureStack] = ib;
expect(magic).toEqual(constants.INTEGRITY_BLOCK_MAGIC);
expect(version).toEqual(constants.VERSION_B2);
expect(Object.keys(ibAttributes).length).toEqual(1);
expect(ibAttributes.webBundleId).toEqual(webBundleId);
expect(signatureStack.length).toEqual(keyPairs.length);
signatureStack.map((entry) => expect(entry.length).toEqual(2));
const ibWithoutSignatures = new wbnSign.IntegrityBlock();
ibWithoutSignatures.setWebBundleId(webBundleId);
for (let i = 0; i < keyPairs.length; i++) {
const publicKey = keyPairs[i].publicKey;
const attrName = utils.getPublicKeyAttributeName(publicKey);
const rawPubKey = wbnSign.getRawPublicKey(publicKey);
const [signatureAttributes, signature] = signatureStack[i];
expect(Object.keys(signatureAttributes).length).toEqual(1);
expect(signatureAttributes[attrName]).toEqual(rawPubKey);
const dataToBeSigned = utils.generateDataToBeSigned(
signer.calcWebBundleHash(),
ibWithoutSignatures.toCbor(),
cborg.encode(signatureAttributes)
);
// For ECDSA P-256 keys the algorithm is implicitly selected as SHA-256.
expect(
crypto.verify(
/*algorithm=*/ undefined,
dataToBeSigned,
publicKey,
signature
)
).toBeTruthy();
}
});
});