added package management to ipsec attest
[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 <utils/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_PACKAGES,
106 OP_PRODUCTS,
107 OP_ADD,
108 OP_DEL,
109 } op = OP_UNDEF;
110
111 /* reinit getopt state */
112 optind = 0;
113
114 while (TRUE)
115 {
116 int c;
117
118 struct option long_opts[] = {
119 { "help", no_argument, NULL, 'h' },
120 { "components", no_argument, NULL, 'c' },
121 { "files", no_argument, NULL, 'f' },
122 { "keys", no_argument, NULL, 'k' },
123 { "packages", no_argument, NULL, 'g' },
124 { "products", no_argument, NULL, 'p' },
125 { "hashes", no_argument, NULL, 'H' },
126 { "measurements", no_argument, NULL, 'm' },
127 { "add", no_argument, NULL, 'a' },
128 { "delete", no_argument, NULL, 'd' },
129 { "del", no_argument, NULL, 'd' },
130 { "aik", required_argument, NULL, 'A' },
131 { "security", no_argument, NULL, 'B' },
132 { "component", required_argument, NULL, 'C' },
133 { "comp", required_argument, NULL, 'C' },
134 { "directory", required_argument, NULL, 'D' },
135 { "dir", required_argument, NULL, 'D' },
136 { "file", required_argument, NULL, 'F' },
137 { "sha1-ima", no_argument, NULL, 'I' },
138 { "package", required_argument, NULL, 'G' },
139 { "key", required_argument, NULL, 'K' },
140 { "owner", required_argument, NULL, 'O' },
141 { "product", required_argument, NULL, 'P' },
142 { "relative", no_argument, NULL, 'R' },
143 { "rel", no_argument, NULL, 'R' },
144 { "sequence", required_argument, NULL, 'S' },
145 { "version", required_argument, NULL, 'V' },
146 { "seq", required_argument, NULL, 'S' },
147 { "sha1", no_argument, NULL, '1' },
148 { "sha256", no_argument, NULL, '2' },
149 { "sha384", no_argument, NULL, '3' },
150 { "did", required_argument, NULL, '4' },
151 { "fid", required_argument, NULL, '5' },
152 { "pid", required_argument, NULL, '6' },
153 { "cid", required_argument, NULL, '7' },
154 { "kid", required_argument, NULL, '8' },
155 { "gid", required_argument, NULL, '9' },
156 { 0,0,0,0 }
157 };
158
159 c = getopt_long(argc, argv, "", long_opts, NULL);
160 switch (c)
161 {
162 case EOF:
163 break;
164 case 'h':
165 op = OP_USAGE;
166 break;
167 case 'c':
168 op = OP_COMPONENTS;
169 continue;
170 case 'f':
171 op = OP_FILES;
172 continue;
173 case 'g':
174 op = OP_PACKAGES;
175 continue;
176 case 'k':
177 op = OP_KEYS;
178 continue;
179 case 'p':
180 op = OP_PRODUCTS;
181 continue;
182 case 'H':
183 op = OP_HASHES;
184 continue;
185 case 'm':
186 op = OP_MEASUREMENTS;
187 continue;
188 case 'a':
189 op = OP_ADD;
190 continue;
191 case 'd':
192 op = OP_DEL;
193 continue;
194 case 'A':
195 {
196 certificate_t *aik_cert;
197 public_key_t *aik_key;
198 chunk_t aik;
199
200 aik_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
201 CERT_X509, BUILD_FROM_FILE, optarg, BUILD_END);
202 if (!aik_cert)
203 {
204 printf("AIK certificate '%s' could not be loaded\n", optarg);
205 exit(EXIT_FAILURE);
206 }
207 aik_key = aik_cert->get_public_key(aik_cert);
208 aik_cert->destroy(aik_cert);
209
210 if (!aik_key)
211 {
212 printf("AIK public key could not be retrieved\n");
213 exit(EXIT_FAILURE);
214 }
215 if (!aik_key->get_fingerprint(aik_key, KEYID_PUBKEY_INFO_SHA1,
216 &aik))
217 {
218 printf("AIK fingerprint could not be computed\n");
219 aik_key->destroy(aik_key);
220 exit(EXIT_FAILURE);
221 }
222 aik = chunk_clone(aik);
223 aik_key->destroy(aik_key);
224
225 if (!attest->set_key(attest, aik, op == OP_ADD))
226 {
227 exit(EXIT_FAILURE);
228 }
229 continue;
230 }
231 case 'B':
232 attest->set_security(attest);
233 continue;
234 case 'C':
235 if (!attest->set_component(attest, optarg, op == OP_ADD))
236 {
237 exit(EXIT_FAILURE);
238 }
239 continue;
240 case 'D':
241 if (!attest->set_directory(attest, optarg, op == OP_ADD))
242 {
243 exit(EXIT_FAILURE);
244 }
245 continue;
246 case 'F':
247 if (!attest->set_file(attest, optarg, op == OP_ADD))
248 {
249 exit(EXIT_FAILURE);
250 }
251 continue;
252 case 'G':
253 if (!attest->set_package(attest, optarg, op == OP_ADD))
254 {
255 exit(EXIT_FAILURE);
256 }
257 continue;
258 case 'I':
259 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1_IMA);
260 continue;
261 case 'K':
262 {
263 chunk_t aik;
264
265 aik = chunk_from_hex(chunk_create(optarg, strlen(optarg)), NULL);
266 if (!attest->set_key(attest, aik, op == OP_ADD))
267 {
268 exit(EXIT_FAILURE);
269 }
270 continue;
271 }
272 case 'O':
273 attest->set_owner(attest, optarg);
274 continue;
275 case 'P':
276 if (!attest->set_product(attest, optarg, op == OP_ADD))
277 {
278 exit(EXIT_FAILURE);
279 }
280 continue;
281 case 'R':
282 attest->set_relative(attest);
283 continue;
284 case 'S':
285 attest->set_sequence(attest, atoi(optarg));
286 continue;
287 case 'V':
288 if (!attest->set_version(attest, optarg))
289 {
290 exit(EXIT_FAILURE);
291 }
292 continue;
293 case '1':
294 attest->set_algo(attest, PTS_MEAS_ALGO_SHA1);
295 continue;
296 case '2':
297 attest->set_algo(attest, PTS_MEAS_ALGO_SHA256);
298 continue;
299 case '3':
300 attest->set_algo(attest, PTS_MEAS_ALGO_SHA384);
301 continue;
302 case '4':
303 if (!attest->set_did(attest, atoi(optarg)))
304 {
305 exit(EXIT_FAILURE);
306 }
307 continue;
308 case '5':
309 if (!attest->set_fid(attest, atoi(optarg)))
310 {
311 exit(EXIT_FAILURE);
312 }
313 continue;
314 case '6':
315 if (!attest->set_pid(attest, atoi(optarg)))
316 {
317 exit(EXIT_FAILURE);
318 }
319 continue;
320 case '7':
321 if (!attest->set_cid(attest, atoi(optarg)))
322 {
323 exit(EXIT_FAILURE);
324 }
325 continue;
326 case '8':
327 if (!attest->set_kid(attest, atoi(optarg)))
328 {
329 exit(EXIT_FAILURE);
330 }
331 continue;
332 case '9':
333 if (!attest->set_gid(attest, atoi(optarg)))
334 {
335 exit(EXIT_FAILURE);
336 }
337 continue;
338 }
339 break;
340 }
341
342 switch (op)
343 {
344 case OP_USAGE:
345 usage();
346 break;
347 case OP_PACKAGES:
348 attest->list_packages(attest);
349 break;
350 case OP_PRODUCTS:
351 attest->list_products(attest);
352 break;
353 case OP_KEYS:
354 attest->list_keys(attest);
355 break;
356 case OP_COMPONENTS:
357 attest->list_components(attest);
358 break;
359 case OP_FILES:
360 attest->list_files(attest);
361 break;
362 case OP_HASHES:
363 attest->list_hashes(attest);
364 break;
365 case OP_MEASUREMENTS:
366 attest->list_measurements(attest);
367 break;
368 case OP_ADD:
369 attest->add(attest);
370 break;
371 case OP_DEL:
372 attest->delete(attest);
373 break;
374 default:
375 usage();
376 exit(EXIT_FAILURE);
377 }
378 }
379
380 int main(int argc, char *argv[])
381 {
382 char *uri;
383
384 /* enable attest debugging hook */
385 dbg = attest_dbg;
386 openlog("attest", 0, LOG_DEBUG);
387
388 atexit(library_deinit);
389
390 /* initialize library */
391 if (!library_init(NULL))
392 {
393 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
394 }
395 if (!lib->plugins->load(lib->plugins, NULL,
396 lib->settings->get_str(lib->settings, "attest.load", PLUGINS)))
397 {
398 exit(SS_RC_INITIALIZATION_FAILED);
399 }
400
401 uri = lib->settings->get_str(lib->settings, "attest.database", NULL);
402 if (!uri)
403 {
404 fprintf(stderr, "database URI attest.database not set.\n");
405 exit(SS_RC_INITIALIZATION_FAILED);
406 }
407 attest = attest_db_create(uri);
408 if (!attest)
409 {
410 exit(SS_RC_INITIALIZATION_FAILED);
411 }
412 atexit(cleanup);
413 libimcv_init();
414 libpts_init();
415
416 do_args(argc, argv);
417
418 exit(EXIT_SUCCESS);
419 }
420