2015-06-23 18:50:30 +02:00
|
|
|
var malloc = Module['_malloc'];
|
|
|
|
var free = Module['_free'];
|
2018-09-21 17:01:51 +02:00
|
|
|
var OLM_ERROR;
|
2016-07-06 00:01:23 +02:00
|
|
|
|
|
|
|
/* allocate a number of bytes of storage on the stack.
|
|
|
|
*
|
|
|
|
* If size_or_array is a Number, allocates that number of zero-initialised bytes.
|
|
|
|
*/
|
2015-06-23 18:50:30 +02:00
|
|
|
function stack(size_or_array) {
|
2018-09-21 17:01:51 +02:00
|
|
|
return allocate(size_or_array, 'i8', Module['ALLOC_STACK']);
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function array_from_string(string) {
|
2018-09-21 17:01:51 +02:00
|
|
|
return intArrayFromString(string, true);
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function random_stack(size) {
|
|
|
|
var ptr = stack(size);
|
|
|
|
var array = new Uint8Array(Module['HEAPU8'].buffer, ptr, size);
|
2015-07-14 17:45:00 +02:00
|
|
|
get_random_values(array);
|
2015-06-26 17:16:34 +02:00
|
|
|
return ptr;
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function restore_stack(wrapped) {
|
|
|
|
return function() {
|
2018-09-21 17:01:51 +02:00
|
|
|
var sp = stackSave();
|
2015-06-23 18:50:30 +02:00
|
|
|
try {
|
|
|
|
return wrapped.apply(this, arguments);
|
|
|
|
} finally {
|
2018-09-21 17:01:51 +02:00
|
|
|
stackRestore(sp);
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 14:37:34 +01:00
|
|
|
/* set a memory area to zero */
|
|
|
|
function bzero(ptr, n) {
|
|
|
|
while(n-- > 0) {
|
|
|
|
Module['HEAP8'][ptr++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:50:30 +02:00
|
|
|
function Account() {
|
2015-06-27 01:15:23 +02:00
|
|
|
var size = Module['_olm_account_size']();
|
2015-06-23 18:50:30 +02:00
|
|
|
this.buf = malloc(size);
|
2015-06-27 01:15:23 +02:00
|
|
|
this.ptr = Module['_olm_account'](this.buf);
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function account_method(wrapped) {
|
|
|
|
return function() {
|
|
|
|
var result = wrapped.apply(this, arguments);
|
2015-06-27 01:15:23 +02:00
|
|
|
if (result === OLM_ERROR) {
|
2015-06-23 18:50:30 +02:00
|
|
|
var message = Pointer_stringify(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_account_last_error'](arguments[0])
|
2015-06-23 18:50:30 +02:00
|
|
|
);
|
2015-06-27 01:15:23 +02:00
|
|
|
throw new Error("OLM." + message);
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Account.prototype['free'] = function() {
|
2015-08-04 12:55:04 +02:00
|
|
|
Module['_olm_clear_account'](this.ptr);
|
2015-06-23 18:50:30 +02:00
|
|
|
free(this.ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Account.prototype['create'] = restore_stack(function() {
|
|
|
|
var random_length = account_method(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_create_account_random_length']
|
2015-06-23 18:50:30 +02:00
|
|
|
)(this.ptr);
|
|
|
|
var random = random_stack(random_length);
|
2015-06-27 01:15:23 +02:00
|
|
|
account_method(Module['_olm_create_account'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, random, random_length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
Account.prototype['identity_keys'] = restore_stack(function() {
|
2015-06-23 18:50:30 +02:00
|
|
|
var keys_length = account_method(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_account_identity_keys_length']
|
2015-07-10 12:57:53 +02:00
|
|
|
)(this.ptr);
|
2016-07-06 00:01:23 +02:00
|
|
|
var keys = stack(keys_length + NULL_BYTE_PADDING_LENGTH);
|
2015-06-27 01:15:23 +02:00
|
|
|
account_method(Module['_olm_account_identity_keys'])(
|
2015-07-10 12:57:53 +02:00
|
|
|
this.ptr, keys, keys_length
|
2015-06-23 18:50:30 +02:00
|
|
|
);
|
2016-07-06 00:01:23 +02:00
|
|
|
return Pointer_stringify(keys);
|
2015-06-23 18:50:30 +02:00
|
|
|
});
|
|
|
|
|
2015-07-10 12:57:53 +02:00
|
|
|
Account.prototype['sign'] = restore_stack(function(message) {
|
|
|
|
var signature_length = account_method(
|
|
|
|
Module['_olm_account_signature_length']
|
|
|
|
)(this.ptr);
|
|
|
|
var message_array = array_from_string(message);
|
|
|
|
var message_buffer = stack(message_array);
|
2016-07-06 00:01:23 +02:00
|
|
|
var signature_buffer = stack(signature_length + NULL_BYTE_PADDING_LENGTH);
|
2015-07-10 12:57:53 +02:00
|
|
|
account_method(Module['_olm_account_sign'])(
|
|
|
|
this.ptr,
|
|
|
|
message_buffer, message_array.length,
|
|
|
|
signature_buffer, signature_length
|
|
|
|
);
|
2016-07-06 00:01:23 +02:00
|
|
|
return Pointer_stringify(signature_buffer);
|
2015-07-10 12:57:53 +02:00
|
|
|
});
|
|
|
|
|
2015-06-23 18:50:30 +02:00
|
|
|
Account.prototype['one_time_keys'] = restore_stack(function() {
|
|
|
|
var keys_length = account_method(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_account_one_time_keys_length']
|
2015-06-23 18:50:30 +02:00
|
|
|
)(this.ptr);
|
2016-07-06 00:01:23 +02:00
|
|
|
var keys = stack(keys_length + NULL_BYTE_PADDING_LENGTH);
|
2015-06-27 01:15:23 +02:00
|
|
|
account_method(Module['_olm_account_one_time_keys'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, keys, keys_length
|
|
|
|
);
|
2016-07-06 00:01:23 +02:00
|
|
|
return Pointer_stringify(keys);
|
2015-06-23 18:50:30 +02:00
|
|
|
});
|
|
|
|
|
2015-07-09 19:35:54 +02:00
|
|
|
Account.prototype['mark_keys_as_published'] = restore_stack(function() {
|
|
|
|
account_method(Module['_olm_account_mark_keys_as_published'])(this.ptr);
|
|
|
|
});
|
|
|
|
|
|
|
|
Account.prototype['max_number_of_one_time_keys'] = restore_stack(function() {
|
2015-07-17 19:43:41 +02:00
|
|
|
return account_method(Module['_olm_account_max_number_of_one_time_keys'])(
|
2015-07-09 19:35:54 +02:00
|
|
|
this.ptr
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Account.prototype['generate_one_time_keys'] = restore_stack(function(
|
|
|
|
number_of_keys
|
|
|
|
) {
|
|
|
|
var random_length = account_method(
|
|
|
|
Module['_olm_account_generate_one_time_keys_random_length']
|
|
|
|
)(this.ptr, number_of_keys);
|
|
|
|
var random = random_stack(random_length);
|
|
|
|
account_method(Module['_olm_account_generate_one_time_keys'])(
|
|
|
|
this.ptr, number_of_keys, random, random_length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-07-17 19:43:41 +02:00
|
|
|
Account.prototype['remove_one_time_keys'] = restore_stack(function(session) {
|
|
|
|
account_method(Module['_olm_remove_one_time_keys'])(
|
|
|
|
this.ptr, session.ptr
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-06-23 18:50:30 +02:00
|
|
|
Account.prototype['pickle'] = restore_stack(function(key) {
|
|
|
|
var key_array = array_from_string(key);
|
|
|
|
var pickle_length = account_method(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_pickle_account_length']
|
2015-06-23 18:50:30 +02:00
|
|
|
)(this.ptr);
|
|
|
|
var key_buffer = stack(key_array);
|
2016-07-06 00:01:23 +02:00
|
|
|
var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH);
|
2015-06-27 01:15:23 +02:00
|
|
|
account_method(Module['_olm_pickle_account'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
|
|
|
|
);
|
2016-07-06 00:01:23 +02:00
|
|
|
return Pointer_stringify(pickle_buffer);
|
2015-06-23 18:50:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Account.prototype['unpickle'] = restore_stack(function(key, pickle) {
|
|
|
|
var key_array = array_from_string(key);
|
|
|
|
var key_buffer = stack(key_array);
|
|
|
|
var pickle_array = array_from_string(pickle);
|
2015-07-14 17:45:00 +02:00
|
|
|
var pickle_buffer = stack(pickle_array);
|
2015-06-27 01:15:23 +02:00
|
|
|
account_method(Module['_olm_unpickle_account'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, key_buffer, key_array.length, pickle_buffer,
|
|
|
|
pickle_array.length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
function Session() {
|
2015-06-27 01:15:23 +02:00
|
|
|
var size = Module['_olm_session_size']();
|
2015-06-23 18:50:30 +02:00
|
|
|
this.buf = malloc(size);
|
2015-06-27 01:15:23 +02:00
|
|
|
this.ptr = Module['_olm_session'](this.buf);
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function session_method(wrapped) {
|
|
|
|
return function() {
|
|
|
|
var result = wrapped.apply(this, arguments);
|
2015-06-27 01:15:23 +02:00
|
|
|
if (result === OLM_ERROR) {
|
2015-06-23 18:50:30 +02:00
|
|
|
var message = Pointer_stringify(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_session_last_error'](arguments[0])
|
2015-06-23 18:50:30 +02:00
|
|
|
);
|
2015-06-27 01:15:23 +02:00
|
|
|
throw new Error("OLM." + message);
|
2015-06-23 18:50:30 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Session.prototype['free'] = function() {
|
2015-08-04 12:55:04 +02:00
|
|
|
Module['_olm_clear_session'](this.ptr);
|
2015-06-23 18:50:30 +02:00
|
|
|
free(this.ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Session.prototype['pickle'] = restore_stack(function(key) {
|
|
|
|
var key_array = array_from_string(key);
|
|
|
|
var pickle_length = session_method(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_pickle_session_length']
|
2015-06-23 18:50:30 +02:00
|
|
|
)(this.ptr);
|
|
|
|
var key_buffer = stack(key_array);
|
2016-07-06 00:01:23 +02:00
|
|
|
var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH);
|
2015-06-27 01:15:23 +02:00
|
|
|
session_method(Module['_olm_pickle_session'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
|
|
|
|
);
|
2016-07-06 00:01:23 +02:00
|
|
|
return Pointer_stringify(pickle_buffer);
|
2015-06-23 18:50:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Session.prototype['unpickle'] = restore_stack(function(key, pickle) {
|
|
|
|
var key_array = array_from_string(key);
|
|
|
|
var key_buffer = stack(key_array);
|
|
|
|
var pickle_array = array_from_string(pickle);
|
|
|
|
var pickle_buffer = stack(pickle_array);
|
2015-06-27 01:15:23 +02:00
|
|
|
session_method(Module['_olm_unpickle_session'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, key_buffer, key_array.length, pickle_buffer,
|
|
|
|
pickle_array.length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Session.prototype['create_outbound'] = restore_stack(function(
|
2015-07-08 17:00:08 +02:00
|
|
|
account, their_identity_key, their_one_time_key
|
2015-06-23 18:50:30 +02:00
|
|
|
) {
|
|
|
|
var random_length = session_method(
|
2015-06-27 01:15:23 +02:00
|
|
|
Module['_olm_create_outbound_session_random_length']
|
2015-06-23 18:50:30 +02:00
|
|
|
)(this.ptr);
|
|
|
|
var random = random_stack(random_length);
|
|
|
|
var identity_key_array = array_from_string(their_identity_key);
|
|
|
|
var one_time_key_array = array_from_string(their_one_time_key);
|
|
|
|
var identity_key_buffer = stack(identity_key_array);
|
|
|
|
var one_time_key_buffer = stack(one_time_key_array);
|
2015-06-27 01:15:23 +02:00
|
|
|
session_method(Module['_olm_create_outbound_session'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, account.ptr,
|
|
|
|
identity_key_buffer, identity_key_array.length,
|
|
|
|
one_time_key_buffer, one_time_key_array.length,
|
|
|
|
random, random_length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Session.prototype['create_inbound'] = restore_stack(function(
|
|
|
|
account, one_time_key_message
|
|
|
|
) {
|
|
|
|
var message_array = array_from_string(one_time_key_message);
|
|
|
|
var message_buffer = stack(message_array);
|
2015-06-27 01:15:23 +02:00
|
|
|
session_method(Module['_olm_create_inbound_session'])(
|
2015-06-23 18:50:30 +02:00
|
|
|
this.ptr, account.ptr, message_buffer, message_array.length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-07-16 12:45:20 +02:00
|
|
|
Session.prototype['create_inbound_from'] = restore_stack(function(
|
|
|
|
account, identity_key, one_time_key_message
|
|
|
|
) {
|
|
|
|
var identity_key_array = array_from_string(identity_key);
|
|
|
|
var identity_key_buffer = stack(identity_key_array);
|
|
|
|
var message_array = array_from_string(one_time_key_message);
|
|
|
|
var message_buffer = stack(message_array);
|
|
|
|
session_method(Module['_olm_create_inbound_session_from'])(
|
|
|
|
this.ptr, account.ptr,
|
|
|
|
identity_key_buffer, identity_key_array.length,
|
|
|
|
message_buffer, message_array.length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Session.prototype['session_id'] = restore_stack(function() {
|
|
|
|
var id_length = session_method(Module['_olm_session_id_length'])(this.ptr);
|
2016-07-06 00:01:23 +02:00
|
|
|
var id_buffer = stack(id_length + NULL_BYTE_PADDING_LENGTH);
|
2015-07-16 12:45:20 +02:00
|
|
|
session_method(Module['_olm_session_id'])(
|
|
|
|
this.ptr, id_buffer, id_length
|
|
|
|
);
|
2016-07-06 00:01:23 +02:00
|
|
|
return Pointer_stringify(id_buffer);
|
2015-07-16 12:45:20 +02:00
|
|
|
});
|
|
|
|
|
2016-09-05 00:34:24 +02:00
|
|
|
Session.prototype['has_received_message'] = function() {
|
|
|
|
return session_method(Module['_olm_session_has_received_message'])(
|
|
|
|
this.ptr
|
|
|
|
) ? true : false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-23 18:50:30 +02:00
|
|
|
Session.prototype['matches_inbound'] = restore_stack(function(
|
2015-07-21 11:50:17 +02:00
|
|
|
one_time_key_message
|
2015-06-23 18:50:30 +02:00
|
|
|
) {
|
|
|
|
var message_array = array_from_string(one_time_key_message);
|
|
|
|
var message_buffer = stack(message_array);
|
2015-06-27 01:15:23 +02:00
|
|
|
return session_method(Module['_olm_matches_inbound_session'])(
|
2015-07-21 11:50:17 +02:00
|
|
|
this.ptr, message_buffer, message_array.length
|
2015-06-23 18:50:30 +02:00
|
|
|
) ? true : false;
|
|
|
|
});
|
|
|
|
|
2015-07-16 12:45:20 +02:00
|
|
|
Session.prototype['matches_inbound_from'] = restore_stack(function(
|
2015-07-21 11:50:17 +02:00
|
|
|
identity_key, one_time_key_message
|
2015-07-16 12:45:20 +02:00
|
|
|
) {
|
|
|
|
var identity_key_array = array_from_string(identity_key);
|
|
|
|
var identity_key_buffer = stack(identity_key_array);
|
|
|
|
var message_array = array_from_string(one_time_key_message);
|
|
|
|
var message_buffer = stack(message_array);
|
|
|
|
return session_method(Module['_olm_matches_inbound_session_from'])(
|
2015-07-21 11:50:17 +02:00
|
|
|
this.ptr,
|
2015-07-16 12:45:20 +02:00
|
|
|
identity_key_buffer, identity_key_array.length,
|
|
|
|
message_buffer, message_array.length
|
|
|
|
) ? true : false;
|
|
|
|
});
|
|
|
|
|
2015-06-23 18:50:30 +02:00
|
|
|
Session.prototype['encrypt'] = restore_stack(function(
|
|
|
|
plaintext
|
|
|
|
) {
|
2016-12-15 14:37:34 +01:00
|
|
|
var plaintext_buffer, message_buffer, plaintext_length;
|
2016-12-14 12:43:00 +01:00
|
|
|
try {
|
|
|
|
var random_length = session_method(
|
|
|
|
Module['_olm_encrypt_random_length']
|
|
|
|
)(this.ptr);
|
|
|
|
var message_type = session_method(
|
|
|
|
Module['_olm_encrypt_message_type']
|
|
|
|
)(this.ptr);
|
|
|
|
|
2018-09-21 17:01:51 +02:00
|
|
|
plaintext_length = lengthBytesUTF8(plaintext);
|
2016-12-14 12:43:00 +01:00
|
|
|
var message_length = session_method(
|
|
|
|
Module['_olm_encrypt_message_length']
|
|
|
|
)(this.ptr, plaintext_length);
|
|
|
|
|
|
|
|
var random = random_stack(random_length);
|
|
|
|
|
|
|
|
// need to allow space for the terminator (which stringToUTF8 always
|
|
|
|
// writes), hence + 1.
|
|
|
|
plaintext_buffer = malloc(plaintext_length + 1);
|
2018-09-21 17:01:51 +02:00
|
|
|
stringToUTF8(plaintext, plaintext_buffer, plaintext_length + 1);
|
2016-12-14 12:43:00 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
);
|
2016-12-16 15:42:41 +01:00
|
|
|
|
|
|
|
// UTF8ToString requires a null-terminated argument, so add the
|
|
|
|
// null terminator.
|
2018-09-21 17:01:51 +02:00
|
|
|
setValue(
|
2016-12-16 15:42:41 +01:00
|
|
|
message_buffer+message_length,
|
|
|
|
0, "i8"
|
|
|
|
);
|
|
|
|
|
2016-12-14 12:43:00 +01:00
|
|
|
return {
|
|
|
|
"type": message_type,
|
2018-09-21 17:01:51 +02:00
|
|
|
"body": UTF8ToString(message_buffer),
|
2016-12-14 12:43:00 +01:00
|
|
|
};
|
|
|
|
} finally {
|
|
|
|
if (plaintext_buffer !== undefined) {
|
2016-12-15 14:37:34 +01:00
|
|
|
// don't leave a copy of the plaintext in the heap.
|
|
|
|
bzero(plaintext_buffer, plaintext_length + 1);
|
2016-12-14 12:43:00 +01:00
|
|
|
free(plaintext_buffer);
|
|
|
|
}
|
|
|
|
if (message_buffer !== undefined) {
|
|
|
|
free(message_buffer);
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 18:50:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Session.prototype['decrypt'] = restore_stack(function(
|
|
|
|
message_type, message
|
|
|
|
) {
|
2016-12-15 17:28:30 +01:00
|
|
|
var message_buffer, plaintext_buffer, max_plaintext_length;
|
2016-12-14 12:43:00 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
message_buffer = malloc(message.length);
|
2018-09-21 17:01:51 +02:00
|
|
|
writeAsciiToMemory(message, message_buffer, true);
|
2016-12-14 12:43:00 +01:00
|
|
|
|
2016-12-15 14:37:34 +01:00
|
|
|
max_plaintext_length = session_method(
|
2016-12-14 12:43:00 +01:00
|
|
|
Module['_olm_decrypt_max_plaintext_length']
|
|
|
|
)(this.ptr, message_type, message_buffer, message.length);
|
|
|
|
|
|
|
|
// caculating the length destroys the input buffer, so we need to re-copy it.
|
2018-09-21 17:01:51 +02:00
|
|
|
writeAsciiToMemory(message, message_buffer, true);
|
2016-12-14 12:43:00 +01:00
|
|
|
|
|
|
|
plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
|
|
|
|
|
|
|
|
var plaintext_length = session_method(Module["_olm_decrypt"])(
|
|
|
|
this.ptr, message_type,
|
|
|
|
message_buffer, message.length,
|
|
|
|
plaintext_buffer, max_plaintext_length
|
|
|
|
);
|
|
|
|
|
|
|
|
// UTF8ToString requires a null-terminated argument, so add the
|
|
|
|
// null terminator.
|
2018-09-21 17:01:51 +02:00
|
|
|
setValue(
|
2016-12-14 12:43:00 +01:00
|
|
|
plaintext_buffer+plaintext_length,
|
|
|
|
0, "i8"
|
|
|
|
);
|
|
|
|
|
|
|
|
return UTF8ToString(plaintext_buffer);
|
|
|
|
} finally {
|
|
|
|
if (message_buffer !== undefined) {
|
|
|
|
free(message_buffer);
|
|
|
|
}
|
|
|
|
if (plaintext_buffer !== undefined) {
|
2016-12-15 14:37:34 +01:00
|
|
|
// don't leave a copy of the plaintext in the heap.
|
|
|
|
bzero(plaintext_buffer, max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
|
2016-12-14 12:43:00 +01:00
|
|
|
free(plaintext_buffer);
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 00:01:23 +02:00
|
|
|
|
2015-06-23 18:50:30 +02:00
|
|
|
});
|
|
|
|
|
2015-08-04 12:55:04 +02:00
|
|
|
function Utility() {
|
|
|
|
var size = Module['_olm_utility_size']();
|
|
|
|
this.buf = malloc(size);
|
|
|
|
this.ptr = Module['_olm_utility'](this.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
function utility_method(wrapped) {
|
|
|
|
return function() {
|
|
|
|
var result = wrapped.apply(this, arguments);
|
|
|
|
if (result === OLM_ERROR) {
|
|
|
|
var message = Pointer_stringify(
|
|
|
|
Module['_olm_utility_last_error'](arguments[0])
|
|
|
|
);
|
|
|
|
throw new Error("OLM." + message);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Utility.prototype['free'] = function() {
|
|
|
|
Module['_olm_clear_utility'](this.ptr);
|
|
|
|
free(this.ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Utility.prototype['sha256'] = restore_stack(function(input) {
|
|
|
|
var output_length = utility_method(Module['_olm_sha256_length'])(this.ptr);
|
|
|
|
var input_array = array_from_string(input);
|
|
|
|
var input_buffer = stack(input_array);
|
2016-07-06 00:01:23 +02:00
|
|
|
var output_buffer = stack(output_length + NULL_BYTE_PADDING_LENGTH);
|
2018-01-04 03:41:48 +01:00
|
|
|
utility_method(Module['_olm_sha256'])(
|
2015-08-04 12:55:04 +02:00
|
|
|
this.ptr,
|
2018-01-04 03:41:48 +01:00
|
|
|
input_buffer, input_array.length,
|
2015-08-04 12:55:04 +02:00
|
|
|
output_buffer, output_length
|
|
|
|
);
|
2016-07-06 00:01:23 +02:00
|
|
|
return Pointer_stringify(output_buffer);
|
2015-08-04 12:55:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Utility.prototype['ed25519_verify'] = restore_stack(function(
|
|
|
|
key, message, signature
|
|
|
|
) {
|
|
|
|
var key_array = array_from_string(key);
|
|
|
|
var key_buffer = stack(key_array);
|
|
|
|
var message_array = array_from_string(message);
|
|
|
|
var message_buffer = stack(message_array);
|
|
|
|
var signature_array = array_from_string(signature);
|
|
|
|
var signature_buffer = stack(signature_array);
|
|
|
|
utility_method(Module['_olm_ed25519_verify'])(
|
|
|
|
this.ptr,
|
|
|
|
key_buffer, key_array.length,
|
|
|
|
message_buffer, message_array.length,
|
|
|
|
signature_buffer, signature_array.length
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-07-14 17:45:00 +02:00
|
|
|
olm_exports["Account"] = Account;
|
|
|
|
olm_exports["Session"] = Session;
|
2015-08-04 12:55:04 +02:00
|
|
|
olm_exports["Utility"] = Utility;
|
2018-06-11 23:48:45 +02:00
|
|
|
olm_exports["PkEncryption"] = PkEncryption;
|
|
|
|
olm_exports["PkDecryption"] = PkDecryption;
|
2016-09-01 14:35:23 +02:00
|
|
|
|
|
|
|
olm_exports["get_library_version"] = restore_stack(function() {
|
|
|
|
var buf = stack(3);
|
|
|
|
Module['_olm_get_library_version'](buf, buf+1, buf+2);
|
|
|
|
return [
|
|
|
|
getValue(buf, 'i8'),
|
|
|
|
getValue(buf+1, 'i8'),
|
|
|
|
getValue(buf+2, 'i8'),
|
|
|
|
];
|
|
|
|
});
|
2016-12-12 17:52:03 +01:00
|
|
|
|
2018-09-25 18:13:29 +02:00
|
|
|
Module['onRuntimeInitialized'] = function() {
|
2018-09-21 17:01:51 +02:00
|
|
|
OLM_ERROR = Module['_olm_error']();
|
2018-10-02 20:21:05 +02:00
|
|
|
olm_exports["PRIVATE_KEY_LENGTH"] = Module['_olm_pk_private_key_length']();
|
2018-09-25 18:13:29 +02:00
|
|
|
if (onInitSuccess) onInitSuccess();
|
|
|
|
};
|
|
|
|
|
|
|
|
Module['onAbort'] = function(err) {
|
|
|
|
if (onInitFail) onInitFail(err);
|
|
|
|
};
|