olm_outbound_group_session.cpp 18.8 KB
Newer Older
1
2
/*
 * Copyright 2016 OpenMarket Ltd
ylecollen's avatar
ylecollen committed
3
 * Copyright 2016 Vector Creations Ltd
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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_outbound_group_session.h"

pedroGitt's avatar
pedroGitt committed
20
using namespace AndroidOlmSdk;
21
22
23
24
25
26
27
28

/**
 * Release the session allocation made by initializeOutboundGroupSessionMemory().<br>
 * This method MUST be called when java counter part account instance is done.
 *
 */
JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz)
{
29
    LOGD("## releaseSessionJni(): OutBound group session IN");
30

31
    OlmOutboundGroupSession* sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
32

33
34
35
36
37
38
39
    if (!sessionPtr)
    {
        LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance");
    }
    else
    {
        LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr);
40

41
#ifdef ENABLE_JNI_LOG
42
43
        size_t retCode = olm_clear_outbound_group_session(sessionPtr);
        LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",static_cast<long unsigned int>(retCode));
44
#else
45
        olm_clear_outbound_group_session(sessionPtr);
46
#endif
47

48
49
50
51
        LOGD(" ## releaseSessionJni(): free IN");
        free(sessionPtr);
        LOGD(" ## releaseSessionJni(): free OUT");
    }
52
53
54
55
56
57
58
59
}

/**
* Initialize a new outbound group session and return it to JAVA side.<br>
* Since a C prt is returned as a jlong, special care will be taken
* to make the cast (OlmOutboundGroupSession* => jlong) platform independent.
* @return the initialized OlmOutboundGroupSession* instance if init succeed, NULL otherwise
**/
60
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
61
62
{
    OlmOutboundGroupSession* sessionPtr = NULL;
63
64
    size_t sessionSize = 0;

65
    LOGD("## createNewSessionJni(): outbound group session IN");
66
    sessionSize = olm_outbound_group_session_size();
67

68
    if (0 == sessionSize)
69
    {
70
        LOGE(" ## createNewSessionJni(): failure - outbound group session size = 0");
71
    }
ylecollen's avatar
ylecollen committed
72
    else if ((sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize)))
73
    {
74
75
        sessionPtr = olm_outbound_group_session(sessionPtr);
        LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",static_cast<long unsigned int>(sessionSize));
76
77
78
    }
    else
    {
79
        LOGE(" ## createNewSessionJni(): failure - outbound group session OOM");
80
81
82
83
84
85
    }

    return (jlong)(intptr_t)sessionPtr;
}

/**
86
 * Start a new outbound session.<br>
87
 */
88
JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz)
89
{
90
    const char* errorMessage = NULL;
91
92

    LOGD("## initOutboundGroupSessionJni(): IN");
93

94
95
96
    OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);

    if (!sessionPtr)
97
    {
98
        LOGE(" ## initOutboundGroupSessionJni(): failure - invalid outbound group session instance");
99
        errorMessage = "invalid outbound group session instance";
100
101
102
103
104
    }
    else
    {
        // compute random buffer
        size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr);
105
106
        uint8_t *randomBuffPtr = NULL;

pedroGitt's avatar
pedroGitt committed
107
        LOGW(" ## initOutboundGroupSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength));
108
109

        if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
110
        {
111
            LOGE(" ## initOutboundGroupSessionJni(): failure - random buffer init");
112
            errorMessage = "random buffer init";
113
114
115
        }
        else
        {
116
            if (0 == randomLength)
117
            {
118
                LOGW(" ## initOutboundGroupSessionJni(): random buffer is not required");
119
120
            }

121
            size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength);
122
123

            if (sessionResult == olm_error()) {
124
125
                errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr);
                LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation  Msg=%s", errorMessage);
126
127
128
            }
            else
            {
pedroGitt's avatar
pedroGitt committed
129
                LOGD(" ## initOutboundGroupSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
130
131
            }

132
133
            free(randomBuffPtr);
        }
134
    }
135

136
137
138
139
    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }
140
141
142
143
144
}

/**
* Get a base64-encoded identifier for this outbound group session.
*/
145
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
146
147
148
{
    LOGD("## sessionIdentifierJni(): outbound group session IN");

149
    const char* errorMessage = NULL;
150
151
152
    OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
    jbyteArray returnValue = 0;
    
153
    if (!sessionPtr)
154
    {
155
        LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance");
156
        errorMessage = "invalid outbound group session instance";
157
158
159
    }
    else
    {
160
161
        // get the size to alloc
        size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr);
pedroGitt's avatar
pedroGitt committed
162
        LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
163

164
165
166
        uint8_t *sessionIdPtr =  (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t));

        if (!sessionIdPtr)
