olm_outbound_group_session.cpp 19.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
}

/**
ylecollen's avatar
ylecollen committed
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 or throw an exception
 **/
60
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
61
{
ylecollen's avatar
ylecollen committed
62
63
    const char* errorMessage = NULL;

64
    OlmOutboundGroupSession* sessionPtr = NULL;
65
66
    size_t sessionSize = 0;

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

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

85
86
        // compute random buffer
        size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr);
87
88
        uint8_t *randomBuffPtr = NULL;

ylecollen's avatar
ylecollen committed
89
        LOGW(" ## createNewSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength));
90
91

        if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
92
        {
ylecollen's avatar
ylecollen committed
93
            LOGE(" ## createNewSessionJni(): failure - random buffer init");
94
            errorMessage = "random buffer init";
95
96
97
        }
        else
        {
98
            if (0 == randomLength)
99
            {
ylecollen's avatar
ylecollen committed
100
                LOGW(" ## createNewSessionJni(): random buffer is not required");
101
102
            }

103
            size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength);
104
105

            if (sessionResult == olm_error()) {
106
                errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr);
ylecollen's avatar
ylecollen committed
107
                LOGE(" ## createNewSessionJni(): failure - init outbound session creation  Msg=%s", errorMessage);
108
109
110
            }
            else
            {
ylecollen's avatar
ylecollen committed
111
                LOGD(" ## createNewSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
112
113
            }

114
115
            // clear the random buffer
            memset(randomBuffPtr, 0, randomLength);
116
117
            free(randomBuffPtr);
        }
118
    }
119

120
121
    if (errorMessage)
    {
ylecollen's avatar
ylecollen committed
122
123
124
125
126
127
        if (sessionPtr)
        {
            olm_clear_outbound_group_session(sessionPtr);
            free(sessionPtr);
        }

128
129
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }
ylecollen's avatar
ylecollen committed
130
131

    return (jlong)(intptr_t)sessionPtr;
132
133
134
}

/**
ylecollen's avatar
ylecollen committed
135
136
137
138
 * Return the session identifier.
 * An exception is thrown if the operation fails.
 * @return the session identifier
 */
139
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
140
141
142
{
    LOGD("## sessionIdentifierJni(): outbound group session IN");

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

158
        uint8_t *sessionIdPtr =  (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t));
159
160

        if (!sessionIdPtr)
161
        {
162
           LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM");
163
           errorMessage = "outbound identifier allocation OOM";
164
165
166
        }
        else
        {
167
            size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
168

169
170
            if (result == olm_error())
            {
171
172
                errorMessage = reinterpret_cast<const char*>(olm_outbound_group_session_last_error(sessionPtr));
                LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s", errorMessage);
173
174
175
            }
            else
            {
176
177
178
                returnValue = env->NewByteArray(result);
                env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr);

ylecollen's avatar
ylecollen committed
179
                LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId= %.*s",static_cast<long unsigned int>(result), static_cast<int>(result), reinterpret_cast<char*>(sessionIdPtr));
180
            }
181

182
183
184
185
            // free alloc
            free(sessionIdPtr);
        }
    }
186

187
188
189
190
191
    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

192
    return returnValue;
193
194
195
}


196
/**
ylecollen's avatar
ylecollen committed
197
198
199
200
201
202
 * 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.
 * An exception is thrown if the operation fails.
 * @return current session index
 */
203
JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz)
204
{
205
206
    OlmOutboundGroupSession *sessionPtr = NULL;
    jint indexRetValue = 0;
207

208
    LOGD("## messageIndexJni(): IN");
209

210
    if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
211
    {
212
        LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance");
213
214
    }
    else
215
    {
216
        indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr));
217
    }
218

219
    LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue);
220
221
222
223
224

    return indexRetValue;
}

/**
ylecollen's avatar
ylecollen committed
225
226
227
228
 * Return the session key.
 * An exception is thrown if the operation fails.
 * @return the session key
 */
229
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz)
230
231
{
    LOGD("## sessionKeyJni(): outbound group session IN");
232

233
    const char* errorMessage = NULL;
234
    OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
235
    jbyteArray returnValue = 0;
236

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

248
        uint8_t *sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t));
249
250

        if (!sessionKeyPtr)
251
        {
252
           LOGE(" ## sessionKeyJni(): failure - session key allocation OOM");
253
           errorMessage = "session key allocation OOM";
254
255
256
        }
        else
        {
257
            size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength);
258

259
260
            if (result == olm_error())
            {
261
262
                errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr);
                LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s", errorMessage);
263
264
265
            }
            else
            {
ylecollen's avatar
ylecollen committed
266
                LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%.*s",static_cast<long unsigned int>(result), static_cast<int>(result), reinterpret_cast<char*>(sessionKeyPtr));
267
268
269

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

272
273
            // free alloc
            free(sessionKeyPtr);
274
275
276
        }
    }

277
278
279
280
281
    if (errorMessage)
    {
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

282
    return returnValue;
283
284
}

ylecollen's avatar
ylecollen committed
285
286
287
288
289
290
/**
 * Encrypt a bytes buffer messages.
 * An exception is thrown if the operation fails.
 * @param aClearMsgBuffer  the message to encode
 * @return the encoded message
 */
