add usign-exec.c
[project/ucert.git] / ucert.c
diff --git a/ucert.c b/ucert.c
index 54ec2233cbe57713439dccbcee26f7d0bdb24bdf..06b46bba84ead7ea141bc36f65a598a166c42f93 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,17 +48,95 @@ 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] = { .type = BLOBMSG_TYPE_INT8 },
+       [CERT_PL_ATTR_CERTID] = { .type = BLOBMSG_TYPE_INT64 },
+       [CERT_PL_ATTR_VALIDFROMTIME] = { .type = BLOBMSG_TYPE_INT64 },
+       [CERT_PL_ATTR_EXPIRETIME] = { .type = BLOBMSG_TYPE_INT64 },
+       [CERT_PL_ATTR_PUBKEY] = { .type = BLOBMSG_TYPE_STRING },
+       [CERT_PL_ATTR_KEY_FINGERPRINT] = { .type = BLOBMSG_TYPE_STRING },
+};
+
+
+static int cert_load(const char *certfile, struct blob_attr *certtb[]) {
+       FILE *f;
+       struct blob_buf certbuf;
+       int ret = 0;
+       char filebuf[CERT_BUF_LEN];
+       int len;
+
+       blob_buf_init(&certbuf, 0);
+
+       f = fopen(certfile, "r");
+       if (!f)
+               return 1;
+
+       do {
+               len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
+               blob_put_raw(&certbuf, filebuf, len);
+       } while(!feof(f) && !ferror(f));
+
+       ret = ferror(f);
+       fclose(f);
+
+       if (ret)
+               return 1;
+
+       return (blob_parse(certbuf.head, certtb, cert_policy, CERT_ATTR_MAX) != 0);
+}
+
 static int cert_append(const char *certfile, const char *pubkeyfile, const char *sigfile) {
        fprintf(stderr, "not implemented\n");
        return 1;
 }
 
 static int cert_dump(const char *certfile) {
-       fprintf(stderr, "not implemented\n");
-       return 1;
+       struct blob_attr *certtb[CERT_ATTR_MAX];
+
+       if (cert_load(certfile, certtb)) {
+               fprintf(stderr, "cannot parse cert\n");
+               return 1;
+       }
+
+       return 0;
 }
 
 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
+       struct blob_buf certbuf;
+       struct blob_buf payloadbuf;
+
+       blob_buf_init(&payloadbuf, 0);
+/*     usign_s() */
+
+       blob_buf_init(&certbuf, 0);
+
        fprintf(stderr, "not implemented\n");
        return 1;
 }