use list to model certificate chain
[oweals/ucert.git] / ucert.c
diff --git a/ucert.c b/ucert.c
index 54ec2233cbe57713439dccbcee26f7d0bdb24bdf..6a830539fddd06890bca72e2d11a1e45a1f331ed 100644 (file)
--- a/ucert.c
+++ b/ucert.c
 
 #include <json-c/json.h>
 #include <libubox/blob.h>
+#include <libubox/utils.h>
 #include <libubox/list.h>
 #include <libubox/vlist.h>
 #include <libubox/blobmsg_json.h>
 
+#include "usign.h"
+
+#define CERT_BUF_LEN 4096
+
 static enum {
        CMD_APPEND,
        CMD_DUMP,
@@ -43,19 +48,213 @@ static enum {
 
 static bool quiet;
 
+enum cert_attr {
+       CERT_ATTR_SIGNATURE,
+       CERT_ATTR_PAYLOAD,
+       CERT_ATTR_MAX
+};
+
+static const struct blob_attr_info cert_policy[CERT_ATTR_MAX] = {
+       [CERT_ATTR_SIGNATURE] = { .type = BLOB_ATTR_BINARY },
+       [CERT_ATTR_PAYLOAD] = { .type = BLOB_ATTR_NESTED },
+};
+
+enum cert_payload_attr {
+       CERT_PL_ATTR_CERTTYPE,
+       CERT_PL_ATTR_CERTID,
+       CERT_PL_ATTR_VALIDFROMTIME,
+       CERT_PL_ATTR_EXPIRETIME,
+       CERT_PL_ATTR_PUBKEY,
+       CERT_PL_ATTR_KEY_FINGERPRINT,
+       CERT_PL_ATTR_MAX
+};
+
+enum certtype_id {
+       CERTTYPE_UNSPEC,
+       CERTTYPE_AUTH,
+       CERTTYPE_REVOKE
+};
+
+static const struct blobmsg_policy cert_payload_policy[CERT_PL_ATTR_MAX] = {
+       [CERT_PL_ATTR_CERTTYPE] = { .name = "certtype", .type = BLOBMSG_TYPE_INT32 },
+       [CERT_PL_ATTR_CERTID] = { .name = "certid", .type = BLOBMSG_TYPE_INT32 },
+       [CERT_PL_ATTR_VALIDFROMTIME] = { .name = "validfrom", .type = BLOBMSG_TYPE_INT64 },
+       [CERT_PL_ATTR_EXPIRETIME] = { .name = "expiresat", .type = BLOBMSG_TYPE_INT64 },
+       [CERT_PL_ATTR_PUBKEY] = { .name = "pubkey", .type = BLOBMSG_TYPE_STRING },
+       [CERT_PL_ATTR_KEY_FINGERPRINT] = { .name = "fingerprint", .type = BLOBMSG_TYPE_STRING },
+};
+
+struct cert_object {
+       struct list_head list;
+       struct blob_attr **cert;
+};
+
+static int write_file(const char *filename, void *buf, size_t len, bool append) {
+       FILE *f;
+       size_t outlen;
+
+       f = fopen(filename, append?"a":"w");
+       if (!f)
+               return 1;
+
+       outlen = fwrite(buf, 1, len, f);
+       fclose(f);
+       return (outlen == len);
+}
+
+static int cert_load(const char *certfile, struct list_head *chain) {
+       FILE *f;
+       struct blob_attr *certtb[CERT_ATTR_MAX];
+       struct cert_object *cobj;
+       char filebuf[CERT_BUF_LEN];
+       int ret = 0;
+       int len;
+
+       f = fopen(certfile, "r");
+       if (!f)
+               return 1;
+
+       len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
+       ret = ferror(f) || !feof(f);
+       fclose(f);
+       if (ret)
+               return 1;
+       ret = blob_parse(filebuf, certtb, cert_policy, CERT_ATTR_MAX);
+       cobj = calloc(1, sizeof(*cobj));
+       cobj->cert = &certtb;
+       list_add_tail(&cobj->list, chain);
+
+       fprintf(stderr, "blob_parse return %d\n", ret);
+       return (ret <= 0);
+}
+
 static int cert_append(const char *certfile, const char *pubkeyfile, const char *sigfile) {
        fprintf(stderr, "not implemented\n");
        return 1;
 }
 
+static void cert_dump_blob(struct blob_attr *cert[CERT_ATTR_MAX]) {
+       int i;
+
+       for (i = 0; i < CERT_ATTR_MAX; i++) {
+               struct blob_attr *v = cert[i];
+
+               if (!v)
+                       continue;
+
+               switch(cert_policy[i].type) {
+               case BLOB_ATTR_BINARY:
+                       fprintf(stdout, "signature: %s\n", blob_data(v));
+                       break;
+               case BLOB_ATTR_NESTED:
+                       fprintf(stdout, "payload:\n%s\n", blobmsg_format_json(blob_data(v), false));
+                       break;
+               }
+       }
+}
+
 static int cert_dump(const char *certfile) {
-       fprintf(stderr, "not implemented\n");
-       return 1;
+       struct cert_object *cobj;
+       static LIST_HEAD(certchain);
+
+       if (cert_load(certfile, &certchain)) {
+               fprintf(stderr, "cannot parse cert\n");
+               return 1;
+       }
+
+       list_for_each_entry(cobj, &certchain, list)
+               cert_dump_blob(cobj->cert);
+
+       return 0;
 }
 
 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
-       fprintf(stderr, "not implemented\n");
-       return 1;
+       struct blob_buf certbuf;
+       struct blob_buf payloadbuf;
+       struct timeval tv;
+       struct stat st;
+       int pklen, siglen;
+       int revoker = 1;
+       void *c;
+
+       FILE *pkf, *sigf;
+       char pkb[512];
+       char sigb[512];
+       char fname[256], sfname[256];
+       char pkfp[17];
+       char tmpdir[] = "/tmp/ucert-XXXXXX";
+
+       if (stat(certfile, &st) == 0) {
+               fprintf(stderr, "certfile %s exists, won't overwrite.\n", certfile);
+               return -1;
+       }
+
+       pkf = fopen(pubkeyfile, "r");
+       if (!pkf)
+               return -1;
+
+       pklen = fread(pkb, 1, 512, pkf);
+       pkb[pklen - 1] = '\0';
+
+       if (pklen < 32)
+               return -1;
+
+       fclose(pkf);
+
+       if (usign_f_pubkey(pkfp, pubkeyfile))
+               return -1;
+
+       gettimeofday(&tv, NULL);
+
+       if (mkdtemp(tmpdir) == NULL)
+               return errno;
+
+       while (revoker >= 0) {
+               blob_buf_init(&payloadbuf, 0);
+               c = blobmsg_open_table(&payloadbuf, "ucert");
+               blobmsg_add_u32(&payloadbuf, "certtype", revoker?CERTTYPE_REVOKE:CERTTYPE_AUTH);
+               blobmsg_add_u64(&payloadbuf, "validfrom", tv.tv_sec);
+               if (!revoker) {
+                       blobmsg_add_u64(&payloadbuf, "expiresat", tv.tv_sec + 60 * 60 * 24 * 365);
+                       blobmsg_add_string(&payloadbuf, "pubkey", pkb);
+               } else {
+                       blobmsg_add_string(&payloadbuf, "fingerprint", pkfp);
+               }
+
+               blobmsg_close_table(&payloadbuf, c);
+
+               snprintf(fname, sizeof(fname) - 1, "%s/%s", tmpdir, revoker?"revoker":"payload");
+               write_file(fname, blob_data(payloadbuf.head), blob_len(payloadbuf.head), false);
+
+               snprintf(sfname, sizeof(sfname) - 1, "%s/%s", tmpdir, revoker?"revoker.sig":"payload.sig");
+               if (usign_s(fname, seckeyfile, sfname, quiet))
+                       return 1;
+
+               sigf = fopen(sfname, "r");
+               if (!sigf)
+                       return 1;
+
+               siglen = fread(sigb, 1, 1024, sigf);
+               if (siglen < 1)
+                       return 1;
+
+               sigb[siglen-1] = '\0';
+               fclose(sigf);
+
+               unlink(fname);
+               unlink(sfname);
+
+               blob_buf_init(&certbuf, 0);
+               blob_put(&certbuf, CERT_ATTR_SIGNATURE, sigb, siglen);
+               blob_put(&certbuf, CERT_ATTR_PAYLOAD, blob_data(payloadbuf.head), blob_len(payloadbuf.head));
+               snprintf(fname, sizeof(fname) - 1, "%s%s", certfile, revoker?".revoke":"");
+               write_file(fname, certbuf.head, blob_raw_len(certbuf.head), false);
+               revoker--;
+       }
+
+       rmdir(tmpdir);
+
+       return 0;
 }
 
 static int cert_process_revoker(const char *certfile) {