Skip to content

Commit 6ba7ec0

Browse files
committed
ver 0.3.5
1 parent d7d530e commit 6ba7ec0

File tree

12 files changed

+305
-33
lines changed

12 files changed

+305
-33
lines changed

deno.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.3.4",
3+
"version": "0.3.5",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]
@@ -16,8 +16,6 @@
1616
"@noble/curves": "npm:@noble/curves@^1.7.0",
1717
"@peculiar/x509": "npm:@peculiar/x509@^1.12.3",
1818
"@std/assert": "jsr:@std/assert@^1.0.2",
19-
"@tls/extension": "jsr:@tls/extension@^0.2.1",
20-
"@tls/record": "jsr:@tls/record@^0.0.7",
2119
"@tls/struct": "jsr:@tls/struct@^0.3.1"
2220
}
2321
}

src/certificatetype.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// @ts-self-types="../type/certificatetype.d.ts"
33

44
import { Enum } from "./enum.js";
5-
import { Constrained, Struct, Uint24, Uint8, Uint16, Extension } from "./dep.ts"
5+
import { Constrained, Struct, Uint24, Uint8, Uint16 } from "./dep.ts"
66
import { x509 } from "./dep.ts";
7+
import { Extension } from "./extensiontype.js"
78

89
/**
910
* Supported groups - @see https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.7.

src/contentype.js

Lines changed: 28 additions & 1 deletion
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/contentype.d.ts"
33

4-
import { Uint8, TLSPlaintext } from "./dep.ts";
4+
import { Uint8, Uint16, Struct } from "./dep.ts";
55
import { Enum } from "./enum.js";
66
import { Version } from "./version.js";
77

@@ -45,4 +45,31 @@ export class ContentType extends Enum {
4545
}
4646
}
4747

48+
export class TLSPlaintext extends Uint8Array {
49+
static from(array){
50+
let offset = 0;
51+
const copy = Uint8Array.from(array);
52+
const type = ContentType.from(copy);offset+=1;
53+
const version = Version.from(copy.subarray(offset));offset+=2;
54+
const lengthOf = Uint16.from(copy.subarray(offset)).value; offset+=2;
55+
const fragment = copy.subarray(offset, offset+lengthOf)
56+
return new TLSPlaintext(type, version, fragment)
57+
}
58+
static createFrom(type, version, fragment){ return new TLSPlaintext(type, version, fragment)}
59+
constructor(type, version, fragment){
60+
const struct = new Struct(
61+
type.Uint8,
62+
version.protocolVersion(),
63+
Uint16.fromValue(fragment.length),
64+
fragment
65+
)
66+
super(struct)
67+
68+
this.type = type;
69+
this.version = version;
70+
this.fragment = fragment
71+
this.items = struct.items
72+
}
73+
}
74+
4875
//npx -p typescript tsc ./src/contentype.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

src/dep.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
export { Struct, Constrained, Byte } from "@tls/struct"
2-
export { Uint8, Uint16, Uint24, Uint32 } from "@tls/struct"
3-
export { Extension, KeyExchange, KeyShareEntry } from "@tls/extension"
1+
export * from "@tls/struct"
42
export { p256 } from '@noble/curves/p256'
53
export { p384 } from '@noble/curves/p384'
64
export { p521 } from '@noble/curves/p521'
75
export { x25519 } from '@noble/curves/ed25519'
86
export { x448 } from '@noble/curves/ed448'
97
export * as utils from "@noble/curves/abstract/utils"
10-
export { TLSPlaintext } from "@tls/record"
118
export { HexaDecimal } from "@tls/struct"
129
export * as x509 from "@peculiar/x509"
1310

src/extensiontype.js

Lines changed: 30 additions & 1 deletion
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/extensiontype.d.ts"
33

4-
import { Uint16, Extension } from "./dep.ts";
4+
import { Uint16, Constrained, Struct } from "./dep.ts";
55
import { Enum } from "./enum.js";
66

77
/**
@@ -216,7 +216,36 @@ export class ExtensionType extends Enum {
216216
extension(extension_data){return new Extension(this, extension_data)}
217217
}
218218

219+
export class ExtensionData extends Constrained {
220+
opaque
221+
static fromOpaque(opaque) { return new ExtensionData(opaque) }
222+
static from(array) {
223+
const copy = Uint8Array.from(array);
224+
const lengthOf = Uint16.from(copy).value; // First 2 bytes represent the length
225+
const extensionData = copy.subarray(2, lengthOf + 2);
226+
return new ExtensionData(extensionData);
227+
}
228+
constructor(opaque) {
229+
super(0, 2 ** 16 - 1, opaque)
230+
this.opaque = opaque
231+
}
232+
}
219233

234+
export class Extension extends Struct {
235+
extension_type
236+
extension_data
237+
constructor(extension_type, extension_data) {
238+
super(extension_type.Uint16, ExtensionData.fromOpaque(extension_data))
239+
this.extension_type = extension_type;
240+
this.extension_data = extension_data
241+
}
242+
static from(array) {
243+
const copy = Uint8Array.from(array);
244+
const extension_type = ExtensionType.from(copy.subarray(0, 2))
245+
const extension_data = ExtensionData.from(copy.subarray(2));
246+
return new Extension(extension_type, extension_data.opaque)
247+
}
248+
}
220249

221250

222251

src/namedgroup.js

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

77
/**
@@ -122,7 +122,44 @@ export class NamedGroup extends Enum {
122122
}
123123
}
124124

125+
/**
126+
* Represents a key exchange mechanism.
127+
*/
128+
export class KeyExchange extends Constrained {
129+
130+
static fromKey(octet) { return new KeyExchange(octet); }
131+
132+
static from(array) {
133+
const copy = Uint8Array.from(array);
134+
const lengthOf = Uint16.from(copy.subarray(0, 2)).value;
135+
const octet = copy.subarray(2, 2 + lengthOf);
136+
return new KeyExchange(octet);
137+
}
138+
139+
constructor(octet) {
140+
super(1, 65535, octet);
141+
this.key_exchange = octet;
142+
}
143+
}
125144

