allow --rel as an abbreviation for --relative
[strongswan.git] / src / libpts / plugins / imv_attestation / attest.c
1 /*
2 * Copyright (C) 2011-2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <getopt.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <syslog.h>
23
24 #include <library.h>
25 #include <debug.h>
26
27 #include <imcv.h>
28 #include <libpts.h>
29 #include <pts/pts_meas_algo.h>
30
31 #include "attest_db.h"
32 #include "attest_usage.h"
33
34 /**
35 * global debug output variables
36 */
37 static int debug_level = 1;
38 static bool stderr_quiet = TRUE;
39
40 /**
41 * attest dbg function
42 */
43 static void attest_dbg(debug_t group, level_t level, char *fmt, ...)
44 {
45 int priority = LOG_INFO;
46 char buffer[8192];
47 char *current = buffer, *next;
48 va_list args;
49
50 if (level <= debug_level)
51 {
52 if (!stderr_quiet)
53 {
54 va_start(args, fmt);
55 vfprintf(stderr, fmt, args);
56 fprintf(stderr, "\n");
57 va_end(args);
58 }
59
60 /* write in memory buffer first */
61 va_start(args, fmt);
62 vsnprintf(buffer, sizeof(buffer), fmt, args);
63 va_end(args);
64
65 /* do a syslog with every line */
66 while (current)
67 {
68 next = strchr(current, '\n');
69 if (next)
70 {
71 *(next++) = '\0';
72 }
73 syslog(priority, "%s\n", current);
74 current = next;
75 }
76 }
77 }
78
79 /**
80 * global attestation database object
81 */
82 attest_db_t *attest;
83
84 /**
85 * atexit handler to close db on shutdown
86 */
87 static void cleanup(void)
88 {
89 attest->destroy(attest);
90 libpts_deinit();
91 libimcv_deinit();
92 closelog();
93 }
94
95 static void do_args(int argc, char *argv[])
96 {
97 enum {
98 OP_UNDEF,
99 OP_USAGE,
100 OP_KEYS,
101 OP_COMPONENTS,
102 OP_FILES,
103 OP_HASHES,
104 OP_MEASUREMENTS,
105 OP_PRODUCTS,
106 OP_ADD,
107 OP_DEL,
108 } op = OP_UNDEF;
109
110 /* reinit getopt state */
111 optind = 0;
112
113 while (TRUE)
114 {
115 int c;
116
117 struct option long_opts[] = {
118 { "help", no_argument, NULL, 'h' },
119 { "components", no_argument, NULL, 'c' },
120 { "files", no_argument, NULL, 'f' },
121 { "keys", no_argument, NULL, 'k' },
122 { "products", no_argument, NULL, 'p' },
123 { "hashes", no_argument, NULL, 'H' },
124 { "measurements", no_argument, NULL, 'm' },
125 { "add", no_argument, NULL, 'a' },
126 { "delete", no_argument, NULL, 'd' },
127 { "del", no_argument, NULL, 'd' },
128 { "aik", required_argument, NULL, 'A' },
129 { "component", required_argument, NULL, 'C' },
130 { "comp", required_argument, NULL, 'C' },
131 { "directory", required_argument, NULL, 'D' },
132 { "dir", required_argument, NULL, 'D' },
133 { "file", required_argument, NULL, 'F' },
134 { "sha1-ima", no_argument, NULL, 'I' },
135 { "key", required_argument, NULL, 'K' },
136 { "owner", required_argument, NULL, 'O' },
137 { "product", required_argument, NULL, 'P' },
138 { "relative", no_argument, NULL, 'R' },
139 { "rel", no_argument, NULL, 'R' },
140 { "sha1", no_argument, NULL, '1' },
141 { "sha256", no_argument, NULL, '2' },
142 { "sha384", no_argument, NULL, '3' },
143 { "did", required_argument, NULL, '4' },
144 { "fid", required_argument, NULL, '5' },
145 { "pid", required_argument, NULL, '6' },
146 { "cid", required_argument, NULL, '7' },
147 { "kid", required_argument, NULL, '8' },
148 { 0,0,0,0 }
149 };
150
151 c = getopt_long(argc, argv, "", long_opts, NULL);
152 switch (c)
153 {
154 case EOF:
155 break;
156 case 'h':
157 op = OP_USAGE;
158 break;
159 case 'c':
160 op = OP_COMPONENTS;
161 continue;
162 case 'f':
163 op = OP_FILES;
164 continue;
165 case 'k':
166 op = OP_KEYS;
167 continue;
168 case 'p':
169 op = OP_PRODUCTS;
170 continue;
171 case 'H':
172 op = OP_HASHES;
173 continue;
174 case 'm':
175 op = OP_MEASUREMENTS;
176 continue;
177 case 'a':
178 op = OP_ADD;
179 continue;
180 case 'd':
181 op = OP_DEL;
182 continue;
183 case 'A':
184 {
185 certificate_t *aik_cert;
186 public_key_t *aik_key;
187 chunk_t aik;
188
189 aik_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
190 CERT_X509, BUILD_FROM_FILE, optarg, BUILD_END);
191 if (!aik_cert)
192 {
193 printf("AIK certificate '%s' could not be loaded\n", optarg);
194 exit(EXIT_FAILURE);
195 }
196 aik_key = aik_cert->get_public_key(aik_cert);
197 aik_cert->destroy(aik_cert);
198
199 if (!aik_key)
200 {
201 printf("AIK public key could not be retrieved\n");
202 exit(EXIT_FAILURE);
203 }
204 if (!aik_key->get_fingerprint(aik_key, KEYID_PUBKEY_INFO_SHA1,
205 &aik))
206 {
207 printf("AIK fingerprint could not be computed\n");
208 aik_key->destroy(aik_key);
209 exit(EXIT_FAILURE);
210 }
211 aik = chunk_clone(aik);
212 aik_key->destroy(aik_key);
213
214 if (!attest->set_key(attest, aik, op == OP_ADD))
215 {
216 exit(EXIT_FAILURE);
217 }
218 continue;
219 }
220 case 'C':
221 if (!attest->set_component(attest, optarg, op == OP_ADD))
222 {
223 exit(EXIT_FAILURE);
224 }
225 continue;
226 case 'D':
227 if (!attest->set_directory(attest, optarg, op == OP_ADD))
228 {
229 exit(EXIT_FAILURE);
230 }
231 continue;
232 case 'F':
233 if (!attest->set_file(attest, optarg, op == OP_ADD))
234 {
235 exit(EXIT_FAILURE);
236 }
237 continue;
238 case 'I':
239 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1_IMA);
240 continue;
241 case 'K':
242 {
243 chunk_t aik;
244
245 aik = chunk_from_hex(chunk_create(optarg, strlen(optarg)), NULL);
246 if (!attest->set_key(attest, aik, op == OP_ADD))
247 {
248 exit(EXIT_FAILURE);
249 }
250 continue;
251 }
252 case 'O':
253 attest->set_owner(attest, optarg);
254 continue;
255 case 'P':
256 if (!attest->set_product(attest, optarg, op == OP_ADD))
257 {
258 exit(EXIT_FAILURE);
259 }
260 continue;
261 case 'R':
262 attest->set_relative(attest);
263 continue;
264 case '1':
265 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1);
266 continue;
267 case '2':
268 attest->set_algo(attest, PTS_MEAS_ALGO_SHA256);
269 continue;
270 case '3':
271 attest->set_algo(attest, PTS_MEAS_ALGO_SHA384);
272 continue;
273 case '4':
274 if (!attest->set_did(attest, atoi(optarg)))
275 {
276 exit(EXIT_FAILURE);
277 }
278 continue;
279 case '5':
280 if (!attest->set_fid(attest, atoi(optarg)))
281 {
282 exit(EXIT_FAILURE);
283 }
284 continue;
285 case '6':
286 if (!attest->set_pid(attest, atoi(optarg)))
287 {
288 exit(EXIT_FAILURE);
289 }
290 continue;
291 case '7':
292 if (!attest->set_cid(attest, atoi(optarg)))
293 {
294 exit(EXIT_FAILURE);
295 }
296 continue;
297 case '8':
298 if (!attest->set_kid(attest, atoi(optarg)))
299 {
300 exit(EXIT_FAILURE);
301 }
302 continue;
303 }
304 break;
305 }
306
307 switch (op)
308 {
309 case OP_USAGE:
310 usage();
311 break;
312 case OP_PRODUCTS:
313 attest->list_products(attest);
314 break;
315 case OP_KEYS:
316 attest->list_keys(attest);
317 break;
318 case OP_COMPONENTS:
319 attest->list_components(attest);
320 break;
321 case OP_FILES:
322 attest->list_files(attest);
323 break;
324 case OP_HASHES:
325 attest->list_hashes(attest);
326 break;
327 case OP_MEASUREMENTS:
328 attest->list_measurements(attest);
329 break;
330 case OP_ADD:
331 attest->add(attest);
332 break;
333 case OP_DEL:
334 attest->delete(attest);
335 break;
336 default:
337 usage();
338 exit(EXIT_FAILURE);
339 }
340 }
341
342 int main(int argc, char *argv[])
343 {
344 char *uri;
345
346 /* enable attest debugging hook */
347 dbg = attest_dbg;
348 openlog("attest", 0, LOG_DEBUG);
349
350 atexit(library_deinit);
351
352 /* initialize library */
353 if (!library_init(NULL))
354 {
355 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
356 }
357 if (!lib->plugins->load(lib->plugins, NULL,
358 lib->settings->get_str(lib->settings, "attest.load", PLUGINS)))
359 {
360 exit(SS_RC_INITIALIZATION_FAILED);
361 }
362
363 uri = lib->settings->get_str(lib->settings, "attest.database", NULL);
364 if (!uri)
365 {
366 fprintf(stderr, "database URI attest.database not set.\n");
367 exit(SS_RC_INITIALIZATION_FAILED);
368 }
369 attest = attest_db_create(uri);
370 if (!attest)
371 {
372 exit(SS_RC_INITIALIZATION_FAILED);
373 }
374 atexit(cleanup);
375 libimcv_init();
376 libpts_init();
377
378 do_args(argc, argv);
379
380 exit(EXIT_SUCCESS);
381 }
382