2 * @file sipe-tls-tester.c
6 * Copyright (C) 2011-2016 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 <c1>[:<c2>...] ]
39 * ciphers: RC4-MD5, RC4-SHA, AES128-SHA, AES256-SHA
41 * - Running the test program in another shell:
45 * You can add <host>[:<port>] to connect to a server on another machine
55 #include <sys/types.h>
56 #include <sys/socket.h>
62 #include "sipe-common.h" /* coverity[hfa: FALSE] */
63 #include "sipe-backend.h"
64 #include "sipe-cert-crypto.h"
65 #include "sipe-crypt.h"
71 gboolean
sipe_backend_debug_enabled(void)
76 void sipe_backend_debug_literal(sipe_debug_level level
,
79 printf("DEBUG(%d): %s\n", level
, msg
);
82 void sipe_backend_debug(sipe_debug_level level
,
87 gchar
*newformat
= g_strdup_printf("DEBUG(%d): %s\n", level
, format
);
90 vprintf(newformat
, ap
);
96 /* needed when linking against NSS */
97 void md4sum(const guchar
*data
, gsize length
, guchar
*digest
);
98 void md4sum(SIPE_UNUSED_PARAMETER
const guchar
*data
,
99 SIPE_UNUSED_PARAMETER gsize length
,
100 SIPE_UNUSED_PARAMETER guchar
*digest
)
112 static guchar
*read_tls_record(int fd
,
115 GSList
*fragments
= NULL
;
116 guchar
*merged
= NULL
;
118 static gchar buffer
[10000];
121 struct pollfd fds
[] = {
125 struct record
*record
;
128 result
= poll(fds
, 1, 500 /* [milliseconds] */);
130 printf("poll failed: %s\n", strerror(errno
));
135 printf("timeout.\n");
138 printf("reading done.\n");
143 result
= read(fd
, buffer
, sizeof(buffer
));
145 printf("read failed: %s\n", strerror(errno
));
149 printf("server closed connection: %s\n",
154 printf("received %d bytes from server\n", result
);
155 record
= g_new0(struct record
, 1);
156 record
->length
= result
;
157 record
->msg
= g_memdup(buffer
, result
);
159 fragments
= g_slist_append(fragments
, record
);
163 GSList
*elem
= fragments
;
166 printf("received a total of %" G_GSIZE_FORMAT
" bytes.\n",
169 p
= merged
= g_malloc(length
);
172 struct record
*record
= elem
->data
;
174 memcpy(p
, record
->msg
, record
->length
);
182 printf("can't allocate %" G_GSIZE_FORMAT
" bytes.\n",
186 g_slist_free(fragments
);
193 static void tls_handshake(struct sipe_tls_state
*state
,
196 gboolean success
= FALSE
;
198 printf("TLS handshake starting...\n");
200 /* generate next handshake message */
201 while (sipe_tls_next(state
)) {
204 /* handshake completed? */
205 if (!state
->out_buffer
) {
210 /* send buffer to server */
211 sent
= write(fd
, state
->out_buffer
, state
->out_length
);
213 printf("write to server failed: %s\n",
216 } else if ((unsigned int) sent
< state
->out_length
) {
217 printf("could only write %d bytes, out of %" G_GSIZE_FORMAT
"\n",
218 sent
, state
->out_length
);
222 /* message sent, drop buffer */
223 g_free(state
->out_buffer
);
224 state
->out_buffer
= NULL
;
226 state
->in_buffer
= read_tls_record(fd
, &state
->in_length
);
227 if (!state
->in_buffer
) {
228 printf("end of data.\n");
233 printf("TLS handshake %s.\n", success
? "SUCCESSFUL" : "FAILED");
237 static int tls_connect(const gchar
*param
)
239 gchar
**parts
= g_strsplit(param
, ":", 2);
243 const gchar
*host
= parts
[0];
244 const gchar
*port
= parts
[1] ? parts
[1] : "443";
245 struct addrinfo hints
;
246 struct addrinfo
*result
;
249 printf("TLS connect to host '%s', port %s...\n",
252 memset(&hints
, 0, sizeof(struct addrinfo
));
253 hints
.ai_family
= AF_UNSPEC
;
254 hints
.ai_socktype
= SOCK_STREAM
;
256 hints
.ai_protocol
= 0;
257 status
= getaddrinfo(host
, port
, &hints
, &result
);
262 for (rp
= result
; rp
!= NULL
; rp
= rp
->ai_next
) {
263 int sock
= socket(rp
->ai_family
,
267 if (sock
< 0) continue;
271 rp
->ai_addrlen
) >= 0) {
273 printf("connected to host '%s', port %s.\n",
278 fprintf(stderr
, "failed to connect: %s\n",
283 freeaddrinfo(result
);
286 fprintf(stderr
, "couldn't connect to host '%s'!\n",
290 fprintf(stderr
, "couldn't find host '%s': %s\n",
291 host
, gai_strerror(status
));
294 fprintf(stderr
, "corrupted host[:port] '%s'!\n", param
);
301 int main(int argc
, char *argv
[])
303 struct sipe_cert_crypto
*scc
;
305 sipe_crypto_init(FALSE
);
308 scc
= sipe_cert_crypto_init();
310 gpointer certificate
;
311 struct sipe_tls_state
*state
;
313 printf("SIPE cert crypto backend initialized.\n");
315 certificate
= sipe_cert_crypto_test_certificate(scc
);
316 state
= sipe_tls_start(certificate
);
320 printf("SIPE TLS initialized.\n");
322 fd
= tls_connect((argc
> 1) ? argv
[1] : "localhost:8443");
324 tls_handshake(state
, fd
);
328 sipe_tls_free(state
);
331 sipe_cert_crypto_destroy(certificate
);
332 sipe_cert_crypto_free(scc
);
334 sipe_crypto_shutdown();