olm.cpp 19.6 KB
Newer Older
Mark Haines's avatar
Mark Haines committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Copyright 2015 OpenMarket 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.
 */
Richard van der Hoff's avatar
Richard van der Hoff committed
15
#include "olm/olm.h"
16
17
#include "olm/session.hh"
#include "olm/account.hh"
18
#include "olm/cipher.h"
19
#include "olm/pickle_encoding.h"
20
#include "olm/utility.hh"
21
#include "olm/base64.hh"
22
#include "olm/memory.hh"
23
24
25
26
27
28

#include <new>
#include <cstring>

namespace {

29
30
static OlmAccount * to_c(olm::Account * account) {
    return reinterpret_cast<OlmAccount *>(account);
31
32
}

33
34
35
36
37
38
static OlmSession * to_c(olm::Session * session) {
    return reinterpret_cast<OlmSession *>(session);
}

static OlmUtility * to_c(olm::Utility * utility) {
    return reinterpret_cast<OlmUtility *>(utility);
39
40
}

41
42
static olm::Account * from_c(OlmAccount * account) {
    return reinterpret_cast<olm::Account *>(account);
43
44
}

45
46
47
48
49
50
static olm::Session * from_c(OlmSession * session) {
    return reinterpret_cast<olm::Session *>(session);
}

static olm::Utility * from_c(OlmUtility * utility) {
    return reinterpret_cast<olm::Utility *>(utility);
51
52
53
54
55
56
57
58
59
60
61
62
63
}

static std::uint8_t * from_c(void * bytes) {
    return reinterpret_cast<std::uint8_t *>(bytes);
}

static std::uint8_t const * from_c(void const * bytes) {
    return reinterpret_cast<std::uint8_t const *>(bytes);
}

std::size_t b64_output_length(
    size_t raw_length
) {
64
    return olm::encode_base64_length(raw_length);
65
66
67
68
69
70
}

std::uint8_t * b64_output_pos(
    std::uint8_t * output,
    size_t raw_length
) {
71
    return output + olm::encode_base64_length(raw_length) - raw_length;
72
73
74
75
76
}

std::size_t b64_output(
    std::uint8_t * output, size_t raw_length
) {
77
    std::size_t base64_length = olm::encode_base64_length(raw_length);
78
    std::uint8_t * raw_output = output + base64_length - raw_length;
79
    olm::encode_base64(raw_output, raw_length, output);
80
81
82
83
84
    return base64_length;
}

std::size_t b64_input(
    std::uint8_t * input, size_t b64_length,
85
    OlmErrorCode & last_error
86
) {
87
    std::size_t raw_length = olm::decode_base64_length(b64_length);
88
    if (raw_length == std::size_t(-1)) {
89
        last_error = OlmErrorCode::OLM_INVALID_BASE64;
90
91
        return std::size_t(-1);
    }
92
    olm::decode_base64(input, b64_length, input);
93
94
95
96
97
98
99
100
101
    return raw_length;
}

} // namespace


