summaryrefslogtreecommitdiffstats
path: root/comm/third_party/libotr/src/context.c
blob: 44d8b860f7034ba59d0d744570a26782c613866d (plain)
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 *  Off-the-Record Messaging library
 *  Copyright (C) 2004-2014  Ian Goldberg, David Goulet, Rob Smits,
 *                           Chris Alexander, Willy Lew, Lisa Du,
 *                           Nikita Borisov
 *                           <otr@cypherpunks.ca>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of version 2.1 of the GNU Lesser General
 *  Public License as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* system headers */
#include <stdlib.h>
#include <assert.h>

/* libgcrypt headers */
#include <gcrypt.h>

/* libotr headers */
#include "context.h"
#include "instag.h"

#if OTRL_DEBUGGING
#include <stdio.h>

void otrl_auth_dump(FILE *f, const OtrlAuthInfo *auth);
void otrl_sm_dump(FILE *f, const OtrlSMState *sm);

/* Dump the contents of a context to the FILE *f. */
void otrl_context_dump(FILE *f, const ConnContext *context)
{
    const Fingerprint *fing;

    fprintf(f, "Context %p:\n\n", context);

    fprintf(f, "  Username: %s\n", context->username);
    fprintf(f, "  Accountname: %s\n", context->accountname);
    fprintf(f, "  Protocol: %s\n\n", context->protocol);
    fprintf(f, "  Master context: %p%s\n", context->m_context,
	    context->m_context == context ? " IS MASTER" : "");
    fprintf(f, "  Recent recv child: %p\n", context->recent_rcvd_child);
    fprintf(f, "  Recent sent child: %p\n", context->recent_sent_child);
    fprintf(f, "  Recent child: %p\n\n", context->recent_child);
    fprintf(f, "  Our instance:   %08x\n", context->our_instance);
    fprintf(f, "  Their instance: %08x\n\n", context->their_instance);
    fprintf(f, "  Msgstate: %d (%s)\n\n", context->msgstate,
	context->msgstate == OTRL_MSGSTATE_PLAINTEXT ? "PLAINTEXT" :
	context->msgstate == OTRL_MSGSTATE_ENCRYPTED ? "ENCRYPTED" :
	context->msgstate == OTRL_MSGSTATE_FINISHED ? "FINISHED" :
	"INVALID");
    otrl_auth_dump(f, &context->auth);
    fprintf(f, "\n  Fingerprints:\n");
    for (fing = context->fingerprint_root.next; fing; fing = fing->next) {
	fprintf(f, "    %p ", fing);
	if (fing->fingerprint == NULL) {
	    fprintf(f, "(null)");
	} else {
	    int i;
	    for (i=0;i<20;++i) {
		fprintf(f, "%02x", fing->fingerprint[i]);
	    }
	}
	fprintf(f, " %p", fing->context);
	if (fing->trust && fing->trust[0]) {
	    fprintf(f, " %s", fing->trust);
	}
	fprintf(f, "\n");
    }
    fprintf(f, "\n  Active fingerprint: %p\n\n", context->active_fingerprint);
    fprintf(f, "  Protocol version: %d\n", context->protocol_version);
    fprintf(f, "  OTR offer: %d (%s)\n\n", context->otr_offer,
	context->otr_offer == OFFER_NOT ? "NOT" :
	context->otr_offer == OFFER_SENT ? "SENT" :
	context->otr_offer == OFFER_REJECTED ? "REJECTED" :
	context->otr_offer == OFFER_ACCEPTED ? "ACCEPTED" :
	"INVALID");

    fprintf(f, "  Application data: %p\n", context->app_data);
    if (context->smstate == NULL) {
	fprintf(f, "  SM state: NULL\n");
    } else {
	otrl_sm_dump(f, context->smstate);
    }
    fprintf(f, "\n");
}

/* Dump the master context of this context, and all of its children. */
void otrl_context_siblings_dump(FILE *f, const ConnContext *context)
{
    const ConnContext *citer;
    for (citer = context->m_context;
	    citer && citer->m_context == context->m_context;
	    citer = citer->next) {
	if (citer == context) {
	    fprintf(f, "*** ");
	}
	otrl_context_dump(f, citer);
    }
}

/* Dump all contexts. */
void otrl_context_all_dump(FILE *f, OtrlUserState us)
{
    const ConnContext *citer;
    unsigned int ctxnum = 1;
    for (citer = us->context_root; citer; citer = citer->next, ++ctxnum) {
	fprintf(f, "%u. ", ctxnum);
	otrl_context_dump(f, citer);
    }
}
#endif

