-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversation.go
307 lines (238 loc) · 8.34 KB
/
conversation.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package gotra
import (
"bytes"
"io"
"math/big"
"github.com/coyim/gotrax"
"github.com/otrv4/ed448"
)
type conversation struct {
r io.Reader
longTerm *gotrax.Keypair
otherInstanceTag uint32
currentClientProfile *gotrax.ClientProfile
im *identityMessage
imp *identityMessagePrivate
ar *authRMessage
arp *authRMessagePrivate
ai *authIMessage
state convState
ssid []byte
ratchetId uint32
ratchetJ uint32
ratchetK uint32
ratchetPN uint32
rootKey []byte
sendingChainKey []byte
receivingChainKey []byte
their_ecdh ed448.Point
their_dh *big.Int
our_ecdh *gotrax.Keypair
our_dh *dhKeypair
brace_key []byte
shouldRatchet bool
}
// TODO: for all these functions, if we're currently in OTRv3 we should fall back to an otr3 conversation
// TODO: figure out how to decide how we should deal with offline etc. maybe a callback or something?
// TODO: fragmentation etc
// TODO: base64
// TODO: I don't remember how the traces are supposed to work. Figure that out later, I guess
func (c *conversation) Send(m MessagePlaintext, trace ...interface{}) ([]ValidMessage, error) {
// TODO: sort out the flow here
// - if we're in an encrypted state, just put it in a data message and send it
// - if we're not encrypted, and encryption is optional, let's just send it
// - we will optionally add a whitespace tag here if policies say we should
// - if we're in a finished state, this should probably result in an error or something
return []ValidMessage{msgEncode(c.createDataMessage(m, []*tlv{}))}, nil
}
func msgEncode(msg []byte) []byte {
return append(append(otrPrefix, b64encode(msg)...), '.')
}
func removeOTRMsgEnvelope(msg []byte) []byte {
return msg[len(otrPrefix) : len(msg)-1]
}
func msgDecode(msg []byte) []byte {
msg = removeOTRMsgEnvelope(msg)
msg, _ = b64decode(msg)
// TODO: don't ignore error here
return msg
}
func isQueryMessage(m ValidMessage) bool {
// TODO: make this work correctly
return bytes.Equal(m, []byte("?OTRv4?"))
}
func isIdentityMessage(m ValidMessage) bool {
// TODO: make this work correctly
return bytes.HasPrefix(m, append(gotrax.AppendShort(nil, version), messageTypeIdentityMessage))
}
func isAuthRMessage(m ValidMessage) bool {
// TODO: make this work correctly
return bytes.HasPrefix(m, append(gotrax.AppendShort(nil, version), messageTypeAuthRMessage))
}
func isAuthIMessage(m ValidMessage) bool {
// TODO: make this work correctly
return bytes.HasPrefix(m, append(gotrax.AppendShort(nil, version), messageTypeAuthIMessage))
}
func isDataMessage(m ValidMessage) bool {
// TODO: make this work correctly
return bytes.HasPrefix(m, append(gotrax.AppendShort(nil, version), messageTypeDataMessage))
}
func (c *conversation) processQueryMessage(m ValidMessage) (plain MessagePlaintext, toSend []ValidMessage, err error) {
// TODO:
// if the message is 4 and we allow 4
c.state = stateWaitingAuthR{}
return nil, []ValidMessage{c.createIdentityMessage()}, nil
}
func (c *conversation) processIdentityMessage(m ValidMessage) (plain MessagePlaintext, toSend []ValidMessage, err error) {
// TODO:
// - if the state is START
// - if the state is WAITING_AUTH_R
// - if the state is WAITING_AUTH_I
// - if the state is WAITING_DAKE_DATA_MESSAGE
// - if the state is ENCRYPTED or FINISHED
// - other
im := &identityMessage{}
_, ok := im.deserialize(m)
if !ok {
// Ignore the message
return nil, nil, nil
}
verr := im.validate(c.getInstanceTag())
if verr != nil {
// Ignore the message
return nil, nil, nil
}
c.fixInstanceTag(im.senderInstanceTag)
c.im = im
c.state = stateWaitingAuthI{}
return nil, []ValidMessage{c.createAuthRMessage()}, nil
}
func (c *conversation) processAuthRMessage(m ValidMessage) (plain MessagePlaintext, toSend []ValidMessage, err error) {
// TODO:
// - If the state is WAITING_AUTH_R:
// - If the receiver's instance tag in the message is not the sender's instance tag you are currently using, ignore the message.
// - Validate the Auth-R message.
// - If validation fails:
// - Ignore the message.
// - Stay in state WAITING_AUTH_R.
// - If validation succeeds:
// - Reply with an Auth-I message, as defined in Sending an Auth-I Message section.
// - If the state is ENCRYPTED_MESSAGES:
// - If this Auth-R message is the same one you received earlier:
// - Retransmit your Auth-I Message.
// - Otherwise:
// - Ignore the message.
// - If the state is not WAITING_AUTH_R:
// - Ignore this message.
arm := &authRMessage{}
_, ok := arm.deserialize(m)
if !ok {
// Ignore the message
return nil, nil, nil
}
verr := arm.validate(c.getInstanceTag())
if verr != nil {
// Ignore the message
return nil, nil, nil
}
c.ar = arm
c.state = stateWaitingDakeDataMessage{}
aim := c.createAuthIMessage()
c.initializeRatchetR()
return nil, []ValidMessage{aim}, nil
}
func (c *conversation) processAuthIMessage(m ValidMessage) (plain MessagePlaintext, toSend []ValidMessage, err error) {
// TODO:
// If the state is WAITING_AUTH_I:
// If the receiver's instance tag in the message is not the sender's instance tag you are currently using, ignore this message.
// Validate the Auth-I message.
// If validation fails:
// Ignore the message.
// Stay in state WAITING_AUTH_I.
// If validation succeeds:
// Transition to state ENCRYPTED_MESSAGES.
// Initialize the double ratcheting, as defined in the Interactive DAKE Overview section.
// Send a regular Data Message. If a plaintext message is waiting to be sent, this can be used. Otherwise an empty heartbeat message should be sent. This data message is called "DAKE Data Message".
// If there are stored Data Messages, remove them from storage - there is no way these messages can be valid for the current DAKE.
// If the state is not WAITING_AUTH_I:
// - Ignore this message.
aim := &authIMessage{}
_, ok := aim.deserialize(m)
if !ok {
// Ignore the message
return nil, nil, nil
}
verr := aim.validate(c.getInstanceTag())
if verr != nil {
// Ignore the message
return nil, nil, nil
}
c.ai = aim
// TODO: if result is error, don't ignore it
c.initializeRatchetI()
c.state = stateEncrypted{}
// TODO: here we can send a message waiting to be sent OR a heartbeat
return nil, []ValidMessage{c.createHeartbeatDataMessage()}, nil
}
func (c *conversation) processDataMessage(m ValidMessage) (plain MessagePlaintext, toSend []ValidMessage, err error) {
// TODO: implement correctly
dm := &dataMessage{}
_, ok := dm.deserialize(m)
if !ok {
// Ignore the message
return nil, nil, nil
}
verr := dm.validate(c.getInstanceTag())
if verr != nil {
// Ignore the message
return nil, nil, nil
}
plain, toSend, err = c.receivedDataMessage(dm)
// TODO: check if error here. but for now we can assume the ratchet is initialized
if c.state.String() == "WAITING_DAKE_DATA_MESSAGE" {
c.state = stateEncrypted{}
}
return
}
func (c *conversation) Receive(m ValidMessage) (plain MessagePlaintext, toSend []ValidMessage, err error) {
// TODO: sort out the flow here
if isQueryMessage(m) {
plain, toSend, err = c.processQueryMessage(m)
}
dm := msgDecode(m)
if isIdentityMessage(dm) {
plain, toSend, err = c.processIdentityMessage(dm)
}
if isAuthRMessage(dm) {
plain, toSend, err = c.processAuthRMessage(dm)
}
if isAuthIMessage(dm) {
plain, toSend, err = c.processAuthIMessage(dm)
}
if isDataMessage(dm) {
plain, toSend, err = c.processDataMessage(dm)
}
// - plaintext without tag
// - plaintext with tag
// - error message
// - non-interactive auth message
for ix, ts := range toSend {
toSend[ix] = msgEncode(ts)
}
return
}
// QueryMessage returns a message that can be sent to request the start of an OTR session
// This function will return nil if the conversation is not in a valid state to start a conversation
func (c *conversation) QueryMessage() ValidMessage {
// TODO: we should create this dynamically, with the approved supported versions
return ValidMessage("?OTRv4?")
}
// End will end the conversation from this side, returning the messages to send to
// indicate the ending for the peer, or an error if something goes wrong
func (c *conversation) End() ([]ValidMessage, error) {
// TODO: implement correctly
// TODO: discard session keys
dm := c.createDataMessage(nil, []*tlv{createDisconnectedTLV()})
c.state = stateStart{}
return []ValidMessage{msgEncode(dm)}, nil
}