]> the.earth.li Git - onak.git/blob - keyindex.c
Enable use of systemd + socket activation support for Debian package
[onak.git] / keyindex.c
1 /*
2  * keyindex.c - Routines to list an OpenPGP key.
3  *
4  * Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <inttypes.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include "decodekey.h"
28 #include "getcgi.h"
29 #include "hash.h"
30 #include "keydb.h"
31 #include "keyid.h"
32 #include "keyindex.h"
33 #include "keystructs.h"
34 #include "log.h"
35 #include "onak.h"
36 #include "onak-conf.h"
37 #include "openpgp.h"
38
39 /*
40  * Convert a Public Key algorithm to its single character representation.
41  */
42 char pkalgo2char(uint8_t algo)
43 {
44         char typech;
45
46         switch (algo) {
47         case OPENPGP_PKALGO_DSA:
48                 typech = 'D';
49                 break;
50         case OPENPGP_PKALGO_ECDSA:
51                 typech = 'E';
52                 break;
53         case OPENPGP_PKALGO_EC:
54                 typech = 'e';
55                 break;
56         case OPENPGP_PKALGO_ELGAMAL_SIGN:
57                 typech = 'G';
58                 break;
59         case OPENPGP_PKALGO_ELGAMAL_ENC:
60                 typech = 'g';
61                 break;
62         case OPENPGP_PKALGO_RSA:
63                 typech = 'R';
64                 break;
65         case OPENPGP_PKALGO_RSA_ENC:
66                 typech = 'r';
67                 break;
68         case OPENPGP_PKALGO_RSA_SIGN:
69                 typech = 's';
70                 break;
71         default:
72                 typech = '?';
73                 break;
74         }
75
76         return typech;
77 }
78
79 /*
80  * Given a public key/subkey packet return the key length.
81  */
82 unsigned int keylength(struct openpgp_packet *keydata)
83 {
84         unsigned int length;
85
86         switch (keydata->data[0]) {
87         case 2:
88         case 3:
89                 length = (keydata->data[8] << 8) +
90                                 keydata->data[9];
91                 break;
92         case 4:
93                 switch (keydata->data[5]) {
94                 case OPENPGP_PKALGO_EC:
95                 case OPENPGP_PKALGO_ECDSA:
96                         /* Elliptic curve key size is based on OID */
97                         if ((keydata->data[6] == 8) &&
98                                         (keydata->data[7] == 0x2A) &&
99                                         (keydata->data[8] == 0x86) &&
100                                         (keydata->data[9] == 0x48) &&
101                                         (keydata->data[10] == 0xCE) &&
102                                         (keydata->data[11] == 0x3D) &&
103                                         (keydata->data[12] == 0x03) &&
104                                         (keydata->data[13] == 0x01) &&
105                                         (keydata->data[14] == 0x07)) {
106                                 length = 256;
107                         } else if ((keydata->data[6] == 5) &&
108                                         (keydata->data[7] == 0x2B) &&
109                                         (keydata->data[8] == 0x81) &&
110                                         (keydata->data[9] == 0x04) &&
111                                         (keydata->data[10] == 0x00) &&
112                                         (keydata->data[11] == 0x22)) {
113                                 length = 384;
114                         } else if ((keydata->data[6] == 5) &&
115                                         (keydata->data[7] == 0x2B) &&
116                                         (keydata->data[8] == 0x81) &&
117                                         (keydata->data[9] == 0x04) &&
118                                         (keydata->data[10] == 0x00) &&
119                                         (keydata->data[11] == 0x23)) {
120                                 length = 521;
121                         } else {
122                                 logthing(LOGTHING_ERROR,
123                                         "Unknown elliptic curve size");
124                                 length = 0;
125                         }
126                         break;
127                 default:
128                         length = (keydata->data[6] << 8) +
129                                 keydata->data[7];
130                 }
131                 break;
132         default:
133                 logthing(LOGTHING_ERROR, "Unknown key version: %d",
134                         keydata->data[0]);
135                 length = 0;
136         }
137
138         return length;
139 }
140
141 int list_sigs(struct onak_dbctx *dbctx,
142                 struct openpgp_packet_list *sigs, bool html)
143 {
144         char *uid = NULL;
145         uint64_t sigid = 0;
146         char *sig = NULL;
147
148         while (sigs != NULL) {
149                 sigid = sig_keyid(sigs->packet);
150                 uid = dbctx->keyid2uid(dbctx, sigid);
151                 if (sigs->packet->data[0] == 4 &&
152                                 sigs->packet->data[1] == 0x30) {
153                         /* It's a Type 4 sig revocation */
154                         sig = "rev";
155                 } else {
156                         sig = "sig";
157                 }
158                 if (html && uid != NULL) {
159                         printf("%s         <a href=\"lookup?op=get&"
160                                 "search=0x%016" PRIX64 "\">%08" PRIX64
161                                 "</a>             "
162                                 "<a href=\"lookup?op=vindex&search=0x%016"
163                                 PRIX64 "\">%s</a>\n",
164                                 sig,
165                                 sigid,
166                                 sigid & 0xFFFFFFFF,
167                                 sigid,
168                                 txt2html(uid));
169                 } else if (html && uid == NULL) {
170                         printf("%s         %08" PRIX64 "             "
171                                 "[User id not found]\n",
172                                 sig,
173                                 sigid & 0xFFFFFFFF);
174                 } else {
175                         printf("%s         %08" PRIX64
176                                 "             %s\n",
177                                 sig,
178                                 sigid & 0xFFFFFFFF,
179                                 (uid != NULL) ? uid :
180                                 "[User id not found]");
181                 }
182                 if (uid != NULL) {
183                         free(uid);
184                         uid = NULL;
185                 }
186                 sigs = sigs->next;
187         }
188
189         return 0;
190 }
191
192 int list_uids(struct onak_dbctx *dbctx,
193                 uint64_t keyid, struct openpgp_signedpacket_list *uids,
194                 bool verbose, bool html)
195 {
196         char buf[1024];
197         int  imgindx = 0;
198
199         while (uids != NULL) {
200                 if (uids->packet->tag == OPENPGP_PACKET_UID) {
201                         snprintf(buf, 1023, "%.*s",
202                                 (int) uids->packet->length,
203                                 uids->packet->data);
204                         printf("                                %s\n",
205                                 (html) ? txt2html(buf) : buf);
206                 } else if (uids->packet->tag == OPENPGP_PACKET_UAT) {
207                         printf("                                ");
208                         if (html) {
209                                 printf("<img src=\"lookup?op=photo&search="
210                                         "0x%016" PRIX64 "&idx=%d\" alt=\""
211                                         "[photo id]\">\n",
212                                         keyid,
213                                         imgindx);
214                                 imgindx++;
215                         } else {
216                                 printf("[photo id]\n");
217                         }
218                 }
219                 if (verbose) {
220                         list_sigs(dbctx, uids->sigs, html);
221                 }
222                 uids = uids->next;
223         }
224
225         return 0;
226 }
227
228 int list_subkeys(struct onak_dbctx *dbctx,
229                 struct openpgp_signedpacket_list *subkeys, bool verbose,
230                 bool html)
231 {
232         struct tm       *created = NULL;
233         time_t          created_time = 0;
234         int             type = 0;
235         int             length = 0;
236         uint64_t        keyid = 0;
237
238         while (subkeys != NULL) {
239                 if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
240
241                         created_time = (subkeys->packet->data[1] << 24) +
242                                         (subkeys->packet->data[2] << 16) +
243                                         (subkeys->packet->data[3] << 8) +
244                                         subkeys->packet->data[4];
245                         created = gmtime(&created_time);
246
247                         switch (subkeys->packet->data[0]) {
248                         case 2:
249                         case 3:
250                                 type = subkeys->packet->data[7];
251                                 break;
252                         case 4:
253                                 type = subkeys->packet->data[5];
254                                 break;
255                         default:
256                                 logthing(LOGTHING_ERROR,
257                                         "Unknown key type: %d",
258                                         subkeys->packet->data[0]);
259                         }
260                         length = keylength(subkeys->packet);
261
262                         if (get_packetid(subkeys->packet,
263                                         &keyid) != ONAK_E_OK) {
264                                 logthing(LOGTHING_ERROR, "Couldn't get keyid.");
265                         }
266                         printf("sub  %5d%c/%08X %04d/%02d/%02d\n",
267                                 length,
268                                 pkalgo2char(type),
269                                 (uint32_t) (keyid & 0xFFFFFFFF),
270                                 created->tm_year + 1900,
271                                 created->tm_mon + 1,
272                                 created->tm_mday);
273
274                 }
275                 if (verbose) {
276                         list_sigs(dbctx, subkeys->sigs, html);
277                 }
278                 subkeys = subkeys->next;
279         }
280
281         return 0;
282 }
283
284 void display_fingerprint(struct openpgp_publickey *key)
285 {
286         int             i = 0;
287         struct openpgp_fingerprint fingerprint;
288
289         get_fingerprint(key->publickey, &fingerprint);
290         printf("      Key fingerprint =");
291         for (i = 0; i < fingerprint.length; i++) {
292                 if ((fingerprint.length == 16) ||
293                         (i % 2 == 0)) {
294                         printf(" ");
295                 }
296                 if (fingerprint.length == 20 &&
297                                 (i * 2) == fingerprint.length) {
298                         /* Extra space in the middle of a SHA1 fingerprint */
299                         printf(" ");
300                 }
301                 printf("%02X", fingerprint.fp[i]);
302         }
303         printf("\n");
304
305         return;
306 }
307
308 void display_skshash(struct openpgp_publickey *key, bool html)
309 {
310         int             i = 0;
311         struct skshash  hash;
312
313         get_skshash(key, &hash);
314         printf("      Key hash = ");
315         if (html) {
316                 printf("<a href=\"lookup?op=hget&search=");
317                 for (i = 0; i < sizeof(hash.hash); i++) {
318                         printf("%02X", hash.hash[i]);
319                 }
320                 printf("\">");
321         }
322         for (i = 0; i < sizeof(hash.hash); i++) {
323                 printf("%02X", hash.hash[i]);
324         }
325         if (html) {
326                 printf("</a>");
327         }
328         printf("\n");
329
330         return;
331 }
332
333 /**
334  *      key_index - List a set of OpenPGP keys.
335  *      @keys: The keys to display.
336  *      @verbose: Should we list sigs as well?
337  *      @fingerprint: List the fingerprint?
338  *      @html: Should the output be tailored for HTML?
339  *
340  *      This function takes a list of OpenPGP public keys and displays an index
341  *      of them. Useful for debugging or the keyserver Index function.
342  */
343 int key_index(struct onak_dbctx *dbctx,
344                 struct openpgp_publickey *keys, bool verbose, bool fingerprint,
345                         bool skshash, bool html)
346 {
347         struct openpgp_signedpacket_list        *curuid = NULL;
348         struct tm                               *created = NULL;
349         time_t                                   created_time = 0;
350         int                                      type = 0;
351         int                                      length = 0;
352         char                                     buf[1024];
353         uint64_t                                 keyid;
354
355         if (html) {
356                 puts("<pre>");
357         }
358         puts("Type   bits/keyID    Date       User ID");
359         while (keys != NULL) {
360                 created_time = (keys->publickey->data[1] << 24) +
361                                         (keys->publickey->data[2] << 16) +
362                                         (keys->publickey->data[3] << 8) +
363                                         keys->publickey->data[4];
364                 created = gmtime(&created_time);
365
366                 switch (keys->publickey->data[0]) {
367                 case 2:
368                 case 3:
369                         type = keys->publickey->data[7];
370                         break;
371                 case 4:
372                         type = keys->publickey->data[5];
373                         break;
374                 default:
375                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
376                                 keys->publickey->data[0]);
377                 }
378                 length = keylength(keys->publickey);
379
380                 if (get_keyid(keys, &keyid) != ONAK_E_OK) {
381                         logthing(LOGTHING_ERROR, "Couldn't get keyid.");
382                 }
383
384                 if (html) {
385                         printf("pub  %5d%c/<a href=\"lookup?op=get&"
386                                 "search=0x%016" PRIX64 "\">%08" PRIX64
387                                 "</a> %04d/%02d/%02d ",
388                                 length,
389                                 pkalgo2char(type),
390                                 keyid,
391                                 keyid & 0xFFFFFFFF,
392                                 created->tm_year + 1900,
393                                 created->tm_mon + 1,
394                                 created->tm_mday);
395                 } else {
396                         printf("pub  %5d%c/%08" PRIX64 " %04d/%02d/%02d ",
397                                 length,
398                                 pkalgo2char(type),
399                                 keyid & 0xFFFFFFFF,
400                                 created->tm_year + 1900,
401                                 created->tm_mon + 1,
402                                 created->tm_mday);
403                 }
404
405                 curuid = keys->uids;
406                 if (curuid != NULL &&
407                                 curuid->packet->tag == OPENPGP_PACKET_UID) {
408                         snprintf(buf, 1023, "%.*s",
409                                 (int) curuid->packet->length,
410                                 curuid->packet->data);
411                         if (html) {
412                                 printf("<a href=\"lookup?op=vindex&"
413                                         "search=0x%016" PRIX64 "\">",
414                                         keyid);
415                         }
416                         printf("%s%s%s\n", 
417                                 (html) ? txt2html(buf) : buf,
418                                 (html) ? "</a>" : "",
419                                 (keys->revoked) ? " *** REVOKED ***" : "");
420                         if (skshash) {
421                                 display_skshash(keys, html);
422                         }
423                         if (fingerprint) {
424                                 display_fingerprint(keys);
425                         }
426                         if (verbose) {
427                                 list_sigs(dbctx, curuid->sigs, html);
428                         }
429                         curuid = curuid->next;
430                 } else {
431                         printf("%s\n", 
432                                 (keys->revoked) ? "*** REVOKED ***": "");
433                         if (fingerprint) {
434                                 display_fingerprint(keys);
435                         }
436                 }
437
438                 list_uids(dbctx, keyid, curuid, verbose, html);
439                 if (verbose) {
440                         list_subkeys(dbctx, keys->subkeys, verbose, html);
441                 }
442
443                 keys = keys->next;
444         }
445
446         if (html) {
447                 puts("</pre>");
448         }
449
450         return 0;
451 }
452
453 /**
454  *      mrkey_index - List a set of OpenPGP keys in the MRHKP format.
455  *      @keys: The keys to display.
456  *
457  *      This function takes a list of OpenPGP public keys and displays a
458  *      machine readable list of them.
459  */
460 int mrkey_index(struct openpgp_publickey *keys)
461 {
462         struct openpgp_signedpacket_list        *curuid = NULL;
463         time_t                                   created_time = 0;
464         int                                      type = 0;
465         int                                      length = 0;
466         int                                      i = 0;
467         int                                      c;
468         uint64_t                                 keyid;
469         struct openpgp_fingerprint fingerprint;
470
471         while (keys != NULL) {
472                 created_time = (keys->publickey->data[1] << 24) +
473                                         (keys->publickey->data[2] << 16) +
474                                         (keys->publickey->data[3] << 8) +
475                                         keys->publickey->data[4];
476
477                 printf("pub:");
478
479                 switch (keys->publickey->data[0]) {
480                 case 2:
481                 case 3:
482                         if (get_keyid(keys, &keyid) != ONAK_E_OK) {
483                                 logthing(LOGTHING_ERROR, "Couldn't get keyid");
484                         }
485                         printf("%016" PRIX64, keyid);
486                         type = keys->publickey->data[7];
487                         break;
488                 case 4:
489                         (void) get_fingerprint(keys->publickey, &fingerprint);
490
491                         for (i = 0; i < fingerprint.length; i++) {
492                                 printf("%02X", fingerprint.fp[i]);
493                         }
494
495                         type = keys->publickey->data[5];
496                         break;
497                 default:
498                         logthing(LOGTHING_ERROR, "Unknown key type: %d",
499                                 keys->publickey->data[0]);
500                 }
501                 length = keylength(keys->publickey);
502
503                 printf(":%d:%d:%ld::%s\n",
504                         type,
505                         length,
506                         created_time,
507                         (keys->revoked) ? "r" : "");
508         
509                 for (curuid = keys->uids; curuid != NULL;
510                          curuid = curuid->next) {
511                 
512                         if (curuid->packet->tag == OPENPGP_PACKET_UID) {
513                                 printf("uid:");
514                                 for (i = 0; i < (int) curuid->packet->length;
515                                                 i++) {
516                                         c = curuid->packet->data[i];
517                                         if (c == '%') {
518                                                 putchar('%');
519                                                 putchar(c);
520                                         } else if (c == ':' || c > 127) {
521                                                 printf("%%%X", c);
522                                         } else {
523                                                 putchar(c);
524                                         }
525                                 }
526                                 printf("\n");
527                         }
528                 }
529                 keys = keys->next;
530         }
531         return 0;
532 }