Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Node encryption of ArrayBuffer plaintext #1280

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/platform/nodejs/lib/util/bufferutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class BufferUtils implements IBufferUtils {
}

toArrayBuffer(buffer: Bufferlike): ArrayBuffer {
return this.toBuffer(buffer).buffer;
const nodeBuffer = this.toBuffer(buffer);
return nodeBuffer.buffer.slice(nodeBuffer.byteOffset, nodeBuffer.byteOffset + nodeBuffer.byteLength);
}

toBuffer(buffer: Bufferlike): Buffer {
Expand Down
7 changes: 5 additions & 2 deletions src/platform/nodejs/lib/util/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,13 @@ var Crypto = (function () {

CBCCipher.prototype.encrypt = function (plaintext, callback) {
Logger.logAction(Logger.LOG_MICRO, 'CBCCipher.encrypt()', '');
var plaintextLength = plaintext.length,
var plaintextBuffer = Platform.BufferUtils.toBuffer(plaintext);
var plaintextLength = plaintextBuffer.length,
paddedLength = getPaddedLength(plaintextLength),
iv = this.getIv();
var cipherOut = this.encryptCipher.update(Buffer.concat([plaintext, pkcs5Padding[paddedLength - plaintextLength]]));
var cipherOut = this.encryptCipher.update(
Buffer.concat([plaintextBuffer, pkcs5Padding[paddedLength - plaintextLength]])
);
var ciphertext = Buffer.concat([iv, toBuffer(cipherOut)]);
return callback(null, ciphertext);
};
Expand Down
35 changes: 30 additions & 5 deletions test/realtime/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
}
}

function testEachFixture(done, filename, channelName, testsPerFixture, fixtureTest) {
function testEachFixture(done, filename, channelName, testsPerFixture, testPlaintextVariants, fixtureTest) {
if (!Crypto) {
done(new Error('Encryption not supported'));
return;
Expand All @@ -71,11 +71,30 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
var item = testData.items[i];

/* read messages from test data and decode (ie remove any base64 encoding). */
var testMessage = Message.fromEncoded(item.encoded);
var createTestMessage = function () {
return Message.fromEncoded(item.encoded);
};

var encryptedMessage = Message.fromEncoded(item.encrypted);
/* reset channel cipher, to ensure it uses the given iv */
channel.setOptions({ cipher: { key: key, iv: iv } });
fixtureTest(channel.channelOptions, testMessage, encryptedMessage, item.msgpack);

var runTest = function (testMessage) {
/* reset channel cipher, to ensure it uses the given iv */
channel.setOptions({ cipher: { key: key, iv: iv } });
fixtureTest(channel.channelOptions, testMessage, encryptedMessage, item.msgpack);
};

// Run the test with the message’s data as-is.
runTest(createTestMessage());

if (testPlaintextVariants) {
var testMessage = createTestMessage();
if (BufferUtils.isBuffer(testMessage.data) && !(testMessage.data instanceof ArrayBuffer)) {
// Now, check that we can also handle an ArrayBuffer plaintext.
var testMessageWithArrayBufferData = createTestMessage();
testMessageWithArrayBufferData.data = BufferUtils.toArrayBuffer(testMessageWithArrayBufferData.data);
runTest(testMessageWithArrayBufferData);
}
}
}
} catch (err) {
closeAndFinish(done, realtime, err);
Expand Down Expand Up @@ -204,6 +223,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'crypto-data-128.json',
'encrypt_message_128',
2,
true,
function (channelOpts, testMessage, encryptedMessage) {
/* encrypt plaintext message; encode() also to handle data that is not already string or buffer */
Message.encode(testMessage, channelOpts, function () {
Expand All @@ -220,6 +240,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'crypto-data-256.json',
'encrypt_message_256',
2,
true,
function (channelOpts, testMessage, encryptedMessage) {
/* encrypt plaintext message; encode() also to handle data that is not already string or buffer */
Message.encode(testMessage, channelOpts, function () {
Expand All @@ -236,6 +257,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'crypto-data-128.json',
'decrypt_message_128',
2,
false,
function (channelOpts, testMessage, encryptedMessage) {
/* decrypt encrypted message; decode() also to handle data that is not string or buffer */
Message.decode(encryptedMessage, channelOpts);
Expand All @@ -251,6 +273,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'crypto-data-256.json',
'decrypt_message_256',
2,
false,
function (channelOpts, testMessage, encryptedMessage) {
/* decrypt encrypted message; decode() also to handle data that is not string or buffer */
Message.decode(encryptedMessage, channelOpts);
Expand Down Expand Up @@ -292,6 +315,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'crypto-data-128.json',
'msgpack_128',
2,
false,
function (channelOpts, testMessage, encryptedMessage, msgpackEncodedMessage) {
Message.encode(testMessage, channelOpts, function () {
var msgpackFromEncoded = msgpack.encode(testMessage);
Expand Down Expand Up @@ -325,6 +349,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'crypto-data-256.json',
'msgpack_256',
2,
false,
function (channelOpts, testMessage, encryptedMessage, msgpackEncodedMessage) {
Message.encode(testMessage, channelOpts, function () {
var msgpackFromEncoded = msgpack.encode(testMessage);
Expand Down