2 * Copyright (C) 2010-2013 Martin Willi, revosec AG
3 * Copyright (C) 2013-2015 Andreas Steffen
4 * HSR Hochschule für Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include <sys/types.h>
29 #include <pt_tls_client.h>
34 #include <utils/debug.h>
35 #include <credentials/sets/mem_cred.h>
36 #include <utils/optionsfrom.h>
39 * Print usage information
41 static void usage(FILE *out
)
44 "Usage: pt-tls --connect <hostname|address> [--port <port>]\n"
45 " [--cert <file>]+ [--keyid <hex>|--key <file>]\n"
46 " [--key-type rsa|ecdsa] [--client <client-id>]\n"
47 " [--secret <password>] [--optionsfrom <filename>]\n"
48 " [--quiet] [--debug <level>]\n");
54 static int client(char *address
, uint16_t port
, char *identity
)
56 pt_tls_client_t
*assessment
;
58 identification_t
*server_id
, *client_id
;
59 host_t
*server_ip
, *client_ip
;
62 server_ip
= host_create_from_dns(address
, AF_UNSPEC
, port
);
68 client_ip
= host_create_any(server_ip
->get_family(server_ip
));
71 server_ip
->destroy(server_ip
);
74 server_id
= identification_create_from_string(address
);
75 client_id
= identification_create_from_string(identity
);
77 tnccs
= (tls_t
*)tnc
->tnccs
->create_instance(tnc
->tnccs
, TNCCS_2_0
, FALSE
,
78 server_id
, client_id
, server_ip
, client_ip
,
79 TNC_IFT_TLS_2_0
, NULL
);
80 client_ip
->destroy(client_ip
);
84 fprintf(stderr
, "loading TNCCS failed: %s\n", PLUGINS
);
85 server_ip
->destroy(server_ip
);
86 server_id
->destroy(server_id
);
87 client_id
->destroy(client_id
);
90 assessment
= pt_tls_client_create(server_ip
, server_id
, client_id
);
91 status
= assessment
->run_assessment(assessment
, (tnccs_t
*)tnccs
);
92 assessment
->destroy(assessment
);
93 tnccs
->destroy(tnccs
);
95 return (status
!= SUCCESS
);
100 * In-Memory credential set
102 static mem_cred_t
*creds
;
105 * Load certificate from file
107 static bool load_certificate(char *filename
)
111 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
112 BUILD_FROM_FILE
, filename
, BUILD_END
);
115 DBG1(DBG_TLS
, "loading certificate from '%s' failed", filename
);
118 creds
->add_cert(creds
, TRUE
, cert
);
123 * Load private key from file
125 static bool load_key(char *keyid
, char *filename
, key_type_t type
)
132 chunk
= chunk_from_hex(chunk_create(keyid
, strlen(keyid
)), NULL
);
133 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_ANY
,
134 BUILD_PKCS11_KEYID
, chunk
, BUILD_END
);
139 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
140 BUILD_FROM_FILE
, filename
, BUILD_END
);
144 DBG1(DBG_TLS
, "loading key from '%s' failed", keyid ? keyid
: filename
);
147 creds
->add_key(creds
, key
);
152 * Logging and debug level
154 static bool log_to_stderr
= TRUE
;
156 static bool log_to_syslog
= TRUE
;
157 #endif /* HAVE_SYSLOG */
158 static level_t default_loglevel
= 1;
160 static void dbg_pt_tls(debug_t group
, level_t level
, char *fmt
, ...)
164 if (level
<= default_loglevel
)
169 vfprintf(stderr
, fmt
, args
);
171 fprintf(stderr
, "\n");
177 char *current
= buffer
, *next
;
179 /* write in memory buffer first */
181 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
184 /* do a syslog with every line */
187 next
= strchr(current
, '\n');
192 syslog(LOG_INFO
, "%s\n", current
);
196 #endif /* HAVE_SYSLOG */
201 * Initialize logging to stderr/syslog
203 static void init_log(const char *program
)
209 setbuf(stderr
, NULL
);
214 openlog(program
, LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_AUTHPRIV
);
216 #endif /* HAVE_SYSLOG */
220 * Handles --optionsfrom arguments
227 static void cleanup()
229 lib
->processor
->cancel(lib
->processor
);
230 lib
->credmgr
->remove_set(lib
->credmgr
, &creds
->set
);
231 creds
->destroy(creds
);
232 options
->destroy(options
);
242 plugin_feature_t features
[] = {
244 PLUGIN_PROVIDE(CUSTOM
, "pt-tls-client"),
245 PLUGIN_DEPENDS(CUSTOM
, "tnccs-manager"),
247 library_init(NULL
, "pt-tls-client");
250 init_log("pt-tls-client");
251 options
= options_create();
253 lib
->plugins
->add_static_features(lib
->plugins
, "pt-tls-client", features
,
254 countof(features
), TRUE
, NULL
, NULL
);
255 if (!lib
->plugins
->load(lib
->plugins
,
256 lib
->settings
->get_str(lib
->settings
, "pt-tls-client.load", PLUGINS
)))
258 exit(SS_RC_INITIALIZATION_FAILED
);
260 lib
->plugins
->status(lib
->plugins
, LEVEL_CTRL
);
262 creds
= mem_cred_create();
263 lib
->credmgr
->add_set(lib
->credmgr
, &creds
->set
);
268 int main(int argc
, char *argv
[])
270 char *address
= NULL
, *identity
= "%any", *secret
= NULL
;
271 char *keyid
= NULL
, *key_file
= NULL
;
272 key_type_t key_type
= KEY_RSA
;
273 int port
= PT_TLS_PORT
;
279 struct option long_opts
[] = {
280 {"help", no_argument
, NULL
, 'h' },
281 {"connect", required_argument
, NULL
, 'c' },
282 {"client", required_argument
, NULL
, 'i' },
283 {"secret", required_argument
, NULL
, 's' },
284 {"port", required_argument
, NULL
, 'p' },
285 {"cert", required_argument
, NULL
, 'x' },
286 {"keyid", required_argument
, NULL
, 'K' },
287 {"key", required_argument
, NULL
, 'k' },
288 {"key-type", required_argument
, NULL
, 't' },
289 {"mutual", no_argument
, NULL
, 'm' },
290 {"quiet", no_argument
, NULL
, 'q' },
291 {"debug", required_argument
, NULL
, 'd' },
292 {"optionsfrom", required_argument
, NULL
, '+' },
295 switch (getopt_long(argc
, argv
, "", long_opts
, NULL
))
299 case 'h': /* --help */
302 case 'x': /* --cert <file> */
303 if (!load_certificate(optarg
))
308 case 'K': /* --keyid <hex> */
311 case 'k': /* --key <file> */
314 case 't': /* --key-type <type> */
315 if (strcaseeq(optarg
, "ecdsa"))
317 key_type
= KEY_ECDSA
;
319 else if (strcaseeq(optarg
, "rsa"))
328 case 'c': /* --connect <hostname|address> */
336 case 'i': /* --client <client-id> */
339 case 's': /* --secret <password> */
342 case 'p': /* --port <port> */
345 case 'm': /* --mutual */
346 lib
->settings
->set_bool(lib
->settings
,
347 "%s.plugins.tnccs-20.mutual", TRUE
, lib
->ns
);
349 case 'q': /* --quiet */
350 log_to_stderr
= FALSE
;
352 case 'd': /* --debug <level> */
353 default_loglevel
= atoi(optarg
);
355 case '+': /* --optionsfrom <filename> */
356 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
372 if ((keyid
|| key_file
) && !load_key(keyid
, key_file
, key_type
))
378 creds
->add_shared(creds
, shared_key_create(SHARED_EAP
,
379 chunk_clone(chunk_from_str(secret
))),
380 identification_create_from_string(identity
), NULL
);
382 return client(address
, port
, identity
);