@fails-components/webtransport 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/dom.d.ts +27 -10
- package/dist/lib/dom.d.ts.map +1 -1
- package/dist/lib/http2/browser/browser.d.ts +5 -2
- package/dist/lib/http2/browser/browser.d.ts.map +1 -1
- package/dist/lib/http2/browser/browserparser.d.ts.map +1 -1
- package/dist/lib/http2/node/capsuleparser.d.ts.map +1 -1
- package/dist/lib/http2/node/client.d.ts +3 -2
- package/dist/lib/http2/node/client.d.ts.map +1 -1
- package/dist/lib/http2/node/server.d.ts +10 -5
- package/dist/lib/http2/node/server.d.ts.map +1 -1
- package/dist/lib/http2/node/websocketparser.d.ts.map +1 -1
- package/dist/lib/http2/parserbase.d.ts +33 -1
- package/dist/lib/http2/parserbase.d.ts.map +1 -1
- package/dist/lib/http2/priorityscheduler.d.ts +167 -0
- package/dist/lib/http2/priorityscheduler.d.ts.map +1 -0
- package/dist/lib/http2/session.d.ts +25 -16
- package/dist/lib/http2/session.d.ts.map +1 -1
- package/dist/lib/http2/stream.d.ts +10 -0
- package/dist/lib/http2/stream.d.ts.map +1 -1
- package/dist/lib/http2/websocketcommon.d.ts.map +1 -1
- package/dist/lib/server.d.ts.map +1 -1
- package/dist/lib/session.d.ts +31 -17
- package/dist/lib/session.d.ts.map +1 -1
- package/dist/lib/stream.d.ts +13 -0
- package/dist/lib/stream.d.ts.map +1 -1
- package/dist/lib/types.d.ts +18 -15
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/lib/webtransport.browser.d.ts +3 -2
- package/dist/lib/webtransport.browser.d.ts.map +1 -1
- package/dist/lib/webtransportbase.d.ts +1 -0
- package/dist/lib/webtransportbase.d.ts.map +1 -1
- package/eslint.config.js +20 -22
- package/lib/dom.ts +31 -13
- package/lib/http2/browser/browser.js +28 -13
- package/lib/http2/browser/browserparser.js +4 -1
- package/lib/http2/node/capsuleparser.js +4 -1
- package/lib/http2/node/client.js +38 -27
- package/lib/http2/node/server.js +56 -19
- package/lib/http2/node/websocketparser.js +5 -2
- package/lib/http2/parserbase.js +76 -7
- package/lib/http2/priorityscheduler.js +654 -0
- package/lib/http2/session.js +69 -20
- package/lib/http2/stream.js +60 -10
- package/lib/http2/websocketcommon.js +1 -1
- package/lib/server.js +0 -1
- package/lib/session.js +128 -64
- package/lib/stream.js +50 -8
- package/lib/types.ts +15 -8
- package/lib/webtransport.browser.js +77 -18
- package/lib/webtransportbase.js +7 -0
- package/old_test/echoserver.js +1 -1
- package/old_test/test.js +1 -1
- package/old_test/testsuite.js +6 -2
- package/package.json +7 -28
- package/readme.md +5 -3
- package/test/bidirectional-streams.spec.js +0 -150
- package/test/datagrams.spec.js +0 -104
- package/test/fixtures/certificate.js +0 -456
- package/test/fixtures/chai.js +0 -8
- package/test/fixtures/known-bytes.js +0 -44
- package/test/fixtures/p-timeout.js +0 -33
- package/test/fixtures/quiche.browser.js +0 -2
- package/test/fixtures/quiche.js +0 -1
- package/test/fixtures/read-cert-hash.js +0 -7
- package/test/fixtures/read-stream.js +0 -57
- package/test/fixtures/reader-value.js +0 -51
- package/test/fixtures/server.js +0 -467
- package/test/fixtures/webtransport.browser.js +0 -19
- package/test/fixtures/webtransport.js +0 -3
- package/test/fixtures/write-stream.js +0 -24
- package/test/index.js +0 -124
- package/test/pw-no-https-errors.json +0 -5
- package/test/session.spec.js +0 -199
- package/test/stream-limits.spec.js +0 -259
- package/test/unidirectional-streams.spec.js +0 -133
package/test/datagrams.spec.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
|
|
3
|
-
import WebTransport from './fixtures/webtransport.js'
|
|
4
|
-
import { expect } from './fixtures/chai.js'
|
|
5
|
-
import { readStream } from './fixtures/read-stream.js'
|
|
6
|
-
import { readCertHash } from './fixtures/read-cert-hash.js'
|
|
7
|
-
import { pTimeout } from './fixtures/p-timeout.js'
|
|
8
|
-
import { quicheLoaded } from './fixtures/quiche.js'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @template T
|
|
12
|
-
* @typedef {import('../lib/types').Deferred<T>} Deferred<T>
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
describe('datagrams', function () {
|
|
16
|
-
/** @type {import('../lib/dom').WebTransport | undefined} */
|
|
17
|
-
let client
|
|
18
|
-
let forceReliable = false
|
|
19
|
-
if (process.env.USE_HTTP2 === 'true') forceReliable = true
|
|
20
|
-
|
|
21
|
-
const wtOptions = {
|
|
22
|
-
serverCertificateHashes: [
|
|
23
|
-
{
|
|
24
|
-
algorithm: 'sha-256',
|
|
25
|
-
value: readCertHash(process.env.CERT_HASH)
|
|
26
|
-
}
|
|
27
|
-
],
|
|
28
|
-
// @ts-ignore
|
|
29
|
-
forceReliable
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (process.env.NO_CERT_HASHES === 'true')
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
delete wtOptions.serverCertificateHashes
|
|
35
|
-
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
beforeEach(async () => {
|
|
38
|
-
if (
|
|
39
|
-
process.env.USE_HTTP2 !== 'true' &&
|
|
40
|
-
process.env.USE_PONYFILL !== 'true' &&
|
|
41
|
-
process.env.USE_POLYFILL !== 'true'
|
|
42
|
-
) {
|
|
43
|
-
await quicheLoaded
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
afterEach(async () => {
|
|
49
|
-
if (client != null) {
|
|
50
|
-
client.close()
|
|
51
|
-
client = undefined
|
|
52
|
-
}
|
|
53
|
-
})
|
|
54
|
-
it('client sends datagrams to the server', async () => {
|
|
55
|
-
// client context - connects to the server, sends some datagrams and reads the response
|
|
56
|
-
client = new WebTransport(
|
|
57
|
-
`${process.env.SERVER_URL}/datagrams_client_send`,
|
|
58
|
-
wtOptions
|
|
59
|
-
)
|
|
60
|
-
await client.ready
|
|
61
|
-
|
|
62
|
-
const writer = client.datagrams.writable.getWriter()
|
|
63
|
-
let closed = false
|
|
64
|
-
|
|
65
|
-
// write datagrams until the server receives one and closes the connection
|
|
66
|
-
// eslint-disable-next-line promise/catch-or-return
|
|
67
|
-
Promise.resolve().then(async () => {
|
|
68
|
-
while (!closed) {
|
|
69
|
-
try {
|
|
70
|
-
await writer.ready
|
|
71
|
-
await writer.write(Uint8Array.from([0, 1, 2, 3, 4]))
|
|
72
|
-
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
73
|
-
} catch {
|
|
74
|
-
// the session can be closed while we are writing
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
const result = await client.closed
|
|
80
|
-
closed = true
|
|
81
|
-
|
|
82
|
-
// should receive the default close info
|
|
83
|
-
expect(result).to.have.property('reason', '')
|
|
84
|
-
expect(result).to.have.property('closeCode', 0)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
it('receives datagrams from the server', async () => {
|
|
88
|
-
// client context - pipes the server's datagrams back to them
|
|
89
|
-
client = new WebTransport(
|
|
90
|
-
`${process.env.SERVER_URL}/datagrams_server_send`,
|
|
91
|
-
wtOptions
|
|
92
|
-
)
|
|
93
|
-
await client.ready
|
|
94
|
-
|
|
95
|
-
// datagram transport is unreliable, at least one message should make it through
|
|
96
|
-
const expected = 1
|
|
97
|
-
|
|
98
|
-
const received = await pTimeout(
|
|
99
|
-
readStream(client.datagrams.readable, expected),
|
|
100
|
-
1000
|
|
101
|
-
)
|
|
102
|
-
expect(received).to.have.lengthOf(expected)
|
|
103
|
-
})
|
|
104
|
-
})
|
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2022 Marten Richter or other contributers (see commit). All rights reserved.
|
|
2
|
-
// Use of this source code is governed by a BSD-style license that can be
|
|
3
|
-
// found in the LICENSE file.
|
|
4
|
-
|
|
5
|
-
// large portions taken from selfsigned with the following Copyright and license
|
|
6
|
-
/* MIT License
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2013 José F. Romaniello
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in all
|
|
18
|
-
copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
-
SOFTWARE.
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
// also large portions taken from the node-forge project licensed under
|
|
30
|
-
/* New BSD License (3-clause)
|
|
31
|
-
Copyright (c) 2010, Digital Bazaar, Inc.
|
|
32
|
-
All rights reserved.
|
|
33
|
-
|
|
34
|
-
Redistribution and use in source and binary forms, with or without
|
|
35
|
-
modification, are permitted provided that the following conditions are met:
|
|
36
|
-
* Redistributions of source code must retain the above copyright
|
|
37
|
-
notice, this list of conditions and the following disclaimer.
|
|
38
|
-
* Redistributions in binary form must reproduce the above copyright
|
|
39
|
-
notice, this list of conditions and the following disclaimer in the
|
|
40
|
-
documentation and/or other materials provided with the distribution.
|
|
41
|
-
* Neither the name of Digital Bazaar, Inc. nor the
|
|
42
|
-
names of its contributors may be used to endorse or promote products
|
|
43
|
-
derived from this software without specific prior written permission.
|
|
44
|
-
|
|
45
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
46
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
47
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
48
|
-
DISCLAIMED. IN NO EVENT SHALL DIGITAL BAZAAR BE LIABLE FOR ANY
|
|
49
|
-
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
50
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
51
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
52
|
-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
53
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
54
|
-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
|
55
|
-
|
|
56
|
-
// @ts-expect-error node-forge has no types and @types/node-forge do not include oids
|
|
57
|
-
import forge from 'node-forge'
|
|
58
|
-
import { webcrypto as crypto, X509Certificate } from 'crypto'
|
|
59
|
-
|
|
60
|
-
const { pki, asn1, oids } = forge
|
|
61
|
-
// taken from node-forge
|
|
62
|
-
/**
|
|
63
|
-
* Converts an X.509 subject or issuer to an ASN.1 RDNSequence.
|
|
64
|
-
*
|
|
65
|
-
* @param {any} obj the subject or issuer (distinguished name).
|
|
66
|
-
*
|
|
67
|
-
* @return the ASN.1 RDNSequence.
|
|
68
|
-
*/
|
|
69
|
-
function _dnToAsn1(obj) {
|
|
70
|
-
// create an empty RDNSequence
|
|
71
|
-
const rval = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [])
|
|
72
|
-
|
|
73
|
-
// iterate over attributes
|
|
74
|
-
let attr, set
|
|
75
|
-
const attrs = obj.attributes
|
|
76
|
-
for (let i = 0; i < attrs.length; ++i) {
|
|
77
|
-
attr = attrs[i]
|
|
78
|
-
let value = attr.value
|
|
79
|
-
|
|
80
|
-
// reuse tag class for attribute value if available
|
|
81
|
-
let valueTagClass = asn1.Type.PRINTABLESTRING
|
|
82
|
-
if ('valueTagClass' in attr) {
|
|
83
|
-
valueTagClass = attr.valueTagClass
|
|
84
|
-
|
|
85
|
-
if (valueTagClass === asn1.Type.UTF8) {
|
|
86
|
-
value = forge.util.encodeUtf8(value)
|
|
87
|
-
}
|
|
88
|
-
// FIXME: handle more encodings
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// create a RelativeDistinguishedName set
|
|
92
|
-
// each value in the set is an AttributeTypeAndValue first
|
|
93
|
-
// containing the type (an OID) and second the value
|
|
94
|
-
set = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true, [
|
|
95
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
|
96
|
-
// AttributeType
|
|
97
|
-
asn1.create(
|
|
98
|
-
asn1.Class.UNIVERSAL,
|
|
99
|
-
asn1.Type.OID,
|
|
100
|
-
false,
|
|
101
|
-
asn1.oidToDer(attr.type).getBytes()
|
|
102
|
-
),
|
|
103
|
-
// AttributeValue
|
|
104
|
-
asn1.create(asn1.Class.UNIVERSAL, valueTagClass, false, value)
|
|
105
|
-
])
|
|
106
|
-
])
|
|
107
|
-
rval.value.push(set)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return rval
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const jan_1_1950 = new Date('1950-01-01T00:00:00Z') // eslint-disable-line camelcase
|
|
114
|
-
const jan_1_2050 = new Date('2050-01-01T00:00:00Z') // eslint-disable-line camelcase
|
|
115
|
-
// taken from node-forge almost not modified
|
|
116
|
-
/**
|
|
117
|
-
* Converts a Date object to ASN.1
|
|
118
|
-
* Handles the different format before and after 1st January 2050
|
|
119
|
-
*
|
|
120
|
-
* @param {Date} date date object.
|
|
121
|
-
*
|
|
122
|
-
* @return the ASN.1 object representing the date.
|
|
123
|
-
*/
|
|
124
|
-
function _dateToAsn1(date) {
|
|
125
|
-
// eslint-disable-next-line camelcase
|
|
126
|
-
if (date >= jan_1_1950 && date < jan_1_2050) {
|
|
127
|
-
return asn1.create(
|
|
128
|
-
asn1.Class.UNIVERSAL,
|
|
129
|
-
asn1.Type.UTCTIME,
|
|
130
|
-
false,
|
|
131
|
-
asn1.dateToUtcTime(date)
|
|
132
|
-
)
|
|
133
|
-
} else {
|
|
134
|
-
return asn1.create(
|
|
135
|
-
asn1.Class.UNIVERSAL,
|
|
136
|
-
asn1.Type.GENERALIZEDTIME,
|
|
137
|
-
false,
|
|
138
|
-
asn1.dateToGeneralizedTime(date)
|
|
139
|
-
)
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// taken from node-forge almost not modified
|
|
144
|
-
/**
|
|
145
|
-
* Convert signature parameters object to ASN.1
|
|
146
|
-
*
|
|
147
|
-
* @param {string} oid Signature algorithm OID
|
|
148
|
-
* @param {any} params The signature parameters object
|
|
149
|
-
* @return ASN.1 object representing signature parameters
|
|
150
|
-
*/
|
|
151
|
-
function _signatureParametersToAsn1(oid, params) {
|
|
152
|
-
const parts = []
|
|
153
|
-
|
|
154
|
-
switch (oid) {
|
|
155
|
-
case oids['RSASSA-PSS']:
|
|
156
|
-
if (params.hash.algorithmOid !== undefined) {
|
|
157
|
-
parts.push(
|
|
158
|
-
asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
|
|
159
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
|
160
|
-
asn1.create(
|
|
161
|
-
asn1.Class.UNIVERSAL,
|
|
162
|
-
asn1.Type.OID,
|
|
163
|
-
false,
|
|
164
|
-
asn1.oidToDer(params.hash.algorithmOid).getBytes()
|
|
165
|
-
),
|
|
166
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
|
|
167
|
-
])
|
|
168
|
-
])
|
|
169
|
-
)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (params.mgf.algorithmOid !== undefined) {
|
|
173
|
-
parts.push(
|
|
174
|
-
asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, [
|
|
175
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
|
176
|
-
asn1.create(
|
|
177
|
-
asn1.Class.UNIVERSAL,
|
|
178
|
-
asn1.Type.OID,
|
|
179
|
-
false,
|
|
180
|
-
asn1.oidToDer(params.mgf.algorithmOid).getBytes()
|
|
181
|
-
),
|
|
182
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
|
183
|
-
asn1.create(
|
|
184
|
-
asn1.Class.UNIVERSAL,
|
|
185
|
-
asn1.Type.OID,
|
|
186
|
-
false,
|
|
187
|
-
asn1.oidToDer(params.mgf.hash.algorithmOid).getBytes()
|
|
188
|
-
),
|
|
189
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
|
|
190
|
-
])
|
|
191
|
-
])
|
|
192
|
-
])
|
|
193
|
-
)
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (params.saltLength !== undefined) {
|
|
197
|
-
parts.push(
|
|
198
|
-
asn1.create(asn1.Class.CONTEXT_SPECIFIC, 2, true, [
|
|
199
|
-
asn1.create(
|
|
200
|
-
asn1.Class.UNIVERSAL,
|
|
201
|
-
asn1.Type.INTEGER,
|
|
202
|
-
false,
|
|
203
|
-
asn1.integerToDer(params.saltLength).getBytes()
|
|
204
|
-
)
|
|
205
|
-
])
|
|
206
|
-
)
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, parts)
|
|
210
|
-
|
|
211
|
-
default:
|
|
212
|
-
return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// taken from node-forge and modified to work with ECDSA
|
|
217
|
-
/**
|
|
218
|
-
* Gets the ASN.1 TBSCertificate part of an X.509v3 certificate.
|
|
219
|
-
*
|
|
220
|
-
* @param {any} cert the certificate.
|
|
221
|
-
*
|
|
222
|
-
* @return the asn1 TBSCertificate.
|
|
223
|
-
*/
|
|
224
|
-
function getTBSCertificate(cert) {
|
|
225
|
-
// TBSCertificate
|
|
226
|
-
const notBefore = _dateToAsn1(cert.validity.notBefore)
|
|
227
|
-
const notAfter = _dateToAsn1(cert.validity.notAfter)
|
|
228
|
-
|
|
229
|
-
const tbs = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
|
230
|
-
// version
|
|
231
|
-
asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
|
|
232
|
-
// integer
|
|
233
|
-
asn1.create(
|
|
234
|
-
asn1.Class.UNIVERSAL,
|
|
235
|
-
asn1.Type.INTEGER,
|
|
236
|
-
false,
|
|
237
|
-
asn1.integerToDer(cert.version).getBytes()
|
|
238
|
-
)
|
|
239
|
-
]),
|
|
240
|
-
// serialNumber
|
|
241
|
-
asn1.create(
|
|
242
|
-
asn1.Class.UNIVERSAL,
|
|
243
|
-
asn1.Type.INTEGER,
|
|
244
|
-
false,
|
|
245
|
-
forge.util.hexToBytes(cert.serialNumber)
|
|
246
|
-
),
|
|
247
|
-
// signature
|
|
248
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
|
249
|
-
// algorithm
|
|
250
|
-
asn1.create(
|
|
251
|
-
asn1.Class.UNIVERSAL,
|
|
252
|
-
asn1.Type.OID,
|
|
253
|
-
false,
|
|
254
|
-
asn1.oidToDer(cert.siginfo.algorithmOid).getBytes()
|
|
255
|
-
),
|
|
256
|
-
// parameters
|
|
257
|
-
_signatureParametersToAsn1(
|
|
258
|
-
cert.siginfo.algorithmOid,
|
|
259
|
-
cert.siginfo.parameters
|
|
260
|
-
)
|
|
261
|
-
]),
|
|
262
|
-
// issuer
|
|
263
|
-
_dnToAsn1(cert.issuer),
|
|
264
|
-
// validity
|
|
265
|
-
asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
|
|
266
|
-
notBefore,
|
|
267
|
-
notAfter
|
|
268
|
-
]),
|
|
269
|
-
// subject
|
|
270
|
-
_dnToAsn1(cert.subject),
|
|
271
|
-
// SubjectPublicKeyInfo
|
|
272
|
-
// here comes our modification, we are other objects here
|
|
273
|
-
asn1.fromDer(
|
|
274
|
-
new forge.util.ByteBuffer(
|
|
275
|
-
cert.publicKey
|
|
276
|
-
) /* is in already SPKI format but in DER encoding */
|
|
277
|
-
)
|
|
278
|
-
])
|
|
279
|
-
|
|
280
|
-
if (cert.issuer.uniqueId) {
|
|
281
|
-
// issuerUniqueID (optional)
|
|
282
|
-
tbs.value.push(
|
|
283
|
-
asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, [
|
|
284
|
-
asn1.create(
|
|
285
|
-
asn1.Class.UNIVERSAL,
|
|
286
|
-
asn1.Type.BITSTRING,
|
|
287
|
-
false,
|
|
288
|
-
// TODO: support arbitrary bit length ids
|
|
289
|
-
String.fromCharCode(0x00) + cert.issuer.uniqueId
|
|
290
|
-
)
|
|
291
|
-
])
|
|
292
|
-
)
|
|
293
|
-
}
|
|
294
|
-
if (cert.subject.uniqueId) {
|
|
295
|
-
// subjectUniqueID (optional)
|
|
296
|
-
tbs.value.push(
|
|
297
|
-
asn1.create(asn1.Class.CONTEXT_SPECIFIC, 2, true, [
|
|
298
|
-
asn1.create(
|
|
299
|
-
asn1.Class.UNIVERSAL,
|
|
300
|
-
asn1.Type.BITSTRING,
|
|
301
|
-
false,
|
|
302
|
-
// TODO: support arbitrary bit length ids
|
|
303
|
-
String.fromCharCode(0x00) + cert.subject.uniqueId
|
|
304
|
-
)
|
|
305
|
-
])
|
|
306
|
-
)
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
if (cert.extensions.length > 0) {
|
|
310
|
-
// extensions (optional)
|
|
311
|
-
tbs.value.push(pki.certificateExtensionsToAsn1(cert.extensions))
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
return tbs
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// function taken form selfsigned
|
|
318
|
-
// a hexString is considered negative if it's most significant bit is 1
|
|
319
|
-
// because serial numbers use ones' complement notation
|
|
320
|
-
// this RFC in section 4.1.2.2 requires serial numbers to be positive
|
|
321
|
-
// http://www.ietf.org/rfc/rfc5280.txt
|
|
322
|
-
/**
|
|
323
|
-
* @param {string} hexString
|
|
324
|
-
* @returns
|
|
325
|
-
*/
|
|
326
|
-
function toPositiveHex(hexString) {
|
|
327
|
-
let mostSiginficativeHexAsInt = parseInt(hexString[0], 16)
|
|
328
|
-
if (mostSiginficativeHexAsInt < 8) {
|
|
329
|
-
return hexString
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
mostSiginficativeHexAsInt -= 8
|
|
333
|
-
return mostSiginficativeHexAsInt.toString() + hexString.substring(1)
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// the next is an edit of the selfsigned function reduced to the function necessary for webtransport
|
|
337
|
-
/**
|
|
338
|
-
* @typedef {object} Certificate
|
|
339
|
-
* @property {string} public
|
|
340
|
-
* @property {string} private
|
|
341
|
-
* @property {string} cert
|
|
342
|
-
* @property {Uint8Array} hash
|
|
343
|
-
* @property {string} fingerprint
|
|
344
|
-
*
|
|
345
|
-
* @param {*} attrs
|
|
346
|
-
* @param {*} options
|
|
347
|
-
* @returns {Promise<Certificate | null>}
|
|
348
|
-
*/
|
|
349
|
-
export async function generateWebTransportCertificate(attrs, options) {
|
|
350
|
-
try {
|
|
351
|
-
const keyPair = await crypto.subtle.generateKey(
|
|
352
|
-
{
|
|
353
|
-
name: 'ECDSA',
|
|
354
|
-
namedCurve: 'P-256'
|
|
355
|
-
},
|
|
356
|
-
true,
|
|
357
|
-
['sign', 'verify']
|
|
358
|
-
)
|
|
359
|
-
|
|
360
|
-
const cert = pki.createCertificate()
|
|
361
|
-
|
|
362
|
-
cert.serialNumber = toPositiveHex(
|
|
363
|
-
forge.util.bytesToHex(forge.random.getBytesSync(9))
|
|
364
|
-
) // the serial number can be decimal or hex (if preceded by 0x)
|
|
365
|
-
cert.validity.notBefore = new Date()
|
|
366
|
-
cert.validity.notAfter = new Date()
|
|
367
|
-
cert.validity.notAfter.setDate(
|
|
368
|
-
cert.validity.notBefore.getDate() + (options.days || 14)
|
|
369
|
-
) // per spec only 14 days allowed
|
|
370
|
-
|
|
371
|
-
cert.setSubject(attrs)
|
|
372
|
-
cert.setIssuer(attrs)
|
|
373
|
-
|
|
374
|
-
const privateKey = crypto.subtle.exportKey('pkcs8', keyPair.privateKey)
|
|
375
|
-
const publicKey = (cert.publicKey = await crypto.subtle.exportKey(
|
|
376
|
-
'spki',
|
|
377
|
-
keyPair.publicKey
|
|
378
|
-
))
|
|
379
|
-
|
|
380
|
-
cert.setExtensions(
|
|
381
|
-
options.extensions || [
|
|
382
|
-
{
|
|
383
|
-
name: 'basicConstraints',
|
|
384
|
-
cA: false
|
|
385
|
-
},
|
|
386
|
-
{
|
|
387
|
-
name: 'keyUsage',
|
|
388
|
-
keyCertSign: true,
|
|
389
|
-
digitalSignature: true,
|
|
390
|
-
nonRepudiation: true,
|
|
391
|
-
keyEncipherment: true,
|
|
392
|
-
dataEncipherment: true
|
|
393
|
-
},
|
|
394
|
-
{
|
|
395
|
-
name: 'subjectAltName',
|
|
396
|
-
altNames: [
|
|
397
|
-
{
|
|
398
|
-
type: 6, // URI
|
|
399
|
-
value: 'http://example.org/webid#me'
|
|
400
|
-
}
|
|
401
|
-
]
|
|
402
|
-
}
|
|
403
|
-
]
|
|
404
|
-
)
|
|
405
|
-
|
|
406
|
-
// to signing
|
|
407
|
-
// patch oids object
|
|
408
|
-
oids['1.2.840.10045.4.3.2'] = 'ecdsa-with-sha256'
|
|
409
|
-
oids['ecdsa-with-sha256'] = '1.2.840.10045.4.3.2'
|
|
410
|
-
|
|
411
|
-
cert.siginfo.algorithmOid = cert.signatureOid = '1.2.840.10045.4.3.2' // 'ecdsa-with-sha256'
|
|
412
|
-
|
|
413
|
-
cert.tbsCertificate = getTBSCertificate(cert)
|
|
414
|
-
const encoded = Buffer.from(
|
|
415
|
-
asn1.toDer(cert.tbsCertificate).getBytes(),
|
|
416
|
-
'binary'
|
|
417
|
-
)
|
|
418
|
-
cert.md = crypto.subtle.digest('SHA-256', encoded)
|
|
419
|
-
cert.signature = crypto.subtle.sign(
|
|
420
|
-
{
|
|
421
|
-
name: 'ECDSA',
|
|
422
|
-
hash: { name: 'SHA-256' }
|
|
423
|
-
},
|
|
424
|
-
keyPair.privateKey,
|
|
425
|
-
encoded
|
|
426
|
-
)
|
|
427
|
-
cert.md = await cert.md
|
|
428
|
-
cert.signature = await cert.signature
|
|
429
|
-
|
|
430
|
-
const pemcert = pki.certificateToPem(cert)
|
|
431
|
-
const x509cert = new X509Certificate(pemcert)
|
|
432
|
-
|
|
433
|
-
const certhash = Buffer.from(
|
|
434
|
-
x509cert.fingerprint256.split(':').map((el) => parseInt(el, 16))
|
|
435
|
-
)
|
|
436
|
-
|
|
437
|
-
const pem = {
|
|
438
|
-
private: forge.pem.encode({
|
|
439
|
-
type: 'PRIVATE KEY',
|
|
440
|
-
body: new forge.util.ByteBuffer(await privateKey).getBytes()
|
|
441
|
-
}),
|
|
442
|
-
public: forge.pem.encode({
|
|
443
|
-
type: 'PUBLIC KEY',
|
|
444
|
-
body: new forge.util.ByteBuffer(publicKey).getBytes()
|
|
445
|
-
}),
|
|
446
|
-
cert: pemcert,
|
|
447
|
-
hash: certhash,
|
|
448
|
-
fingerprint: x509cert.fingerprint256
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
return pem
|
|
452
|
-
} catch (error) {
|
|
453
|
-
console.log('error in generate certificate', error)
|
|
454
|
-
return null
|
|
455
|
-
}
|
|
456
|
-
}
|
package/test/fixtures/chai.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bytes send by tests
|
|
3
|
-
*/
|
|
4
|
-
export const KNOWN_BYTES = [
|
|
5
|
-
Uint8Array.from([0, 1, 2, 3, 4]),
|
|
6
|
-
Uint8Array.from([5, 6, 7, 8, 9]),
|
|
7
|
-
Uint8Array.from([10, 11, 12, 13, 14]),
|
|
8
|
-
Uint8Array.from([15, 16, 17, 18, 19]),
|
|
9
|
-
Uint8Array.from([20, 21, 22, 23, 24])
|
|
10
|
-
]
|
|
11
|
-
|
|
12
|
-
export const KNOWN_BYTES_LENGTH = KNOWN_BYTES.reduce(
|
|
13
|
-
(accumulator, currentValue) => accumulator + currentValue.length,
|
|
14
|
-
0
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
const createBytesChunk = function (/** @type {number} */ length) {
|
|
18
|
-
const workArray = new Array(length / 2)
|
|
19
|
-
for (let i = 0; i < length / 4; i++) {
|
|
20
|
-
workArray[2 * i + 1] = length % 0xffff
|
|
21
|
-
workArray[2 * i] = i
|
|
22
|
-
}
|
|
23
|
-
const helper = new Uint16Array(workArray)
|
|
24
|
-
const toreturn = new Uint8Array(
|
|
25
|
-
helper.buffer,
|
|
26
|
-
helper.byteOffset,
|
|
27
|
-
helper.byteLength
|
|
28
|
-
)
|
|
29
|
-
return toreturn
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const KNOWN_BYTES_LONG = [
|
|
33
|
-
createBytesChunk(60000), // 96, 234
|
|
34
|
-
createBytesChunk(12), // 0, 12
|
|
35
|
-
createBytesChunk(50000), // 195, 80
|
|
36
|
-
createBytesChunk(1600), // 6, 64
|
|
37
|
-
createBytesChunk(20000), // 78, 32
|
|
38
|
-
createBytesChunk(30000) // 117, 48
|
|
39
|
-
]
|
|
40
|
-
|
|
41
|
-
export const KNOWN_BYTES_LONG_LENGTH = KNOWN_BYTES_LONG.reduce(
|
|
42
|
-
(accumulator, currentValue) => accumulator + currentValue.length,
|
|
43
|
-
0
|
|
44
|
-
)
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
class TimeoutError extends Error {
|
|
2
|
-
/**
|
|
3
|
-
* @param {string} message
|
|
4
|
-
*/
|
|
5
|
-
constructor(message) {
|
|
6
|
-
super(message)
|
|
7
|
-
|
|
8
|
-
this.name = this[Symbol.toStringTag] = 'TimeoutError'
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @template T
|
|
14
|
-
* @param {Promise<T>} promise
|
|
15
|
-
* @param {number} timeout
|
|
16
|
-
* @returns {Promise<T>}
|
|
17
|
-
*/
|
|
18
|
-
export async function pTimeout(promise, timeout) {
|
|
19
|
-
let ref
|
|
20
|
-
|
|
21
|
-
const value = await Promise.race([
|
|
22
|
-
promise,
|
|
23
|
-
new Promise((resolve, reject) => {
|
|
24
|
-
ref = setTimeout(() => {
|
|
25
|
-
reject(new TimeoutError('timeout'))
|
|
26
|
-
}, timeout)
|
|
27
|
-
})
|
|
28
|
-
])
|
|
29
|
-
|
|
30
|
-
clearTimeout(ref)
|
|
31
|
-
|
|
32
|
-
return value
|
|
33
|
-
}
|
package/test/fixtures/quiche.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { quicheLoaded } from '../../lib/index.node.js'
|