2 * Copyright (C) 2015 Nikos Mavrogiannopoulos
4 * This file is part of GnuTLS.
6 * GnuTLS is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuTLS is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GnuTLS; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
38 # include <sys/types.h>
39 # include <netinet/in.h>
40 # include <sys/socket.h>
41 # include <sys/wait.h>
42 # include <arpa/inet.h>
44 # include <gnutls/gnutls.h>
45 # include <gnutls/dtls.h>
62 fail("sigpipe received\n");
68 static void client(int fd)
71 gnutls_anon_client_credentials_t anoncred;
72 gnutls_session_t session;
75 /* Need to enable anonymous KX specifically. */
80 gnutls_global_set_log_level(4711);
83 gnutls_anon_allocate_client_credentials(&anoncred);
85 /* Initialize TLS session
87 gnutls_init(&session, GNUTLS_CLIENT);
88 gnutls_handshake_set_timeout(session, 20 * 1000);
90 /* Use default priorities */
91 gnutls_priority_set_direct(session,
92 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
95 /* put the anonymous credentials to the current session
97 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
99 gnutls_transport_set_int(session, fd);
101 /* Perform the TLS handshake
104 ret = gnutls_handshake(session);
106 while (ret < 0 && (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED));
108 ret = gnutls_record_recv(session, buf, sizeof(buf));
109 if (ret < 0 || ret != sizeof(buf)) {
110 kill(getppid(), SIGPIPE);
111 fail("client: recv failed");
114 success("client: received %d bytes\n", ret);
116 memset(buf2, 0, sizeof(buf));
117 if (memcmp(buf, buf2, sizeof(buf)) != 0) {
118 kill(getppid(), SIGPIPE);
119 fail("client: recv data failed");
123 gnutls_deinit(session);
124 gnutls_anon_free_client_credentials(anoncred);
125 gnutls_global_deinit();
128 fail("client: Handshake failed with unexpected reason: %s\n", gnutls_strerror(ret));
131 success("client: Handshake was completed\n");
136 /* These are global */
139 static void server(int fd)
141 gnutls_anon_server_credentials_t anoncred;
142 gnutls_session_t session;
146 /* this must be called once in the program
151 gnutls_global_set_log_level(4711);
154 gnutls_anon_allocate_server_credentials(&anoncred);
156 gnutls_init(&session, GNUTLS_SERVER|GNUTLS_NO_SIGNAL);
158 /* avoid calling all the priority functions, since the defaults
161 gnutls_priority_set_direct(session,
162 "NONE:+VERS-TLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
165 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
167 gnutls_transport_set_int(session, fd);
170 ret = gnutls_handshake(session);
172 while (ret < 0 && (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED));
175 fail("error in handshake: %s\n", gnutls_strerror(ret));
179 memset(buf, 0, sizeof(buf));
182 ret = gnutls_record_send(session, buf, sizeof(buf));
189 gnutls_deinit(session);
190 gnutls_anon_free_server_credentials(anoncred);
191 gnutls_global_deinit();
195 static void start(void)
200 /* we need dgram in this test */
201 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
203 perror("socketpair");
219 kill(child, SIGTERM);
228 static void ch_handler(int sig)
232 if (WEXITSTATUS(status) != 0)
233 fail("Child died with status %d\n", WEXITSTATUS(status));
239 signal(SIGCHLD, ch_handler);
240 signal(SIGPIPE, sigpipe);
245 # endif /* MSG_NOSIGNAL */