signed-unsigned comparison fixes and removed unused parameter.
[gnutls.git] / tests / mini-record.c
blob24bf509156d600b6083b87823ea2afa9b4d1e321
1 /*
2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
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.
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.
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
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
30 #if defined(_WIN32)
32 int main()
34 exit(77);
37 #else
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 <gnutls/gnutls.h>
47 #include <gnutls/dtls.h>
48 #include <signal.h>
50 #include "utils.h"
52 static void terminate(void);
54 /* This program tests the robustness of record
55 * decoding.
58 static void
59 server_log_func (int level, const char *str)
61 // fprintf (stderr, "server|<%d>| %s", level, str);
64 static void
65 client_log_func (int level, const char *str)
67 fprintf (stderr, "client|<%d>| %s", level, str);
70 static unsigned char server_cert_pem[] =
71 "-----BEGIN CERTIFICATE-----\n"
72 "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
73 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
74 "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
75 "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
76 "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
77 "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
78 "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
79 "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
80 "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
81 "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
82 "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
83 "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
84 "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
86 const gnutls_datum_t server_cert = { server_cert_pem,
87 sizeof (server_cert_pem)
90 static unsigned char server_key_pem[] =
91 "-----BEGIN RSA PRIVATE KEY-----\n"
92 "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
93 "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
94 "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
95 "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
96 "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
97 "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
98 "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
99 "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
100 "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
101 "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
102 "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
103 "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
104 "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
105 "-----END RSA PRIVATE KEY-----\n";
107 const gnutls_datum_t server_key = { server_key_pem,
108 sizeof (server_key_pem)
112 /* A very basic TLS client, with anonymous authentication.
115 #define MAX_BUF 1024
117 static int to_send = -1;
119 static ssize_t
120 push (gnutls_transport_ptr_t tr, const void *data, size_t len)
122 int fd = (long int)tr;
124 return send(fd, data, len, 0);
127 #define RECORD_HEADER_SIZE (5+8)
129 static ssize_t
130 push_crippled (gnutls_transport_ptr_t tr, const void *data, size_t len)
132 int fd = (long int)tr;
133 int _len, ret;
134 uint8_t* _data = (void*)data;
136 if (to_send == -1)
137 return send(fd, data, len, 0);
138 else
140 // _len = ((uint8_t*)data)[11] << 8 | ((uint8_t*)data)[12];
141 //fprintf(stderr, "len : %d\n", (int)_len);
143 _len = to_send;
144 _data[11] = _len >> 8;
145 _data[12] = _len;
147 /* correct len */
148 ret = send(fd, data, RECORD_HEADER_SIZE+_len, 0);
150 if (ret < 0) return ret;
152 return len;
156 static void
157 client (int fd, const char* prio)
159 int ret;
160 char buffer[MAX_BUF + 1];
161 gnutls_anon_client_credentials_t anoncred;
162 gnutls_certificate_credentials_t x509_cred;
163 gnutls_session_t session;
164 /* Need to enable anonymous KX specifically. */
166 gnutls_global_init ();
168 if (debug)
170 gnutls_global_set_log_function (client_log_func);
171 gnutls_global_set_log_level (7);
174 gnutls_anon_allocate_client_credentials (&anoncred);
175 gnutls_certificate_allocate_credentials (&x509_cred);
177 /* Initialize TLS session
179 gnutls_init (&session, GNUTLS_CLIENT|GNUTLS_DATAGRAM);
180 gnutls_dtls_set_mtu( session, 1500);
182 /* Use default priorities */
183 gnutls_priority_set_direct (session, prio, NULL);
185 /* put the anonymous credentials to the current session
187 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
188 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
190 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
191 gnutls_transport_set_push_function (session, push);
193 /* Perform the TLS handshake
197 ret = gnutls_handshake (session);
199 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
201 if (ret < 0)
203 fail ("client: Handshake failed\n");
204 gnutls_perror (ret);
205 exit(1);
207 else
209 if (debug)
210 success ("client: Handshake was completed\n");
213 if (debug)
214 success ("client: TLS version is: %s\n",
215 gnutls_protocol_get_name (gnutls_protocol_get_version
216 (session)));
218 do {
219 do {
220 ret = gnutls_record_recv (session, buffer, MAX_BUF);
221 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
222 } while( ret > 0);
224 if (ret == 0)
226 if (debug)
227 success ("client: Peer has closed the TLS connection\n");
228 goto end;
230 else if (ret < 0)
232 if (ret != 0)
234 fail ("client: Error: %s\n", gnutls_strerror (ret));
235 exit(1);
239 gnutls_bye (session, GNUTLS_SHUT_WR);
241 end:
243 close (fd);
245 gnutls_deinit (session);
247 gnutls_anon_free_client_credentials (anoncred);
248 gnutls_certificate_free_credentials (x509_cred);
250 gnutls_global_deinit ();
254 /* These are global */
255 gnutls_anon_server_credentials_t anoncred;
256 gnutls_certificate_credentials_t x509_cred;
257 pid_t child;
259 static gnutls_session_t
260 initialize_tls_session (const char* prio)
262 gnutls_session_t session;
264 gnutls_init (&session, GNUTLS_SERVER|GNUTLS_DATAGRAM);
265 gnutls_dtls_set_mtu( session, 1500);
267 /* avoid calling all the priority functions, since the defaults
268 * are adequate.
270 gnutls_priority_set_direct (session, prio, NULL);
272 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
273 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
275 return session;
278 static void terminate(void)
280 kill(child, SIGTERM);
281 exit(1);
284 static void
285 server (int fd, const char* prio)
287 int ret;
288 char buffer[MAX_BUF + 1];
289 gnutls_session_t session;
291 /* this must be called once in the program
293 gnutls_global_init ();
294 memset(buffer, 0, sizeof(buffer));
296 if (debug)
298 gnutls_global_set_log_function (server_log_func);
299 gnutls_global_set_log_level (4711);
302 gnutls_certificate_allocate_credentials (&x509_cred);
303 gnutls_certificate_set_x509_key_mem (x509_cred, &server_cert, &server_key,
304 GNUTLS_X509_FMT_PEM);
306 gnutls_anon_allocate_server_credentials (&anoncred);
308 session = initialize_tls_session (prio);
310 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
311 gnutls_transport_set_push_function (session, push_crippled);
315 ret = gnutls_handshake (session);
317 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
318 if (ret < 0)
320 close (fd);
321 gnutls_deinit (session);
322 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
323 terminate();
325 if (debug)
326 success ("server: Handshake was completed\n");
328 if (debug)
329 success ("server: TLS version is: %s\n",
330 gnutls_protocol_get_name (gnutls_protocol_get_version
331 (session)));
334 do {
335 ret = gnutls_record_send (session, buffer, sizeof (buffer));
336 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
337 usleep(400*1000);
339 if (ret < 0)
341 fail("Error sending %d byte packet\n", to_send);
342 terminate();
344 to_send++;
346 while(to_send < 64);
348 to_send = -1;
349 /* do not wait for the peer to close the connection.
351 gnutls_bye (session, GNUTLS_SHUT_WR);
353 close (fd);
354 gnutls_deinit (session);
356 gnutls_anon_free_server_credentials (anoncred);
357 gnutls_certificate_free_credentials (x509_cred);
359 gnutls_global_deinit ();
361 if (debug)
362 success ("server: finished\n");
365 static void start (const char* prio)
367 int fd[2];
368 int ret;
370 ret = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
371 if (ret < 0)
373 perror("socketpair");
374 exit(1);
377 child = fork ();
378 if (child < 0)
380 perror ("fork");
381 fail ("fork");
382 exit(1);
385 if (child)
387 /* parent */
388 close(fd[1]);
389 server (fd[0], prio);
390 kill(child, SIGTERM);
392 else
394 close(fd[0]);
395 client (fd[1], prio);
396 exit(0);
400 #define AES_CBC "NONE:+VERS-DTLS1.0:-CIPHER-ALL:+AES-128-CBC:+SHA1:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL"
401 #define AES_CBC_SHA256 "NONE:+VERS-DTLS1.0:-CIPHER-ALL:+RSA:+AES-128-CBC:+AES-256-CBC:+SHA256:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL"
402 #define AES_GCM "NONE:+VERS-DTLS1.0:-CIPHER-ALL:+RSA:+AES-128-GCM:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL"
404 static void ch_handler(int sig)
406 int status;
407 wait(&status);
408 if (WEXITSTATUS(status) != 0 ||
409 (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV))
411 if (WIFSIGNALED(status))
412 fail("Child died with sigsegv\n");
413 else
414 fail("Child died with status %d\n", WEXITSTATUS(status));
415 terminate();
417 return;
420 void
421 doit (void)
423 signal(SIGCHLD, ch_handler);
425 start(AES_CBC);
426 start(AES_CBC_SHA256);
427 start(AES_GCM);
430 #endif /* _WIN32 */