diff --git a/deno.json b/deno.json index 85ae88c..3ba8c05 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@tls/keyexchange", - "version": "0.0.9", + "version": "0.1.0", "exports": "./src/mod.ts", "publish": { "exclude": ["dist/"] diff --git a/src/clienthello.js b/src/clienthello.js index 2b376d3..7d2126f 100644 --- a/src/clienthello.js +++ b/src/clienthello.js @@ -20,7 +20,7 @@ export class ClientHello extends Struct { cipher_suites; // offset = 33; legacy_compression_methods; // offset = 33 + cipher_suites.length extensions; - ext = {}; + ext = new Map; static fromHandShake(handshake) { const copy = Uint8Array.from(handshake); let offset = 0; @@ -65,9 +65,12 @@ export class ClientHello extends Struct { this.legacy_session = legacy_session; this.cipher_suites = cipher_suites; this.legacy_compression_methods = legacy_compression_methods + let offset = legacy_version.length + random.length + legacy_session.length + + cipher_suites.length + legacy_compression_methods.length; this.extensions = extensions; for (const ex of extensions) { - this.ext[ex.extension_type?.name] = ex.extension_data + this.ext.set(ex.extension_type?.name, {pos: offset + 2, data: ex.extension_data}); + offset += ex.length; } } static fromServerName(serverName) { @@ -85,12 +88,17 @@ export class ClientHello extends Struct { )) ) } - toRecord() { + toRecord() { return ContentType.HANDSHAKE.tlsPlainText( HandshakeType.CLIENT_HELLO.handshake(this) - ) } - add(data){ - const array = safeuint8array(this, data); + ) + } + addBinders(binders) { + const psk = this.ext.get('PRE_SHARED_KEY'); + const lengthOf = psk.data.length + binders.length; + const uint16 = Uint16.fromValue(lengthOf) + const array = safeuint8array(this, binders); + array.set(uint16, psk.pos + 2); return ClientHello.from(array) } } diff --git a/src/serverhello.js b/src/serverhello.js index 1f2fe2b..023e464 100644 --- a/src/serverhello.js +++ b/src/serverhello.js @@ -101,8 +101,8 @@ function parseExtension(extension) { function fromClient_hello(clientHello) { const { legacy_session, cipher_suites, ext } = clientHello; const { ciphers } = cipher_suites; - const { KEY_SHARE } = ext; - const { keyShareEntries } = KEY_SHARE + const KEY_SHARE = ext.get('KEY_SHARE'); + const { keyShareEntries } = KEY_SHARE.data; const cipherPreferences = new Set([Cipher.AES_128_GCM_SHA256, Cipher.AES_256_GCM_SHA384, Cipher.CHACHA20_POLY1305_SHA256]); const namedGroupPreferences = new Set([NamedGroup.X25519, NamedGroup.SECP256R1, NamedGroup.SECP384R1]) const cipher = cipherPreferences.intersection(ciphers).values().next().value//selectFirstMatch(ciphers, cipherPreferences); diff --git a/test/clienthello_test.js b/test/clienthello_test.js index cd2b949..336e0c8 100644 --- a/test/clienthello_test.js +++ b/test/clienthello_test.js @@ -89,5 +89,9 @@ const clientHelloPSKBinder = HexaDecimal.fromString( ).byte const clientHelloPSKBinderBack = ClientHello.fromHandShake(clientHelloPSKBinder); +const binders = HexaDecimal.fromString(`00 21 20 3a dd 4f b2 d8 fd f8 22 a0 ca + 3c f7 67 8e f5 e8 8d ae 99 01 41 c5 92 4d 57 bb 6f a3 1b 9e 5f + 9d`).byte; +const clientHelloPsk_0 = clientHelloPSKBinderBack.addBinders(binders) debugger; \ No newline at end of file diff --git a/type/clienthello.d.ts b/type/clienthello.d.ts index 3622eba..49ad0c4 100644 --- a/type/clienthello.d.ts +++ b/type/clienthello.d.ts @@ -81,10 +81,10 @@ export class ClientHello extends Struct { /** * Appends the given data to the current instance and creates a new `ClientHello` object. * - * @param {Uint8Array} data - The data to be appended. Must be a valid `Uint8Array`. + * @param {Uint8Array} binders - The data to be appended. Must be a valid `Uint8Array`. * @returns {ClientHello} A new `ClientHello` instance with the combined data. */ - add(data: Uint8Array): ClientHello; + addBinders(binders: Uint8Array): ClientHello; } /**