291
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer)
292
{
293
294
    LOGD("## encryptMessageJni(): IN");

295
    const char* errorMessage = NULL;
296
297
    jbyteArray encryptedMsgRet = 0;

298
    OlmOutboundGroupSession *sessionPtr = NULL;
299
    jbyte* clearMsgPtr = NULL;
300
    jboolean clearMsgIsCopied = JNI_FALSE;
301

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

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

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

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


343
            if (encryptedLength == olm_error())
344
            {
345
346
                errorMessage = olm_outbound_group_session_last_error(sessionPtr);
                LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s", errorMessage);
347
348
349
            }
            else
            {
ylecollen's avatar
ylecollen committed
350
                LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%.*s",static_cast<long unsigned int>(encryptedLength), static_cast<int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr));
351
352
353

                encryptedMsgRet = env->NewByteArray(encryptedLength);
                env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr);
354
            }
355
356
357

            free(encryptedMsgPtr);
         }
358
    }
359
360

    // free alloc
361
    if (clearMsgPtr)
362
    {
363
364
365
366
        if (clearMsgIsCopied)
        {
            memset(clearMsgPtr, 0, (size_t)env->GetArrayLength(aClearMsgBuffer));
        }
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
/**
ylecollen's avatar
ylecollen committed
379
380
381
382
383
 * Serialize and encrypt session instance into a base64 string.<br>
 * An exception is thrown if the operation fails.
 * @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
        void *pickledPtr = malloc(pickledLength*sizeof(uint8_t));
416
417

        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
            }
            else
            {
ylecollen's avatar
ylecollen committed
436
                LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(result), static_cast<char*>(pickledPtr));
437
438
439

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

443
        free(pickledPtr);
444
445
    }

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

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

457
    return returnValue;
458
459
}

ylecollen's avatar
ylecollen committed
460
461
462
463
464
465
466
467
/**
 * Allocate a new session and initialize it with the serialisation data.<br>
 * An exception is thrown if the operation fails.
 * @param aSerializedData the session serialisation buffer
 * @param aKey the key used to encrypt the serialized account data
 * @return the deserialized session
 **/
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer)
468
{
ylecollen's avatar
ylecollen committed
469
470
    const char* errorMessage = NULL;
    size_t sessionSize = olm_outbound_group_session_size();
471
    OlmOutboundGroupSession* sessionPtr = NULL;
ylecollen's avatar
ylecollen committed
472

473
474
    jbyte* keyPtr = NULL;
    jbyte* pickledPtr = NULL;
475

476
    LOGD("## deserializeJni(): IN");
477

ylecollen's avatar
ylecollen committed
478
479
480
481
482
483
    if (!sessionSize)
    {
        LOGE(" ## deserializeJni(): failure - outbound group session size = 0");
        errorMessage = "outbound group session size = 0";
    }
    else if (!(sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize)))
484
    {
485
        LOGE(" ## deserializeJni(): failure - session failure OOM");
ylecollen's avatar
ylecollen committed
486
        errorMessage = "session failure OOM";
487
    }
488
    else if (!aKeyBuffer)
489
    {
490
        LOGE(" ## deserializeJni(): failure - invalid key");
ylecollen's avatar
ylecollen committed
491
        errorMessage = "invalid key";
492
    }
493
    else if (!aSerializedDataBuffer)
494
    {
495
        LOGE(" ## deserializeJni(): failure - serialized data");
ylecollen's avatar
ylecollen committed
496
        errorMessage = "invalid serialized data";
497
    }
498
    else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0)))
499
    {
500
        LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM");
ylecollen's avatar
ylecollen committed
501
        errorMessage = "keyPtr JNI allocation OOM";
502
    }
503
    else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0)))
504
    {
505
        LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM");
ylecollen's avatar
ylecollen committed
506
        errorMessage = "pickledPtr JNI allocation OOM";
507
508
509
    }
    else
    {
ylecollen's avatar
ylecollen committed
510
        sessionPtr = olm_outbound_group_session(sessionPtr);
511
512
        size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer);
        size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
513
        LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
ylecollen's avatar
ylecollen committed
514
        LOGD(" ## deserializeJni(): pickled=%.*s", static_cast<int>(pickledLength), (char const *)pickledPtr);
515
516
517
518
519
520

        size_t result = olm_unpickle_outbound_group_session(sessionPtr,
                                                            (void const *)keyPtr,
                                                            keyLength,
                                                            (void*)pickledPtr,
                                                            pickledLength);
521
        if (result == olm_error())
522
        {
ylecollen's avatar
ylecollen committed
523
524
            errorMessage = olm_outbound_group_session_last_error(sessionPtr);
            LOGE(" ## deserializeJni(): failure - olm_unpickle_outbound_group_session() Msg=%s", errorMessage);
525
526
527
        }
        else
        {
528
            LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result));
529
530
531
532
        }
    }

    // free alloc
533
    if (keyPtr)
534
    {
535
        env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
536
537
    }

538
    if (pickledPtr)
539
    {
540
        env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT);
541
542
    }

ylecollen's avatar
ylecollen committed
543
544
545
546
547
548
549
550
551
552
553
    if (errorMessage)
    {
        if (sessionPtr)
        {
            olm_clear_outbound_group_session(sessionPtr);
            free(sessionPtr);
        }
        env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
    }

    return (jlong)(intptr_t)sessionPtr;
554
}
555