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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
#include "session_record.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "axolotl_internal.h"
#include "session_state.h"
#include "utlist.h"
#include "LocalStorageProtocol.pb-c.h"
#define ARCHIVED_STATES_MAX_LENGTH 40
struct session_record_state_node
{
session_state *state;
struct session_record_state_node *prev, *next;
};
struct session_record
{
axolotl_type_base base;
session_state *state;
session_record_state_node *previous_states_head;
int is_fresh;
axolotl_context *global_context;
};
static void session_record_free_previous_states(session_record *record);
int session_record_create(session_record **record, session_state *state, axolotl_context *global_context)
{
session_record *result = malloc(sizeof(session_record));
if(!result) {
return AX_ERR_NOMEM;
}
memset(result, 0, sizeof(session_record));
AXOLOTL_INIT(result, session_record_destroy);
if(!state) {
int ret = session_state_create(&result->state, global_context);
if(ret < 0) {
AXOLOTL_UNREF(result);
return ret;
}
result->is_fresh = 1;
}
else {
AXOLOTL_REF(state);
result->state = state;
result->is_fresh = 0;
}
result->global_context = global_context;
*record = result;
return 0;
}
int session_record_serialize(axolotl_buffer **buffer, const session_record *record)
{
int result = 0;
size_t result_size = 0;
unsigned int i = 0;
Textsecure__RecordStructure record_structure = TEXTSECURE__RECORD_STRUCTURE__INIT;
session_record_state_node *cur_node = 0;
axolotl_buffer *result_buf = 0;
size_t len = 0;
uint8_t *data = 0;
if(record->state) {
record_structure.currentsession = malloc(sizeof(Textsecure__SessionStructure));
if(!record_structure.currentsession) {
result = AX_ERR_NOMEM;
goto complete;
}
textsecure__session_structure__init(record_structure.currentsession);
result = session_state_serialize_prepare(record->state, record_structure.currentsession);
if(result < 0) {
goto complete;
}
}
if(record->previous_states_head) {
unsigned int count;
DL_COUNT(record->previous_states_head, cur_node, count);
if(count > SIZE_MAX / sizeof(Textsecure__SessionStructure *)) {
result = AX_ERR_NOMEM;
goto complete;
}
record_structure.previoussessions = malloc(sizeof(Textsecure__SessionStructure *) * count);
if(!record_structure.previoussessions) {
result = AX_ERR_NOMEM;
goto complete;
}
i = 0;
DL_FOREACH(record->previous_states_head, cur_node) {
record_structure.previoussessions[i] = malloc(sizeof(Textsecure__SessionStructure));
if(!record_structure.previoussessions[i]) {
result = AX_ERR_NOMEM;
break;
}
textsecure__session_structure__init(record_structure.previoussessions[i]);
result = session_state_serialize_prepare(cur_node->state, record_structure.previoussessions[i]);
if(result < 0) {
break;
}
i++;
}
record_structure.n_previoussessions = i;
if(result < 0) {
goto complete;
}
}
len = textsecure__record_structure__get_packed_size(&record_structure);
result_buf = axolotl_buffer_alloc(len);
if(!result_buf) {
result = AX_ERR_NOMEM;
goto complete;
}
data = axolotl_buffer_data(result_buf);
result_size = textsecure__record_structure__pack(&record_structure, data);
if(result_size != len) {
axolotl_buffer_free(result_buf);
result = AX_ERR_INVALID_PROTO_BUF;
result_buf = 0;
goto complete;
}
complete:
if(record_structure.currentsession) {
session_state_serialize_prepare_free(record_structure.currentsession);
}
if(record_structure.previoussessions) {
for(i = 0; i < record_structure.n_previoussessions; i++) {
if(record_structure.previoussessions[i]) {
session_state_serialize_prepare_free(record_structure.previoussessions[i]);
}
}
free(record_structure.previoussessions);
}
if(result >= 0) {
*buffer = result_buf;
}
return result;
}
int session_record_deserialize(session_record **record, const uint8_t *data, size_t len, axolotl_context *global_context)
{
int result = 0;
session_record *result_record = 0;
session_state *current_state = 0;
session_record_state_node *previous_states_head = 0;
Textsecure__RecordStructure *record_structure = 0;
record_structure = textsecure__record_structure__unpack(0, len, data);
if(!record_structure) {
result = AX_ERR_INVALID_PROTO_BUF;
goto complete;
}
if(record_structure->currentsession) {
result = session_state_deserialize_protobuf(¤t_state, record_structure->currentsession, global_context);
if(result < 0) {
goto complete;
}
}
result = session_record_create(&result_record, current_state, global_context);
if(result < 0) {
goto complete;
}
AXOLOTL_UNREF(current_state);
current_state = 0;
result_record->is_fresh = 0;
if(record_structure->n_previoussessions > 0) {
unsigned int i;
for(i = 0; i < record_structure->n_previoussessions; i++) {
Textsecure__SessionStructure *session_structure =
record_structure->previoussessions[i];
session_record_state_node *node = malloc(sizeof(session_record_state_node));
if(!node) {
result = AX_ERR_NOMEM;
goto complete;
}
result = session_state_deserialize_protobuf(&node->state, session_structure, global_context);
if(result < 0) {
free(node);
goto complete;
}
DL_APPEND(previous_states_head, node);
}
}
result_record->previous_states_head = previous_states_head;
previous_states_head = 0;
complete:
if(record_structure) {
textsecure__record_structure__free_unpacked(record_structure, 0);
}
if(current_state) {
AXOLOTL_UNREF(current_state);
}
if(previous_states_head) {
session_record_state_node *cur_node;
session_record_state_node *tmp_node;
DL_FOREACH_SAFE(previous_states_head, cur_node, tmp_node) {
DL_DELETE(previous_states_head, cur_node);
free(cur_node);
}
}
if(result_record) {
if(result < 0) {
AXOLOTL_UNREF(result_record);
}
else {
*record = result_record;
}
}
return result;
}
int session_record_copy(session_record **record, session_record *other_record, axolotl_context *global_context)
{
int result = 0;
axolotl_buffer *buffer = 0;
size_t len = 0;
uint8_t *data = 0;
assert(other_record);
assert(global_context);
result = session_record_serialize(&buffer, other_record);
if(result < 0) {
goto complete;
}
data = axolotl_buffer_data(buffer);
len = axolotl_buffer_len(buffer);
result = session_record_deserialize(record, data, len, global_context);
if(result < 0) {
goto complete;
}
complete:
if(buffer) {
axolotl_buffer_free(buffer);
}
return result;
}
int session_record_has_session_state(session_record *record, uint32_t version, const ec_public_key *alice_base_key)
{
session_record_state_node *cur_node = 0;
assert(record);
assert(record->state);
if(session_state_get_session_version(record->state) == version &&
ec_public_key_compare(
session_state_get_alice_base_key(record->state),
alice_base_key) == 0) {
return 1;
}
DL_FOREACH(record->previous_states_head, cur_node) {
if(session_state_get_session_version(cur_node->state) == version &&
ec_public_key_compare(
session_state_get_alice_base_key(cur_node->state),
alice_base_key) == 0) {
return 1;
}
}
return 0;
}
session_state *session_record_get_state(session_record *record)
{
return record->state;
}
void session_record_set_state(session_record *record, session_state *state)
{
assert(record);
assert(state);
if(record->state) {
AXOLOTL_UNREF(record->state);
}
AXOLOTL_REF(state);
record->state = state;
}
session_record_state_node *session_record_get_previous_states_head(const session_record *record)
{
assert(record);
return record->previous_states_head;
}
session_state *session_record_get_previous_states_element(const session_record_state_node *node)
{
assert(node);
return node->state;
}
session_record_state_node *session_record_get_previous_states_next(const session_record_state_node *node)
{
assert(node);
return node->next;
}
session_record_state_node *session_record_get_previous_states_remove(session_record *record, session_record_state_node *node)
{
session_record_state_node *next_node = 0;
assert(record);
assert(node);
next_node = node->next;
DL_DELETE(record->previous_states_head, node);
AXOLOTL_UNREF(node->state);
free(node);
return next_node;
}
int session_record_is_fresh(session_record *record)
{
assert(record);
return record->is_fresh;
}
int session_record_archive_current_state(session_record *record)
{
int result = 0;
session_state *new_state = 0;
assert(record);
result = session_state_create(&new_state, record->global_context);
if(result < 0) {
goto complete;
}
result = session_record_promote_state(record, new_state);
complete:
AXOLOTL_UNREF(new_state);
return result;
}
int session_record_promote_state(session_record *record, session_state *promoted_state)
{
int count = 0;
session_record_state_node *cur_node = 0;
session_record_state_node *tmp_node = 0;
assert(record);
assert(promoted_state);
// Move the previously current state to the list of previous states
if(record->state) {
session_record_state_node *node = malloc(sizeof(session_record_state_node));
if(!node) {
return AX_ERR_NOMEM;
}
node->state = record->state;
DL_PREPEND(record->previous_states_head, node);
record->state = 0;
}
// Make the promoted state the current state
AXOLOTL_REF(promoted_state);
record->state = promoted_state;
// Remove any previous nodes beyond the maximum length
DL_FOREACH_SAFE(record->previous_states_head, cur_node, tmp_node) {
count++;
if(count > ARCHIVED_STATES_MAX_LENGTH) {
DL_DELETE(record->previous_states_head, cur_node);
if(cur_node->state) {
AXOLOTL_UNREF(cur_node->state);
}
free(cur_node);
}
}
return 0;
}
static void session_record_free_previous_states(session_record *record)
{
session_record_state_node *cur_node;
session_record_state_node *tmp_node;
DL_FOREACH_SAFE(record->previous_states_head, cur_node, tmp_node) {
DL_DELETE(record->previous_states_head, cur_node);
if(cur_node->state) {
AXOLOTL_UNREF(cur_node->state);
}
free(cur_node);
}
record->previous_states_head = 0;
}
void session_record_destroy(axolotl_type_base *type)
{
session_record *record = (session_record *)type;
if(record->state) {
AXOLOTL_UNREF(record->state);
}
session_record_free_previous_states(record);
free(record);
}
|