summaryrefslogtreecommitdiff
path: root/src/otr/key.c
blob: 7fee408488873609e67d47244ef6ed5db725cebf (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
/*
 * Off-the-Record Messaging (OTR) modules for IRC
 *
 * Copyright (C) 2008 - Uli Meis <a.sporto+bee@gmail.com>
 *               2012 - David Goulet <dgoulet@ev0ke.net>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
 */

#define _GNU_SOURCE
#include <glib.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/poll.h>
#include <signal.h>
#include <unistd.h>

#include "key.h"

#include "levels.h"
#include "network.h"
#include "pidwait.h"
#include "printtext.h"

#include "irssi-otr.h"
#include "otr-formats.h"

/*
 * Status of key generation.
 */
enum key_gen_status {
	KEY_GEN_IDLE		= 0,
	KEY_GEN_STARTED		= 1,
	KEY_GEN_RUNNING		= 2,
	KEY_GEN_FINISHED	= 3,
	KEY_GEN_ERROR		= 4,
};

/*
 * Data of the state of key generation.
 */
struct key_gen_data {
	struct otr_user_state *ustate;
	char *account_name;
	char *key_file_path;
	enum key_gen_status status;
	gcry_error_t gcry_error;
};

/*
 * Event from the key generation process.
 */
struct key_gen_event {
	enum key_gen_status status;
	gcry_error_t error;
};

/*
 * Key generation process.
 */
struct key_gen_worker {
	int tag;
	GIOChannel *pipes[2];
};

/*
 * Key generation data for the thread in charge of creating the key.
 */
static struct key_gen_data key_gen_state = {
	.status = KEY_GEN_IDLE,
	.gcry_error = GPG_ERR_NO_ERROR,
};

/*
 * Build file path concatenate to the irssi config dir.
 */
static char *file_path_build(const char *path)
{
	g_return_val_if_fail(path != NULL, NULL);

	/* Either NULL or the filename is returned here which is valid. */
	return g_strdup_printf("%s/%s", get_irssi_dir(), path);
}

/*
 * Emit a key generation status event.
 */
static void emit_event(GIOChannel *pipe, enum key_gen_status status, gcry_error_t error)
{
	struct key_gen_event event;

	g_return_if_fail(pipe != NULL);

	event.status = status;
	event.error = error;

	g_io_channel_write_block(pipe, &event, sizeof(event));
}

/*
 * Reset key generation state and status is IDLE.
 */
static void reset_key_gen_state(void)
{
	/* Safety. */
	g_free(key_gen_state.key_file_path);
	g_free(key_gen_state.account_name);

	/* Nullify everything. */
	memset(&key_gen_state, 0, sizeof(key_gen_state));
	key_gen_state.status = KEY_GEN_IDLE;
	key_gen_state.gcry_error = GPG_ERR_NO_ERROR;
}

/*
 * Read status event from key generation worker.
 */
static void read_key_gen_status(struct key_gen_worker *worker, GIOChannel *pipe)
{
	struct key_gen_event event;
	gcry_error_t err;

	g_return_if_fail(worker != NULL);

	fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);

	if (g_io_channel_read_block(pipe, &event, sizeof(event)) == -1) {
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
				TXT_OTR_KEYGEN_FAILED,
				key_gen_state.account_name,
				g_strerror(errno));
		return;
	}

	key_gen_state.status = event.status;
	key_gen_state.gcry_error = event.error;

	if (event.status == KEY_GEN_FINISHED || event.status == KEY_GEN_ERROR) {
		/* Worker is done. */
		g_source_remove(worker->tag);

		g_io_channel_shutdown(worker->pipes[0], TRUE, NULL);
		g_io_channel_unref(worker->pipes[0]);

		g_io_channel_shutdown(worker->pipes[1], TRUE, NULL);
		g_io_channel_unref(worker->pipes[1]);

		g_free(worker);

		if (event.status == KEY_GEN_ERROR) {
			printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
					TXT_OTR_KEYGEN_FAILED,
					key_gen_state.account_name,
					gcry_strerror(key_gen_state.gcry_error));
			reset_key_gen_state();
			return;
		}

		err = otrl_privkey_read(key_gen_state.ustate->otr_state, key_gen_state.key_file_path);

		if (err != GPG_ERR_NO_ERROR) {
			printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
					TXT_OTR_KEYGEN_FAILED,
					key_gen_state.account_name,
					gcry_strerror(key_gen_state.gcry_error));
		} else {
			printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
					TXT_OTR_KEYGEN_COMPLETED,
					key_gen_state.account_name);
		}

		reset_key_gen_state();
	}
}

/*
 * Run key generation in a seperate process (takes ages). The other process
 * will rewrite the key file, we shouldn't change anything till it's done and
 * we've reloaded the keys.
 */
