11// deno-lint-ignore-file no-slow-types
22// @ts -self-types="../type/namedgroup.d.ts"
33
4- import { Constrained , p256 , p384 , p521 , Struct , Uint16 , Byte , x25519 , x448 } from "./dep.ts" ;
4+ import { /* p256, p384, p521, x25519, x448, */ Uint16 , KeyExchange , KeyShareEntry } from "./dep.ts" ;
55import { Enum } from "./enum.js" ;
66
77/**
@@ -53,7 +53,7 @@ export class NamedGroup extends Enum {
5353 */
5454 constructor ( name , value ) {
5555 super ( name , value ) ;
56- switch ( this . name ) {
56+ /* switch (this.name) {
5757 case "SECP256R1": {
5858 this.#keyGen = p256; break;
5959 }
@@ -69,7 +69,7 @@ export class NamedGroup extends Enum {
6969 case "X448": {
7070 this.#keyGen = x448; break;
7171 }
72- }
72+ } */
7373 this . #privateKey = this . #keyGen?. utils . randomPrivateKey ( ) ;
7474 this . #publicKey = this . #keyGen?. getPublicKey ( this . #privateKey) ;
7575 }
@@ -123,228 +123,7 @@ export class NamedGroup extends Enum {
123123 }
124124}
125125
126- /**
127- * Represents a list of NamedGroup instances.
128- */
129- export class NamedGroupList extends Constrained {
130- /**
131- * Creates a NamedGroupList instance from the provided NamedGroup instances.
132- *
133- * @param {...NamedGroup } namedgroup - The NamedGroup instances to include in the list.
134- * @returns {NamedGroupList } A new instance of NamedGroupList.
135- */
136- static fromNamedGroup ( ...namedgroup ) {
137- return new NamedGroupList ( ...namedgroup ) ;
138- }
139-
140- /**
141- * Creates a NamedGroupList from a Uint8Array.
142- *
143- * @static
144- * @param {Uint8Array } array - The array to parse into a NamedGroupList.
145- * @returns {NamedGroupList } A new instance of NamedGroupList.
146- * @throws {Error } If the length of the array is invalid.
147- */
148- static from ( array ) {
149- const copy = Uint8Array . from ( array ) ;
150- const lengthOf = Byte . from ( copy . subarray ( 0 , 2 ) ) . value ;
151- const namedGroups = [ ] ;
152- let offset = 2 ;
153- while ( true ) {
154- const namedgroup = NamedGroup . from ( copy . subarray ( offset ) ) ;
155- namedGroups . push ( namedgroup ) ;
156- offset += 2 ;
157- if ( offset >= lengthOf + 2 ) break ;
158- }
159- return NamedGroupList . fromNamedGroup ( ...namedGroups ) ;
160- }
161-
162- /**
163- * Creates an instance of NamedGroupList.
164- *
165- * @param {...NamedGroup } namedgroup - The NamedGroup instances to include in the list.
166- */
167- constructor ( ...namedgroup ) {
168- const namedgroupUint16 = namedgroup . map ( e => e . Uint16 ) ;
169- super ( 2 , 65535 , ...namedgroupUint16 ) ;
170- this . namedGroups = namedgroup ;
171- }
172- }
173-
174- /**
175- * Represents a key exchange mechanism.
176- */
177- export class KeyExchange extends Constrained {
178- /**
179- * Creates a KeyExchange instance from a given octet.
180- *
181- * @static
182- * @param {Uint8Array } octet - The octet to create the KeyExchange from.
183- * @returns {KeyExchange } A new instance of KeyExchange.
184- */
185- static fromKey ( octet ) { return new KeyExchange ( octet ) ; }
186-
187- /**
188- * Creates a KeyExchange from a Uint8Array.
189- *
190- * @static
191- * @param {Uint8Array } array - The array to parse into a KeyExchange.
192- * @returns {KeyExchange } A new instance of KeyExchange.
193- * @throws {Error } If the length of the array is invalid.
194- */
195- static from ( array ) {
196- const copy = Uint8Array . from ( array ) ;
197- const lengthOf = Byte . from ( copy . subarray ( 0 , 2 ) ) . value ;
198- const octet = copy . subarray ( 2 , 2 + lengthOf ) ;
199- return new KeyExchange ( octet ) ;
200- }
201-
202- /**
203- * Creates an instance of KeyExchange.
204- *
205- * @param {Uint8Array } octet - The octet representing the key exchange.
206- */
207- constructor ( octet ) {
208- super ( 1 , 65535 , octet ) ;
209- this . key_exchange = octet ;
210- }
211- }
212-
213- /**
214- * Represents a key share entry.
215- */
216- export class KeyShareEntry extends Struct {
217- /**
218- * Creates a KeyShareEntry from a Uint8Array.
219- *
220- * @static
221- * @param {Uint8Array } array - The array to parse into a KeyShareEntry.
222- * @returns {KeyShareEntry } A new instance of KeyShareEntry.
223- */
224- static from ( array ) {
225- const copy = Uint8Array . from ( array ) ;
226- const group = NamedGroup . from ( copy . subarray ( 0 , 2 ) ) ;
227- const key_exchange = KeyExchange . from ( copy . subarray ( 2 ) ) ;
228- return new KeyShareEntry ( group , key_exchange ) ;
229- }
230126
231- /**
232- * Creates an instance of KeyShareEntry.
233- *
234- * @param {NamedGroup } group - The NamedGroup associated with the key share.
235- * @param {KeyExchange } key_exchange - The KeyExchange associated with the key share.
236- */
237- constructor ( group , key_exchange ) {
238- super ( group . Uint16 , key_exchange ) ;
239- this . group = group ;
240- this . key_exchange = key_exchange . key_exchange ;
241- }
242- }
243-
244- /**
245- * Represents a KeyShare extension in the ClientHello message in TLS handshake.
246- * This class holds multiple KeyShareEntry instances and manages their constraints.
247- */
248- export class KeyShareClientHello extends Constrained {
249- /**
250- * Creates a new instance of KeyShareClientHello from multiple KeyShareEntry instances.
251- * @param {...KeyShareEntry } keyShareEntries - The key share entries to include.
252- * @returns {KeyShareClientHello } An instance of KeyShareClientHello.
253- */
254- static fromKeyShareEntries ( ...keyShareEntries ) {
255- return new KeyShareClientHello ( ...keyShareEntries ) }
256-
257- /**
258- * Creates a new instance of KeyShareClientHello from a Uint8Array.
259- * Parses the array to extract KeyShareEntry instances.
260- * @param {Uint8Array } array - The byte array containing KeyShareEntry data.
261- * @returns {KeyShareClientHello } An instance of KeyShareClientHello.
262- */
263- static from ( array ) {
264- const copy = Uint8Array . from ( array ) ;
265- const l = Byte . from ( copy . subarray ( 0 , 2 ) ) . value ;
266- const keyShareEntries = [ ]
267- for ( let offset = 2 ; offset < l ; ) {
268- const keyShareEntry = KeyShareEntry . from ( copy . subarray ( offset ) ) ;
269- keyShareEntries . push ( keyShareEntry ) ;
270- offset += keyShareEntry . length
271- }
272- return new KeyShareClientHello ( ...keyShareEntries ) ;
273- }
274-
275- /**
276- * Constructs a new KeyShareClientHello instance.
277- * @param {...KeyShareEntry } keyShareEntries - The key share entries to include in this message.
278- */
279- constructor ( ...keyShareEntries ) {
280- super ( 0 , 65535 , ...keyShareEntries )
281- this . keyShareEntries = keyShareEntries
282- }
283- }
284-
285- /**
286- * Represents a KeyShare extension in the HelloRetryRequest message.
287- * This class manages the NamedGroup for key share negotiation.
288- */
289- export class KeyShareHelloRetryRequest extends Uint16 {
290- /**
291- * Creates a new KeyShareHelloRetryRequest instance from a NamedGroup.
292- * @param {NamedGroup } group - The named group to be included in the request.
293- * @returns {KeyShareHelloRetryRequest } An instance of KeyShareHelloRetryRequest.
294- */
295- static fromGroup ( group ) { return new KeyShareHelloRetryRequest ( group ) }
296-
297- /**
298- * Creates a new KeyShareHelloRetryRequest instance from a byte array.
299- * Parses the array to extract the NamedGroup.
300- * @param {Uint8Array } array - The byte array containing NamedGroup data.
301- * @returns {KeyShareHelloRetryRequest } An instance of KeyShareHelloRetryRequest.
302- */
303- static from ( array ) {
304- const group = NamedGroup . from ( array )
305- return new KeyShareHelloRetryRequest ( group )
306- }
307- /**
308- * Constructs a new KeyShareHelloRetryRequest instance.
309- * @param {NamedGroup } group - The named group to be used in this request.
310- */
311- constructor ( group ) {
312- super ( + group )
313- }
314- }
315-
316- /**
317- * Represents a KeyShare extension in the ServerHello message in TLS handshake.
318- * This class holds a single KeyShareEntry and manages its constraints.
319- */
320- export class KeyShareServerHello extends Uint16 {
321- /**
322- * Creates a new KeyShareServerHello instance from a KeyShareEntry.
323- * @param {KeyShareEntry } keyShareEntry - The key share entry to be included in the message.
324- * @returns {KeyShareServerHello } An instance of KeyShareServerHello.
325- */
326- static fromKeyShareEntry ( keyShareEntry ) { return new KeyShareServerHello ( keyShareEntry ) }
327-
328- /**
329- * Creates a new KeyShareServerHello instance from a byte array.
330- * Parses the array to extract a KeyShareEntry.
331- * @param {Uint8Array } array - The byte array containing KeyShareEntry data.
332- * @returns {KeyShareServerHello } An instance of KeyShareServerHello.
333- */
334- static from ( array ) {
335- const copy = Uint8Array . from ( array )
336- const keyShareEntry = KeyShareEntry . from ( copy . subarray ( offset ) ) ;
337- return new KeyShareServerHello ( keyShareEntry )
338- }
339-
340- /**
341- * Constructs a new KeyShareServerHello instance.
342- * @param {KeyShareEntry } keyShareEntry - The key share entry to be used in this message.
343- */
344- constructor ( keyShareEntry ) {
345- super ( keyShareEntry )
346- }
347- }
348127
349128
350129
0 commit comments