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