aboutsummaryrefslogtreecommitdiff
path: root/javascript/olm_inbound_group_session.js
blob: dd8e4934c1e81b3d21018d6af2d1e77394f90b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function InboundGroupSession() {
    var size = Module['_olm_inbound_group_session_size']();
    this.buf = malloc(size);
    this.ptr = Module['_olm_inbound_group_session'](this.buf);
}

function inbound_group_session_method(wrapped) {
    return function() {
        var result = wrapped.apply(this, arguments);
        if (result === OLM_ERROR) {
            var message = Pointer_stringify(
                Module['_olm_inbound_group_session_last_error'](arguments[0])
            );
            throw new Error("OLM." + message);
        }
        return result;
    }
}

InboundGroupSession.prototype['free'] = function() {
    Module['_olm_clear_inbound_group_session'](this.ptr);
    free(this.ptr);
}

InboundGroupSession.prototype['pickle'] = restore_stack(function(key) {
    var key_array = array_from_string(key);
    var pickle_length = inbound_group_session_method(
        Module['_olm_pickle_inbound_group_session_length']
    )(this.ptr);
    var key_buffer = stack(key_array);
    var pickle_buffer = stack(pickle_length + NULL_BYTE_PADDING_LENGTH);
    try {
        inbound_group_session_method(Module['_olm_pickle_inbound_group_session'])(
            this.ptr, key_buffer, key_array.length, pickle_buffer, pickle_length
        );
    } finally {
        // clear out copies of the pickle key
        bzero(key_buffer, key_array.length)
        for (var i = 0; i < key_array.length; i++) {
            key_array[i] = 0;
        }
    }
    return Pointer_stringify(pickle_buffer);
});

InboundGroupSession.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);
    try {
        inbound_group_session_method(Module['_olm_unpickle_inbound_group_session'])(
            this.ptr, key_buffer, key_array.length, pickle_buffer,
            pickle_array.length
        );
    } finally {
        // clear out copies of the pickle key
        bzero(key_buffer, key_array.length)
        for (var i = 0; i < key_array.length; i++) {
            key_array[i] = 0;
        }
    }
});

InboundGroupSession.prototype['create'] = restore_stack(function(session_key) {
    var key_array = array_from_string(session_key);
    var key_buffer = stack(key_array);

    try {
        inbound_group_session_method(Module['_olm_init_inbound_group_session'])(
            this.ptr, key_buffer, key_array.length
        );
    } finally {
        // clear out copies of the key
        bzero(key_buffer, key_array.length)
        for (var i = 0; i < key_array.length; i++) {
            key_array[i] = 0;
        }
    }
});

InboundGroupSession.prototype['import_session'] = restore_stack(function(session_key) {
    var key_array = array_from_string(session_key);
    var key_buffer = stack(key_array);

    try {
        inbound_group_session_method(Module['_olm_import_inbound_group_session'])(
            this.ptr, key_buffer, key_array.length
        );
    } finally {
        // clear out copies of the key
        bzero(key_buffer, key_array.length)
        for (var i = 0; i < key_array.length; i++) {
            key_array[i] = 0;
        }
    }
});

InboundGroupSession.prototype['decrypt'] = restore_stack(function(
    message
) {
    var message_buffer, plaintext_buffer, plaintext_length;

    try {
        message_buffer = malloc(message.length);
        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.
        writeAsciiToMemory(message, message_buffer, true);

        plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
        var message_index = stack(4);

        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.
        setValue(
            plaintext_buffer+plaintext_length,
            0, "i8"
        );

        return {
            "plaintext": UTF8ToString(plaintext_buffer),
            "message_index": 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);
        }
    }
});

InboundGroupSession.prototype['session_id'] = restore_stack(function() {
    var length = inbound_group_session_method(
        Module['_olm_inbound_group_session_id_length']
    )(this.ptr);
    var session_id = stack(length + NULL_BYTE_PADDING_LENGTH);
    inbound_group_session_method(Module['_olm_inbound_group_session_id'])(
        this.ptr, session_id, length
    );
    return Pointer_stringify(session_id);
});

InboundGroupSession.prototype['first_known_index'] = restore_stack(function() {
    return inbound_group_session_method(
        Module['_olm_inbound_group_session_first_known_index']
    )(this.ptr);
});

InboundGroupSession.prototype['export_session'] = restore_stack(function(message_index) {
    var key_length = inbound_group_session_method(
        Module['_olm_export_inbound_group_session_length']
    )(this.ptr);
    var key = stack(key_length + NULL_BYTE_PADDING_LENGTH);
    outbound_group_session_method(Module['_olm_export_inbound_group_session'])(
        this.ptr, key, key_length, message_index
    );
    var key_str = Pointer_stringify(key);
    bzero(key, key_length); // clear out a copy of the key
    return key_str;
});

olm_exports['InboundGroupSession'] = InboundGroupSession;