Commit 76610c0a authored by Richard van der Hoff's avatar Richard van der Hoff
Browse files

Allocate memory for message blobs on the heap

Messages can be very large, so we don't really want to allocate them on the
stack. Switch to using the heap for them, and try to clean up some of the
string handling while we're at it.
parent f6c05be8
...@@ -64,33 +64,49 @@ InboundGroupSession.prototype['create'] = restore_stack(function(session_key) { ...@@ -64,33 +64,49 @@ InboundGroupSession.prototype['create'] = restore_stack(function(session_key) {
InboundGroupSession.prototype['decrypt'] = restore_stack(function( InboundGroupSession.prototype['decrypt'] = restore_stack(function(
message message
) { ) {
var message_array = array_from_string(message); var message_buffer, plaintext_buffer;
var message_buffer = stack(message_array);
var max_plaintext_length = inbound_group_session_method(
Module['_olm_group_decrypt_max_plaintext_length']
)(this.ptr, message_buffer, message_array.length);
// caculating the length destroys the input buffer.
// So we copy the array to a new buffer
var message_buffer = stack(message_array);
var plaintext_buffer = stack(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
var message_index = stack(4);
var plaintext_length = inbound_group_session_method(Module["_olm_group_decrypt"])(
this.ptr,
message_buffer, message_array.length,
plaintext_buffer, max_plaintext_length,
message_index
);
// Pointer_stringify requires a null-terminated argument (the optional try {
// 'len' argument doesn't work for UTF-8 data). message_buffer = malloc(message.length);
Module['setValue']( Module['writeAsciiToMemory'](message, message_buffer, true);
plaintext_buffer+plaintext_length,
0, "i8" var max_plaintext_length = inbound_group_session_method(
); Module['_olm_group_decrypt_max_plaintext_length']
)(this.ptr, message_buffer, message.length);
// caculating the length destroys the input buffer, so we need to re-copy it.
Module['writeAsciiToMemory'](message, message_buffer, true);
plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
var message_index = stack(4);
return { var plaintext_length = inbound_group_session_method(
"plaintext": Pointer_stringify(plaintext_buffer), Module["_olm_group_decrypt"]
"message_index": Module['getValue'](message_index, "i32") )(
this.ptr,
message_buffer, message.length,
plaintext_buffer, max_plaintext_length,
message_index
);
// UTF8ToString requires a null-terminated argument, so add the
// null terminator.
Module['setValue'](
plaintext_buffer+plaintext_length,
0, "i8"
);
return {
"plaintext": UTF8ToString(plaintext_buffer),
"message_index": Module['getValue'](message_index, "i32")
}
} finally {
if (message_buffer !== undefined) {
free(message_buffer);
}
if (plaintext_buffer !== undefined) {
free(plaintext_buffer);
}
} }
}); });
......
...@@ -63,20 +63,36 @@ OutboundGroupSession.prototype['create'] = restore_stack(function() { ...@@ -63,20 +63,36 @@ OutboundGroupSession.prototype['create'] = restore_stack(function() {
); );
}); });
OutboundGroupSession.prototype['encrypt'] = restore_stack(function(plaintext) { OutboundGroupSession.prototype['encrypt'] = function(plaintext) {
var plaintext_array = array_from_string(plaintext); var plaintext_buffer, message_buffer;
var message_length = outbound_group_session_method( try {
Module['_olm_group_encrypt_message_length'] var plaintext_length = Module['lengthBytesUTF8'](plaintext);
)(this.ptr, plaintext_array.length);
var plaintext_buffer = stack(plaintext_array); var message_length = outbound_group_session_method(
var message_buffer = stack(message_length + NULL_BYTE_PADDING_LENGTH); Module['_olm_group_encrypt_message_length']
outbound_group_session_method(Module['_olm_group_encrypt'])( )(this.ptr, plaintext_length);
this.ptr,
plaintext_buffer, plaintext_array.length, // need to allow space for the terminator (which stringToUTF8 always
message_buffer, message_length // writes), hence + 1.
); plaintext_buffer = malloc(plaintext_length + 1);
return Pointer_stringify(message_buffer); Module['stringToUTF8'](plaintext, plaintext_buffer, plaintext_length + 1);
});
message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH);
outbound_group_session_method(Module['_olm_group_encrypt'])(
this.ptr,
plaintext_buffer, plaintext_length,
message_buffer, message_length
);
return Module['UTF8ToString'](message_buffer);
} finally {
if (plaintext_buffer !== undefined) {
free(plaintext_buffer);
}
if (message_buffer !== undefined) {
free(message_buffer);
}
}
};
OutboundGroupSession.prototype['session_id'] = restore_stack(function() { OutboundGroupSession.prototype['session_id'] = restore_stack(function() {
var length = outbound_group_session_method( var length = outbound_group_session_method(
......
...@@ -4,9 +4,11 @@ var free = Module['_free']; ...@@ -4,9 +4,11 @@ var free = Module['_free'];
var Pointer_stringify = Module['Pointer_stringify']; var Pointer_stringify = Module['Pointer_stringify'];
var OLM_ERROR = Module['_olm_error'](); var OLM_ERROR = Module['_olm_error']();
/* The 'length' argument to Pointer_stringify doesn't work if the input includes /* The 'length' argument to Pointer_stringify doesn't work if the input
* characters >= 128; we therefore need to add a NULL character to all of our * includes characters >= 128, which makes Pointer_stringify unreliable. We
* strings. This acts as a symbolic constant to help show what we're doing. * could use it on strings which are known to be ascii, but that seems
* dangerous. Instead we add a NULL character to all of our strings and just
* use UTF8ToString.
*/ */
var NULL_BYTE_PADDING_LENGTH = 1; var NULL_BYTE_PADDING_LENGTH = 1;
...@@ -297,59 +299,90 @@ Session.prototype['matches_inbound_from'] = restore_stack(function( ...@@ -297,59 +299,90 @@ Session.prototype['matches_inbound_from'] = restore_stack(function(
Session.prototype['encrypt'] = restore_stack(function( Session.prototype['encrypt'] = restore_stack(function(
plaintext plaintext
) { ) {
var random_length = session_method( var plaintext_buffer, message_buffer;
Module['_olm_encrypt_random_length'] try {
)(this.ptr); var random_length = session_method(
var message_type = session_method( Module['_olm_encrypt_random_length']
Module['_olm_encrypt_message_type'] )(this.ptr);
)(this.ptr); var message_type = session_method(
var plaintext_array = array_from_string(plaintext); Module['_olm_encrypt_message_type']
var message_length = session_method( )(this.ptr);
Module['_olm_encrypt_message_length']
)(this.ptr, plaintext_array.length); var plaintext_length = Module['lengthBytesUTF8'](plaintext);
var random = random_stack(random_length); var message_length = session_method(
var plaintext_buffer = stack(plaintext_array); Module['_olm_encrypt_message_length']
var message_buffer = stack(message_length + NULL_BYTE_PADDING_LENGTH); )(this.ptr, plaintext_length);
session_method(Module['_olm_encrypt'])(
this.ptr, var random = random_stack(random_length);
plaintext_buffer, plaintext_array.length,
random, random_length, // need to allow space for the terminator (which stringToUTF8 always
message_buffer, message_length // writes), hence + 1.
); plaintext_buffer = malloc(plaintext_length + 1);
return { Module['stringToUTF8'](plaintext, plaintext_buffer, plaintext_length + 1);
"type": message_type,
"body": Pointer_stringify(message_buffer) message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH);
};
session_method(Module['_olm_encrypt'])(
this.ptr,
plaintext_buffer, plaintext_length,
random, random_length,
message_buffer, message_length
);
return {
"type": message_type,
"body": Module['UTF8ToString'](message_buffer),
};
} finally {
if (plaintext_buffer !== undefined) {
free(plaintext_buffer);
}
if (message_buffer !== undefined) {
free(message_buffer);
}
}
}); });
Session.prototype['decrypt'] = restore_stack(function( Session.prototype['decrypt'] = restore_stack(function(
message_type, message message_type, message
) { ) {
var message_array = array_from_string(message); var message_buffer, plaintext_buffer;
var message_buffer = stack(message_array);
var max_plaintext_length = session_method( try {
Module['_olm_decrypt_max_plaintext_length'] message_buffer = malloc(message.length);
)(this.ptr, message_type, message_buffer, message_array.length); Module['writeAsciiToMemory'](message, message_buffer, true);
// caculating the length destroys the input buffer.
// So we copy the array to a new buffer var max_plaintext_length = session_method(
var message_buffer = stack(message_array); Module['_olm_decrypt_max_plaintext_length']
var plaintext_buffer = stack( )(this.ptr, message_type, message_buffer, message.length);
max_plaintext_length + NULL_BYTE_PADDING_LENGTH
); // caculating the length destroys the input buffer, so we need to re-copy it.
var plaintext_length = session_method(Module["_olm_decrypt"])( Module['writeAsciiToMemory'](message, message_buffer, true);
this.ptr, message_type,
message_buffer, message.length, plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
plaintext_buffer, max_plaintext_length
); var plaintext_length = session_method(Module["_olm_decrypt"])(
this.ptr, message_type,
// Pointer_stringify requires a null-terminated argument (the optional message_buffer, message.length,
// 'len' argument doesn't work for UTF-8 data). plaintext_buffer, max_plaintext_length
Module['setValue']( );
plaintext_buffer+plaintext_length,
0, "i8" // UTF8ToString requires a null-terminated argument, so add the
); // null terminator.
Module['setValue'](
plaintext_buffer+plaintext_length,
0, "i8"
);
return UTF8ToString(plaintext_buffer);
} finally {
if (message_buffer !== undefined) {
free(message_buffer);
}
if (plaintext_buffer !== undefined) {
free(plaintext_buffer);
}
}
return Pointer_stringify(plaintext_buffer);
}); });
function Utility() { function Utility() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment