1
1
import Logger , { formatting , LogLevel , StreamHandler } from '@matrixai/logger' ;
2
- import * as events from '../dist/events.js' ;
3
- import * as utils from '../dist/utils.js' ;
4
2
import * as peculiarWebcrypto from '@peculiar/webcrypto' ;
5
3
import * as x509 from '@peculiar/x509' ;
4
+ import * as events from '../dist/events.js' ;
5
+ import * as utils from '../dist/utils.js' ;
6
6
import QUICServer from '../dist/QUICServer.js' ;
7
7
import QUICClient from '../dist/QUICClient.js' ;
8
8
import QUICStream from '../dist/QUICStream.js' ;
@@ -21,10 +21,7 @@ const extendedKeyUsageFlags = {
21
21
22
22
async function generateKeyHMAC ( ) {
23
23
const cryptoKey = await webcrypto . subtle . generateKey (
24
- {
25
- name : 'HMAC' ,
26
- hash : 'SHA-256' ,
27
- } ,
24
+ { name : 'HMAC' , hash : 'SHA-256' } ,
28
25
true ,
29
26
[ 'sign' , 'verify' ] ,
30
27
) ;
@@ -51,10 +48,7 @@ async function signHMAC(key, data) {
51
48
const cryptoKey = await webcrypto . subtle . importKey (
52
49
'raw' ,
53
50
key ,
54
- {
55
- name : 'HMAC' ,
56
- hash : 'SHA-256' ,
57
- } ,
51
+ { name : 'HMAC' , hash : 'SHA-256' } ,
58
52
true ,
59
53
[ 'sign' , 'verify' ] ,
60
54
) ;
@@ -65,10 +59,7 @@ async function verifyHMAC(key, data, sig) {
65
59
const cryptoKey = await webcrypto . subtle . importKey (
66
60
'raw' ,
67
61
key ,
68
- {
69
- name : 'HMAC' ,
70
- hash : 'SHA-256' ,
71
- } ,
62
+ { name : 'HMAC' , hash : 'SHA-256' } ,
72
63
true ,
73
64
[ 'sign' , 'verify' ] ,
74
65
) ;
@@ -81,22 +72,13 @@ async function importPublicKey(publicKey) {
81
72
case 'RSA' :
82
73
switch ( publicKey . alg ) {
83
74
case 'RS256' :
84
- algorithm = {
85
- name : 'RSASSA-PKCS1-v1_5' ,
86
- hash : 'SHA-256' ,
87
- } ;
75
+ algorithm = { name : 'RSASSA-PKCS1-v1_5' , hash : 'SHA-256' } ;
88
76
break ;
89
77
case 'RS384' :
90
- algorithm = {
91
- name : 'RSASSA-PKCS1-v1_5' ,
92
- hash : 'SHA-384' ,
93
- } ;
78
+ algorithm = { name : 'RSASSA-PKCS1-v1_5' , hash : 'SHA-384' } ;
94
79
break ;
95
80
case 'RS512' :
96
- algorithm = {
97
- name : 'RSASSA-PKCS1-v1_5' ,
98
- hash : 'SHA-512' ,
99
- } ;
81
+ algorithm = { name : 'RSASSA-PKCS1-v1_5' , hash : 'SHA-512' } ;
100
82
break ;
101
83
default :
102
84
throw new Error ( `Unsupported algorithm ${ publicKey . alg } ` ) ;
@@ -110,32 +92,19 @@ async function importPublicKey(publicKey) {
110
92
] ) ;
111
93
}
112
94
113
- /**
114
- * Imports private key.
115
- * This uses `@peculiar/webcrypto` API for Ed25519 keys.
116
- */
117
95
async function importPrivateKey ( privateKey ) {
118
96
let algorithm ;
119
97
switch ( privateKey . kty ) {
120
98
case 'RSA' :
121
99
switch ( privateKey . alg ) {
122
100
case 'RS256' :
123
- algorithm = {
124
- name : 'RSASSA-PKCS1-v1_5' ,
125
- hash : 'SHA-256' ,
126
- } ;
101
+ algorithm = { name : 'RSASSA-PKCS1-v1_5' , hash : 'SHA-256' } ;
127
102
break ;
128
103
case 'RS384' :
129
- algorithm = {
130
- name : 'RSASSA-PKCS1-v1_5' ,
131
- hash : 'SHA-384' ,
132
- } ;
104
+ algorithm = { name : 'RSASSA-PKCS1-v1_5' , hash : 'SHA-384' } ;
133
105
break ;
134
106
case 'RS512' :
135
- algorithm = {
136
- name : 'RSASSA-PKCS1-v1_5' ,
137
- hash : 'SHA-512' ,
138
- } ;
107
+ algorithm = { name : 'RSASSA-PKCS1-v1_5' , hash : 'SHA-512' } ;
139
108
break ;
140
109
default :
141
110
throw new Error ( `Unsupported algorithm ${ privateKey . alg } ` ) ;
@@ -226,11 +195,8 @@ async function generateCertificate({
226
195
if ( duration < 0 ) {
227
196
throw new RangeError ( '`duration` must be positive' ) ;
228
197
}
229
- // X509 `UTCTime` format only has resolution of seconds
230
- // this truncates to second resolution
231
198
const notBeforeDate = new Date ( now . getTime ( ) - ( now . getTime ( ) % 1000 ) ) ;
232
199
const notAfterDate = new Date ( now . getTime ( ) - ( now . getTime ( ) % 1000 ) ) ;
233
- // If the duration is 0, then only the `now` is valid
234
200
notAfterDate . setSeconds ( notAfterDate . getSeconds ( ) + duration ) ;
235
201
if ( notBeforeDate < new Date ( 0 ) ) {
236
202
throw new RangeError (
@@ -253,26 +219,12 @@ async function generateCertificate({
253
219
const serialNumber = certId ;
254
220
const subjectNodeIdEncoded = Buffer . from ( subjectNodeId ) . toString ( 'hex' ) ;
255
221
const issuerNodeIdEncoded = Buffer . from ( issuerNodeId ) . toString ( 'hex' ) ;
256
- // The entire subject attributes and issuer attributes
257
- // is constructed via `x509.Name` class
258
- // By default this supports on a limited set of names:
259
- // CN, L, ST, O, OU, C, DC, E, G, I, SN, T
260
- // If custom names are desired, this needs to change to constructing
261
- // `new x509.Name('FOO=BAR', { FOO: '1.2.3.4' })` manually
262
- // And each custom attribute requires a registered OID
263
- // Because the OID is what is encoded into ASN.1
264
222
const subjectAttrs = [
265
- {
266
- CN : [ subjectNodeIdEncoded ] ,
267
- } ,
268
- // Filter out conflicting CN attributes
223
+ { CN : [ subjectNodeIdEncoded ] } ,
269
224
...subjectAttrsExtra . filter ( ( attr ) => ! ( 'CN' in attr ) ) ,
270
225
] ;
271
226
const issuerAttrs = [
272
- {
273
- CN : [ issuerNodeIdEncoded ] ,
274
- } ,
275
- // Filter out conflicting CN attributes
227
+ { CN : [ issuerNodeIdEncoded ] } ,
276
228
...issuerAttrsExtra . filter ( ( attr ) => ! ( 'CN' in attr ) ) ,
277
229
] ;
278
230
const signingAlgorithm = issuerPrivateCryptoKey . algorithm ;
@@ -305,34 +257,12 @@ async function generateCertificate({
305
257
extendedKeyUsageFlags . ocspSigning ,
306
258
] ) ,
307
259
new x509 . SubjectAlternativeNameExtension ( [
308
- {
309
- type : 'dns' ,
310
- value : subjectNodeIdEncoded ,
311
- } ,
312
- {
313
- type : 'dns' ,
314
- value : 'localhost' ,
315
- } ,
316
- // Quiche doesn't support IP SANs,
317
- // instead we hack these in as DNS SANs for testing purposes
318
- {
319
- type : 'dns' ,
320
- value : '127.0.0.1' ,
321
- } ,
322
- // Quiche doesn't support IP SANs,
323
- // instead we hack these in as DNS SANs for testing purposes
324
- {
325
- type : 'dns' ,
326
- value : '::1' ,
327
- } ,
328
- {
329
- type : 'ip' ,
330
- value : '127.0.0.1' ,
331
- } ,
332
- {
333
- type : 'ip' ,
334
- value : '::1' ,
335
- } ,
260
+ { type : 'dns' , value : subjectNodeIdEncoded } ,
261
+ { type : 'dns' , value : 'localhost' } ,
262
+ { type : 'dns' , value : '127.0.0.1' } ,
263
+ { type : 'dns' , value : '::1' } ,
264
+ { type : 'ip' , value : '127.0.0.1' } ,
265
+ { type : 'ip' , value : '::1' } ,
336
266
] ) ,
337
267
await x509 . SubjectKeyIdentifierExtension . create ( subjectPublicCryptoKey ) ,
338
268
] ,
@@ -376,6 +306,7 @@ async function generateTLSConfig() {
376
306
} ;
377
307
}
378
308
309
+ /* eslint-disable no-console */
379
310
const main = async ( ) => {
380
311
const logger = new Logger ( `${ QUICStream . name } Test` , LogLevel . WARN , [
381
312
new StreamHandler (
@@ -428,7 +359,6 @@ const main = async () => {
428
359
} ) ;
429
360
socketCleanMethods . extractSocket ( client ) ;
430
361
const conn = ( await connectionEventProm . p ) . detail ;
431
- // Do the test
432
362
const activeServerStreams = [ ] ;
433
363
conn . addEventListener (
434
364
events . EventQUICConnectionStream . name ,
@@ -439,7 +369,6 @@ const main = async () => {
439
369
} ,
440
370
) ;
441
371
442
- // Let's make a new streams.
443
372
for ( let i = 0 ; i < 1000 ; i ++ ) {
444
373
console . error ( 'loop' ) ;
445
374
const stream = client . connection . newStream ( ) ;
0 commit comments