aboutsummaryrefslogtreecommitdiff
path: root/src/inbound_group_session.c
diff options
context:
space:
mode:
authorRichard van der Hoff <github@rvanderhoff.org.uk>2017-01-10 15:39:42 +0000
committerGitHub <noreply@github.com>2017-01-10 15:39:42 +0000
commit860740a91ee0efcf3c594ece23ef0a38ddda394e (patch)
tree32bc83b640221ff5c983b92fb3a90c6d73d7c85d /src/inbound_group_session.c
parent14c30da0e2bdff675c8af97e3c6ee49fba82af8d (diff)
parentc04b770cd3c96aa3a55ff3b6d817ba5b6f6f6922 (diff)
Merge pull request #42 from matrix-org/rav/megolm_export
Export and import of megolm session data
Diffstat (limited to 'src/inbound_group_session.c')
-rw-r--r--src/inbound_group_session.c191
1 files changed, 161 insertions, 30 deletions
diff --git a/src/inbound_group_session.c b/src/inbound_group_session.c
index a54e55f..a78404d 100644
--- a/src/inbound_group_session.c
+++ b/src/inbound_group_session.c
@@ -30,8 +30,9 @@
#define OLM_PROTOCOL_VERSION 3
#define GROUP_SESSION_ID_LENGTH ED25519_PUBLIC_KEY_LENGTH
-#define PICKLE_VERSION 1
+#define PICKLE_VERSION 2
#define SESSION_KEY_VERSION 2
+#define SESSION_EXPORT_VERSION 1
struct OlmInboundGroupSession {
/** our earliest known ratchet value */
@@ -43,6 +44,17 @@ struct OlmInboundGroupSession {
/** The ed25519 signing key */
struct _olm_ed25519_public_key signing_key;
+ /**
+ * Have we ever seen any evidence that this is a valid session?
+ * (either because the original session share was signed, or because we
+ * have subsequently successfully decrypted a message)
+ *
+ * (We don't do anything with this currently, but we may want to bear it in
+ * mind when we consider handling key-shares for sessions we already know
+ * about.)
+ */
+ int signing_key_verified;
+
enum OlmErrorCode last_error;
};
@@ -71,19 +83,24 @@ size_t olm_clear_inbound_group_session(
return sizeof(OlmInboundGroupSession);
}
+#define SESSION_EXPORT_RAW_LENGTH \
+ (1 + 4 + MEGOLM_RATCHET_LENGTH + ED25519_PUBLIC_KEY_LENGTH)
+
#define SESSION_KEY_RAW_LENGTH \
(1 + 4 + MEGOLM_RATCHET_LENGTH + ED25519_PUBLIC_KEY_LENGTH\
+ ED25519_SIGNATURE_LENGTH)
-/** init the session keys from the un-base64-ed session keys */
static size_t _init_group_session_keys(
OlmInboundGroupSession *session,
- const uint8_t *key_buf
+ const uint8_t *key_buf,
+ int export_format
) {
+ const uint8_t expected_version =
+ (export_format ? SESSION_EXPORT_VERSION : SESSION_KEY_VERSION);
const uint8_t *ptr = key_buf;
size_t version = *ptr++;
- if (version != SESSION_KEY_VERSION) {
+ if (version != expected_version) {
session->last_error = OLM_BAD_SESSION_KEY;
return (size_t)-1;
}
@@ -103,11 +120,15 @@ static size_t _init_group_session_keys(
);
ptr += ED25519_PUBLIC_KEY_LENGTH;
- if (!_olm_crypto_ed25519_verify(
- &session->signing_key, key_buf, ptr - key_buf, ptr
- )) {
- session->last_error = OLM_BAD_SIGNATURE;
- return (size_t)-1;
+ if (!export_format) {
+ if (!_olm_crypto_ed25519_verify(&session->signing_key, key_buf,
+ ptr - key_buf, ptr)) {
+ session->last_error = OLM_BAD_SIGNATURE;
+ return (size_t)-1;
+ }
+
+ /* signed keyshare */
+ session->signing_key_verified = 1;
}
return 0;
}
@@ -131,11 +152,35 @@ size_t olm_init_inbound_group_session(
}
_olm_decode_base64(session_key, session_key_length, key_buf);
- result = _init_group_session_keys(session, key_buf);
+ result = _init_group_session_keys(session, key_buf, 0);
_olm_unset(key_buf, SESSION_KEY_RAW_LENGTH);
return result;
}
+size_t olm_import_inbound_group_session(
+ OlmInboundGroupSession *session,
+ const uint8_t * session_key, size_t session_key_length
+) {
+ uint8_t key_buf[SESSION_EXPORT_RAW_LENGTH];
+ size_t raw_length = _olm_decode_base64_length(session_key_length);
+ size_t result;
+
+ if (raw_length == (size_t)-1) {
+ session->last_error = OLM_INVALID_BASE64;
+ return (size_t)-1;
+ }
+
+ if (raw_length != SESSION_EXPORT_RAW_LENGTH) {
+ session->last_error = OLM_BAD_SESSION_KEY;
+ return (size_t)-1;
+ }
+
+ _olm_decode_base64(session_key, session_key_length, key_buf);
+ result = _init_group_session_keys(session, key_buf, 1);
+ _olm_unset(key_buf, SESSION_EXPORT_RAW_LENGTH);
+ return result;
+}
+
static size_t raw_pickle_length(
const OlmInboundGroupSession *session
) {
@@ -144,6 +189,7 @@ static size_t raw_pickle_length(
length += megolm_pickle_length(&session->initial_ratchet);
length += megolm_pickle_length(&session->latest_ratchet);
length += _olm_pickle_ed25519_public_key_length(&session->signing_key);
+ length += _olm_pickle_bool_length(session->signing_key_verified);
return length;
}
@@ -171,6 +217,7 @@ size_t olm_pickle_inbound_group_session(
pos = megolm_pickle(&session->initial_ratchet, pos);
pos = megolm_pickle(&session->latest_ratchet, pos);
pos = _olm_pickle_ed25519_public_key(pos, &session->signing_key);
+ pos = _olm_pickle_bool(pos, session->signing_key_verified);
return _olm_enc_output(key, key_length, pickled, raw_length);
}
@@ -194,7 +241,7 @@ size_t olm_unpickle_inbound_group_session(
pos = pickled;
end = pos + raw_length;
pos = _olm_unpickle_uint32(pos, end, &pickle_version);
- if (pickle_version != PICKLE_VERSION) {
+ if (pickle_version < 1 || pickle_version > PICKLE_VERSION) {
session->last_error = OLM_UNKNOWN_PICKLE_VERSION;
return (size_t)-1;
}
@@ -202,6 +249,14 @@ size_t olm_unpickle_inbound_group_session(
pos = megolm_unpickle(&session->latest_ratchet, pos, end);
pos = _olm_unpickle_ed25519_public_key(pos, end, &session->signing_key);
+ if (pickle_version == 1) {
+ /* pickle v1 had no signing_key_verified field (all keyshares were
+ * verified at import time) */
+ session->signing_key_verified = 1;
+ } else {
+ pos = _olm_unpickle_bool(pos, end, &(session->signing_key_verified));
+ }
+
if (end != pos) {
/* We had the wrong number of bytes in the input. */
session->last_error = OLM_CORRUPTED_PICKLE;
@@ -258,6 +313,32 @@ size_t olm_group_decrypt_max_plaintext_length(
}
/**
+ * get a copy of the megolm ratchet, advanced
+ * to the relevant index. Returns 0 on success, -1 on error
+ */
+static size_t _get_megolm(
+ OlmInboundGroupSession *session, uint32_t message_index, Megolm *result
+) {
+ /* pick a megolm instance to use. If we're at or beyond the latest ratchet
+ * value, use that */
+ if ((message_index - session->latest_ratchet.counter) < (1U << 31)) {
+ megolm_advance_to(&session->latest_ratchet, message_index);
+ *result = session->latest_ratchet;
+ return 0;
+ } else if ((message_index - session->initial_ratchet.counter) >= (1U << 31)) {
+ /* the counter is before our intial ratchet - we can't decode this. */
+ session->last_error = OLM_UNKNOWN_MESSAGE_INDEX;
+ return (size_t)-1;
+ } else {
+ /* otherwise, start from the initial megolm. Take a copy so that we
+ * don't overwrite the initial megolm */
+ *result = session->initial_ratchet;
+ megolm_advance_to(result, message_index);
+ return 0;
+ }
+}
+
+/**
* decrypt an un-base64-ed message
*/
static size_t _decrypt(
@@ -268,8 +349,7 @@ static size_t _decrypt(
) {
struct _OlmDecodeGroupMessageResults decoded_results;
size_t max_length, r;
- Megolm *megolm;
- Megolm tmp_megolm;
+ Megolm megolm;
_olm_decode_group_message(
message, message_length,
@@ -316,38 +396,30 @@ static size_t _decrypt(
return (size_t)-1;
}
- /* pick a megolm instance to use. If we're at or beyond the latest ratchet
- * value, use that */
- if ((decoded_results.message_index - session->latest_ratchet.counter) < (1U << 31)) {
- megolm = &session->latest_ratchet;
- } else if ((decoded_results.message_index - session->initial_ratchet.counter) >= (1U << 31)) {
- /* the counter is before our intial ratchet - we can't decode this. */
- session->last_error = OLM_UNKNOWN_MESSAGE_INDEX;
- return (size_t)-1;
- } else {
- /* otherwise, start from the initial megolm. Take a copy so that we
- * don't overwrite the initial megolm */
- tmp_megolm = session->initial_ratchet;
- megolm = &tmp_megolm;
+ r = _get_megolm(session, decoded_results.message_index, &megolm);
+ if (r == (size_t)-1) {
+ return r;
}
- megolm_advance_to(megolm, decoded_results.message_index);
-
/* now try checking the mac, and decrypting */
r = megolm_cipher->ops->decrypt(
megolm_cipher,
- megolm_get_data(megolm), MEGOLM_RATCHET_LENGTH,
+ megolm_get_data(&megolm), MEGOLM_RATCHET_LENGTH,
message, message_length,
decoded_results.ciphertext, decoded_results.ciphertext_length,
plaintext, max_plaintext_length
);
- _olm_unset(&tmp_megolm, sizeof(tmp_megolm));
+ _olm_unset(&megolm, sizeof(megolm));
if (r == (size_t)-1) {
session->last_error = OLM_BAD_MESSAGE_MAC;
return r;
}
+ /* once we have successfully decrypted a message, set a flag to say the
+ * session appears valid. */
+ session->signing_key_verified = 1;
+
return r;
}
@@ -391,3 +463,62 @@ size_t olm_inbound_group_session_id(
session->signing_key.public_key, GROUP_SESSION_ID_LENGTH, id
);
}
+
+uint32_t olm_inbound_group_session_first_known_index(
+ const OlmInboundGroupSession *session
+) {
+ return session->initial_ratchet.counter;
+}
+
+int olm_inbound_group_session_is_verified(
+ const OlmInboundGroupSession *session
+) {
+ return session->signing_key_verified;
+}
+
+size_t olm_export_inbound_group_session_length(
+ const OlmInboundGroupSession *session
+) {
+ return _olm_encode_base64_length(SESSION_EXPORT_RAW_LENGTH);
+}
+
+size_t olm_export_inbound_group_session(
+ OlmInboundGroupSession *session,
+ uint8_t * key, size_t key_length, uint32_t message_index
+) {
+ uint8_t *raw;
+ uint8_t *ptr;
+ Megolm megolm;
+ size_t r;
+ size_t encoded_length = olm_export_inbound_group_session_length(session);
+
+ if (key_length < encoded_length) {
+ session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
+ return (size_t)-1;
+ }
+
+ r = _get_megolm(session, message_index, &megolm);
+ if (r == (size_t)-1) {
+ return r;
+ }
+
+ /* put the raw data at the end of the output buffer. */
+ raw = ptr = key + encoded_length - SESSION_EXPORT_RAW_LENGTH;
+ *ptr++ = SESSION_EXPORT_VERSION;
+
+ // Encode message index as a big endian 32-bit number.
+ for (unsigned i = 0; i < 4; i++) {
+ *ptr++ = 0xFF & (message_index >> 24); message_index <<= 8;
+ }
+
+ memcpy(ptr, megolm_get_data(&megolm), MEGOLM_RATCHET_LENGTH);
+ ptr += MEGOLM_RATCHET_LENGTH;
+
+ memcpy(
+ ptr, session->signing_key.public_key,
+ ED25519_PUBLIC_KEY_LENGTH
+ );
+ ptr += ED25519_PUBLIC_KEY_LENGTH;
+
+ return _olm_encode_base64(raw, SESSION_EXPORT_RAW_LENGTH, key);
+}