Skip to content

Commit 3cb2238

Browse files
committed
add TLSInnerPlaintext and TLSChipertext
1 parent be12a9b commit 3cb2238

File tree

3 files changed

+101
-11
lines changed

3 files changed

+101
-11
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.3.8",
3+
"version": "0.3.9",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]
@@ -18,6 +18,6 @@
1818
"@peculiar/x509": "npm:@peculiar/x509@^1.12.3",
1919
"@std/assert": "jsr:@std/assert@^1.0.2",
2020
"@tls/crypto": "jsr:@tls/crypto@^0.0.9",
21-
"@tls/struct": "jsr:@tls/struct@^0.3.1"
21+
"@tls/struct": "jsr:@tls/struct@^0.3.2"
2222
}
2323
}

src/contentype.js

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,42 @@ export class ContentType extends Enum {
2929
return ContentType.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} ContentType type`);
3030
}
3131

32-
get Uint8(){
32+
get Uint8() {
3333
return Uint8.fromValue(+this)
3434
}
3535

3636
/**return 8 */
3737
get bit() { return 8 }
3838

39-
tlsPlainText(fragment){
39+
tlsPlainText(fragment) {
4040
return TLSPlaintext.createFrom(
4141
this,
4242
Version.TLS13,
4343
fragment
4444
)
4545
}
46+
47+
tlsInnerPlaintext(content, numZeros){
48+
return new TLSInnerPlaintext(content, this, numZeros)
49+
}
50+
51+
tlsCiphertext(encrypted_record){
52+
return new TLSCiphertext(encrypted_record)
53+
}
4654
}
4755

4856
export class TLSPlaintext extends Uint8Array {
49-
static from(array){
57+
static from(array) {
5058
let offset = 0;
5159
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)
60+
const type = ContentType.from(copy); offset += 1;
61+
const version = Version.from(copy.subarray(offset)); offset += 2;
62+
const lengthOf = Uint16.from(copy.subarray(offset)).value; offset += 2;
63+
const fragment = copy.subarray(offset, offset + lengthOf)
5664
return new TLSPlaintext(type, version, fragment)
5765
}
58-
static createFrom(type, version, fragment){ return new TLSPlaintext(type, version, fragment)}
59-
constructor(type, version, fragment){
66+
static createFrom(type, version, fragment) { return new TLSPlaintext(type, version, fragment) }
67+
constructor(type, version, fragment) {
6068
const struct = new Struct(
6169
type.Uint8,
6270
version.protocolVersion(),
@@ -72,4 +80,42 @@ export class TLSPlaintext extends Uint8Array {
7280
}
7381
}
7482

83+
export class TLSInnerPlaintext extends Uint8Array {
84+
static from(array){
85+
const copy = Uint8Array.from(array);
86+
const lastNonZeroIndex = copy.reduceRight((li,v,i)=>(li===-1 && v!==0? i:li),-1);
87+
const content = copy.slice(0, lastNonZeroIndex);
88+
const type = ContentType.fromValue(copy[lastNonZeroIndex]);
89+
const numZeros = copy.length - 1 - lastNonZeroIndex;
90+
return new TLSInnerPlaintext(content, type, numZeros)
91+
}
92+
constructor(content, type, numZeros) {
93+
const struct = new Uint8Array(content.length + 1 + numZeros);
94+
struct.set(content, 0);
95+
struct[content.length] = +type;
96+
super(struct)
97+
}
98+
}
99+
100+
export class TLSCiphertext extends Uint8Array {
101+
static from(array){
102+
const copy = Uint8Array.from(array);
103+
// NOTE should check contentType
104+
// NOTE legacy version can be bypassed
105+
const lengthOf = Uint16.from(copy.subarray(3));
106+
const encrypted_record = copy.subarray(5, lengthOf+5);
107+
return new TLSCiphertext(encrypted_record)
108+
}
109+
constructor(encrypted_record){
110+
const struct = new Uint8Array(encrypted_record.length+5);
111+
const lengthOf = Uint16.fromValue(encrypted_record.length);
112+
struct[0] = 23; // always application data
113+
struct[1] = 3; // major legacy version;
114+
struct[2] = 3; // minor legacy verions = TLS v1.2
115+
struct.set(lengthOf,3);
116+
struct.set(encrypted_record, 5);
117+
super(struct)
118+
}
119+
}
120+
75121
//npx -p typescript tsc ./src/contentype.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

type/contentype.d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,47 @@ export class TLSPlaintext extends Uint8Array {
115115
*/
116116
readonly items: [Uint8Array, Uint8Array, Uint8Array];
117117
}
118+
119+
/**
120+
* Represents a TLSInnerPlaintext structure in the TLS 1.3 protocol.
121+
* This class extends `Uint8Array` to encapsulate TLSInnerPlaintext data.
122+
*/
123+
export class TLSInnerPlaintext extends Uint8Array {
124+
/**
125+
* Creates a TLSInnerPlaintext instance from a given array.
126+
*
127+
* @param {Uint8Array } array - The input array to parse.
128+
* @returns {TLSInnerPlaintext} A new instance of TLSInnerPlaintext.
129+
*/
130+
static from(array: Uint8Array): TLSInnerPlaintext;
131+
132+
/**
133+
* Constructs a TLSInnerPlaintext structure.
134+
*
135+
* @param {Uint8Array} content - The content bytes.
136+
* @param {ContentType} type - The content type as a `ContentType` instance.
137+
* @param {number} numZeros - The number of zero bytes to pad.
138+
*/
139+
constructor(content: Uint8Array, type: ContentType, numZeros: number);
140+
}
141+
142+
/**
143+
* Represents a TLSCiphertext structure in the TLS 1.3 protocol.
144+
* This class extends `Uint8Array` to encapsulate TLSCiphertext data.
145+
*/
146+
export class TLSCiphertext extends Uint8Array {
147+
/**
148+
* Creates a TLSCiphertext instance from a given array.
149+
*
150+
* @param {Uint8Array } array - The input array to parse.
151+
* @returns {TLSCiphertext} A new instance of TLSCiphertext.
152+
*/
153+
static from(array: Uint8Array): TLSCiphertext;
154+
155+
/**
156+
* Constructs a TLSCiphertext structure.
157+
*
158+
* @param {Uint8Array} encrypted_record - The encrypted record bytes.
159+
*/
160+
constructor(encrypted_record: Uint8Array);
161+
}

0 commit comments

Comments
 (0)