167
        {
168
           LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM");
169
           errorMessage = "outbound identifier allocation OOM";
170
171
172
        }
        else
        {
173
            size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
174

175
176
            if (result == olm_error())
            {
177
178
                errorMessage = reinterpret_cast<const char*>(olm_outbound_group_session_last_error(sessionPtr));
                LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s", errorMessage);
179
180
181
182
183
            }
            else
            {
                // update length
                sessionIdPtr[result] = static_cast<char>('\0');
184
185
186
187

                returnValue = env->NewByteArray(result);
                env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr);

pedroGitt's avatar
pedroGitt committed
188
                LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(sessionIdPtr));
189
            }
190

191
192
193
194
            // free alloc
            free(sessionIdPtr);
        }
    }
195

196
197
198
199
200
    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

201
    return returnValue;
202
203
204
}


205
206
207
208
209
210
211
/**
* Get the current message index for this session.<br>
* Each message is sent with an increasing index, this
* method returns the index for the next message.
* @return current session index
*/
JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz)
212
{
213
214
    OlmOutboundGroupSession *sessionPtr = NULL;
    jint indexRetValue = 0;
215

216
    LOGD("## messageIndexJni(): IN");
217

218
    if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
219
    {
220
        LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance");
221
222
    }
    else
223
    {
224
        indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr));
225
    }
226

227
    LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue);
228
229
230
231
232
233
234

    return indexRetValue;
}

/**
* Get the base64-encoded current ratchet key for this session.<br>
*/
235
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz)
236
237
{
    LOGD("## sessionKeyJni(): outbound group session IN");
238

239
    const char* errorMessage = NULL;
240
    OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
241
    jbyteArray returnValue = 0;
242

243
    if (!sessionPtr)
244
    {
245
        LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance");
246
        errorMessage = "invalid outbound group session instance";
247
248
249
    }
    else
    {
250
251
        // get the size to alloc
        size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr);
pedroGitt's avatar
pedroGitt committed
252
        LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength));
253

254
255
256
        uint8_t *sessionKeyPtr = (uint8_t*)malloc((sessionKeyLength+1)*sizeof(uint8_t));

        if (!sessionKeyPtr)
257
        {
258
           LOGE(" ## sessionKeyJni(): failure - session key allocation OOM");
259
           errorMessage = "session key allocation OOM";
260
261
262
        }
        else
        {
263
            size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength);
264

265
266
            if (result == olm_error())
            {
267
268
                errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr);
                LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s", errorMessage);
269
270
271
272
273
            }
            else
            {
                // update length
                sessionKeyPtr[result] = static_cast<char>('\0');
pedroGitt's avatar
pedroGitt committed
274
                LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(sessionKeyPtr));
275
276
277

                returnValue = env->NewByteArray(result);
                env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionKeyPtr);
278
            }
279

280
281
            // free alloc
            free(sessionKeyPtr);
282
283
284
        }
    }

285
286
287
288
289
    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

290
    return returnValue;
291
292
}

293
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer)
294
{
295
296
    LOGD("## encryptMessageJni(): IN");

297
    const char* errorMessage = NULL;
298
299
    jbyteArray encryptedMsgRet = 0;

300
    OlmOutboundGroupSession *sessionPtr = NULL;
301
    jbyte* clearMsgPtr = NULL;
302

303
    if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
304
    {
305
        LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL");
306
        errorMessage = "invalid outbound group session ptr=NULL";
307
    }
308
    else if (!aClearMsgBuffer)
309
    {
310
        LOGE(" ## encryptMessageJni(): failure - invalid clear message");
311
        errorMessage = "invalid clear message";
312
    }
313
    else if (!(clearMsgPtr = env->GetByteArrayElements(aClearMsgBuffer, NULL)))
314
    {
315
        LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM");
316
        errorMessage = "clear message JNI allocation OOM";
317
    }
318
319
    else
    {
320
        // get clear message length
321
        size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer);
pedroGitt's avatar
pedroGitt committed
322
        LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",static_cast<long unsigned int>(clearMsgLength));
323

324
325
        // compute max encrypted length
        size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength);
326
327
328
        uint8_t *encryptedMsgPtr = (uint8_t*)malloc((encryptedMsgLength+1)*sizeof(uint8_t));

        if (!encryptedMsgPtr)
329
        {
330
            LOGE(" ## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM");
331
            errorMessage = "encryptedMsgPtr buffer OOM";
332
333
334
        }
        else
        {
pedroGitt's avatar
pedroGitt committed
335
            LOGD(" ## encryptMessageJni(): estimated encryptedMsgLength=%lu",static_cast<long unsigned int>(encryptedMsgLength));
336

337
            size_t encryptedLength = olm_group_encrypt(sessionPtr,
338
339
340
341
                                                       (uint8_t*)clearMsgPtr,
                                                       clearMsgLength,
                                                       encryptedMsgPtr,
                                                       encryptedMsgLength);
342
343


344
            if (encryptedLength == olm_error())
345
            {
346
347
                errorMessage = olm_outbound_group_session_last_error(sessionPtr);
                LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s", errorMessage);
348
349
350
            }
            else
            {
351
                // update decrypted buffer size
352
                encryptedMsgPtr[encryptedLength] = static_cast<char>('\0');
353

pedroGitt's avatar
pedroGitt committed
354
                LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr));
