-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathintegration_tests.ts
294 lines (268 loc) · 8.41 KB
/
integration_tests.ts
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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
TestVectorInfo,
TestVectorResult,
parseIntegrationTestVectorsToTestVectorIterator,
PositiveTestVectorInfo,
} from '@aws-crypto/integration-vectors'
import {
EncryptTestVectorInfo,
getEncryptTestVectorIterator,
} from './get_encrypt_test_iterator'
import {
decryptMaterialsManagerNode,
encryptMaterialsManagerNode,
} from './decrypt_materials_manager_node'
import {
buildClient,
CommitmentPolicy,
MessageHeader,
needs,
DecryptOutput,
} from '@aws-crypto/client-node'
import { URL } from 'url'
import got from 'got'
import streamToPromise from 'stream-to-promise'
const { encrypt, decrypt, decryptUnsignedMessageStream } = buildClient(
CommitmentPolicy.FORBID_ENCRYPT_ALLOW_DECRYPT
)
import * as stream from 'stream'
import * as util from 'util'
const pipeline = util.promisify(stream.pipeline)
const notSupportedEncryptMessages = [
'frameLength out of bounds: 0 > frameLength >= 4294967295',
'Not supported at this time.',
'Negative Integration Tests are not supported for EncryptTests',
]
const notSupportedDecryptMessages = ['Not supported at this time.']
async function runDecryption(
testVectorInfo: TestVectorInfo
): Promise<DecryptOutput> {
const cmm = decryptMaterialsManagerNode(testVectorInfo.keysInfo)
if (testVectorInfo.decryptionMethod == 'streaming-unsigned-only') {
const plaintext: Buffer[] = []
let messageHeader: MessageHeader | false = false
// This ignores the return value, but we will either fail and throw an error
// or retrieve the header/plaintext as a side-effect.
await pipeline(
await testVectorInfo.cipherStream(),
decryptUnsignedMessageStream(cmm)
.once('MessageHeader', (header: MessageHeader) => {
messageHeader = header
})
.on('data', (chunk: Buffer) => plaintext.push(chunk)),
// This is necessary to actually make the data flow and populate the plaintext
new stream.PassThrough()
)
needs(messageHeader, 'Unknown format')
return {
plaintext: Buffer.concat(plaintext),
messageHeader,
}
} else {
return await decrypt(cmm, await testVectorInfo.cipherStream(), {})
}
}
// This is only viable for small streams, if we start get get larger streams, a stream equality should get written
export async function testDecryptVector(
testVectorInfo: TestVectorInfo
): Promise<TestVectorResult> {
if ('plainTextStream' in testVectorInfo) {
return testPositiveDecryptVector(testVectorInfo)
}
// Negative Decryption Tests
try {
await runDecryption(testVectorInfo)
} catch (err) {
return { result: true, name: testVectorInfo.name }
}
return {
result: false,
name: testVectorInfo.name,
err: new Error(
`Should have failed with ${testVectorInfo.errorDescription} but decryption succeeded`
),
}
}
async function testPositiveDecryptVector(
testVectorInfo: PositiveTestVectorInfo
): Promise<TestVectorResult> {
const knownGood = await streamToPromise(
await testVectorInfo.plainTextStream()
)
try {
const { plaintext } = await runDecryption(testVectorInfo)
if (knownGood.equals(plaintext)) {
return { result: true, name: testVectorInfo.name }
}
// noinspection ExceptionCaughtLocallyJS
throw new Error('Decrypted Plaintext did not match expected plaintext')
} catch (err) {
return { result: false, name: testVectorInfo.name, err }
}
}
// This is only viable for small streams, if we start get get larger streams, an stream equality should get written
export async function testEncryptVector(
{ name, keysInfo, encryptOp, plainTextData }: EncryptTestVectorInfo,
decryptOracle: string
): Promise<TestVectorResult> {
try {
const cmm = encryptMaterialsManagerNode(keysInfo)
const { result: encryptResult } = await encrypt(
cmm,
plainTextData,
encryptOp
)
const decryptResponse = await got.post(decryptOracle, {
headers: {
'Content-Type': 'application/octet-stream',
Accept: 'application/octet-stream',
},
body: encryptResult,
responseType: 'buffer',
})
needs(decryptResponse.statusCode === 200, 'decrypt failure')
const { body } = decryptResponse
const result = plainTextData.equals(body)
return { result, name }
} catch (err) {
return { result: false, name, err }
}
}
function handleTestResults(
{ name, result, err }: TestVectorResult,
notSupportedMessages: string[]
) {
if (result) {
console.log({ name, result })
return true
} else {
if (err && notSupportedMessages.includes(err.message)) {
console.log({ name, result: `Not supported: ${err.message}` })
return true
}
console.log({ name, result, err })
return false
}
}
export async function integrationDecryptTestVectors(
vectorFile: string,
tolerateFailures = 0,
testName?: string,
CVE202346809?: boolean,
concurrency = 1
): Promise<number> {
const tests = await parseIntegrationTestVectorsToTestVectorIterator(
vectorFile
)
return parallelTests(concurrency, tolerateFailures, runTest, tests)
async function runTest(test: TestVectorInfo): Promise<boolean> {
if (testName) {
if (test.name !== testName) return true
}
if (
!CVE202346809 &&
test.keysInfo.some(
([info, _]) =>
info.type == 'raw' && info['padding-algorithm'] == 'pkcs1'
)
) {
return true
}
return handleTestResults(
await testDecryptVector(test),
notSupportedDecryptMessages
)
}
}
export async function integrationEncryptTestVectors(
manifestFile: string,
keyFile: string,
decryptOracle: string,
tolerateFailures = 0,
testName?: string,
concurrency = 1
): Promise<number> {
const decryptOracleUrl = new URL(decryptOracle).toString()
const tests = await getEncryptTestVectorIterator(manifestFile, keyFile)
return parallelTests(concurrency, tolerateFailures, runTest, tests)
async function runTest(test: EncryptTestVectorInfo): Promise<boolean> {
if (testName) {
if (test.name !== testName) return true
}
return handleTestResults(
await testEncryptVector(test, decryptOracleUrl),
notSupportedEncryptMessages
)
}
}
async function parallelTests<
Test extends EncryptTestVectorInfo | TestVectorInfo,
work extends (test: Test) => Promise<boolean>
>(
max: number,
tolerateFailures: number,
runTest: work,
tests: IterableIterator<Test>
) {
let _resolve: (failureCount: number) => void
const queue = new Set<Promise<void>>()
let failureCount = 0
return new Promise<number>((resolve) => {
_resolve = resolve
enqueue()
})
function enqueue(): void {
/* If there are more failures than I am willing to tolerate, stop. */
if (failureCount > tolerateFailures) return _resolve(failureCount)
/* Do not over-fill the queue! */
if (queue.size > max) return
const { value, done } = tests.next()
/* There is an edge here,
* you _could_ return a value *and* be done.
* Most iterators don't but in this case
* we just process the value and ask for another.
* Which will return done as true again.
*/
if (!value && done) return _resolve(failureCount)
/* I need to define the work to be enqueue
* and a way to dequeue this work when complete.
* A Set of promises works nicely.
* Hold the variable here
* put it in the Set, take it out, and Bob's your uncle.
*/
const work: Promise<void> = runTest(value)
.then((pass: boolean) => {
if (!pass) failureCount += 1
})
/* If there is some unknown error,
* it's just an error...
* Treat it like a test failure.
*/
.catch((err) => {
console.log(err)
failureCount += 1
})
.then(() => {
/* Dequeue this work. */
queue.delete(work)
/* More to eat? */
enqueue()
})
/* Enqueue this work */
queue.add(work)
/* Fill the queue.
* The over-fill check above protects me.
* Sure, it is possible to exceed the stack depth.
* If you are trying to run ~10K tests in parallel
* on a system where that is actually faster,
* I want to talk to you.
* It is true that node can be configured to process
* > 10K HTTP requests no problem,
* but even the decrypt tests require that you first
* encrypt something locally before making the http call.
*/
enqueue()
}
}