add external blob and internal blobmsg data structures
[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/list.h>
32 #include <libubox/vlist.h>
33 #include <libubox/blobmsg_json.h>
34
35 static enum {
36 CMD_APPEND,
37 CMD_DUMP,
38 CMD_ISSUE,
39 CMD_REVOKE,
40 CMD_VERIFY,
41 CMD_NONE,
42 } cmd = CMD_NONE;
43
44 static bool quiet;
45
46 enum cert_attr {
47 CERT_ATTR_SIGNATURE,
48 CERT_ATTR_PAYLOAD,
49 CERT_ATTR_MAX
50 };
51
52 static const struct blob_attr_info cert_policy[CERT_ATTR_MAX] = {
53 [CERT_ATTR_SIGNATURE] = { .type = BLOB_ATTR_BINARY },
54 [CERT_ATTR_PAYLOAD] = { .type = BLOB_ATTR_NESTED },
55 };
56
57 enum cert_payload_attr {
58 CERT_PL_ATTR_CERTTYPE,
59 CERT_PL_ATTR_CERTID,
60 CERT_PL_ATTR_PUBKEY,
61 CERT_PL_ATTR_KEY_FINGERPRINT,
62 CERT_PL_ATTR_MAX
63 };
64
65 enum certtype_id {
66 CERTTYPE_AUTH,
67 CERTTYPE_REVOKE
68 };
69
70 static const struct blobmsg_policy cert_payload_policy[CERT_PL_ATTR_MAX] = {
71 [CERT_PL_ATTR_CERTTYPE] = { .type = BLOBMSG_TYPE_INT8 },
72 [CERT_PL_ATTR_CERTID] = { .type = BLOBMSG_TYPE_INT64 },
73 [CERT_PL_ATTR_PUBKEY] = { .type = BLOBMSG_TYPE_STRING },
74 [CERT_PL_ATTR_KEY_FINGERPRINT] = { .type = BLOBMSG_TYPE_STRING },
75 };
76
77 static int cert_append(const char *certfile, const char *pubkeyfile, const char *sigfile) {
78 fprintf(stderr, "not implemented\n");
79 return 1;
80 }
81
82 static int cert_dump(const char *certfile) {
83 fprintf(stderr, "not implemented\n");
84 return 1;
85 }
86
87 static int cert_issue(const char *certfile, const char *pubkeyfile, const char *seckeyfile) {
88 fprintf(stderr, "not implemented\n");
89 return 1;
90 }
91
92 static int cert_process_revoker(const char *certfile) {
93 fprintf(stderr, "not implemented\n");
94 return 1;
95 }
96
97 static int cert_verify(const char *certfile, const char *pubkeyfile, const char *pubkeydir, const char *msgfile) {
98 fprintf(stderr, "not implemented\n");
99 return 1;
100 }
101
102 static int usage(const char *cmd)
103 {
104 fprintf(stderr,
105 "Usage: %s <command> <options>\n"
106 "Commands:\n"
107 " -A: append (needs -c and -p and/or -x)\n"
108 " -D: dump\n"
109 " -I: issue cert and revoker (needs -c and -p and -s)\n"
110 " -R: process revoker certificate (needs -c)\n"
111 " -V: verify (needs -c and -p|-P)\n"
112 "Options:\n"
113 " -c <file>: certificate file\n"
114 " -m <file>: message file (verify only)\n"
115 " -p <file>: public key file\n"
116 " -P <path>: public key directory (verify only)\n"
117 " -q: quiet (do not print verification result, use return code only)\n"
118 " -s <file>: secret key file (issue only)\n"
119 " -x <file>: signature file\n"
120 "\n",
121 cmd);
122 return 1;
123 }
124
125 int main(int argc, char *argv[]) {
126 int ch;
127 const char *msgfile = NULL;
128 const char *sigfile = NULL;
129 const char *pubkeyfile = NULL;
130 const char *pubkeydir = NULL;
131 const char *certfile = NULL;
132 const char *seckeyfile = NULL;
133
134 quiet = false;
135 while ((ch = getopt(argc, argv, "ADIRVc:m:p:P:qs:x:")) != -1) {
136 switch (ch) {
137 case 'A':
138 cmd = CMD_APPEND;
139 break;
140 case 'D':
141 cmd = CMD_DUMP;
142 break;
143 case 'I':
144 cmd = CMD_ISSUE;
145 break;
146 case 'R':
147 cmd = CMD_REVOKE;
148 break;
149 case 'V':
150 cmd = CMD_VERIFY;
151 break;
152 case 'c':
153 certfile = optarg;
154 break;
155 case 'm':
156 msgfile = optarg;
157 break;
158 case 'p':
159 pubkeyfile = optarg;
160 break;
161 case 'P':
162 pubkeydir = optarg;
163 break;
164 case 's':
165 seckeyfile = optarg;
166 break;
167 case 'q':
168 quiet = true;
169 break;
170 case 'x':
171 sigfile = optarg;
172 break;
173 default:
174 return usage(argv[0]);
175 }
176 }
177
178 switch (cmd) {
179 case CMD_APPEND:
180 if (certfile && (pubkeyfile || sigfile))
181 return cert_append(certfile, pubkeyfile, sigfile);
182 else
183 return usage(argv[0]);
184 case CMD_DUMP:
185 if (certfile)
186 return cert_dump(certfile);
187 else
188 return usage(argv[0]);
189 case CMD_ISSUE:
190 if (certfile && pubkeyfile && seckeyfile)
191 return cert_issue(certfile, pubkeyfile, seckeyfile);
192 else
193 return usage(argv[0]);
194 case CMD_REVOKE:
195 if (certfile)
196 return cert_process_revoker(certfile);
197 else
198 return usage(argv[0]);
199 case CMD_VERIFY:
200 if (certfile && (pubkeyfile || pubkeydir))
201 return cert_verify(certfile, pubkeyfile, pubkeydir, msgfile);
202 else
203 return usage(argv[0]);
204 case CMD_NONE:
205 return usage(argv[0]);
206 }
207
208 /* unreachable */
209 return usage(argv[0]);
210 }