Skip to content

Commit

Permalink
New Crypto Middleware in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal Web committed Aug 25, 2023
1 parent 050642d commit 7790e22
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 39 deletions.
Binary file modified profiles/default-Ephemeral.cert
Binary file not shown.
Binary file modified profiles/default-EphemeralPublic.cert
Binary file not shown.
Binary file modified profiles/default-Master.cert
Binary file not shown.
Binary file modified profiles/default-MasterPublic.cert
Binary file not shown.
Binary file modified profiles/default-Profile.cert
Binary file not shown.
Binary file modified services/universal.web-Ephemeral.cert
Binary file not shown.
Binary file modified services/universal.web-EphemeralPublic.cert
Binary file not shown.
Binary file modified services/universal.web-Master.cert
Binary file not shown.
Binary file modified services/universal.web-MasterPublic.cert
Binary file not shown.
Binary file modified services/universal.web-Profile.cert
Binary file not shown.
12 changes: 6 additions & 6 deletions udsp/base.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { construct, UniqID, each } from '@universalweb/acid';
import { cryptography } from '#udsp/crypto/cryptography';
import {
construct, UniqID, each, hasValue
} from '@universalweb/acid';
import dgram from 'dgram';
import { randomConnectionId, randomBuffer } from '#crypto';
import { UWCrypto } from './cryptoMiddleware/index.js';
export class UDSP {
async calculatePacketOverhead() {
const {
Expand All @@ -14,8 +14,8 @@ export class UDSP {
maxParametersSize,
cipherSuite
} = this;
const encryptOverhead = cipherSuite?.encrypt?.overhead;
if (encryptOverhead) {
const encryptOverhead = cipherSuite?.encrypt?.overhead || 0;
if (hasValue(encryptOverhead)) {
this.encryptOverhead = encryptOverhead;
}
if (maxPayloadSize) {
Expand Down Expand Up @@ -111,5 +111,5 @@ export class UDSP {
randomId = randomBuffer(8);
cipherSuiteName = 'x25519-xchacha20-poly1305';
cipherSuiteNames = ['x25519-xchacha20-poly1305'];
cipherSuites = {};
version = 1;
}
19 changes: 13 additions & 6 deletions udsp/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +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';
import { processPublicKey } from '../cryptoMiddleware/index';
import { getAlgorithm, processPublicKey } from '../cryptoMiddleware/index.js';
// UNIVERSAL WEB Client Class
export class Client extends UDSP {
constructor(configuration) {
Expand Down Expand Up @@ -109,9 +108,6 @@ export class Client extends UDSP {
} else if (encryptServerConnectionId) {
this.encryptServerConnectionId = true;
}
if (this.cryptography.connectionIdKeypair) {
this.connectionIdKeypair = this.cryptography.connectionIdKeypair;
}
// console.log('Destination', destination.cryptography);
}
async getKeychainSave(keychain) {
Expand Down Expand Up @@ -149,6 +145,7 @@ export class Client extends UDSP {
}
this.publicKeyCryptography = getAlgorithm(publicKeyAlgorithm);
this.cipherSuite = getAlgorithm(this.cipherSuiteName);
console.log(this.cipherSuiteName);
if (destination.boxCryptography) {
this.boxCryptography = getAlgorithm(destination.boxCryptography);
}
Expand All @@ -158,7 +155,7 @@ export class Client extends UDSP {
this.autoLogin = true;
}
if (!this.keypair) {
this.keypair = this.publicKeyCryptography.keypair();
this.keypair = this.cipherSuite.keypair();
success(`Created Connection Keypair`);
}
if (!this.encryptionKeypair) {
Expand Down Expand Up @@ -297,6 +294,16 @@ export class Client extends UDSP {
}
return header;
}
setCryptographyHeaders(header = {}) {
const key = this.encryptionKeypair.publicKey;
console.log('Setting Cryptography in UDSP Header', toBase64(key));
const {
cipherSuiteName, version
} = this;
header.cs = cipherSuiteName;
header.v = version;
return header;
}
sendIntro() {
console.log('Sending Intro');
this.state = 1;
Expand Down
34 changes: 17 additions & 17 deletions udsp/cryptoMiddleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import { pbkdf2, pbkdf2Async } from '@noble/hashes/pbkdf2';
import { scrypt, scryptAsync } from '@noble/hashes/scrypt';
const { seal } = Object;
import * as defaultCrypto from '#crypto';
import { assign } from '@universalweb/acid';
import { assign, hasValue } from '@universalweb/acid';
const {
encrypt, decrypt, nonceBox, sign, signVerify, createSecretKey,
signKeypair, encryptKeypair, createSessionKey, clientSessionKeys,
serverSessionKeys, signPrivateKeyToEncryptPrivateKey, signPublicKeyToEncryptPublicKey,
signKeypairToEncryptKeypair, getSignPublicKeyFromPrivateKey, keypair,
signKeypairToEncryptionKeypair, getSignPublicKeyFromPrivateKey, keypair,
boxUnseal, boxSeal, randomConnectionId, hashMin: defaultHashMin, hash: defaultHash,
} = defaultCrypto;
const x25519XChaChaPoly1305Algo = {
Expand All @@ -55,7 +55,7 @@ const ed25519Algo = {
signVerify,
signPrivateKeyToEncryptPrivateKey,
signPublicKeyToEncryptPublicKey,
signKeypairToEncryptKeypair,
signKeypairToEncryptionKeypair,
getSignPublicKeyFromPrivateKey,
safeMath: RistrettoPoint,
clientSessionKeys,
Expand All @@ -66,7 +66,9 @@ const xsalsa20Algo = {
boxUnseal
};
export const algorithms = {
x25519XChaChaPoly1305: x25519XChaChaPoly1305Algo,
'x25519-xchacha20-poly1305': x25519XChaChaPoly1305Algo,
ed25519: ed25519Algo,
xsalsa20: xsalsa20Algo,
version: {
1: {
0: x25519XChaChaPoly1305Algo,
Expand All @@ -82,24 +84,22 @@ export function getAlgorithm(cipherSuite, version) {
if (!cipherSuite) {
return false;
}
const algo = algorithms[version || currentVersion][cipherSuite];
if (algo) {
return algo;
if (hasValue(version)) {
return algorithms.version[version || currentVersion][cipherSuite];
} else {
return algorithms[cipherSuite];
}
}
export function processPublicKey(source) {
export function processPublicKey(certificate) {
console.log('keypairType', certificate);
const {
publicKeyAlgorithm,
publicKeyCryptography,
encryptionKeypair,
keypair: {
privateKey,
publicKey
},
} = source;
console.log('keypairType', publicKeyAlgorithm);
publicKeyAlgorithm, encryptionKeypair,
privateKey,
publicKey
} = certificate;
if (!encryptionKeypair && publicKeyAlgorithm === 'ed25519') {
if (publicKeyAlgorithm === 'ed25519') {
const publicKeyCryptography = getAlgorithm(publicKeyAlgorithm);
if (privateKey) {
return publicKeyCryptography.signKeypairToEncryptionKeypair({
publicKey,
Expand Down
1 change: 1 addition & 0 deletions udsp/encoding/decodePacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export async function decodePacket(config) {
info(`Receive Key ${toBase64(destination.sessionKeys.receiveKey)}`);
if (messageEncoded) {
info(`encrypted Message size ${messageEncoded.length}bytes`);
console.log(destination);
const decryptedMessage = cipherSuite.decrypt(packet[1], destination.sessionKeys, ad);
if (!decryptedMessage) {
return failed('Encryption failed');
Expand Down
11 changes: 8 additions & 3 deletions udsp/server/clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ export class Client {
this.publicKeyCryptography = server.publicKeyCryptography;
this.encryptClientConnectionId = server.encryptClientConnectionId;
this.encryptServerConnectionId = server.encryptServerConnectionId;
return this.initialize(config);
}
initialize = initialize;
async calculatePacketOverhead() {
const {
maxPacketSize,
maxDataSize,
maxHeadSize,
maxPathSize,
maxParametersSize,
packetMaxPayloadSafeEstimate
} = server;
} = this.server();
const cipherSuite = this.cipherSuite;
const encryptOverhead = cipherSuite?.encrypt?.overhead || 0;
if (maxPacketSize) {
this.maxPacketSize = maxPacketSize;
}
Expand All @@ -70,7 +76,6 @@ export class Client {
if (packetMaxPayloadSafeEstimate) {
this.packetMaxPayloadSafeEstimate = packetMaxPayloadSafeEstimate;
}
return initialize(config, client);
}
async created() {
const server = this.server();
Expand All @@ -83,7 +88,7 @@ export class Client {
success(`socket EVENT -> connected - ID:${this.idString}`);
}
async generateSessionKeypair() {
const newKeypair = this.cryptography.keypair();
const newKeypair = this.cipherSuite.keypair();
this.newKeypair = newKeypair;
info(`socket EVENT -> reKey - ID:${this.idString}`);
}
Expand Down
4 changes: 3 additions & 1 deletion udsp/server/clients/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@universalweb/acid';
import { Client } from './index.js';
import { getAlgorithm } from '../../cryptoMiddleware/index.js';
export async function initialize(config, client) {
export async function initialize(config) {
const {
packet: {
header: {
Expand All @@ -36,6 +36,7 @@ export async function initialize(config, client) {
address: ip,
port
} = connection;
const client = this;
let selectedCipherSuite = cipherSuite;
if (cipherSuites) {
const cipherSelection = intersection(cipherSuites, keys(server.ciphers));
Expand All @@ -48,6 +49,7 @@ export async function initialize(config, client) {
} else {
client.cipherSuite = getAlgorithm(server.cipherSuite);
}
client.calculatePacketOverhead();
client.certificate = server.certificate;
// When changing to a new key you must first create new keys from scratch to replace these.
client.keypair = server.keypair;
Expand Down
9 changes: 4 additions & 5 deletions udsp/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import { requestMethods } from './methods/index.js';
import { getCertificate, parseCertificate, loadCertificate } from '#certificate';
import { randomBuffer, toBase64 } from '#crypto';
import { UDSP } from '#udsp/base';
import { UWCrypto } from '../cryptoMiddleware/index.js';
import { processPublicKey, getAlgorithm } from '../cryptoMiddleware/index';
import { processPublicKey, getAlgorithm } from '../cryptoMiddleware/index.js';
const { seal } = Object;
export class Server extends UDSP {
constructor(configuration) {
Expand Down Expand Up @@ -106,7 +105,7 @@ export class Server extends UDSP {
this.chunkCertificate();
}
if (this.certificate) {
this.publicKeyCryptography = getAlgorithm(this.certificate.publicKeyAlgorithm);
this.publicKeyCryptography = getAlgorithm(this.certificate.publicKeyAlgorithm, this.certificate.version);
const convertSignKeypairToEncryptionKeypair = processPublicKey(this.certificate);
if (convertSignKeypairToEncryptionKeypair) {
this.encryptionKeypair = convertSignKeypairToEncryptionKeypair;
Expand All @@ -126,8 +125,8 @@ export class Server extends UDSP {
} else if (encryptServerConnectionId) {
this.encryptServerConnectionId = true;
}
if (this.cryptography.connectionIdKeypair) {
this.connectionIdKeypair = this.cryptography.connectionIdKeypair;
if (this.certificate.connectionIdKeypair) {
this.connectionIdKeypair = this.certificate.connectionIdKeypair;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion utilities/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export function signPrivateKeyToEncryptPrivateKey(originalPrivateKey) {
crypto_sign_ed25519_sk_to_curve25519(privateKey, originalPrivateKey);
return privateKey;
}
export function signKeypairToEncryptKeypair(originalKeypair) {
export function signKeypairToEncryptionKeypair(originalKeypair) {
const publicKey = bufferAlloc(crypto_box_PUBLICKEYBYTES);
crypto_sign_ed25519_pk_to_curve25519(publicKey, originalKeypair.publicKey);
const result = {
Expand Down

0 comments on commit 7790e22

Please sign in to comment.