|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import "../../olm-loader"; |
| 18 | +import { RendezvousFailureReason, RendezvousIntent } from "../../../src/rendezvous"; |
| 19 | +import { MSC3903ECDHPayload, MSC3903ECDHv1RendezvousChannel } from "../../../src/rendezvous/channels"; |
| 20 | +import { decodeBase64 } from "../../../src/crypto/olmlib"; |
| 21 | +import { DummyTransport } from "./DummyTransport"; |
| 22 | + |
| 23 | +function makeTransport(name: string) { |
| 24 | + return new DummyTransport<any, MSC3903ECDHPayload>(name, { type: "dummy" }); |
| 25 | +} |
| 26 | + |
| 27 | +describe("ECDHv1", function () { |
| 28 | + beforeAll(async function () { |
| 29 | + await global.Olm.init(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe("with crypto", () => { |
| 33 | + it("initiator wants to sign in", async function () { |
| 34 | + const aliceTransport = makeTransport("Alice"); |
| 35 | + const bobTransport = makeTransport("Bob"); |
| 36 | + aliceTransport.otherParty = bobTransport; |
| 37 | + bobTransport.otherParty = aliceTransport; |
| 38 | + |
| 39 | + // alice is signing in initiates and generates a code |
| 40 | + const alice = new MSC3903ECDHv1RendezvousChannel(aliceTransport); |
| 41 | + const aliceCode = await alice.generateCode(RendezvousIntent.LOGIN_ON_NEW_DEVICE); |
| 42 | + const bob = new MSC3903ECDHv1RendezvousChannel(bobTransport, decodeBase64(aliceCode.rendezvous.key)); |
| 43 | + |
| 44 | + const bobChecksum = await bob.connect(); |
| 45 | + const aliceChecksum = await alice.connect(); |
| 46 | + |
| 47 | + expect(aliceChecksum).toEqual(bobChecksum); |
| 48 | + |
| 49 | + const message = { key: "xxx" }; |
| 50 | + await alice.send(message); |
| 51 | + const bobReceive = await bob.receive(); |
| 52 | + expect(bobReceive).toEqual(message); |
| 53 | + |
| 54 | + await alice.cancel(RendezvousFailureReason.Unknown); |
| 55 | + await bob.cancel(RendezvousFailureReason.Unknown); |
| 56 | + }); |
| 57 | + |
| 58 | + it("initiator wants to reciprocate", async function () { |
| 59 | + const aliceTransport = makeTransport("Alice"); |
| 60 | + const bobTransport = makeTransport("Bob"); |
| 61 | + aliceTransport.otherParty = bobTransport; |
| 62 | + bobTransport.otherParty = aliceTransport; |
| 63 | + |
| 64 | + // alice is signing in initiates and generates a code |
| 65 | + const alice = new MSC3903ECDHv1RendezvousChannel(aliceTransport); |
| 66 | + const aliceCode = await alice.generateCode(RendezvousIntent.LOGIN_ON_NEW_DEVICE); |
| 67 | + const bob = new MSC3903ECDHv1RendezvousChannel(bobTransport, decodeBase64(aliceCode.rendezvous.key)); |
| 68 | + |
| 69 | + const bobChecksum = await bob.connect(); |
| 70 | + const aliceChecksum = await alice.connect(); |
| 71 | + |
| 72 | + expect(aliceChecksum).toEqual(bobChecksum); |
| 73 | + |
| 74 | + const message = { key: "xxx" }; |
| 75 | + await bob.send(message); |
| 76 | + const aliceReceive = await alice.receive(); |
| 77 | + expect(aliceReceive).toEqual(message); |
| 78 | + |
| 79 | + await alice.cancel(RendezvousFailureReason.Unknown); |
| 80 | + await bob.cancel(RendezvousFailureReason.Unknown); |
| 81 | + }); |
| 82 | + |
| 83 | + it("double connect", async function () { |
| 84 | + const aliceTransport = makeTransport("Alice"); |
| 85 | + const bobTransport = makeTransport("Bob"); |
| 86 | + aliceTransport.otherParty = bobTransport; |
| 87 | + bobTransport.otherParty = aliceTransport; |
| 88 | + |
| 89 | + // alice is signing in initiates and generates a code |
| 90 | + const alice = new MSC3903ECDHv1RendezvousChannel(aliceTransport); |
| 91 | + const aliceCode = await alice.generateCode(RendezvousIntent.LOGIN_ON_NEW_DEVICE); |
| 92 | + const bob = new MSC3903ECDHv1RendezvousChannel(bobTransport, decodeBase64(aliceCode.rendezvous.key)); |
| 93 | + |
| 94 | + const bobChecksum = await bob.connect(); |
| 95 | + const aliceChecksum = await alice.connect(); |
| 96 | + |
| 97 | + expect(aliceChecksum).toEqual(bobChecksum); |
| 98 | + |
| 99 | + expect(alice.connect()).rejects.toThrow(); |
| 100 | + |
| 101 | + await alice.cancel(RendezvousFailureReason.Unknown); |
| 102 | + await bob.cancel(RendezvousFailureReason.Unknown); |
| 103 | + }); |
| 104 | + |
| 105 | + it("closed", async function () { |
| 106 | + const aliceTransport = makeTransport("Alice"); |
| 107 | + const bobTransport = makeTransport("Bob"); |
| 108 | + aliceTransport.otherParty = bobTransport; |
| 109 | + bobTransport.otherParty = aliceTransport; |
| 110 | + |
| 111 | + // alice is signing in initiates and generates a code |
| 112 | + const alice = new MSC3903ECDHv1RendezvousChannel(aliceTransport); |
| 113 | + const aliceCode = await alice.generateCode(RendezvousIntent.LOGIN_ON_NEW_DEVICE); |
| 114 | + const bob = new MSC3903ECDHv1RendezvousChannel(bobTransport, decodeBase64(aliceCode.rendezvous.key)); |
| 115 | + |
| 116 | + const bobChecksum = await bob.connect(); |
| 117 | + const aliceChecksum = await alice.connect(); |
| 118 | + |
| 119 | + expect(aliceChecksum).toEqual(bobChecksum); |
| 120 | + |
| 121 | + alice.close(); |
| 122 | + |
| 123 | + expect(alice.connect()).rejects.toThrow(); |
| 124 | + expect(alice.send({})).rejects.toThrow(); |
| 125 | + expect(alice.receive()).rejects.toThrow(); |
| 126 | + |
| 127 | + await alice.cancel(RendezvousFailureReason.Unknown); |
| 128 | + await bob.cancel(RendezvousFailureReason.Unknown); |
| 129 | + }); |
| 130 | + |
| 131 | + it("require ciphertext", async function () { |
| 132 | + const aliceTransport = makeTransport("Alice"); |
| 133 | + const bobTransport = makeTransport("Bob"); |
| 134 | + aliceTransport.otherParty = bobTransport; |
| 135 | + bobTransport.otherParty = aliceTransport; |
| 136 | + |
| 137 | + // alice is signing in initiates and generates a code |
| 138 | + const alice = new MSC3903ECDHv1RendezvousChannel(aliceTransport); |
| 139 | + const aliceCode = await alice.generateCode(RendezvousIntent.LOGIN_ON_NEW_DEVICE); |
| 140 | + const bob = new MSC3903ECDHv1RendezvousChannel(bobTransport, decodeBase64(aliceCode.rendezvous.key)); |
| 141 | + |
| 142 | + const bobChecksum = await bob.connect(); |
| 143 | + const aliceChecksum = await alice.connect(); |
| 144 | + |
| 145 | + expect(aliceChecksum).toEqual(bobChecksum); |
| 146 | + |
| 147 | + // send a message without encryption |
| 148 | + await aliceTransport.send({ iv: "dummy", ciphertext: "dummy" }); |
| 149 | + expect(bob.receive()).rejects.toThrow(); |
| 150 | + |
| 151 | + await alice.cancel(RendezvousFailureReason.Unknown); |
| 152 | + await bob.cancel(RendezvousFailureReason.Unknown); |
| 153 | + }); |
| 154 | + |
| 155 | + it("ciphertext before set up", async function () { |
| 156 | + const aliceTransport = makeTransport("Alice"); |
| 157 | + const bobTransport = makeTransport("Bob"); |
| 158 | + aliceTransport.otherParty = bobTransport; |
| 159 | + bobTransport.otherParty = aliceTransport; |
| 160 | + |
| 161 | + // alice is signing in initiates and generates a code |
| 162 | + const alice = new MSC3903ECDHv1RendezvousChannel(aliceTransport); |
| 163 | + await alice.generateCode(RendezvousIntent.LOGIN_ON_NEW_DEVICE); |
| 164 | + |
| 165 | + await bobTransport.send({ iv: "dummy", ciphertext: "dummy" }); |
| 166 | + |
| 167 | + expect(alice.receive()).rejects.toThrow(); |
| 168 | + |
| 169 | + await alice.cancel(RendezvousFailureReason.Unknown); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments