full_conversation_test.go 8.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
package otr3

import (
	"crypto/rand"
	"testing"
	"time"
)

func Test_conversation_SMPStateMachineStartsAtSmpExpect1(t *testing.T) {
	c := newConversation(otrV3{}, fixtureRand())
	assertEquals(t, c.smp.state, smpStateExpect1{})
}

func Test_receive_generatesErrorIfDoesNotHaveASecureChannel(t *testing.T) {
	states := []msgState{
		plainText, finished,
	}
	c := bobContextAfterAKE()
	c.msgState = encrypted
	smpMsg := fixtureMessage1()
	dataMsg, _, _ := c.genDataMsg(nil, smpMsg.tlv())
	m := dataMsg.serialize(c.version)
	m, _ = c.wrapMessageHeader(msgTypeData, m)
	for _, s := range states {
		c.msgState = s
		c.expectMessageEvent(t, func() {
			_, _, err := c.receiveDecoded(m)
			assertEquals(t, err, errMessageNotInPrivate)
		}, MessageEventReceivedMessageNotInPrivate, nil, nil)
	}
}

func Test_receive_doesntGenerateErrorIfThereIsNoSecureChannelButTheMessageIsIGNORE_UNREADABLE(t *testing.T) {
	states := []msgState{
		plainText, finished,
	}
	c := bobContextAfterAKE()
	c.msgState = encrypted
	smpMsg := fixtureMessage1()
	dataMsg, _, _ := c.genDataMsgWithFlag(nil, messageFlagIgnoreUnreadable, smpMsg.tlv())
	m, _ := c.wrapMessageHeader(msgTypeData, dataMsg.serialize(c.version))

	for _, s := range states {
		c.msgState = s
		_, _, err := c.receiveDecoded(m)
		assertNil(t, err)
	}
}

func Test_AKE_forVersion3And2InThePolicy(t *testing.T) {
	alice := &Conversation{Rand: rand.Reader}
	alice.SetOurKeys([]PrivateKey{alicePrivateKey})
	alice.ourCurrentKey = alicePrivateKey
	alice.Policies = policies(allowV2 | allowV3)

	bob := &Conversation{Rand: rand.Reader}
	bob.SetOurKeys([]PrivateKey{bobPrivateKey})
	bob.ourCurrentKey = bobPrivateKey
	bob.Policies = policies(allowV2 | allowV3)

	var toSend []ValidMessage
	var err error
	msg := alice.QueryMessage()

	//Alice send Bob queryMsg
	_, toSend, err = bob.Receive(msg)
	assertEquals(t, err, nil)
	assertEquals(t, bob.ake.state, authStateAwaitingDHKey{})

	//Bob send Alice DHCommit
	_, toSend, err = alice.Receive(toSend[0])
	assertEquals(t, alice.ake.state, authStateAwaitingRevealSig{})
	assertEquals(t, err, nil)

	//Alice send Bob DHKey
	_, toSend, err = bob.Receive(toSend[0])
	m, _ := bob.decode(encodedMessage(toSend[0]))
	assertEquals(t, err, nil)
	assertDeepEquals(t, bob.ake.state, authStateAwaitingSig{revealSigMsg: m})

	//Bob send Alice RevealSig
	_, toSend, err = alice.Receive(toSend[0])
	assertEquals(t, err, nil)
	assertEquals(t, alice.ake.state, authStateNone{})

	//Alice send Bob Sig
	_, toSend, err = bob.Receive(toSend[0])
	assertEquals(t, err, nil)
	assertEquals(t, bob.ake.state, authStateNone{})

	// "When starting a private Conversation [...],
	// generate two DH key pairs for yourself, and set our_keyid = 2"
	assertEquals(t, alice.keys.ourKeyID, uint32(2))
	assertEquals(t, alice.keys.ourCurrentDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, alice.keys.ourCurrentDHKeys.pub.BitLen() > 0, true)
	assertEquals(t, alice.keys.ourPreviousDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, alice.keys.ourPreviousDHKeys.pub.BitLen() > 0, true)

	assertEquals(t, bob.keys.ourKeyID, uint32(2))
	assertEquals(t, bob.keys.ourCurrentDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, bob.keys.ourCurrentDHKeys.pub.BitLen() > 0, true)
	assertEquals(t, bob.keys.ourPreviousDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, bob.keys.ourPreviousDHKeys.pub.BitLen() > 0, true)
}

func Test_AKE_withVersion3ButWithoutVersion2InThePolicy(t *testing.T) {
	alice := &Conversation{Rand: rand.Reader}
	alice.SetOurKeys([]PrivateKey{alicePrivateKey})
	alice.ourCurrentKey = alicePrivateKey
	alice.Policies = policies(allowV3)

	bob := &Conversation{Rand: rand.Reader}
	bob.SetOurKeys([]PrivateKey{bobPrivateKey})
	bob.ourCurrentKey = bobPrivateKey
	bob.Policies = policies(allowV3)

	var toSend []ValidMessage
	var err error
	msg := alice.QueryMessage()

	//Alice send Bob queryMsg
	_, toSend, err = bob.Receive(msg)
	assertEquals(t, err, nil)
	assertEquals(t, bob.ake.state, authStateAwaitingDHKey{})

	//Bob send Alice DHCommit
	_, toSend, err = alice.Receive(toSend[0])
	assertEquals(t, alice.ake.state, authStateAwaitingRevealSig{})
	assertEquals(t, err, nil)

	//Alice send Bob DHKey
	_, toSend, err = bob.Receive(toSend[0])
	m, _ := bob.decode(encodedMessage(toSend[0]))
	assertEquals(t, err, nil)
	assertDeepEquals(t, bob.ake.state, authStateAwaitingSig{revealSigMsg: m})

	//Bob send Alice RevealSig
	_, toSend, err = alice.Receive(toSend[0])
	assertEquals(t, err, nil)
	assertEquals(t, alice.ake.state, authStateNone{})

	//Alice send Bob Sig
	_, toSend, err = bob.Receive(toSend[0])
	assertEquals(t, err, nil)
	assertEquals(t, bob.ake.state, authStateNone{})

	// "When starting a private Conversation [...],
	// generate two DH key pairs for yourself, and set our_keyid = 2"
	assertEquals(t, alice.keys.ourKeyID, uint32(2))
	assertEquals(t, alice.keys.ourCurrentDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, alice.keys.ourCurrentDHKeys.pub.BitLen() > 0, true)
	assertEquals(t, alice.keys.ourPreviousDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, alice.keys.ourPreviousDHKeys.pub.BitLen() > 0, true)

	assertEquals(t, bob.keys.ourKeyID, uint32(2))
	assertEquals(t, bob.keys.ourCurrentDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, bob.keys.ourCurrentDHKeys.pub.BitLen() > 0, true)
	assertEquals(t, bob.keys.ourPreviousDHKeys.priv.BitLen() > 0, true)
	assertEquals(t, bob.keys.ourPreviousDHKeys.pub.BitLen() > 0, true)
}

