diff --git a/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts b/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts index 784961b5..b921a6a8 100644 --- a/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts +++ b/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts @@ -13,40 +13,41 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; import anotherjson from "another-json"; +import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; import {HASHES_LIST, MAC_LIST, SAS_SET, KEY_AGREEMENT_LIST} from "./constants"; import {CancelTypes, VerificationEventTypes} from "../channel/types"; import {SendKeyStage} from "./SendKeyStage"; +// from element-web +function intersection(anArray: T[], aSet: Set): T[] { + return Array.isArray(anArray) ? anArray.filter((x) => aSet.has(x)) : []; +} + export class SendAcceptVerificationStage extends BaseSASVerificationStage { async completeStage() { await this.log.wrap("SendAcceptVerificationStage.completeStage", async (log) => { - const { content } = this.channel.startMessage; - const keyAgreement = intersection(KEY_AGREEMENT_LIST, new Set(content.key_agreement_protocols))[0]; - const hashMethod = intersection(HASHES_LIST, new Set(content.hashes))[0]; - const macMethod = intersection(MAC_LIST, new Set(content.message_authentication_codes))[0]; - const sasMethods = intersection(content.short_authentication_string, SAS_SET); - if (!(keyAgreement !== undefined && hashMethod !== undefined && macMethod !== undefined && sasMethods.length)) { + const {content: startMessage} = this.channel.startMessage; + const keyAgreement = intersection(KEY_AGREEMENT_LIST, new Set(startMessage.key_agreement_protocols))[0]; + const hashMethod = intersection(HASHES_LIST, new Set(startMessage.hashes))[0]; + const macMethod = intersection(MAC_LIST, new Set(startMessage.message_authentication_codes))[0]; + const sasMethod = intersection(startMessage.short_authentication_string, SAS_SET); + if (!keyAgreement || !hashMethod || !macMethod || !sasMethod.length) { await this.channel.cancelVerification(CancelTypes.UnknownMethod); return; } const ourPubKey = this.olmSAS.get_pubkey(); - const commitmentStr = ourPubKey + anotherjson.stringify(content); - const contentToSend = { + const commitmentStr = ourPubKey + anotherjson.stringify(startMessage); + const content = { key_agreement_protocol: keyAgreement, hash: hashMethod, message_authentication_code: macMethod, - short_authentication_string: sasMethods, + short_authentication_string: sasMethod, commitment: this.olmUtil.sha256(commitmentStr), }; - await this.channel.send(VerificationEventTypes.Accept, contentToSend, log); + await this.channel.send(VerificationEventTypes.Accept, content, log); await this.channel.waitForEvent(VerificationEventTypes.Key); this.setNextStage(new SendKeyStage(this.options)); }); } } - -function intersection(anArray: T[], aSet: Set): T[] { - return Array.isArray(anArray) ? anArray.filter((x) => aSet.has(x)) : []; -}