Skip to content

Commit 652d3b6

Browse files
committed
replace @tls/struct with @aicone/byte
1 parent d8cd64f commit 652d3b6

17 files changed

+47
-296
lines changed

deno.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.9.1",
3+
"version": "0.9.2",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]
@@ -17,11 +17,11 @@
1717
}
1818
},
1919
"imports": {
20+
"@aicone/byte": "jsr:@aicone/byte@^0.7.9",
2021
"@noble/curves": "npm:@noble/curves@^1.8.1",
2122
"@noble/hashes": "jsr:@noble/hashes@^1.7.1",
2223
"@peculiar/x509": "npm:@peculiar/x509@^1.12.3",
2324
"@std/assert": "jsr:@std/assert@^1.0.12",
24-
"@tls/struct": "jsr:@tls/struct@^0.4.1",
2525
"ec-key": "npm:ec-key@^0.0.6",
2626
"elliptic": "npm:elliptic@^6.6.1"
2727
}

src/cipher.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export class Cipher extends Enum {
4242
return Cipher.fromValue(copy)
4343
}
4444

45-
get Uint16() { return Uint16.fromValue(this.value); }
46-
get byte() { return this.Uint16 }
45+
get byte() { return Uint16.fromValue(+this) }
4746
get length() { return 2 }
4847

4948
get hashLength() {

src/contentype.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// deno-lint-ignore-file no-slow-types
22
// @ts-self-types="../type/contentype.d.ts"
33

4-
import { Uint8 } from "./dep.ts";
54
import { Enum } from "./enum.js";
65

76
/**
@@ -28,10 +27,7 @@ export class ContentType extends Enum {
2827
return ContentType.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} ContentType type`);
2928
}
3029

31-
get Uint8() {
32-
return Uint8.fromValue(+this)
33-
}
34-
get byte() { return this.Uint8 }
30+
get byte() { return Uint8Array.of(+this) }
3531
get length(){ return 1 }
3632

3733
/**return 8 */

src/dep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from "@tls/struct"
1+
export * from "@aicone/byte"
22
export { p256 } from '@noble/curves/p256'
33
export { p384 } from '@noble/curves/p384'
44
export { p521 } from '@noble/curves/p521'

src/extensiontype.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,7 @@ export class ExtensionType extends Enum {
206206
return ExtensionType.fromValue(value) ?? Error(`Unknown ${value} extension type`);
207207
}
208208

209-
get Uint16() {
210-
return Uint16.fromValue(+this)
211-
}
212-
get byte() { return this.Uint16 }
209+
get byte() { return Uint16.fromValue(+this) }
213210

214211
/**
215212
* Bit 16

src/handshaketype.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// deno-lint-ignore-file no-slow-types
22
// @ts-self-types="../type/handshaketype.d.ts"
33
import { Enum } from "./enum.js";
4-
import { Uint8 } from "./dep.ts";
5-
64

75
/**
86
* Represents TLS 1.3 Handshake message types as defined in RFC 8446 Section 4
@@ -101,8 +99,7 @@ export class HandshakeType extends Enum {
10199
/**return 8 */
102100
get bit() { return 8 }
103101
get length() { return 1 }
104-
get Uint8() { return Uint8.fromValue(+this) }
105-
get byte() { return this.Uint8 }
102+
get byte() { return Uint8Array.of(+this) }
106103
}
107104

108105

src/namedgroup.js

Lines changed: 2 additions & 135 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 { p256, p384, p521, x25519, x448, Uint16, Constrained, Struct, safeuint8array } from "./dep.ts";
4+
import { p256, p384, p521, x25519, x448, Uint16 } from "./dep.ts";
55
import { Enum } from "./enum.js";
66

77
/**
@@ -58,13 +58,7 @@ export class NamedGroup extends Enum {
5858

5959
}
6060

61-
/**
62-
* Converts the NamedGroup to a Uint16 representation.
63-
*
64-
* @returns {Uint16} The Uint16 representation of the NamedGroup.
65-
*/
66-
get Uint16() { return Uint16.fromValue(+this); }
67-
get byte() { return this.Uint16 }
61+
get byte() { return Uint16.fromValue(+this) }
6862