void key_gen_run(struct otr_user_state *ustate, const char *account_name)
{
	struct key_gen_worker *worker;
	int fd[2];
	gcry_error_t err;
	pid_t pid;

	g_return_if_fail(ustate != NULL);
	g_return_if_fail(account_name != NULL);

	if (key_gen_state.status != KEY_GEN_IDLE) {
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_OTR_KEYGEN_RUNNING, key_gen_state.account_name);
		return;
	}

	/* Make sure the pointer does not go away during the proess. */
	key_gen_state.account_name = strdup(account_name);
	key_gen_state.ustate = ustate;
	key_gen_state.status = KEY_GEN_STARTED;

	/* Creating key file path. */
	key_gen_state.key_file_path = file_path_build(OTR_KEYFILE);
	if (key_gen_state.key_file_path == NULL) {
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
				TXT_OTR_KEYGEN_FAILED,
				key_gen_state.account_name,
				g_strerror(errno));
		reset_key_gen_state();
		return;
	}

	printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_OTR_KEYGEN_STARTED, key_gen_state.account_name);

	if (pipe(fd) != 0) {
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
				TXT_OTR_KEYGEN_FAILED,
				key_gen_state.account_name,
				g_strerror(errno));
		reset_key_gen_state();
		return;
	}

	worker = g_new0(struct key_gen_worker, 1);

	if (worker == NULL) {
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
				TXT_OTR_KEYGEN_FAILED,
				key_gen_state.account_name,
				g_strerror(errno));
		reset_key_gen_state();
		return;
	}

	worker->pipes[0] = g_io_channel_new(fd[0]);
	worker->pipes[1] = g_io_channel_new(fd[1]);

	pid = fork();

	if (pid > 0) {
		/* Parent process */
		pidwait_add(pid);
		worker->tag = g_input_add(worker->pipes[0], G_INPUT_READ, (GInputFunction)read_key_gen_status, worker);
		return;
	}

	if (pid != 0) {
		/* error */
		g_warning("Key generation failed: %s", g_strerror(errno));

		g_source_remove(worker->tag);

		g_io_channel_shutdown(worker->pipes[0], TRUE, NULL);
		g_io_channel_unref(worker->pipes[0]);

		g_io_channel_shutdown(worker->pipes[1], TRUE, NULL);
		g_io_channel_unref(worker->pipes[1]);

		g_free(worker);

		return;
	}

	/* Child process */
	key_gen_state.status = KEY_GEN_RUNNING;
	emit_event(worker->pipes[1], KEY_GEN_RUNNING, GPG_ERR_NO_ERROR);

	err = otrl_privkey_generate(key_gen_state.ustate->otr_state, key_gen_state.key_file_path, key_gen_state.account_name, OTR_PROTOCOL_ID);

	if (err != GPG_ERR_NO_ERROR) {
		emit_event(worker->pipes[1], KEY_GEN_ERROR, err);
		_exit(99);
		return;
	}

	emit_event(worker->pipes[1], KEY_GEN_FINISHED, GPG_ERR_NO_ERROR);

	_exit(99);
}

/*
 * Write fingerprints to file.
 */
void key_write_fingerprints(struct otr_user_state *ustate)
{
	gcry_error_t err;
	char *filename;

	g_return_if_fail(ustate != NULL);

	filename = file_path_build(OTR_FINGERPRINTS_FILE);
	g_return_if_fail(filename != NULL);

	err = otrl_privkey_write_fingerprints(ustate->otr_state, filename);
	if (err == GPG_ERR_NO_ERROR) {
		IRSSI_OTR_DEBUG("Fingerprints saved to %9%s%9", filename);
	} else {
		IRSSI_OTR_DEBUG("Error writing fingerprints: %d (%d)",
				gcry_strerror(err), gcry_strsource(err));
	}

	g_free(filename);
}

/*
 * Write instance tags to file.
 */
void key_write_instags(struct otr_user_state *ustate)
{
	gcry_error_t err;
	char *filename;

	g_return_if_fail(ustate != NULL);

	filename = file_path_build(OTR_INSTAG_FILE);
	g_return_if_fail(filename != NULL);

	err = otrl_instag_write(ustate->otr_state, filename);
	if (err == GPG_ERR_NO_ERROR) {
		IRSSI_OTR_DEBUG("Instance tags saved in %9%s%9", filename);
	} else {
		IRSSI_OTR_DEBUG("Error saving instance tags: %d (%d)",
				gcry_strerror(err), gcry_strsource(err));
	}

	g_free(filename);
}

/*
 * Load private keys.
 */
void key_load(struct otr_user_state *ustate)
{
	int ret;
	gcry_error_t err;
	char *filename;

	g_return_if_fail(ustate != NULL);

	filename = file_path_build(OTR_KEYFILE);
	g_return_if_fail(filename != NULL);

	ret = access(filename, F_OK);
	if (ret < 0) {
		IRSSI_OTR_DEBUG("No private keys found in %9%s%9", filename);
		g_free(filename);
		return;
	}

	err = otrl_privkey_read(ustate->otr_state, filename);
	if (err == GPG_ERR_NO_ERROR) {
		IRSSI_OTR_DEBUG("Private keys loaded from %9%s%9", filename);
	} else {
		IRSSI_OTR_DEBUG("Error loading private keys: %d (%d)",
				gcry_strerror(err), gcry_strsource(err));
	}

	g_free(filename);
}

/*
 * Load fingerprints.
 */
void key_load_fingerprints(struct otr_user_state *ustate)
{
	int ret;
	gcry_error_t err;
	char *filename;

	g_return_if_fail(ustate != NULL);

	filename = file_path_build(OTR_FINGERPRINTS_FILE);
	g_return_if_fail(filename != NULL);

	ret = access(filename, F_OK);
	if (ret < 0) {
		IRSSI_OTR_DEBUG("No fingerprints found in %9%s%9", filename);
		g_free(filename);
		return;
	}

	err = otrl_privkey_read_fingerprints(ustate->otr_state, filename, NULL,
			NULL);
	if (err == GPG_ERR_NO_ERROR) {
		IRSSI_OTR_DEBUG("Fingerprints loaded from %9%s%9", filename);
	} else {
		IRSSI_OTR_DEBUG("Error loading fingerprints: %d (%d)",
				gcry_strerror(err), gcry_strsource(err));
	}

	g_free(filename);
}