Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasFella committed Nov 4, 2024
1 parent 9a2b1c1 commit 1fda4d1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
9 changes: 7 additions & 2 deletions Quotient/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ void Connection::Private::processOutgoingRequests()
}
requestsInFlight += id;
if (request.request_type() == 0) { // Keys upload
q->callApi<UploadKeysJob>(fromJson<DeviceKeys>(jsonFromRust(request.keys_upload_device_keys())), fromJson<OneTimeKeys>(jsonFromRust(request.keys_upload_one_time_keys())), fromJson<OneTimeKeys>(jsonFromRust(request.keys_upload_fallback_keys()))).then([id, this](const auto& job) {
auto deviceKeys = jsonFromRust(request.keys_upload_device_keys());
q->callApi<UploadKeysJob>(deviceKeys.isEmpty() ? std::nullopt : std::make_optional(fromJson<DeviceKeys>(deviceKeys)), fromJson<OneTimeKeys>(jsonFromRust(request.keys_upload_one_time_keys())), fromJson<OneTimeKeys>(jsonFromRust(request.keys_upload_fallback_keys()))).then([id, this](const auto& job) {
(*cryptoMachine)->mark_keys_upload_as_sent(bytesToRust(job->rawData()), stringToRust(id));
requestsInFlight.removeAll(id);
}, [this, id](const auto& job){
Expand Down Expand Up @@ -1951,7 +1952,8 @@ void Connection::Private::startKeyVerification(KeyVerificationSession* session)

void Connection::Private::confirmKeyVerification(KeyVerificationSession* session)
{
for (const auto& request : (*cryptoMachine)->confirm_verification(stringToRust(session->remoteUser()), stringToRust(session->verificationId()))) {
auto requests = (*cryptoMachine)->confirm_verification(stringToRust(session->remoteUser()), stringToRust(session->verificationId()));
for (const auto& request : requests->verification_requests()) {
if (!session->room()) {
const auto& type = stringFromRust(request.to_device_event_type());
q->callApi<SendToDeviceJob>(type, stringFromRust(request.to_device_txn_id()), fromJson<QHash<UserId, QHash<QString, QJsonObject>>>(jsonFromRust(request.to_device_messages())));
Expand All @@ -1972,6 +1974,9 @@ void Connection::Private::confirmKeyVerification(KeyVerificationSession* session
}
}
}
if (requests->has_signature_request()) {
q->callApi<UploadCrossSigningSignaturesJob>(fromJson<QHash<UserId, QHash<QString, QJsonObject>>>(jsonFromRust(requests->signature_request_content())));
}
session->setSasState(sasState(session));
}

Expand Down
64 changes: 46 additions & 18 deletions matrix-rust-sdk-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,32 +637,35 @@ impl CryptoMachine {
&mut self,
remote_user: String,
verification_id: String,
) -> Vec<OutgoingKeyVerificationRequest> {
) -> Box<ConfirmRequests> {
self.runtime.block_on(async {
//TODO signature upload
let user_id = UserId::parse(remote_user).unwrap();
if let VerificationRequestState::Transitioned { verification } = self
let VerificationRequestState::Transitioned { verification } = self
.machine
.as_ref()
.unwrap()
.get_verification_request(&user_id, &verification_id)
.unwrap()
.state()
{
if let Verification::SasV1(sas) = verification {
sas.confirm()
.await
.unwrap()
.0
.iter()
.map(|it| OutgoingKeyVerificationRequest(it.clone()))
.collect()
} else {
panic!()
}
} else {
panic!()
}
else {
panic!();
};
let Verification::SasV1(sas) = verification else {
panic!();
};
let (outgoing_verification_requests, signature_upload_request) =
sas.confirm().await.unwrap();

let outgoing_verification_requests = outgoing_verification_requests
.iter()
.map(|it| OutgoingKeyVerificationRequest(it.clone()))
.collect();

Box::new(ConfirmRequests {
verification: outgoing_verification_requests,
signature: signature_upload_request,
})
})
}

Expand Down Expand Up @@ -1100,6 +1103,25 @@ impl CryptoMachine {
}
}

impl ConfirmRequests {
fn has_signature_request(&self) -> bool {
self.signature.is_some()
}

fn verification_requests(&self) -> Vec<OutgoingKeyVerificationRequest> {
self.verification.clone()
}

fn signature_request_content(&self) -> String {
serde_json::to_string(&self.signature.as_ref().unwrap().signed_keys).unwrap()
}
}

struct ConfirmRequests {
verification: Vec<OutgoingKeyVerificationRequest>,
signature: Option<upload_signatures::v3::Request>,
}

struct CreatedSession(VerificationRequest, OutgoingVerificationRequest);

impl CreatedSession {
Expand Down Expand Up @@ -1142,6 +1164,7 @@ impl Emoji {
}
}

#[derive(Clone)]
struct OutgoingKeyVerificationRequest(OutgoingVerificationRequest);

impl OutgoingKeyVerificationRequest {
Expand Down Expand Up @@ -1332,6 +1355,7 @@ mod ffi {
type CreatedSession;
type EncryptionInfo;
type MediaEncryptionInfo;
type ConfirmRequests;

// General CryptoMachine functions
fn init(
Expand Down Expand Up @@ -1435,7 +1459,7 @@ mod ffi {
self: &mut CryptoMachine,
remote_user: String,
verification_id: String,
) -> Vec<OutgoingKeyVerificationRequest>;
) -> Box<ConfirmRequests>;
fn start_sas(
self: &mut CryptoMachine,
remote_user: String,
Expand Down Expand Up @@ -1514,5 +1538,9 @@ mod ffi {
user_key_cipher: String,
user_key_mac: String,
);

fn verification_requests(self: &ConfirmRequests) -> Vec<OutgoingKeyVerificationRequest>;
fn has_signature_request(self: &ConfirmRequests) -> bool;
fn signature_request_content(self: &ConfirmRequests) -> String;
}
}

0 comments on commit 1fda4d1

Please sign in to comment.