-
Notifications
You must be signed in to change notification settings - Fork 18
/
packetdispatcher.go
162 lines (131 loc) · 4.47 KB
/
packetdispatcher.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
//
// Package o3 functions to prepare and send packets. All preparation required to transmit a
// packet takes place in the packet's respective dispatcher function. Functions
// from packetserializer are used to convert from struct to byte buffer form that
// can then be transmitted on the wire. Errors from packetserializer bubble up here
// in the form of panics that have to be passed on to communicationhandler for
// conversion to go errors.
//
package o3
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"golang.org/x/crypto/nacl/box"
)
func dispatcherPanicHandler(context string, i interface{}) error {
if _, ok := i.(string); ok {
return fmt.Errorf("%s: error occurred dispatching %s", context, i)
}
return fmt.Errorf("%s: unknown dispatch error occurred: %#v", context, i)
}
func writeHelper(wr io.Writer, buf *bytes.Buffer) {
i, err := wr.Write(buf.Bytes())
if i != buf.Len() {
panic("not enough bytes were transmitted")
}
if err != nil {
panic(err)
}
}
func (sc *SessionContext) dispatchClientHello(wr io.Writer) {
defer func() {
if r := recover(); r != nil {
panic(dispatcherPanicHandler("client hello", r))
}
}()
var ch clientHelloPacket
ch.ClientSPK = sc.clientSPK
ch.NoncePrefix = sc.clientNonce.prefix()
buf := serializeClientHelloPkt(ch)
writeHelper(wr, buf)
}
//not necessary on the client side
func (sc *SessionContext) dispatchServerHello(wr io.Writer) {}
func (sc *SessionContext) dispatchAuthMsg(wr io.Writer) {
defer func() {
if r := recover(); r != nil {
panic(dispatcherPanicHandler("authentication packet", r))
}
}()
var app authPacketPayload
var ap authPacket
app.Username = sc.ID.ID
//TODO System Data: app.SysData = ..
app.ServerNoncePrefix = sc.serverNonce.prefix()
app.RandomNonce = newNonce()
//create payload ciphertext
ct := box.Seal(nil, sc.clientSPK[:], app.RandomNonce.bytes(), &sc.serverLPK, &sc.ID.LSK)
if len(ct) != 48 {
panic("error encrypting client short-term public key")
}
copy(app.Ciphertext[:], ct[0:48])
appBuf := serializeAuthPktPayload(app)
//create auth packet ciphertext
sc.clientNonce.setCounter(1)
apct := box.Seal(nil, appBuf.Bytes(), sc.clientNonce.bytes(), &sc.serverSPK, &sc.clientSSK)
if len(apct) != 144 {
panic("error encrypting payload")
}
copy(ap.Ciphertext[:], apct[0:144])
buf := serializeAuthPkt(ap)
writeHelper(wr, buf)
}
func (sc *SessionContext) dispatchAckMsg(wr io.Writer, mp messagePacket) {
ackP := ackPacket{
PktType: clientAck,
SenderID: mp.Sender,
MsgID: mp.ID}
serializedAckPkt := serializeAckPkt(ackP)
sc.clientNonce.increaseCounter()
ackpCipherText := box.Seal(nil, serializedAckPkt.Bytes(), sc.clientNonce.bytes(), &sc.serverSPK, &sc.clientSSK)
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, uint16(len(ackpCipherText)))
binary.Write(buf, binary.LittleEndian, ackpCipherText)
writeHelper(wr, buf)
}
func (sc *SessionContext) dispatchEchoMsg(wr io.Writer, oldEchoPacket echoPacket) {
ep := echoPacket{
Counter: oldEchoPacket.Counter + 1}
serializedEchoPkt := serializeEchoPkt(ep)
sc.clientNonce.increaseCounter()
epCipherText := box.Seal(nil, serializedEchoPkt.Bytes(), sc.clientNonce.bytes(), &sc.serverSPK, &sc.clientSSK)
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, uint16(len(epCipherText)))
binary.Write(buf, binary.LittleEndian, epCipherText)
writeHelper(wr, buf)
}
func (sc *SessionContext) dispatchMessage(wr io.Writer, m Message) {
mh := m.header()
randNonce := newRandomNonce()
recipient, ok := sc.ID.Contacts.Get(mh.recipient.String())
if !ok {
var tr ThreemaRest
var err error
recipient, err = tr.GetContactByID(mh.recipient)
if err != nil {
panic("Recipient's PublicKey could not be found!")
}
sc.ID.Contacts.Add(recipient)
}
msgCipherText := box.Seal(nil, m.Serialize(), randNonce.bytes(), &recipient.LPK, &sc.ID.LSK)
messagePkt := messagePacket{
PktType: sendingMsg,
Sender: mh.sender,
Recipient: mh.recipient,
ID: mh.id,
Time: mh.time,
Flags: msgFlags{PushMessage: true},
PubNick: mh.pubNick,
Nonce: randNonce,
Ciphertext: msgCipherText,
}
serializedMsgPkt := serializeMsgPkt(messagePkt)
sc.clientNonce.increaseCounter()
serializedMsgPktCipherText := box.Seal(nil, serializedMsgPkt.Bytes(), sc.clientNonce.bytes(), &sc.serverSPK, &sc.clientSSK)
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, uint16(len(serializedMsgPktCipherText)))
binary.Write(buf, binary.LittleEndian, serializedMsgPktCipherText)
writeHelper(wr, buf)
}