Skip to content

Commit

Permalink
Reduce duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
duxovni committed Oct 15, 2022
1 parent 3ec8058 commit 8d29d5f
Showing 1 changed file with 42 additions and 63 deletions.
105 changes: 42 additions & 63 deletions src/crypto/verification/SAS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,45 @@ export class SAS extends Base<SasEvent, EventHandlerMap> {
return startContent;
}

private async verifyAndCheckMAC(
keyAgreement: string,
sasMethods: string[],
olmSAS: OlmSAS,
macMethod: string,
): Promise<void> {
const sasBytes = calculateKeyAgreement[keyAgreement](this, olmSAS, 6);
const verifySAS = new Promise<void>((resolve, reject) => {
this.sasEvent = {
sas: generateSas(sasBytes, sasMethods),
confirm: async () => {
try {
await this.sendMAC(olmSAS, macMethod);
resolve();
} catch (err) {
reject(err);
}
},
cancel: () => reject(newUserCancelledError()),
mismatch: () => reject(newMismatchedSASError()),
};
this.emit(SasEvent.ShowSas, this.sasEvent);
});

const [e] = await Promise.all([
this.waitForEvent(EventType.KeyVerificationMac)
.then((e) => {
// we don't expect any more messages from the other
// party, and they may send a m.key.verification.done
// when they're done on their end
this.expectedEvent = EventType.KeyVerificationDone;
return e;
}),
verifySAS,
]);
const content = e.getContent();
await this.checkMAC(olmSAS, content, macMethod);
}

private async doSendVerification(): Promise<void> {
this.waitingForAccept = true;
let startContent;
Expand Down Expand Up @@ -367,37 +406,7 @@ export class SAS extends Base<SasEvent, EventHandlerMap> {
this.theirSASPubKey = content.key;
olmSAS.set_their_key(content.key);

const sasBytes = calculateKeyAgreement[keyAgreement](this, olmSAS, 6);
const verifySAS = new Promise<void>((resolve, reject) => {
this.sasEvent = {
sas: generateSas(sasBytes, sasMethods),
confirm: async () => {
try {
await this.sendMAC(olmSAS, macMethod);
resolve();
} catch (err) {
reject(err);
}
},
cancel: () => reject(newUserCancelledError()),
mismatch: () => reject(newMismatchedSASError()),
};
this.emit(SasEvent.ShowSas, this.sasEvent);
});

[e] = await Promise.all([
this.waitForEvent(EventType.KeyVerificationMac)
.then((e) => {
// we don't expect any more messages from the other
// party, and they may send a m.key.verification.done
// when they're done on their end
this.expectedEvent = EventType.KeyVerificationDone;
return e;
}),
verifySAS,
]);
content = e.getContent();
await this.checkMAC(olmSAS, content, macMethod);
await this.verifyAndCheckMAC(keyAgreement, sasMethods, olmSAS, macMethod);
} finally {
olmSAS.free();
}
Expand Down Expand Up @@ -433,7 +442,7 @@ export class SAS extends Base<SasEvent, EventHandlerMap> {
commitment: olmutil.sha256(commitmentStr),
});

let e = await this.waitForEvent(EventType.KeyVerificationKey);
const e = await this.waitForEvent(EventType.KeyVerificationKey);
// FIXME: make sure event is properly formed
content = e.getContent();
this.theirSASPubKey = content.key;
Expand All @@ -443,37 +452,7 @@ export class SAS extends Base<SasEvent, EventHandlerMap> {
key: this.ourSASPubKey,
});

const sasBytes = calculateKeyAgreement[keyAgreement](this, olmSAS, 6);
const verifySAS = new Promise<void>((resolve, reject) => {
this.sasEvent = {
sas: generateSas(sasBytes, sasMethods),
confirm: async () => {
try {
await this.sendMAC(olmSAS, macMethod);
resolve();
} catch (err) {
reject(err);
}
},
cancel: () => reject(newUserCancelledError()),
mismatch: () => reject(newMismatchedSASError()),
};
this.emit(SasEvent.ShowSas, this.sasEvent);
});

[e] = await Promise.all([
this.waitForEvent(EventType.KeyVerificationMac)
.then((e) => {
// we don't expect any more messages from the other
// party, and they may send a m.key.verification.done
// when they're done on their end
this.expectedEvent = EventType.KeyVerificationDone;
return e;
}),
verifySAS,
]);
content = e.getContent();
await this.checkMAC(olmSAS, content, macMethod);
await this.verifyAndCheckMAC(keyAgreement, sasMethods, olmSAS, macMethod);
} finally {
olmSAS.free();
}
Expand Down

0 comments on commit 8d29d5f

Please sign in to comment.