sick

sign and check files using ed25519
Log | Files | Refs | Submodules | README | LICENSE

sick.c (9245B)


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
/* See LICENSE file for copyright and license details. */
#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "arg.h"
#include "base64.h"
#include "ed25519.h"

#define SIGBEGIN "-----BEGIN ED25519 SIGNATURE-----\n"
#define SIGEND   "-----END ED25519 SIGNATURE-----\n"

enum {
	ACT_NONE,
	ACT_SIGN,
	ACT_CHCK,
	ACT_TRIM
};

enum {
	ERR_NOKEY  = 1,
	ERR_NOSIG  = 2,
	ERR_NOMSG  = 3,
	ERR_NORING = 4
};

static void usage();
static size_t bufferize(char **buf, FILE *fp);
static size_t extractmsg(unsigned char *msg[], char *buf, size_t len);
static size_t extractsig(unsigned char *sig[], char *buf);
static int createkeypair(const char *);
static int check_keyring(unsigned char *sig, unsigned char *msg, size_t len);
static int sign(FILE *fp, FILE *key);
static int check(FILE *fp, FILE *key);
static int trimsig(FILE *fp);

char *argv0;
static int verbose = 0;

static void
usage()
{
	fprintf(stderr, "usage: %s [-stv] [-g ALIAS] [-f KEY] [FILE]\n",
			argv0);
	exit(EXIT_FAILURE);
}

/*
 * read chunks of data from a stream into a buffer, and return the size of the
 * buffer
 */
static size_t
bufferize(char **buf, FILE *fp)
{
	size_t n, len = 0;
	char chunk[MAX_INPUT], *tmp;

	/*
	 * For each chunk read, reallocate the buffer size to fit the newly
	 * read data, and copy it over
	 */
	while ((n = fread(chunk, 1, MAX_INPUT, fp)) > 0) {
		if ((tmp = realloc(*buf, len + n)) == NULL) {
			free(*buf);
			*buf = NULL;
			return 0;
		}

		*buf = tmp;
		memcpy((*buf) + len, chunk, n);
		len += n;
	}

	return len;
}

/*
 * Copy the full content of the buffer, minus the signature to the given
 * pointer
 */
static size_t
extractmsg(unsigned char **msg, char *buf, size_t buflen)
{
	size_t len = 0;
	char *sig;

	/* signature start is identified by SIGBEGIN */
	sig = strstr(buf, SIGBEGIN);

	/* if signature is not found, return the whole buffer */
	if (sig == NULL) {
		len = buflen;
	} else {
		len = sig - buf;
	}

	*msg = malloc(len);
	memcpy(*msg, buf, len);

	return len;
}

/*
 * Copy the signature at the end of the buffer to the given pointer
 */
static size_t
extractsig(unsigned char **sig, char *buf)
{
	off_t i;
	size_t n, len = 0;
	char *begin, *end, *tmp;
	unsigned char base64[76];

	/* search start and end strings for the signatures */
	begin = strstr(buf, SIGBEGIN) + strlen(SIGBEGIN);
	end   = strstr(buf, SIGEND);

	/* in case the signature block isn't well formated, return 0 */
	if (!(begin && end))
		return 0;

	/* ed25519 signatures are 64 bytes longs */
	*sig = malloc(64);
	if (*sig == NULL)
		return 0;

	memset(*sig, 0, 64);

	/*
	 * base64 signature are wrapped at 76 chars.
	 * 76 being a multiple of 4, it means we can decode the signature in
	 * chunks of 76 bytes, and concatenate them together to get the
	 * original data.
	 */
	for (i = 0; begin+i < end; i+=77) {
		/*
		 * black magic pointer arithmetic there..
		 * if we reached the "end" pointer, it means we're at the end
		 * of the signature.
		 * The line length is either 76 bytes long, or less (for the
		 * last line)
		 */
		n = begin+i+76 < end ? 76 : end - (begin + i);
		memset(base64, 0, 76);
		memcpy(base64, begin+i, n);

		n = base64_decode(&tmp, base64, n);
		memcpy((*sig) + len, tmp, n);
		len += n;
		free(tmp);
	}

	return len;
}

