olm_sas.cpp 9.55 KB
Newer Older
Valere's avatar
Valere committed
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright 2019 New Vector Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "olm_sas.h"

#include "olm/olm.h"

using namespace AndroidOlmSdk;

JNIEXPORT jlong OLM_SAS_FUNC_DEF(createNewSASJni)(JNIEnv *env, jobject thiz)
{
    
    size_t sasSize = olm_sas_size();
    OlmSAS *sasPtr = (OlmSAS *) malloc(sasSize);
    const char* errorMessage = NULL;

    if (!sasPtr)
    {
        LOGE("## createNewSASJni(): failure - init SAS OOM");
        env->ThrowNew(env->FindClass("java/lang/Exception"), "init sas OOM");
    }
    else
    {
        sasPtr = olm_sas(sasPtr)
        LOGD(" ## createNewSASJni(): success - sasPtr=%p (jlong)(intptr_t)accountPtr=%lld",sasPtr,(jlong)(intptr_t)sasPtr);
    }

    size_t randomSize = olm_create_sas_random_length(sasPtr);
    uint8_t *randomBuffPtr = NULL;

    LOGD("## createNewSASJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize));

    if ( (0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
    {
        LOGE("## createNewSASJni(): failure - random buffer init");
        errorMessage = "Failed to init private key";
    }
    else
    {
        size_t result = olm_create_sas(sasPtr, randomBuffPtr, randomSize);
        if (result == olm_error())
        {
            errorMessage = (const char *)olm_sas_last_error(sasPtr);
            LOGE("## createNewSASJni(): failure - error creating SAS Msg=%s", errorMessage);
        }
    }

    if (randomBuffPtr)
    {
        memset(randomBuffPtr, 0, randomSize);
        free(randomBuffPtr);
    }

    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

    return (jlong)(intptr_t)sasPtr;
}

JNIEXPORT void OLM_SAS_FUNC_DEF(releaseSASJni)(JNIEnv *env, jobject thiz)
{
    LOGD("## releaseSASJni(): IN");
    OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz);

    if (!sasPtr)
    {
        LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL");
    }
    else
    {
        olm_clear_sas(sasPtr);
        // even if free(NULL) does not crash, logs are performed for debug purpose
        free(sasPtr);
    }
}


JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(getPubKeyJni)(JNIEnv *env, jobject thiz) 
{
    LOGD("## getPubKeyJni(): IN");
    const char* errorMessage = NULL;
    jbyteArray returnValue = 0;
    OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz);

    if (!sasPtr)
    {
        LOGE("## getPubKeyJni(): failure - invalid SAS ptr=NULL");
        errorMessage = "invalid SAS ptr=NULL";
    }
    else
    {
        size_t pubKeyLength = olm_sas_pubkey_length(sasPtr);
        void *pubkey = malloc(pubKeyLength*sizeof(uint8_t));
        size_t result = olm_sas_get_pubkey(sasPtr, pubkey, pubKeyLength);
        if (result == olm_error())
        {
            errorMessage = (const char *)olm_sas_last_error(sasPtr);
            LOGE("## getPubKeyJni(): failure - error getting pub key Msg=%s", errorMessage);
        }
        else
        {
            returnValue = env->NewByteArray(pubKeyLength);
            env->SetByteArrayRegion(returnValue, 0 , pubKeyLength, (jbyte*)pubkey);
        }
        if (pubkey) {
            free(pubkey);
        }
    }

    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

    return returnValue;
}

JNIEXPORT void OLM_SAS_FUNC_DEF(setTheirPubKey)(JNIEnv *env, jobject thiz,jbyteArray pubKeyBuffer) {
    
    OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz);

    const char* errorMessage = NULL;
    jbyte *pubKeyPtr = NULL;
    jboolean pubKeyWasCopied = JNI_FALSE;

    if (!sasPtr)
    {
        LOGE("## setTheirPubKey(): failure - invalid SAS ptr=NULL");
        errorMessage = "invalid SAS ptr=NULL";
    } else if(!pubKeyBuffer) {
        LOGE("## setTheirPubKey(): failure - invalid info");
        errorMessage = "invalid pubKey";
    }
    else if (!(pubKeyPtr = env->GetByteArrayElements(pubKeyBuffer, &pubKeyWasCopied)))
    {
        LOGE(" ## generateShortCodeJni(): failure - info JNI allocation OOM");
        errorMessage = "info JNI allocation OOM";
    }
    else 
    {
        size_t pubKeyLength = (size_t)env->GetArrayLength(pubKeyBuffer);
        size_t result = olm_sas_set_their_key(sasPtr,pubKeyPtr,pubKeyLength);
        if (result == olm_error())
        {
            errorMessage = (const char *)olm_sas_last_error(sasPtr);
            LOGE("## setTheirPubKey(): failure - error setting their key Msg=%s", errorMessage);
        }
    }
    // free alloc
    if (pubKeyPtr)
    {
        if (pubKeyWasCopied)
        {
            memset(pubKeyPtr, 0, (size_t)env->GetArrayLength(pubKeyBuffer));
        }
        env->ReleaseByteArrayElements(pubKeyBuffer, pubKeyPtr, JNI_ABORT);
    }

    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

}

JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(generateShortCodeJni)(JNIEnv *env, jobject thiz, jbyteArray infoStringBytes, jint byteNb) {
    LOGD("## generateShortCodeJni(): IN");
    const char* errorMessage = NULL;
    jbyteArray returnValue = 0;
    OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz);

    jbyte *infoPtr = NULL;
    jboolean infoWasCopied = JNI_FALSE;

    if (!sasPtr)
    {
        LOGE("## generateShortCodeJni(): failure - invalid SAS ptr=NULL");
        errorMessage = "invalid SAS ptr=NULL";
    } else if(!infoStringBytes) {
        LOGE("## generateShortCodeJni(): failure - invalid info");
        errorMessage = "invalid info";
    }
    else if (!(infoPtr = env->GetByteArrayElements(infoStringBytes, &infoWasCopied)))
    {
        LOGE(" ## generateShortCodeJni(): failure - info JNI allocation OOM");
        errorMessage = "info JNI allocation OOM";
    }
    else {
        size_t shortBytesCodeLength = (size_t) byteNb;
        void *shortBytesCode = malloc(shortBytesCodeLength * sizeof(uint8_t));
        size_t infoLength = (size_t)env->GetArrayLength(infoStringBytes);
        olm_sas_generate_bytes(sasPtr, infoPtr, infoLength, shortBytesCode, shortBytesCodeLength);
        returnValue = env->NewByteArray(shortBytesCodeLength);
        env->SetByteArrayRegion(returnValue, 0 , shortBytesCodeLength, (jbyte*)shortBytesCode);
        free(shortBytesCode);
    }

    // free alloc
    if (infoPtr)
    {
        if (infoWasCopied)
        {
            memset(infoPtr, 0, (size_t)env->GetArrayLength(infoStringBytes));
        }
        env->ReleaseByteArrayElements(infoStringBytes, infoPtr, JNI_ABORT);
    }

    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

    return returnValue;
}


JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(calculateMacJni)(JNIEnv *env, jobject thiz,jbyteArray messageBuffer,jbyteArray infoBuffer) {
     LOGD("## calculateMacJni(): IN");
    const char* errorMessage = NULL;
    jbyteArray returnValue = 0;
    OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz);

    jbyte *messagePtr = NULL;
    jboolean messageWasCopied = JNI_FALSE;

    jbyte *infoPtr = NULL;
    jboolean infoWasCopied = JNI_FALSE;

    if (!sasPtr)
    {
        LOGE("## calculateMacJni(): failure - invalid SAS ptr=NULL");
        errorMessage = "invalid SAS ptr=NULL";
    } else if(!messageBuffer) {
        LOGE("## calculateMacJni(): failure - invalid message");
        errorMessage = "invalid info";
    }
    else if (!(messagePtr = env->GetByteArrayElements(messageBuffer, &messageWasCopied)))
    {
        LOGE(" ## calculateMacJni(): failure - message JNI allocation OOM");
        errorMessage = "message JNI allocation OOM";
    }
    else if (!(infoPtr = env->GetByteArrayElements(infoBuffer, &infoWasCopied)))
    {
        LOGE(" ## calculateMacJni(): failure - info JNI allocation OOM");
        errorMessage = "info JNI allocation OOM";
    } else {

        size_t infoLength = (size_t)env->GetArrayLength(infoBuffer);
        size_t messageLength = (size_t)env->GetArrayLength(messageBuffer);
        size_t macLength = olm_sas_mac_length(sasPtr);

        void *macPtr = malloc(macLength*sizeof(uint8_t));

        size_t result = olm_sas_calculate_mac(sasPtr,messagePtr,messageLength,infoPtr,infoLength,macPtr,macLength);
        if (result == olm_error())
        {
            errorMessage = (const char *)olm_sas_last_error(sasPtr);
            LOGE("## calculateMacJni(): failure - error calculating SAS mac Msg=%s", errorMessage);
        }
        else
        {
            returnValue = env->NewByteArray(macLength);
            env->SetByteArrayRegion(returnValue, 0 , macLength, (jbyte*)macPtr);
        }

        if (macPtr) {
            free(macPtr);
        }
    }

    // free alloc
    if (infoPtr)
    {
        if (infoWasCopied)
        {
            memset(infoPtr, 0, (size_t)env->GetArrayLength(infoBuffer));
        }
        env->ReleaseByteArrayElements(infoBuffer, infoPtr, JNI_ABORT);
    }
    if (messagePtr)
    {
        if (messageWasCopied)
        {
            memset(messagePtr, 0, (size_t)env->GetArrayLength(messageBuffer));
        }
        env->ReleaseByteArrayElements(messageBuffer, messagePtr, JNI_ABORT);
    }

    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

    return returnValue;
}