Skip to content

Commit bbc6707

Browse files
committed
signature algorithms
1 parent bc860a5 commit bbc6707

File tree

11 files changed

+166
-137
lines changed

11 files changed

+166
-137
lines changed

src/mods/binary/records/handshakes/client_hello/client_hello2.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Binary } from "@hazae41/binary"
22
import { Number16, Number8 } from "mods/binary/number.js"
33
import { Random } from "mods/binary/random.js"
4+
import { Extension } from "mods/binary/records/handshakes/extensions/extension.js"
45
import { Handshake } from "mods/binary/records/handshakes/handshake.js"
56
import { ArrayVector, Vector, Vector16, Vector8 } from "mods/binary/vector.js"
67
import { Cipher } from "mods/ciphers/cipher.js"
8+
import { SignatureAlgorithms } from "../extensions/signature_algorithms/signature_algorithms.js"
79

810
export class ClientHello2 {
911
readonly #class = ClientHello2
@@ -31,11 +33,14 @@ export class ClientHello2 {
3133
const version = 0x0303
3234
const random = Random.default()
3335

34-
const session_id = new (ArrayVector<Number8, Number8>(Number8, Number8))([])
35-
const cipher_suites = new (Vector16<Number16>(Number16))(ciphers.map(it => it.id))
36-
const compression_methods = new (Vector8<Number8>(Number8))([0])
36+
const session_id = ArrayVector<Number8, Number8>(Number8, Number8).from([])
37+
const cipher_suites = Vector16<Number16>(Number16).from(ciphers.map(it => it.id))
38+
const compression_methods = Vector8<Number8>(Number8).from([0])
3739

38-
return new this(version, random, session_id, cipher_suites, compression_methods)
40+
const signature_algorithms = SignatureAlgorithms.default().extension()
41+
const extensions = ArrayVector<Number16, Extension>(Number16, Extension).from([signature_algorithms])
42+
43+
return new this(version, random, session_id, cipher_suites, compression_methods, extensions)
3944
}
4045

4146
size() {

src/mods/binary/records/handshakes/client_hello/client_hello3.ts

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from "./client_hello2.js";
2-
export * from "./client_hello3.js";
2+
33

src/mods/binary/records/handshakes/extensions/extension.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@ import { Number16 } from "mods/binary/number.js"
33
import { AnyVector, Vector } from "mods/binary/vector.js"
44
import { Writable } from "mods/binary/writable.js"
55

6-
export interface IExtension extends Writable {
7-
type: number
8-
}
96

107
export class Extension {
118
readonly #class = Extension
129

10+
static types = {
11+
signature_algorithms: 13
12+
} as const
13+
1314
constructor(
14-
readonly type: number,
15-
readonly data: Vector<Number16>
15+
readonly extension_type: number,
16+
readonly extension_data: Vector<Number16>
1617
) { }
1718

18-
static from(extension: IExtension) {
19-
const data = new (AnyVector<Number16>(Number16))(extension)
19+
static from(type: number, extension: Writable) {
20+
const data = AnyVector<Number16, Writable>(Number16).from(extension)
2021

21-
return new this(extension.type, data)
22+
return new this(type, data)
2223
}
2324

2425
get class() {
2526
return this.#class
2627
}
2728

2829
size() {
29-
return 2 + this.data.size()
30+
return 2 + this.extension_data.size()
3031
}
3132

3233
write(binary: Binary) {
33-
binary.writeUint16(this.type)
34-
this.data.write(binary)
34+
binary.writeUint16(this.extension_type)
35+
this.extension_data.write(binary)
3536
}
3637

3738
static read(binary: Binary): Extension {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./extension.js";
2-
export * from "./supported_versions/index.js";
2+
export * from "./signature_algorithms/signature_algorithms.js";
3+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Binary } from "@hazae41/binary";
2+
import { Number16 } from "mods/binary/number.js";
3+
import { Extension } from "mods/binary/records/handshakes/extensions/extension.js";
4+
import { SignatureAndHashAlgorithm } from "mods/binary/signature.js";
5+
import { ArrayVector } from "mods/binary/vector.js";
6+
7+
export class SignatureAlgorithms {
8+
readonly #class = SignatureAlgorithms
9+
10+
static type = Extension.types.signature_algorithms
11+
12+
constructor(
13+
readonly supported_signature_algorithms: ArrayVector<Number16, SignatureAndHashAlgorithm>
14+
) { }
15+
16+
static default() {
17+
const { rsaWithSha256 } = SignatureAndHashAlgorithm.instances
18+
19+
const supported_signature_algorithms =
20+
ArrayVector<Number16, SignatureAndHashAlgorithm>(
21+
Number16, SignatureAndHashAlgorithm
22+
).from([rsaWithSha256])
23+
24+
return new this(supported_signature_algorithms)
25+
}
26+
27+
get class() {
28+
return this.#class
29+
}
30+
31+
get type() {
32+
return this.#class.type
33+
}
34+
35+
size() {
36+
return this.supported_signature_algorithms.size()
37+
}
38+
39+
write(binary: Binary) {
40+
this.supported_signature_algorithms.write(binary)
41+
}
42+
43+
extension() {
44+
return Extension.from(this.#class.type, this)
45+
}
46+
}

src/mods/binary/records/handshakes/extensions/supported_versions/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/mods/binary/records/handshakes/extensions/supported_versions/supported_versions.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/mods/binary/signature.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ import { BytesVector, Vector } from "mods/binary/vector.js";
55
export class HashAlgorithm {
66
readonly #class = HashAlgorithm
77

8-
static types = {
8+
static readonly types = {
99
none: 0,
1010
md5: 1,
1111
sha1: 2,
1212
sha224: 3,
1313
sha256: 4,
1414
sha384: 5,
1515
sha512: 6,
16-
}
16+
} as const
17+
18+
static readonly instances = {
19+
none: new this(this.types.none),
20+
md5: new this(this.types.md5),
21+
sha1: new this(this.types.sha1),
22+
sha224: new this(this.types.sha224),
23+
sha256: new this(this.types.sha256),
24+
sha384: new this(this.types.sha384),
25+
sha512: new this(this.types.sha512)
26+
} as const
1727

1828
constructor(
1929
readonly type: number
@@ -39,12 +49,19 @@ export class HashAlgorithm {
3949
export class SignatureAlgorithm {
4050
readonly #class = SignatureAlgorithm
4151

42-
static types = {
52+
static readonly types = {
4353
anonymous: 0,
4454
rsa: 1,
4555
dsa: 2,
4656
ecdsa: 3
47-
}
57+
} as const
58+
59+
static readonly instances = {
60+
anonymous: new this(this.types.anonymous),
61+
rsa: new this(this.types.rsa),
62+
dsa: new this(this.types.dsa),
63+
ecdsa: new this(this.types.ecdsa)
64+
} as const
4865

4966
constructor(
5067
readonly type: number
@@ -70,6 +87,10 @@ export class SignatureAlgorithm {
7087
export class SignatureAndHashAlgorithm {
7188
readonly #class = SignatureAndHashAlgorithm
7289

90+
static readonly instances = {
91+
rsaWithSha256: new this(HashAlgorithm.instances.sha256, SignatureAlgorithm.instances.rsa)
92+
} as const
93+
7394
constructor(
7495
readonly hash: HashAlgorithm,
7596
readonly signature: SignatureAlgorithm

0 commit comments

Comments
 (0)