2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
3 * Copyright (C) 2013 Adam Sampson <ats@offog.org>
5 * Author: Simon Josefsson
7 * This file is part of GnuTLS.
9 * GnuTLS is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * GnuTLS is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GnuTLS; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 /* Parts copied from GnuTLS example programs. */
33 /* This program tests anonymous authentication as well as the gnutls_record_recv_packet.
38 /* socketpair isn't supported on Win32. */
39 int main(int argc, char **argv)
47 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <sys/socket.h>
54 #include <gnutls/gnutls.h>
58 static void tls_log_func(int level, const char *str)
60 fprintf(stderr, "|<%d>| %s", level, str);
64 #define MSG "Hello TLS"
66 static void client(int sd)
69 gnutls_session_t session;
70 char buffer[MAX_BUF + 1];
71 gnutls_anon_client_credentials_t anoncred;
72 /* Need to enable anonymous KX specifically. */
76 gnutls_global_set_log_function(tls_log_func);
78 gnutls_global_set_log_level(4711);
80 gnutls_anon_allocate_client_credentials(&anoncred);
82 /* Initialize TLS session
84 gnutls_init(&session, GNUTLS_CLIENT);
86 /* Use default priorities */
87 gnutls_priority_set_direct(session,
88 "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH",
91 /* put the anonymous credentials to the current session
93 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
95 gnutls_transport_set_int(session, sd);
97 /* Perform the TLS handshake
99 ret = gnutls_handshake(session);
102 fail("client: Handshake failed\n");
107 success("client: Handshake was completed\n");
110 ret = gnutls_dh_get_prime_bits(session);
112 fail("server: too small prime size: %d\n", ret);
115 ret = gnutls_dh_get_secret_bits(session);
117 fail("server: too small secret key size: %d\n", ret);
121 success("client: TLS version is: %s\n",
122 gnutls_protocol_get_name
123 (gnutls_protocol_get_version(session)));
125 gnutls_record_send(session, MSG, strlen(MSG));
127 ret = gnutls_record_recv(session, buffer, MAX_BUF);
131 ("client: Peer has closed the TLS connection\n");
133 } else if (ret < 0) {
134 fail("client: Error: %s\n", gnutls_strerror(ret));
138 if (ret != strlen(MSG) || memcmp(buffer, MSG, ret) != 0) {
139 fail("client: received data of different size! (expected: %d, have: %d)\n",
140 (int)strlen(MSG), ret);
145 printf("- Received %d bytes: ", ret);
146 for (ii = 0; ii < ret; ii++) {
147 fputc(buffer[ii], stdout);
152 gnutls_bye(session, GNUTLS_SHUT_RDWR);
158 gnutls_deinit(session);
160 gnutls_anon_free_client_credentials(anoncred);
162 gnutls_global_deinit();
165 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
171 /* These are global */
172 gnutls_anon_server_credentials_t anoncred;
174 static gnutls_session_t initialize_tls_session(void)
176 gnutls_session_t session;
178 gnutls_init(&session, GNUTLS_SERVER);
180 /* avoid calling all the priority functions, since the defaults
183 gnutls_priority_set_direct(session,
184 "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH",
187 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
189 gnutls_dh_set_prime_bits(session, DH_BITS);
194 static gnutls_dh_params_t dh_params;
196 static int generate_dh_params(void)
198 const gnutls_datum_t p3 = { (void *) pkcs3, strlen(pkcs3) };
199 /* Generate Diffie-Hellman parameters - for use with DHE
200 * kx algorithms. These should be discarded and regenerated
201 * once a day, once a week or once a month. Depending on the
202 * security requirements.
204 gnutls_dh_params_init(&dh_params);
205 return gnutls_dh_params_import_pkcs3(dh_params, &p3,
206 GNUTLS_X509_FMT_PEM);
211 gnutls_session_t session;
212 char buffer[MAX_BUF + 1];
215 static void server(int sd)
217 gnutls_packet_t packet;
219 /* this must be called once in the program
223 gnutls_global_set_log_function(tls_log_func);
225 gnutls_global_set_log_level(4711);
227 gnutls_anon_allocate_server_credentials(&anoncred);
230 success("Launched, generating DH parameters...\n");
232 generate_dh_params();
234 gnutls_anon_set_server_dh_params(anoncred, dh_params);
236 session = initialize_tls_session();
238 gnutls_transport_set_int(session, sd);
239 ret = gnutls_handshake(session);
242 gnutls_deinit(session);
243 fail("server: Handshake has failed (%s)\n\n",
244 gnutls_strerror(ret));
248 success("server: Handshake was completed\n");
251 success("server: TLS version is: %s\n",
252 gnutls_protocol_get_name
253 (gnutls_protocol_get_version(session)));
255 ret = gnutls_dh_get_prime_bits(session);
257 fail("server: too small prime size: %d\n", ret);
260 ret = gnutls_dh_get_secret_bits(session);
262 fail("server: too small secret key size: %d\n", ret);
265 /* see the Getting peer's information example */
266 /* print_info(session); */
269 ret = gnutls_record_recv_packet(session, &packet);
272 gnutls_packet_deinit(packet);
275 ("server: Peer has closed the GnuTLS connection\n");
277 } else if (ret < 0) {
278 fail("server: Received corrupted data(%d). Closing...\n", ret);
280 } else if (ret > 0) {
281 gnutls_datum_t pdata;
283 gnutls_packet_get(packet, &pdata, NULL);
284 /* echo data back to the client
286 gnutls_record_send(session, pdata.data,
288 gnutls_packet_deinit(packet);
291 /* do not wait for the peer to close the connection.
293 gnutls_bye(session, GNUTLS_SHUT_WR);
296 gnutls_deinit(session);
298 gnutls_anon_free_server_credentials(anoncred);
300 gnutls_dh_params_deinit(dh_params);
302 gnutls_global_deinit();
305 success("server: finished\n");
313 err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
315 perror("socketpair");
316 fail("socketpair failed\n");