2 * Copyright (C) 2018 Daniel Golle <daniel@makrotopia.org>
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
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.
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>
38 #define CERT_BUF_LEN 4096
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 },
62 enum cert_payload_attr {
63 CERT_PL_ATTR_CERTTYPE,
65 CERT_PL_ATTR_VALIDFROMTIME,
66 CERT_PL_ATTR_EXPIRETIME,
68 CERT_PL_ATTR_KEY_FINGERPRINT,
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 },
88 struct list_head list;
89 struct blob_attr **cert;
92 static int write_file(const char *filename, void *buf, size_t len, bool append) {
96 f = fopen(filename, append?"a":"w");
100 outlen = fwrite(buf, 1, len, f);
102 return (outlen == len);
105 static int cert_load(const char *certfile, struct list_head *chain) {
107 struct blob_attr *certtb[CERT_ATTR_MAX];
108 struct cert_object *cobj;
109 char filebuf[CERT_BUF_LEN];
113 f = fopen(certfile, "r");
117 len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
118 ret = ferror(f) || !feof(f);
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);
127 fprintf(stderr, "blob_parse return %d\n", ret);
131 static int cert_append(const char *certfile, const char *pubkeyfile, const char *sigfile) {
132 fprintf(stderr, "not implemented\n");
136 static void cert_dump_blob(struct blob_attr *cert[CERT_ATTR_MAX]) {
139 for (i = 0; i < CERT_ATTR_MAX; i++) {
140 struct blob_attr *v = cert[i];
145 switch(cert_policy[i].type) {
146 case BLOB_ATTR_BINARY:
147 fprintf(stdout, "signature: %s\n", blob_data(v));
149 case BLOB_ATTR_NESTED:
150 fprintf(stdout, "payload:\n%s\n", blobmsg_format_json(blob_data(v), false));
156 static int cert_dump(const char *certfile) {
157 struct cert_object *cobj;
158 static LIST_HEAD(certchain);
160 if (cert_load(certfile, &certchain)) {
161 fprintf(stderr, "cannot parse cert\n");
165 list_for_each_entry(cobj, &certchain, list)
166 cert_dump_blob(cobj->cert);
171 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
172 struct blob_buf certbuf;
173 struct blob_buf payloadbuf;
183 char fname[256], sfname[256];
185 char tmpdir[] = "/tmp/ucert-XXXXXX";
187 if (stat(certfile, &st) == 0) {
188 fprintf(stderr, "certfile %s exists, won't overwrite.\n", certfile);
192 pkf = fopen(pubkeyfile, "r");
196 pklen = fread(pkb, 1, 512, pkf);
197 pkb[pklen - 1] = '\0';
204 if (usign_f_pubkey(pkfp, pubkeyfile))
207 gettimeofday(&tv, NULL);
209 if (mkdtemp(tmpdir) == NULL)
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);
218 blobmsg_add_u64(&payloadbuf, "expiresat", tv.tv_sec + 60 * 60 * 24 * 365);
219 blobmsg_add_string(&payloadbuf, "pubkey", pkb);
221 blobmsg_add_string(&payloadbuf, "fingerprint", pkfp);
224 blobmsg_close_table(&payloadbuf, c);
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);
229 snprintf(sfname, sizeof(sfname) - 1, "%s/%s", tmpdir, revoker?"revoker.sig":"payload.sig");
230 if (usign_s(fname, seckeyfile, sfname, quiet))
233 sigf = fopen(sfname, "r");
237 siglen = fread(sigb, 1, 1024, sigf);
241 sigb[siglen-1] = '\0';
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);
260 static int cert_process_revoker(const char *certfile) {
261 fprintf(stderr, "not implemented\n");
265 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
266 fprintf(stderr, "not implemented\n");
270 static int usage(const char *cmd)
273 "Usage: %s <command> <options>\n"
275 " -A: append (needs -c and -p and/or -x)\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"
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"
293 int main(int argc, char *argv[]) {
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;
303 while ((ch = getopt(argc, argv, "ADIRVc:m:p:P:qs:x:")) != -1) {
342 return usage(argv[0]);
348 if (certfile && (pubkeyfile || sigfile))
349 return cert_append(certfile, pubkeyfile, sigfile);
351 return usage(argv[0]);
354 return cert_dump(certfile);
356 return usage(argv[0]);
358 if (certfile && pubkeyfile && seckeyfile)
359 return cert_issue(certfile, pubkeyfile, seckeyfile);
361 return usage(argv[0]);
364 return cert_process_revoker(certfile);
366 return usage(argv[0]);
368 if (certfile && (pubkeyfile || pubkeydir))
369 return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
371 return usage(argv[0]);
373 return usage(argv[0]);
377 return usage(argv[0]);