aboutsummaryrefslogtreecommitdiff
path: root/javascript/olm_inbound_group_session.js
diff options
context:
space:
mode:
Diffstat (limited to 'javascript/olm_inbound_group_session.js')
-rw-r--r--javascript/olm_inbound_group_session.js68
1 files changed, 43 insertions, 25 deletions
diff --git a/javascript/olm_inbound_group_session.js b/javascript/olm_inbound_group_session.js
index 1b7fcfe..2e4727f 100644
--- a/javascript/olm_inbound_group_session.js
+++ b/javascript/olm_inbound_group_session.js
@@ -64,33 +64,51 @@ InboundGroupSession.prototype['create'] = restore_stack(function(session_key) {
InboundGroupSession.prototype['decrypt'] = restore_stack(function(
message
) {
- var message_array = array_from_string(message);
- 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
- );
+ var message_buffer, plaintext_buffer, plaintext_length;
- // Pointer_stringify requires a null-terminated argument (the optional
- // 'len' argument doesn't work for UTF-8 data).
- Module['setValue'](
- plaintext_buffer+plaintext_length,
- 0, "i8"
- );
+ try {
+ message_buffer = malloc(message.length);
+ Module['writeAsciiToMemory'](message, message_buffer, true);
+
+ 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 {
- "plaintext": Pointer_stringify(plaintext_buffer),
- "message_index": Module['getValue'](message_index, "i32")
+ plaintext_length = inbound_group_session_method(
+ Module["_olm_group_decrypt"]
+ )(
+ 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) {
+ // don't leave a copy of the plaintext in the heap.
+ bzero(plaintext_buffer, plaintext_length + NULL_BYTE_PADDING_LENGTH);
+ free(plaintext_buffer);
+ }
}
});