355
356
357

                encryptedMsgRet = env->NewByteArray(encryptedLength);
                env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr);
358
            }
359
360
361

            free(encryptedMsgPtr);
         }
362
    }
363
364

    // free alloc
365
    if (clearMsgPtr)
366
    {
367
        env->ReleaseByteArrayElements(aClearMsgBuffer, clearMsgPtr, JNI_ABORT);
368
369
    }

370
371
372
373
374
    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

375
    return encryptedMsgRet;
376
377
378
}


379
380
381
382
383
/**
* Serialize and encrypt session instance into a base64 string.<br>
* @param aKey key used to encrypt the serialized session data
* @return a base64 string if operation succeed, null otherwise
**/
384
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer)
385
{
386
    const char* errorMessage = NULL;
387
    jbyteArray returnValue = 0;
388

389
    jbyte* keyPtr = NULL;
390
391
    OlmOutboundGroupSession* sessionPtr = NULL;

392
    LOGD("## outbound group session serializeJni(): IN");
393

394
    if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
395
    {
396
        LOGE(" ## serializeJni(): failure - invalid session ptr");
397
        errorMessage = "invalid session ptr";
398
    }
399
    else if (!aKeyBuffer)
400
    {
401
        LOGE(" ## serializeJni(): failure - invalid key");
402
        errorMessage = "invalid key";
403
    }
404
    else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0)))
405
    {
406
        LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM");
407
        errorMessage = "keyPtr JNI allocation OOM";
408
409
410
411
    }
    else
    {
        size_t pickledLength = olm_pickle_outbound_group_session_length(sessionPtr);
412
        size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
413
        LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
414

415
416
417
        void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t));

        if(!pickledPtr)
418
        {
419
            LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM");
420
            errorMessage = "pickledPtr buffer OOM";
421
422
423
424
425
426
427
428
        }
        else
        {
            size_t result = olm_pickle_outbound_group_session(sessionPtr,
                                                             (void const *)keyPtr,
                                                              keyLength,
                                                              (void*)pickledPtr,
                                                              pickledLength);
429
            if (result == olm_error())
430
            {
431
432
                errorMessage = olm_outbound_group_session_last_error(sessionPtr);
                LOGE(" ## serializeJni(): failure - olm_pickle_outbound_group_session() Msg=%s", errorMessage);
433
434
435
436
437
            }
            else
            {
                // build success output
                (static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
438
                LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
439
440
441

                returnValue = env->NewByteArray(pickledLength);
                env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr);
442
443
444
            }
        }

445
        free(pickledPtr);
446
447
    }

448
449
    // free alloc
    if (keyPtr)
450
    {
451
        env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
452
453
    }

454
455
456
457
458
    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

459
    return returnValue;
460
461
462
}


463
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer)
464
465
466
{
    OlmOutboundGroupSession* sessionPtr = NULL;
    jstring errorMessageRetValue = 0;
467
468
    jbyte* keyPtr = NULL;
    jbyte* pickledPtr = NULL;
469

470
    LOGD("## deserializeJni(): IN");
471

472
    if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
473
    {
474
        LOGE(" ## deserializeJni(): failure - session failure OOM");
475
    }
476
    else if (!aKeyBuffer)
477
    {
478
        LOGE(" ## deserializeJni(): failure - invalid key");
479
    }
480
    else if (!aSerializedDataBuffer)
481
    {
482
        LOGE(" ## deserializeJni(): failure - serialized data");
483
    }
484
    else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0)))
485
    {
486
        LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM");
487
    }
488
    else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0)))
489
    {
490
        LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM");
491
492
493
    }
    else
    {
494
495
        size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer);
        size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
496
497
        LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
        LOGD(" ## deserializeJni(): pickled=%s",(char const *)pickledPtr);
498
499
500
501
502
503

        size_t result = olm_unpickle_outbound_group_session(sessionPtr,
                                                            (void const *)keyPtr,
                                                            keyLength,
                                                            (void*)pickledPtr,
                                                            pickledLength);
504
        if (result == olm_error())
505
506
        {
            const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
507
            LOGE(" ## deserializeJni(): failure - olm_unpickle_outbound_group_session() Msg=%s",errorMsgPtr);
508
509
510
511
            errorMessageRetValue = env->NewStringUTF(errorMsgPtr);
        }
        else
        {
512
            LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result));
513
514
515
516
        }
    }

    // free alloc
517
    if (keyPtr)
518
    {
519
        env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
520
521
    }

522
    if (pickledPtr)
523
    {
524
        env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT);
525
526
527
528
    }

    return errorMessageRetValue;
}
529