Merge branch 'upstream' into tizen
[platform/upstream/gnutls.git] / tests / mini-tls-nonblock.c
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #if defined(_WIN32)
31
32 int main()
33 {
34         exit(77);
35 }
36
37 #else
38
39 #include <string.h>
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <sys/wait.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <gnutls/gnutls.h>
48 #include <gnutls/dtls.h>
49 #include <signal.h>
50
51 #include "utils.h"
52
53 static void terminate(void);
54
55 /* This program tests whether our pull timeout function is called
56  * during handshake.
57  */
58
59 static void server_log_func(int level, const char *str)
60 {
61 //  fprintf (stderr, "server|<%d>| %s", level, str);
62 }
63
64 static void client_log_func(int level, const char *str)
65 {
66         fprintf(stderr, "client|<%d>| %s", level, str);
67 }
68
69 static unsigned char server_cert_pem[] =
70     "-----BEGIN CERTIFICATE-----\n"
71     "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
72     "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
73     "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
74     "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
75     "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
76     "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
77     "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
78     "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
79     "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
80     "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
81     "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
82     "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
83     "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
84
85 const gnutls_datum_t server_cert = { server_cert_pem,
86         sizeof(server_cert_pem)
87 };
88
89 static unsigned char server_key_pem[] =
90     "-----BEGIN RSA PRIVATE KEY-----\n"
91     "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
92     "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
93     "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
94     "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
95     "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
96     "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
97     "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
98     "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
99     "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
100     "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
101     "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
102     "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
103     "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
104     "-----END RSA PRIVATE KEY-----\n";
105
106 const gnutls_datum_t server_key = { server_key_pem,
107         sizeof(server_key_pem)
108 };
109
110
111 /* A very basic TLS client, with anonymous authentication.
112  */
113
114 #define MAX_BUF 1024
115 static const char *g_msg = "";
116
117 static ssize_t
118 my_pull(gnutls_transport_ptr_t tr, void *data, size_t len)
119 {
120         return recv((long)tr, data, len, 0);
121 }
122
123 static ssize_t
124 my_push(gnutls_transport_ptr_t tr, const void *data, size_t len)
125 {
126         return send((long)tr, data, len, 0);
127 }
128
129 static int
130 my_pull_timeout(gnutls_transport_ptr_t tr, unsigned ms)
131 {
132         if (ms != 0) {
133                 fail("pull timeout was called: %s!\n", g_msg);
134                 exit(1);
135         }
136         return 1;
137 }
138
139 static void client(int fd, const char *msg, unsigned expl)
140 {
141         int ret;
142         gnutls_anon_client_credentials_t anoncred;
143         gnutls_certificate_credentials_t x509_cred;
144         gnutls_session_t session;
145         /* Need to enable anonymous KX specifically. */
146
147         global_init();
148         g_msg = msg;
149
150         if (debug) {
151                 gnutls_global_set_log_function(client_log_func);
152                 gnutls_global_set_log_level(7);
153         }
154
155         /* set socket to non-blocking */
156         ret = fcntl(fd, F_GETFL, 0);
157         fcntl(fd, F_SETFL, ret | O_NONBLOCK);
158
159         gnutls_anon_allocate_client_credentials(&anoncred);
160         gnutls_certificate_allocate_credentials(&x509_cred);
161
162         /* Initialize TLS session
163          */
164         gnutls_init(&session, GNUTLS_CLIENT|expl);
165
166         /* Use default priorities */
167         ret = gnutls_priority_set_direct(session, "NORMAL", NULL);
168         if (ret < 0) {
169                 fail("error in setting priority\n");
170                 exit(1);
171         }
172
173         /* put the anonymous credentials to the current session
174          */
175         gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
176         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
177
178         gnutls_transport_set_int(session, fd);
179         gnutls_transport_set_push_function(session, my_push);
180         gnutls_transport_set_pull_function(session, my_pull);
181         gnutls_transport_set_pull_timeout_function(session, my_pull_timeout);
182
183         /* Perform the TLS handshake
184          */
185         do {
186                 ret = gnutls_handshake(session);
187         }
188         while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
189
190         if (ret < 0) {
191                 fail("client: Handshake failed\n");
192                 gnutls_perror(ret);
193                 exit(1);
194         } else {
195                 if (debug)
196                         success("client: Handshake was completed\n");
197         }
198
199         if (debug)
200                 success("client: TLS version is: %s\n",
201                         gnutls_protocol_get_name
202                         (gnutls_protocol_get_version(session)));
203
204         close(fd);
205
206         gnutls_deinit(session);
207
208         gnutls_anon_free_client_credentials(anoncred);
209         gnutls_certificate_free_credentials(x509_cred);
210
211         gnutls_global_deinit();
212 }
213
214
215 /* These are global */
216 pid_t child;
217
218
219 static void terminate(void)
220 {
221         kill(child, SIGTERM);
222         exit(1);
223 }
224
225 static void server(int fd, unsigned expl)
226 {
227         int ret;
228         char buffer[MAX_BUF + 1];
229         gnutls_session_t session;
230         gnutls_anon_server_credentials_t anoncred;
231         gnutls_certificate_credentials_t x509_cred;
232
233         /* this must be called once in the program
234          */
235         global_init();
236         memset(buffer, 0, sizeof(buffer));
237
238         if (debug) {
239                 gnutls_global_set_log_function(server_log_func);
240                 gnutls_global_set_log_level(4711);
241         }
242
243         gnutls_certificate_allocate_credentials(&x509_cred);
244         gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
245                                             &server_key,
246                                             GNUTLS_X509_FMT_PEM);
247
248         gnutls_anon_allocate_server_credentials(&anoncred);
249
250         if (expl & GNUTLS_DATAGRAM)
251                 gnutls_init(&session, GNUTLS_SERVER|GNUTLS_DATAGRAM);
252         else
253                 gnutls_init(&session, GNUTLS_SERVER);
254
255         /* avoid calling all the priority functions, since the defaults
256          * are adequate.
257          */
258         gnutls_priority_set_direct(session, "NORMAL", NULL);
259
260         gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
261         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
262
263         gnutls_transport_set_int(session, fd);
264
265         do {
266                 ret = gnutls_handshake(session);
267         }
268         while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
269         if (ret < 0) {
270                 close(fd);
271                 gnutls_deinit(session);
272                 fail("server: Handshake has failed (%s)\n\n",
273                      gnutls_strerror(ret));
274                 terminate();
275         }
276         if (debug)
277                 success("server: Handshake was completed\n");
278
279         if (debug)
280                 success("server: TLS version is: %s\n",
281                         gnutls_protocol_get_name
282                         (gnutls_protocol_get_version(session)));
283         close(fd);
284         gnutls_deinit(session);
285
286         gnutls_anon_free_server_credentials(anoncred);
287         gnutls_certificate_free_credentials(x509_cred);
288
289         gnutls_global_deinit();
290
291         if (debug)
292                 success("server: finished\n");
293 }
294
295 static void start(const char *msg, unsigned expl)
296 {
297         int fd[2];
298         int ret;
299
300         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
301         if (ret < 0) {
302                 perror("socketpair");
303                 exit(1);
304         }
305
306         child = fork();
307         if (child < 0) {
308                 perror("fork");
309                 fail("fork");
310                 exit(1);
311         }
312
313         if (child) {
314                 /* parent */
315                 close(fd[1]);
316                 client(fd[0], msg, expl);
317                 waitpid(-1, NULL, 0);
318                 //kill(child, SIGTERM);
319         } else {
320                 close(fd[0]);
321                 server(fd[1], expl);
322                 exit(0);
323         }
324 }
325
326 static void ch_handler(int sig)
327 {
328         int status = 0;
329
330         waitpid(-1, &status, 0);
331         if (WEXITSTATUS(status) != 0 ||
332             (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)) {
333                 if (WIFSIGNALED(status))
334                         fail("Child died with sigsegv\n");
335                 else
336                         fail("Child died with status %d\n",
337                              WEXITSTATUS(status));
338                 terminate();
339         }
340         return;
341 }
342
343 void doit(void)
344 {
345         signal(SIGCHLD, ch_handler);
346
347         start("TLS-explicit flag", GNUTLS_NONBLOCK);
348         start("DTLS-explicit flag", GNUTLS_NONBLOCK|GNUTLS_DATAGRAM);
349         start("TLS-no flag", 0);
350 }
351
352 #endif                          /* _WIN32 */