Skip to content

Commit

Permalink
Converting to new advanced keypair system 90%
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal Web committed May 29, 2023
1 parent 8ebabce commit 57abfb4
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 130 deletions.
6 changes: 2 additions & 4 deletions scripts/simulateClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import { decode } from 'msgpackr';
console.time('Connected');
// Universal Web Socket
const uwClient = await client({
service: `${currentPath(import.meta)}/../services/universal.web-Ephemeral.cert`,
profile: `${currentPath(import.meta)}/../profiles/default.cert`,
ip: '::1',
port: 8888
service: `${currentPath(import.meta)}/../services/universal.web-EphemeralPublic.cert`,
profile: `${currentPath(import.meta)}/../profiles/default-Ephemeral.cert`,
});
console.timeEnd('Connected');
console.log('INTRO =>', uwClient);
Expand Down
4 changes: 2 additions & 2 deletions serverApp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const appServer = await createServer({
// default file extension default is .js but WWW default is www
defaultExtension: 'html',
// Domain certificate to be loaded used for connection encryption
certificate: `${currentPath(import.meta)}../services/universal.web-Profile.cert`,
certificate: `${currentPath(import.meta)}../services/universal.web-Ephemeral.cert`,
// Where to load app resources from
resourceDirectory: `${currentPath(import.meta)}resources/`,
// Server ID used for load balancing and attaching to the end of connection IDs
Expand All @@ -42,4 +42,4 @@ const appServer = await createServer({
// port: 8888,
// ip: '::1'
});
info('App Server Status', appServer);
// info('App Server Status', appServer);
79 changes: 22 additions & 57 deletions udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import dgram from 'dgram';
// Default utility imports
import { success, configure, info } from '#logs';
import {
createSessionKey, clientSession, keypair, toBase64, emptyNonce, sessionKeys,
createConnectionIdKey, encodeConnectionId, randomConnectionId
createSessionKey, clientSession, keypair, toBase64, emptyNonce, sessionKeys, randomConnectionId, ed25519ToCurve25519PublicKey
} from '#crypto';
import { pluckBuffer } from '#pluckBuffer';
import { getCertificate } from '#certificate';
Expand All @@ -37,7 +36,6 @@ import { onMessage } from './onPacket.js';
import { connect } from './connect.js';
import { onListening } from './listening.js';
import { currentPath } from '#directory';
import { encrypt } from '../../utilities/crypto';
// UNIVERSAL WEB Client Class
export class Client {
constructor(configuration) {
Expand All @@ -48,70 +46,50 @@ export class Client {
service,
profile,
ip: configIP,
port: configPort,
encryptConnectionId
port: configPort
} = configuration;
const {
ip,
port
} = service.ephemeral;
configure('CLIENT CONFIGURATION');
} = service;
configure('CLIENT CONFIGURATION', service);
assign(this, {
ip: configIP || ip,
port: configPort || port,
service,
profile,
});
thisClient.keypair = keypair();
thisClient.destinationPublicKey = service.publicKey;
thisClient.destinationBoxPublicKey = ed25519ToCurve25519PublicKey(service.publicKey);
const {
publicKey,
privateKey,
} = thisClient.keypair;
const clientSessionKeys = sessionKeys(publicKey, privateKey, thisClient.destinationPublicKey);
const {
transmitKey,
receiveKey
} = clientSessionKeys;
thisClient.transmitKey = transmitKey;
thisClient.receiveKey = receiveKey;
configure(`Shared Keys Created`);
this.connect = connect.bind(this);
this.send = send.bind(this);
this.request = request.bind(this);
this.processMessage = processMessage.bind(this);
this.emit = emit.bind(this);
this.onListening = onListening.bind(this);
this.onMessage = onMessage.bind(this);
thisClient.baseId = randomConnectionId();
thisClient.id = encodeConnectionId(thisClient.baseId, thisClient.connectionIdKey);
thisClient.idString = toBase64(thisClient.baseId);
thisClient.id = randomConnectionId();
thisClient.idString = toBase64(thisClient.id);
thisClient.clientId = thisClient.id;
success(`clientId:`, toBase64(this.id));
success(`Creating Shared Keys`);
const transmitKey = thisClient.transmitKey = createSessionKey();
const receiveKey = thisClient.receiveKey = createSessionKey();
// Currently unused but may in the future
const ephemeralProfileTransmitKey = thisClient.ephemeralProfileTransmitKey = createSessionKey();
const ephemeralProfileReceiveKey = thisClient.ephemeralProfileReceiveKey = createSessionKey();
success(`Creating Connection Keypair`);
thisClient.keypair = keypair();
if (encryptConnectionId) {
if (service.encryptConnectionId) {
thisClient.connectionIdKeypair = thisClient.keypair;
}
thisClient.keypair = keypair();
thisClient.ephemeralPublic = omit(profile.ephemeral, ['private']);
if (profile.master) {
thisClient.masterPublic = omit(profile.master, ['private']);
}
const { ephemeral: { signature: profileSignature } } = profile;
const {
ephemeral: {
key: destinationPublicKey,
signature: destinationSignature
}
} = service;
thisClient.destination = {
publicKey: destinationPublicKey,
};
const {
publicKey,
privateKey,
} = thisClient.keypair;
clientSession(receiveKey, transmitKey, publicKey, privateKey, destinationPublicKey);
configure(`Shared Keys Created`);
console.log(receiveKey, transmitKey);
const serviceKey = toBase64(destinationSignature);
const profileKey = toBase64(profileSignature);
success(`serviceKey:`, serviceKey);
success(`profileKey:`, profileKey);
// Needs to be more complex if forcing no connection with the same credentials
Client.connections.set(thisClient.idString, thisClient);
thisClient.server.on('message', thisClient.onMessage.bind(thisClient));
thisClient.server.on('listening', thisClient.onListening);
Expand Down Expand Up @@ -149,15 +127,6 @@ export class Client {
}
packetIdGenerator = construct(UniqID);
}
export function getClient(configuration) {
const serviceKey = configuration.service.ephemeral.signature.toString('base64');
const profileKey = configuration.profile.ephemeral.signature.toString('base64');
const connectionId = `${serviceKey}${profileKey}`;
const clientFound = Client.connections.get(connectionId);
if (clientFound) {
return clientFound;
}
}
export async function createClient(configuration, ignoreConnections) {
console.log(configuration);
return construct(Client, [configuration]);
Expand All @@ -169,10 +138,6 @@ export async function client(configuration, ignoreConnections) {
if (isString(configuration.profile)) {
configuration.profile = await getCertificate(configuration.profile);
}
const result = getClient(configuration, Client);
if (result) {
return result;
}
const uwClient = await createClient(configuration);
console.time('CONNECTING');
const connectRequest = await uwClient.connect();
Expand Down
16 changes: 9 additions & 7 deletions udsp/client/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export async function send(config) {
info(`Send to server`);
const client = this;
const {
destination,
ephemeralPublic,
id,
ip,
keypair,
Expand All @@ -26,12 +24,13 @@ export async function send(config) {
serverId,
state,
transmitKey,
connectionIdKey
connectionIdKeypair,
service,
service: { encryptConnectionId },
destinationBoxPublicKey
} = client;
const packet = await encodePacket({
client,
destination,
ephemeralPublic,
footer,
headers,
id: serverId || id,
Expand All @@ -43,9 +42,12 @@ export async function send(config) {
profile,
state,
transmitKey,
connectionIdKey
destination: service,
connectionIdKeypair,
encryptConnectionId,
destinationBoxPublicKey
});
msgSent(`Packet Size ${packet.length}`, message);
msgSent(`Packet Size ${packet.length}`, message, port, ip);
return promise((accept, reject) => {
server.send(packet, port, ip, (error) => {
if (error) {
Expand Down
5 changes: 2 additions & 3 deletions udsp/decodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
import { decode, } from 'msgpackr';
import { assign, } from 'Acid';
import {
encrypt, nonceBox, toBase64, hashSign, decrypt, boxUnseal, sessionKeys
encrypt, nonceBox, toBase64, decrypt, boxUnseal, sessionKeys
} from '#crypto';
import { createClient } from './server/clients/index.js';
export function decodePacketHeaders(config) {
Expand All @@ -15,7 +15,6 @@ export function decodePacketHeaders(config) {
options,
packetEncoded,
server,
source,
state,
connectionIdKeypair,
keypair,
Expand All @@ -34,6 +33,7 @@ export function decodePacketHeaders(config) {
info(`clientId: ${toBase64(headers.id)}`);
if (headers.key) {
success(`Public Key is given -> Processing as create client`);
console.log(keypair);
const publicKey = boxUnseal(headers.key, keypair.publicKey, keypair.privateKey);
if (!publicKey) {
return failed(publicKey, 'Client Key Decrypt Failed');
Expand Down Expand Up @@ -66,7 +66,6 @@ export async function decodePacket(config, result) {
options,
packetEncoded,
server,
source,
state,
headers,
client,
Expand Down
26 changes: 16 additions & 10 deletions udsp/encodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import {
nonceBox,
randomize,
toBase64,
hashSignDetached,
signDetached,
boxSeal,
boxUnseal,
crypto_box_keypair,
crypto_box_PUBLICKEYBYTES,
crypto_box_SECRETKEYBYTES
} from '#crypto';
export async function encodePacket(data) {
const {
Expand All @@ -35,13 +32,19 @@ export async function encodePacket(data) {
client,
keypair,
profile,
ephemeralPublic,
destination,
isClient
connectionIdKeypair,
encryptConnectionId,
isClient,
destinationBoxPublicKey
} = data;
const nonce = randomize(nonceBuffer);
if (id) {
headers.id = boxSeal(id, destination.publicKey);
if (encryptConnectionId) {
headers.id = boxSeal(id, destination.publicKey);
} else {
headers.id = id;
}
} else {
return console.error(`ID IS'T ASSIGNED`);
}
Expand All @@ -51,9 +54,12 @@ export async function encodePacket(data) {
if (state === 0) {
console.log('DESTINATION PUBLIC KEY', destination.publicKey);
headers.key = boxSeal(keypair.publicKey, destination.publicKey);
const profileKeypairSignature = hashSignDetached(Buffer.concat([nonce, keypair.publicKey]), profile.ephemeral.private);
const timeBuffer = Buffer.from(message.t.toString());
// This can be seperated out as an authentication request to the service or it can be done here
const authenticationBuffer = Buffer.concat([timeBuffer, destination.publicKey, keypair.publicKey]);
const profileKeypairSignature = signDetached(authenticationBuffer, profile.privateKey);
message.sig = profileKeypairSignature;
message.idc = ephemeralPublic;
message.idc = profile.publicKey;
console.log(`Sig Size:${message.sig.length}`);
console.log(`Setting ephemeral random public key to header & profile cert to message.body`);
}
Expand All @@ -77,7 +83,7 @@ export async function encodePacket(data) {
info(`Transmit Key ${toBase64(transmitKey)}`);
info(`Nonce Size: ${headers.nonce.length} ${toBase64(headers.nonce)}`);
const packetSize = packet.length;
info(`encode Packet Size ${packetSize}`);
info(`encoded Packet Size ${packetSize}`);
if (packetSize >= 1280) {
console.log(packet);
failed(`WARNING: Packet size is larger than max allowed size 1280 -> ${packetSize} over by ${packetSize - 1280}`);
Expand Down
3 changes: 1 addition & 2 deletions udsp/server/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export async function bindServer() {
port,
ip,
} = this;
info(`BIND SERVER`);
console.log(this);
info(`BIND SERVER`, ip, port);
await promise((accept) => {
server.bind(port, ip, accept);
info(`SERVER BOUND: IP:${ip} - PORT:${port}`);
Expand Down
2 changes: 0 additions & 2 deletions udsp/server/chunkMessage.js

This file was deleted.

4 changes: 1 addition & 3 deletions udsp/server/clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
success, failed, imported, msgSent, info, msgReceived
} from '#logs';
import { UniqID, construct, assign } from 'Acid';
import {
sessionKeys, keypair, toBase64, signVerifyHash
} from '#crypto';
import { sessionKeys, keypair, toBase64 } from '#crypto';
export class Client {
descriptor = 'client';
client = true;
Expand Down
7 changes: 3 additions & 4 deletions udsp/server/clients/initialize.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { created } from './created.js';
import {
decrypt, emptyNonce, keypair, randombytes_buf,
sessionKeys, signVerifyHash, toBase64, boxUnseal,
encodeConnectionId, randomConnectionId,
getConnectionId,
decrypt, emptyNonce, keypair,
sessionKeys, toBase64, boxUnseal,
randomConnectionId,
randomBuffer
} from '#crypto';
import {
Expand Down
18 changes: 0 additions & 18 deletions udsp/server/configure.js

This file was deleted.

Loading

0 comments on commit 57abfb4

Please sign in to comment.