Skip to content

Commit

Permalink
File Organization
Browse files Browse the repository at this point in the history
New simplified crypto middlware in progress
DO NOT RUN DEMO
  • Loading branch information
Universal Web committed Aug 23, 2023
1 parent f37cf14 commit 3688b4b
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 172 deletions.
21 changes: 13 additions & 8 deletions udsp/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { construct, UniqID, each } from '@universalweb/acid';
import { cryptography } from '#udsp/crypto/cryptography';
import dgram from 'dgram';
import { randomConnectionId, randomBuffer } from '#crypto';
import { UWCrypto } from './crypto/availableCryptography.js';
import { UWCrypto } from './cryptoMiddleware/index.js';
export class UDSP {
async calculatePacketOverhead() {
const {
Expand All @@ -11,8 +11,13 @@ export class UDSP {
maxDataSize,
maxHeadSize,
maxPathSize,
maxParametersSize
maxParametersSize,
cipherSuite
} = this;
const encryptOverhead = cipherSuite?.encrypt?.overhead;
if (encryptOverhead) {
this.encryptOverhead = encryptOverhead;
}
if (maxPayloadSize) {
if (!maxDataSize) {
this.maxDataSize = maxPayloadSize;
Expand All @@ -28,7 +33,7 @@ export class UDSP {
}
} else {
const packetInitialOverhead = 2;
this.encryptPacketOverhead = this.cryptography.encryptOverhead;
this.encryptPacketOverhead = this.encryptOverhead;
this.packetOverhead = packetInitialOverhead + this.encryptPacketOverhead + this.connectionIdSize;
this.maxPayloadSize = this.maxPacketSize - this.packetOverhead;
this.maxPayloadSizeSafeEstimate = this.maxPayloadSize - 10;
Expand Down Expand Up @@ -90,12 +95,12 @@ export class UDSP {
*/
puzzleFlag = false;
/*
* IPv6 preferred.
* IPv6 enforced
*/
ipVersion = 'udp6';
events = construct(Map);
streamIdGenerator = construct(UniqID);
defaultExtension = 'js';
defaultExtension = 'html';
packetCount = 0;
dataPacketCount = 0;
headPacketCount = 0;
Expand All @@ -104,7 +109,7 @@ export class UDSP {
throttle = false;
debounce = false;
randomId = randomBuffer(8);
cipherSuite = 'x25519-xchacha20-poly1305';
cipherSuites = ['x25519-xchacha20-poly1305'];
ciphers = {};
cipherSuiteName = 'x25519-xchacha20-poly1305';
cipherSuiteNames = ['x25519-xchacha20-poly1305'];
cipherSuites = {};
}
32 changes: 17 additions & 15 deletions udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { watch } from '#watch';
// Client specific imports to extend class
import { emit } from '../requestMethods/emit.js';
import { request } from '#udsp/requestMethods/request';
import { cryptography } from '#udsp/crypto/cryptography';
import { processMessage } from './processMessage.js';
import { onPacket } from './onPacket.js';
import { onListening } from './listening.js';
Expand All @@ -47,6 +46,7 @@ import { fetchRequest } from '../requestMethods/fetch.js';
import { UDSP } from '#udsp/base';
import { sendPacket } from '../sendPacket.js';
import { post } from '../requestMethods/post.js';
import { getAlgorithm } from '../cryptoMiddleware/index.js';
// UNIVERSAL WEB Client Class
export class Client extends UDSP {
constructor(configuration) {
Expand Down Expand Up @@ -113,28 +113,30 @@ export class Client extends UDSP {
const {
encryptClientConnectionId,
encryptServerConnectionId,
encryptConnectionId
encryptConnectionId,
publicKeyAlgorithm,
} = destination;
if (!has(destination.cipherSuites, this.cipherSuite)) {
if (!has(destination.cipherSuites, this.cipherSuiteName)) {
console.log('Default ciphersuite not available');
this.cipherSuite = intersection(this.cipherSuites, destination.cipherSuites)[0];
if (!this.cipherSuite) {
this.cipherSuiteName = intersection(this.cipherSuites, destination.cipherSuites)[0];
if (!this.cipherSuiteName) {
console.log('No matching cipher suite found.');
return false;
}
}
this.cryptography = await cryptography(this.cipherSuite, destination);
this.publicKeyCryptography = getAlgorithm(publicKeyAlgorithm);
this.cipherSuite = getAlgorithm(this.cipherSuiteNam);
this.compression = destination.compression;
this.headerCompression = destination.headerCompression;
if (destination.autoLogin && this.autoLogin) {
this.autoLogin = true;
}
if (!this.keypair) {
this.keypair = this.cryptography.keypair();
this.keypair = this.publicKeyCryptography.keypair();
success(`Created Connection Keypair`);
}
if (!this.encryptKeypair) {
this.encryptKeypair = this.keypair;
if (!this.encryptionKeypair) {
this.encryptionKeypair = this.keypair;
}
if (encryptClientConnectionId || encryptServerConnectionId || encryptConnectionId) {
this.connectionIdKeypair = this.keypair;
Expand Down Expand Up @@ -209,7 +211,7 @@ export class Client extends UDSP {
}
}
async setSessionKeys(generatedKeys) {
this.sessionKeys = generatedKeys || this.cryptography.clientSessionKeys(this.encryptKeypair, this.destination.encryptKeypair);
this.sessionKeys = generatedKeys || this.publicKeyCryptography.clientSessionKeys(this.encryptionKeypair, this.destination.encryptionKeypair);
if (this.sessionKeys) {
success(`Created Shared Keys`);
success(`receiveKey: ${toBase64(this.sessionKeys.receiveKey)}`);
Expand All @@ -218,7 +220,7 @@ export class Client extends UDSP {
}
async setNewDestinationKeys() {
if (!(this.handshakeSet)) {
this.destination.encryptKeypair = {
this.destination.encryptionKeypair = {
publicKey: this.newKeypair
};
await this.setSessionKeys();
Expand Down Expand Up @@ -257,14 +259,14 @@ export class Client extends UDSP {
this.handshakeCompleted(message);
}
setPublicKeyHeader(header = {}) {
const key = this.encryptKeypair.publicKey;
const key = this.encryptionKeypair.publicKey;
console.log('Setting Public Key in UDSP Header', toBase64(key));
const { encryptClientKey } = this.cryptography.config;
const { encryptClientKey } = this.certificate;
header.key = key;
if (this.destination.encryptKeypair) {
if (this.destination.encryptionKeypair) {
if (isString(encryptClientKey)) {
console.log('Encrypting Public Key in UDSP Header');
header.key = cryptography.encryptClientKey(header.key, this.destination.encryptKeypair);
header.key = this.cipherSuite.boxSeal(header.key, this.destination.encryptionKeypair);
}
}
return header;
Expand Down
2 changes: 1 addition & 1 deletion udsp/client/onPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
} from '#logs';
import { decode } from 'msgpackr';
import { decrypt, createSessionKey } from '#crypto';
import { decodePacket, decodePacketHeaders } from '#udsp/decodePacket';
import { decodePacket, decodePacketHeaders } from '#udsp/encoding/decodePacket';
import { processMessage } from './processMessage.js';
import { hasValue } from '@universalweb/acid';
export async function onPacket(packet) {
Expand Down
72 changes: 0 additions & 72 deletions udsp/crypto/cryptography.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,9 @@ const x25519XChaChaPoly1305Algo = {
nonceBox,
encryptKeypair,
createSessionKey,
clientSessionKeys,
serverSessionKeys,
keypair,
decrypt,
encrypt,
id: 0,
weight: 9,
signature: 4,
aead: 5,
};
const ed25519Algo = {
signKeypair,
Expand All @@ -64,39 +58,58 @@ const ed25519Algo = {
signKeypairToEncryptKeypair,
getSignPublicKeyFromPrivateKey,
safeMath: RistrettoPoint,
clientSessionKeys,
serverSessionKeys,
};
const xsalsa20Algo = {
boxSeal,
boxUnseal
};
export class UWCrypto {
construct(cipherSuite = 'x25519-xchacha20-poly1305', config) {
const {
version = this.version,
publicKeyAlgorithm = 'ed25519',
publicKeyEncryption,
} = config;
if (publicKeyAlgorithm) {
assign(this, this.algorithms[version][publicKeyAlgorithm]);
}
if (cipherSuite) {
assign(this, this.algorithms[version][cipherSuite]);
}
if (publicKeyEncryption) {
assign(this, this.algorithms[version][publicKeyEncryption]);
export const algorithms = {
x25519XChaChaPoly1305: x25519XChaChaPoly1305Algo,
version: {
1: {
0: x25519XChaChaPoly1305Algo,
default: x25519XChaChaPoly1305Algo,
'x25519-xchacha20-poly1305': x25519XChaChaPoly1305Algo,
ed25519: ed25519Algo,
xsalsa20: xsalsa20Algo
}
}
version = 1;
algorithms = {
x25519XChaChaPoly1305: x25519XChaChaPoly1305Algo,
version: {
1: {
0: null,
default: null,
'x25519-xchacha20-poly1305': x25519XChaChaPoly1305Algo,
ed25519: ed25519Algo,
xsalsa20: xsalsa20Algo
};
const currentVersion = 1;
export function getAlgorithm(cipherSuite, version) {
if (!cipherSuite) {
return false;
}
const algo = algorithms[version || currentVersion][cipherSuite];
if (algo) {
return algo;
}
}
export function processPublicKey(source) {
const {
publicKeyAlgorithm,
publicKeyCryptography,
encryptionKeypair,
keypair: {
privateKey,
publicKey
},
} = source;
console.log('keypairType', publicKeyAlgorithm);
if (!encryptionKeypair && publicKeyAlgorithm === 'ed25519') {
if (publicKeyAlgorithm === 'ed25519') {
if (privateKey) {
return publicKeyCryptography.signKeypairToEncryptionKeypair({
publicKey,
privateKey
});
} else {
return publicKeyCryptography.signKeypairToEncryptionKeypair({
publicKey
});
}
}
};
}
}
8 changes: 4 additions & 4 deletions udsp/decodePacket.js → udsp/encoding/decodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
assign, isBuffer, isArray, isTrue, isUndefined, isString, hasValue
} from '@universalweb/acid';
import { toBase64 } from '#crypto';
import { createClient } from './server/clients/index.js';
import { createClient } from '../server/clients/index.js';
export async function decodePacketHeaders(config) {
const {
source,
destination,
packet: packetEncoded
} = config;
const {
encryptKeypair,
encryptionKeypair,
connectionIdKeypair,
cryptography,
state,
Expand Down Expand Up @@ -71,11 +71,11 @@ export async function decodePacketHeaders(config) {
if (header.key) {
success(`Public Key is given -> Processing as create client`);
const { encryptClientKey } = cryptography.config;
if (encryptKeypair) {
if (encryptionKeypair) {
if (isString(encryptClientKey)) {
console.log('Decrypting Public Key in UDSP Header');
const { key } = header;
header.key = cryptography.decryptClientKey(key, encryptKeypair);
header.key = cryptography.decryptClientKey(key, encryptionKeypair);
if (!header.key) {
return failed('Client Key Decode Failed', toBase64(key));
}
Expand Down
Loading

0 comments on commit 3688b4b

Please sign in to comment.