2 * @file sipe-tls-tester.c
6 * Copyright (C) 2011-12 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 with same parameters used by Lync:
35 * $ openssl s_server -accept 8443 -debug -msg \
36 * -cert server.cert -key server.pem \
37 * -tls1 -verify 0 -cipher RC4-SHA
39 * - Running the test program in another shell:
43 * You can add <host>[:<port>] to connect to a server on another machine
53 #include <sys/types.h>
54 #include <sys/socket.h>
60 #include "sipe-common.h" /* coverity[hfa: FALSE] */
61 #include "sipe-backend.h"
62 #include "sipe-cert-crypto.h"
63 #include "sipe-crypt.h"
69 gboolean
sipe_backend_debug_enabled(void)
74 void sipe_backend_debug_literal(sipe_debug_level level
,
77 printf("DEBUG(%d): %s\n", level
, msg
);
80 void sipe_backend_debug(sipe_debug_level level
,
85 gchar
*newformat
= g_strdup_printf("DEBUG(%d): %s\n", level
, format
);
88 vprintf(newformat
, ap
);
102 static guchar
*read_tls_record(int fd
,
105 GSList
*fragments
= NULL
;
106 guchar
*merged
= NULL
;
108 static gchar buffer
[10000];
111 struct pollfd fds
[] = {
115 struct record
*record
;
118 result
= poll(fds
, 1, 500 /* [milliseconds] */);
120 printf("poll failed: %s\n", strerror(errno
));
125 printf("timeout.\n");
128 printf("reading done.\n");
133 result
= read(fd
, buffer
, sizeof(buffer
));
135 printf("read failed: %s\n", strerror(errno
));
139 printf("server closed connection: %s\n",
144 printf("received %d bytes from server\n", result
);
145 record
= g_new0(struct record
, 1);
146 record
->length
= result
;
147 record
->msg
= g_memdup(buffer
, result
);
149 fragments
= g_slist_append(fragments
, record
);
153 GSList
*elem
= fragments
;
156 printf("received a total of %" G_GSIZE_FORMAT
" bytes.\n",
159 p
= merged
= g_malloc(length
);
161 struct record
*record
= elem
->data
;
163 memcpy(p
, record
->msg
, record
->length
);
171 g_slist_free(fragments
);
178 static void tls_handshake(struct sipe_tls_state
*state
,
181 printf("TLS handshake starting...\n");
183 /* generate next handshake message */
184 while (sipe_tls_next(state
)) {
187 /* handshake completed? */
188 if (!state
->out_buffer
) {
189 printf("Handshake completed.\n");
193 /* send buffer to server */
194 sent
= write(fd
, state
->out_buffer
, state
->out_length
);
196 printf("write to server failed: %s\n",
199 } else if ((unsigned int) sent
< state
->out_length
) {
200 printf("could only write %d bytes, out of %" G_GSIZE_FORMAT
"\n",
201 sent
, state
->out_length
);
205 /* message sent, drop buffer */
206 g_free(state
->out_buffer
);
207 state
->out_buffer
= NULL
;
209 state
->in_buffer
= read_tls_record(fd
, &state
->in_length
);
210 if (!state
->in_buffer
) {
211 printf("end of data.\n");
216 printf("TLS handshake done.\n");
220 static int tls_connect(const gchar
*param
)
222 gchar
**parts
= g_strsplit(param
, ":", 2);
226 const gchar
*host
= parts
[0];
227 const gchar
*port
= parts
[1] ? parts
[1] : "443";
228 struct addrinfo hints
;
229 struct addrinfo
*result
;
232 printf("TLS connect to host '%s', port %s...\n",
235 memset(&hints
, 0, sizeof(struct addrinfo
));
236 hints
.ai_family
= AF_UNSPEC
;
237 hints
.ai_socktype
= SOCK_STREAM
;
239 hints
.ai_protocol
= 0;
240 status
= getaddrinfo(host
, port
, &hints
, &result
);
245 for (rp
= result
; rp
!= NULL
; rp
= rp
->ai_next
) {
246 int sock
= socket(rp
->ai_family
,
250 if (sock
< 0) continue;
254 rp
->ai_addrlen
) >= 0) {
256 printf("connected to host '%s', port %s.\n",
261 fprintf(stderr
, "failed to connect: %s\n",
266 freeaddrinfo(result
);
269 fprintf(stderr
, "couldn't connect to host '%s'!\n",
273 fprintf(stderr
, "couldn't find host '%s': %s\n",
274 host
, gai_strerror(status
));
277 fprintf(stderr
, "corrupted host[:port] '%s'!\n", param
);
284 int main(int argc
, char *argv
[])
286 struct sipe_cert_crypto
*scc
;
288 sipe_crypto_init(FALSE
);
291 scc
= sipe_cert_crypto_init();
293 gpointer certificate
;
294 struct sipe_tls_state
*state
;
296 printf("SIPE cert crypto backend initialized.\n");
298 certificate
= sipe_cert_crypto_test_certificate(scc
);
299 state
= sipe_tls_start(certificate
);
303 printf("SIPE TLS initialized.\n");
305 fd
= tls_connect((argc
> 1) ? argv
[1] : "localhost:8443");
307 tls_handshake(state
, fd
);
311 sipe_tls_free(state
);
314 sipe_cert_crypto_destroy(certificate
);
315 sipe_cert_crypto_free(scc
);
317 sipe_crypto_shutdown();