Merge branch '2024.9-rc' into staging

This commit is contained in:
yflory 2024-09-30 17:52:19 +02:00
commit a21d281d18
6 changed files with 119 additions and 14 deletions

28
lib/crypto.js Normal file
View File

@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 XWiki CryptPad Team <contact@cryptpad.org> and contributors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
const Nacl = require('tweetnacl/nacl-fast');
const CPCrypto = module.exports;
const plugins = require('./plugin-manager');
CPCrypto.init = (cb) => {
const crypto = {};
crypto.open = (signedMsg, validateKey) => {
return Nacl.sign.open(signedMsg, validateKey);
};
crypto.detachedVerify = (signedBuffer, signatureBuffer, validateKey) => {
return Nacl.sign.detached.verify(signedBuffer, signatureBuffer, validateKey);
};
if (plugins.SODIUM && plugins.SODIUM.crypto) {
let c = plugins.SODIUM.crypto;
if (c.open) { crypto.open = c.open; }
if (c.detachedVerify) { crypto.detachedVerify = c.detachedVerify; }
}
// Make async because we might need it later with libsodium's promise
// libsodium.ready.then(() => {});
setTimeout(() => {
cb(void 0, crypto);
});
};

View File

@ -16,6 +16,7 @@ const Logger = require("../log");
const Tasks = require("../storage/tasks");
const Nacl = require('tweetnacl/nacl-fast');
const Eviction = require("../eviction");
const CPCrypto = require('../crypto');
const Env = {
Log: {},
@ -101,6 +102,10 @@ const init = function (config, _cb) {
}
Env.tasks = tasks;
}));
}).nThen(function (w) {
CPCrypto.init(w(function (err, crypto) {
Env.crypto = crypto;
}));
}).nThen(function () {
cb();
});
@ -680,7 +685,8 @@ COMMANDS.INLINE = function (data, cb) {
return void cb("E_BADKEY");
}
// validate the message
const validated = Nacl.sign.open(signedMsg, validateKey);
//const validated = Nacl.sign.open(signedMsg, validateKey);
const validated = Env.crypto.open(signedMsg, validateKey);
if (!validated) {
return void cb("FAILED");
}
@ -720,7 +726,8 @@ const checkDetachedSignature = function (signedMsg, signature, publicKey) {
throw new Error("INVALID_SIGNATURE_LENGTH");
}
if (Nacl.sign.detached.verify(signedBuffer, signatureBuffer, pubBuffer) !== true) {
//if (Nacl.sign.detached.verify(signedBuffer, signatureBuffer, pubBuffer) !== true) {
if (Env.crypto.detachedVerify(signedBuffer, signatureBuffer, pubBuffer) !== true) {
throw new Error("FAILED");
}
};

65
scripts/testcrypto.js Normal file
View File

@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2024 XWiki CryptPad Team <contact@cryptpad.org> and contributors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
let SodiumNative = require('sodium-native');
let Nacl = require('tweetnacl/nacl-fast');
let LibSodium = require('libsodium-wrappers');
let msgStr = "This is a test";
let keys = Nacl.sign.keyPair();
let pub = keys.publicKey;
let msg = Nacl.util.decodeUTF8(msgStr);
let signedMsg = Nacl.sign(msg, keys.secretKey);
let sig = signedMsg.subarray(0, 64);
LibSodium.ready.then(() => {
/*
console.log('tweetnacl open');
console.log(!!Nacl.sign.open(signedMsg, pub));
console.log('tweetnacl detached');
console.log(Nacl.sign.detached.verify(msg, sig, pub));
console.log('sodium-native open');
console.log(SodiumNative.crypto_sign_open(msg, signedMsg, pub));
console.log('sodium-native detached');
console.log(SodiumNative.crypto_sign_verify_detached(sig, msg, pub));
LibSodium.ready.then(() => {
console.log('libsodium open');
console.log(!!LibSodium.crypto_sign_open(signedMsg, pub));
console.log('libsodium detached');
console.log(LibSodium.crypto_sign_verify_detached(sig, msg, pub));
});
*/
const n = 10000;
let a;
console.log('start sodium-native');
a = +new Date();
for (var i = 0; i < n; i++) {
SodiumNative.crypto_sign_open(msg, signedMsg, pub);
SodiumNative.crypto_sign_verify_detached(sig, msg, pub);
}
console.log('end sodium-native ', (+new Date() - a), ' ms');
console.log('start libsodium');
a = +new Date();
for (var i = 0; i < n; i++) {
LibSodium.crypto_sign_open(signedMsg, pub);
LibSodium.crypto_sign_verify_detached(sig, msg, pub);
}
console.log('end libsodium ', (+new Date() - a), ' ms');
console.log('start tweetnacl');
a = +new Date();
for (var i = 0; i < n; i++) {
Nacl.sign.open(signedMsg, pub);
Nacl.sign.detached.verify(msg, sig, pub);
}
console.log('end tweetnacl ', (+new Date() - a), ' ms');
});

View File

@ -3,15 +3,10 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
define([
'/components/x2js/x2js.js',
'/diagram/util.js',
], function (
X2JS,
DiagramUtil
) {
const x2js = new X2JS();
const jsonContentAsXML = (content) => x2js.js2xml(content);
const parseDrawioStyle = (styleAttrValue) => {
if (!styleAttrValue) {
return;
@ -70,12 +65,12 @@ define([
}
return doc;
};
return {
main: function(userDoc, cb) {
delete userDoc.metadata;
const xml = jsonContentAsXML(userDoc);
const xml = DiagramUtil.jsonContentAsXML(userDoc);
let doc;
try {

View File

@ -95,11 +95,9 @@ define([
drawioFrame.contentWindow.postMessage(JSON.stringify(msg), '*');
};
const jsonContentAsXML = (content) => x2js.js2xml(content);
var onDrawioInit = function() {
drawIoInitalized = true;
var xmlStr = jsonContentAsXML(lastContent);
var xmlStr = DiagramUtil.jsonContentAsXML(lastContent);
postMessageToDrawio({
action: 'load',
xml: xmlStr,
@ -165,7 +163,7 @@ define([
// This is the function from which you will receive updates from CryptPad
framework.onContentUpdate(function (newContent) {
lastContent = newContent;
var xmlStr = jsonContentAsXML(lastContent);
var xmlStr = DiagramUtil.jsonContentAsXML(lastContent);
postMessageToDrawio({
action: 'merge',
xml: xmlStr,

View File

@ -6,12 +6,15 @@ define([
'/common/common-util.js',
'/file/file-crypto.js',
'/common/outer/cache-store.js',
'/components/x2js/x2js.js',
], function (
Util,
FileCrypto,
Cache
Cache,
X2JS,
) {
const Nacl = window.nacl;
const x2js = new X2JS();
const parseCryptPadUrl = function(href) {
const url = new URL(href);
@ -38,9 +41,18 @@ define([
return fixedBlob;
};
const jsonContentAsXML = (content) => {
// Sometimes `content` has additional fiels, that break the XML output. Only grab mxfile here.
const cleaned = { mxfile: content.mxfile };
return x2js.js2xml(cleaned);
};
return {
parseCryptPadUrl,
getCryptPadUrl,
jsonContentAsXML,
loadImage: function(href) {
return new Promise((resolve, reject) => {