func Test_processDataMessageShouldExtractData(t *testing.T) {
	var toSend []ValidMessage
	var err error

	alice := &Conversation{Rand: rand.Reader}
	alice.Policies = policies(allowV2 | allowV3)
	alice.SetOurKeys([]PrivateKey{alicePrivateKey})

	bob := &Conversation{Rand: rand.Reader}
	bob.Policies = policies(allowV2 | allowV3)
	bob.SetOurKeys([]PrivateKey{bobPrivateKey})

	msg := []byte("?OTRv3?")

	//Alice send Bob queryMsg
	_, toSend, err = bob.Receive(msg)
	assertNil(t, err)
	assertEquals(t, bob.ake.state, authStateAwaitingDHKey{})
	assertEquals(t, bob.version, otrV3{})

	//Bob send Alice DHCommit
	_, toSend, err = alice.Receive(toSend[0])
	assertEquals(t, alice.ake.state, authStateAwaitingRevealSig{})
	assertNil(t, err)

	//Alice send Bob DHKey
	_, toSend, err = bob.Receive(toSend[0])
	m, _ := bob.decode(encodedMessage(toSend[0]))
	assertNil(t, err)
	assertDeepEquals(t, bob.ake.state, authStateAwaitingSig{revealSigMsg: m})

	//Bob send Alice RevealSig
	_, toSend, err = alice.Receive(toSend[0])
	assertNil(t, err)
	assertEquals(t, alice.ake.state, authStateNone{})

	//Alice send Bob Sig
	_, toSend, err = bob.Receive(toSend[0])
	assertNil(t, err)
	assertEquals(t, bob.ake.state, authStateNone{})

	// Alice sends a message to bob
	msg = []byte("hello")
	dataMsg, _, _ := alice.genDataMsg(msg)
	m, _ = alice.wrapMessageHeader(msgTypeData, dataMsg.serialize(alice.version))

	bob.updateLastSent()
	plain, ret, err := bob.receiveDecoded(m)

	assertNil(t, err)
	assertDeepEquals(t, plain, MessagePlaintext(msg))
	assertNil(t, ret)
}

func Test_startingAKE_shouldNotBreakTheEncryptedChannel(t *testing.T) {
	var hello = []byte("hello")
	var toSend []ValidMessage
	var err error

	alice := &Conversation{Rand: rand.Reader}
	alice.Policies = policies(allowV2 | allowV3)
	alice.SetOurKeys([]PrivateKey{alicePrivateKey})

	bob := &Conversation{Rand: rand.Reader}
	bob.Policies = policies(allowV2 | allowV3)
	bob.SetOurKeys([]PrivateKey{bobPrivateKey})

	//Alice send Bob queryMsg
	_, toSend, err = bob.Receive(alice.QueryMessage())
	assertNil(t, err)
	assertEquals(t, bob.ake.state, authStateAwaitingDHKey{})
	assertEquals(t, bob.version, otrV3{})

	//Bob send Alice DHCommit
	_, toSend, err = alice.Receive(toSend[0])
	assertEquals(t, alice.ake.state, authStateAwaitingRevealSig{})
	assertNil(t, err)

	//Alice send Bob DHKey
	_, toSend, err = bob.Receive(toSend[0])
	dec, _ := bob.decode(encodedMessage(toSend[0]))
	assertNil(t, err)
	assertDeepEquals(t, bob.ake.state, authStateAwaitingSig{revealSigMsg: dec})

	//Bob send Alice RevealSig
	_, toSend, err = alice.Receive(toSend[0])
	assertNil(t, err)
	assertEquals(t, alice.ake.state, authStateNone{})

	//Alice send Bob Sig
	_, toSend, err = bob.Receive(toSend[0])
	assertNil(t, err)
	assertEquals(t, bob.ake.state, authStateNone{})

	// Alice sends a message to bob
	m, err := alice.Send(hello)

	bob.updateLastSent()
	plain, ret, err := bob.Receive(m[0])

	assertNil(t, err)
	assertDeepEquals(t, plain, MessagePlaintext(hello))
	assertNil(t, ret)

	//
	// Restarts the AKE
	//

	//Alice send Bob queryMsg
	bob.lastMessageStateChange = time.Time{}
	bob.ake.lastStateChange = time.Time{}
	_, toSend, err = bob.Receive(alice.QueryMessage())
	assertNil(t, err)
	assertEquals(t, bob.ake.state, authStateAwaitingDHKey{})
	assertEquals(t, bob.version, otrV3{})

	//Alice sends Bob a data message
	m, err = alice.Send(hello)
	assertNil(t, err)

	plain, ret, err = bob.Receive(m[0])
	assertNil(t, err)
	assertDeepEquals(t, plain, MessagePlaintext(hello))
	assertNil(t, ret)
}