File: armor.c

package info (click to toggle)
onak 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,788 kB
  • ctags: 819
  • sloc: ansic: 12,323; perl: 301; sh: 283; makefile: 179; sql: 21
file content (421 lines) | stat: -rw-r--r-- 9,341 bytes parent folder | download | duplicates (2)
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
/**
 * @file armor.c
 * @brief Routines to (de)armor OpenPGP packet streams.
 *
 * Copyright 2002-2004, 2011 Jonathan McDowell <noodles@earth.li>
 *
 * 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; version 2 of the License.
 *
 * 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.
 */

#include <stdlib.h>

#include "armor.h"
#include "keystructs.h"
#include "onak-conf.h"
#include "parsekey.h"
#include "version.h"

/**
 * @brief Line length we'll use for armored output
 */
#define ARMOR_WIDTH 64

/**
 * @brief CRC24 initialisation value
 */
#define CRC24_INIT 0xb704ceL
/**
 * @brief CRC24 polynomial value
 */
#define CRC24_POLY 0x1864cfbL

/**
 *
 */
static unsigned char encode64(unsigned char c) {
	if (c <= 25) {
		c += 'A';
	} else if (c >= 26 && c <= 51) {
		c += 'a' - 26;
	} else if (c >= 52 && c <= 61) {
		c += '0' - 52;
	} else if (c == 62) {
		c = '+';
	} else if (c == 63) {
		c = '/';
	} else {
		c = '?';
	}

	return c;
}

/**
 *
 */
static unsigned char decode64(unsigned char c) {
	if (c >= 'A' && c <= 'Z') {
		c -= 'A';
	} else if (c >= 'a' && c <= 'z') {
		c -= 'a' - 26;
	} else if (c >= '0' && c <= '9') {
		c -= '0' - 52;
	} else if (c == '+') {
		c = 62;
	} else if (c == '/') {
		c = 63;
	} else if (c == '=' || c == '-') {
		c = 64;
	} else {
		c = 65;
	}

	return c;
}

/**
 * @brief Holds the context of an ongoing ASCII armor operation
 */
struct armor_context {
	/** The last octet we got. */
	unsigned char lastoctet;
	/** The current octet we're expecting (0, 1 or 2). */
	int curoctet;
	/** The number of octets we've seen. */
	int count;
	/** A running CRC24 of the data we've seen. */
	long crc24;
	/** The function to output a character. */
	int (*putchar_func)(void *ctx, size_t count, void *c);
	/** Context for putchar_func. */
	void *ctx;
};

static void armor_init(struct armor_context *ctx)
{
	ctx->curoctet = 0;
	ctx->lastoctet = 0;
	ctx->count = 0;
	ctx->crc24 = CRC24_INIT;
}

static void armor_finish(struct armor_context *state)
{
	unsigned char c;

	switch (state->curoctet++) {
	case 0:
		break;
	case 1:
		c = encode64((state->lastoctet & 3) << 4);
		state->putchar_func(state->ctx, 1, &c);
		state->putchar_func(state->ctx, 1, (unsigned char *) "=");
		state->putchar_func(state->ctx, 1, (unsigned char *) "=");
		state->count += 3;
		if ((state->count % ARMOR_WIDTH) == 0) {
			state->putchar_func(state->ctx, 1,
				 (unsigned char *) "\n");
		}
		break;
	case 2:
		c = encode64((state->lastoctet & 0xF) << 2);
		state->putchar_func(state->ctx, 1, &c);
		state->putchar_func(state->ctx, 1, (unsigned char *) "=");
		state->count += 2;
		if ((state->count % ARMOR_WIDTH) == 0) {
			state->putchar_func(state->ctx, 1,
				 (unsigned char *) "\n");
		}
		break;
	}

	state->crc24 &= 0xffffffL;
	if ((state->count % ARMOR_WIDTH) != 0) {
		state->putchar_func(state->ctx, 1, (unsigned char *) "\n");
	}
	state->putchar_func(state->ctx, 1, (unsigned char *) "=");
	c = encode64(state->crc24 >> 18);
	state->putchar_func(state->ctx, 1, &c);
	c = encode64((state->crc24 >> 12) & 0x3F);
	state->putchar_func(state->ctx, 1, &c);
	c = encode64((state->crc24 >> 6) & 0x3F);
	state->putchar_func(state->ctx, 1, &c);
	c = encode64(state->crc24 & 0x3F);
	state->putchar_func(state->ctx, 1, &c);
	state->putchar_func(state->ctx, 1, (unsigned char *) "\n");

}


static int armor_putchar_int(void *ctx, unsigned char c)
{
	struct armor_context *state;
	unsigned char t;
	int i;

	state = (struct armor_context *) ctx;

	switch (state->curoctet++) {
	case 0:
		t = encode64(c >> 2);
		state->putchar_func(state->ctx, 1, &t);
		state->count++;
		break;
	case 1:
		t = encode64(((state->lastoctet & 3) << 4) + (c >> 4));
		state->putchar_func(state->ctx, 1, &t);
		state->count++;
		break;
	case 2:
		t = encode64(((state->lastoctet & 0xF) << 2) + (c >> 6));
		state->putchar_func(state->ctx, 1, &t);
		t = encode64(c & 0x3F);
		state->putchar_func(state->ctx, 1, &t);
		state->count += 2;
		break;
	}
	state->curoctet %= 3;
	state->lastoctet = c;
	
	state->crc24 ^= c << 16;
	for (i = 0; i < 8; i++) {
		state->crc24 <<= 1;
		if (state->crc24 & 0x1000000) {
			state->crc24 ^= CRC24_POLY;
		}
	}

	if ((state->count % ARMOR_WIDTH) == 0) {
		state->putchar_func(state->ctx, 1, (unsigned char *) "\n");
	}

	return 0;
}


static int armor_putchar(void *ctx, size_t count, void *c)
{
	int i;


	for (i = 0; i < count; i++) {
		armor_putchar_int(ctx, ((char *) c)[i]);
	}
	
	return 0;
}

/**
 * @brief Holds the context of an ongoing ASCII dearmor operation
 */
struct dearmor_context {
	/** The last octet we got. */
	unsigned char lastoctet;
	/** The current octet we're expecting (0, 1 or 2). */
	int curoctet;
	/** The number of octets we've seen. */
	int count;
	/** A running CRC24 of the data we've seen. */
	long crc24;
	/** The function to get the next character. */
	int (*getchar_func)(void *ctx, size_t count, void *c);
	/** Context for getchar_func. */
	void *ctx;
};

static void dearmor_init(struct dearmor_context *ctx)
{
	ctx->curoctet = 0;
	ctx->lastoctet = 0;
	ctx->count = 0;
	ctx->crc24 = CRC24_INIT;
}

static void dearmor_finish(struct dearmor_context *state)
{
	/*
	 * Check the checksum
	 */

	state->crc24 &= 0xffffffL;
	/*
	state->putchar_func(state->ctx, '\n');
	state->putchar_func(state->ctx, '=');
	state->putchar_func(state->ctx, encode64(state->crc24 >> 18));
	state->putchar_func(state->ctx, encode64((state->crc24 >> 12) & 0x3F));
	state->putchar_func(state->ctx, encode64((state->crc24 >> 6) & 0x3F));
	state->putchar_func(state->ctx, encode64(state->crc24 & 0x3F));
	*/
}