/*
 * Creates a set of ed25519 key pairs on disk.
 */
static int
createkeypair(const char *alias)
{
	size_t len = 0;
	FILE *fp = NULL;
	char fn[PATH_MAX];
	unsigned char seed[32], pub[32], priv[64];

	/*
	 * don't bother checking if `len > 0`. If the user wants to create
	 * files named ".key" and ".pub", that's OK.
	 */
	len = strnlen(alias, PATH_MAX);

	ed25519_create_seed(seed);
	ed25519_create_keypair(pub, priv, seed);

	/* write private key to "<alias>.key" */
	memset(fn, 0, PATH_MAX);
	memcpy(fn, alias, len);
	memcpy(fn+len, ".key", 4);
	if (verbose)
		fprintf(stderr, "Creating private key %s\n", fn);
	if ((fp = fopen(fn, "w")) == NULL) {
		perror(fn);
		return -1;
	}
	if (fwrite(priv, 1, sizeof(priv), fp) < sizeof(priv)) {
		fclose(fp);
		perror(fn);
		return -1;
	}
	fclose(fp);

	/* write public key to "<alias>.pub" */
	memcpy(fn+len, ".pub", 4);
	if (verbose)
		fprintf(stderr, "Creating public key %s\n", fn);
	if ((fp = fopen(fn, "w")) == NULL) {
		perror(fn);
		return -1;
	}
	if (fwrite(pub, 1, sizeof(pub), fp) < sizeof(pub)) {
		fclose(fp);
		perror(fn);
		return -1;
	}
	fclose(fp);

	return 0;
}

/*
 * Buffer a data stream, sign it, and write the buffer + base64 encoded
 * signature to stdout
 */
int
sign(FILE *fp, FILE *key)
{
	size_t len;
	char *base64, *buf = NULL;
	unsigned char sig[64], priv[64], *msg = NULL;

	if (key == NULL)
		return ERR_NOKEY;

	if (!fread(priv, 1, 64, key))
		return ERR_NOKEY;

	len = bufferize(&buf, fp);
	if (len == 0)
		return ERR_NOMSG;

	msg = malloc(len);
	memcpy(msg, buf, len);
	free(buf);

	if (verbose)
		fprintf(stderr, "Signing stream (%lu bytes)\n", len);

	ed25519_sign(sig, msg, len, priv);

	/* write buffer to stdout .. */
	fwrite(msg, 1, len, stdout);
	free(msg);

	/* .. followed by the signature delimiter .. */
	fwrite(SIGBEGIN, 1, strlen(SIGBEGIN), stdout);

	/* .. then the base64 encoded signature .. */
	len = base64_encode(&base64, sig, 64);
	base64_fold(stdout, base64, len, 0);
	free(base64);

	/* .. and the final signature delimiter! */
	fwrite(SIGEND, 1, strlen(SIGEND), stdout);

	return 0;
}

/*
 * Check a buffer against all files in the $KEYRING directory set in the
 * environment.
 */
