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