b9c5c889ddfad04fb81ab4cb882c45160355b10e
[project/ucert.git] / ucert.c
1 /*
2 * Copyright (C) 2018 Daniel Golle <daniel@makrotopia.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #define _GNU_SOURCE
15
16 #include <fcntl.h>
17 #include <dlfcn.h>
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <getopt.h>
23 #include <stdint.h>
24 #include <unistd.h>
25 #include <inttypes.h>
26 #include <sys/stat.h>
27 #include <sys/wait.h>
28
29 #include <json-c/json.h>
30 #include <libubox/blob.h>
31 #include <libubox/utils.h>
32 #include <libubox/list.h>
33 #include <libubox/vlist.h>
34 #include <libubox/blobmsg_json.h>
35
36 #include "usign.h"
37
38 #define CERT_BUF_LEN 4096
39
40 static enum {
41 CMD_APPEND,
42 CMD_DUMP,
43 CMD_ISSUE,
44 CMD_REVOKE,
45 CMD_VERIFY,
46 CMD_NONE,
47 } cmd = CMD_NONE;
48
49 static bool quiet;
50 #ifndef UCERT_STRIP_MESSAGES
51 #define DPRINTF(format, ...) \
52 do { \
53 if (!quiet) \
54 fprintf(stderr, "%s(%d): " format, __func__, __LINE__, ## __VA_ARGS__); \
55 } while (0)
56 #else
57 #define DPRINTF(format, ...) do { } while (0)
58 #endif
59
60 /*
61 * ucert structure
62 * | BLOB |
63 * | SIGNATURE | PAYLOAD |
64 * | |[ BLOBMSG CONTAINER ]|
65 * | |[[T,i,v,e,f,pubkey ]]|
66 */
67 enum cert_attr {
68 CERT_ATTR_SIGNATURE,
69 CERT_ATTR_PAYLOAD,
70 CERT_ATTR_MAX
71 };
72
73 static const struct blob_attr_info cert_policy[CERT_ATTR_MAX] = {
74 [CERT_ATTR_SIGNATURE] = { .type = BLOB_ATTR_BINARY },
75 [CERT_ATTR_PAYLOAD] = { .type = BLOB_ATTR_NESTED },
76 };
77
78 enum cert_cont_attr {
79 CERT_CT_ATTR_PAYLOAD,
80 CERT_CT_ATTR_MAX
81 };
82
83 static const struct blobmsg_policy cert_cont_policy[CERT_CT_ATTR_MAX] = {
84 [CERT_CT_ATTR_PAYLOAD] = { .name = "ucert", .type = BLOBMSG_TYPE_TABLE },
85 };
86
87 enum cert_payload_attr {
88 CERT_PL_ATTR_CERTTYPE,
89 CERT_PL_ATTR_CERTID,
90 CERT_PL_ATTR_VALIDFROMTIME,
91 CERT_PL_ATTR_EXPIRETIME,
92 CERT_PL_ATTR_PUBKEY,
93 CERT_PL_ATTR_KEY_FINGERPRINT,
94 CERT_PL_ATTR_MAX
95 };
96
97 enum certtype_id {
98 CERTTYPE_UNSPEC,
99 CERTTYPE_AUTH,
100 CERTTYPE_REVOKE
101 };
102
103 static const struct blobmsg_policy cert_payload_policy[CERT_PL_ATTR_MAX] = {
104 [CERT_PL_ATTR_CERTTYPE] = { .name = "certtype", .type = BLOBMSG_TYPE_INT32 },
105 [CERT_PL_ATTR_CERTID] = { .name = "certid", .type = BLOBMSG_TYPE_INT32 },
106 [CERT_PL_ATTR_VALIDFROMTIME] = { .name = "validfrom", .type = BLOBMSG_TYPE_INT64 },
107 [CERT_PL_ATTR_EXPIRETIME] = { .name = "expiresat", .type = BLOBMSG_TYPE_INT64 },
108 [CERT_PL_ATTR_PUBKEY] = { .name = "pubkey", .type = BLOBMSG_TYPE_STRING },
109 [CERT_PL_ATTR_KEY_FINGERPRINT] = { .name = "fingerprint", .type = BLOBMSG_TYPE_STRING },
110 };
111
112 /* list to store certificate chain at runtime */
113 struct cert_object {
114 struct list_head list;
115 struct blob_attr *cert[CERT_ATTR_MAX];
116 };
117
118 /* write buffer to file */
119 static int write_file(const char *filename, void *buf, size_t len, bool append) {
120 FILE *f;
121 size_t outlen;
122
123 f = fopen(filename, append?"a":"w");
124 if (!f)
125 return 1;
126
127 outlen = fwrite(buf, 1, len, f);
128 fclose(f);
129 return (outlen == len);
130 }
131
132 /* load certfile into list */
133 static int cert_load(const char *certfile, struct list_head *chain) {
134 FILE *f;
135 struct blob_attr *certtb[CERT_ATTR_MAX];
136 struct blob_attr *bufpt;
137 struct cert_object *cobj;
138 char filebuf[CERT_BUF_LEN];
139 int ret = 0, pret = 0;
140 size_t len, pos = 0;
141
142 f = fopen(certfile, "r");
143 if (!f)
144 return 1;
145
146 len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
147 if (len < 64)
148 return 1;
149
150 ret = ferror(f) || !feof(f);
151 fclose(f);
152 if (ret)
153 return 1;
154
155 bufpt = (struct blob_attr *)filebuf;
156 do {
157 pret = blob_parse(bufpt, certtb, cert_policy, CERT_ATTR_MAX);
158 if (pret <= 0)
159 /* no attributes found */
160 break;
161
162 if (pos + blob_pad_len(bufpt) > len)
163 /* blob exceeds filebuffer */
164 break;
165 else
166 pos += blob_pad_len(bufpt);
167
168 if (!certtb[CERT_ATTR_SIGNATURE])
169 /* no signature -> drop */
170 break;
171
172 cobj = calloc(1, sizeof(*cobj));
173 cobj->cert[CERT_ATTR_SIGNATURE] = blob_memdup(certtb[CERT_ATTR_SIGNATURE]);
174 if (certtb[CERT_ATTR_PAYLOAD])
175 cobj->cert[CERT_ATTR_PAYLOAD] = blob_memdup(certtb[CERT_ATTR_PAYLOAD]);
176
177 list_add_tail(&cobj->list, chain);
178 ret += pret;
179 /* repeat parsing while there is still enough remaining data in buffer */
180 } while(len > pos + sizeof(struct blob_attr) && (bufpt = blob_next(bufpt)));
181
182 return (ret <= 0);
183 }
184
185 #ifdef UCERT_FULL
186 /* append signature to certfile */
187 static int cert_append(const char *certfile, const char *sigfile) {
188 FILE *fs;
189 char filebuf[CERT_BUF_LEN];
190 struct blob_buf sigbuf = {0};
191 int len;
192 int ret;
193
194 fs = fopen(sigfile, "r");
195 if (!fs)
196 return 1;
197
198 len = fread(&filebuf, 1, CERT_BUF_LEN - 1, fs);
199 ret = ferror(fs) || !feof(fs) || (len < 64);
200 fclose(fs);
201 if (ret)
202 return 1;
203
204 blob_buf_init(&sigbuf, 0);
205 blob_put(&sigbuf, CERT_ATTR_SIGNATURE, filebuf, len);
206 ret = write_file(certfile, sigbuf.head, blob_raw_len(sigbuf.head), true);
207 blob_buf_free(&sigbuf);
208 return ret;
209 }
210 #endif
211
212 /* verify the signature of a single chain element */
213 static int cert_verify_blob(struct blob_attr *cert[CERT_ATTR_MAX],
214 const char *pubkeyfile, const char *pubkeydir) {
215 int i;
216 char msgfname[256], sigfname[256];
217 int ret;
218 char tmpdir[] = "/tmp/ucert-XXXXXX";
219
220 if (mkdtemp(tmpdir) == NULL)
221 return errno;
222
223 snprintf(msgfname, sizeof(msgfname) - 1, "%s/%s", tmpdir, "payload");
224 snprintf(sigfname, sizeof(sigfname) - 1, "%s/%s", tmpdir, "payload.sig");
225
226 for (i = 0; i < CERT_ATTR_MAX; i++) {
227 struct blob_attr *v = cert[i];
228
229 if (!v)
230 break;
231
232 switch(cert_policy[i].type) {
233 case BLOB_ATTR_BINARY:
234 write_file(sigfname, blob_data(v), blob_len(v), false);
235 break;
236 case BLOB_ATTR_NESTED:
237 write_file(msgfname, blob_data(v), blob_len(v), false);
238 break;
239 }
240 }
241
242 ret = usign_v(msgfname, pubkeyfile, pubkeydir, sigfname, quiet);
243
244 unlink(msgfname);
245 unlink(sigfname);
246 rmdir(tmpdir);
247
248 return ret;
249 }
250
251 /* verify cert chain (and message) */
252 static int chain_verify(const char *msgfile, const char *pubkeyfile,
253 const char *pubkeydir, struct list_head *chain) {
254 struct cert_object *cobj;
255 struct blob_attr *containertb[CERT_CT_ATTR_MAX];
256 struct blob_attr *payloadtb[CERT_PL_ATTR_MAX];
257 char tmpdir[] = "/tmp/ucert-XXXXXX";
258 char chainedpubkey[256] = {0};
259 char chainedfp[17] = {0};
260 char extsigfile[256] = {0};
261 int ret = 1;
262 int checkmsg = 0;
263 struct timeval tv;
264
265 if (mkdtemp(tmpdir) == NULL)
266 return errno;
267
268 if (msgfile)
269 checkmsg = -1;
270
271 gettimeofday(&tv, NULL);
272
273 list_for_each_entry(cobj, chain, list) {
274 /* blob has payload, verify that using signature */
275 if (cobj->cert[CERT_ATTR_PAYLOAD]) {
276 time_t validfrom;
277 time_t expiresat;
278 uint32_t certtype;
279
280 ret = cert_verify_blob(cobj->cert, chainedpubkey[0]?chainedpubkey:pubkeyfile, pubkeydir);
281 if (ret)
282 goto clean_and_return;
283
284 blobmsg_parse(cert_cont_policy,
285 ARRAY_SIZE(cert_cont_policy),
286 containertb,
287 blob_data(cobj->cert[CERT_ATTR_PAYLOAD]),
288 blob_len(cobj->cert[CERT_ATTR_PAYLOAD]));
289 if (!containertb[CERT_CT_ATTR_PAYLOAD]) {
290 ret = 1;
291 DPRINTF("no ucert in signed payload\n");
292 goto clean_and_return;
293 }
294 blobmsg_parse(cert_payload_policy,
295 ARRAY_SIZE(cert_payload_policy),
296 payloadtb,
297 blobmsg_data(containertb[CERT_CT_ATTR_PAYLOAD]),
298 blobmsg_data_len(containertb[CERT_CT_ATTR_PAYLOAD]));
299
300 if (!payloadtb[CERT_PL_ATTR_CERTTYPE] ||
301 !payloadtb[CERT_PL_ATTR_VALIDFROMTIME] ||
302 !payloadtb[CERT_PL_ATTR_EXPIRETIME] ||
303 !payloadtb[CERT_PL_ATTR_PUBKEY]) {
304 ret = 1;
305 DPRINTF("missing mandatory ucert attributes\n");
306 goto clean_and_return;
307 }
308 certtype = blobmsg_get_u32(payloadtb[CERT_PL_ATTR_CERTTYPE]);
309 validfrom = blobmsg_get_u64(payloadtb[CERT_PL_ATTR_VALIDFROMTIME]);
310 expiresat = blobmsg_get_u64(payloadtb[CERT_PL_ATTR_EXPIRETIME]);
311
312 if (certtype != CERTTYPE_AUTH) {
313 ret = 2;
314 DPRINTF("wrong certificate type\n");
315 goto clean_and_return;
316 }
317
318 if (tv.tv_sec < validfrom ||
319 tv.tv_sec >= expiresat) {
320 ret = 3;
321 DPRINTF("certificate expired\n");
322 goto clean_and_return;
323 }
324
325 snprintf(chainedpubkey, sizeof(chainedpubkey) - 1, "%s/%s", tmpdir, "chained-pubkey");
326 write_file(chainedpubkey,
327 blobmsg_data(payloadtb[CERT_PL_ATTR_PUBKEY]),
328 blobmsg_data_len(payloadtb[CERT_PL_ATTR_PUBKEY]),
329 false);
330
331 if (usign_f_pubkey(chainedfp, chainedpubkey)) {
332 DPRINTF("cannot get fingerprint for chained key\n");
333 ret = 2;
334 goto clean_and_return;
335 }
336 if (pubkeydir && _usign_key_is_revoked(chainedfp, pubkeydir)) {
337 DPRINTF("key %s has been revoked!\n", chainedfp);
338 ret = 4;
339 goto clean_and_return;
340 }
341 } else {
342 /* blob doesn't have payload, verify message using signature */
343 if (msgfile) {
344 snprintf(extsigfile, sizeof(extsigfile) - 1, "%s/%s", tmpdir, "ext-sig");
345 write_file(extsigfile,
346 blob_data(cobj->cert[CERT_ATTR_SIGNATURE]),
347 blob_len(cobj->cert[CERT_ATTR_SIGNATURE]),
348 false);
349 checkmsg = ret = usign_v(msgfile,
350 chainedpubkey[0]?chainedpubkey:pubkeyfile,
351 pubkeydir, extsigfile, quiet);
352 unlink(extsigfile);
353 } else {
354 DPRINTF("stray trailing signature without anything to verify!\n");
355 ret = 1;
356 };
357 }
358 }
359
360 if (checkmsg == -1)
361 DPRINTF("missing signature to verify message!\n");
362
363 clean_and_return:
364 if (chainedpubkey[0])
365 unlink(chainedpubkey);
366 rmdir(tmpdir);
367 return ret | checkmsg;
368 }
369
370 #ifdef UCERT_FULL
371 /* dump single chain element to console */
372 static void cert_dump_blob(struct blob_attr *cert[CERT_ATTR_MAX]) {
373 int i;
374
375 for (i = 0; i < CERT_ATTR_MAX; i++) {
376 struct blob_attr *v = cert[i];
377
378 if (!v)
379 continue;
380
381 switch(cert_policy[i].type) {
382 case BLOB_ATTR_BINARY:
383 fprintf(stdout, "signature:\n---\n%s---\n", (char *) blob_data(v));
384 break;
385 case BLOB_ATTR_NESTED:
386 fprintf(stdout, "payload:\n---\n%s\n---\n", blobmsg_format_json_indent(blob_data(v), false, 0));
387 break;
388 }
389 }
390 }
391
392 /* dump certfile to console */
393 static int cert_dump(const char *certfile) {
394 struct cert_object *cobj;
395 static LIST_HEAD(certchain);
396 unsigned int count = 0;
397
398 if (cert_load(certfile, &certchain)) {
399 DPRINTF("cannot parse cert\n");
400 return 1;
401 }
402
403 list_for_each_entry(cobj, &certchain, list) {
404 fprintf(stdout, "=== CHAIN ELEMENT %02u ===\n", ++count);
405 cert_dump_blob(cobj->cert);
406 }
407
408 return 0;
409 }
410
411 /* issue an auth certificate for pubkey */
412 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
413 struct blob_buf payloadbuf = {0};
414 struct blob_buf certbuf = {0};
415 struct timeval tv;
416 int pklen, siglen;
417 int revoker = 1;
418 void *c;
419 FILE *pkf, *sigf;
420 char pkb[512];
421 char sigb[1024];
422 char fname[256], sfname[256];
423 char pkfp[17];
424 char tmpdir[] = "/tmp/ucert-XXXXXX";
425
426 pkf = fopen(pubkeyfile, "r");
427 if (!pkf)
428 return -1;
429
430 pklen = fread(pkb, 1, 512, pkf);
431 pkb[pklen] = '\0';
432
433 if (pklen < 32)
434 return -1;
435
436 fclose(pkf);
437
438 if (usign_f_pubkey(pkfp, pubkeyfile))
439 return -1;
440
441 gettimeofday(&tv, NULL);
442
443 if (mkdtemp(tmpdir) == NULL)
444 return errno;
445
446 while (revoker >= 0) {
447 blob_buf_init(&payloadbuf, 0);
448 c = blobmsg_open_table(&payloadbuf, "ucert");
449 blobmsg_add_u32(&payloadbuf, "certtype", revoker?CERTTYPE_REVOKE:CERTTYPE_AUTH);
450 blobmsg_add_u64(&payloadbuf, "validfrom", tv.tv_sec);
451 if (!revoker) {
452 blobmsg_add_u64(&payloadbuf, "expiresat", tv.tv_sec + 60 * 60 * 24 * 365);
453 blobmsg_add_string(&payloadbuf, "pubkey", pkb);
454 } else {
455 blobmsg_add_string(&payloadbuf, "fingerprint", pkfp);
456 }
457
458 blobmsg_close_table(&payloadbuf, c);
459
460 snprintf(fname, sizeof(fname) - 1, "%s/%s", tmpdir, revoker?"revoker":"payload");
461 write_file(fname, blob_data(payloadbuf.head), blob_len(payloadbuf.head), false);
462
463 snprintf(sfname, sizeof(sfname) - 1, "%s/%s", tmpdir, revoker?"revoker.sig":"payload.sig");
464 if (usign_s(fname, seckeyfile, sfname, quiet))
465 return 1;
466
467 sigf = fopen(sfname, "r");
468 if (!sigf)
469 return 1;
470
471 siglen = fread(sigb, 1, 1024, sigf);
472 if (siglen < 1)
473 return 1;
474
475 sigb[siglen] = '\0';
476 fclose(sigf);
477
478 unlink(fname);
479 unlink(sfname);
480
481 blob_buf_init(&certbuf, 0);
482 blob_put(&certbuf, CERT_ATTR_SIGNATURE, sigb, siglen);
483 blob_put(&certbuf, CERT_ATTR_PAYLOAD, blob_data(payloadbuf.head), blob_len(payloadbuf.head));
484 snprintf(fname, sizeof(fname) - 1, "%s%s", certfile, revoker?".revoke":"");
485 write_file(fname, certbuf.head, blob_raw_len(certbuf.head), true);
486 blob_buf_free(&certbuf);
487 blob_buf_free(&payloadbuf);
488
489 revoker--;
490 }
491
492 rmdir(tmpdir);
493
494 return 0;
495 }
496 #endif
497
498 /* process revoker certificate */
499 static int cert_process_revoker(const char *certfile, const char *pubkeydir) {
500 static LIST_HEAD(certchain);
501 struct cert_object *cobj;
502 struct blob_attr *containertb[CERT_CT_ATTR_MAX];
503 struct blob_attr *payloadtb[CERT_PL_ATTR_MAX];
504 struct stat st;
505 struct timeval tv;
506 time_t validfrom;
507 enum certtype_id certtype;
508 char *fingerprint;
509 char rfname[512];
510
511 int ret = -1;
512
513 if (cert_load(certfile, &certchain)) {
514 DPRINTF("cannot parse cert\n");
515 return 1;
516 }
517
518 gettimeofday(&tv, NULL);
519
520 list_for_each_entry(cobj, &certchain, list) {
521 if (!cobj->cert[CERT_ATTR_PAYLOAD])
522 return 2;
523
524 /* blob has payload, verify that using signature */
525 ret = cert_verify_blob(cobj->cert, NULL, pubkeydir);
526 if (ret)
527 return ret;
528
529 blobmsg_parse(cert_cont_policy,
530 ARRAY_SIZE(cert_cont_policy),
531 containertb,
532 blob_data(cobj->cert[CERT_ATTR_PAYLOAD]),
533 blob_len(cobj->cert[CERT_ATTR_PAYLOAD]));
534 if (!containertb[CERT_CT_ATTR_PAYLOAD]) {
535 DPRINTF("no ucert in signed payload\n");
536 return 2;
537 }
538
539 blobmsg_parse(cert_payload_policy,
540 ARRAY_SIZE(cert_payload_policy),
541 payloadtb,
542 blobmsg_data(containertb[CERT_CT_ATTR_PAYLOAD]),
543 blobmsg_data_len(containertb[CERT_CT_ATTR_PAYLOAD]));
544
545 if (!payloadtb[CERT_PL_ATTR_CERTTYPE] ||
546 !payloadtb[CERT_PL_ATTR_VALIDFROMTIME] ||
547 !payloadtb[CERT_PL_ATTR_KEY_FINGERPRINT]) {
548 DPRINTF("missing mandatory ucert attributes\n");
549 return 2;
550 }
551
552 certtype = blobmsg_get_u32(payloadtb[CERT_PL_ATTR_CERTTYPE]);
553 validfrom = blobmsg_get_u64(payloadtb[CERT_PL_ATTR_VALIDFROMTIME]);
554 fingerprint = blobmsg_get_string(payloadtb[CERT_PL_ATTR_KEY_FINGERPRINT]);
555
556 if (certtype != CERTTYPE_REVOKE) {
557 DPRINTF("wrong certificate type\n");
558 return 2;
559 }
560
561 if (tv.tv_sec < validfrom) {
562 return 3;
563 }
564
565 snprintf(rfname, sizeof(rfname)-1, "%s/%s", pubkeydir, fingerprint);
566 /* check if entry in pubkeydir exists */
567 if (stat(rfname, &st) == 0) {
568 if (_usign_key_is_revoked(fingerprint, pubkeydir)) {
569 DPRINTF("existing revoker deadlink for key %s\n", fingerprint);
570 continue;
571 };
572
573 /* remove any other entry */
574 if (unlink(rfname))
575 return -1;
576 }
577
578 ret = symlink(".revoked.", rfname);
579 if (ret)
580 return ret;
581
582 DPRINTF("created revoker deadlink for key %s\n", fingerprint);
583 };
584
585 return ret;
586 }
587
588 /* load and verify certfile (and message) */
589 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
590 static LIST_HEAD(certchain);
591
592 if (cert_load(certfile, &certchain)) {
593 DPRINTF("cannot parse cert\n");
594 return 1;
595 }
596
597 return chain_verify(msgfile, pubkeyfile, pubkeydir, &certchain);
598 }
599
600 /* output help */
601 static int usage(const char *cmd)
602 {
603 #ifndef UCERT_STRIP_MESSAGES
604 fprintf(stderr,
605 "Usage: %s <command> <options>\n"
606 "Commands:\n"
607 #ifdef UCERT_FULL
608 " -A: append signature (needs -c and -x)\n"
609 " -D: dump (needs -c)\n"
610 " -I: issue cert and revoker (needs -c and -p and -s)\n"
611 #endif /* UCERT_FULL */
612 " -R: process revoker certificate (needs -c and -P)\n"
613 " -V: verify (needs -c and -p|-P, may have -m)\n"
614 "Options:\n"
615 " -c <file>: certificate file\n"
616 " -m <file>: message file (verify only)\n"
617 " -p <file>: public key file\n"
618 " -P <path>: public key directory (verify only)\n"
619 " -q: quiet (do not print verification result, use return code only)\n"
620 #ifdef UCERT_FULL
621 " -s <file>: secret key file (issue only)\n"
622 " -x <file>: signature file (append only)\n"
623 #endif /* UCERT_FULL */
624 "\n",
625 cmd);
626 #endif /* UCERT_STRIP_MESSAGES */
627 return 1;
628 }
629
630 /* parse command line options and call functions */
631 int main(int argc, char *argv[]) {
632 int ch;
633 const char *msgfile = NULL;
634 const char *pubkeyfile = NULL;
635 const char *pubkeydir = NULL;
636 const char *certfile = NULL;
637 #ifdef UCERT_FULL
638 const char *sigfile = NULL;
639 const char *seckeyfile = NULL;
640 #endif
641
642 quiet = false;
643 while ((ch = getopt(argc, argv,
644 "RVc:m:p:P:q"
645 #ifdef UCERT_FULL
646 "ADIs:x:"
647 #endif
648 )) != -1) {
649 switch (ch) {
650 #ifdef UCERT_FULL
651 case 'A':
652 if (cmd != CMD_NONE)
653 return usage(argv[0]);
654 cmd = CMD_APPEND;
655 break;
656 case 'D':
657 if (cmd != CMD_NONE)
658 return usage(argv[0]);
659 cmd = CMD_DUMP;
660 break;
661 case 'I':
662 if (cmd != CMD_NONE)
663 return usage(argv[0]);
664 cmd = CMD_ISSUE;
665 break;
666 #endif
667 case 'R':
668 if (cmd != CMD_NONE)
669 return usage(argv[0]);
670 cmd = CMD_REVOKE;
671 break;
672 case 'V':
673 if (cmd != CMD_NONE)
674 return usage(argv[0]);
675 cmd = CMD_VERIFY;
676 break;
677 case 'c':
678 if (certfile || cmd == CMD_NONE)
679 return usage(argv[0]);
680 certfile = optarg;
681 break;
682 case 'm':
683 if (msgfile || cmd != CMD_VERIFY)
684 return usage(argv[0]);
685 msgfile = optarg;
686 break;
687 case 'p':
688 if (pubkeyfile || (cmd != CMD_VERIFY && cmd != CMD_ISSUE) || cmd == CMD_NONE)
689 return usage(argv[0]);
690 pubkeyfile = optarg;
691 break;
692 case 'P':
693 if (pubkeydir || (cmd != CMD_VERIFY && cmd != CMD_REVOKE) || cmd == CMD_NONE)
694 return usage(argv[0]);
695 pubkeydir = optarg;
696 break;
697 case 'q':
698 if (quiet)
699 return usage(argv[0]);
700 quiet = true;
701 break;
702 #ifdef UCERT_FULL
703 case 's':
704 if (seckeyfile || cmd != CMD_ISSUE || cmd == CMD_NONE)
705 return usage(argv[0]);
706 seckeyfile = optarg;
707 break;
708 case 'x':
709 if (sigfile || cmd != CMD_APPEND || cmd == CMD_NONE)
710 return usage(argv[0]);
711 sigfile = optarg;
712 break;
713 #endif
714 default:
715 return usage(argv[0]);
716 }
717 }
718
719 switch (cmd) {
720 #ifdef UCERT_FULL
721 case CMD_APPEND:
722 if (certfile && sigfile)
723 return cert_append(certfile, sigfile);
724 else
725 return usage(argv[0]);
726 case CMD_DUMP:
727 if (certfile)
728 return cert_dump(certfile);
729 else
730 return usage(argv[0]);
731 case CMD_ISSUE:
732 if (certfile && pubkeyfile && seckeyfile)
733 return cert_issue(certfile, pubkeyfile, seckeyfile);
734 else
735 return usage(argv[0]);
736 #endif
737 case CMD_REVOKE:
738 if (certfile && pubkeydir)
739 return cert_process_revoker(certfile, pubkeydir);
740 else
741 return usage(argv[0]);
742 case CMD_VERIFY:
743 if (certfile && (pubkeyfile || pubkeydir))
744 return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
745 else
746 return usage(argv[0]);
747 default:
748 return usage(argv[0]);
749 }
750
751 /* unreachable */
752 return usage(argv[0]);
753 }