static int dearmor_getchar(void *ctx, unsigned char *c)
{
	struct dearmor_context *state;
	unsigned char tmpc;
	int i;

	state = (struct dearmor_context *) ctx;
	*c = 0;
	
	tmpc = 65;
	while (tmpc == 65) {
		state->getchar_func(state->ctx, 1, &tmpc);
		tmpc = decode64(tmpc);
	}

	if (tmpc != 64) {
		switch (state->curoctet++) {
		case 0:
			state->lastoctet = tmpc;
			tmpc = 65;
			while (tmpc == 65) {
				state->getchar_func(state->ctx, 1, &tmpc);
				tmpc = decode64(tmpc);
			}
			*c = (state->lastoctet << 2) + (tmpc >> 4);
			break;
		case 1:
			*c = ((state->lastoctet & 0xF) << 4) + (tmpc >> 2);
			break;
		case 2:
			*c = ((state->lastoctet & 3) << 6) + tmpc;
			break;
		}
	
		state->curoctet %= 3;
		state->lastoctet = tmpc;
		state->count++;
		
		state->crc24 ^= *c << 16;
		for (i = 0; i < 8; i++) {
			state->crc24 <<= 1;
			if (state->crc24 & 0x1000000) {
				state->crc24 ^= CRC24_POLY;
			}
		}
	}

	return (tmpc == 64);
}

static int dearmor_getchar_c(void *ctx, size_t count, void *c)
{
	int i, rc = 0;

	for (i = 0; i < count && rc == 0; i++) {
		rc = dearmor_getchar(ctx, &((unsigned char *) c)[i]);
	}

	return rc;
}

int armor_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
						void *c),
				void *ctx,
				struct openpgp_packet_list *packets)
{
	struct armor_context armor_ctx;

	/*
	 * Print armor header
	 */
	putchar_func(ctx, sizeof("-----BEGIN PGP PUBLIC KEY BLOCK-----\n") - 1,
		(unsigned char *) "-----BEGIN PGP PUBLIC KEY BLOCK-----\n");
	putchar_func(ctx, sizeof("Version: onak " ONAK_VERSION "\n\n") - 1,
		(unsigned char *) "Version: onak " ONAK_VERSION "\n\n");
	
	armor_init(&armor_ctx);
	armor_ctx.putchar_func = putchar_func;
	armor_ctx.ctx = ctx;
	write_openpgp_stream(armor_putchar, &armor_ctx, packets);
	armor_finish(&armor_ctx);

	/*
	 * Print armor footer
	 */
	putchar_func(ctx, sizeof("-----END PGP PUBLIC KEY BLOCK-----\n") - 1,
		(unsigned char *) "-----END PGP PUBLIC KEY BLOCK-----\n");

	return 0;
}

int dearmor_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
						void *c),
				void *ctx,
				struct openpgp_packet_list **packets)
{
	struct dearmor_context dearmor_ctx;
	unsigned char curchar;
	int state = 0;
	int count = 0;

	/*
	 * Look for armor header. We want "-----BEGIN.*\n", then some headers
	 * with :s in them, then a blank line, then the data.
	 */
	state = 1;
	while (state != 4 && !getchar_func(ctx, 1, &curchar)) {
		switch (state) {
			case 0:
				if (curchar == '\n') {
					count = 0;
					state = 1;
				}
				break;
			case 1:
				if (curchar == '-') {
					count++;
					if (count == 5) {
						state = 2;
					}
				} else if (curchar != '\n') {
					state = 0;
				}
				break;
			case 2:
				if (curchar == 'B') {
					count = 0;
					state = 3;
				} else {
					state = 0;
				}
				break;
			case 3:
				if (curchar == '\n') {
					count++;
					if (count == 2) {
						state = 4;
					}
				} else if (curchar != '\r') {
					count = 0;
				}
				break;
		}
	}

	if (state == 4) {
		dearmor_init(&dearmor_ctx);
		dearmor_ctx.getchar_func = getchar_func;
		dearmor_ctx.ctx = ctx;
		read_openpgp_stream(dearmor_getchar_c, &dearmor_ctx,
			packets, 0);
		dearmor_finish(&dearmor_ctx);
		/*
		 * TODO: Look for armor footer
		 */
	}

	return 0;
}