]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - scripts/tls_test.c
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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
18 #include <sys/types.h>
19 #include <sys/socket.h>
26 #include <tls_socket.h>
27 #include <utils/host.h>
28 #include <credentials/sets/mem_cred.h>
31 * Print usage information
33 static void usage(FILE *out
, char *cmd
)
35 fprintf(out
, "usage:\n");
36 fprintf(out
, " %s --connect <address> --port <port> [--cert <file>]+ [--times <n>]\n", cmd
);
37 fprintf(out
, " %s --listen <address> --port <port> --key <key> [--cert <file>]+ [--times <n>]\n", cmd
);
41 * Stream between stdio and TLS socket
43 static int stream(int fd
, tls_socket_t
*tls
)
54 if (select(fd
+ 1, &set
, NULL
, NULL
, NULL
) == -1)
58 if (FD_ISSET(fd
, &set
))
60 if (!tls
->read(tls
, &data
))
66 ignore_result(write(1, data
.ptr
, data
.len
));
70 if (FD_ISSET(0, &set
))
75 len
= read(0, buf
, sizeof(buf
));
82 if (!tls
->write(tls
, chunk_create(buf
, len
)))
84 DBG1(DBG_TLS
, "TLS write error");
95 static int client(host_t
*host
, identification_t
*server
,
96 int times
, tls_cache_t
*cache
)
101 while (times
== -1 || times
-- > 0)
103 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
106 DBG1(DBG_TLS
, "opening socket failed: %s", strerror(errno
));
109 if (connect(fd
, host
->get_sockaddr(host
),
110 *host
->get_sockaddr_len(host
)) == -1)
112 DBG1(DBG_TLS
, "connecting to %#H failed: %s", host
, strerror(errno
));
116 tls
= tls_socket_create(FALSE
, server
, NULL
, fd
, cache
);
122 res
= stream(fd
, tls
);
136 static int serve(host_t
*host
, identification_t
*server
,
137 int times
, tls_cache_t
*cache
)
142 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
145 DBG1(DBG_TLS
, "opening socket failed: %s", strerror(errno
));
148 if (bind(fd
, host
->get_sockaddr(host
),
149 *host
->get_sockaddr_len(host
)) == -1)
151 DBG1(DBG_TLS
, "binding to %#H failed: %s", host
, strerror(errno
));
155 if (listen(fd
, 1) == -1)
157 DBG1(DBG_TLS
, "listen to %#H failed: %m", host
, strerror(errno
));
162 while (times
== -1 || times
-- > 0)
164 cfd
= accept(fd
, host
->get_sockaddr(host
), host
->get_sockaddr_len(host
));
167 DBG1(DBG_TLS
, "accept failed: %s", strerror(errno
));
171 DBG1(DBG_TLS
, "%#H connected", host
);
173 tls
= tls_socket_create(TRUE
, server
, NULL
, cfd
, cache
);
180 DBG1(DBG_TLS
, "%#H disconnected", host
);
189 * In-Memory credential set
191 static mem_cred_t
*creds
;
194 * Load certificate from file
196 static bool load_certificate(char *filename
)
200 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
201 BUILD_FROM_FILE
, filename
, BUILD_END
);
204 DBG1(DBG_TLS
, "loading certificate from '%s' failed", filename
);
207 creds
->add_cert(creds
, TRUE
, cert
);
212 * Load private key from file
214 static bool load_key(char *filename
)
218 key
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_RSA
,
219 BUILD_FROM_FILE
, filename
, BUILD_END
);
222 DBG1(DBG_TLS
, "loading key from '%s' failed", filename
);
225 creds
->add_key(creds
, key
);
232 static level_t tls_level
= 1;
234 static void dbg_tls(debug_t group
, level_t level
, char *fmt
, ...)
236 if ((group
== DBG_TLS
&& level
<= tls_level
) || level
<= 1)
241 vfprintf(stderr
, fmt
, args
);
242 fprintf(stderr
, "\n");
250 static void cleanup()
252 lib
->credmgr
->remove_set(lib
->credmgr
, &creds
->set
);
253 creds
->destroy(creds
);
266 lib
->plugins
->load(lib
->plugins
, NULL
, PLUGINS
);
268 creds
= mem_cred_create();
269 lib
->credmgr
->add_set(lib
->credmgr
, &creds
->set
);
274 int main(int argc
, char *argv
[])
276 char *address
= NULL
;
278 int port
= 0, times
= -1, res
;
279 identification_t
*server
;
287 struct option long_opts
[] = {
288 {"help", no_argument
, NULL
, 'h' },
289 {"connect", required_argument
, NULL
, 'c' },
290 {"listen", required_argument
, NULL
, 'l' },
291 {"port", required_argument
, NULL
, 'p' },
292 {"cert", required_argument
, NULL
, 'x' },
293 {"key", required_argument
, NULL
, 'k' },
294 {"times", required_argument
, NULL
, 't' },
295 {"debug", required_argument
, NULL
, 'd' },
298 switch (getopt_long(argc
, argv
, "", long_opts
, NULL
))
303 usage(stdout
, argv
[0]);
306 if (!load_certificate(optarg
))
312 if (!load_key(optarg
))
323 usage(stderr
, argv
[0]);
332 times
= atoi(optarg
);
335 tls_level
= atoi(optarg
);
338 usage(stderr
, argv
[0]);
343 if (!port
|| !address
)
345 usage(stderr
, argv
[0]);
348 host
= host_create_from_dns(address
, 0, port
);
351 DBG1(DBG_TLS
, "resolving hostname %s failed", address
);
354 server
= identification_create_from_string(address
);
355 cache
= tls_cache_create(100, 30);
358 res
= serve(host
, server
, times
, cache
);
362 res
= client(host
, server
, times
, cache
);
364 cache
->destroy(cache
);
366 server
->destroy(server
);