tls: start on TLS test program
[siplcs.git] / src / core / sipe-tls-tester.c
blob15aa83f6508784356402f10915586e31a432933b
1 /**
2 * @file sipe-tls-tester.c
4 * pidgin-sipe
6 * Copyright (C) 2011 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * TLS handshake implementation (sipe-tls.c) tester
26 * Example test setup using OpenSSL:
28 * - Setting up the server certificate:
30 * $ openssl req -new -keyout server.pem -out server.req
31 * $ openssl x509 -req -in server.req -signkey server.pem -out server.cert
33 * - Running the test server in one shell:
35 * $ openssl s_server -accept 8443 -debug -tls1 -cert server.cert \
36 * -key server.pem
38 * - Running the test program in another shell:
40 * $ sipe_tls_tester
42 * You can add <host>[:<port>] to connect to a server on another machine
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <stdarg.h>
49 #include <time.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netdb.h>
56 #include <glib.h>
58 #include "sipe-common.h"
59 #include "sipe-backend.h"
60 #include "sipe-cert-crypto.h"
61 #include "sipe-crypt.h"
62 #include "sipe-tls.h"
65 * Stubs
67 gboolean sipe_backend_debug_enabled(void)
69 return(TRUE);
72 void sipe_backend_debug_literal(sipe_debug_level level,
73 const gchar *msg)
75 printf("DEBUG(%d): %s\n", level, msg);
78 void sipe_backend_debug(sipe_debug_level level,
79 const gchar *format,
80 ...)
82 va_list ap;
83 gchar *newformat = g_strdup_printf("DEBUG(%d): %s\n", level, format);
85 va_start(ap, format);
86 vprintf(newformat, ap);
87 va_end(ap);
89 g_free(newformat);
93 * Tester code
95 static int tls_connect(const gchar *param)
97 gchar **parts = g_strsplit(param, ":", 2);
98 int fd = -1;
100 if (parts[0]) {
101 const gchar *host = parts[0];
102 const gchar *port = parts[1] ? parts[1] : "443";
103 struct addrinfo hints;
104 struct addrinfo *result;
105 int status;
107 printf("TLS connect to host '%s', port %s...\n",
108 host, port);
110 memset(&hints, 0, sizeof(struct addrinfo));
111 hints.ai_family = AF_UNSPEC;
112 hints.ai_socktype = SOCK_STREAM;
113 hints.ai_flags = 0;
114 hints.ai_protocol = 0;
115 status = getaddrinfo(host, port, &hints, &result);
117 if (status == 0) {
118 struct addrinfo *rp;
120 for (rp = result; rp != NULL; rp = rp->ai_next) {
121 int sock = socket(rp->ai_family,
122 rp->ai_socktype,
123 rp->ai_protocol);
125 if (sock < 0) continue;
127 if (connect(sock,
128 rp->ai_addr,
129 rp->ai_addrlen) >= 0) {
130 /* connected */
131 printf("connected to host '%s', port %s.\n",
132 host, port);
133 fd = sock;
134 break;
136 fprintf(stderr, "failed to connect: %s\n",
137 strerror(errno));
139 close(sock);
141 freeaddrinfo(result);
143 if (rp == NULL) {
144 fprintf(stderr, "couldn't connect to host '%s'!\n",
145 host);
147 } else {
148 fprintf(stderr, "couldn't find host '%s': %s\n",
149 host, gai_strerror(status));
151 } else {
152 fprintf(stderr, "corrupted host[:port] '%s'!\n", param);
154 g_strfreev(parts);
156 return(fd);
159 int main(int argc, char *argv[])
161 int fd;
163 sipe_crypto_init(FALSE);
164 srand(time(NULL));
166 fd = tls_connect((argc > 1) ? argv[1] : "localhost:8443");
167 if (fd >= 0) {
168 struct sipe_tls_state *state = sipe_tls_start(NULL);
170 if (state) {
171 printf("starting TLS handshake...\n");
173 sipe_tls_free(state);
176 close(fd);
179 sipe_crypto_shutdown();
180 return(0);
184 Local Variables:
185 mode: c
186 c-file-style: "bsd"
187 indent-tabs-mode: t
188 tab-width: 8
189 End: