implement cert issue
[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/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
51 enum cert_attr {
52         CERT_ATTR_SIGNATURE,
53         CERT_ATTR_PAYLOAD,
54         CERT_ATTR_MAX
55 };
56
57 static const struct blob_attr_info cert_policy[CERT_ATTR_MAX] = {
58         [CERT_ATTR_SIGNATURE] = { .type = BLOB_ATTR_BINARY },
59         [CERT_ATTR_PAYLOAD] = { .type = BLOB_ATTR_NESTED },
60 };
61
62 enum cert_payload_attr {
63         CERT_PL_ATTR_CERTTYPE,
64         CERT_PL_ATTR_CERTID,
65         CERT_PL_ATTR_VALIDFROMTIME,
66         CERT_PL_ATTR_EXPIRETIME,
67         CERT_PL_ATTR_PUBKEY,
68         CERT_PL_ATTR_KEY_FINGERPRINT,
69         CERT_PL_ATTR_MAX
70 };
71
72 enum certtype_id {
73         CERTTYPE_UNSPEC,
74         CERTTYPE_AUTH,
75         CERTTYPE_REVOKE
76 };
77
78 static const struct blobmsg_policy cert_payload_policy[CERT_PL_ATTR_MAX] = {
79         [CERT_PL_ATTR_CERTTYPE] = { .name = "certtype", .type = BLOBMSG_TYPE_INT8 },
80         [CERT_PL_ATTR_CERTID] = { .name = "certid", .type = BLOBMSG_TYPE_INT64 },
81         [CERT_PL_ATTR_VALIDFROMTIME] = { .name = "validfrom", .type = BLOBMSG_TYPE_INT64 },
82         [CERT_PL_ATTR_EXPIRETIME] = { .name = "expiresat", .type = BLOBMSG_TYPE_INT64 },
83         [CERT_PL_ATTR_PUBKEY] = { .name = "pubkey", .type = BLOBMSG_TYPE_STRING },
84         [CERT_PL_ATTR_KEY_FINGERPRINT] = { .name = "fingerprint", .type = BLOBMSG_TYPE_STRING },
85 };
86
87 static int write_file(const char *filename, void *buf, size_t len, bool append) {
88         FILE *f;
89         size_t outlen;
90
91         f = fopen(filename, append?"a":"w");
92         if (!f)
93                 return 1;
94
95         outlen = fwrite(buf, 1, len, f);
96         fclose(f);
97         return (outlen == len);
98 }
99
100 static int cert_load(const char *certfile, struct blob_attr *certtb[]) {
101         FILE *f;
102         int ret = 0;
103         char filebuf[CERT_BUF_LEN];
104         int len;
105
106         f = fopen(certfile, "r");
107         if (!f)
108                 return 1;
109
110         len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
111         ret = ferror(f) || !feof(f);
112         fclose(f);
113         if (ret)
114                 return 1;
115         ret = blob_parse(filebuf, certtb, cert_policy, CERT_ATTR_MAX);
116         fprintf(stderr, "blob_parse return %d\n", ret);
117         return (ret != 0);
118 }
119
120 static int cert_append(const char *certfile, const char *pubkeyfile, const char *sigfile) {
121         fprintf(stderr, "not implemented\n");
122         return 1;
123 }
124
125 static int cert_dump(const char *certfile) {
126         struct blob_attr *certtb[CERT_ATTR_MAX];
127         int i;
128
129         if (cert_load(certfile, certtb)) {
130                 fprintf(stderr, "cannot parse cert\n");
131                 return 1;
132         }
133
134         for (i = 0; i < CERT_ATTR_MAX; i++) {
135                 struct blob_attr *v = certtb[i];
136
137                 if (!v)
138                         continue;
139
140                 switch(cert_policy[i].type) {
141                 case BLOB_ATTR_BINARY:
142                         fprintf(stdout, "signature: %s\n", blob_data(v));
143                         break;
144                 case BLOB_ATTR_NESTED:
145                         fprintf(stdout, "payload:\n%s\n", blobmsg_format_json(blob_data(v), true));
146                         break;
147                 }
148         }
149         return 0;
150 }
151
152 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
153         struct blob_buf certbuf;
154         struct blob_buf payloadbuf;
155         struct timeval tv;
156         struct stat st;
157         int pklen, siglen;
158         int revoker = 1;
159
160         FILE *pkf, *sigf;
161         char pkb[512];
162         char sigb[512];
163         char fname[256], sfname[256];
164         char pkfp[17];
165         char tmpdir[] = "/tmp/ucert-XXXXXX";
166
167         if (stat(certfile, &st) == 0) {
168                 fprintf(stderr, "certfile %s exists, won't overwrite.\n", certfile);
169                 return -1;
170         }
171
172         pkf = fopen(pubkeyfile, "r");
173         if (!pkf)
174                 return -1;
175
176         pklen = fread(pkb, 1, 512, pkf);
177         pkb[pklen - 1] = '\0';
178
179         if (pklen < 32)
180                 return -1;
181
182         fclose(pkf);
183
184         if (usign_f_pubkey(pkfp, pubkeyfile))
185                 return -1;
186
187         gettimeofday(&tv, NULL);
188
189         if (mkdtemp(tmpdir) == NULL)
190                 return errno;
191
192         while (revoker >= 0) {
193                 blob_buf_init(&payloadbuf, 0);
194                 blobmsg_add_u8(&payloadbuf, "certtype", revoker?CERTTYPE_REVOKE:CERTTYPE_AUTH);
195                 blobmsg_add_u64(&payloadbuf, "validfrom", tv.tv_sec);
196                 if (!revoker) {
197                         blobmsg_add_u64(&payloadbuf, "expiresat", tv.tv_sec + 60 * 60 * 24 * 365);
198                         blobmsg_add_string(&payloadbuf, "pubkey", pkb);
199                 } else {
200                         blobmsg_add_string(&payloadbuf, "fingerprint", pkfp);
201                 }
202
203                 snprintf(fname, sizeof(fname) - 1, "%s/%s", tmpdir, revoker?"revoker":"payload");
204                 write_file(fname, blob_data(payloadbuf.head), blob_len(payloadbuf.head), false);
205
206                 snprintf(sfname, sizeof(sfname) - 1, "%s/%s", tmpdir, revoker?"revoker.sig":"payload.sig");
207                 if (usign_s(fname, seckeyfile, sfname, quiet))
208                         return 1;
209
210                 sigf = fopen(sfname, "r");
211                 if (!sigf)
212                         return 1;
213
214                 siglen = fread(sigb, 1, 1024, sigf);
215                 if (siglen < 1)
216                         return 1;
217
218                 sigb[siglen-1] = '\0';
219                 fclose(sigf);
220
221                 unlink(fname);
222                 unlink(sfname);
223
224                 blob_buf_init(&certbuf, 0);
225                 blob_put(&certbuf, CERT_ATTR_SIGNATURE, sigb, siglen);
226                 blob_put(&certbuf, CERT_ATTR_PAYLOAD, blob_data(payloadbuf.head), blob_len(payloadbuf.head));
227                 snprintf(fname, sizeof(fname) - 1, "%s%s", certfile, revoker?".revoke":"");
228                 write_file(fname, blob_data(certbuf.head), blob_len(certbuf.head), false);
229                 revoker--;
230         }
231
232         rmdir(tmpdir);
233
234         return 0;
235 }
236
237 static int cert_process_revoker(const char *certfile) {
238         fprintf(stderr, "not implemented\n");
239         return 1;
240 }
241
242 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
243         fprintf(stderr, "not implemented\n");
244         return 1;
245 }
246
247 static int usage(const char *cmd)
248 {
249         fprintf(stderr,
250                 "Usage: %s <command> <options>\n"
251                 "Commands:\n"
252                 "  -A:                  append (needs -c and -p and/or -x)\n"
253                 "  -D:                  dump\n"
254                 "  -I:                  issue cert and revoker (needs -c and -p and -s)\n"
255                 "  -R:                  process revoker certificate (needs -c)\n"
256                 "  -V:                  verify (needs -c and -p|-P)\n"
257                 "Options:\n"
258                 "  -c <file>:           certificate file\n"
259                 "  -m <file>:           message file (verify only)\n"
260                 "  -p <file>:           public key file\n"
261                 "  -P <path>:           public key directory (verify only)\n"
262                 "  -q:                  quiet (do not print verification result, use return code only)\n"
263                 "  -s <file>:           secret key file (issue only)\n"
264                 "  -x <file>:           signature file\n"
265                 "\n",
266                 cmd);
267         return 1;
268 }
269
270 int main(int argc, char *argv[]) {
271         int ch;
272         const char *msgfile = NULL;
273         const char *sigfile = NULL;
274         const char *pubkeyfile = NULL;
275         const char *pubkeydir = NULL;
276         const char *certfile = NULL;
277         const char *seckeyfile = NULL;
278
279         quiet = false;
280         while ((ch = getopt(argc, argv, "ADIRVc:m:p:P:qs:x:")) != -1) {
281                 switch (ch) {
282                 case 'A':
283                         cmd = CMD_APPEND;
284                         break;
285                 case 'D':
286                         cmd = CMD_DUMP;
287                         break;
288                 case 'I':
289                         cmd = CMD_ISSUE;
290                         break;
291                 case 'R':
292                         cmd = CMD_REVOKE;
293                         break;
294                 case 'V':
295                         cmd = CMD_VERIFY;
296                         break;
297                 case 'c':
298                         certfile = optarg;
299                         break;
300                 case 'm':
301                         msgfile = optarg;
302                         break;
303                 case 'p':
304                         pubkeyfile = optarg;
305                         break;
306                 case 'P':
307                         pubkeydir = optarg;
308                         break;
309                 case 's':
310                         seckeyfile = optarg;
311                         break;
312                 case 'q':
313                         quiet = true;
314                         break;
315                 case 'x':
316                         sigfile = optarg;
317                         break;
318                 default:
319                         return usage(argv[0]);
320                 }
321         }
322
323         switch (cmd) {
324         case CMD_APPEND:
325                 if (certfile && (pubkeyfile || sigfile))
326                         return cert_append(certfile, pubkeyfile, sigfile);
327                 else
328                         return usage(argv[0]);
329         case CMD_DUMP:
330                 if (certfile)
331                         return cert_dump(certfile);
332                 else
333                         return usage(argv[0]);
334         case CMD_ISSUE:
335                 if (certfile && pubkeyfile && seckeyfile)
336                         return cert_issue(certfile, pubkeyfile, seckeyfile);
337                 else
338                         return usage(argv[0]);
339         case CMD_REVOKE:
340                 if (certfile)
341                         return cert_process_revoker(certfile);
342                 else
343                         return usage(argv[0]);
344         case CMD_VERIFY:
345                 if (certfile && (pubkeyfile || pubkeydir))
346                         return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
347                 else
348                         return usage(argv[0]);
349         case CMD_NONE:
350                 return usage(argv[0]);
351         }
352
353         /* unreachable */
354         return usage(argv[0]);
355 }