/* Create a new connection context. */
static ConnContext * new_context(const char * user, const char * accountname,
	const char * protocol)
{
    ConnContext * context;
    OtrlSMState *smstate;

    context = malloc(sizeof(ConnContext));
    assert(context != NULL);

    context->username = strdup(user);
    context->accountname = strdup(accountname);
    context->protocol = strdup(protocol);

    context->msgstate = OTRL_MSGSTATE_PLAINTEXT;
    otrl_auth_new(context);

    smstate = malloc(sizeof(OtrlSMState));
    assert(smstate != NULL);
    otrl_sm_state_new(smstate);
    context->smstate = smstate;

    context->our_instance = 0;
    context->their_instance = OTRL_INSTAG_MASTER;
    context->fingerprint_root.fingerprint = NULL;
    context->fingerprint_root.context = context;
    context->fingerprint_root.next = NULL;
    context->fingerprint_root.tous = NULL;
    context->active_fingerprint = NULL;
    memset(context->sessionid, 0, 20);
    context->sessionid_len = 0;
    context->protocol_version = 0;
    context->otr_offer = OFFER_NOT;
    context->app_data = NULL;
    context->app_data_free = NULL;
    context->context_priv = otrl_context_priv_new();
    assert(context->context_priv != NULL);
    context->next = NULL;
    context->m_context = context;
    context->recent_rcvd_child = NULL;
    context->recent_sent_child = NULL;
    context->recent_child = NULL;

    return context;
}

ConnContext * otrl_context_find_recent_instance(ConnContext * context,
	otrl_instag_t recent_instag) {
    ConnContext * m_context;

    if (!context) return NULL;

    m_context = context->m_context;

    if (!m_context) return NULL;

    switch(recent_instag) {
	case OTRL_INSTAG_RECENT:
	    return m_context->recent_child;
	case OTRL_INSTAG_RECENT_RECEIVED:
	    return m_context->recent_rcvd_child;
	case OTRL_INSTAG_RECENT_SENT:
	    return m_context->recent_sent_child;
	default:
	    return NULL;
    }
}

/* Find the instance of this context that has the best security level,
   and for which we have most recently received a message from. Note that most
   recent in this case is limited to a one-second resolution. */
ConnContext * otrl_context_find_recent_secure_instance(ConnContext * context)
{
    ConnContext *curp; /* for iteration */
    ConnContext *m_context; /* master */
    ConnContext *cresult = context;  /* best so far */

    if (!context) {
	return cresult;
    }

    m_context = context->m_context;

    for (curp = m_context; curp && curp->m_context == m_context;
	    curp = curp->next) {
	int msgstate_improved = 0; /* 0 == same, 1 == improved   */
	int trust_improved = 0;    /* (will immediately 'continue' if worse
				    * than) */

	if (cresult->msgstate == curp->msgstate) {
	    msgstate_improved = 0;
	} else if (curp->msgstate == OTRL_MSGSTATE_ENCRYPTED ||
		(cresult->msgstate == OTRL_MSGSTATE_PLAINTEXT &&
		curp->msgstate == OTRL_MSGSTATE_FINISHED)) {
	    msgstate_improved = 1;
	} else {
	    continue;
	}


	if (otrl_context_is_fingerprint_trusted(cresult->active_fingerprint) ==
		otrl_context_is_fingerprint_trusted(curp->active_fingerprint)) {

	    trust_improved = 0;
	} else if
		(otrl_context_is_fingerprint_trusted(curp->active_fingerprint)){

	    trust_improved = 1;
	} else {
	    continue;
	}

	if (msgstate_improved || trust_improved ||
		(!msgstate_improved && !trust_improved &&
		curp->context_priv->lastrecv >=
		cresult->context_priv->lastrecv)) {
	    cresult = curp;
	}
    }

    return cresult;
}

/* Look up a connection context by name/account/protocol/instag from the given
 * OtrlUserState.  If add_if_missing is true, allocate and return a new
 * context if one does not currently exist.  In that event, call
 * add_app_data(data, context) so that app_data and app_data_free can be
 * filled in by the application, and set *addedp to 1.
 * In the 'their_instance' field note that you can also specify a 'meta-
 * instance' value such as OTRL_INSTAG_MASTER, OTRL_INSTAG_RECENT,
 * OTRL_INSTAG_RECENT_RECEIVED and OTRL_INSTAG_RECENT_SENT. */
