2 * @file sipe-tls-tester.c
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 \
38 * - Running the test program in another shell:
42 * You can add <host>[:<port>] to connect to a server on another machine
52 #include <sys/types.h>
53 #include <sys/socket.h>
58 #include "sipe-common.h"
59 #include "sipe-backend.h"
60 #include "sipe-cert-crypto.h"
61 #include "sipe-crypt.h"
67 gboolean
sipe_backend_debug_enabled(void)
72 void sipe_backend_debug_literal(sipe_debug_level level
,
75 printf("DEBUG(%d): %s\n", level
, msg
);
78 void sipe_backend_debug(sipe_debug_level level
,
83 gchar
*newformat
= g_strdup_printf("DEBUG(%d): %s\n", level
, format
);
86 vprintf(newformat
, ap
);
95 static int tls_connect(const gchar
*param
)
97 gchar
**parts
= g_strsplit(param
, ":", 2);
101 const gchar
*host
= parts
[0];
102 const gchar
*port
= parts
[1] ? parts
[1] : "443";
103 struct addrinfo hints
;
104 struct addrinfo
*result
;
107 printf("TLS connect to host '%s', port %s...\n",
110 memset(&hints
, 0, sizeof(struct addrinfo
));
111 hints
.ai_family
= AF_UNSPEC
;
112 hints
.ai_socktype
= SOCK_STREAM
;
114 hints
.ai_protocol
= 0;
115 status
= getaddrinfo(host
, port
, &hints
, &result
);
120 for (rp
= result
; rp
!= NULL
; rp
= rp
->ai_next
) {
121 int sock
= socket(rp
->ai_family
,
125 if (sock
< 0) continue;
129 rp
->ai_addrlen
) >= 0) {
131 printf("connected to host '%s', port %s.\n",
136 fprintf(stderr
, "failed to connect: %s\n",
141 freeaddrinfo(result
);
144 fprintf(stderr
, "couldn't connect to host '%s'!\n",
148 fprintf(stderr
, "couldn't find host '%s': %s\n",
149 host
, gai_strerror(status
));
152 fprintf(stderr
, "corrupted host[:port] '%s'!\n", param
);
159 int main(int argc
, char *argv
[])
163 sipe_crypto_init(FALSE
);
166 fd
= tls_connect((argc
> 1) ? argv
[1] : "localhost:8443");
168 struct sipe_tls_state
*state
= sipe_tls_start(NULL
);
171 printf("starting TLS handshake...\n");
173 sipe_tls_free(state
);
179 sipe_crypto_shutdown();