static int
check_keyring(unsigned char *sig, unsigned char *msg, size_t len)
{
	int ret = 0;
	size_t n = 0;
	DIR *dirp = NULL;
	FILE *key = NULL;
	struct dirent *dt = NULL;
	char *keyring = NULL, path[PATH_MAX];
	unsigned char pub[32];

	/* get the keyring from the environment */
	keyring = getenv("KEYRING");
	if (keyring == NULL) {
		if (verbose)
			fprintf(stderr, "KEYRING not set\n");
		return ERR_NORING;
	}

	dirp = opendir(keyring);
	if (dirp == NULL) {
		perror(keyring);
		return ERR_NORING;
	}

	/* loop through all entries in the $KEYRING directory */
        while ((dt = readdir(dirp)) != NULL) {
		/* ignore entries that are not regular files */
                if (dt->d_type != DT_REG)
			continue;

		/* ignore all entries that are not 32 bytes long */
		if (dt->d_reclen != 32)
			continue;

		/* set public key file path and store its content */
                n = strnlen(keyring, PATH_MAX);
                memset(path, 0, PATH_MAX);
                memcpy(path, keyring, n);
                path[n] = '/';
                memcpy(path+n+1, dt->d_name, dt->d_reclen);
		if ((key = fopen(path, "r")) == NULL) {
			perror(path);
			continue;
		}
		if (fread(pub, 1, 32, key) < 32) {
			perror(path);
			fclose(key);
			continue;
		}

		/* check message for the given public key */
		ret += ed25519_verify(sig, msg, len, pub);
		if (ret) {
			if (verbose)
				fprintf(stderr, "Key match: %s\n", path);
			break;
		}
        }

        closedir(dirp);
	return !ret;
}

/*
 * Check the given stream against the key provided. If the stream pointer
 * supposed to hold the key is NULL, check the stream against all public keys
 * located in the $KEYRING directory.
 */
static int
check(FILE *fp, FILE *key)
{
	int ret = 0;
	size_t len;
	char *buf = NULL;
	unsigned char *sig, *msg, pub[32];

	if ((len = bufferize(&buf, fp)) == 0)
		return ERR_NOMSG;

	if (verbose)
		fprintf(stderr, "Extracting signature from input\n");

	if (extractsig(&sig, buf) == 0) {
		if (verbose)
			fprintf(stderr, "ERROR: No signature found\n");

		free(buf);
		return ERR_NOSIG;
	}

	if ((len = extractmsg(&msg, buf, len)) == 0) {
		free(buf);
		free(sig);
	}

	if (verbose)
		fprintf(stderr, "Verifying stream (%lu bytes)\n", len);

	if (key) {
		if (fread(pub, 1, 32, key) < 32)
			return ERR_NOKEY;

		ret = !ed25519_verify(sig, msg, len, pub);
	} else {
		ret = check_keyring(sig, msg, len);
	}

	/*
	 * if we're able to verify the signature, dump buffer's content to
	 * stdout
	 */
	if (ret == 0)
		fwrite(msg, 1, len, stdout);

	if (verbose)
		fprintf(stderr, "Stream check %s\n", ret ? "FAILED" : "OK");

	free(msg);
	free(buf);
	free(sig);

	return ret;
}

/*
 * Remove a signature from a stream, and dump it to stdout
 */
static int
trimsig(FILE *fp)
{
	size_t len = 0;
	char *buf = NULL;
	unsigned char *msg = NULL;

	len = bufferize(&buf, fp);
	if (!buf)
		return -1;

	len = extractmsg(&msg, buf, len);
	if (!msg) {
		free(buf);
		return ERR_NOMSG;
	}

	fwrite(msg, 1, len, stdout);

	free(buf);
	free(msg);

	return 0;
}

int
main(int argc, char *argv[])
{
	int ret = 0, action = ACT_CHCK;
	FILE *key = NULL, *fp = NULL;

	ARGBEGIN{
	case 'f':
		key = fopen(EARGF(usage()), "r");
		break;
	case 'g':
		return createkeypair(EARGF(usage()));
		break; /* NOTREACHED */
	case 's':
		action = ACT_SIGN;
		break;
	case 't':
		action = ACT_TRIM;
		break;
	case 'v':
		verbose = 1;
		break;
	default:
		usage();
	}ARGEND;

	/* if no argument is provided, read stdin */
	fp = argc ? fopen(*argv, "r") : stdin;

	switch (action) {
	case ACT_SIGN:
		ret |= sign(fp, key);
		break;
	case ACT_CHCK:
		ret |= check(fp, key);
		break;
	case ACT_TRIM:
		ret |= trimsig(fp);
		break;
	}

	fclose(fp);
	if (key)
		fclose(key);

	return ret;
}