ConnContext * otrl_context_find(OtrlUserState us, const char *user,
	const char *accountname, const char *protocol,
	otrl_instag_t their_instance, int add_if_missing, int *addedp,
	void (*add_app_data)(void *data, ConnContext *context), void *data)
{
    ConnContext ** curp;
    int usercmp = 1, acctcmp = 1, protocmp = 1;
    if (addedp) *addedp = 0;
    if (!user || !accountname || !protocol) return NULL;

    for (curp = &(us->context_root); *curp; curp = &((*curp)->next)) {
	if ((usercmp = strcmp((*curp)->username, user)) > 0 ||
		(usercmp == 0 &&
		(acctcmp = strcmp((*curp)->accountname, accountname)) > 0) ||
		(usercmp == 0 && acctcmp == 0 &&
		(protocmp = strcmp((*curp)->protocol, protocol)) > 0) ||
		(usercmp == 0 && acctcmp == 0 && protocmp == 0
		&& (their_instance < OTRL_MIN_VALID_INSTAG ||
		    ((*curp)->their_instance >= their_instance))))
	    /* We're at the right place in the list.  We've either found
	     * it, or gone too far. */
	    break;
    }

    if (usercmp == 0 && acctcmp == 0 && protocmp == 0 && *curp &&
	    (their_instance < OTRL_MIN_VALID_INSTAG ||
	    (their_instance == (*curp)->their_instance))) {
	/* Found one! */
	if (their_instance >= OTRL_MIN_VALID_INSTAG ||
		their_instance == OTRL_INSTAG_MASTER) {
	    return *curp;
	}

	/* We need to go back and check more values in the context */
	switch(their_instance) {
	    case OTRL_INSTAG_BEST:
		return otrl_context_find_recent_secure_instance(*curp);
	    case OTRL_INSTAG_RECENT:
	    case OTRL_INSTAG_RECENT_RECEIVED:
	    case OTRL_INSTAG_RECENT_SENT:
		return otrl_context_find_recent_instance(*curp, their_instance);
	    default:
		return NULL;
	}
    }

    if (add_if_missing) {
	ConnContext *newctx;
	OtrlInsTag *our_instag = (OtrlInsTag *)otrl_instag_find(us, accountname,
		protocol);

	if (addedp) *addedp = 1;
	newctx = new_context(user, accountname, protocol);
	newctx->next = *curp;
	if (*curp) {
	    (*curp)->tous = &(newctx->next);
	}
	*curp = newctx;
	newctx->tous = curp;
	if (add_app_data) {
	    add_app_data(data, *curp);
	}

	/* Initialize specified instance tags */
	if (our_instag) {
	    newctx->our_instance = our_instag->instag;
	}

	if (their_instance >= OTRL_MIN_VALID_INSTAG ||
		their_instance == OTRL_INSTAG_MASTER) {
	    newctx->their_instance = their_instance;
	}

	if (their_instance >= OTRL_MIN_VALID_INSTAG) {
	    newctx->m_context = otrl_context_find(us, user, accountname,
		protocol, OTRL_INSTAG_MASTER, 1, NULL, add_app_data, data);
	}

	if (their_instance == OTRL_INSTAG_MASTER) {
	    /* if we're adding a master, there are no children, so the most
	     * recent context is the one we add. */
	    newctx->recent_child = newctx;
	    newctx->recent_rcvd_child = newctx;
	    newctx->recent_sent_child = newctx;
	}

	return *curp;
    }
    return NULL;
}

/* Return true iff the given fingerprint is marked as trusted. */
int otrl_context_is_fingerprint_trusted(Fingerprint *fprint) {
    return fprint && fprint->trust && fprint->trust[0] != '\0';
}

/* This method gets called after sending or receiving a message, to
 * update the master context's "recent context" pointers. */
void otrl_context_update_recent_child(ConnContext *context,
	unsigned int sent_msg) {
    ConnContext *m_context = context->m_context;

    if (sent_msg) {
	m_context->recent_sent_child = context;
    } else {
	m_context->recent_rcvd_child = context;
    }

    m_context->recent_child = context;

}

/* Find a fingerprint in a given context, perhaps adding it if not
 * present. */
Fingerprint *otrl_context_find_fingerprint(ConnContext *context,
	unsigned char fingerprint[20], int add_if_missing, int *addedp)
{
    Fingerprint *f;
    if (addedp) *addedp = 0;

    if (!context || !context->m_context) return NULL;

    context = context->m_context;

    f = context->fingerprint_root.next;
    while(f) {
	if (!memcmp(f->fingerprint, fingerprint, 20)) return f;
	f = f->next;
    }

    /* Didn't find it. */
    if (add_if_missing) {
	if (addedp) *addedp = 1;
	f = malloc(sizeof(*f));
	assert(f != NULL);
	f->fingerprint = malloc(20);
	assert(f->fingerprint != NULL);
	memmove(f->fingerprint, fingerprint, 20);
	f->context = context;
	f->trust = NULL;
	f->next = context->fingerprint_root.next;
	if (f->next) {
	    f->next->tous = &(f->next);
	}
	context->fingerprint_root.next = f;
	f->tous = &(context->fingerprint_root.next);
	return f;
    }
    return NULL;
}

