start implementing loading cert from filesystem, add validity times
[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 #define CERT_BUF_LEN 4096
37
38 static enum {
39 CMD_APPEND,
40 CMD_DUMP,
41 CMD_ISSUE,
42 CMD_REVOKE,
43 CMD_VERIFY,
44 CMD_NONE,
45 } cmd = CMD_NONE;
46
47 static bool quiet;
48
49 enum cert_attr {
50 CERT_ATTR_SIGNATURE,
51 CERT_ATTR_PAYLOAD,
52 CERT_ATTR_MAX
53 };
54
55 static const struct blob_attr_info cert_policy[CERT_ATTR_MAX] = {
56 [CERT_ATTR_SIGNATURE] = { .type = BLOB_ATTR_BINARY },
57 [CERT_ATTR_PAYLOAD] = { .type = BLOB_ATTR_NESTED },
58 };
59
60 enum cert_payload_attr {
61 CERT_PL_ATTR_CERTTYPE,
62 CERT_PL_ATTR_CERTID,
63 CERT_PL_ATTR_VALIDFROMTIME,
64 CERT_PL_ATTR_EXPIRETIME,
65 CERT_PL_ATTR_PUBKEY,
66 CERT_PL_ATTR_KEY_FINGERPRINT,
67 CERT_PL_ATTR_MAX
68 };
69
70 enum certtype_id {
71 CERTTYPE_UNSPEC,
72 CERTTYPE_AUTH,
73 CERTTYPE_REVOKE
74 };
75
76 static const struct blobmsg_policy cert_payload_policy[CERT_PL_ATTR_MAX] = {
77 [CERT_PL_ATTR_CERTTYPE] = { .type = BLOBMSG_TYPE_INT8 },
78 [CERT_PL_ATTR_CERTID] = { .type = BLOBMSG_TYPE_INT64 },
79 [CERT_PL_ATTR_VALIDFROMTIME] = { .type = BLOBMSG_TYPE_INT64 },
80 [CERT_PL_ATTR_EXPIRETIME] = { .type = BLOBMSG_TYPE_INT64 },
81 [CERT_PL_ATTR_PUBKEY] = { .type = BLOBMSG_TYPE_STRING },
82 [CERT_PL_ATTR_KEY_FINGERPRINT] = { .type = BLOBMSG_TYPE_STRING },
83 };
84
85
86 static int cert_load(const char *certfile, struct blob_attr *certtb[]) {
87 FILE *f;
88 struct blob_buf certbuf;
89 int ret = 0;
90 char filebuf[CERT_BUF_LEN];
91 int len;
92
93 blob_buf_init(&certbuf, 0);
94
95 f = fopen(certfile, "r");
96 if (!f)
97 return 1;
98
99 do {
100 len = fread(&filebuf, 1, CERT_BUF_LEN - 1, f);
101 blob_put_raw(&certbuf, filebuf, len);
102 } while(!feof(f) && !ferror(f));
103
104 ret = ferror(f);
105 fclose(f);
106
107 if (ret)
108 return 1;
109
110 blob_parse(certbuf.head, certtb, cert_policy, CERT_ATTR_MAX);
111
112 return 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 fprintf(stderr, "not implemented\n");
133 return 1;
134 }
135
136 static int cert_process_revoker(const char *certfile) {
137 fprintf(stderr, "not implemented\n");
138 return 1;
139 }
140
141 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
142 fprintf(stderr, "not implemented\n");
143 return 1;
144 }
145
146 static int usage(const char *cmd)
147 {
148 fprintf(stderr,
149 "Usage: %s <command> <options>\n"
150 "Commands:\n"
151 " -A: append (needs -c and -p and/or -x)\n"
152 " -D: dump\n"
153 " -I: issue cert and revoker (needs -c and -p and -s)\n"
154 " -R: process revoker certificate (needs -c)\n"
155 " -V: verify (needs -c and -p|-P)\n"
156 "Options:\n"
157 " -c <file>: certificate file\n"
158 " -m <file>: message file (verify only)\n"
159 " -p <file>: public key file\n"
160 " -P <path>: public key directory (verify only)\n"
161 " -q: quiet (do not print verification result, use return code only)\n"
162 " -s <file>: secret key file (issue only)\n"
163 " -x <file>: signature file\n"
164 "\n",
165 cmd);
166 return 1;
167 }
168
169 int main(int argc, char *argv[]) {
170 int ch;
171 const char *msgfile = NULL;
172 const char *sigfile = NULL;
173 const char *pubkeyfile = NULL;
174 const char *pubkeydir = NULL;
175 const char *certfile = NULL;
176 const char *seckeyfile = NULL;
177
178 quiet = false;
179 while ((ch = getopt(argc, argv, "ADIRVc:m:p:P:qs:x:")) != -1) {
180 switch (ch) {
181 case 'A':
182 cmd = CMD_APPEND;
183 break;
184 case 'D':
185 cmd = CMD_DUMP;
186 break;
187 case 'I':
188 cmd = CMD_ISSUE;
189 break;
190 case 'R':
191 cmd = CMD_REVOKE;
192 break;
193 case 'V':
194 cmd = CMD_VERIFY;
195 break;
196 case 'c':
197 certfile = optarg;
198 break;
199 case 'm':
200 msgfile = optarg;
201 break;
202 case 'p':
203 pubkeyfile = optarg;
204 break;
205 case 'P':
206 pubkeydir = optarg;
207 break;
208 case 's':
209 seckeyfile = optarg;
210 break;
211 case 'q':
212 quiet = true;
213 break;
214 case 'x':
215 sigfile = optarg;
216 break;
217 default:
218 return usage(argv[0]);
219 }
220 }
221
222 switch (cmd) {
223 case CMD_APPEND:
224 if (certfile && (pubkeyfile || sigfile))
225 return cert_append(certfile, pubkeyfile, sigfile);
226 else
227 return usage(argv[0]);
228 case CMD_DUMP:
229 if (certfile)
230 return cert_dump(certfile);
231 else
232 return usage(argv[0]);
233 case CMD_ISSUE:
234 if (certfile && pubkeyfile && seckeyfile)
235 return cert_issue(certfile, pubkeyfile, seckeyfile);
236 else
237 return usage(argv[0]);
238 case CMD_REVOKE:
239 if (certfile)
240 return cert_process_revoker(certfile);
241 else
242 return usage(argv[0]);
243 case CMD_VERIFY:
244 if (certfile && (pubkeyfile || pubkeydir))
245 return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
246 else
247 return usage(argv[0]);
248 case CMD_NONE:
249 return usage(argv[0]);
250 }
251
252 /* unreachable */
253 return usage(argv[0]);
254 }