Skip to content

Commit

Permalink
Package Broken Reduction in Progress
Browse files Browse the repository at this point in the history
Streamline Packet
Simplify Encode
Simplyfy Decode
Simplify Recieve
Simplify Sending
  • Loading branch information
Universal Web committed May 15, 2023
1 parent 09687b7 commit 83f921d
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 132 deletions.
2 changes: 2 additions & 0 deletions udsp/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class Ask {
to keep track of the state of the request, where `0` represents an unsent request, `1` represents a
request that is currently being sent, and `2` represents a completed request. */
state = 0;
recieve() {
}
callback(response, headers) {
this.accept({
response,
Expand Down
6 changes: 6 additions & 0 deletions udsp/client/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ export async function connect(payload = {}) {
console.log('-------CLIENT CONNECTED-------\n');
return result;
}
// const [headers] = packet;
// if (headers?.key) {
// msgReceived(`New PublicKey received ${headers.key.length}`);
// this.destination.publicKey = headers.key;
// reKey(this.transmitKey, this.receiveKey, keypair.publicKey, keypair.privateKey, headers.key);
// }
2 changes: 1 addition & 1 deletion udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { send } from './send.js';
import { emit } from './emit.js';
import { request } from '#udsp/request';
import { processMessage } from './processMessage.js';
import { onMessage } from './onMessage.js';
import { onMessage } from './onPaacket.js';
import { connect } from './connect.js';
import { onListening } from './listening.js';
import { currentPath } from '#directory';
Expand Down
6 changes: 0 additions & 6 deletions udsp/client/onMessage.js → udsp/client/onPaacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ export async function onMessage(packetEncoded) {
nonce,
packetEncoded
});
const [headers] = packet;
if (headers?.key) {
msgReceived(`New PublicKey received ${headers.key.length}`);
this.destination.publicKey = headers.key;
reKey(this.transmitKey, this.receiveKey, keypair.publicKey, keypair.privateKey, headers.key);
}
this.processMessage(packet);
}

17 changes: 14 additions & 3 deletions udsp/decodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ export async function decodePacket(config) {
} = config;
msgReceived(`Packet Size ${packetEncoded.length}`);
const packet = decode(packetEncoded);
const headers = decode(packet[0]);
const headersEncoded = packet[0];
if (!headersEncoded) {
return failed(`No headers -> Invalid Packet`);
}
const headers = decode(headersEncoded);
if (!headers) {
return failed(`No headersEncrypted -> Invalid Packet`);
return failed(`No headers -> Invalid Packet`);
}
const {
id,
nonce
} = headers;
const footer = packet[2] && decode(packet[2]);
if (packet[2] && !footer) {
return failed(`Footer failed to decode -> Invalid Packet`);
}
const ad = (footer) ? Buffer.concat([packet[0], packet[2]]) : packet[0];
const encryptedMessage = decrypt(packet[1], ad, nonce, receiveKey);
if (!encryptedMessage) {
Expand All @@ -53,5 +60,9 @@ export async function decodePacket(config) {
console.log(packet);
failed(`WARNING: Packet size is larger than max allowed size 1280 -> ${packetSize} over by ${packetSize - 1280}`);
}
return [headers, message, footer];
return {
headers,
message,
footer
};
}
8 changes: 7 additions & 1 deletion udsp/server/clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
success, failed, imported, msgSent, info, msgReceived
} from '#logs';
import { UniqID, construct } from 'Acid';
import { sessionKeys, keypair } from '#crypto';
import { sessionKeys, keypair, toBase64 } from '#crypto';
export class Client {
descriptor = 'client';
client = true;
Expand Down Expand Up @@ -72,3 +72,9 @@ export class Client {
info(`socket EVENT -> destroy - ID:${this.id}`);
}
}
export async function createClient(server, connectionInfo, receiveKey, transmitKey, ephemeralKeypair, clientId) {
console.log('Creating Client Object', toBase64(clientId));
const client = await construct(Client, [server, connectionInfo, receiveKey, transmitKey, ephemeralKeypair, clientId]);
console.log('Client has been created', toBase64(clientId));
return client;
}
48 changes: 46 additions & 2 deletions udsp/server/clients/initialize.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
import { created } from './created.js';
import {
toBase64, emptyNonce, randombytes_buf, keypair
toBase64, emptyNonce, randombytes_buf, keypair, signVerifyHash, decrypt, sessionKeys
} from '#crypto';
import {
success, failed, imported, msgSent, info, msgReceived
} from '#logs';
export async function initialize(client, server, connection, receiveKey, transmitKey, ephemeralKeypair, clientId) {
import { construct } from 'Acid';
import { Client } from './index.js';
export async function initialize(config) {
const {
client,
packet,
server,
connection
} = config;
const {
profile: {
ephemeral: {
private: serverPrivateKey,
key: serverPublicKey
}
},
} = server;
const ephemeralKeypair = packet.key;
const clientId = packet.id;
const nonce = packet.nonce;
success(`Encrypted Message Size: ${packet.length}`);
const sessionKey = sessionKeys(serverPublicKey, serverPrivateKey, ephemeralKeypair);
const receiveKey = sessionKey.receiveKey;
const transmitKey = sessionKey.transmitKey;
info(`receiveKey: ${toBase64(receiveKey)}`);
info(`transmitKey: ${toBase64(transmitKey)}`);
console.log(toBase64(packet));
console.log(toBase64(nonce));
const idc = packet.message.idc;
const sig = packet.message.sig;
if (!idc) {
return failed('No Identity Provided', connection);
}
if (!sig) {
return failed('No Sig Provided', connection);
}
success(`Decrypted`);
const destination = {
publicKey: ephemeralKeypair
};
const sigVerify = signVerifyHash(sig, Buffer.concat([nonce, ephemeralKeypair]), idc.key);
console.log('Concat Sig', Buffer.concat([nonce, ephemeralKeypair]));
console.log('SIGNature Hash', sig);
console.log('Ephemeral Key', ephemeralKeypair);
const {
clients,
configuration: { id: serverIdRaw }
Expand Down Expand Up @@ -71,3 +114,4 @@ export async function initialize(client, server, connection, receiveKey, transmi
await client.created();
return client;
}

14 changes: 0 additions & 14 deletions udsp/server/createClient.js

This file was deleted.

45 changes: 16 additions & 29 deletions udsp/server/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,25 @@ import { boxUnseal, decrypt } from '#crypto';
import { processPacket } from './processPacket.js';
import { processSocket } from './processSocket.js';
import { isEmpty } from 'Acid';
export async function onPacket(packet, connection) {
import { decodePacket } from '#udsp/decodePacket';
export async function onPacket(packetEncoded, connection) {
const thisServer = this;
msgReceived(`Message Received Total packet size: ${packet.length}`);
console.log(packet);
const packetDecoded = decode(packet);
if (isEmpty(packetDecoded)) {
return failed(`No header buffer -> Invalid Packet`);
}
const headersBuffer = packetDecoded[0];
if (!headersBuffer) {
return failed(`No header buffer -> Invalid Packet`);
}
const messageBuffer = packetDecoded[1];
if (!packet) {
return failed(`No packet -> Invalid Packet`);
}
const headers = decode(headersBuffer);
if (!headers) {
return failed(`No headers -> Invalid Packet`);
}
const footer = packetDecoded[2];
if (!footer) {
info(`No footer`);
}
success(`Packet`);
const { key, } = headers;
console.log('Headers', headers);
if (key) {
const {
receiveKey,
nonce,
keypair
} = thisServer;
msgReceived('Message Received');
const packet = await decodePacket({
receiveKey,
nonce,
packetEncoded
});
if (packet.headers.key) {
success(`Public Key is given -> Processing handshake`);
await processSocket(thisServer, connection, headersBuffer, headers, messageBuffer);
await processSocket(thisServer, connection, packet);
} else {
success(`No Public Key is given -> Processing as a message`);
await processPacket(thisServer, connection, headersBuffer, headers, messageBuffer);
await processPacket(thisServer, connection, packet);
}
}
3 changes: 0 additions & 3 deletions udsp/server/processPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export async function processPacket(server, connection, headersBuffer, headers,
return false;
}
const nonce = headers.nonce;
if (headers.reKeyed) {
client.reKey();
}
const decrypted = decrypt(packet, headersBuffer, nonce, client.receiveKey);
if (!decrypted) {
return failed(`Decrypt Failed`);
Expand Down
84 changes: 11 additions & 73 deletions udsp/server/processSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,16 @@ import {
import { createClient } from './createClient.js';
import { processPacketEvent } from './processPacketEvent.js';
// headers (ad) are the main UDSP headers. It may be called headers at times or headers.
export async function processSocket(server, connection, headersBuffer, headers, messageBuffer) {
const {
profile: {
ephemeral: {
private: serverPrivateKey,
key: serverPublicKey
}
},
} = server;
if (headers.key) {
console.log('HEADERS SEALED', headers);
console.log(serverPublicKey);
const unsealedKey = boxUnseal(headers.key, serverPublicKey, serverPrivateKey);
console.log(headers.key, '\n', serverPublicKey, '\n', serverPrivateKey);
if (!unsealedKey) {
return new Error('UNSEALED KEY BROKEN');
}
headers.key = unsealedKey;
}
const ephemeralKeypair = headers.key;
const clientId = headers.id;
const nonce = headers.nonce;
success(`Encrypted Message Size: ${messageBuffer.length}`);
const sessionKey = sessionKeys(serverPublicKey, serverPrivateKey, ephemeralKeypair);
const receiveKey = sessionKey.receiveKey;
const transmitKey = sessionKey.transmitKey;
info(`receiveKey: ${toBase64(receiveKey)}`);
info(`transmitKey: ${toBase64(transmitKey)}`);
console.log(toBase64(messageBuffer));
console.log(toBase64(nonce));
console.log(headersBuffer.length);
const decrypted = decrypt(messageBuffer, headersBuffer, nonce, receiveKey);
if (!decrypted) {
return failed(`Decrypt Failed`);
}
success(`Decrypted`);
if (decrypted) {
const message = decode(decrypted);
if (!message) {
return failed('MSGPACK ERROR', connection);
}
console.log(message);
if (!message.idc) {
return failed('No Identity Provided', connection);
}
if (!message.sig) {
return failed('No Sig Provided', connection);
}
const {
idc,
sig
} = message;
server.socketCount++;
console.log(message);
success(`Encrypted Message Signature: ${toBase64(sig)}`);
success(`Encrypted Message Signature Size: ${sig.length}`);
const destination = {
publicKey: ephemeralKeypair
};
const sigVerify = signVerifyHash(sig, Buffer.concat([nonce, ephemeralKeypair]), idc.key);
console.log('Concat Sig', Buffer.concat([nonce, ephemeralKeypair]));
console.log('SIGNature Hash', sig);
console.log('Ephemeral Key', ephemeralKeypair);
if (sigVerify) {
msgReceived(`Signature is valid`);
const client = await createClient(server, connection, receiveKey, transmitKey, destination, clientId);
console.log(client);
await processPacketEvent(server, client, message);
} else {
console.log('SIGNATURE FAILED NO SOCKET CREATED', sigVerify);
return;
}
success(`Socket Count: ${server.socketCount}`);
export async function processSocket(server, connection, packet) {
server.socketCount++;
if (packet.headers.key) {
msgReceived(`Signature is valid`);
const client = await createClient(server, connection);
console.log(client);
await processPacketEvent(server, client, packet.message);
} else {
console.log('NO SOCKET CREATED NO public key', packet);
return;
}
success(`Socket Count: ${server.socketCount}`);
}

0 comments on commit 83f921d

Please sign in to comment.