Skip to content

Commit e438733

Browse files
committed
ver 0.1.7
1 parent 29ccbb6 commit e438733

File tree

7 files changed

+18
-475
lines changed

7 files changed

+18
-475
lines changed

deno.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]
@@ -14,8 +14,9 @@
1414
},
1515
"imports": {
1616
"@aicone/byte": "jsr:@aicone/byte@^0.5.0",
17+
"@noble/curves": "npm:@noble/curves@^1.6.0",
1718
"@std/assert": "jsr:@std/assert@^1.0.2",
18-
"@tls/extension": "jsr:@tls/extension@^0.0.6",
19+
"@tls/extension": "jsr:@tls/extension@^0.1.0",
1920
"@tls/struct": "jsr:@tls/struct@^0.2.2"
2021
}
2122
}

src/dep.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export { Struct, Constrained, Byte } from "@tls/struct"
22
export { Uint8, Uint16, Uint24, Uint32 } from "@tls/struct"
3-
export { Extension } from "@tls/extension"
4-
export { p256 } from 'npm:@noble/[email protected]/p256'
3+
export { Extension, KeyExchange, KeyShareEntry } from "@tls/extension"
4+
/* export { p256 } from 'npm:@noble/[email protected]/p256'
55
export { p384 } from 'npm:@noble/[email protected]/p384'
66
export { p521 } from 'npm:@noble/[email protected]/p521'
77
export { x25519 } from 'npm:@noble/[email protected]/ed25519'
8-
export { x448 } from 'npm:@noble/[email protected]/ed448'
8+
export { x448 } from 'npm:@noble/[email protected]/ed448' */

src/handshaketype.js

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ export class Handshake extends Struct {
112112
return new Handshake(msg_type, Uint24.fromValue(message.length), message)
113113
}
114114
static from(array){
115-
const msg_type = HandshakeType.fromValue(array[0]);
116-
const length = Uint24.from(array.subarray(1,3));
117-
const message = array.subarray(3, 3 + length.value)
115+
const copy = Uint8Array.from(array)
116+
const msg_type = HandshakeType.fromValue(copy[0]);
117+
const length = Uint24.from(copy.subarray(1,3));
118+
const message = copy.subarray(3, 3 + length.value)
118119
return new Handshake(msg_type, length, message)
119120
}
120121
constructor(msg_type, length, message){
@@ -124,56 +125,4 @@ export class Handshake extends Struct {
124125
}
125126
}
126127

127-
export class ClientHello extends Struct {
128-
legacy_version;
129-
random;
130-
legacy_session;
131-
cipher_suites;
132-
legacy_compression_methods;
133-
extensions;
134-
constructor(
135-
legacy_version,
136-
random,
137-
legacy_session,
138-
cipher_suites,
139-
legacy_compression_methods,
140-
extensions
141-
){
142-
super(
143-
legacy_version,
144-
random,
145-
legacy_session,
146-
cipher_suites,
147-
legacy_compression_methods,
148-
extensions
149-
)
150-
}
151-
}
152-
153-
export class ServerHello extends Struct {
154-
legacy_version;
155-
random;
156-
legacy_session_id_echo;
157-
cipher_suite;
158-
legacy_compression_method ; // Uint8 = 0
159-
extensions;
160-
constructor(
161-
legacy_version,
162-
random,
163-
legacy_session_id_echo,
164-
cipher_suite,
165-
legacy_compression_method,
166-
extensions
167-
){
168-
super(
169-
legacy_version,
170-
random,
171-
legacy_session_id_echo,
172-
cipher_suite,
173-
legacy_compression_method,
174-
extensions
175-
)
176-
}
177-
}
178-
179128
// npx -p typescript tsc ./src/handshaketype.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

src/namedgroup.js

Lines changed: 3 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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";
55
import { 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

test/extensiontype_test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { ExtensionType } from "../src/extensiontype.js";
22

3-
console.log(ExtensionType.KEY_SHARE);
4-
console.log(ExtensionType.KEY_SHARE)
5-
63
const codes = [1, 5, 10, 13, 14, 15, 16, 18, 19, 20, 21, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 65281]
74
for (const e of codes) {
85
const parse = ExtensionType.from(new Uint8Array([Math.floor(e / 256), e % 256]))

0 commit comments

Comments
 (0)