/* Set the trust level for a given fingerprint */
void otrl_context_set_trust(Fingerprint *fprint, const char *trust)
{
    if (fprint == NULL) return;

    free(fprint->trust);
    fprint->trust = trust ? strdup(trust) : NULL;
}

/* Force a context into the OTRL_MSGSTATE_FINISHED state. */
void otrl_context_force_finished(ConnContext *context)
{
    context->msgstate = OTRL_MSGSTATE_FINISHED;
    otrl_auth_clear(&(context->auth));
    context->active_fingerprint = NULL;
    memset(context->sessionid, 0, 20);
    context->sessionid_len = 0;
    context->protocol_version = 0;
    otrl_sm_state_free(context->smstate);
    otrl_context_priv_force_finished(context->context_priv);
}

/* Force a context into the OTRL_MSGSTATE_PLAINTEXT state. */
void otrl_context_force_plaintext(ConnContext *context)
{
    /* First clean up everything we'd need to do for the FINISHED state */
    otrl_context_force_finished(context);

    /* And just set the state properly */
    context->msgstate = OTRL_MSGSTATE_PLAINTEXT;
}

/* Forget a fingerprint (so long as it's not the active one.  If it's a
 * fingerprint_root, forget the whole context (as long as
 * and_maybe_context is set, and it's PLAINTEXT).  Also, if it's not
 * the fingerprint_root, but it's the only fingerprint, and we're
 * PLAINTEXT, forget the whole context if and_maybe_context is set. */
void otrl_context_forget_fingerprint(Fingerprint *fprint,
	int and_maybe_context)
{
    ConnContext *context = fprint->context;
    if (fprint == &(context->fingerprint_root)) {
	if (context->msgstate == OTRL_MSGSTATE_PLAINTEXT &&
		and_maybe_context) {
	    otrl_context_forget(context);
	}
    } else {
	if (context->msgstate != OTRL_MSGSTATE_PLAINTEXT ||
		context->active_fingerprint != fprint) {

	    free(fprint->fingerprint);
	    free(fprint->trust);
	    *(fprint->tous) = fprint->next;
	    if (fprint->next) {
		fprint->next->tous = fprint->tous;
	    }
	    free(fprint);
	    if (context->msgstate == OTRL_MSGSTATE_PLAINTEXT &&
		    context->fingerprint_root.next == NULL &&
		    and_maybe_context) {
		/* We just deleted the only fingerprint.  Forget the
		 * whole thing. */
		otrl_context_forget(context);
	    }
	}
    }
}

/* Forget a whole context, so long as it's PLAINTEXT. If a context has child
 * instances, don't remove this instance unless children are also all in
 * PLAINTEXT state. In this case, the children will also be removed.
 * Returns 0 on success, 1 on failure. */
int otrl_context_forget(ConnContext *context)
{
    if (context->msgstate != OTRL_MSGSTATE_PLAINTEXT) return 1;

    if (context->their_instance == OTRL_INSTAG_MASTER) {
	ConnContext *c_iter;

	for (c_iter = context; c_iter &&
		c_iter->m_context == context->m_context;
		c_iter = c_iter->next) {
	    if (c_iter->msgstate != OTRL_MSGSTATE_PLAINTEXT) return 1;
	}

	c_iter = context->next;
	while (c_iter && c_iter->m_context == context->m_context) {
	    if (!otrl_context_forget(c_iter)) {
		c_iter = context->next;
	    } else {
		return 1;
	    }
	}

    }

    /* Just to be safe, force to plaintext.  This also frees any
     * extraneous data lying around. */
    otrl_context_force_plaintext(context);

    /* First free all the Fingerprints */
    while(context->fingerprint_root.next) {
	otrl_context_forget_fingerprint(context->fingerprint_root.next, 0);
    }
    /* Now free all the dynamic info here */
    free(context->username);
    free(context->accountname);
    free(context->protocol);
    free(context->smstate);
    context->username = NULL;
    context->accountname = NULL;
    context->protocol = NULL;
    context->smstate = NULL;

    /* Free the application data, if it exists */
    if (context->app_data && context->app_data_free) {
	(context->app_data_free)(context->app_data);
	context->app_data = NULL;
    }

    /* Fix the list linkages */
    *(context->tous) = context->next;
    if (context->next) {
	context->next->tous = context->tous;
    }

    free(context);
    return 0;
}

/* Forget all the contexts in a given OtrlUserState. */
void otrl_context_forget_all(OtrlUserState us)
{
    ConnContext *c_iter;

    for (c_iter = us->context_root; c_iter; c_iter = c_iter->next) {
	otrl_context_force_plaintext(c_iter);
    }

    while (us->context_root) {
	otrl_context_forget(us->context_root);
    }
}