@@ -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
4856export 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
0 commit comments