This commit is contained in:
timoreo 2023-12-09 23:40:19 +01:00
parent aa512aa4af
commit a4f6c72e1d
Signed by: timoreo
GPG key ID: 121A72C3512BA288

View file

@ -1,24 +1,20 @@
#include <3ds.h>
#include <matrixclient.h>
#include <memorystore.h>
#include <olm/olm.h>
#include <olm/account.hh>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memorystore.h>
#include <olm/account.hh>
#define PREFIX_DIRECTORY "./matrix-storage"
#define TOKEN_FILENAME "token"
#define DEVICE_ID "XREVXEDLHU"
class Store : public Matrix::Store {
void setSyncToken(std::string token) override {
}
std::string getSyncToken() override {
return "";
}
void setSyncToken(std::string token) override {} //TODO store in file
std::string getSyncToken() override { return ""; }
void setFilterId(std::string filterId) override {}
std::string getFilterId() override { return ""; }
};
@ -30,7 +26,7 @@ std::string ask_for_pass() {
swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, 0, 0);
swkbdSetPasswordMode(&swkbd, SWKBD_PASSWORD_HIDE_DELAY);
swkbdSetHintText(&swkbd, "Enter password");
char buf[256]{0};
char buf[256]{ 0 };
swkbdInputText(&swkbd, buf, sizeof(buf));
return buf;
}
@ -40,12 +36,11 @@ void generate_otk(olm::Account& acc, size_t otkcount) {
PS_GenerateRandomBytes(otkrandom.get(), len);
acc.generate_one_time_keys(otkcount, otkrandom.get(), len);
}
void generate_otk(olm::Account& acc){
size_t otkcount = acc.max_number_of_one_time_keys()/2;
void generate_otk(olm::Account& acc) {
size_t otkcount = acc.max_number_of_one_time_keys() / 2;
generate_otk(acc, otkcount);
}
void generate_device_key(olm::Account& acc) {
size_t len = acc.new_account_random_length();
std::unique_ptr<uint8_t[]> random = std::make_unique<uint8_t[]>(len);
@ -67,15 +62,15 @@ void sign_json(Matrix::Client& client, olm::Account& acc, json_t* json) {
std::string signature(signlen, '\0');
acc.sign(reinterpret_cast<const uint8_t*>(jsonStr), strlen(jsonStr), reinterpret_cast<uint8_t*>(signature.data()), signlen);
free(jsonStr);
json_t * signitem = json_object();
json_t* signitem = json_object();
json_object_set(signitem, ("ed25519:" + client.getDeviceId()).c_str(), json_stringn(signature.data(), signlen));
json_t * signobj = json_object();
json_t* signobj = json_object();
json_object_set(signobj, client.getUserId().c_str(), signitem);
json_object_set(json, "signatures", signobj);
}
json_t* get_device_keys(Matrix::Client& client, olm::Account& acc) {
json_t * device_keys = json_object();
json_t * algorithms = json_array();
json_t* device_keys = json_object();
json_t* algorithms = json_array();
json_array_append_new(algorithms, json_string("m.olm.v1.curve25519-aes-sha2"));
json_array_append_new(algorithms, json_string("m.megolm.v1.aes-sha2"));
json_object_set_new(device_keys, "algorithms", algorithms);
@ -90,9 +85,9 @@ json_t* get_device_keys(Matrix::Client& client, olm::Account& acc) {
json_error_t error;
sleep(5);
std::cout << "Heading into it..." << std::endl;
json_t * keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, &error);
if(keys == nullptr){
printf("error: on line %d at char %d: %s\n", error.line, error.column ,error.text);
json_t* keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, &error);
if (keys == nullptr) {
printf("error: on line %d at char %d: %s\n", error.line, error.column, error.text);
sleep(10);
}
std::cout << "Reloaded data" << std::endl;
@ -106,12 +101,12 @@ json_t* get_device_keys(Matrix::Client& client, olm::Account& acc) {
return device_keys;
}
json_t* get_fallback_keys(Matrix::Client& client, olm::Account& acc) {
json_t * fallback_keys = json_object();
json_t* fallback_keys = json_object();
// Extract keys
size_t acclen = acc.get_unpublished_fallback_key_json_length();
auto acckeys = std::make_unique<uint8_t[]>(acclen);
size_t bytes = acc.get_unpublished_fallback_key_json(acckeys.get(), acclen);
json_t * keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
json_t* keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
json_t* keyobj = json_object_get(keys, "curve25519");
void* iter = json_object_iter(keyobj);
@ -124,7 +119,7 @@ json_t* get_fallback_keys(Matrix::Client& client, olm::Account& acc) {
json_object_set_new(tosign, "fallback", json_true());
sign_json(client, acc, tosign);
json_object_set_new(fallback_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign );
json_object_set_new(fallback_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign);
iter = json_object_iter_next(keyobj, iter);
}
json_decref(keys);
@ -132,12 +127,12 @@ json_t* get_fallback_keys(Matrix::Client& client, olm::Account& acc) {
}
json_t* get_unpublished_otk(Matrix::Client& client, olm::Account& acc) {
json_t * otk_keys = json_object();
json_t* otk_keys = json_object();
// Extract keys
size_t acclen = acc.get_one_time_keys_json_length();
auto acckeys = std::make_unique<uint8_t []>(acclen);
auto acckeys = std::make_unique<uint8_t[]>(acclen);
size_t bytes = acc.get_one_time_keys_json(acckeys.get(), acclen);
json_t * keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
json_t* keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
json_t* keyobj = json_object_get(keys, "curve25519");
void* iter = json_object_iter(keyobj);
@ -149,7 +144,7 @@ json_t* get_unpublished_otk(Matrix::Client& client, olm::Account& acc) {
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 );
json_object_set_new(otk_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign);
iter = json_object_iter_next(keyobj, iter);
}
json_decref(keys);
@ -158,16 +153,16 @@ json_t* get_unpublished_otk(Matrix::Client& client, olm::Account& acc) {
void save_keys(olm::Account& acc) {
size_t pacclen = olm::pickle_length(acc);
auto m = std::make_unique<uint8_t []>(pacclen);
auto m = std::make_unique<uint8_t[]>(pacclen);
olm::pickle(m.get(), acc);
FILE* file = fopen("secret-keys", "w");
fwrite(m.get(), pacclen, 1, file);
fclose(file);
}
void load_account(Matrix::Client& client, olm::Account& acc){
void load_account(Matrix::Client& client, olm::Account& acc) {
// std::FILE* file = std::fopen("secret-keys", "r");
std::ifstream ifs("secret-keys", std::ios::binary|std::ios::ate);
std::ifstream ifs("secret-keys", std::ios::binary | std::ios::ate);
if (!ifs.is_open()) {
std::cout << "No secret keys found, generating " << std::endl;
// Generate and save to file
@ -186,7 +181,7 @@ void load_account(Matrix::Client& client, olm::Account& acc){
std::cout << "Saved keys postgen." << std::endl;
// Upload the keys ---
// Build device key json
json_t * upload_keys = json_object();
json_t* upload_keys = json_object();
json_t* device_keys = get_device_keys(client, acc);
std::cout << "Gathered device keys" << std::endl;
@ -215,18 +210,17 @@ void load_account(Matrix::Client& client, olm::Account& acc){
} else {
// Load from file
std::ifstream::pos_type fsize = ifs.tellg();
if(fsize == -1){
if (fsize == -1) {
std::cout << "What." << std::endl;
return;
}
auto data = std::make_unique<uint8_t []>(fsize);
auto data = std::make_unique<uint8_t[]>(fsize);
ifs.seekg(0, std::ios::beg);
ifs.read(reinterpret_cast<char*>(data.get()), static_cast<std::streamsize>(fsize));
olm::unpickle(data.get(),data.get() + fsize, acc);
olm::unpickle(data.get(), data.get() + fsize, acc);
}
}
int main() {
fsInit();
gfxInitDefault();
@ -249,14 +243,14 @@ int main() {
break;
}
if (kDown & KEY_SELECT) {
std::ifstream stream{TOKEN_FILENAME, std::fstream::in};
if(!stream.is_open()){
std::ifstream stream{ TOKEN_FILENAME, std::fstream::in };
if (!stream.is_open()) {
// File dosn't exist, log in
if (client.login("timoreo-3ds", ask_for_pass(), DEVICE_ID)) {
// logged in
puts("Logged in !!");
std::ofstream ostr{TOKEN_FILENAME, std::fstream::out};
std::ofstream ostr{ TOKEN_FILENAME, std::fstream::out };
ostr << client.getToken() << std::endl;
ostr.close();
} else {
@ -264,33 +258,32 @@ int main() {
puts("Wrong combo !");
goto wrong;
}
}else{
} else {
std::string line;
std::getline(stream, line);
client = Matrix::Client("https://matrix.timoreo.fr", line, nullptr); // &s
client = Matrix::Client("https://matrix.timoreo.fr", line, nullptr); // &s
stream.close();
}
auto userid = client.getUserId();
if(userid.empty()){
if (userid.empty()) {
puts("Userid is empty ! Wrong token ?");
goto wrong;
}
std::cout << "Logged in as " << userid << std::endl;
// Load encryption here
load_account(client, *acc);
client.setRoomInfoCallback([](std::string roomId, Matrix::RoomInfo info){
client.setRoomInfoCallback([](std::string roomId, Matrix::RoomInfo info) {
consoleSelect(bottom);
printf("Joined room %s named %s\n", roomId.c_str(), info.name.c_str());
});
client.setEventCallback([](std::string roomId, json_t* event){
client.setEventCallback([](std::string roomId, json_t* event) {
consoleSelect(bottom);
printf("Event in room %s\n", roomId.c_str());
printf("%s\n", json_dumps(event, JSON_ENSURE_ASCII));
});
client.startSyncLoop();
}
wrong:
;
wrong:;
}
client.stopSyncLoop();
// DO NOT LOGOUT, KEEP ENCRYPTION KEYS