2 * armor.c - Routines to (de)armor OpenPGP packet streams.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
13 #include "keystructs.h"
15 #include "onak-conf.h"
18 #define ARMOR_WIDTH 64
20 #define CRC24_INIT 0xb704ceL
21 #define CRC24_POLY 0x1864cfbL
26 static unsigned char encode64(unsigned char c) {
29 } else if (c >= 26 && c <= 51) {
31 } else if (c >= 52 && c <= 61) {
47 static unsigned char decode64(unsigned char c) {
48 if (c >= 'A' && c <= 'Z') {
50 } else if (c >= 'a' && c <= 'z') {
52 } else if (c >= '0' && c <= '9') {
54 } else if (c == '+') {
56 } else if (c == '/') {
58 } else if (c == '=' || c == '-') {
68 * @lastoctet: The last octet we got.
69 * @curoctet: The current octet we're expecting (0, 1 or 2).
70 * @count: The number of octets we've seen.
71 * @crc24: A running CRC24 of the data we've seen.
72 * @putchar_func: The function to output a character.
73 * @ctx: Context for putchar_func.
75 struct armor_context {
76 unsigned char lastoctet;
80 int (*putchar_func)(void *ctx, size_t count, unsigned char *c);
84 static void armor_init(struct armor_context *ctx)
89 ctx->crc24 = CRC24_INIT;
92 static void armor_finish(struct armor_context *state)
96 switch (state->curoctet++) {
100 c = encode64((state->lastoctet & 3) << 4);
101 state->putchar_func(state->ctx, 1, &c);
102 state->putchar_func(state->ctx, 1, (unsigned char *) "=");
103 state->putchar_func(state->ctx, 1, (unsigned char *) "=");
105 if ((state->count % ARMOR_WIDTH) == 0) {
106 state->putchar_func(state->ctx, 1,
107 (unsigned char *) "\n");
111 c = encode64((state->lastoctet & 0xF) << 2);
112 state->putchar_func(state->ctx, 1, &c);
113 state->putchar_func(state->ctx, 1, (unsigned char *) "=");
115 if ((state->count % ARMOR_WIDTH) == 0) {
116 state->putchar_func(state->ctx, 1,
117 (unsigned char *) "\n");
122 state->crc24 &= 0xffffffL;
123 if ((state->count % ARMOR_WIDTH) != 0) {
124 state->putchar_func(state->ctx, 1, (unsigned char *) "\n");
126 state->putchar_func(state->ctx, 1, (unsigned char *) "=");
127 c = encode64(state->crc24 >> 18);
128 state->putchar_func(state->ctx, 1, &c);
129 c = encode64((state->crc24 >> 12) & 0x3F);
130 state->putchar_func(state->ctx, 1, &c);
131 c = encode64((state->crc24 >> 6) & 0x3F);
132 state->putchar_func(state->ctx, 1, &c);
133 c = encode64(state->crc24 & 0x3F);
134 state->putchar_func(state->ctx, 1, &c);
135 state->putchar_func(state->ctx, 1, (unsigned char *) "\n");
140 static int armor_putchar_int(void *ctx, unsigned char c)
142 struct armor_context *state;
146 log_assert(ctx != NULL);
147 state = (struct armor_context *) ctx;
149 switch (state->curoctet++) {
151 t = encode64(c >> 2);
152 state->putchar_func(state->ctx, 1, &t);
156 t = encode64(((state->lastoctet & 3) << 4) + (c >> 4));
157 state->putchar_func(state->ctx, 1, &t);
161 t = encode64(((state->lastoctet & 0xF) << 2) + (c >> 6));
162 state->putchar_func(state->ctx, 1, &t);
163 t = encode64(c & 0x3F);
164 state->putchar_func(state->ctx, 1, &t);
168 state->curoctet %= 3;
169 state->lastoctet = c;
171 state->crc24 ^= c << 16;
172 for (i = 0; i < 8; i++) {
174 if (state->crc24 & 0x1000000) {
175 state->crc24 ^= CRC24_POLY;
179 if ((state->count % ARMOR_WIDTH) == 0) {
180 state->putchar_func(state->ctx, 1, (unsigned char *) "\n");
187 static int armor_putchar(void *ctx, size_t count, unsigned char *c)
191 for (i = 0; i < count; i++) {
192 armor_putchar_int(ctx, c[i]);
199 * @lastoctet: The last octet we got.
200 * @curoctet: The current octet we're expecting (0, 1 or 2).
201 * @count: The number of octets we've seen.
202 * @crc24: A running CRC24 of the data we've seen.
203 * @putchar_func: The function to output a character.
204 * @ctx: Context for putchar_func.
206 struct dearmor_context {
207 unsigned char lastoctet;
211 int (*getchar_func)(void *ctx, size_t count, unsigned char *c);
215 static void dearmor_init(struct dearmor_context *ctx)
220 ctx->crc24 = CRC24_INIT;
223 static void dearmor_finish(struct dearmor_context *state)
229 state->crc24 &= 0xffffffL;
231 state->putchar_func(state->ctx, '\n');
232 state->putchar_func(state->ctx, '=');
233 state->putchar_func(state->ctx, encode64(state->crc24 >> 18));
234 state->putchar_func(state->ctx, encode64((state->crc24 >> 12) & 0x3F));
235 state->putchar_func(state->ctx, encode64((state->crc24 >> 6) & 0x3F));
236 state->putchar_func(state->ctx, encode64(state->crc24 & 0x3F));
241 static int dearmor_getchar(void *ctx, unsigned char *c)
243 struct dearmor_context *state;
247 log_assert(ctx != NULL);
248 state = (struct dearmor_context *) ctx;
253 state->getchar_func(state->ctx, 1, &tmpc);
254 tmpc = decode64(tmpc);
258 switch (state->curoctet++) {
260 state->lastoctet = tmpc;
263 state->getchar_func(state->ctx, 1, &tmpc);
264 tmpc = decode64(tmpc);
266 *c = (state->lastoctet << 2) + (tmpc >> 4);
269 *c = ((state->lastoctet & 0xF) << 4) + (tmpc >> 2);
272 *c = ((state->lastoctet & 3) << 6) + tmpc;
276 state->curoctet %= 3;
277 state->lastoctet = tmpc;
280 state->crc24 ^= *c << 16;
281 for (i = 0; i < 8; i++) {
283 if (state->crc24 & 0x1000000) {
284 state->crc24 ^= CRC24_POLY;
292 static int dearmor_getchar_c(void *ctx, size_t count, unsigned char *c)
296 for (i = 0; i < count && rc == 0; i++) {
297 rc = dearmor_getchar(ctx, &c[i]);
304 * armor_openpgp_stream - Takes a list of OpenPGP packets and armors it.
305 * @putchar_func: The function to output the next armor character.
306 * @ctx: The context pointer for putchar_func.
307 * @packets: The list of packets to output.
309 * This function ASCII armors a list of OpenPGP packets and outputs it
310 * using putchar_func.
312 int armor_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
315 struct openpgp_packet_list *packets)
317 struct armor_context armor_ctx;
322 putchar_func(ctx, sizeof("-----BEGIN PGP PUBLIC KEY BLOCK-----\n") - 1,
323 (unsigned char *) "-----BEGIN PGP PUBLIC KEY BLOCK-----\n");
324 putchar_func(ctx, sizeof("Version: onak " PACKAGE_VERSION "\n\n") - 1,
325 (unsigned char *) "Version: onak " PACKAGE_VERSION "\n\n");
327 armor_init(&armor_ctx);
328 armor_ctx.putchar_func = putchar_func;
330 write_openpgp_stream(armor_putchar, &armor_ctx, packets);
331 armor_finish(&armor_ctx);
336 putchar_func(ctx, sizeof("-----END PGP PUBLIC KEY BLOCK-----\n") - 1,
337 (unsigned char *) "-----END PGP PUBLIC KEY BLOCK-----\n");
343 * dearmor_openpgp_stream - Reads & decodes an ACSII armored OpenPGP msg.
344 * @getchar_func: The function to get the next character from the stream.
345 * @ctx: The context pointer for getchar_func.
346 * @packets: The list of packets.
348 * This function uses getchar_func to read characters from an ASCII
349 * armored OpenPGP stream and outputs the data as a linked list of
352 int dearmor_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
355 struct openpgp_packet_list **packets)
357 struct dearmor_context dearmor_ctx;
358 unsigned char curchar;
363 * Look for armor header. We want "-----BEGIN.*\n", then some headers
364 * with :s in them, then a blank line, then the data.
367 while (state != 4 && !getchar_func(ctx, 1, &curchar)) {
370 if (curchar == '\n') {
376 if (curchar == '-') {
381 } else if (curchar != '\n') {
386 if (curchar == 'B') {
394 if (curchar == '\n') {
399 } else if (curchar != '\r') {
407 dearmor_init(&dearmor_ctx);
408 dearmor_ctx.getchar_func = getchar_func;
409 dearmor_ctx.ctx = ctx;
410 read_openpgp_stream(dearmor_getchar_c, &dearmor_ctx,
412 dearmor_finish(&dearmor_ctx);
414 * TODO: Look for armor footer