2 * Copyright (C) 2010-2013 Martin Willi, revosec AG
3 * Copyright (C) 2013-2014 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>
20 #include <sys/socket.h>
28 #include <pt_tls_client.h>
33 #include <utils/debug.h>
34 #include <credentials/sets/mem_cred.h>
35 #include <utils/optionsfrom.h>
38 * Print usage information
40 static void usage(FILE *out
)
43 "Usage: pt-tls --connect <hostname|address> [--port <port>]\n"
44 " [--cert <file>]+ [--key <file>]\n"
45 " [--client <client-id>] [--secret <password>]\n"
46 " [--optionsfrom <filename>] [--quiet] [--debug <level>]\n");
52 static int client(char *address
, u_int16_t port
, char *identity
)
54 pt_tls_client_t
*assessment
;
56 identification_t
*server
, *client
;
60 host
= host_create_from_dns(address
, AF_UNSPEC
, port
);
65 server
= identification_create_from_string(address
);
66 client
= identification_create_from_string(identity
);
67 tnccs
= (tls_t
*)tnc
->tnccs
->create_instance(tnc
->tnccs
, TNCCS_2_0
, FALSE
,
68 server
, client
, TNC_IFT_TLS_2_0
, NULL
);
71 fprintf(stderr
, "loading TNCCS failed: %s\n", PLUGINS
);
73 server
->destroy(server
);
74 client
->destroy(client
);
77 assessment
= pt_tls_client_create(host
, server
, client
);
78 status
= assessment
->run_assessment(assessment
, (tnccs_t
*)tnccs
);
79 assessment
->destroy(assessment
);
80 tnccs
->destroy(tnccs
);
86 * In-Memory credential set
88 static mem_cred_t
*creds
;
91 * Load certificate from file
93 static bool load_certificate(char *filename
)
97 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
98 BUILD_FROM_FILE
, filename
, BUILD_END
);
101 DBG1(DBG_TLS
, "loading certificate from '%s' failed", filename
);
104 creds
->add_cert(creds
, TRUE
, cert
);
109 * Load private key from file
111 static bool load_key(char *filename
)
115 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_RSA
,
116 BUILD_FROM_FILE
, filename
, BUILD_END
);
119 DBG1(DBG_TLS
, "loading key from '%s' failed", filename
);
122 creds
->add_key(creds
, key
);
127 * Logging and debug level
129 static bool log_to_stderr
= TRUE
;
130 static bool log_to_syslog
= TRUE
;
131 static level_t default_loglevel
= 1;
133 static void dbg_pt_tls(debug_t group
, level_t level
, char *fmt
, ...)
136 char *current
= buffer
, *next
;
139 if (level
<= default_loglevel
)
144 vfprintf(stderr
, fmt
, args
);
146 fprintf(stderr
, "\n");
150 /* write in memory buffer first */
152 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
155 /* do a syslog with every line */
158 next
= strchr(current
, '\n');
163 syslog(LOG_INFO
, "%s\n", current
);
171 * Initialize logging to stderr/syslog
173 static void init_log(const char *program
)
179 setbuf(stderr
, NULL
);
183 openlog(program
, LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_AUTHPRIV
);
188 * Handles --optionsfrom arguments
195 static void cleanup()
197 lib
->processor
->cancel(lib
->processor
);
198 lib
->credmgr
->remove_set(lib
->credmgr
, &creds
->set
);
199 creds
->destroy(creds
);
200 options
->destroy(options
);
210 plugin_feature_t features
[] = {
212 PLUGIN_PROVIDE(CUSTOM
, "pt-tls-client"),
213 PLUGIN_DEPENDS(CUSTOM
, "tnccs-manager"),
215 library_init(NULL
, "pt-tls-client");
218 init_log("pt-tls-client");
219 options
= options_create();
221 lib
->plugins
->add_static_features(lib
->plugins
, "pt-tls-client", features
,
222 countof(features
), TRUE
);
223 if (!lib
->plugins
->load(lib
->plugins
,
224 lib
->settings
->get_str(lib
->settings
, "pt-tls-client.load", PLUGINS
)))
226 exit(SS_RC_INITIALIZATION_FAILED
);
228 lib
->plugins
->status(lib
->plugins
, LEVEL_CTRL
);
230 creds
= mem_cred_create();
231 lib
->credmgr
->add_set(lib
->credmgr
, &creds
->set
);
236 int main(int argc
, char *argv
[])
238 char *address
= NULL
, *identity
= "%any", *secret
= NULL
;
239 int port
= PT_TLS_PORT
;
245 struct option long_opts
[] = {
246 {"help", no_argument
, NULL
, 'h' },
247 {"connect", required_argument
, NULL
, 'c' },
248 {"client", required_argument
, NULL
, 'i' },
249 {"secret", required_argument
, NULL
, 's' },
250 {"port", required_argument
, NULL
, 'p' },
251 {"cert", required_argument
, NULL
, 'x' },
252 {"key", required_argument
, NULL
, 'k' },
253 {"quiet", no_argument
, NULL
, 'q' },
254 {"debug", required_argument
, NULL
, 'd' },
255 {"optionsfrom", required_argument
, NULL
, '+' },
258 switch (getopt_long(argc
, argv
, "", long_opts
, NULL
))
262 case 'h': /* --help */
265 case 'x': /* --cert <file> */
266 if (!load_certificate(optarg
))
271 case 'k': /* --key <file> */
272 if (!load_key(optarg
))
277 case 'c': /* --connect <hostname|address> */
285 case 'i': /* --client <client-id> */
288 case 's': /* --secret <password> */
291 case 'p': /* --port <port> */
294 case 'q': /* --quiet */
295 log_to_stderr
= FALSE
;
297 case 'd': /* --debug <level> */
298 default_loglevel
= atoi(optarg
);
300 case '+': /* --optionsfrom <filename> */
301 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
319 creds
->add_shared(creds
, shared_key_create(SHARED_EAP
,
320 chunk_clone(chunk_from_str(secret
))),
321 identification_create_from_string(identity
), NULL
);
324 return client(address
, port
, identity
);