add shim executable and CMakeLists
[oweals/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/list.h>
32 #include <libubox/vlist.h>
33 #include <libubox/blobmsg_json.h>
34
35 static enum {
36         CMD_APPEND,
37         CMD_DUMP,
38         CMD_ISSUE,
39         CMD_REVOKE,
40         CMD_VERIFY,
41         CMD_NONE,
42 } cmd = CMD_NONE;
43
44 static bool quiet;
45
46 static int cert_append(const char *certfile, const char *pubkeyfile, const char *sigfile) {
47         fprintf(stderr, "not implemented\n");
48         return 1;
49 }
50
51 static int cert_dump(const char *certfile) {
52         fprintf(stderr, "not implemented\n");
53         return 1;
54 }
55
56 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
57         fprintf(stderr, "not implemented\n");
58         return 1;
59 }
60
61 static int cert_process_revoker(const char *certfile) {
62         fprintf(stderr, "not implemented\n");
63         return 1;
64 }
65
66 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
67         fprintf(stderr, "not implemented\n");
68         return 1;
69 }
70
71 static int usage(const char *cmd)
72 {
73         fprintf(stderr,
74                 "Usage: %s <command> <options>\n"
75                 "Commands:\n"
76                 "  -A:                  append (needs -c and -p and/or -x)\n"
77                 "  -D:                  dump\n"
78                 "  -I:                  issue cert and revoker (needs -c and -p and -s)\n"
79                 "  -R:                  process revoker certificate (needs -c)\n"
80                 "  -V:                  verify (needs -c and -p|-P)\n"
81                 "Options:\n"
82                 "  -c <file>:           certificate file\n"
83                 "  -m <file>:           message file (verify only)\n"
84                 "  -p <file>:           public key file\n"
85                 "  -P <path>:           public key directory (verify only)\n"
86                 "  -q:                  quiet (do not print verification result, use return code only)\n"
87                 "  -s <file>:           secret key file (issue only)\n"
88                 "  -x <file>:           signature file\n"
89                 "\n",
90                 cmd);
91         return 1;
92 }
93
94 int main(int argc, char *argv[]) {
95         int ch;
96         const char *msgfile = NULL;
97         const char *sigfile = NULL;
98         const char *pubkeyfile = NULL;
99         const char *pubkeydir = NULL;
100         const char *certfile = NULL;
101         const char *seckeyfile = NULL;
102
103         quiet = false;
104         while ((ch = getopt(argc, argv, "ADIRVc:m:p:P:qs:x:")) != -1) {
105                 switch (ch) {
106                 case 'A':
107                         cmd = CMD_APPEND;
108                         break;
109                 case 'D':
110                         cmd = CMD_DUMP;
111                         break;
112                 case 'I':
113                         cmd = CMD_ISSUE;
114                         break;
115                 case 'R':
116                         cmd = CMD_REVOKE;
117                         break;
118                 case 'V':
119                         cmd = CMD_VERIFY;
120                         break;
121                 case 'c':
122                         certfile = optarg;
123                         break;
124                 case 'm':
125                         msgfile = optarg;
126                         break;
127                 case 'p':
128                         pubkeyfile = optarg;
129                         break;
130                 case 'P':
131                         pubkeydir = optarg;
132                         break;
133                 case 's':
134                         seckeyfile = optarg;
135                         break;
136                 case 'q':
137                         quiet = true;
138                         break;
139                 case 'x':
140                         sigfile = optarg;
141                         break;
142                 default:
143                         return usage(argv[0]);
144                 }
145         }
146
147         switch (cmd) {
148         case CMD_APPEND:
149                 if (certfile && (pubkeyfile || sigfile))
150                         return cert_append(certfile, pubkeyfile, sigfile);
151                 else
152                         return usage(argv[0]);
153         case CMD_DUMP:
154                 if (certfile)
155                         return cert_dump(certfile);
156                 else
157                         return usage(argv[0]);
158         case CMD_ISSUE:
159                 if (certfile && pubkeyfile && seckeyfile)
160                         return cert_issue(certfile, pubkeyfile, seckeyfile);
161                 else
162                         return usage(argv[0]);
163         case CMD_REVOKE:
164                 if (certfile)
165                         return cert_process_revoker(certfile);
166                 else
167                         return usage(argv[0]);
168         case CMD_VERIFY:
169                 if (certfile && (pubkeyfile || pubkeydir))
170                         return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
171                 else
172                         return usage(argv[0]);
173         case CMD_NONE:
174                 return usage(argv[0]);
175         }
176
177         /* unreachable */
178         return usage(argv[0]);
179 }