Commit 071029c2 authored by poljar's avatar poljar Committed by Hubert Chathi
Browse files

javascript: Switch from deprecated Pointer_stringify() to UTF8toString().

The Pointer_stringify() function is deprecated and has a couple of
issues because it tries to guess the encoding of the buffer. In some
cases it can ignore the length parameter which could end up in
inconsistencies.

Switch to UTF8toString() that takes a length parameter and respects,
that way we don't need to allocate an additional byte for a NULL byte.
parent 709687a7
...@@ -8,7 +8,7 @@ function inbound_group_session_method(wrapped) { ...@@ -8,7 +8,7 @@ function inbound_group_session_method(wrapped) {
return function() { return function() {
var result = wrapped.apply(this, arguments); var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) { if (result === OLM_ERROR) {
var message = Pointer_stringify( var message = UTF8ToString(
Module['_olm_inbound_group_session_last_error'](arguments[0]) Module['_olm_inbound_group_session_last_error'](arguments[0])
); );
throw new Error("OLM." + message); throw new Error("OLM." + message);
...@@ -28,7 +28,7 @@ InboundGroupSession.prototype['pickle'] = restore_stack(function(key) { ...@@ -28,7 +28,7 @@ InboundGroupSession.prototype['pickle'] = restore_stack(function(key) {
Module['_olm_pickle_inbound_group_session_length'] Module['_olm_pickle_inbound_group_session_length']
)(this.ptr); )(this.ptr);
var key_buffer = stack(key_array); var key_buffer = stack(key_array);
var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH); var pickle_buffer = stack(pickle_length);
try { try {
inbound_group_session_method(Module['_olm_pickle_inbound_group_session'])( inbound_group_session_method(Module['_olm_pickle_inbound_group_session'])(
this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
...@@ -40,7 +40,7 @@ InboundGroupSession.prototype['pickle'] = restore_stack(function(key) { ...@@ -40,7 +40,7 @@ InboundGroupSession.prototype['pickle'] = restore_stack(function(key) {
key_array[i] = 0; key_array[i] = 0;
} }
} }
return Pointer_stringify(pickle_buffer); return UTF8ToString(pickle_buffer, pickle_length);
}); });
InboundGroupSession.prototype['unpickle'] = restore_stack(function(key, pickle) { InboundGroupSession.prototype['unpickle'] = restore_stack(function(key, pickle) {
...@@ -112,7 +112,7 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function( ...@@ -112,7 +112,7 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function(
// caculating the length destroys the input buffer, so we need to re-copy it. // caculating the length destroys the input buffer, so we need to re-copy it.
writeAsciiToMemory(message, message_buffer, true); writeAsciiToMemory(message, message_buffer, true);
plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH); plaintext_buffer = malloc(max_plaintext_length);
var message_index = stack(4); var message_index = stack(4);
plaintext_length = inbound_group_session_method( plaintext_length = inbound_group_session_method(
...@@ -124,15 +124,8 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function( ...@@ -124,15 +124,8 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function(
message_index message_index
); );
// UTF8ToString requires a null-terminated argument, so add the
// null terminator.
setValue(
plaintext_buffer+plaintext_length,
0, "i8"
);
return { return {
"plaintext": UTF8ToString(plaintext_buffer), "plaintext": UTF8ToString(plaintext_buffer, plaintext_length),
"message_index": getValue(message_index, "i32") "message_index": getValue(message_index, "i32")
} }
} finally { } finally {
...@@ -141,7 +134,7 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function( ...@@ -141,7 +134,7 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function(
} }
if (plaintext_buffer !== undefined) { if (plaintext_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap. // don't leave a copy of the plaintext in the heap.
bzero(plaintext_buffer, plaintext_length + NULL_BYTE_PADDING_LENGTH); bzero(plaintext_buffer, plaintext_length);
free(plaintext_buffer); free(plaintext_buffer);
} }
} }
...@@ -151,11 +144,11 @@ InboundGroupSession.prototype['session_id'] = restore_stack(function() { ...@@ -151,11 +144,11 @@ InboundGroupSession.prototype['session_id'] = restore_stack(function() {
var length = inbound_group_session_method( var length = inbound_group_session_method(
Module['_olm_inbound_group_session_id_length'] Module['_olm_inbound_group_session_id_length']
)(this.ptr); )(this.ptr);
var session_id = stack(length + NULL_BYTE_PADDING_LENGTH); var session_id = stack(length);
inbound_group_session_method(Module['_olm_inbound_group_session_id'])( inbound_group_session_method(Module['_olm_inbound_group_session_id'])(
this.ptr, session_id, length this.ptr, session_id, length
); );
return Pointer_stringify(session_id); return UTF8ToString(session_id, length);
}); });
InboundGroupSession.prototype['first_known_index'] = restore_stack(function() { InboundGroupSession.prototype['first_known_index'] = restore_stack(function() {
...@@ -168,11 +161,11 @@ InboundGroupSession.prototype['export_session'] = restore_stack(function(message ...@@ -168,11 +161,11 @@ InboundGroupSession.prototype['export_session'] = restore_stack(function(message
var key_length = inbound_group_session_method( var key_length = inbound_group_session_method(
Module['_olm_export_inbound_group_session_length'] Module['_olm_export_inbound_group_session_length']
)(this.ptr); )(this.ptr);
var key = stack(key_length + NULL_BYTE_PADDING_LENGTH); var key = stack(key_length);
outbound_group_session_method(Module['_olm_export_inbound_group_session'])( outbound_group_session_method(Module['_olm_export_inbound_group_session'])(
this.ptr, key, key_length, message_index this.ptr, key, key_length, message_index
); );
var key_str = Pointer_stringify(key); var key_str = UTF8ToString(key, key_length);
bzero(key, key_length); // clear out a copy of the key bzero(key, key_length); // clear out a copy of the key
return key_str; return key_str;
}); });
......
...@@ -8,7 +8,7 @@ function outbound_group_session_method(wrapped) { ...@@ -8,7 +8,7 @@ function outbound_group_session_method(wrapped) {
return function() { return function() {
var result = wrapped.apply(this, arguments); var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) { if (result === OLM_ERROR) {
var message = Pointer_stringify( var message = UTF8ToString(
Module['_olm_outbound_group_session_last_error'](arguments[0]) Module['_olm_outbound_group_session_last_error'](arguments[0])
); );
throw new Error("OLM." + message); throw new Error("OLM." + message);
...@@ -28,7 +28,7 @@ OutboundGroupSession.prototype['pickle'] = restore_stack(function(key) { ...@@ -28,7 +28,7 @@ OutboundGroupSession.prototype['pickle'] = restore_stack(function(key) {
Module['_olm_pickle_outbound_group_session_length'] Module['_olm_pickle_outbound_group_session_length']
)(this.ptr); )(this.ptr);
var key_buffer = stack(key_array); var key_buffer = stack(key_array);
var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH); var pickle_buffer = stack(pickle_length);
try { try {
outbound_group_session_method(Module['_olm_pickle_outbound_group_session'])( outbound_group_session_method(Module['_olm_pickle_outbound_group_session'])(
this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
...@@ -40,7 +40,7 @@ OutboundGroupSession.prototype['pickle'] = restore_stack(function(key) { ...@@ -40,7 +40,7 @@ OutboundGroupSession.prototype['pickle'] = restore_stack(function(key) {
key_array[i] = 0; key_array[i] = 0;
} }
} }
return Pointer_stringify(pickle_buffer); return UTF8ToString(pickle_buffer, pickle_length);
}); });
OutboundGroupSession.prototype['unpickle'] = restore_stack(function(key, pickle) { OutboundGroupSession.prototype['unpickle'] = restore_stack(function(key, pickle) {
...@@ -86,21 +86,14 @@ OutboundGroupSession.prototype['encrypt'] = function(plaintext) { ...@@ -86,21 +86,14 @@ OutboundGroupSession.prototype['encrypt'] = function(plaintext) {
plaintext_buffer = malloc(plaintext_length + 1); plaintext_buffer = malloc(plaintext_length + 1);
stringToUTF8(plaintext, plaintext_buffer, plaintext_length + 1); stringToUTF8(plaintext, plaintext_buffer, plaintext_length + 1);
message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH); message_buffer = malloc(message_length);
outbound_group_session_method(Module['_olm_group_encrypt'])( outbound_group_session_method(Module['_olm_group_encrypt'])(
this.ptr, this.ptr,
plaintext_buffer, plaintext_length, plaintext_buffer, plaintext_length,
message_buffer, message_length message_buffer, message_length
); );
// UTF8ToString requires a null-terminated argument, so add the return UTF8ToString(message_buffer, message_length);
// null terminator.
setValue(
message_buffer+message_length,
0, "i8"
);
return UTF8ToString(message_buffer);
} finally { } finally {
if (plaintext_buffer !== undefined) { if (plaintext_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap. // don't leave a copy of the plaintext in the heap.
...@@ -117,22 +110,22 @@ OutboundGroupSession.prototype['session_id'] = restore_stack(function() { ...@@ -117,22 +110,22 @@ OutboundGroupSession.prototype['session_id'] = restore_stack(function() {
var length = outbound_group_session_method( var length = outbound_group_session_method(
Module['_olm_outbound_group_session_id_length'] Module['_olm_outbound_group_session_id_length']
)(this.ptr); )(this.ptr);
var session_id = stack(length + NULL_BYTE_PADDING_LENGTH); var session_id = stack(length);
outbound_group_session_method(Module['_olm_outbound_group_session_id'])( outbound_group_session_method(Module['_olm_outbound_group_session_id'])(
this.ptr, session_id, length this.ptr, session_id, length
); );
return Pointer_stringify(session_id); return UTF8ToString(session_id, length);
}); });
OutboundGroupSession.prototype['session_key'] = restore_stack(function() { OutboundGroupSession.prototype['session_key'] = restore_stack(function() {
var key_length = outbound_group_session_method( var key_length = outbound_group_session_method(
Module['_olm_outbound_group_session_key_length'] Module['_olm_outbound_group_session_key_length']
)(this.ptr); )(this.ptr);
var key = stack(key_length + NULL_BYTE_PADDING_LENGTH); var key = stack(key_length);
outbound_group_session_method(Module['_olm_outbound_group_session_key'])( outbound_group_session_method(Module['_olm_outbound_group_session_key'])(
this.ptr, key, key_length this.ptr, key, key_length
); );
var key_str = Pointer_stringify(key); var key_str = UTF8ToString(key, key_length);
bzero(key, key_length); // clear out our copy of the key bzero(key, key_length); // clear out our copy of the key
return key_str; return key_str;
}); });
......
...@@ -8,7 +8,7 @@ function pk_encryption_method(wrapped) { ...@@ -8,7 +8,7 @@ function pk_encryption_method(wrapped) {
return function() { return function() {
var result = wrapped.apply(this, arguments); var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) { if (result === OLM_ERROR) {
var message = Pointer_stringify( var message = UTF8ToString(
Module['_olm_pk_encryption_last_error'](arguments[0]) Module['_olm_pk_encryption_last_error'](arguments[0])
); );
throw new Error("OLM." + message); throw new Error("OLM." + message);
...@@ -45,11 +45,11 @@ PkEncryption.prototype['encrypt'] = restore_stack(function( ...@@ -45,11 +45,11 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
var ciphertext_length = pk_encryption_method( var ciphertext_length = pk_encryption_method(
Module['_olm_pk_ciphertext_length'] Module['_olm_pk_ciphertext_length']
)(this.ptr, plaintext_length); )(this.ptr, plaintext_length);
ciphertext_buffer = malloc(ciphertext_length + NULL_BYTE_PADDING_LENGTH); ciphertext_buffer = malloc(ciphertext_length);
var mac_length = pk_encryption_method( var mac_length = pk_encryption_method(
Module['_olm_pk_mac_length'] Module['_olm_pk_mac_length']
)(this.ptr); )(this.ptr);
var mac_buffer = stack(mac_length + NULL_BYTE_PADDING_LENGTH); var mac_buffer = stack(mac_length);
setValue( setValue(
mac_buffer + mac_length, mac_buffer + mac_length,
0, "i8" 0, "i8"
...@@ -57,7 +57,7 @@ PkEncryption.prototype['encrypt'] = restore_stack(function( ...@@ -57,7 +57,7 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
var ephemeral_length = pk_encryption_method( var ephemeral_length = pk_encryption_method(
Module['_olm_pk_key_length'] Module['_olm_pk_key_length']
)(); )();
var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH); var ephemeral_buffer = stack(ephemeral_length);
setValue( setValue(
ephemeral_buffer + ephemeral_length, ephemeral_buffer + ephemeral_length,
0, "i8" 0, "i8"
...@@ -70,16 +70,10 @@ PkEncryption.prototype['encrypt'] = restore_stack(function( ...@@ -70,16 +70,10 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
ephemeral_buffer, ephemeral_length, ephemeral_buffer, ephemeral_length,
random, random_length random, random_length
); );
// UTF8ToString requires a null-terminated argument, so add the
// null terminator.
setValue(
ciphertext_buffer + ciphertext_length,
0, "i8"
);
return { return {
"ciphertext": UTF8ToString(ciphertext_buffer), "ciphertext": UTF8ToString(ciphertext_buffer, ciphertext_length),
"mac": Pointer_stringify(mac_buffer), "mac": UTF8ToString(mac_buffer, mac_length),
"ephemeral": Pointer_stringify(ephemeral_buffer) "ephemeral": UTF8ToString(ephemeral_buffer, ephemeral_length)
}; };
} finally { } finally {
if (random !== undefined) { if (random !== undefined) {
...@@ -108,7 +102,7 @@ function pk_decryption_method(wrapped) { ...@@ -108,7 +102,7 @@ function pk_decryption_method(wrapped) {
return function() { return function() {
var result = wrapped.apply(this, arguments); var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) { if (result === OLM_ERROR) {
var message = Pointer_stringify( var message = UTF8ToString(
Module['_olm_pk_decryption_last_error'](arguments[0]) Module['_olm_pk_decryption_last_error'](arguments[0])
); );
throw new Error("OLM." + message); throw new Error("OLM." + message);
...@@ -129,7 +123,7 @@ PkDecryption.prototype['init_with_private_key'] = restore_stack(function (privat ...@@ -129,7 +123,7 @@ PkDecryption.prototype['init_with_private_key'] = restore_stack(function (privat
var pubkey_length = pk_decryption_method( var pubkey_length = pk_decryption_method(
Module['_olm_pk_key_length'] Module['_olm_pk_key_length']
)(); )();
var pubkey_buffer = stack(pubkey_length + NULL_BYTE_PADDING_LENGTH); var pubkey_buffer = stack(pubkey_length);
try { try {
pk_decryption_method(Module['_olm_pk_key_from_private'])( pk_decryption_method(Module['_olm_pk_key_from_private'])(
this.ptr, this.ptr,
...@@ -140,7 +134,7 @@ PkDecryption.prototype['init_with_private_key'] = restore_stack(function (privat ...@@ -140,7 +134,7 @@ PkDecryption.prototype['init_with_private_key'] = restore_stack(function (privat
// clear out our copy of the private key // clear out our copy of the private key
bzero(private_key_buffer, private_key.length); bzero(private_key_buffer, private_key.length);
} }
return Pointer_stringify(pubkey_buffer); return UTF8ToString(pubkey_buffer, pubkey_length);
}); });
PkDecryption.prototype['generate_key'] = restore_stack(function () { PkDecryption.prototype['generate_key'] = restore_stack(function () {
...@@ -151,7 +145,7 @@ PkDecryption.prototype['generate_key'] = restore_stack(function () { ...@@ -151,7 +145,7 @@ PkDecryption.prototype['generate_key'] = restore_stack(function () {
var pubkey_length = pk_decryption_method( var pubkey_length = pk_decryption_method(
Module['_olm_pk_key_length'] Module['_olm_pk_key_length']
)(); )();
var pubkey_buffer = stack(pubkey_length + NULL_BYTE_PADDING_LENGTH); var pubkey_buffer = stack(pubkey_length);
try { try {
pk_decryption_method(Module['_olm_pk_key_from_private'])( pk_decryption_method(Module['_olm_pk_key_from_private'])(
this.ptr, this.ptr,
...@@ -162,7 +156,7 @@ PkDecryption.prototype['generate_key'] = restore_stack(function () { ...@@ -162,7 +156,7 @@ PkDecryption.prototype['generate_key'] = restore_stack(function () {
// clear out the random buffer (= private key) // clear out the random buffer (= private key)
bzero(random_buffer, random_length); bzero(random_buffer, random_length);
} }
return Pointer_stringify(pubkey_buffer); return UTF8ToString(pubkey_buffer, pubkey_length);
}); });
PkDecryption.prototype['get_private_key'] = restore_stack(function () { PkDecryption.prototype['get_private_key'] = restore_stack(function () {
...@@ -190,7 +184,7 @@ PkDecryption.prototype['pickle'] = restore_stack(function (key) { ...@@ -190,7 +184,7 @@ PkDecryption.prototype['pickle'] = restore_stack(function (key) {
Module['_olm_pickle_pk_decryption_length'] Module['_olm_pickle_pk_decryption_length']
)(this.ptr); )(this.ptr);
var key_buffer = stack(key_array); var key_buffer = stack(key_array);
var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH); var pickle_buffer = stack(pickle_length);
try { try {
pk_decryption_method(Module['_olm_pickle_pk_decryption'])( pk_decryption_method(Module['_olm_pickle_pk_decryption'])(
this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
...@@ -202,7 +196,7 @@ PkDecryption.prototype['pickle'] = restore_stack(function (key) { ...@@ -202,7 +196,7 @@ PkDecryption.prototype['pickle'] = restore_stack(function (key) {
key_array[i] = 0; key_array[i] = 0;
} }
} }
return Pointer_stringify(pickle_buffer); return UTF8ToString(pickle_buffer, pickle_length);
}); });
PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) { PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) {
...@@ -213,7 +207,7 @@ PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) { ...@@ -213,7 +207,7 @@ PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) {
var ephemeral_length = pk_decryption_method( var ephemeral_length = pk_decryption_method(
Module["_olm_pk_key_length"] Module["_olm_pk_key_length"]
)(); )();
var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH); var ephemeral_buffer = stack(ephemeral_length);
try { try {
pk_decryption_method(Module['_olm_unpickle_pk_decryption'])( pk_decryption_method(Module['_olm_unpickle_pk_decryption'])(
this.ptr, key_buffer, key_array.length, pickle_buffer, this.ptr, key_buffer, key_array.length, pickle_buffer,
...@@ -226,7 +220,7 @@ PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) { ...@@ -226,7 +220,7 @@ PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) {
key_array[i] = 0; key_array[i] = 0;
} }
} }
return Pointer_stringify(ephemeral_buffer); return UTF8ToString(ephemeral_buffer, ephemeral_length);
}); });
PkDecryption.prototype['decrypt'] = restore_stack(function ( PkDecryption.prototype['decrypt'] = restore_stack(function (
...@@ -245,7 +239,7 @@ PkDecryption.prototype['decrypt'] = restore_stack(function ( ...@@ -245,7 +239,7 @@ PkDecryption.prototype['decrypt'] = restore_stack(function (
this.ptr, this.ptr,
ciphertext_length ciphertext_length
); );
plaintext_buffer = malloc(plaintext_max_length + NULL_BYTE_PADDING_LENGTH); plaintext_buffer = malloc(plaintext_max_length);
var plaintext_length = pk_decryption_method(Module['_olm_pk_decrypt'])( var plaintext_length = pk_decryption_method(Module['_olm_pk_decrypt'])(
this.ptr, this.ptr,
ephemeralkey_buffer, ephemeralkey_array.length, ephemeralkey_buffer, ephemeralkey_array.length,
...@@ -253,13 +247,7 @@ PkDecryption.prototype['decrypt'] = restore_stack(function ( ...@@ -253,13 +247,7 @@ PkDecryption.prototype['decrypt'] = restore_stack(function (
ciphertext_buffer, ciphertext_length, ciphertext_buffer, ciphertext_length,
plaintext_buffer, plaintext_max_length plaintext_buffer, plaintext_max_length
); );
// UTF8ToString requires a null-terminated argument, so add the return UTF8ToString(plaintext_buffer, plaintext_length);
// null terminator.
setValue(
plaintext_buffer + plaintext_length,
0, "i8"
);
return UTF8ToString(plaintext_buffer);
} finally { } finally {
if (plaintext_buffer !== undefined) { if (plaintext_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap. // don't leave a copy of the plaintext in the heap.
...@@ -283,7 +271,7 @@ function pk_signing_method(wrapped) { ...@@ -283,7 +271,7 @@ function pk_signing_method(wrapped) {
return function() { return function() {
var result = wrapped.apply(this, arguments); var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) { if (result === OLM_ERROR) {
var message = Pointer_stringify( var message = UTF8ToString(
Module['_olm_pk_signing_last_error'](arguments[0]) Module['_olm_pk_signing_last_error'](arguments[0])
); );
throw new Error("OLM." + message); throw new Error("OLM." + message);
...@@ -304,7 +292,7 @@ PkSigning.prototype['init_with_seed'] = restore_stack(function (seed) { ...@@ -304,7 +292,7 @@ PkSigning.prototype['init_with_seed'] = restore_stack(function (seed) {
var pubkey_length = pk_signing_method( var pubkey_length = pk_signing_method(
Module['_olm_pk_signing_public_key_length'] Module['_olm_pk_signing_public_key_length']
)(); )();
var pubkey_buffer = stack(pubkey_length + NULL_BYTE_PADDING_LENGTH); var pubkey_buffer = stack(pubkey_length);
try { try {
pk_signing_method(Module['_olm_pk_signing_key_from_seed'])( pk_signing_method(Module['_olm_pk_signing_key_from_seed'])(
this.ptr, this.ptr,
...@@ -315,7 +303,7 @@ PkSigning.prototype['init_with_seed'] = restore_stack(function (seed) { ...@@ -315,7 +303,7 @@ PkSigning.prototype['init_with_seed'] = restore_stack(function (seed) {
// clear out our copy of the seed // clear out our copy of the seed
bzero(seed_buffer, seed.length); bzero(seed_buffer, seed.length);
} }
return Pointer_stringify(pubkey_buffer); return UTF8ToString(pubkey_buffer, pubkey_length);
}); });
PkSigning.prototype['generate_seed'] = restore_stack(function () { PkSigning.prototype['generate_seed'] = restore_stack(function () {
...@@ -345,13 +333,13 @@ PkSigning.prototype['sign'] = restore_stack(function (message) { ...@@ -345,13 +333,13 @@ PkSigning.prototype['sign'] = restore_stack(function (message) {
var sig_length = pk_signing_method( var sig_length = pk_signing_method(
Module['_olm_pk_signature_length'] Module['_olm_pk_signature_length']
)(); )();
var sig_buffer = stack(sig_length + NULL_BYTE_PADDING_LENGTH); var sig_buffer = stack(sig_length);
pk_signing_method(Module['_olm_pk_sign'])( pk_signing_method(Module['_olm_pk_sign'])(
this.ptr, this.ptr,
message_buffer, message_length, message_buffer, message_length,
sig_buffer, sig_length sig_buffer, sig_length
); );
return Pointer_stringify(sig_buffer); return UTF8ToString(sig_buffer, sig_length);
} finally { } finally {
if (message_buffer !== undefined) { if (message_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap. // don't leave a copy of the plaintext in the heap.
......
...@@ -49,7 +49,7 @@ function account_method(wrapped) { ...@@ -49,7 +49,7 @@ function account_method(wrapped) {
return function() { return function() {
var result = wrapped.apply(this, arguments); var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) { if (result === OLM_ERROR) {
var message = Pointer_stringify( var message = UTF8ToString(
Module['_olm_account_last_error'](arguments[0]) Module['_olm_account_last_error'](arguments[0])
); );
throw new Error("OLM." + message); throw new Error("OLM." + message);
...@@ -77,11 +77,11 @@ Account.prototype['identity_keys'] = restore_stack(function() { ...@@ -77,11 +77,11 @@ Account.prototype['identity_keys'] = restore_stack(function() {
var keys_length = account_method( var keys_length = account_method(
Module['_olm_account_identity_keys_length'] Module['_olm_account_identity_keys_length']
)(this.ptr); )(this.ptr);
var keys = stack(keys_length + NULL_BYTE_PADDING_LENGTH); var keys = stack(keys_length);
account_method(Module['_olm_account_identity_keys'])( account_method(Module['_olm_account_identity_keys'])(
this.ptr, keys, keys_length this.ptr, keys, keys_length
); );
return Pointer_stringify(keys); return UTF8ToString(keys, keys_length);
}); });
Account.prototype['sign'] = restore_stack(function(message) { Account.prototype['sign'] = restore_stack(function(message) {
...@@ -90,7 +90,7 @@ Account.prototype['sign'] = restore_stack(function(message) { ...@@ -90,7 +90,7 @@ Account.prototype['sign'] = restore_stack(function(message) {
)(this.ptr); )(this.ptr);
var message_array = array_from_string(message); var message_array = array_from_string(message);
var message_buffer = stack(message_array); var message_buffer = stack(message_array);
var signature_buffer = stack(signature_length + NULL_BYTE_PADDING_LENGTH); var signature_buffer = stack(signature_length);
try { try {
account_method(Module['_olm_account_sign'])( account_method(Module['_olm_account_sign'])(
this.ptr, this.ptr,
...@@ -104,18 +104,18 @@ Account.prototype['sign'] = restore_stack(function(message) { ...@@ -104,18 +104,18 @@ Account.prototype['sign'] = restore_stack(function(message) {
message_array[i] = 0; message_array[i] = 0;
} }
} }
return Pointer_stringify(signature_buffer); return UTF8ToString(signature_buffer, signature_length);
}); });
Account.prototype['one_time_keys'] = restore_stack(function() { Account.prototype['one_time_keys'] = restore_stack(function() {
var keys_length = account_method( var keys_length = account_method(
Module['_olm_account_one_time_keys_length'] Module['_olm_account_one_time_keys_length']
)(this.ptr); )(this.ptr);
var keys = stack(keys_length + NULL_BYTE_PADDING_LENGTH); var keys = stack(keys_length);