145+
/**
146+
* Represents a key share entry.
147+
*/
148+
export class KeyShareEntry extends Struct {
149+
150+
static from(array) {
151+
const copy = Uint8Array.from(array);
152+
const group = NamedGroup.from(copy.subarray(0, 2));
153+
const key_exchange = KeyExchange.from(copy.subarray(2));
154+
return new KeyShareEntry(group, key_exchange);
155+
}
156+
157+
constructor(group, key_exchange) {
158+
super(group.Uint16, key_exchange);
159+
this.group = group;
160+
this.key_exchange = key_exchange.key_exchange;
161+
}
162+
}
126163

127164

128165

test/version_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ Deno.test("ProtocolVersion Class - Constructor", () => {
6262
});
6363

6464
const protocolVersion = ProtocolVersion.fromVersion(Version.TLS12);
65+

type/certificatetype.d.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Enum } from "../src/enum.js";
2-
import { Constrained } from "../src/dep.ts";
2+
import { Constrained, x509 } from "../src/dep.ts";
33

44
/**
55
* Represents TLS Certificate Types.
@@ -35,24 +35,35 @@ export class CertificateType extends Enum {
3535
*/
3636
export class CertificateEntry extends Uint8Array {
3737
/**
38-
* Parses an array to create a `CertificateEntry`.
39-
* @param {Uint8Array} array - The array to parse.
40-
* @returns {CertificateEntry} The parsed certificate entry.
38+
* The opaque certificate data.
4139
*/
42-
static from(array: Uint8Array): CertificateEntry;
40+
readonly opaque: Uint8Array;
4341

4442
/**
45-
* Constructs a `CertificateEntry`.
46-
* @param {Uint8Array} opaque - The certificate data.
47-
* @param {...Uint8Array} extension - The certificate extensions.
43+
* The extensions associated with the certificate.
4844
*/
49-
constructor(opaque: Uint8Array, ...extension: Uint8Array[]);
45+
readonly extension: any[];
5046

51-
/** The certificate data. */
52-
opaque: Uint8Array;
47+
/**
48+
* Parsed X.509 certificate.
49+
*/
50+
readonly x509: x509.X509Certificate;
5351

54-
/** The certificate extensions. */
55-
extension: Uint8Array[];
52+
/**
53+
* Creates a `CertificateEntry` instance from an array-like object.
54+
*
55+
* @param {Uint8Array} array - The source array-like object to create the certificate entry from.
56+
* @returns {CertificateEntry} - A new `CertificateEntry` instance.
57+
*/
58+
static from(array: Uint8Array): CertificateEntry;
59+
60+
/**
61+
* Constructs a `CertificateEntry` instance.
62+
*
63+
* @param {Uint8Array} opaque - The opaque certificate data.
64+
* @param {...Uint8Array} extension - The extensions associated with the certificate.
65+
*/
66+
constructor(opaque: Uint8Array, ...extension: Uint8Array[]);
5667
}
5768

