Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element R: Implement requestOwnUserVerification #3508

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import fetchMock from "fetch-mock-jest";
import { MockResponse } from "fetch-mock";
import "fake-indexeddb/auto";

import { MockResponse } from "fetch-mock";
import fetchMock from "fetch-mock-jest";
import { IDBFactory } from "fake-indexeddb";

import { createClient, CryptoEvent, MatrixClient } from "../../../src";
import {
canAcceptVerificationRequest,
Expand Down Expand Up @@ -67,6 +69,13 @@ beforeAll(async () => {
await global.Olm.init();
});

afterEach(() => {
// reset fake-indexeddb after each test, to make sure we don't leak connections
// cf https://github.com/dumbmatter/fakeIndexedDB#wipingresetting-the-indexeddb-for-a-fresh-state
// eslint-disable-next-line no-global-assign
indexedDB = new IDBFactory();
});

// restore the original global.crypto
afterAll(() => {
if (previousCrypto === undefined) {
Expand Down Expand Up @@ -156,6 +165,12 @@ function runTests(backend: string, initCrypto: InitCrypto, methods: string[] | u
it("can verify another device via SAS", async () => {
await waitForDeviceList();

// initially there should be no verifications in progress
{
const requests = aliceClient.getCrypto()!.getVerificationRequestsToDeviceInProgress(TEST_USER_ID);
expect(requests.length).toEqual(0);
}

// have alice initiate a verification. She should send a m.key.verification.request
let [requestBody, request] = await Promise.all([
expectSendToDeviceMessage("m.key.verification.request"),
Expand All @@ -171,6 +186,13 @@ function runTests(backend: string, initCrypto: InitCrypto, methods: string[] | u
expect(request.initiatedByMe).toBe(true);
expect(request.otherUserId).toEqual(TEST_USER_ID);

// and now the request should be visible via `getVerificationRequestsToDeviceInProgress`
{
const requests = aliceClient.getCrypto()!.getVerificationRequestsToDeviceInProgress(TEST_USER_ID);
expect(requests.length).toEqual(1);
expect(requests[0].transactionId).toEqual(transactionId);
}

let toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
expect(toDeviceMessage.from_device).toEqual(aliceClient.deviceId);
expect(toDeviceMessage.transaction_id).toEqual(transactionId);
Expand Down Expand Up @@ -304,6 +326,35 @@ function runTests(backend: string, initCrypto: InitCrypto, methods: string[] | u
olmSAS.free();
});

it("Can make a verification request to *all* devices", async () => {
// we need an existing cross-signing key for this
e2eKeyResponder.addCrossSigningData(SIGNED_CROSS_SIGNING_KEYS_DATA);
await waitForDeviceList();

// have alice initiate a verification. She should send a m.key.verification.request
const [requestBody, request] = await Promise.all([
expectSendToDeviceMessage("m.key.verification.request"),
aliceClient.getCrypto()!.requestOwnUserVerification(),
]);

const transactionId = request.transactionId;
expect(transactionId).toBeDefined();
expect(request.phase).toEqual(VerificationPhase.Requested);

// and now the request should be visible via `getVerificationRequestsToDeviceInProgress`
{
const requests = aliceClient.getCrypto()!.getVerificationRequestsToDeviceInProgress(TEST_USER_ID);
expect(requests.length).toEqual(1);
expect(requests[0].transactionId).toEqual(transactionId);
}

// legacy crypto picks devices individually; rust crypto uses a broadcast message
const toDeviceMessage =
requestBody.messages[TEST_USER_ID]["*"] ?? requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
expect(toDeviceMessage.from_device).toEqual(aliceClient.deviceId);
expect(toDeviceMessage.transaction_id).toEqual(transactionId);
});

oldBackendOnly("can verify another via QR code with an untrusted cross-signing key", async () => {
// QRCode fails if we don't yet have the cross-signing keys, so make sure we have them now.
e2eKeyResponder.addCrossSigningData(SIGNED_CROSS_SIGNING_KEYS_DATA);
Expand Down
22 changes: 19 additions & 3 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,12 @@ export class RustCrypto implements CryptoBackend {
* @returns the VerificationRequests that are in progress
*/
public getVerificationRequestsToDeviceInProgress(userId: string): VerificationRequest[] {
// TODO
return [];
const requests: RustSdkCryptoJs.VerificationRequest[] = this.olmMachine.getVerificationRequests(
new RustSdkCryptoJs.UserId(this.userId),
);
return requests
.filter((request) => request.roomId === undefined)
.map((request) => new RustVerificationRequest(request, this.outgoingRequestProcessor));
}

/**
Expand Down Expand Up @@ -580,7 +584,19 @@ export class RustCrypto implements CryptoBackend {
* @returns a VerificationRequest when the request has been sent to the other party.
*/
public async requestOwnUserVerification(): Promise<VerificationRequest> {
throw new Error("not implemented");
const userIdentity: RustSdkCryptoJs.OwnUserIdentity | undefined = await this.olmMachine.getIdentity(
new RustSdkCryptoJs.UserId(this.userId),
);
if (userIdentity === undefined) {
throw new Error("cannot request verification for this device when there is no existing cross-signing key");
}

const [request, outgoingRequest]: [RustSdkCryptoJs.VerificationRequest, RustSdkCryptoJs.ToDeviceRequest] =
await userIdentity.requestVerification(
this.supportedVerificationMethods?.map(verificationMethodIdentifierToMethod),
);
await this.outgoingRequestProcessor.makeOutgoingRequest(outgoingRequest);
return new RustVerificationRequest(request, this.outgoingRequestProcessor);
}

/**
Expand Down