Skip to content

Commit ae39ca0

Browse files
committed
add PskExchangeModes
1 parent 13668ba commit ae39ca0

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.5.9",
3+
"version": "0.6.0",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]

src/pskmode.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
// deno-lint-ignore-file no-slow-types
22
// @ts-self-types="../type/pskmode.d.ts"
33

4+
import { Constrained, Uint8 } from "./dep.ts";
45
import { Enum } from "./enum.js";
6+
import { parseItems } from "./utils.js";
57

68
/**
79
* PskKeyExchangeMode - @see https://datatracker.ietf.org/doc/html/rfc8446#section-4.4.2
810
*/
911
export class PskKeyExchangeMode extends Enum {
1012

13+
14+
/**
15+
* PSK-only key establishment
16+
* In this mode, the server
17+
MUST NOT supply a "key_share" value.
18+
*/
1119
static PSK_KE = new PskKeyExchangeMode('PSK_KE', 0);
1220

21+
22+
/**
23+
* PSK with (EC)DHE key establishment
24+
* In this mode, the
25+
client and server MUST supply "key_share" values as described in
26+
Section 4.2.8.
27+
*/
1328
static PSK_DHE_KE = new PskKeyExchangeMode('PSK_DHE_KE', 1);
1429

1530
static from(octet) {
@@ -24,4 +39,21 @@ export class PskKeyExchangeMode extends Enum {
2439

2540
}
2641

42+
export class PskKeyExchangeModes extends Constrained {
43+
static from(array){
44+
const copy = Uint8Array.from(array);
45+
const lengthOf = Uint8.from(copy).value;
46+
const ke_modes = parseItems(copy, 1, lengthOf, PskKeyExchangeMode);
47+
return new PskKeyExchangeModes(...ke_modes)
48+
}
49+
constructor(...ke_modes) {
50+
super(1, 255, ...ke_modes.map(e => e.Uint8))
51+
this.ke_modes = ke_modes;
52+
}
53+
}
54+
55+
/* const test = new PskKeyExchangeModes(PskKeyExchangeMode.PSK_KE);
56+
const back = PskKeyExchangeModes.from(test); */
57+
58+
2759
// npx -p typescript tsc ./src/pskmode.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

type/pskmode.d.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1+
import { Constrained } from "../src/dep.ts";
12
import { Enum } from "../src/enum.js";
23

34
/**
45
* Represents the PskKeyExchangeMode, as defined in RFC 8446, Section 4.4.2.
56
* @see https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.7.
67
*/
78
export declare class PskKeyExchangeMode extends Enum {
8-
/** PSK-only key establishment. The server MUST NOT supply a "key_share" value. */
9-
static PSK_KE: PskKeyExchangeMode;
9+
/** PSK-only key establishment. The server MUST NOT supply a "key_share" value. */
10+
static PSK_KE: PskKeyExchangeMode;
1011

11-
/** PSK with (EC)DHE key establishment. Both the client and server MUST supply "key_share" values. */
12-
static PSK_DHE_KE: PskKeyExchangeMode;
12+
/** PSK with (EC)DHE key establishment. Both the client and server MUST supply "key_share" values. */
13+
static PSK_DHE_KE: PskKeyExchangeMode;
1314

14-
/**
15-
* Creates a PskKeyExchangeMode instance from an octet.
16-
* @param octet - The octet value (Uint8Array) to parse.
17-
* @returns {PskKeyExchangeMode} - The corresponding PskKeyExchangeMode instance.
18-
* @throws {Error} - If the octet is not a valid PskKeyExchangeMode type.
19-
*/
20-
static from(octet: Uint8Array): PskKeyExchangeMode;
15+
/**
16+
* Creates a PskKeyExchangeMode instance from an octet.
17+
* @param octet - The octet value (Uint8Array) to parse.
18+
* @returns {PskKeyExchangeMode} - The corresponding PskKeyExchangeMode instance.
19+
* @throws {Error} - If the octet is not a valid PskKeyExchangeMode type.
20+
*/
21+
static from(octet: Uint8Array): PskKeyExchangeMode;
2122

22-
/** Returns the bit value for the mode (8). */
23-
readonly bit: number;
23+
/** Returns the bit value for the mode (8). */
24+
readonly bit: number;
2425

25-
/** Returns the Uint8Array representation of the mode. */
26-
readonly Uint8: Uint8Array;
26+
/** Returns the Uint8Array representation of the mode. */
27+
readonly Uint8: Uint8Array;
28+
}
29+
30+
export declare class PskKeyExchangeModes extends Constrained {
31+
/**
32+
* Parses a `PskKeyExchangeModes` instance from a `Uint8Array`.
33+
* @param array - The input array containing PSK key exchange mode data.
34+
* @returns A new instance of `PskKeyExchangeModes`.
35+
* @throws {Error} If the input array is invalid or incomplete.
36+
*/
37+
static from(array: Uint8Array): PskKeyExchangeModes;
38+
39+
/**
40+
* Constructs a `PskKeyExchangeModes` instance.
41+
* @param ke_modes - A list of PSK key exchange modes.
42+
* @throws {Error} If the constraints are not satisfied.
43+
*/
44+
constructor(...ke_modes: PskKeyExchangeMode[]);
45+
46+
/**
47+
* The list of PSK key exchange modes.
48+
*/
49+
readonly ke_modes: PskKeyExchangeMode[];
2750
}

0 commit comments

Comments
 (0)