gnutls_srtp_get_profile_by_name -> gnutls_srtp_get_profile_id
[gnutls.git] / tests / mini-termination.c
blob1dd0e02e40d42dc0b79a442630b1f05513adaa1c
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 void
118 client (int fd, const char* prio)
120 int ret;
121 char buffer[MAX_BUF + 1];
122 gnutls_anon_client_credentials_t anoncred;
123 gnutls_certificate_credentials_t x509_cred;
124 gnutls_session_t session;
125 /* Need to enable anonymous KX specifically. */
127 gnutls_global_init ();
129 if (debug)
131 gnutls_global_set_log_function (client_log_func);
132 gnutls_global_set_log_level (7);
135 gnutls_anon_allocate_client_credentials (&anoncred);
136 gnutls_certificate_allocate_credentials (&x509_cred);
138 /* Initialize TLS session
140 gnutls_init (&session, GNUTLS_CLIENT);
142 /* Use default priorities */
143 gnutls_priority_set_direct (session, prio, NULL);
145 /* put the anonymous credentials to the current session
147 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
148 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
150 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
152 /* Perform the TLS handshake
156 ret = gnutls_handshake (session);
158 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
160 if (ret < 0)
162 fail ("client: Handshake failed\n");
163 gnutls_perror (ret);
164 exit(1);
166 else
168 if (debug)
169 success ("client: Handshake was completed\n");
172 if (debug)
173 success ("client: TLS version is: %s\n",
174 gnutls_protocol_get_name (gnutls_protocol_get_version
175 (session)));
176 do {
177 do {
178 ret = gnutls_record_recv (session, buffer, MAX_BUF);
179 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
180 } while( ret > 0);
182 if (ret == GNUTLS_E_PREMATURE_TERMINATION)
184 if (debug)
185 success ("client: Peer has closed the TLS connection\n");
186 goto end;
188 else
190 fail ("client: Unexpected error: %d (%s)\n", ret, gnutls_strerror (ret));
191 exit(1);
194 end:
195 close (fd);
197 gnutls_deinit (session);
199 gnutls_anon_free_client_credentials (anoncred);
200 gnutls_certificate_free_credentials (x509_cred);
202 gnutls_global_deinit ();
206 /* These are global */
207 gnutls_anon_server_credentials_t anoncred;
208 gnutls_certificate_credentials_t x509_cred;
209 pid_t child;
211 static gnutls_session_t
212 initialize_tls_session (const char* prio)
214 gnutls_session_t session;
216 gnutls_init (&session, GNUTLS_SERVER);
218 /* avoid calling all the priority functions, since the defaults
219 * are adequate.
221 gnutls_priority_set_direct (session, prio, NULL);
223 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
224 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
226 return session;
229 static void terminate(void)
231 kill(child, SIGTERM);
232 exit(1);
235 static void
236 server (int fd, const char* prio)
238 int ret;
239 char buffer[MAX_BUF + 1];
240 gnutls_session_t session;
242 /* this must be called once in the program
244 gnutls_global_init ();
245 memset(buffer, 0, sizeof(buffer));
247 if (debug)
249 gnutls_global_set_log_function (server_log_func);
250 gnutls_global_set_log_level (4711);
253 gnutls_certificate_allocate_credentials (&x509_cred);
254 gnutls_certificate_set_x509_key_mem (x509_cred, &server_cert, &server_key,
255 GNUTLS_X509_FMT_PEM);
257 gnutls_anon_allocate_server_credentials (&anoncred);
259 session = initialize_tls_session (prio);
261 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
265 ret = gnutls_handshake (session);
267 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
268 if (ret < 0)
270 close (fd);
271 gnutls_deinit (session);
272 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
273 terminate();
275 if (debug)
276 success ("server: Handshake was completed\n");
278 if (debug)
279 success ("server: TLS version is: %s\n",
280 gnutls_protocol_get_name (gnutls_protocol_get_version
281 (session)));
282 close(fd);
283 gnutls_deinit (session);
285 gnutls_anon_free_server_credentials (anoncred);
286 gnutls_certificate_free_credentials (x509_cred);
288 gnutls_global_deinit ();
290 if (debug)
291 success ("server: finished\n");
294 static void start (const char* prio)
296 int fd[2];
297 int ret;
299 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
300 if (ret < 0)
302 perror("socketpair");
303 exit(1);
306 child = fork ();
307 if (child < 0)
309 perror ("fork");
310 fail ("fork");
311 exit(1);
314 if (child)
316 /* parent */
317 close(fd[1]);
318 server (fd[0], prio);
319 waitpid(-1, NULL, 0);
320 //kill(child, SIGTERM);
322 else
324 close(fd[0]);
325 client (fd[1], prio);
326 exit(0);
330 static void ch_handler(int sig)
332 int status = 0;
334 waitpid(-1, &status, 0);
335 if (WEXITSTATUS(status) != 0 ||
336 (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV))
338 if (WIFSIGNALED(status))
339 fail("Child died with sigsegv\n");
340 else
341 fail("Child died with status %d\n", WEXITSTATUS(status));
342 terminate();
344 return;
347 void
348 doit (void)
350 signal(SIGCHLD, ch_handler);
352 start("NORMAL");
355 #endif /* _WIN32 */