OTK + Uploading keys

This commit is contained in:
timoreo 2023-12-09 15:37:06 +01:00
parent 77e9344057
commit 3a35dadf3d
Signed by: timoreo
GPG key ID: 121A72C3512BA288

View file

@ -189,6 +189,43 @@ json_t* get_fallback_keys(Matrix::Client& client, OlmAccount* acc) {
json_decref(keys); json_decref(keys);
return fallback_keys; return fallback_keys;
} }
json_t* get_unpublished_otk(Matrix::Client client, OlmAccount* acc) {
json_t * otk_keys = json_object();
// Extract keys
size_t acclen = olm_account_one_time_keys_length(acc);
auto acckeys = reinterpret_cast<char *>(malloc(acclen));
olm_account_one_time_keys(acc, acckeys, acclen);
json_t * keys = json_loads(acckeys, 0, nullptr);
free(acckeys);
json_t* keyobj = json_object_get(keys, "curve25519");
void* iter = json_object_iter(keyobj);
while (iter != nullptr) {
const char* keyid = json_object_iter_key(iter);
json_t* keyval = json_object_iter_value(iter);
json_t* tosign = json_object();
json_object_set_new(tosign, "key", keyval);
sign_json(client, acc, tosign);
json_object_set_new(otk_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign );
iter = json_object_iter_next(keyobj, iter);
}
json_decref(keys);
return otk_keys;
}
void save_keys(OlmAccount* acc) {
size_t pacclen = olm_pickle_account_length(acc);
auto m = reinterpret_cast<uint8_t *>(malloc(pacclen));
olm_pickle_account(acc, nullptr, 0, m, pacclen);
pacclen = _olm_decode_base64(m, pacclen, m);
FILE* file = fopen("secret-keys", "w");
fwrite(m, pacclen, 1, file);
free(m);
fclose(file);
}
OlmAccount* load_account(Matrix::Client client){ OlmAccount* load_account(Matrix::Client client){
auto* acc = static_cast<OlmAccount*>(malloc(olm_account_size())); auto* acc = static_cast<OlmAccount*>(malloc(olm_account_size()));
olm_account(acc); olm_account(acc);
@ -206,18 +243,7 @@ OlmAccount* load_account(Matrix::Client client){
generate_fallback_key(acc); generate_fallback_key(acc);
// Convert to pickled format // Convert to pickled format
size_t pacclen = olm_pickle_account_length(acc); save_keys(acc);
auto m = reinterpret_cast<uint8_t *>(malloc(pacclen));
olm_pickle_account(acc, nullptr, 0, m, pacclen);
pacclen = _olm_decode_base64(m, pacclen, m);
FILE* file = std::fopen("secret-keys", "w");
std::fwrite(m, pacclen, 1, file);
free(m);
std::fclose(file);
// Upload the keys --- // Upload the keys ---
// Build device key json // Build device key json
json_t * upload_keys = json_object(); json_t * upload_keys = json_object();
@ -230,8 +256,17 @@ OlmAccount* load_account(Matrix::Client client){
json_t* fallback_keys = get_fallback_keys(client, acc); json_t* fallback_keys = get_fallback_keys(client, acc);
// fallback keys are already signed // fallback keys are already signed
json_object_set(upload_keys, "fallback_keys", fallback_keys); json_object_set(upload_keys, "fallback_keys", fallback_keys);
json_decref(upload_keys);
// OTK keys
json_t* otk = get_unpublished_otk(client, acc);
// OTK keys are already signed
json_object_set(upload_keys, "one_time_keys", otk);
client.uploadKeys(upload_keys);
json_decref(upload_keys);
olm_account_mark_keys_as_published(acc);
// Whether a key or not is published is saved, let's save again
save_keys(acc);
} else { } else {
// Load from file // Load from file
std::ifstream::pos_type fsize = ifs.tellg(); std::ifstream::pos_type fsize = ifs.tellg();