1 /* Copyright 2015, 2016 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.
15 #include "olm/session.hh"
16 #include "olm/cipher.h"
17 #include "olm/crypto.h"
18 #include "olm/account.hh"
19 #include "olm/memory.hh"
20 #include "olm/message.hh"
21 #include "olm/pickle.hh"
28 static const std::uint8_t PROTOCOL_VERSION = 0x3;
30 static const std::uint8_t ROOT_KDF_INFO[] = "OLM_ROOT";
31 static const std::uint8_t RATCHET_KDF_INFO[] = "OLM_RATCHET";
32 static const std::uint8_t CIPHER_KDF_INFO[] = "OLM_KEYS";
34 static const olm::KdfInfo OLM_KDF_INFO = {
35 ROOT_KDF_INFO, sizeof(ROOT_KDF_INFO) - 1,
36 RATCHET_KDF_INFO, sizeof(RATCHET_KDF_INFO) - 1
39 static const struct _olm_cipher_aes_sha_256 OLM_CIPHER =
40 OLM_CIPHER_INIT_AES_SHA_256(CIPHER_KDF_INFO);
44 olm::Session::Session(
45 ) : ratchet(OLM_KDF_INFO, OLM_CIPHER_BASE(&OLM_CIPHER)),
46 last_error(OlmErrorCode::OLM_SUCCESS),
47 received_message(false) {
52 std::size_t olm::Session::new_outbound_session_random_length() const {
53 return CURVE25519_RANDOM_LENGTH * 2;
57 std::size_t olm::Session::new_outbound_session(
58 olm::Account const & local_account,
59 _olm_curve25519_public_key const & identity_key,
60 _olm_curve25519_public_key const & one_time_key,
61 std::uint8_t const * random, std::size_t random_length
63 if (random_length < new_outbound_session_random_length()) {
64 last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
65 return std::size_t(-1);
68 _olm_curve25519_key_pair base_key;
69 _olm_crypto_curve25519_generate_key(random, &base_key);
71 _olm_curve25519_key_pair ratchet_key;
72 _olm_crypto_curve25519_generate_key(random + CURVE25519_RANDOM_LENGTH, &ratchet_key);
74 _olm_curve25519_key_pair const & alice_identity_key_pair = (
75 local_account.identity_keys.curve25519_key
78 received_message = false;
79 alice_identity_key = alice_identity_key_pair.public_key;
80 alice_base_key = base_key.public_key;
81 bob_one_time_key = one_time_key;
83 // Calculate the shared secret S via triple DH
84 std::uint8_t secret[3 * CURVE25519_SHARED_SECRET_LENGTH];
85 std::uint8_t * pos = secret;
87 _olm_crypto_curve25519_shared_secret(&alice_identity_key_pair, &one_time_key, pos);
88 pos += CURVE25519_SHARED_SECRET_LENGTH;
89 _olm_crypto_curve25519_shared_secret(&base_key, &identity_key, pos);
90 pos += CURVE25519_SHARED_SECRET_LENGTH;
91 _olm_crypto_curve25519_shared_secret(&base_key, &one_time_key, pos);
93 ratchet.initialise_as_alice(secret, sizeof(secret), ratchet_key);
96 olm::unset(ratchet_key);
99 return std::size_t(0);
104 static bool check_message_fields(
105 olm::PreKeyMessageReader & reader, bool have_their_identity_key
108 ok = ok && (have_their_identity_key || reader.identity_key);
109 if (reader.identity_key) {
110 ok = ok && reader.identity_key_length == CURVE25519_KEY_LENGTH;
112 ok = ok && reader.message;
113 ok = ok && reader.base_key;
114 ok = ok && reader.base_key_length == CURVE25519_KEY_LENGTH;
115 ok = ok && reader.one_time_key;
116 ok = ok && reader.one_time_key_length == CURVE25519_KEY_LENGTH;
123 std::size_t olm::Session::new_inbound_session(
124 olm::Account & local_account,
125 _olm_curve25519_public_key const * their_identity_key,
126 std::uint8_t const * one_time_key_message, std::size_t message_length
128 olm::PreKeyMessageReader reader;
129 decode_one_time_key_message(reader, one_time_key_message, message_length);
131 if (!check_message_fields(reader, their_identity_key)) {
132 last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
133 return std::size_t(-1);
136 if (reader.identity_key && their_identity_key) {
137 bool same = 0 == std::memcmp(
138 their_identity_key->public_key, reader.identity_key, CURVE25519_KEY_LENGTH
141 last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
142 return std::size_t(-1);
146 olm::load_array(alice_identity_key.public_key, reader.identity_key);
147 olm::load_array(alice_base_key.public_key, reader.base_key);
148 olm::load_array(bob_one_time_key.public_key, reader.one_time_key);
150 olm::MessageReader message_reader;
152 message_reader, reader.message, reader.message_length,
153 ratchet.ratchet_cipher->ops->mac_length(ratchet.ratchet_cipher)
156 if (!message_reader.ratchet_key
157 || message_reader.ratchet_key_length != CURVE25519_KEY_LENGTH) {
158 last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
159 return std::size_t(-1);
162 _olm_curve25519_public_key ratchet_key;
163 olm::load_array(ratchet_key.public_key, message_reader.ratchet_key);
165 olm::OneTimeKey const * our_one_time_key = local_account.lookup_key(
169 if (!our_one_time_key) {
170 last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
171 return std::size_t(-1);
174 _olm_curve25519_key_pair const & bob_identity_key = (
175 local_account.identity_keys.curve25519_key
177 _olm_curve25519_key_pair const & bob_one_time_key = our_one_time_key->key;
179 // Calculate the shared secret S via triple DH
180 std::uint8_t secret[CURVE25519_SHARED_SECRET_LENGTH * 3];
181 std::uint8_t * pos = secret;
182 _olm_crypto_curve25519_shared_secret(&bob_one_time_key, &alice_identity_key, pos);
183 pos += CURVE25519_SHARED_SECRET_LENGTH;
184 _olm_crypto_curve25519_shared_secret(&bob_identity_key, &alice_base_key, pos);
185 pos += CURVE25519_SHARED_SECRET_LENGTH;
186 _olm_crypto_curve25519_shared_secret(&bob_one_time_key, &alice_base_key, pos);
188 ratchet.initialise_as_bob(secret, sizeof(secret), ratchet_key);
192 return std::size_t(0);
196 std::size_t olm::Session::session_id_length() const {
197 return SHA256_OUTPUT_LENGTH;
201 std::size_t olm::Session::session_id(
202 std::uint8_t * id, std::size_t id_length
204 if (id_length < session_id_length()) {
205 last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
206 return std::size_t(-1);
208 std::uint8_t tmp[CURVE25519_KEY_LENGTH * 3];
209 std::uint8_t * pos = tmp;
210 pos = olm::store_array(pos, alice_identity_key.public_key);
211 pos = olm::store_array(pos, alice_base_key.public_key);
212 pos = olm::store_array(pos, bob_one_time_key.public_key);
213 _olm_crypto_sha256(tmp, sizeof(tmp), id);
214 return session_id_length();
218 bool olm::Session::matches_inbound_session(
219 _olm_curve25519_public_key const * their_identity_key,
220 std::uint8_t const * one_time_key_message, std::size_t message_length
222 olm::PreKeyMessageReader reader;
223 decode_one_time_key_message(reader, one_time_key_message, message_length);
225 if (!check_message_fields(reader, their_identity_key)) {
230 if (reader.identity_key) {
231 same = same && 0 == std::memcmp(
232 reader.identity_key, alice_identity_key.public_key, CURVE25519_KEY_LENGTH
235 if (their_identity_key) {
236 same = same && 0 == std::memcmp(
237 their_identity_key->public_key, alice_identity_key.public_key,
238 CURVE25519_KEY_LENGTH
241 same = same && 0 == std::memcmp(
242 reader.base_key, alice_base_key.public_key, CURVE25519_KEY_LENGTH
244 same = same && 0 == std::memcmp(
245 reader.one_time_key, bob_one_time_key.public_key, CURVE25519_KEY_LENGTH
251 olm::MessageType olm::Session::encrypt_message_type() const {
252 if (received_message) {
253 return olm::MessageType::MESSAGE;
255 return olm::MessageType::PRE_KEY;
260 std::size_t olm::Session::encrypt_message_length(
261 std::size_t plaintext_length
263 std::size_t message_length = ratchet.encrypt_output_length(
267 if (received_message) {
268 return message_length;
271 return encode_one_time_key_message_length(
272 CURVE25519_KEY_LENGTH,
273 CURVE25519_KEY_LENGTH,
274 CURVE25519_KEY_LENGTH,
280 std::size_t olm::Session::encrypt_random_length() const {
281 return ratchet.encrypt_random_length();
285 std::size_t olm::Session::encrypt(
286 std::uint8_t const * plaintext, std::size_t plaintext_length,
287 std::uint8_t const * random, std::size_t random_length,
288 std::uint8_t * message, std::size_t message_length
290 if (message_length < encrypt_message_length(plaintext_length)) {
291 last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
292 return std::size_t(-1);
294 std::uint8_t * message_body;
295 std::size_t message_body_length = ratchet.encrypt_output_length(
299 if (received_message) {
300 message_body = message;
302 olm::PreKeyMessageWriter writer;
303 encode_one_time_key_message(
306 CURVE25519_KEY_LENGTH,
307 CURVE25519_KEY_LENGTH,
308 CURVE25519_KEY_LENGTH,
312 olm::store_array(writer.one_time_key, bob_one_time_key.public_key);
313 olm::store_array(writer.identity_key, alice_identity_key.public_key);
314 olm::store_array(writer.base_key, alice_base_key.public_key);
315 message_body = writer.message;
318 std::size_t result = ratchet.encrypt(
319 plaintext, plaintext_length,
320 random, random_length,
321 message_body, message_body_length
324 if (result == std::size_t(-1)) {
325 last_error = ratchet.last_error;
326 ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
334 std::size_t olm::Session::decrypt_max_plaintext_length(
335 MessageType message_type,
336 std::uint8_t const * message, std::size_t message_length
338 std::uint8_t const * message_body;
339 std::size_t message_body_length;
340 if (message_type == olm::MessageType::MESSAGE) {
341 message_body = message;
342 message_body_length = message_length;
344 olm::PreKeyMessageReader reader;
345 decode_one_time_key_message(reader, message, message_length);
346 if (!reader.message) {
347 last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
348 return std::size_t(-1);
350 message_body = reader.message;
351 message_body_length = reader.message_length;
354 std::size_t result = ratchet.decrypt_max_plaintext_length(
355 message_body, message_body_length
358 if (result == std::size_t(-1)) {
359 last_error = ratchet.last_error;
360 ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
366 std::size_t olm::Session::decrypt(
367 olm::MessageType message_type,
368 std::uint8_t const * message, std::size_t message_length,
369 std::uint8_t * plaintext, std::size_t max_plaintext_length
371 std::uint8_t const * message_body;
372 std::size_t message_body_length;
373 if (message_type == olm::MessageType::MESSAGE) {
374 message_body = message;
375 message_body_length = message_length;
377 olm::PreKeyMessageReader reader;
378 decode_one_time_key_message(reader, message, message_length);
379 if (!reader.message) {
380 last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
381 return std::size_t(-1);
383 message_body = reader.message;
384 message_body_length = reader.message_length;
387 std::size_t result = ratchet.decrypt(
388 message_body, message_body_length, plaintext, max_plaintext_length
391 if (result == std::size_t(-1)) {
392 last_error = ratchet.last_error;
393 ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
397 received_message = true;
401 // make the description end with "..." instead of stopping abruptly with no
403 void elide_description(char *end) {
410 void olm::Session::describe(char *describe_buffer, size_t buflen) {
411 // how much of the buffer is remaining (this is an int rather than a size_t
412 // because it will get compared to the return value from snprintf)
413 int remaining = buflen;
414 // do nothing if we have a zero-length buffer, or if buflen > INT_MAX,
415 // resulting in an overflow
416 if (remaining <= 0) return;
418 describe_buffer[0] = '\0';
419 // we need at least 23 characters to get any sort of meaningful
420 // information, so bail if we don't have that. (But more importantly, we
421 // need it to be at least 4 so that elide_description doesn't go out of
423 if (remaining < 23) return;
427 // check that snprintf didn't return an error or reach the end of the buffer
428 #define CHECK_SIZE_AND_ADVANCE \
429 if (size > remaining) { \
430 return elide_description(describe_buffer + remaining - 1); \
431 } else if (size > 0) { \
432 describe_buffer += size; \
439 describe_buffer, remaining,
440 "sender chain index: %ld ", ratchet.sender_chain[0].chain_key.index
442 CHECK_SIZE_AND_ADVANCE;
444 size = snprintf(describe_buffer, remaining, "receiver chain indices:");
445 CHECK_SIZE_AND_ADVANCE;
447 for (size_t i = 0; i < ratchet.receiver_chains.size(); ++i) {
449 describe_buffer, remaining,
450 " %ld", ratchet.receiver_chains[i].chain_key.index
452 CHECK_SIZE_AND_ADVANCE;
455 size = snprintf(describe_buffer, remaining, " skipped message keys:");
456 CHECK_SIZE_AND_ADVANCE;
458 for (size_t i = 0; i < ratchet.skipped_message_keys.size(); ++i) {
460 describe_buffer, remaining,
461 " %ld", ratchet.skipped_message_keys[i].message_key.index
463 CHECK_SIZE_AND_ADVANCE;
465 #undef CHECK_SIZE_AND_ADVANCE
469 // the master branch writes pickle version 1; the logging_enabled branch writes
471 static const std::uint32_t SESSION_PICKLE_VERSION = 1;
474 std::size_t olm::pickle_length(
475 Session const & value
477 std::size_t length = 0;
478 length += olm::pickle_length(SESSION_PICKLE_VERSION);
479 length += olm::pickle_length(value.received_message);
480 length += olm::pickle_length(value.alice_identity_key);
481 length += olm::pickle_length(value.alice_base_key);
482 length += olm::pickle_length(value.bob_one_time_key);
483 length += olm::pickle_length(value.ratchet);
488 std::uint8_t * olm::pickle(
490 Session const & value
492 pos = olm::pickle(pos, SESSION_PICKLE_VERSION);
493 pos = olm::pickle(pos, value.received_message);
494 pos = olm::pickle(pos, value.alice_identity_key);
495 pos = olm::pickle(pos, value.alice_base_key);
496 pos = olm::pickle(pos, value.bob_one_time_key);
497 pos = olm::pickle(pos, value.ratchet);
502 std::uint8_t const * olm::unpickle(
503 std::uint8_t const * pos, std::uint8_t const * end,
506 uint32_t pickle_version;
507 pos = olm::unpickle(pos, end, pickle_version); UNPICKLE_OK(pos);
509 bool includes_chain_index;
510 switch (pickle_version) {
512 includes_chain_index = false;
516 includes_chain_index = true;
520 value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION;
524 pos = olm::unpickle(pos, end, value.received_message); UNPICKLE_OK(pos);
525 pos = olm::unpickle(pos, end, value.alice_identity_key); UNPICKLE_OK(pos);
526 pos = olm::unpickle(pos, end, value.alice_base_key); UNPICKLE_OK(pos);
527 pos = olm::unpickle(pos, end, value.bob_one_time_key); UNPICKLE_OK(pos);
528 pos = olm::unpickle(pos, end, value.ratchet, includes_chain_index); UNPICKLE_OK(pos);