2 * Copyright (C) 2011-2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
25 #include <utils/debug.h>
29 #include <pts/pts_meas_algo.h>
31 #include "attest_db.h"
32 #include "attest_usage.h"
35 * global debug output variables
37 static int debug_level
= 1;
38 static bool stderr_quiet
= TRUE
;
43 static void attest_dbg(debug_t group
, level_t level
, char *fmt
, ...)
45 int priority
= LOG_INFO
;
47 char *current
= buffer
, *next
;
50 if (level
<= debug_level
)
55 vfprintf(stderr
, fmt
, args
);
56 fprintf(stderr
, "\n");
60 /* write in memory buffer first */
62 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
65 /* do a syslog with every line */
68 next
= strchr(current
, '\n');
73 syslog(priority
, "%s\n", current
);
80 * global attestation database object
85 * atexit handler to close db on shutdown
87 static void cleanup(void)
89 attest
->destroy(attest
);
95 static void do_args(int argc
, char *argv
[])
112 /* reinit getopt state */
119 struct option long_opts
[] = {
120 { "help", no_argument
, NULL
, 'h' },
121 { "components", no_argument
, NULL
, 'c' },
122 { "devices", no_argument
, NULL
, 'e' },
123 { "files", no_argument
, NULL
, 'f' },
124 { "keys", no_argument
, NULL
, 'k' },
125 { "packages", no_argument
, NULL
, 'g' },
126 { "products", no_argument
, NULL
, 'p' },
127 { "hashes", no_argument
, NULL
, 'H' },
128 { "measurements", no_argument
, NULL
, 'm' },
129 { "add", no_argument
, NULL
, 'a' },
130 { "delete", no_argument
, NULL
, 'd' },
131 { "del", no_argument
, NULL
, 'd' },
132 { "aik", required_argument
, NULL
, 'A' },
133 { "blacklist", no_argument
, NULL
, 'B' },
134 { "component", required_argument
, NULL
, 'C' },
135 { "comp", required_argument
, NULL
, 'C' },
136 { "directory", required_argument
, NULL
, 'D' },
137 { "dir", required_argument
, NULL
, 'D' },
138 { "file", required_argument
, NULL
, 'F' },
139 { "sha1-ima", no_argument
, NULL
, 'I' },
140 { "package", required_argument
, NULL
, 'G' },
141 { "key", required_argument
, NULL
, 'K' },
142 { "owner", required_argument
, NULL
, 'O' },
143 { "product", required_argument
, NULL
, 'P' },
144 { "relative", no_argument
, NULL
, 'R' },
145 { "rel", no_argument
, NULL
, 'R' },
146 { "sequence", required_argument
, NULL
, 'S' },
147 { "seq", required_argument
, NULL
, 'S' },
148 { "version", required_argument
, NULL
, 'V' },
149 { "security", no_argument
, NULL
, 'Y' },
150 { "sha1", no_argument
, NULL
, '1' },
151 { "sha256", no_argument
, NULL
, '2' },
152 { "sha384", no_argument
, NULL
, '3' },
153 { "did", required_argument
, NULL
, '4' },
154 { "fid", required_argument
, NULL
, '5' },
155 { "pid", required_argument
, NULL
, '6' },
156 { "cid", required_argument
, NULL
, '7' },
157 { "kid", required_argument
, NULL
, '8' },
158 { "gid", required_argument
, NULL
, '9' },
162 c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
192 op
= OP_MEASUREMENTS
;
202 certificate_t
*aik_cert
;
203 public_key_t
*aik_key
;
206 aik_cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
207 CERT_X509
, BUILD_FROM_FILE
, optarg
, BUILD_END
);
210 printf("AIK certificate '%s' could not be loaded\n", optarg
);
213 aik_key
= aik_cert
->get_public_key(aik_cert
);
214 aik_cert
->destroy(aik_cert
);
218 printf("AIK public key could not be retrieved\n");
221 if (!aik_key
->get_fingerprint(aik_key
, KEYID_PUBKEY_INFO_SHA1
,
224 printf("AIK fingerprint could not be computed\n");
225 aik_key
->destroy(aik_key
);
228 aik
= chunk_clone(aik
);
229 aik_key
->destroy(aik_key
);
231 if (!attest
->set_key(attest
, aik
, op
== OP_ADD
))
238 attest
->set_security(attest
, OS_PACKAGE_STATE_BLACKLIST
);
241 if (!attest
->set_component(attest
, optarg
, op
== OP_ADD
))
247 if (!attest
->set_directory(attest
, optarg
, op
== OP_ADD
))
253 if (!attest
->set_file(attest
, optarg
, op
== OP_ADD
))
259 if (!attest
->set_package(attest
, optarg
, op
== OP_ADD
))
265 attest
->set_algo(attest
, PTS_MEAS_ALGO_SHA1_IMA
);
271 aik
= chunk_from_hex(chunk_create(optarg
, strlen(optarg
)), NULL
);
272 if (!attest
->set_key(attest
, aik
, op
== OP_ADD
))
279 attest
->set_owner(attest
, optarg
);
282 if (!attest
->set_product(attest
, optarg
, op
== OP_ADD
))
288 attest
->set_relative(attest
);
291 attest
->set_sequence(attest
, atoi(optarg
));
294 if (!attest
->set_version(attest
, optarg
))
300 attest
->set_security(attest
, OS_PACKAGE_STATE_SECURITY
);
303 attest
->set_algo(attest
, PTS_MEAS_ALGO_SHA1
);
306 attest
->set_algo(attest
, PTS_MEAS_ALGO_SHA256
);
309 attest
->set_algo(attest
, PTS_MEAS_ALGO_SHA384
);
312 if (!attest
->set_did(attest
, atoi(optarg
)))
318 if (!attest
->set_fid(attest
, atoi(optarg
)))
324 if (!attest
->set_pid(attest
, atoi(optarg
)))
330 if (!attest
->set_cid(attest
, atoi(optarg
)))
336 if (!attest
->set_kid(attest
, atoi(optarg
)))
342 if (!attest
->set_gid(attest
, atoi(optarg
)))
357 attest
->list_packages(attest
);
360 attest
->list_products(attest
);
363 attest
->list_keys(attest
);
366 attest
->list_components(attest
);
369 attest
->list_devices(attest
);
372 attest
->list_files(attest
);
375 attest
->list_hashes(attest
);
377 case OP_MEASUREMENTS
:
378 attest
->list_measurements(attest
);
384 attest
->delete(attest
);
392 int main(int argc
, char *argv
[])
396 /* enable attest debugging hook */
398 openlog("attest", 0, LOG_DEBUG
);
400 atexit(library_deinit
);
402 /* initialize library */
403 if (!library_init(NULL
))
405 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY
);
407 if (!lib
->plugins
->load(lib
->plugins
, NULL
,
408 lib
->settings
->get_str(lib
->settings
, "attest.load", PLUGINS
)))
410 exit(SS_RC_INITIALIZATION_FAILED
);
413 uri
= lib
->settings
->get_str(lib
->settings
, "attest.database", NULL
);
416 fprintf(stderr
, "database URI attest.database not set.\n");
417 exit(SS_RC_INITIALIZATION_FAILED
);
419 attest
= attest_db_create(uri
);
422 exit(SS_RC_INITIALIZATION_FAILED
);