aboutsummaryrefslogtreecommitdiff
path: root/javascript/olm_pk.js
diff options
context:
space:
mode:
authorDamir Jelić <poljar@termina.org.uk>2019-04-01 17:40:43 +0200
committerHubert Chathi <hubert@uhoreg.ca>2019-04-08 15:18:28 -0400
commit071029c20174307de28ca20232196f83fcc37763 (patch)
tree272e901d2bfcf0efe6be018caed3a3a06d54a815 /javascript/olm_pk.js
parent709687a7b56d6768831766459940b6f0bb078d85 (diff)
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.
Diffstat (limited to 'javascript/olm_pk.js')
-rw-r--r--javascript/olm_pk.js58
1 files changed, 23 insertions, 35 deletions
diff --git a/javascript/olm_pk.js b/javascript/olm_pk.js
index 2cbe44d..8e99494 100644
--- a/javascript/olm_pk.js
+++ b/javascript/olm_pk.js
@@ -8,7 +8,7 @@ function pk_encryption_method(wrapped) {
return function() {
var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) {
- var message = Pointer_stringify(
+ var message = UTF8ToString(
Module['_olm_pk_encryption_last_error'](arguments[0])
);
throw new Error("OLM." + message);
@@ -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 + NULL_BYTE_PADDING_LENGTH);
+ ciphertext_buffer = malloc(ciphertext_length);
var mac_length = pk_encryption_method(
Module['_olm_pk_mac_length']
)(this.ptr);
- var mac_buffer = stack(mac_length + NULL_BYTE_PADDING_LENGTH);
+ var mac_buffer = stack(mac_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 + NULL_BYTE_PADDING_LENGTH);
+ var ephemeral_buffer = stack(ephemeral_length);
setValue(
ephemeral_buffer + ephemeral_length,
0, "i8"
@@ -70,16 +70,10 @@ 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),
- "mac": Pointer_stringify(mac_buffer),
- "ephemeral": Pointer_stringify(ephemeral_buffer)
+ "ciphertext": UTF8ToString(ciphertext_buffer, ciphertext_length),
+ "mac": UTF8ToString(mac_buffer, mac_length),
+ "ephemeral": UTF8ToString(ephemeral_buffer, ephemeral_length)
};
} finally {
if (random !== undefined) {
@@ -108,7 +102,7 @@ function pk_decryption_method(wrapped) {
return function() {
var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) {
- var message = Pointer_stringify(
+ var message = UTF8ToString(
Module['_olm_pk_decryption_last_error'](arguments[0])
);
throw new Error("OLM." + message);
@@ -129,7 +123,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 + NULL_BYTE_PADDING_LENGTH);
+ var pubkey_buffer = stack(pubkey_length);
try {
pk_decryption_method(Module['_olm_pk_key_from_private'])(
this.ptr,
@@ -140,7 +134,7 @@ PkDecryption.prototype['init_with_private_key'] = restore_stack(function (privat
// clear out our copy of the private key
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 () {
@@ -151,7 +145,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 + NULL_BYTE_PADDING_LENGTH);
+ var pubkey_buffer = stack(pubkey_length);
try {
pk_decryption_method(Module['_olm_pk_key_from_private'])(
this.ptr,
@@ -162,7 +156,7 @@ PkDecryption.prototype['generate_key'] = restore_stack(function () {
// clear out the random buffer (= private key)
bzero(random_buffer, random_length);
}
- return Pointer_stringify(pubkey_buffer);
+ return UTF8ToString(pubkey_buffer, pubkey_length);
});
PkDecryption.prototype['get_private_key'] = restore_stack(function () {
@@ -190,7 +184,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 + NULL_BYTE_PADDING_LENGTH);
+ var pickle_buffer = stack(pickle_length);
try {
pk_decryption_method(Module['_olm_pickle_pk_decryption'])(
this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
@@ -202,7 +196,7 @@ PkDecryption.prototype['pickle'] = restore_stack(function (key) {
key_array[i] = 0;
}
}
- return Pointer_stringify(pickle_buffer);
+ return UTF8ToString(pickle_buffer, pickle_length);
});
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(
Module["_olm_pk_key_length"]
)();
- var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH);
+ var ephemeral_buffer = stack(ephemeral_length);
try {
pk_decryption_method(Module['_olm_unpickle_pk_decryption'])(
this.ptr, key_buffer, key_array.length, pickle_buffer,
@@ -226,7 +220,7 @@ PkDecryption.prototype['unpickle'] = restore_stack(function (key, pickle) {
key_array[i] = 0;
}
}
- return Pointer_stringify(ephemeral_buffer);
+ return UTF8ToString(ephemeral_buffer, ephemeral_length);
});
PkDecryption.prototype['decrypt'] = restore_stack(function (
@@ -245,7 +239,7 @@ PkDecryption.prototype['decrypt'] = restore_stack(function (
this.ptr,
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'])(
this.ptr,
ephemeralkey_buffer, ephemeralkey_array.length,
@@ -253,13 +247,7 @@ 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);
+ return UTF8ToString(plaintext_buffer, plaintext_length);
} finally {
if (plaintext_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap.
@@ -283,7 +271,7 @@ function pk_signing_method(wrapped) {
return function() {
var result = wrapped.apply(this, arguments);
if (result === OLM_ERROR) {
- var message = Pointer_stringify(
+ var message = UTF8ToString(
Module['_olm_pk_signing_last_error'](arguments[0])
);
throw new Error("OLM." + message);
@@ -304,7 +292,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 + NULL_BYTE_PADDING_LENGTH);
+ var pubkey_buffer = stack(pubkey_length);
try {
pk_signing_method(Module['_olm_pk_signing_key_from_seed'])(
this.ptr,
@@ -315,7 +303,7 @@ PkSigning.prototype['init_with_seed'] = restore_stack(function (seed) {
// clear out our copy of the seed
bzero(seed_buffer, seed.length);
}
- return Pointer_stringify(pubkey_buffer);
+ return UTF8ToString(pubkey_buffer, pubkey_length);
});
PkSigning.prototype['generate_seed'] = restore_stack(function () {
@@ -345,13 +333,13 @@ PkSigning.prototype['sign'] = restore_stack(function (message) {
var sig_length = pk_signing_method(
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'])(
this.ptr,
message_buffer, message_length,
sig_buffer, sig_length
);
- return Pointer_stringify(sig_buffer);
+ return UTF8ToString(sig_buffer, sig_length);
} finally {
if (message_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap.