-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2ee_test.go
195 lines (154 loc) · 7.41 KB
/
e2ee_test.go
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
package e2ee
import (
"testing"
"github.com/stretchr/testify/assert"
)
var version = "test"
func TestE2EEVersion(t *testing.T) {
alice := newE2EE(version)
assert.NotNil(t, alice.getVersion())
}
func TestE2EE(t *testing.T) {
version := "dev"
aliceConnectionID := "ALICE---------------------"
alice := newE2EE(version)
alice.init()
assert.Equal(t, len(alice.secretKeyMaterial), 32)
alice.start(aliceConnectionID)
// 最初なので 0
assert.Equal(t, uint32(0), alice.keyID)
assert.NotNil(t, alice.selfFingerprint())
assert.Empty(t, alice.remoteFingerprints())
bobConnectionID := "BOB-----------------------"
bob := newE2EE(version)
bob.init()
assert.Equal(t, len(bob.secretKeyMaterial), 32)
bob.start(bobConnectionID)
// 最初なので 0
assert.Equal(t, uint32(0), bob.keyID)
assert.NotNil(t, bob.selfFingerprint())
assert.Empty(t, bob.remoteFingerprints())
result, err := alice.startSession(bobConnectionID, bob.selfPreKeyBundle.identityKey, bob.selfPreKeyBundle.signedPreKey[:], bob.selfPreKeyBundle.preKeySignature)
assert.Nil(t, err)
assert.NotEmpty(t, alice.remoteFingerprints())
assert.Equal(t, 1, len(alice.remoteFingerprints()))
// 新しく参加者が来たので sender の alice は keyId++ する
assert.Equal(t, 2, len(result.messages))
assert.Equal(t, uint32(1), result.selfKeyID)
assert.Equal(t, aliceConnectionID, result.selfConnectionID)
assert.Equal(t, uint32(1), alice.keyID)
// まだ SK と keyID を相手からもらっていない
_, ok := result.remoteSecretKeyMaterials[bobConnectionID]
assert.False(t, ok)
err = bob.addPreKeyBundle(aliceConnectionID, alice.selfPreKeyBundle.identityKey, alice.selfPreKeyBundle.signedPreKey[:], alice.selfPreKeyBundle.preKeySignature)
assert.Nil(t, err)
assert.NotEmpty(t, bob.remoteFingerprints())
assert.Equal(t, 1, len(bob.remoteFingerprints()))
r0, err := bob.receiveMessage(result.messages[0])
assert.Nil(t, err)
assert.Equal(t, 0, len(r0.remoteSecretKeyMaterials))
assert.Equal(t, 0, len(r0.messages))
// bob は reciever なので keyId++ しない
assert.Equal(t, uint32(0), bob.keyID)
// bob はまだ alice から sk をもらっていない
_, ok = r0.remoteSecretKeyMaterials[aliceConnectionID]
assert.False(t, ok)
assert.Equal(t, alice.sessions[bobConnectionID].rootKey, bob.sessions[aliceConnectionID].rootKey)
r1, err := bob.receiveMessage(result.messages[1])
assert.Nil(t, err)
assert.Equal(t, 1, len(r1.remoteSecretKeyMaterials))
assert.Equal(t, 1, len(r1.messages))
assert.Equal(t, uint32(0), bob.keyID)
// alice の keyID は bob が参加したことにより sk が ratchet してるので 1
assert.Equal(t, uint32(1), r1.remoteSecretKeyMaterials[aliceConnectionID].keyID)
r2, err := alice.receiveMessage(r1.messages[0])
assert.Nil(t, err)
assert.Equal(t, 1, len(r2.remoteSecretKeyMaterials))
assert.Equal(t, 0, len(r2.messages))
assert.Equal(t, uint32(1), alice.keyID)
// bob は新規参加者なので sk を ratchet してないので keyID は 0 のまま
assert.Equal(t, uint32(0), r2.remoteSecretKeyMaterials[bobConnectionID].keyID)
assert.Equal(t, bob.secretKeyMaterial, r2.remoteSecretKeyMaterials[bobConnectionID].secretKeyMaterial)
assert.Equal(t, bob.secretKeyMaterial, alice.sessions[bobConnectionID].remoteSecretKeyMaterial)
assert.Equal(t, uint32(1), alice.keyID)
assert.Equal(t, uint32(0), alice.sessions[bobConnectionID].remoteKeyID)
// Carol を登場させる
carolConnectionID := "CAROL---------------------"
carol := newE2EE(version)
carol.init()
carol.start(carolConnectionID)
err = carol.addPreKeyBundle(aliceConnectionID, alice.selfPreKeyBundle.identityKey, alice.selfPreKeyBundle.signedPreKey[:], alice.selfPreKeyBundle.preKeySignature)
assert.Nil(t, err)
err = carol.addPreKeyBundle(bobConnectionID, bob.selfPreKeyBundle.identityKey, bob.selfPreKeyBundle.signedPreKey[:], bob.selfPreKeyBundle.preKeySignature)
assert.Nil(t, err)
assert.Equal(t, 2, len(carol.remoteFingerprints()))
r3, err := alice.startSession(carolConnectionID, carol.selfPreKeyBundle.identityKey, carol.selfPreKeyBundle.signedPreKey[:], carol.selfPreKeyBundle.preKeySignature)
assert.Nil(t, err)
assert.Equal(t, 2, len(alice.remoteFingerprints()))
assert.Equal(t, 2, len(r3.messages))
assert.Equal(t, aliceConnectionID, r3.selfConnectionID)
// carol がきたので key を ratchet したので 2 へ
assert.Equal(t, uint32(2), alice.keyID)
r4, err := carol.receiveMessage(r3.messages[0])
assert.Nil(t, err)
assert.Equal(t, 0, len(r4.messages))
// carol は新規参加者なので key は 0 のまま
assert.Equal(t, uint32(0), carol.keyID)
r5, err := carol.receiveMessage(r3.messages[1])
assert.Nil(t, err)
assert.Equal(t, 1, len(r5.messages))
// carol は新規参加者なので key は 0 のまま
assert.Equal(t, uint32(0), carol.keyID)
r6, err := bob.startSession(carolConnectionID, carol.selfPreKeyBundle.identityKey, carol.selfPreKeyBundle.signedPreKey[:], carol.selfPreKeyBundle.preKeySignature)
assert.Nil(t, err)
assert.Equal(t, 2, len(bob.remoteFingerprints()))
assert.Equal(t, bobConnectionID, r6.selfConnectionID)
// 新規参加者がきたので鍵を ratchet した
assert.Equal(t, uint32(1), bob.keyID)
r7, err := carol.receiveMessage(r6.messages[0])
assert.Nil(t, err)
assert.Equal(t, 0, len(r7.messages))
assert.Equal(t, uint32(0), carol.keyID)
r8, err := carol.receiveMessage(r6.messages[1])
assert.Nil(t, err)
assert.Equal(t, 1, len(r8.messages))
assert.Equal(t, uint32(0), carol.keyID)
r9, err := alice.receiveMessage(r5.messages[0])
assert.Nil(t, err)
assert.Equal(t, 0, len(r9.messages))
assert.Equal(t, uint32(2), alice.keyID)
r10, err := bob.receiveMessage(r8.messages[0])
assert.Nil(t, err)
assert.Equal(t, 0, len(r10.messages))
assert.Equal(t, uint32(1), bob.keyID)
assert.Equal(t, alice.secretKeyMaterial, bob.sessions[aliceConnectionID].remoteSecretKeyMaterial)
assert.Equal(t, alice.secretKeyMaterial, carol.sessions[aliceConnectionID].remoteSecretKeyMaterial)
assert.Equal(t, bob.secretKeyMaterial, alice.sessions[bobConnectionID].remoteSecretKeyMaterial)
assert.Equal(t, bob.secretKeyMaterial, carol.sessions[bobConnectionID].remoteSecretKeyMaterial)
assert.Equal(t, carol.secretKeyMaterial, alice.sessions[carolConnectionID].remoteSecretKeyMaterial)
assert.Equal(t, carol.secretKeyMaterial, bob.sessions[carolConnectionID].remoteSecretKeyMaterial)
r11, err := alice.stopSession(carolConnectionID)
assert.Nil(t, err)
assert.Equal(t, 1, len(alice.remoteFingerprints()))
assert.Equal(t, 1, len(r11.messages))
assert.Equal(t, aliceConnectionID, r11.selfConnectionID)
assert.Equal(t, uint32(3), alice.keyID)
assert.Equal(t, uint32(3), r11.selfKeyID)
r12, err := bob.stopSession(carolConnectionID)
assert.Nil(t, err)
assert.Equal(t, 1, len(bob.remoteFingerprints()))
assert.Equal(t, 1, len(r12.messages))
assert.Equal(t, bobConnectionID, r12.selfConnectionID)
assert.Equal(t, uint32(2), bob.keyID)
assert.Equal(t, uint32(2), r12.selfKeyID)
r13, err := alice.receiveMessage(r12.messages[0])
assert.Nil(t, err)
assert.Equal(t, 0, len(r13.messages))
r14, err := bob.receiveMessage(r11.messages[0])
assert.Nil(t, err)
assert.Equal(t, 0, len(r14.messages))
assert.Equal(t, uint32(2), alice.sessions[bobConnectionID].remoteKeyID)
assert.Equal(t, uint32(3), bob.sessions[aliceConnectionID].remoteKeyID)
assert.Equal(t, alice.secretKeyMaterial, bob.sessions[aliceConnectionID].remoteSecretKeyMaterial)
assert.Equal(t, bob.secretKeyMaterial, alice.sessions[bobConnectionID].remoteSecretKeyMaterial)
}