extern "C" {


102
size_t olm_error() {
103
104
105
106
    return std::size_t(-1);
}


107
const char * olm_account_last_error(
108
    OlmAccount * account
109
) {
110
111
    auto error = from_c(account)->last_error;
    return _olm_error_to_string(error);
112
113
114
}


115
116
const char * olm_session_last_error(
    OlmSession * session
117
) {
118
119
    auto error = from_c(session)->last_error;
    return _olm_error_to_string(error);
120
121
}

122
123
124
const char * olm_utility_last_error(
    OlmUtility * utility
) {
125
126
    auto error = from_c(utility)->last_error;
    return _olm_error_to_string(error);
127
}
128

129
130
size_t olm_account_size() {
    return sizeof(olm::Account);
131
132
133
}


134
135
size_t olm_session_size() {
    return sizeof(olm::Session);
136
137
}

138
139
140
size_t olm_utility_size() {
    return sizeof(olm::Utility);
}
141

142
OlmAccount * olm_account(
143
144
    void * memory
) {
145
    olm::unset(memory, sizeof(olm::Account));
146
    return to_c(new(memory) olm::Account());
147
148
149
}


150
OlmSession * olm_session(
151
152
    void * memory
) {
153
    olm::unset(memory, sizeof(olm::Session));
154
    return to_c(new(memory) olm::Session());
155
156
157
}


158
159
160
161
162
163
164
165
OlmUtility * olm_utility(
    void * memory
) {
    olm::unset(memory, sizeof(olm::Utility));
    return to_c(new(memory) olm::Utility());
}


166
size_t olm_clear_account(
167
    OlmAccount * account
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
) {
    /* Clear the memory backing the account  */
    olm::unset(account, sizeof(olm::Account));
    /* Initialise a fresh account object in case someone tries to use it */
    new(account) olm::Account();
    return sizeof(olm::Account);
}


size_t olm_clear_session(
    OlmSession * session
) {
    /* Clear the memory backing the session */
    olm::unset(session, sizeof(olm::Session));
    /* Initialise a fresh session object in case someone tries to use it */
    new(session) olm::Session();
    return sizeof(olm::Session);
}


188
189
190
191
192
193
194
195
196
197
198
size_t olm_clear_utility(
    OlmUtility * utility
) {
    /* Clear the memory backing the session */
    olm::unset(utility, sizeof(olm::Utility));
    /* Initialise a fresh session object in case someone tries to use it */
    new(utility) olm::Utility();
    return sizeof(olm::Utility);
}


199
200
size_t olm_pickle_account_length(
    OlmAccount * account
201
) {
202
    return _olm_enc_output_length(pickle_length(*from_c(account)));
203
204
205
}


206
207
size_t olm_pickle_session_length(
    OlmSession * session
208
) {
209
    return _olm_enc_output_length(pickle_length(*from_c(session)));
210
211
212
}


213
214
size_t olm_pickle_account(
    OlmAccount * account,
215
216
217
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
218
    olm::Account & object = *from_c(account);
219
    std::size_t raw_length = pickle_length(object);
220
    if (pickled_length < _olm_enc_output_length(raw_length)) {
221
        object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
222
223
        return size_t(-1);
    }
224
225
    pickle(_olm_enc_output_pos(from_c(pickled), raw_length), object);
    return _olm_enc_output(from_c(key), key_length, from_c(pickled), raw_length);
226
227
228
}


229
230
size_t olm_pickle_session(
    OlmSession * session,
231
232
233
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
234
    olm::Session & object = *from_c(session);
235
    std::size_t raw_length = pickle_length(object);
236
    if (pickled_length < _olm_enc_output_length(raw_length)) {
237
        object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
238
239
        return size_t(-1);
    }
240
241
    pickle(_olm_enc_output_pos(from_c(pickled), raw_length), object);
    return _olm_enc_output(from_c(key), key_length, from_c(pickled), raw_length);
242
243
244
}


245
246
size_t olm_unpickle_account(
    OlmAccount * account,
247
248
249
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
250
    olm::Account & object = *from_c(account);
251
    std::uint8_t * const pos = from_c(pickled);
252
253
    std::size_t raw_length = _olm_enc_input(
        from_c(key), key_length, pos, pickled_length, &object.last_error
254
255
256
257
258
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    std::uint8_t * const end = pos + raw_length;
259
260
261
262
263
    /* On success unpickle will return (pos + raw_length). If unpickling
     * terminates too soon then it will return a pointer before
     * (pos + raw_length). On error unpickle will return (pos + raw_length + 1).
     */
    if (end != unpickle(pos, end + 1, object)) {
264
265
        if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
            object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
266
267
268
        }
        return std::size_t(-1);
    }
269
270
271
272
    return pickled_length;
}


273
274
size_t olm_unpickle_session(
    OlmSession * session,
275
276
277
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
278
    olm::Session & object = *from_c(session);
279
    std::uint8_t * const pos = from_c(pickled);
280
281
    std::size_t raw_length = _olm_enc_input(
        from_c(key), key_length, pos, pickled_length, &object.last_error
282
283
284
285
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
286

287
    std::uint8_t * const end = pos + raw_length;
288
289
290
291
292
    /* On success unpickle will return (pos + raw_length). If unpickling
     * terminates too soon then it will return a pointer before
     * (pos + raw_length). On error unpickle will return (pos + raw_length + 1).
     */
    if (end != unpickle(pos, end + 1, object)) {
293
294
        if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
            object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
295
296
297
        }
        return std::size_t(-1);
    }
298
299
300
301
    return pickled_length;
}


302
303
size_t olm_create_account_random_length(
    OlmAccount * account
304
305
306
307
308
) {
    return from_c(account)->new_account_random_length();
}


309
310
size_t olm_create_account(
    OlmAccount * account,
311
    void * random, size_t random_length
312
) {
313
314
315
    size_t result = from_c(account)->new_account(from_c(random), random_length);
    olm::unset(random, random_length);
    return result;
316
317
}

318

319
size_t olm_account_identity_keys_length(
320
321
322
    OlmAccount * account
) {
    return from_c(account)->get_identity_json_length();
323
324
}

325

326
327
size_t olm_account_identity_keys(
    OlmAccount * account,
328
329
    void * identity_keys, size_t identity_key_length
) {
330
331
332
    return from_c(account)->get_identity_json(
        from_c(identity_keys), identity_key_length
    );
333
334
335
}


336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
size_t olm_account_signature_length(
    OlmAccount * account
) {
    return b64_output_length(from_c(account)->signature_length());
}


size_t olm_account_sign(
    OlmAccount * account,
    void const * message, size_t message_length,
    void * signature, size_t signature_length
) {
    std::size_t raw_length = from_c(account)->signature_length();
    if (signature_length < b64_output_length(raw_length)) {
        from_c(account)->last_error =
351
            OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
352
353
354
355
356
357
358
359
360
361
        return std::size_t(-1);
    }
    from_c(account)->sign(
         from_c(message), message_length,
         b64_output_pos(from_c(signature), raw_length), raw_length
    );
    return b64_output(from_c(signature), raw_length);
}


362
363
size_t olm_account_one_time_keys_length(
    OlmAccount * account
364
) {
365
    return from_c(account)->get_one_time_keys_json_length();
366
367
368
}


369
370
size_t olm_account_one_time_keys(
    OlmAccount * account,
371
    void * one_time_keys_json, size_t one_time_key_json_length
372
) {
373
374
375
    return from_c(account)->get_one_time_keys_json(
        from_c(one_time_keys_json), one_time_key_json_length
    );
376
377
378
}


379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
size_t olm_account_mark_keys_as_published(
    OlmAccount * account
) {
    return from_c(account)->mark_keys_as_published();
}


size_t olm_account_max_number_of_one_time_keys(
    OlmAccount * account
) {
    return from_c(account)->max_number_of_one_time_keys();
}


size_t olm_account_generate_one_time_keys_random_length(
    OlmAccount * account,
    size_t number_of_keys
) {
    return from_c(account)->generate_one_time_keys_random_length(number_of_keys);
}


size_t olm_account_generate_one_time_keys(
    OlmAccount * account,
    size_t number_of_keys,
404
    void * random, size_t random_length
405
) {
406
    size_t result = from_c(account)->generate_one_time_keys(
407
408
409
        number_of_keys,
        from_c(random), random_length
    );
410
411
    olm::unset(random, random_length);
    return result;
412
413
414
}


415
416
size_t olm_create_outbound_session_random_length(
    OlmSession * session
Mark Haines's avatar
Mark Haines committed
417
418
419
420
) {
    return from_c(session)->new_outbound_session_random_length();
}

421

422
423
424
size_t olm_create_outbound_session(
    OlmSession * session,
    OlmAccount * account,
425
426
    void const * their_identity_key, size_t their_identity_key_length,
    void const * their_one_time_key, size_t their_one_time_key_length,
427
    void * random, size_t random_length
428
) {
429
430
431
432
433
434
435
    std::uint8_t const * id_key = from_c(their_identity_key);
    std::uint8_t const * ot_key = from_c(their_one_time_key);
    std::size_t id_key_length = their_identity_key_length;
    std::size_t ot_key_length = their_one_time_key_length;

    if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH
            || olm::decode_base64_length(ot_key_length) != olm::KEY_LENGTH
436
    ) {
437
        from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
438
439
        return std::size_t(-1);
    }
440
    olm::Curve25519PublicKey identity_key;
441
    olm::Curve25519PublicKey one_time_key;
442

443
444
    olm::decode_base64(id_key, id_key_length, identity_key.public_key);
    olm::decode_base64(ot_key, ot_key_length, one_time_key.public_key);
445

446
    size_t result = from_c(session)->new_outbound_session(
447
448
449
        *from_c(account), identity_key, one_time_key,
        from_c(random), random_length
    );
450
451
    olm::unset(random, random_length);
    return result;
452
453
454
}


