Change to using Autoconf version string.
[onak.git] / armor.c
1 /*
2  * armor.c - Routines to (de)armor OpenPGP packet streams.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <stdlib.h>
10
11 #include "armor.h"
12 #include "config.h"
13 #include "keystructs.h"
14 #include "log.h"
15 #include "onak-conf.h"
16 #include "parsekey.h"
17
18 #define ARMOR_WIDTH 64
19
20 #define CRC24_INIT 0xb704ceL
21 #define CRC24_POLY 0x1864cfbL
22
23 /**
24  *
25  */
26 static unsigned char encode64(unsigned char c) {
27         if (c <= 25) {
28                 c += 'A';
29         } else if (c >= 26 && c <= 51) {
30                 c += 'a' - 26;
31         } else if (c >= 52 && c <= 61) {
32                 c += '0' - 52;
33         } else if (c == 62) {
34                 c = '+';
35         } else if (c == 63) {
36                 c = '/';
37         } else {
38                 log_assert(c < 64);
39         }
40
41         return c;
42 }
43
44 /**
45  *
46  */
47 static unsigned char decode64(unsigned char c) {
48         if (c >= 'A' && c <= 'Z') {
49                 c -= 'A';
50         } else if (c >= 'a' && c <= 'z') {
51                 c -= 'a' - 26;
52         } else if (c >= '0' && c <= '9') {
53                 c -= '0' - 52;
54         } else if (c == '+') {
55                 c = 62;
56         } else if (c == '/') {
57                 c = 63;
58         } else if (c == '=' || c == '-') {
59                 c = 64;
60         } else {
61                 c = 65;
62         }
63
64         return c;
65 }
66
67 /**
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.
74  */
75 struct armor_context {
76         unsigned char lastoctet;
77         int curoctet;
78         int count;
79         long crc24;
80         int (*putchar_func)(void *ctx, size_t count, unsigned char *c);
81         void *ctx;
82 };
83
84 static void armor_init(struct armor_context *ctx)
85 {
86         ctx->curoctet = 0;
87         ctx->lastoctet = 0;
88         ctx->count = 0;
89         ctx->crc24 = CRC24_INIT;
90 }
91
92 static void armor_finish(struct armor_context *state)
93 {
94         unsigned char c;
95
96         switch (state->curoctet++) {
97         case 0:
98                 break;
99         case 1:
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 *) "=");
104                 state->count += 3;
105                 if ((state->count % ARMOR_WIDTH) == 0) {
106                         state->putchar_func(state->ctx, 1,
107                                  (unsigned char *) "\n");
108                 }
109                 break;
110         case 2:
111                 c = encode64((state->lastoctet & 0xF) << 2);
112                 state->putchar_func(state->ctx, 1, &c);
113                 state->putchar_func(state->ctx, 1, (unsigned char *) "=");
114                 state->count += 2;
115                 if ((state->count % ARMOR_WIDTH) == 0) {
116                         state->putchar_func(state->ctx, 1,
117                                  (unsigned char *) "\n");
118                 }
119                 break;
120         }
121
122         state->crc24 &= 0xffffffL;
123         if ((state->count % ARMOR_WIDTH) != 0) {
124                 state->putchar_func(state->ctx, 1, (unsigned char *) "\n");
125         }
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");
136
137 }
138
139
140 static int armor_putchar_int(void *ctx, unsigned char c)
141 {
142         struct armor_context *state;
143         unsigned char t;
144         int i;
145
146         log_assert(ctx != NULL);
147         state = (struct armor_context *) ctx;
148
149         switch (state->curoctet++) {
150         case 0:
151                 t = encode64(c >> 2);
152                 state->putchar_func(state->ctx, 1, &t);
153                 state->count++;
154                 break;
155         case 1:
156                 t = encode64(((state->lastoctet & 3) << 4) + (c >> 4));
157                 state->putchar_func(state->ctx, 1, &t);
158                 state->count++;
159                 break;
160         case 2:
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);
165                 state->count += 2;
166                 break;
167         }
168         state->curoctet %= 3;
169         state->lastoctet = c;
170         
171         state->crc24 ^= c << 16;
172         for (i = 0; i < 8; i++) {
173                 state->crc24 <<= 1;
174                 if (state->crc24 & 0x1000000) {
175                         state->crc24 ^= CRC24_POLY;
176                 }
177         }
178
179         if ((state->count % ARMOR_WIDTH) == 0) {
180                 state->putchar_func(state->ctx, 1, (unsigned char *) "\n");
181         }
182
183         return 0;
184 }
185
186
187 static int armor_putchar(void *ctx, size_t count, unsigned char *c)
188 {
189         int i;
190
191         for (i = 0; i < count; i++) {
192                 armor_putchar_int(ctx, c[i]);
193         }
194         
195         return 0;
196 }
197
198 /**
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.
205  */
206 struct dearmor_context {
207         unsigned char lastoctet;
208         int curoctet;
209         int count;
210         long crc24;
211         int (*getchar_func)(void *ctx, size_t count, unsigned char *c);
212         void *ctx;
213 };
214
215 static void dearmor_init(struct dearmor_context *ctx)
216 {
217         ctx->curoctet = 0;
218         ctx->lastoctet = 0;
219         ctx->count = 0;
220         ctx->crc24 = CRC24_INIT;
221 }
222
223 static void dearmor_finish(struct dearmor_context *state)
224 {
225         /*
226          * Check the checksum
227          */
228
229         state->crc24 &= 0xffffffL;
230         /*
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));
237         */
238 }
239
240
241 static int dearmor_getchar(void *ctx, unsigned char *c)
242 {
243         struct dearmor_context *state;
244         unsigned char tmpc;
245         int i;
246
247         log_assert(ctx != NULL);
248         state = (struct dearmor_context *) ctx;
249         *c = 0;
250         
251         tmpc = 65;
252         while (tmpc == 65) {
253                 state->getchar_func(state->ctx, 1, &tmpc);
254                 tmpc = decode64(tmpc);
255         }
256
257         if (tmpc != 64) {
258                 switch (state->curoctet++) {
259                 case 0:
260                         state->lastoctet = tmpc;
261                         tmpc = 65;
262                         while (tmpc == 65) {
263                                 state->getchar_func(state->ctx, 1, &tmpc);
264                                 tmpc = decode64(tmpc);
265                         }
266                         *c = (state->lastoctet << 2) + (tmpc >> 4);
267                         break;
268                 case 1:
269                         *c = ((state->lastoctet & 0xF) << 4) + (tmpc >> 2);
270                         break;
271                 case 2:
272                         *c = ((state->lastoctet & 3) << 6) + tmpc;
273                         break;
274                 }
275         
276                 state->curoctet %= 3;
277                 state->lastoctet = tmpc;
278                 state->count++;
279                 
280                 state->crc24 ^= *c << 16;
281                 for (i = 0; i < 8; i++) {
282                         state->crc24 <<= 1;
283                         if (state->crc24 & 0x1000000) {
284                                 state->crc24 ^= CRC24_POLY;
285                         }
286                 }
287         }
288
289         return (tmpc == 64);
290 }
291
292 static int dearmor_getchar_c(void *ctx, size_t count, unsigned char *c)
293 {
294         int i, rc = 0;
295
296         for (i = 0; i < count && rc == 0; i++) {
297                 rc = dearmor_getchar(ctx, &c[i]);
298         }
299
300         return rc;
301 }
302
303 /**
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.
308  *
309  *      This function ASCII armors a list of OpenPGP packets and outputs it
310  *      using putchar_func.
311  */
312 int armor_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
313                                                 unsigned char *c),
314                                 void *ctx,
315                                 struct openpgp_packet_list *packets)
316 {
317         struct armor_context armor_ctx;
318
319         /*
320          * Print armor header
321          */
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");
326         
327         armor_init(&armor_ctx);
328         armor_ctx.putchar_func = putchar_func;
329         armor_ctx.ctx = ctx;
330         write_openpgp_stream(armor_putchar, &armor_ctx, packets);
331         armor_finish(&armor_ctx);
332
333         /*
334          * Print armor footer
335          */
336         putchar_func(ctx, sizeof("-----END PGP PUBLIC KEY BLOCK-----\n") - 1,
337                 (unsigned char *) "-----END PGP PUBLIC KEY BLOCK-----\n");
338
339         return 0;
340 }
341
342 /**
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.
347  *
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
350  *      packets.
351  */
352 int dearmor_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
353                                                 unsigned char *c),
354                                 void *ctx,
355                                 struct openpgp_packet_list **packets)
356 {
357         struct dearmor_context dearmor_ctx;
358         unsigned char curchar;
359         int state = 0;
360         int count = 0;
361
362         /*
363          * Look for armor header. We want "-----BEGIN.*\n", then some headers
364          * with :s in them, then a blank line, then the data.
365          */
366         state = 1;
367         while (state != 4 && !getchar_func(ctx, 1, &curchar)) {
368                 switch (state) {
369                         case 0:
370                                 if (curchar == '\n') {
371                                         count = 0;
372                                         state = 1;
373                                 }
374                                 break;
375                         case 1:
376                                 if (curchar == '-') {
377                                         count++;
378                                         if (count == 5) {
379                                                 state = 2;
380                                         }
381                                 } else if (curchar != '\n') {
382                                         state = 0;
383                                 }
384                                 break;
385                         case 2:
386                                 if (curchar == 'B') {
387                                         count = 0;
388                                         state = 3;
389                                 } else {
390                                         state = 0;
391                                 }
392                                 break;
393                         case 3:
394                                 if (curchar == '\n') {
395                                         count++;
396                                         if (count == 2) {
397                                                 state = 4;
398                                         }
399                                 } else if (curchar != '\r') {
400                                         count = 0;
401                                 }
402                                 break;
403                 }
404         }
405
406         if (state == 4) {
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,
411                         packets, 0);
412                 dearmor_finish(&dearmor_ctx);
413                 /*
414                  * TODO: Look for armor footer
415                  */
416         }
417
418         return 0;
419 }