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
|
package serialize
import (
"fmt"
"strconv"
"go.mau.fi/libsignal/logger"
"go.mau.fi/libsignal/protocol"
"go.mau.fi/libsignal/util/bytehelper"
"go.mau.fi/libsignal/util/optional"
proto "google.golang.org/protobuf/proto"
)
// NewProtoBufSerializer will return a serializer for all Signal objects that will
// be responsible for converting objects to and from ProtoBuf bytes.
func NewProtoBufSerializer() *Serializer {
serializer := NewSerializer()
serializer.SignalMessage = &ProtoBufSignalMessageSerializer{}
serializer.PreKeySignalMessage = &ProtoBufPreKeySignalMessageSerializer{}
serializer.SenderKeyMessage = &ProtoBufSenderKeyMessageSerializer{}
serializer.SenderKeyDistributionMessage = &ProtoBufSenderKeyDistributionMessageSerializer{}
serializer.SignedPreKeyRecord = &JSONSignedPreKeyRecordSerializer{}
serializer.PreKeyRecord = &JSONPreKeyRecordSerializer{}
serializer.State = &JSONStateSerializer{}
serializer.Session = &JSONSessionSerializer{}
serializer.SenderKeyRecord = &JSONSenderKeySessionSerializer{}
serializer.SenderKeyState = &JSONSenderKeyStateSerializer{}
return serializer
}
func highBitsToInt(value byte) int {
return int((value & 0xFF) >> 4)
}
func intsToByteHighAndLow(highValue, lowValue int) byte {
return byte((highValue<<4 | lowValue) & 0xFF)
}
// ProtoBufSignalMessageSerializer is a structure for serializing signal messages into
// and from ProtoBuf.
type ProtoBufSignalMessageSerializer struct{}
// Serialize will take a signal message structure and convert it to ProtoBuf bytes.
func (j *ProtoBufSignalMessageSerializer) Serialize(signalMessage *protocol.SignalMessageStructure) []byte {
sm := &SignalMessage{
RatchetKey: signalMessage.RatchetKey,
Counter: &signalMessage.Counter,
PreviousCounter: &signalMessage.PreviousCounter,
Ciphertext: signalMessage.CipherText,
}
var serialized []byte
message, err := proto.Marshal(sm)
if err != nil {
logger.Error("Error serializing signal message: ", err)
}
if signalMessage.Version != 0 {
serialized = append(serialized, []byte(strconv.Itoa(signalMessage.Version))...)
}
serialized = append(serialized, message...)
if signalMessage.Mac != nil {
serialized = append(serialized, signalMessage.Mac...)
}
return serialized
}
// Deserialize will take in ProtoBuf bytes and return a signal message structure.
func (j *ProtoBufSignalMessageSerializer) Deserialize(serialized []byte) (*protocol.SignalMessageStructure, error) {
parts, err := bytehelper.SplitThree(serialized, 1, len(serialized)-1-protocol.MacLength, protocol.MacLength)
if err != nil {
logger.Error("Error split signal message: ", err)
return nil, err
}
version := highBitsToInt(parts[0][0])
message := parts[1]
mac := parts[2]
var sm SignalMessage
err = proto.Unmarshal(message, &sm)
if err != nil {
logger.Error("Error deserializing signal message: ", err)
return nil, err
}
signalMessage := protocol.SignalMessageStructure{
Version: version,
RatchetKey: sm.GetRatchetKey(),
Counter: sm.GetCounter(),
PreviousCounter: sm.GetPreviousCounter(),
CipherText: sm.GetCiphertext(),
Mac: mac,
}
return &signalMessage, nil
}
// ProtoBufPreKeySignalMessageSerializer is a structure for serializing prekey signal messages
// into and from ProtoBuf.
type ProtoBufPreKeySignalMessageSerializer struct{}
// Serialize will take a prekey signal message structure and convert it to ProtoBuf bytes.
func (j *ProtoBufPreKeySignalMessageSerializer) Serialize(signalMessage *protocol.PreKeySignalMessageStructure) []byte {
preKeyMessage := &PreKeySignalMessage{
RegistrationId: &signalMessage.RegistrationID,
SignedPreKeyId: &signalMessage.SignedPreKeyID,
BaseKey: signalMessage.BaseKey,
IdentityKey: signalMessage.IdentityKey,
Message: signalMessage.Message,
}
if !signalMessage.PreKeyID.IsEmpty {
preKeyMessage.PreKeyId = &signalMessage.PreKeyID.Value
}
message, err := proto.Marshal(preKeyMessage)
if err != nil {
logger.Error("Error serializing prekey signal message: ", err)
}
serialized := append([]byte(strconv.Itoa(signalMessage.Version)), message...)
logger.Debug("Serialize PreKeySignalMessage result: ", serialized)
return serialized
}
// Deserialize will take in ProtoBuf bytes and return a prekey signal message structure.
func (j *ProtoBufPreKeySignalMessageSerializer) Deserialize(serialized []byte) (*protocol.PreKeySignalMessageStructure, error) {
version := highBitsToInt(serialized[0])
message := serialized[1:]
var sm PreKeySignalMessage
err := proto.Unmarshal(message, &sm)
if err != nil {
logger.Error("Error deserializing prekey signal message: ", err)
return nil, err
}
preKeyId := optional.NewEmptyUint32()
if sm.GetPreKeyId() != 0 {
preKeyId = optional.NewOptionalUint32(sm.GetPreKeyId())
}
preKeySignalMessage := protocol.PreKeySignalMessageStructure{
Version: version,
RegistrationID: sm.GetRegistrationId(),
BaseKey: sm.GetBaseKey(),
IdentityKey: sm.GetIdentityKey(),
SignedPreKeyID: sm.GetSignedPreKeyId(),
Message: sm.GetMessage(),
PreKeyID: preKeyId,
}
return &preKeySignalMessage, nil
}
// ProtoBufSenderKeyDistributionMessageSerializer is a structure for serializing senderkey
// distribution records to and from ProtoBuf.
type ProtoBufSenderKeyDistributionMessageSerializer struct{}
// Serialize will take a senderkey distribution message and convert it to ProtoBuf bytes.
func (j *ProtoBufSenderKeyDistributionMessageSerializer) Serialize(message *protocol.SenderKeyDistributionMessageStructure) []byte {
senderDis := SenderKeyDistributionMessage{
Id: &message.ID,
Iteration: &message.Iteration,
ChainKey: message.ChainKey,
SigningKey: message.SigningKey,
}
serialized, err := proto.Marshal(&senderDis)
if err != nil {
logger.Error("Error serializing senderkey distribution message: ", err)
}
version := strconv.Itoa(int(message.Version))
serialized = append([]byte(version), serialized...)
logger.Debug("Serialize result: ", serialized)
return serialized
}
// Deserialize will take in ProtoBuf bytes and return a message structure, which can be
// used to create a new SenderKey Distribution object.
func (j *ProtoBufSenderKeyDistributionMessageSerializer) Deserialize(serialized []byte) (*protocol.SenderKeyDistributionMessageStructure, error) {
version := uint32(highBitsToInt(serialized[0]))
message := serialized[1:]
var senderKeyDis SenderKeyDistributionMessage
err := proto.Unmarshal(message, &senderKeyDis)
if err != nil {
logger.Error("Error deserializing senderkey distribution message: ", err)
return nil, err
}
msgStructure := protocol.SenderKeyDistributionMessageStructure{
ID: senderKeyDis.GetId(),
Iteration: senderKeyDis.GetIteration(),
ChainKey: senderKeyDis.GetChainKey(),
SigningKey: senderKeyDis.GetSigningKey(),
Version: version,
}
return &msgStructure, nil
}
// ProtoBufSenderKeyMessageSerializer is a structure for serializing senderkey
// messages to and from ProtoBuf.
type ProtoBufSenderKeyMessageSerializer struct{}
// Serialize will take a senderkey message and convert it to ProtoBuf bytes.
func (j *ProtoBufSenderKeyMessageSerializer) Serialize(message *protocol.SenderKeyMessageStructure) []byte {
senderMessage := &SenderKeyMessage{
Id: &message.ID,
Iteration: &message.Iteration,
Ciphertext: message.CipherText,
}
var serialized []byte
m, err := proto.Marshal(senderMessage)
if err != nil {
logger.Error("Error serializing signal message: ", err)
}
if message.Version != 0 {
serialized = append([]byte(fmt.Sprint(message.Version)), m...)
}
if message.Signature != nil {
serialized = append(serialized, message.Signature...)
}
logger.Debug("Serialize result: ", serialized)
return serialized
}
// Deserialize will take in ProtoBuf bytes and return a message structure, which can be
// used to create a new SenderKey message object.
func (j *ProtoBufSenderKeyMessageSerializer) Deserialize(serialized []byte) (*protocol.SenderKeyMessageStructure, error) {
parts, err := bytehelper.SplitThree(serialized, 1, len(serialized)-1-64, 64)
if err != nil {
logger.Error("Error split signal message: ", err)
return nil, err
}
version := uint32(highBitsToInt(parts[0][0]))
message := parts[1]
signature := parts[2]
var senderKey SenderKeyMessage
err = proto.Unmarshal(message, &senderKey)
if err != nil {
logger.Error("Error deserializing senderkey message: ", err)
return nil, err
}
msgStructure := protocol.SenderKeyMessageStructure{
Version: version,
ID: senderKey.GetId(),
Iteration: senderKey.GetIteration(),
CipherText: senderKey.GetCiphertext(),
Signature: signature,
}
return &msgStructure, nil
}
|