455
456
457
size_t olm_create_inbound_session(
    OlmSession * session,
    OlmAccount * account,
458
459
460
461
462
463
464
465
466
    void * one_time_key_message, size_t message_length
) {
    std::size_t raw_length = b64_input(
        from_c(one_time_key_message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(session)->new_inbound_session(
467
468
469
470
471
472
473
474
475
476
477
        *from_c(account), nullptr, from_c(one_time_key_message), raw_length
    );
}


size_t olm_create_inbound_session_from(
    OlmSession * session,
    OlmAccount * account,
    void const * their_identity_key, size_t their_identity_key_length,
    void * one_time_key_message, size_t message_length
) {
478
479
480
481
    std::uint8_t const * id_key = from_c(their_identity_key);
    std::size_t id_key_length = their_identity_key_length;

    if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH) {
482
        from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
483
484
485
        return std::size_t(-1);
    }
    olm::Curve25519PublicKey identity_key;
486
    olm::decode_base64(id_key, id_key_length, identity_key.public_key);
487
488
489
490
491
492
493
494
495
496

    std::size_t raw_length = b64_input(
        from_c(one_time_key_message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(session)->new_inbound_session(
        *from_c(account), &identity_key,
        from_c(one_time_key_message), raw_length
497
498
499
500
    );
}


501
502
503
504
505
506
507
508
509
510
511
512
513
size_t olm_session_id_length(
    OlmSession * session
) {
    return b64_output_length(from_c(session)->session_id_length());
}

size_t olm_session_id(
    OlmSession * session,
    void * id, size_t id_length
) {
    std::size_t raw_length = from_c(session)->session_id_length();
    if (id_length < b64_output_length(raw_length)) {
        from_c(session)->last_error =
514
                OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
515
516
517
518
519
520
521
522
523
524
525
526
        return std::size_t(-1);
    }
    std::size_t result = from_c(session)->session_id(
       b64_output_pos(from_c(id), raw_length), raw_length
    );
    if (result == std::size_t(-1)) {
        return result;
    }
    return b64_output(from_c(id), raw_length);
}


527
528
size_t olm_matches_inbound_session(
    OlmSession * session,
529
530
531
532
533
534
535
536
537
    void * one_time_key_message, size_t message_length
) {
    std::size_t raw_length = b64_input(
        from_c(one_time_key_message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    bool matches = from_c(session)->matches_inbound_session(
538
539
540
541
542
543
544
545
546
547
548
        nullptr, from_c(one_time_key_message), raw_length
    );
    return matches ? 1 : 0;
}


size_t olm_matches_inbound_session_from(
    OlmSession * session,
    void const * their_identity_key, size_t their_identity_key_length,
    void * one_time_key_message, size_t message_length
) {
549
550
551
552
    std::uint8_t const * id_key = from_c(their_identity_key);
    std::size_t id_key_length = their_identity_key_length;

    if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH) {
553
        from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
554
555
556
        return std::size_t(-1);
    }
    olm::Curve25519PublicKey identity_key;
557
    olm::decode_base64(id_key, id_key_length, identity_key.public_key);
558
559
560
561
562
563
564
565
566

    std::size_t raw_length = b64_input(
        from_c(one_time_key_message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    bool matches = from_c(session)->matches_inbound_session(
        &identity_key, from_c(one_time_key_message), raw_length
567
568
569
570
571
    );
    return matches ? 1 : 0;
}


572
573
574
size_t olm_remove_one_time_keys(
    OlmAccount * account,
    OlmSession * session
575
576
) {
    size_t result = from_c(account)->remove_key(
577
        from_c(session)->bob_one_time_key
578
579
    );
    if (result == std::size_t(-1)) {
580
        from_c(account)->last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
581
582
583
584
585
    }
    return result;
}


586
587
size_t olm_encrypt_message_type(
    OlmSession * session
588
589
590
591
592
) {
    return size_t(from_c(session)->encrypt_message_type());
}


593
594
size_t olm_encrypt_random_length(
    OlmSession * session
595
596
597
598
599
) {
    return from_c(session)->encrypt_random_length();
}


600
601
size_t olm_encrypt_message_length(
    OlmSession * session,
602
603
604
605
606
607
608
609
    size_t plaintext_length
) {
    return b64_output_length(
        from_c(session)->encrypt_message_length(plaintext_length)
    );
}


610
611
size_t olm_encrypt(
    OlmSession * session,
612
    void const * plaintext, size_t plaintext_length,
613
    void * random, size_t random_length,
614
615
616
617
618
    void * message, size_t message_length
) {
    std::size_t raw_length = from_c(session)->encrypt_message_length(
        plaintext_length
    );
619
    if (message_length < b64_output_length(raw_length)) {
620
        from_c(session)->last_error =
621
            OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
622
623
        return std::size_t(-1);
    }
624
    std::size_t result = from_c(session)->encrypt(
625
626
627
628
        from_c(plaintext), plaintext_length,
        from_c(random), random_length,
        b64_output_pos(from_c(message), raw_length), raw_length
    );
629
    olm::unset(random, random_length);
630
631
632
    if (result == std::size_t(-1)) {
        return result;
    }
633
634
635
636
    return b64_output(from_c(message), raw_length);
}


637
638
size_t olm_decrypt_max_plaintext_length(
    OlmSession * session,
639
640
641
642
643
644
645
646
647
648
    size_t message_type,
    void * message, size_t message_length
) {
    std::size_t raw_length = b64_input(
        from_c(message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(session)->decrypt_max_plaintext_length(
649
        olm::MessageType(message_type), from_c(message), raw_length
650
651
652
653
    );
}


654
655
size_t olm_decrypt(
    OlmSession * session,
656
657
658
659
660
661
662
663
664
665
666
    size_t message_type,
    void * message, size_t message_length,
    void * plaintext, size_t max_plaintext_length
) {
    std::size_t raw_length = b64_input(
        from_c(message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(session)->decrypt(
667
        olm::MessageType(message_type), from_c(message), raw_length,
668
669
670
671
        from_c(plaintext), max_plaintext_length
    );
}

672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687

size_t olm_sha256_length(
   OlmUtility * utility
) {
    return b64_output_length(from_c(utility)->sha256_length());
}


size_t olm_sha256(
    OlmUtility * utility,
    void const * input, size_t input_length,
    void * output, size_t output_length
) {
    std::size_t raw_length = from_c(utility)->sha256_length();
    if (output_length < b64_output_length(raw_length)) {
        from_c(utility)->last_error =
688
            OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
        return std::size_t(-1);
    }
    std::size_t result = from_c(utility)->sha256(
       from_c(input), input_length,
       b64_output_pos(from_c(output), raw_length), raw_length
    );
    if (result == std::size_t(-1)) {
        return result;
    }
    return b64_output(from_c(output), raw_length);
}


size_t olm_ed25519_verify(
    OlmUtility * utility,
    void const * key, size_t key_length,
    void const * message, size_t message_length,
    void * signature, size_t signature_length
) {
708
    if (olm::decode_base64_length(key_length) != olm::KEY_LENGTH) {
709
        from_c(utility)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
710
711
712
        return std::size_t(-1);
    }
    olm::Ed25519PublicKey verify_key;
713
    olm::decode_base64(from_c(key), key_length, verify_key.public_key);
714
715
716
717
718
719
720
721
722
723
724
725
726
    std::size_t raw_signature_length = b64_input(
        from_c(signature), signature_length, from_c(utility)->last_error
    );
    if (raw_signature_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(utility)->ed25519_verify(
        verify_key,
        from_c(message), message_length,
        from_c(signature), raw_signature_length
    );
}

727
}