• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucert/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_untrusted(bufpt, len, 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         char *json = NULL;
375 
376         for (i = 0; i < CERT_ATTR_MAX; i++) {
377                 struct blob_attr *v = cert[i];
378 
379                 if (!v)
380                         continue;
381 
382                 switch(cert_policy[i].type) {
383                 case BLOB_ATTR_BINARY:
384                         fprintf(stdout, "signature:\n---\n%s---\n", (char *) blob_data(v));
385                         break;
386                 case BLOB_ATTR_NESTED:
387                         json = blobmsg_format_json_indent(blob_data(v), false, 0);
388                         if (!json) {
389                                 DPRINTF("cannot parse payload\n");
390                                 continue;
391                         }
392                         fprintf(stdout, "payload:\n---\n%s\n---\n", json);
393                         free(json);
394                         break;
395                 }
396         }
397 }
398 
399 /* dump certfile to console */
400 static int cert_dump(const char *certfile) {
401         struct cert_object *cobj;
402         static LIST_HEAD(certchain);
403         unsigned int count = 0;
404 
405         if (cert_load(certfile, &certchain)) {
406                 DPRINTF("cannot parse cert\n");
407                 return 1;
408         }
409 
410         list_for_each_entry(cobj, &certchain, list) {
411                 fprintf(stdout, "=== CHAIN ELEMENT %02u ===\n", ++count);
412                 cert_dump_blob(cobj->cert);
413         }
414 
415         return 0;
416 }
417 
418 /* issue an auth certificate for pubkey */
419 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
420         struct blob_buf payloadbuf = {0};
421         struct blob_buf certbuf = {0};
422         struct timeval tv;
423         int pklen, siglen;
424         int revoker = 1;
425         void *c;
426         FILE *pkf, *sigf;
427         char pkb[512];
428         char sigb[1024];
429         char fname[256], sfname[256];
430         char pkfp[17];
431         char tmpdir[] = "/tmp/ucert-XXXXXX";
432 
433         pkf = fopen(pubkeyfile, "r");
434         if (!pkf)
435                 return -1;
436 
437         pklen = fread(pkb, 1, 512, pkf);
438         pkb[pklen] = '\0';
439 
440         if (pklen < 32)
441                 return -1;
442 
443         fclose(pkf);
444 
445         if (usign_f_pubkey(pkfp, pubkeyfile))
446                 return -1;
447 
448         gettimeofday(&tv, NULL);
449 
450         if (mkdtemp(tmpdir) == NULL)
451                 return errno;
452 
453         while (revoker >= 0) {
454                 blob_buf_init(&payloadbuf, 0);
455                 c = blobmsg_open_table(&payloadbuf, "ucert");
456                 blobmsg_add_u32(&payloadbuf, "certtype", revoker?CERTTYPE_REVOKE:CERTTYPE_AUTH);
457                 blobmsg_add_u64(&payloadbuf, "validfrom", tv.tv_sec);
458                 if (!revoker) {
459                         blobmsg_add_u64(&payloadbuf, "expiresat", tv.tv_sec + 60 * 60 * 24 * 365);
460                         blobmsg_add_string(&payloadbuf, "pubkey", pkb);
461                 } else {
462                         blobmsg_add_string(&payloadbuf, "fingerprint", pkfp);
463                 }
464 
465                 blobmsg_close_table(&payloadbuf, c);
466 
467                 snprintf(fname, sizeof(fname) - 1, "%s/%s", tmpdir, revoker?"revoker":"payload");
468                 write_file(fname, blob_data(payloadbuf.head), blob_len(payloadbuf.head), false);
469 
470                 snprintf(sfname, sizeof(sfname) - 1, "%s/%s", tmpdir, revoker?"revoker.sig":"payload.sig");
471                 if (usign_s(fname, seckeyfile, sfname, quiet))
472                         return 1;
473 
474                 sigf = fopen(sfname, "r");
475                 if (!sigf)
476                         return 1;
477 
478                 siglen = fread(sigb, 1, 1024, sigf);
479                 if (siglen < 1)
480                         return 1;
481 
482                 sigb[siglen] = '\0';
483                 fclose(sigf);
484 
485                 unlink(fname);
486                 unlink(sfname);
487 
488                 blob_buf_init(&certbuf, 0);
489                 blob_put(&certbuf, CERT_ATTR_SIGNATURE, sigb, siglen);
490                 blob_put(&certbuf, CERT_ATTR_PAYLOAD, blob_data(payloadbuf.head), blob_len(payloadbuf.head));
491                 snprintf(fname, sizeof(fname) - 1, "%s%s", certfile, revoker?".revoke":"");
492                 write_file(fname, certbuf.head, blob_raw_len(certbuf.head), true);
493                 blob_buf_free(&certbuf);
494                 blob_buf_free(&payloadbuf);
495 
496                 revoker--;
497         }
498 
499         rmdir(tmpdir);
500 
501         return 0;
502 }
503 #endif
504 
505 /* process revoker certificate */
506 static int cert_process_revoker(const char *certfile, const char *pubkeydir) {
507         static LIST_HEAD(certchain);
508         struct cert_object *cobj;
509         struct blob_attr *containertb[CERT_CT_ATTR_MAX];
510         struct blob_attr *payloadtb[CERT_PL_ATTR_MAX];
511         struct stat st;
512         struct timeval tv;
513         time_t validfrom;
514         enum certtype_id certtype;
515         char *fingerprint;
516         char rfname[512];
517 
518         int ret = -1;
519 
520         if (cert_load(certfile, &certchain)) {
521                 DPRINTF("cannot parse cert\n");
522                 return 1;
523         }
524 
525         gettimeofday(&tv, NULL);
526 
527         list_for_each_entry(cobj, &certchain, list) {
528                 if (!cobj->cert[CERT_ATTR_PAYLOAD])
529                         return 2;
530 
531                 /* blob has payload, verify that using signature */
532                 ret = cert_verify_blob(cobj->cert, NULL, pubkeydir);
533                 if (ret)
534                         return ret;
535 
536                 blobmsg_parse(cert_cont_policy,
537                               ARRAY_SIZE(cert_cont_policy),
538                               containertb,
539                               blob_data(cobj->cert[CERT_ATTR_PAYLOAD]),
540                               blob_len(cobj->cert[CERT_ATTR_PAYLOAD]));
541                 if (!containertb[CERT_CT_ATTR_PAYLOAD]) {
542                         DPRINTF("no ucert in signed payload\n");
543                         return 2;
544                 }
545 
546                 blobmsg_parse(cert_payload_policy,
547                               ARRAY_SIZE(cert_payload_policy),
548                               payloadtb,
549                               blobmsg_data(containertb[CERT_CT_ATTR_PAYLOAD]),
550                               blobmsg_data_len(containertb[CERT_CT_ATTR_PAYLOAD]));
551 
552                 if (!payloadtb[CERT_PL_ATTR_CERTTYPE] ||
553                     !payloadtb[CERT_PL_ATTR_VALIDFROMTIME] ||
554                     !payloadtb[CERT_PL_ATTR_KEY_FINGERPRINT]) {
555                         DPRINTF("missing mandatory ucert attributes\n");
556                         return 2;
557                 }
558 
559                 certtype = blobmsg_get_u32(payloadtb[CERT_PL_ATTR_CERTTYPE]);
560                 validfrom = blobmsg_get_u64(payloadtb[CERT_PL_ATTR_VALIDFROMTIME]);
561                 fingerprint = blobmsg_get_string(payloadtb[CERT_PL_ATTR_KEY_FINGERPRINT]);
562 
563                 if (certtype != CERTTYPE_REVOKE) {
564                         DPRINTF("wrong certificate type\n");
565                         return 2;
566                 }
567 
568                 if (tv.tv_sec < validfrom) {
569                         return 3;
570                 }
571 
572                 snprintf(rfname, sizeof(rfname)-1, "%s/%s", pubkeydir, fingerprint);
573                 /* check if entry in pubkeydir exists */
574                 if (stat(rfname, &st) == 0) {
575                         if (_usign_key_is_revoked(fingerprint, pubkeydir)) {
576                                 DPRINTF("existing revoker deadlink for key %s\n", fingerprint);
577                                 continue;
578                         };
579 
580                         /* remove any other entry */
581                         if (unlink(rfname))
582                                 return -1;
583                 }
584 
585                 ret = symlink(".revoked.", rfname);
586                 if (ret)
587                         return ret;
588 
589                 DPRINTF("created revoker deadlink for key %s\n", fingerprint);
590         };
591 
592         return ret;
593 }
594 
595 /* load and verify certfile (and message) */
596 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
597         static LIST_HEAD(certchain);
598 
599         if (cert_load(certfile, &certchain)) {
600                 DPRINTF("cannot parse cert\n");
601                 return 1;
602         }
603 
604         return chain_verify(msgfile, pubkeyfile, pubkeydir, &certchain);
605 }
606 
607 /* output help */
608 static int usage(const char *cmd)
609 {
610 #ifndef UCERT_STRIP_MESSAGES
611         fprintf(stderr,
612                 "Usage: %s <command> <options>\n"
613                 "Commands:\n"
614 #ifdef UCERT_FULL
615                 "  -A:                  append signature (needs -c and -x)\n"
616                 "  -D:                  dump (needs -c)\n"
617                 "  -I:                  issue cert and revoker (needs -c and -p and -s)\n"
618 #endif /* UCERT_FULL */
619                 "  -R:                  process revoker certificate (needs -c and -P)\n"
620                 "  -V:                  verify (needs -c and -p|-P, may have -m)\n"
621                 "Options:\n"
622                 "  -c <file>:           certificate file\n"
623                 "  -m <file>:           message file (verify only)\n"
624                 "  -p <file>:           public key file\n"
625                 "  -P <path>:           public key directory (verify only)\n"
626                 "  -q:                  quiet (do not print verification result, use return code only)\n"
627 #ifdef UCERT_FULL
628                 "  -s <file>:           secret key file (issue only)\n"
629                 "  -x <file>:           signature file (append only)\n"
630 #endif /* UCERT_FULL */
631                 "\n",
632                 cmd);
633 #endif /* UCERT_STRIP_MESSAGES */
634         return 1;
635 }
636 
637 /* parse command line options and call functions */
638 int main(int argc, char *argv[]) {
639         int ch;
640         const char *msgfile = NULL;
641         const char *pubkeyfile = NULL;
642         const char *pubkeydir = NULL;
643         const char *certfile = NULL;
644 #ifdef UCERT_FULL
645         const char *sigfile = NULL;
646         const char *seckeyfile = NULL;
647 #endif
648 
649         quiet = false;
650         while ((ch = getopt(argc, argv,
651                 "RVc:m:p:P:q"
652 #ifdef UCERT_FULL
653                 "ADIs:x:"
654 #endif
655                )) != -1) {
656                 switch (ch) {
657 #ifdef UCERT_FULL
658                 case 'A':
659                         if (cmd != CMD_NONE)
660                                 return usage(argv[0]);
661                         cmd = CMD_APPEND;
662                         break;
663                 case 'D':
664                         if (cmd != CMD_NONE)
665                                 return usage(argv[0]);
666                         cmd = CMD_DUMP;
667                         break;
668                 case 'I':
669                         if (cmd != CMD_NONE)
670                                 return usage(argv[0]);
671                         cmd = CMD_ISSUE;
672                         break;
673 #endif
674                 case 'R':
675                         if (cmd != CMD_NONE)
676                                 return usage(argv[0]);
677                         cmd = CMD_REVOKE;
678                         break;
679                 case 'V':
680                         if (cmd != CMD_NONE)
681                                 return usage(argv[0]);
682                         cmd = CMD_VERIFY;
683                         break;
684                 case 'c':
685                         if (certfile || cmd == CMD_NONE)
686                                 return usage(argv[0]);
687                         certfile = optarg;
688                         break;
689                 case 'm':
690                         if (msgfile || cmd != CMD_VERIFY)
691                                 return usage(argv[0]);
692                         msgfile = optarg;
693                         break;
694                 case 'p':
695                         if (pubkeyfile || (cmd != CMD_VERIFY && cmd != CMD_ISSUE) || cmd == CMD_NONE)
696                                 return usage(argv[0]);
697                         pubkeyfile = optarg;
698                         break;
699                 case 'P':
700                         if (pubkeydir || (cmd != CMD_VERIFY && cmd != CMD_REVOKE) || cmd == CMD_NONE)
701                                 return usage(argv[0]);
702                         pubkeydir = optarg;
703                         break;
704                 case 'q':
705                         if (quiet)
706                                 return usage(argv[0]);
707                         quiet = true;
708                         break;
709 #ifdef UCERT_FULL
710                 case 's':
711                         if (seckeyfile || cmd != CMD_ISSUE || cmd == CMD_NONE)
712                                 return usage(argv[0]);
713                         seckeyfile = optarg;
714                         break;
715                 case 'x':
716                         if (sigfile || cmd != CMD_APPEND || cmd == CMD_NONE)
717                                 return usage(argv[0]);
718                         sigfile = optarg;
719                         break;
720 #endif
721                 default:
722                         return usage(argv[0]);
723                 }
724         }
725 
726         switch (cmd) {
727 #ifdef UCERT_FULL
728         case CMD_APPEND:
729                 if (certfile && sigfile)
730                         return cert_append(certfile, sigfile);
731                 else
732                         return usage(argv[0]);
733         case CMD_DUMP:
734                 if (certfile)
735                         return cert_dump(certfile);
736                 else
737                         return usage(argv[0]);
738         case CMD_ISSUE:
739                 if (certfile && pubkeyfile && seckeyfile)
740                         return cert_issue(certfile, pubkeyfile, seckeyfile);
741                 else
742                         return usage(argv[0]);
743 #endif
744         case CMD_REVOKE:
745                 if (certfile && pubkeydir)
746                         return cert_process_revoker(certfile, pubkeydir);
747                 else
748                         return usage(argv[0]);
749         case CMD_VERIFY:
750                 if (certfile && (pubkeyfile || pubkeydir))
751                         return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
752                 else
753                         return usage(argv[0]);
754         default:
755                 return usage(argv[0]);
756         }
757 
758         /* unreachable */
759         return usage(argv[0]);
760 }
761 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt