06b46bba84ead7ea141bc36f65a598a166c42f93
[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] = { .type = BLOBMSG_TYPE_INT8 },
80         [CERT_PL_ATTR_CERTID] = { .type = BLOBMSG_TYPE_INT64 },
81         [CERT_PL_ATTR_VALIDFROMTIME] = { .type = BLOBMSG_TYPE_INT64 },
82         [CERT_PL_ATTR_EXPIRETIME] = { .type = BLOBMSG_TYPE_INT64 },
83         [CERT_PL_ATTR_PUBKEY] = { .type = BLOBMSG_TYPE_STRING },
84         [CERT_PL_ATTR_KEY_FINGERPRINT] = { .type = BLOBMSG_TYPE_STRING },
85 };
86
87
88 static int cert_load(const char *certfile, struct blob_attr *certtb[]) {
89         FILE *f;
90         struct blob_buf certbuf;
91         int ret = 0;
92         char filebuf[CERT_BUF_LEN];
93         int len;
94
95         blob_buf_init(&certbuf, 0);
96
97         f = fopen(certfile, "r");
98         if (!f)
99                 return 1;
100
101         do {
102                 len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
103                 blob_put_raw(&certbuf, filebuf, len);
104         } while(!feof(f) && !ferror(f));
105
106         ret = ferror(f);
107         fclose(f);
108
109         if (ret)
110                 return 1;
111
112         return (blob_parse(certbuf.head, certtb, cert_policy, CERT_ATTR_MAX) != 0);
113 }
114
115 static int cert_append(const char *certfile, const char *pubkeyfile, const char *sigfile) {
116         fprintf(stderr, "not implemented\n");
117         return 1;
118 }
119
120 static int cert_dump(const char *certfile) {
121         struct blob_attr *certtb[CERT_ATTR_MAX];
122
123         if (cert_load(certfile, certtb)) {
124                 fprintf(stderr, "cannot parse cert\n");
125                 return 1;
126         }
127
128         return 0;
129 }
130
131 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
132         struct blob_buf certbuf;
133         struct blob_buf payloadbuf;
134
135         blob_buf_init(&payloadbuf, 0);
136 /*      usign_s() */
137
138         blob_buf_init(&certbuf, 0);
139
140         fprintf(stderr, "not implemented\n");
141         return 1;
142 }
143
144 static int cert_process_revoker(const char *certfile) {
145         fprintf(stderr, "not implemented\n");
146         return 1;
147 }
148
149 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
150         fprintf(stderr, "not implemented\n");
151         return 1;
152 }
153
154 static int usage(const char *cmd)
155 {
156         fprintf(stderr,
157                 "Usage: %s <command> <options>\n"
158                 "Commands:\n"
159                 "  -A:                  append (needs -c and -p and/or -x)\n"
160                 "  -D:                  dump\n"
161                 "  -I:                  issue cert and revoker (needs -c and -p and -s)\n"
162                 "  -R:                  process revoker certificate (needs -c)\n"
163                 "  -V:                  verify (needs -c and -p|-P)\n"
164                 "Options:\n"
165                 "  -c <file>:           certificate file\n"
166                 "  -m <file>:           message file (verify only)\n"
167                 "  -p <file>:           public key file\n"
168                 "  -P <path>:           public key directory (verify only)\n"
169                 "  -q:                  quiet (do not print verification result, use return code only)\n"
170                 "  -s <file>:           secret key file (issue only)\n"
171                 "  -x <file>:           signature file\n"
172                 "\n",
173                 cmd);
174         return 1;
175 }
176
177 int main(int argc, char *argv[]) {
178         int ch;
179         const char *msgfile = NULL;
180         const char *sigfile = NULL;
181         const char *pubkeyfile = NULL;
182         const char *pubkeydir = NULL;
183         const char *certfile = NULL;
184         const char *seckeyfile = NULL;
185
186         quiet = false;
187         while ((ch = getopt(argc, argv, "ADIRVc:m:p:P:qs:x:")) != -1) {
188                 switch (ch) {
189                 case 'A':
190                         cmd = CMD_APPEND;
191                         break;
192                 case 'D':
193                         cmd = CMD_DUMP;
194                         break;
195                 case 'I':
196                         cmd = CMD_ISSUE;
197                         break;
198                 case 'R':
199                         cmd = CMD_REVOKE;
200                         break;
201                 case 'V':
202                         cmd = CMD_VERIFY;
203                         break;
204                 case 'c':
205                         certfile = optarg;
206                         break;
207                 case 'm':
208                         msgfile = optarg;
209                         break;
210                 case 'p':
211                         pubkeyfile = optarg;
212                         break;
213                 case 'P':
214                         pubkeydir = optarg;
215                         break;
216                 case 's':
217                         seckeyfile = optarg;
218                         break;
219                 case 'q':
220                         quiet = true;
221                         break;
222                 case 'x':
223                         sigfile = optarg;
224                         break;
225                 default:
226                         return usage(argv[0]);
227                 }
228         }
229
230         switch (cmd) {
231         case CMD_APPEND:
232                 if (certfile && (pubkeyfile || sigfile))
233                         return cert_append(certfile, pubkeyfile, sigfile);
234                 else
235                         return usage(argv[0]);
236         case CMD_DUMP:
237                 if (certfile)
238                         return cert_dump(certfile);
239                 else
240                         return usage(argv[0]);
241         case CMD_ISSUE:
242                 if (certfile && pubkeyfile && seckeyfile)
243                         return cert_issue(certfile, pubkeyfile, seckeyfile);
244                 else
245                         return usage(argv[0]);
246         case CMD_REVOKE:
247                 if (certfile)
248                         return cert_process_revoker(certfile);
249                 else
250                         return usage(argv[0]);
251         case CMD_VERIFY:
252                 if (certfile && (pubkeyfile || pubkeydir))
253                         return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
254                 else
255                         return usage(argv[0]);
256         case CMD_NONE:
257                 return usage(argv[0]);
258         }
259
260         /* unreachable */
261         return usage(argv[0]);
262 }