Skip to content

Commit

Permalink
Handshake patches
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal Web committed Jul 18, 2023
1 parent 31cbe4c commit c4d6f8c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 58 deletions.
17 changes: 0 additions & 17 deletions udsp/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,6 @@ export class UDSP {
socket.close();
});
}
proccessProtocolPacket(message) {
const {
intro,
serverIntro,
confirmClientReKey,
handshake
} = message;
if (intro) {
this.sendServerIntro(message);
} else if (serverIntro) {
this.serverIntro(message);
} else if (confirmClientReKey) {
this.confirmClientReKey(message);
} else if (handshake) {
this.endHandshake(message);
}
}
gracePeriod = 30000;
maxPacketSize = 1328;
connectionIdSize = 8;
Expand Down
23 changes: 19 additions & 4 deletions udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { request } from '#udsp/request';
import { cryptography } from '#udsp/cryptography';
import { processMessage } from './processMessage.js';
import { onPacket } from './onPacket.js';
import { intro } from './intro.js';
import { onListening } from './listening.js';
import { keychainGet } from '#keychain';
import { Ask } from '../request/ask.js';
Expand Down Expand Up @@ -197,7 +196,7 @@ export class Client extends UDSP {
this.send(message, header);
}
serverIntro(message) {
console.log('Got server Intro');
console.log('Got server Intro', message);
this.state = 1;
const {
scid: serverConnectionId,
Expand All @@ -208,13 +207,12 @@ export class Client extends UDSP {
this.destination.encryptKeypair = {
publicKey: reKey
};
this.destination.sessionKeys = this.cryptography.clientSessionKeys(this.encryptKeypair, this.destination.encryptKeypair);
this.destination.sessionKeys = this.cryptography.clientSessionKeys(this.encryptKeypair, reKey);
this.confirmReKey();
}
confirmReKey() {
console.log('Sending rekey confirmation');
const header = {};
this.setPublicKeyHeader(header);
const message = {
confirmClientReKey: true
};
Expand Down Expand Up @@ -259,6 +257,23 @@ export class Client extends UDSP {
console.log(`client.send to Server`, this.destination.port, this.destination.ip);
return sendPacket(message, this, this.socket, this.destination, headers, footer);
}
proccessProtocolPacket(message) {
const {
intro,
serverIntro,
confirmClientReKey,
handshake
} = message;
if (intro) {
this.sendServerIntro(message);
} else if (serverIntro) {
this.serverIntro(message);
} else if (confirmClientReKey) {
this.confirmClientReKey(message);
} else if (handshake) {
this.endHandshake(message);
}
}
request = request;
fetch = fetchRequest;
processMessage = processMessage;
Expand Down
44 changes: 13 additions & 31 deletions udsp/decodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,29 @@ export async function decodePacketHeaders(config) {
return failed(`No header -> Invalid Packet`);
}
// Add single header support which holds only the binary data of the packet.id
const header = decode(headerEncoded);
if (!header) {
const headerDecoded = decode(headerEncoded);
if (!headerDecoded) {
return failed(`No header -> Invalid Packet`);
}
let headerIdEncoded;
const isHeadersBuffer = isBuffer(header);
if (isHeadersBuffer) {
headerIdEncoded = header;
info('Headers are in single header format');
} else {
headerIdEncoded = header.id;
info(`header.id: ${toBase64(header.id)}`);
}
if (!headerIdEncoded) {
const header = isBuffer(headerDecoded) ? {
id: headerDecoded
} : headerDecoded;
if (!header.id) {
return failed(`No connection id in header -> Invalid Packet`);
}
let headerId;
if (encryptConnectionId) {
success('Server Connection ID Decrypted');
// console.log(destination);
if (encryptConnectionId === 'sealedbox') {
if (isServerEnd) {
headerId = cryptography.decryptServerConnectionId(headerIdEncoded, connectionIdKeypair);
} else {
headerId = cryptography.decryptClientConnectionId(headerIdEncoded, connectionIdKeypair);
}
if (isServerEnd) {
header.id = cryptography.decryptServerConnectionId(header.id, connectionIdKeypair);
} else {
header.id = cryptography.decryptClientConnectionId(header.id, connectionIdKeypair);
}
if (!headerId) {
if (!header.id) {
return failed(`Packet ID Decrypt Failed method given:${encryptConnectionId}`);
}
info(`clientId: ${toBase64(headerId)}`);
if (isHeadersBuffer) {
config.packetDecoded = {
header: headerId
};
return true;
} else {
header.id = headerId;
}
} else if (!header?.id && !header) {
return failed(`No ID -> Invalid Packet`);
}
info(`clientId: ${toBase64(header.id)}`);
if (header.key) {
success(`Public Key is given -> Processing as create client`);
const { encryptClientKey } = cryptography.config;
Expand Down Expand Up @@ -129,6 +110,7 @@ export async function decodePacket(config) {
return true;
}
const ad = (footer) ? Buffer.concat([packet[0], packet[2]]) : packet[0];
console.log(destination);
info(`Transmit Key ${toBase64(destination.sessionKeys.receiveKey)}`);
if (messageEncoded) {
info(`encryptedMessage ${messageEncoded.length} bytes`);
Expand Down
2 changes: 1 addition & 1 deletion udsp/encodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function encodePacket(message, source, destination, headers, footer
header = id;
}
console.log(message, headers, header);
info(`clientId: ${toBase64(header.id)}`);
info(`clientId: ${toBase64(id)}`);
info(`Transmit Key ${toBase64(source.sessionKeys.transmitKey)}`);
message.t = Date.now();
const headerEncoded = encode(header);
Expand Down
19 changes: 18 additions & 1 deletion udsp/server/clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Client {
this.generateNewSessionKeys();
const message = {
serverIntro: true,
scid: this.client().id,
scid: this.id,
reKey: this.newKeys.publicKey
};
this.send(message);
Expand All @@ -87,6 +87,23 @@ export class Client {
};
this.send(message);
}
proccessProtocolPacket(message) {
const {
intro,
serverIntro,
confirmClientReKey,
handshake
} = message;
if (intro) {
this.sendServerIntro(message);
} else if (serverIntro) {
this.serverIntro(message);
} else if (confirmClientReKey) {
this.confirmRekey(message);
} else if (handshake) {
this.endHandshake(message);
}
}
description = `Server's client`;
type = 'serverClient';
isServerClient = true;
Expand Down
8 changes: 4 additions & 4 deletions udsp/server/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export async function onPacket(packet, connection) {
if (client) {
config.destination = client;
}
const wasDecoded = await decodePacket(config);
if (!wasDecoded) {
return failed('When decoding the packet but header passed');
}
if (key && !client) {
client = await createClient({
server: thisServer,
Expand All @@ -47,6 +43,10 @@ export async function onPacket(packet, connection) {
// Send error message back to origin or not
return failed('Invalid Client id given', toBase64(id));
}
const wasDecoded = await decodePacket(config);
if (!wasDecoded) {
return failed('When decoding the packet but header passed');
}
const {
header,
message
Expand Down

0 comments on commit c4d6f8c

Please sign in to comment.