5869
/**

type/contentype.d.ts

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Uint8, TLSPlaintext } from "../src/dep.ts";
1+
import { Uint8 } from "../src/dep.ts";
22
import { Enum } from "../src/enum.js";
33
import { Version } from "../src/version.js";
44

@@ -25,33 +25,93 @@ export class ContentType extends Enum {
2525

2626
/**
2727
* Checks the given octet and returns a valid ContentType instance.
28-
*
28+
*
2929
* @param {Uint8Array} octet - A single-octet Uint8Array to evaluate.
3030
* @returns {ContentType} The corresponding ContentType or throws an error if invalid.
3131
*/
3232
static from(octet: Uint8Array): ContentType;
3333

3434
/**
3535
* Returns the Uint8 representation of the ContentType.
36-
*
36+
*
3737
* @readonly
3838
* @type {Uint8}
3939
*/
4040
get Uint8(): Uint8;
4141

4242
/**
4343
* Returns the bit size of the ContentType.
44-
*
44+
*
4545
* @readonly
4646
* @type {8}
4747
*/
4848
get bit(): 8;
4949

5050
/**
5151
* Creates a `TLSPlaintext` instance using the ContentType, TLS version, and fragment.
52-
*
52+
*
5353
* @param {Uint8Array} fragment - The plaintext fragment to include.
5454
* @returns {TLSPlaintext} A TLSPlaintext object created with the specified parameters.
5555
*/
5656
tlsPlainText(fragment: Uint8Array): TLSPlaintext;
5757
}
58+
59+
/**
60+
* Represents a TLS plaintext record as a specialized `Uint8Array`.
61+
*/
62+
export class TLSPlaintext extends Uint8Array {
63+
/**
64+
* Parses a given array into a `TLSPlaintext` instance.
65+
*
66+
* @param {Uint8Array} array - The input byte array.
67+
* @returns {TLSPlaintext} A new `TLSPlaintext` instance created from the array.
68+
*/
69+
static from(array: Uint8Array): TLSPlaintext;
70+
71+
/**
72+
* Creates a `TLSPlaintext` instance from specific type, version, and fragment.
73+
*
74+
* @param {ContentType} type - The content type of the plaintext.
75+
* @param {Version} version - The protocol version.
76+
* @param {Uint8Array} fragment - The fragment data.
77+
* @returns {TLSPlaintext} A new `TLSPlaintext` instance.
78+
*/
79+
static createFrom(
80+
type: ContentType,
81+
version: Version,
82+
fragment: Uint8Array,
83+
): TLSPlaintext;
84+
85+
/**
86+
* Constructs a new `TLSPlaintext` instance.
87+
*
88+
* @param {ContentType} type - The content type.
89+
* @param {Version} version - The protocol version.
90+
* @param {Uint8Array} fragment - The fragment data.
91+
*/
92+
constructor(type: ContentType, version: Version, fragment: Uint8Array);
93+
94+
/**
95+
* The content type of the TLS plaintext record.
96+
* @type {ContentType}
97+
*/
98+
readonly type: ContentType;
99+
100+
/**
101+
* The protocol version of the TLS plaintext record.
102+
* @type {Version}
103+
*/
104+
readonly version: Version;
105+
106+
/**
107+
* The fragment data of the TLS plaintext record.
108+
* @type {Uint8Array}
109+
*/
110+
readonly fragment: Uint8Array;
111+
112+
/**
113+
* The underlying `Struct` instance representing the TLS plaintext record.
114+
* @type {[Uint8Array,Uint8Array,Uint8Array]}
115+
*/
116+
readonly items: [Uint8Array, Uint8Array, Uint8Array];
117+
}

0 commit comments

Comments
 (0)