1 /* Copyright 2015 OpenMarket Ltd
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "olm/session.hh"
17 #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"
29 static OlmAccount * to_c(olm::Account * account) {
30 return reinterpret_cast<OlmAccount *>(account);
33 static OlmSession * to_c(olm::Session * session) {
34 return reinterpret_cast<OlmSession *>(session);
37 static OlmUtility * to_c(olm::Utility * utility) {
38 return reinterpret_cast<OlmUtility *>(utility);
41 static olm::Account * from_c(OlmAccount * account) {
42 return reinterpret_cast<olm::Account *>(account);
45 static const olm::Account * from_c(OlmAccount const * account) {
46 return reinterpret_cast<olm::Account const *>(account);
49 static olm::Session * from_c(OlmSession * session) {
50 return reinterpret_cast<olm::Session *>(session);
53 static const olm::Session * from_c(OlmSession const * session) {
54 return reinterpret_cast<const olm::Session *>(session);
57 static olm::Utility * from_c(OlmUtility * utility) {
58 return reinterpret_cast<olm::Utility *>(utility);
61 static const olm::Utility * from_c(OlmUtility const * utility) {
62 return reinterpret_cast<const olm::Utility *>(utility);
65 static std::uint8_t * from_c(void * bytes) {
66 return reinterpret_cast<std::uint8_t *>(bytes);
69 static std::uint8_t const * from_c(void const * bytes) {
70 return reinterpret_cast<std::uint8_t const *>(bytes);
73 std::size_t b64_output_length(
76 return olm::encode_base64_length(raw_length);
79 std::uint8_t * b64_output_pos(
80 std::uint8_t * output,
83 return output + olm::encode_base64_length(raw_length) - raw_length;
86 std::size_t b64_output(
87 std::uint8_t * output, size_t raw_length
89 std::size_t base64_length = olm::encode_base64_length(raw_length);
90 std::uint8_t * raw_output = output + base64_length - raw_length;
91 olm::encode_base64(raw_output, raw_length, output);
95 std::size_t b64_input(
96 std::uint8_t * input, size_t b64_length,
97 OlmErrorCode & last_error
99 std::size_t raw_length = olm::decode_base64_length(b64_length);
100 if (raw_length == std::size_t(-1)) {
101 last_error = OlmErrorCode::OLM_INVALID_BASE64;
102 return std::size_t(-1);
104 olm::decode_base64(input, b64_length, input);
113 void olm_get_library_version(uint8_t *major, uint8_t *minor, uint8_t *patch) {
114 if (major != NULL) *major = OLMLIB_VERSION_MAJOR;
115 if (minor != NULL) *minor = OLMLIB_VERSION_MINOR;
116 if (patch != NULL) *patch = OLMLIB_VERSION_PATCH;
119 size_t olm_error(void) {
120 return std::size_t(-1);
124 const char * olm_account_last_error(
125 const OlmAccount * account
127 auto error = from_c(account)->last_error;
128 return _olm_error_to_string(error);
131 enum OlmErrorCode olm_account_last_error_code(
132 const OlmAccount * account
134 return from_c(account)->last_error;
137 const char * olm_session_last_error(
138 const OlmSession * session
140 auto error = from_c(session)->last_error;
141 return _olm_error_to_string(error);
144 enum OlmErrorCode olm_session_last_error_code(
145 OlmSession const * session
147 return from_c(session)->last_error;
150 const char * olm_utility_last_error(
151 OlmUtility const * utility
153 auto error = from_c(utility)->last_error;
154 return _olm_error_to_string(error);
157 enum OlmErrorCode olm_utility_last_error_code(
158 OlmUtility const * utility
160 return from_c(utility)->last_error;
163 size_t olm_account_size(void) {
164 return sizeof(olm::Account);
168 size_t olm_session_size(void) {
169 return sizeof(olm::Session);
172 size_t olm_utility_size(void) {
173 return sizeof(olm::Utility);
176 OlmAccount * olm_account(
179 olm::unset(memory, sizeof(olm::Account));
180 return to_c(new(memory) olm::Account());
184 OlmSession * olm_session(
187 olm::unset(memory, sizeof(olm::Session));
188 return to_c(new(memory) olm::Session());
192 OlmUtility * olm_utility(
195 olm::unset(memory, sizeof(olm::Utility));
196 return to_c(new(memory) olm::Utility());
200 size_t olm_clear_account(
203 /* Clear the memory backing the account */
204 olm::unset(account, sizeof(olm::Account));
205 /* Initialise a fresh account object in case someone tries to use it */
206 new(account) olm::Account();
207 return sizeof(olm::Account);
211 size_t olm_clear_session(
214 /* Clear the memory backing the session */
215 olm::unset(session, sizeof(olm::Session));
216 /* Initialise a fresh session object in case someone tries to use it */
217 new(session) olm::Session();
218 return sizeof(olm::Session);
222 size_t olm_clear_utility(
225 /* Clear the memory backing the session */
226 olm::unset(utility, sizeof(olm::Utility));
227 /* Initialise a fresh session object in case someone tries to use it */
228 new(utility) olm::Utility();
229 return sizeof(olm::Utility);
233 size_t olm_pickle_account_length(
234 OlmAccount const * account
236 return _olm_enc_output_length(pickle_length(*from_c(account)));
240 size_t olm_pickle_session_length(
241 OlmSession const * session
243 return _olm_enc_output_length(pickle_length(*from_c(session)));
247 size_t olm_pickle_account(
248 OlmAccount * account,
249 void const * key, size_t key_length,
250 void * pickled, size_t pickled_length
252 olm::Account & object = *from_c(account);
253 std::size_t raw_length = pickle_length(object);
254 if (pickled_length < _olm_enc_output_length(raw_length)) {
255 object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
258 pickle(_olm_enc_output_pos(from_c(pickled), raw_length), object);
259 return _olm_enc_output(from_c(key), key_length, from_c(pickled), raw_length);
263 size_t olm_pickle_session(
264 OlmSession * session,
265 void const * key, size_t key_length,
266 void * pickled, size_t pickled_length
268 olm::Session & object = *from_c(session);
269 std::size_t raw_length = pickle_length(object);
270 if (pickled_length < _olm_enc_output_length(raw_length)) {
271 object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
274 pickle(_olm_enc_output_pos(from_c(pickled), raw_length), object);
275 return _olm_enc_output(from_c(key), key_length, from_c(pickled), raw_length);
279 size_t olm_unpickle_account(
280 OlmAccount * account,
281 void const * key, size_t key_length,
282 void * pickled, size_t pickled_length
284 olm::Account & object = *from_c(account);
285 std::uint8_t * input = from_c(pickled);
286 std::size_t raw_length = _olm_enc_input(
287 from_c(key), key_length, input, pickled_length, &object.last_error
289 if (raw_length == std::size_t(-1)) {
290 return std::size_t(-1);
293 std::uint8_t const * pos = input;
294 std::uint8_t const * end = pos + raw_length;
296 pos = unpickle(pos, end, object);
299 /* Input was corrupted. */
300 if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
301 object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
303 return std::size_t(-1);
304 } else if (pos != end) {
305 /* Input was longer than expected. */
306 object.last_error = OlmErrorCode::OLM_PICKLE_EXTRA_DATA;
307 return std::size_t(-1);
310 return pickled_length;
314 size_t olm_unpickle_session(
315 OlmSession * session,
316 void const * key, size_t key_length,
317 void * pickled, size_t pickled_length
319 olm::Session & object = *from_c(session);
320 std::uint8_t * input = from_c(pickled);
321 std::size_t raw_length = _olm_enc_input(
322 from_c(key), key_length, input, pickled_length, &object.last_error
324 if (raw_length == std::size_t(-1)) {
325 return std::size_t(-1);
328 std::uint8_t const * pos = input;
329 std::uint8_t const * end = pos + raw_length;
331 pos = unpickle(pos, end, object);
334 /* Input was corrupted. */
335 if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
336 object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
338 return std::size_t(-1);
339 } else if (pos != end) {
340 /* Input was longer than expected. */
341 object.last_error = OlmErrorCode::OLM_PICKLE_EXTRA_DATA;
342 return std::size_t(-1);
345 return pickled_length;
349 size_t olm_create_account_random_length(
350 OlmAccount const * account
352 return from_c(account)->new_account_random_length();
356 size_t olm_create_account(
357 OlmAccount * account,
358 void * random, size_t random_length
360 size_t result = from_c(account)->new_account(from_c(random), random_length);
361 olm::unset(random, random_length);
366 size_t olm_account_identity_keys_length(
367 OlmAccount const * account
369 return from_c(account)->get_identity_json_length();
373 size_t olm_account_identity_keys(
374 OlmAccount * account,
375 void * identity_keys, size_t identity_key_length
377 return from_c(account)->get_identity_json(
378 from_c(identity_keys), identity_key_length
383 size_t olm_account_signature_length(
384 OlmAccount const * account
386 return b64_output_length(from_c(account)->signature_length());
390 size_t olm_account_sign(
391 OlmAccount * account,
392 void const * message, size_t message_length,
393 void * signature, size_t signature_length
395 std::size_t raw_length = from_c(account)->signature_length();
396 if (signature_length < b64_output_length(raw_length)) {
397 from_c(account)->last_error =
398 OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
399 return std::size_t(-1);
401 from_c(account)->sign(
402 from_c(message), message_length,
403 b64_output_pos(from_c(signature), raw_length), raw_length
405 return b64_output(from_c(signature), raw_length);
409 size_t olm_account_one_time_keys_length(
410 OlmAccount const * account
412 return from_c(account)->get_one_time_keys_json_length();
416 size_t olm_account_one_time_keys(
417 OlmAccount * account,
418 void * one_time_keys_json, size_t one_time_key_json_length
420 return from_c(account)->get_one_time_keys_json(
421 from_c(one_time_keys_json), one_time_key_json_length
426 size_t olm_account_mark_keys_as_published(
429 return from_c(account)->mark_keys_as_published();
433 size_t olm_account_max_number_of_one_time_keys(
434 OlmAccount const * account
436 return from_c(account)->max_number_of_one_time_keys();
440 size_t olm_account_generate_one_time_keys_random_length(
441 OlmAccount const * account,
442 size_t number_of_keys
444 return from_c(account)->generate_one_time_keys_random_length(number_of_keys);
448 size_t olm_account_generate_one_time_keys(
449 OlmAccount * account,
450 size_t number_of_keys,
451 void * random, size_t random_length
453 size_t result = from_c(account)->generate_one_time_keys(
455 from_c(random), random_length
457 olm::unset(random, random_length);
462 size_t olm_account_generate_fallback_key_random_length(
463 OlmAccount const * account
465 return from_c(account)->generate_fallback_key_random_length();
469 size_t olm_account_generate_fallback_key(
470 OlmAccount * account,
471 void * random, size_t random_length
473 size_t result = from_c(account)->generate_fallback_key(
474 from_c(random), random_length
476 olm::unset(random, random_length);
481 size_t olm_account_fallback_key_length(
482 OlmAccount const * account
484 return from_c(account)->get_fallback_key_json_length();
488 size_t olm_account_fallback_key(
489 OlmAccount * account,
490 void * fallback_key_json, size_t fallback_key_json_length
492 return from_c(account)->get_fallback_key_json(
493 from_c(fallback_key_json), fallback_key_json_length
498 size_t olm_account_unpublished_fallback_key_length(
499 OlmAccount const * account
501 return from_c(account)->get_unpublished_fallback_key_json_length();
505 size_t olm_account_unpublished_fallback_key(
506 OlmAccount * account,
507 void * fallback_key_json, size_t fallback_key_json_length
509 return from_c(account)->get_unpublished_fallback_key_json(
510 from_c(fallback_key_json), fallback_key_json_length
515 void olm_account_forget_old_fallback_key(
518 return from_c(account)->forget_old_fallback_key();
522 size_t olm_create_outbound_session_random_length(
523 OlmSession const * session
525 return from_c(session)->new_outbound_session_random_length();
529 size_t olm_create_outbound_session(
530 OlmSession * session,
531 OlmAccount const * account,
532 void const * their_identity_key, size_t their_identity_key_length,
533 void const * their_one_time_key, size_t their_one_time_key_length,
534 void * random, size_t random_length
536 std::uint8_t const * id_key = from_c(their_identity_key);
537 std::uint8_t const * ot_key = from_c(their_one_time_key);
538 std::size_t id_key_length = their_identity_key_length;
539 std::size_t ot_key_length = their_one_time_key_length;
541 if (olm::decode_base64_length(id_key_length) != CURVE25519_KEY_LENGTH
542 || olm::decode_base64_length(ot_key_length) != CURVE25519_KEY_LENGTH
544 from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
545 return std::size_t(-1);
547 _olm_curve25519_public_key identity_key;
548 _olm_curve25519_public_key one_time_key;
550 olm::decode_base64(id_key, id_key_length, identity_key.public_key);
551 olm::decode_base64(ot_key, ot_key_length, one_time_key.public_key);
553 size_t result = from_c(session)->new_outbound_session(
554 *from_c(account), identity_key, one_time_key,
555 from_c(random), random_length
557 olm::unset(random, random_length);
562 size_t olm_create_inbound_session(
563 OlmSession * session,
564 OlmAccount * account,
565 void * one_time_key_message, size_t message_length
567 std::size_t raw_length = b64_input(
568 from_c(one_time_key_message), message_length, from_c(session)->last_error
570 if (raw_length == std::size_t(-1)) {
571 return std::size_t(-1);
573 return from_c(session)->new_inbound_session(
574 *from_c(account), nullptr, from_c(one_time_key_message), raw_length
579 size_t olm_create_inbound_session_from(
580 OlmSession * session,
581 OlmAccount * account,
582 void const * their_identity_key, size_t their_identity_key_length,
583 void * one_time_key_message, size_t message_length
585 std::uint8_t const * id_key = from_c(their_identity_key);
586 std::size_t id_key_length = their_identity_key_length;
588 if (olm::decode_base64_length(id_key_length) != CURVE25519_KEY_LENGTH) {
589 from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
590 return std::size_t(-1);
592 _olm_curve25519_public_key identity_key;
593 olm::decode_base64(id_key, id_key_length, identity_key.public_key);
595 std::size_t raw_length = b64_input(
596 from_c(one_time_key_message), message_length, from_c(session)->last_error
598 if (raw_length == std::size_t(-1)) {
599 return std::size_t(-1);
601 return from_c(session)->new_inbound_session(
602 *from_c(account), &identity_key,
603 from_c(one_time_key_message), raw_length
608 size_t olm_session_id_length(
609 OlmSession const * session
611 return b64_output_length(from_c(session)->session_id_length());
614 size_t olm_session_id(
615 OlmSession * session,
616 void * id, size_t id_length
618 std::size_t raw_length = from_c(session)->session_id_length();
619 if (id_length < b64_output_length(raw_length)) {
620 from_c(session)->last_error =
621 OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
622 return std::size_t(-1);
624 std::size_t result = from_c(session)->session_id(
625 b64_output_pos(from_c(id), raw_length), raw_length
627 if (result == std::size_t(-1)) {
630 return b64_output(from_c(id), raw_length);
634 int olm_session_has_received_message(
635 OlmSession const * session
637 return from_c(session)->received_message;
640 void olm_session_describe(
641 OlmSession * session, char *buf, size_t buflen
643 from_c(session)->describe(buf, buflen);
646 size_t olm_matches_inbound_session(
647 OlmSession * session,
648 void * one_time_key_message, size_t message_length
650 std::size_t raw_length = b64_input(
651 from_c(one_time_key_message), message_length, from_c(session)->last_error
653 if (raw_length == std::size_t(-1)) {
654 return std::size_t(-1);
656 bool matches = from_c(session)->matches_inbound_session(
657 nullptr, from_c(one_time_key_message), raw_length
659 return matches ? 1 : 0;
663 size_t olm_matches_inbound_session_from(
664 OlmSession * session,
665 void const * their_identity_key, size_t their_identity_key_length,
666 void * one_time_key_message, size_t message_length
668 std::uint8_t const * id_key = from_c(their_identity_key);
669 std::size_t id_key_length = their_identity_key_length;
671 if (olm::decode_base64_length(id_key_length) != CURVE25519_KEY_LENGTH) {
672 from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
673 return std::size_t(-1);
675 _olm_curve25519_public_key identity_key;
676 olm::decode_base64(id_key, id_key_length, identity_key.public_key);
678 std::size_t raw_length = b64_input(
679 from_c(one_time_key_message), message_length, from_c(session)->last_error
681 if (raw_length == std::size_t(-1)) {
682 return std::size_t(-1);
684 bool matches = from_c(session)->matches_inbound_session(
685 &identity_key, from_c(one_time_key_message), raw_length
687 return matches ? 1 : 0;
691 size_t olm_remove_one_time_keys(
692 OlmAccount * account,
695 size_t result = from_c(account)->remove_key(
696 from_c(session)->bob_one_time_key
698 if (result == std::size_t(-1)) {
699 from_c(account)->last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
705 size_t olm_encrypt_message_type(
706 OlmSession const * session
708 return size_t(from_c(session)->encrypt_message_type());
712 size_t olm_encrypt_random_length(
713 OlmSession const * session
715 return from_c(session)->encrypt_random_length();
719 size_t olm_encrypt_message_length(
720 OlmSession const * session,
721 size_t plaintext_length
723 return b64_output_length(
724 from_c(session)->encrypt_message_length(plaintext_length)
730 OlmSession * session,
731 void const * plaintext, size_t plaintext_length,
732 void * random, size_t random_length,
733 void * message, size_t message_length
735 std::size_t raw_length = from_c(session)->encrypt_message_length(
738 if (message_length < b64_output_length(raw_length)) {
739 from_c(session)->last_error =
740 OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
741 return std::size_t(-1);
743 std::size_t result = from_c(session)->encrypt(
744 from_c(plaintext), plaintext_length,
745 from_c(random), random_length,
746 b64_output_pos(from_c(message), raw_length), raw_length
748 olm::unset(random, random_length);
749 if (result == std::size_t(-1)) {
752 return b64_output(from_c(message), raw_length);
756 size_t olm_decrypt_max_plaintext_length(
757 OlmSession * session,
759 void * message, size_t message_length
761 std::size_t raw_length = b64_input(
762 from_c(message), message_length, from_c(session)->last_error
764 if (raw_length == std::size_t(-1)) {
765 return std::size_t(-1);
767 return from_c(session)->decrypt_max_plaintext_length(
768 olm::MessageType(message_type), from_c(message), raw_length
774 OlmSession * session,
776 void * message, size_t message_length,
777 void * plaintext, size_t max_plaintext_length
779 std::size_t raw_length = b64_input(
780 from_c(message), message_length, from_c(session)->last_error
782 if (raw_length == std::size_t(-1)) {
783 return std::size_t(-1);
785 return from_c(session)->decrypt(
786 olm::MessageType(message_type), from_c(message), raw_length,
787 from_c(plaintext), max_plaintext_length
792 size_t olm_sha256_length(
793 OlmUtility const * utility
795 return b64_output_length(from_c(utility)->sha256_length());
800 OlmUtility * utility,
801 void const * input, size_t input_length,
802 void * output, size_t output_length
804 std::size_t raw_length = from_c(utility)->sha256_length();
805 if (output_length < b64_output_length(raw_length)) {
806 from_c(utility)->last_error =
807 OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
808 return std::size_t(-1);
810 std::size_t result = from_c(utility)->sha256(
811 from_c(input), input_length,
812 b64_output_pos(from_c(output), raw_length), raw_length
814 if (result == std::size_t(-1)) {
817 return b64_output(from_c(output), raw_length);
821 size_t olm_ed25519_verify(
822 OlmUtility * utility,
823 void const * key, size_t key_length,
824 void const * message, size_t message_length,
825 void * signature, size_t signature_length
827 if (olm::decode_base64_length(key_length) != CURVE25519_KEY_LENGTH) {
828 from_c(utility)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
829 return std::size_t(-1);
831 _olm_ed25519_public_key verify_key;
832 olm::decode_base64(from_c(key), key_length, verify_key.public_key);
833 std::size_t raw_signature_length = b64_input(
834 from_c(signature), signature_length, from_c(utility)->last_error
836 if (raw_signature_length == std::size_t(-1)) {
837 return std::size_t(-1);
839 return from_c(utility)->ed25519_verify(
841 from_c(message), message_length,
842 from_c(signature), raw_signature_length