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
|
package otr3
import "testing"
func Test_StartAuthenticate_failsIfWeAreNotCurrentlyEncrypted(t *testing.T) {
c := newConversation(otrV3{}, fixtureRand())
c.msgState = plainText
_, e := c.StartAuthenticate("", []byte("hello world"))
assertEquals(t, e, errCantAuthenticateWithoutEncryption)
}
func Test_StartAuthenticate_failsIfThereIsntEnoughRandomness(t *testing.T) {
c := bobContextAfterAKE()
c.Rand = fixedRand([]string{"ABCD"})
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = alicePrivateKey
c.theirKey = bobPrivateKey.PublicKey()
_, e := c.StartAuthenticate("", []byte("hello world"))
assertEquals(t, e, errShortRandomRead)
}
func Test_StartAuthenticate_generatesAnSMPSecretFromTheSharedSecret(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = alicePrivateKey
c.theirKey = bobPrivateKey.PublicKey()
_, e := c.StartAuthenticate("", []byte("hello world"))
assertEquals(t, e, nil)
assertDeepEquals(t, c.smp.secret, bnFromHex("3D7264BD983B8CA53CB365444844816F7D2453580B552EEE45CD09CA13614A5"))
}
func Test_StartAuthenticate_generatesAndReturnsTheFirstSMPMessageToSend(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
msg, e := c.StartAuthenticate("", []byte("hello world"))
assertEquals(t, e, nil)
assertEquals(t, c.smp.state, smpStateExpect2{})
dec, _ := c.decode(encodedMessage(msg[0]))
_, messageBody, _ := c.parseMessageHeader(dec)
assertDeepEquals(t, len(messageBody), 1361)
}
func Test_StartAuthenticate_generatesAndSetsTheFirstMessageOnTheConversation(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.s1 = nil
c.StartAuthenticate("", []byte("hello world"))
assertNotNil(t, c.smp.s1)
assertEquals(t, c.smp.s1.msg.hasQuestion, false)
assertEquals(t, c.smp.s1.msg.tlv().tlvType, tlvTypeSMP1)
}
func Test_StartAuthenticate_generatesAn1QMessageIfAQuestionIsGiven(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.s1 = nil
c.StartAuthenticate("Where did we meet?", []byte("hello world"))
assertNotNil(t, c.smp.s1)
assertEquals(t, c.smp.s1.msg.hasQuestion, true)
assertEquals(t, c.smp.s1.msg.question, "Where did we meet?")
assertEquals(t, c.smp.s1.msg.tlv().tlvType, tlvTypeSMP1WithQuestion)
}
func Test_StartAuthenticate_generatesAnAbortMessageTLVIfWeAreInAnSMPStateAlready(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.s1 = nil
c.smp.state = smpStateExpect3{}
msg, e := c.StartAuthenticate("", []byte("hello world"))
assertEquals(t, e, nil)
dec, _ := c.decode(encodedMessage(msg[0]))
_, messageBody, _ := c.parseMessageHeader(dec)
assertDeepEquals(t, len(messageBody), 1369)
}
func Test_ProvideAuthenticationSecret_failsIfWeAreNotCurrentlyEncrypted(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = plainText
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.state = smpStateWaitingForSecret{msg: fixtureMessage1()}
_, e := c.ProvideAuthenticationSecret([]byte("hello world"))
assertEquals(t, e, errCantAuthenticateWithoutEncryption)
}
func Test_ProvideAuthenticationSecret_generatesAnSMPSecretFromTheSharedSecret(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.state = smpStateWaitingForSecret{msg: fixtureMessage1()}
_, e := c.ProvideAuthenticationSecret([]byte("hello world"))
assertEquals(t, e, nil)
assertDeepEquals(t, c.smp.secret, bnFromHex("3D7264BD983B8CA53CB365444844816F7D2453580B552EEE45CD09CA13614A5"))
}
func Test_ProvideAuthenticationSecret_failsAndAbortsIfWeAreNotWaitingForASecret(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.state = smpStateExpect3{}
_, e := c.ProvideAuthenticationSecret([]byte("hello world"))
assertEquals(t, e, errNotWaitingForSMPSecret)
}
func Test_ProvideAuthenticationSecret_continuesWithMessageProcessingIfInTheRightState(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.state = smpStateWaitingForSecret{msg: fixtureMessage1()}
msg, e := c.ProvideAuthenticationSecret([]byte("hello world"))
assertNil(t, e)
dec, _ := c.decode(encodedMessage(msg[0]))
_, messageBody, _ := c.parseMessageHeader(dec)
assertDeepEquals(t, len(messageBody), 2181)
}
func Test_ProvideAuthenticationSecret_setsTheNextMessageState(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = encrypted
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.state = smpStateWaitingForSecret{msg: fixtureMessage1()}
_, e := c.ProvideAuthenticationSecret([]byte("hello world"))
assertNil(t, e)
assertDeepEquals(t, c.smp.state, smpStateExpect3{})
}
func Test_ProvideAuthenticationSecret_returnsFailureFromContinueSMP(t *testing.T) {
c := bobContextAfterAKE()
c.msgState = plainText
c.ssid = [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
c.ourCurrentKey = bobPrivateKey
c.theirKey = alicePrivateKey.PublicKey()
c.smp.state = smpStateWaitingForSecret{msg: fixtureMessage1()}
_, e := c.ProvideAuthenticationSecret([]byte("hello world"))
assertEquals(t, e, errCantAuthenticateWithoutEncryption)
}
|