Skip to content

Commit

Permalink
Element-R: await /keys/query during Verification requests
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Nov 29, 2023
1 parent 48d9d9b commit db629d1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
.map(
(request) =>
new RustVerificationRequest(
this.olmMachine,
request,
this.outgoingRequestProcessor,
this._supportedVerificationMethods,
Expand Down Expand Up @@ -963,6 +964,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv

if (request) {
return new RustVerificationRequest(
this.olmMachine,
request,
this.outgoingRequestProcessor,
this._supportedVerificationMethods,
Expand Down Expand Up @@ -998,6 +1000,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
methods,
);
return new RustVerificationRequest(
this.olmMachine,
request,
this.outgoingRequestProcessor,
this._supportedVerificationMethods,
Expand Down Expand Up @@ -1073,6 +1076,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
);
await this.outgoingRequestProcessor.makeOutgoingRequest(outgoingRequest);
return new RustVerificationRequest(
this.olmMachine,
request,
this.outgoingRequestProcessor,
this._supportedVerificationMethods,
Expand Down Expand Up @@ -1111,6 +1115,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
);
await this.outgoingRequestProcessor.makeOutgoingRequest(outgoingRequest);
return new RustVerificationRequest(
this.olmMachine,
request,
this.outgoingRequestProcessor,
this._supportedVerificationMethods,
Expand Down Expand Up @@ -1398,7 +1403,12 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
if (request) {
this.emit(
CryptoEvent.VerificationRequestReceived,
new RustVerificationRequest(request, this.outgoingRequestProcessor, this._supportedVerificationMethods),
new RustVerificationRequest(
this.olmMachine,
request,
this.outgoingRequestProcessor,
this._supportedVerificationMethods,
),
);
}
}
Expand Down Expand Up @@ -1615,6 +1625,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
this.emit(
CryptoEvent.VerificationRequestReceived,
new RustVerificationRequest(
this.olmMachine,
request,
this.outgoingRequestProcessor,
this._supportedVerificationMethods,
Expand Down
26 changes: 23 additions & 3 deletions src/rust-crypto/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ export class RustVerificationRequest
/**
* Construct a new RustVerificationRequest to wrap the rust-level `VerificationRequest`.
*
* @param inner - VerificationRequest from the Rust SDK
* @param outgoingRequestProcessor - `OutgoingRequestProcessor` to use for making outgoing HTTP requests
* @param supportedVerificationMethods - Verification methods to use when `accept()` is called
* @param olmMachine - The `OlmMachine` from the underlying rust crypto sdk.
* @param inner - VerificationRequest from the Rust SDK.
* @param outgoingRequestProcessor - `OutgoingRequestProcessor` to use for making outgoing HTTP requests.
* @param supportedVerificationMethods - Verification methods to use when `accept()` is called.
*/
public constructor(
private readonly olmMachine: RustSdkCryptoJs.OlmMachine,
private readonly inner: RustSdkCryptoJs.VerificationRequest,
private readonly outgoingRequestProcessor: OutgoingRequestProcessor,
private readonly supportedVerificationMethods: string[],
Expand Down Expand Up @@ -135,6 +137,15 @@ export class RustVerificationRequest
return this.inner.otherDeviceId?.toString();
}

/** Get the other device involved in the verification, if it is known */
private async getOtherDevice(): Promise<undefined | RustSdkCryptoJs.Device> {
const otherDeviceId = this.inner.otherDeviceId;
if (!otherDeviceId) {
return undefined;
}
return await this.olmMachine.getDevice(this.inner.otherUserId, otherDeviceId, 5);

Check failure on line 146 in src/rust-crypto/verification.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Expected 2 arguments, but got 3.
}

/** True if the other party in this request is one of this user's own devices. */
public get isSelfVerification(): boolean {
return this.inner.isSelfVerification();
Expand Down Expand Up @@ -322,6 +333,10 @@ export class RustVerificationRequest
throw new Error(`Unsupported verification method ${method}`);
}

if (!(await this.getOtherDevice())) {
throw new Error("startVerification(): other device is unknown");
}

const res:
| [RustSdkCryptoJs.Sas, RustSdkCryptoJs.RoomMessageRequest | RustSdkCryptoJs.ToDeviceRequest]
| undefined = await this.inner.startSas();
Expand Down Expand Up @@ -392,6 +407,11 @@ export class RustVerificationRequest
* Implementation of {@link Crypto.VerificationRequest#generateQRCode}.
*/
public async generateQRCode(): Promise<Buffer | undefined> {
// make sure that we have a list of the other user's devices (workaround https://github.com/matrix-org/matrix-rust-sdk/issues/2896)
if (!(await this.getOtherDevice())) {
throw new Error("generateQRCode(): other device is unknown");
}

const innerVerifier: RustSdkCryptoJs.Qr | undefined = await this.inner.generateQrCode();
// If we are unable to generate a QRCode, we return undefined
if (!innerVerifier) return;
Expand Down

0 comments on commit db629d1

Please sign in to comment.