6963
/**
7064
* Gets the key generation algorithm associated with the NamedGroup.
@@ -122,20 +116,6 @@ export class NamedGroup extends Enum {
122116
return sharedKey;
123117
}
124118

125-
/**
126-
* Creates a key share entry from the NamedGroup.
127-
*
128-
* @returns {KeyShareEntry} A new KeyShareEntry instance.
129-
*/
130-
async keyShareEntryAsync() {
131-
const key_exchange = KeyExchange.fromKey(await this.exportPublicKey()).key_exchange;
132-
return KeyShareEntry.fromGroupKey(this, key_exchange);
133-
}
134-
135-
keyShareEntry() {
136-
return KeyShareEntry.fromGroupKey(this, this.publicKey);
137-
}
138-
139119
async keyPair() {
140120
if (this.#keyPair) return this.#keyPair
141121
let algo
@@ -187,117 +167,4 @@ export class NamedGroup extends Enum {
187167
}
188168
}
189169

190-
/**
191-
* Represents a key exchange mechanism.
192-
*/
193-
export class KeyExchange_0 extends Constrained {
194-
195-
static fromKey(octet) { return new KeyExchange_0(octet); }
196-
197-
static from(array) {
198-
const copy = Uint8Array.from(array);
199-
const lengthOf = Uint16.from(copy.subarray(0, 2)).value;
200-
const octet = copy.subarray(2, 2 + lengthOf);
201-
return new KeyExchange_0(octet);
202-
}
203-
204-
constructor(octet) {
205-
super(1, 65535, octet);
206-
this.key_exchange = octet;
207-
}
208-
}
209-
210-
export class KeyExchange extends Uint8Array {
211-
#lengthOf
212-
#key_exchange
213-
static sanitize(array) {
214-
if (isUint8Array(array) == false) throw Error(`Expected Uint8Array`)
215-
const lengthOf = Uint16.from(array).value;
216-
if (lengthOf < 1) throw Error(`Minimum length is 1`)
217-
if (lengthOf > 2 ** 16 - 1) throw Error(`Minimum length is 2**16-1`)
218-
return [array.slice(0, 2 + lengthOf)]
219-
}
220-
static fromKey(key) {
221-
if (isUint8Array(key) == false) throw Error(`Expected Uint8Array`)
222-
const lengthOf = Uint16.fromValue(key.length)
223-
return new KeyExchange(
224-
safeuint8array(lengthOf, key)
225-
);
226-
}
227-
228-
static from(array) {
229-
return new KeyExchange(array);
230-
}
231-
232-
constructor(...args) {
233-
args = isUint8Array(args[0]) ? KeyExchange.sanitize(args[0]) : args;
234-
super(...args);
235-
}
236-
237-
get lengthOf() {
238-
if (this.#lengthOf) return this.#lengthOf
239-
this.#lengthOf ||= Uint16.from(this.subarray(0, 2)).value;
240-
return this.#lengthOf;
241-
}
242-
243-
get key_exchange() {
244-
if (this.#key_exchange) return this.#key_exchange;
245-
this.#key_exchange ||= this.subarray(2, 2 + this.lengthOf);
246-
return this.#key_exchange
247-
}
248-
}
249-
250-
/**
251-
* Represents a key share entry.
252-
*/
253-
export class KeyShareEntry_0 extends Struct {
254-
255-
static from(array) {
256-
const copy = Uint8Array.from(array);
257-
const group = NamedGroup.from(copy.subarray(0, 2));
258-
const key_exchange = KeyExchange.from(copy.subarray(2));
259-
return new KeyShareEntry_0(group, key_exchange);
260-
}
261-
262-
constructor(group, key_exchange) {
263-
super(group.Uint16, key_exchange);
264-
this.group = group;
265-
this.key_exchange = key_exchange.key_exchange;
266-
}
267-
}
268-
269-
export class KeyShareEntry extends Uint8Array {
270-
#group;
271-
#key_exchange;
272-
static fromGroupKey(group, key_exchange){
273-
return KeyShareEntry.from(
274-
safeuint8array(group.byte, KeyExchange.fromKey(key_exchange))
275-
)
276-
}
277-
static from(array){ return new KeyShareEntry(array) }
278-
constructor(...args) {
279-
super(...args)
280-
}
281-
get group() {
282-
if (this.#group) return this.#group;
283-
this.#group ||= NamedGroup.from(this.subarray(0, 2));
284-
return this.#group;
285-
}
286-
get key_exchange() {
287-
if (this.#key_exchange) return this.#key_exchange;
288-
this.#key_exchange ||= KeyExchange.from(this.subarray(2)).key_exchange;
289-
return this.#key_exchange;
290-
}
291-
}
292-
293-
async function generateKey(algo) {
294-
return await crypto.subtle.generateKey(
295-
algo,
296-
true,
297-
["deriveKey", "deriveBits"]
298-
);
299-
}
300-
301-
const isUint8Array = v => v instanceof Uint8Array
302-
303170
// npx -p typescript tsc ./src/namedgroup.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

src/pskmode.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,34 @@ import { Enum } from "./enum.js";
66
/**
77
* PskKeyExchangeMode - @see https://datatracker.ietf.org/doc/html/rfc8446#section-4.4.2
88
*/
9-
export class PskKeyExchangeMode extends Enum {
10-
11-
12-
/**
13-
* PSK-only key establishment
14-
* In this mode, the server
15-
MUST NOT supply a "key_share" value.
16-
*/
17-
static PSK_KE = new PskKeyExchangeMode('PSK_KE', 0);
18-
19-
20-
/**
21-
* PSK with (EC)DHE key establishment
22-
* In this mode, the
23-
client and server MUST supply "key_share" values as described in
24-
Section 4.2.8.
25-
*/
26-
static PSK_DHE_KE = new PskKeyExchangeMode('PSK_DHE_KE', 1);
27-
28-
static from(octet) {
29-
return PskKeyExchangeMode.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} PskKeyExchangeMode type`);
30-
}
31-
32-
/**return 8 */
33-
get bit() { return 8 }
34-
get length() { return 1 }
35-
36-
get Uint8() { return Uint8Array.of(+this)}
37-
get byte() { return this.Uint8 }
9+
export class PskKeyExchangeMode extends Enum {
10+
11+
12+
/**
13+
* PSK-only key establishment
14+
* In this mode, the server
15+
MUST NOT supply a "key_share" value.
16+
*/
17+
static PSK_KE = new PskKeyExchangeMode('PSK_KE', 0);
18+
19+
20+
/**
21+
* PSK with (EC)DHE key establishment
22+
* In this mode, the
23+
client and server MUST supply "key_share" values as described in
24+
Section 4.2.8.
25+
*/
26+
static PSK_DHE_KE = new PskKeyExchangeMode('PSK_DHE_KE', 1);
27+
28+
static from(octet) {
29+
return PskKeyExchangeMode.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} PskKeyExchangeMode type`);
30+
}
31+
32+
/**return 8 */
33+
get bit() { return 8 }
34+
get length() { return 1 }
35+
36+
get byte() { return Uint8Array.of(+this) }
3837

3938
}
4039

src/signaturescheme.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,7 @@ export class SignatureScheme extends Enum {
7272
get bit() { return 16; }
7373
get length() { return 2; }
7474

75-
/**
76-
* Converts the SignatureScheme to a Uint16 representation.
77-
*
78-
* @returns {Uint16} The Uint16 representation of the SignatureScheme.
79-
*/
80-
get Uint16() { return Uint16.fromValue(+this); }
81-
get byte() { return this.Uint16 }
75+
get byte() { return Uint16.fromValue(+this); }
8276

8377
get algo() {
8478
switch (this) {

src/version.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ export class Version extends Enum {
6868
get bit() { return 16 }
6969
get length(){ return 2 }
7070

71-
get Uint16() { return Uint16.fromValue(+this); }
72-
get byte(){ return this.Uint16 }
71+
get byte(){ return Uint16.fromValue(+this); }
7372

7473
}
7574

0 commit comments

Comments
 (0)