aboutsummaryrefslogtreecommitdiff
path: root/javascript/olm_pk.js
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2019-04-08 15:54:02 -0400
committerHubert Chathi <hubert@uhoreg.ca>2019-04-08 15:54:02 -0400
commitebc156e7c272e08e375c3e704651b179541e078b (patch)
treeca421f9d429f009e1c939bf96314fd4764ae241b /javascript/olm_pk.js
parent071029c20174307de28ca20232196f83fcc37763 (diff)
re-add null termination in javascript
because older versions of emscripten don't support the length argument to UTF8ToString.
Diffstat (limited to 'javascript/olm_pk.js')
-rw-r--r--javascript/olm_pk.js32
1 files changed, 22 insertions, 10 deletions
diff --git a/javascript/olm_pk.js b/javascript/olm_pk.js
index 8e99494..4690b90 100644
--- a/javascript/olm_pk.js
+++ b/javascript/olm_pk.js
@@ -45,11 +45,11 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
var ciphertext_length = pk_encryption_method(
Module['_olm_pk_ciphertext_length']
)(this.ptr, plaintext_length);
- ciphertext_buffer = malloc(ciphertext_length);
+ ciphertext_buffer = malloc(ciphertext_length + NULL_BYTE_PADDING_LENGTH);
var mac_length = pk_encryption_method(
Module['_olm_pk_mac_length']
)(this.ptr);
- var mac_buffer = stack(mac_length);
+ var mac_buffer = stack(mac_length + NULL_BYTE_PADDING_LENGTH);
setValue(
mac_buffer + mac_length,
0, "i8"
@@ -57,7 +57,7 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
var ephemeral_length = pk_encryption_method(
Module['_olm_pk_key_length']
)();
- var ephemeral_buffer = stack(ephemeral_length);
+ var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH);
setValue(
ephemeral_buffer + ephemeral_length,
0, "i8"
@@ -70,6 +70,12 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
ephemeral_buffer, ephemeral_length,
random, random_length
);
+ // UTF8ToString requires a null-terminated argument, so add the
+ // null terminator.
+ setValue(
+ ciphertext_buffer + ciphertext_length,
+ 0, "i8"
+ );
return {
"ciphertext": UTF8ToString(ciphertext_buffer, ciphertext_length),
"mac": UTF8ToString(mac_buffer, mac_length),
@@ -123,7 +129,7 @@ PkDecryption.prototype['init_with_private_key'] = restore_stack(function (privat
var pubkey_length = pk_decryption_method(
Module['_olm_pk_key_length']
)();
- var pubkey_buffer = stack(pubkey_length);
+ var pubkey_buffer = stack(pubkey_length + NULL_BYTE_PADDING_LENGTH);
try {
pk_decryption_method(Module['_olm_pk_key_from_private'])(
this.ptr,
@@ -145,7 +151,7 @@ PkDecryption.prototype['generate_key'] = restore_stack(function () {
var pubkey_length = pk_decryption_method(
Module['_olm_pk_key_length']
)();
- var pubkey_buffer = stack(pubkey_length);
+ var pubkey_buffer = stack(pubkey_length + NULL_BYTE_PADDING_LENGTH);
try {
pk_decryption_method(Module['_olm_pk_key_from_private'])(
this.ptr,
@@ -184,7 +190,7 @@ PkDecryption.prototype['pickle'] = restore_stack(function (key) {
Module['_olm_pickle_pk_decryption_length']
)(this.ptr);
var key_buffer = stack(key_array);
- var pickle_buffer = stack(pickle_length);
+ var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH);
try {
pk_decryption_method(Module['_olm_pickle_pk_decryption'])(
this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
@@ -207,7 +213,7 @@ PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) {
var ephemeral_length = pk_decryption_method(
Module["_olm_pk_key_length"]
)();
- var ephemeral_buffer = stack(ephemeral_length);
+ var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH);
try {
pk_decryption_method(Module['_olm_unpickle_pk_decryption'])(
this.ptr, key_buffer, key_array.length, pickle_buffer,
@@ -239,7 +245,7 @@ PkDecryption.prototype['decrypt'] = restore_stack(function (
this.ptr,
ciphertext_length
);
- plaintext_buffer = malloc(plaintext_max_length);
+ plaintext_buffer = malloc(plaintext_max_length + NULL_BYTE_PADDING_LENGTH);
var plaintext_length = pk_decryption_method(Module['_olm_pk_decrypt'])(
this.ptr,
ephemeralkey_buffer, ephemeralkey_array.length,
@@ -247,6 +253,12 @@ PkDecryption.prototype['decrypt'] = restore_stack(function (
ciphertext_buffer, ciphertext_length,
plaintext_buffer, plaintext_max_length
);
+ // UTF8ToString requires a null-terminated argument, so add the
+ // null terminator.
+ setValue(
+ plaintext_buffer + plaintext_length,
+ 0, "i8"
+ );
return UTF8ToString(plaintext_buffer, plaintext_length);
} finally {
if (plaintext_buffer !== undefined) {
@@ -292,7 +304,7 @@ PkSigning.prototype['init_with_seed'] = restore_stack(function (seed) {
var pubkey_length = pk_signing_method(
Module['_olm_pk_signing_public_key_length']
)();
- var pubkey_buffer = stack(pubkey_length);
+ var pubkey_buffer = stack(pubkey_length + NULL_BYTE_PADDING_LENGTH);
try {
pk_signing_method(Module['_olm_pk_signing_key_from_seed'])(
this.ptr,
@@ -333,7 +345,7 @@ PkSigning.prototype['sign'] = restore_stack(function (message) {
var sig_length = pk_signing_method(
Module['_olm_pk_signature_length']
)();
- var sig_buffer = stack(sig_length);
+ var sig_buffer = stack(sig_length + NULL_BYTE_PADDING_LENGTH);
pk_signing_method(Module['_olm_pk_sign'])(
this.ptr,
message_buffer, message_length,