Add announcement message.
[gnutls.git] / tests / finished.c
blob9fea34a8606d7dcda9ed41effa5d1221a32ff040
1 /*
2 * Copyright (C) 2008, 2010 Free Software Foundation, Inc.
4 * Author: Simon Josefsson
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 /* Based on mini.c. */
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <gnutls/gnutls.h>
35 #include "utils.h"
37 static void
38 tls_log_func (int level, const char *str)
40 fprintf (stderr, "|<%d>| %s", level, str);
43 char *to_server;
44 size_t to_server_len;
46 char *to_client;
47 size_t to_client_len;
49 static ssize_t
50 client_pull (gnutls_transport_ptr_t tr, void *data, size_t len)
52 success ("client_pull len %d has %d\n", (int)len, (int)to_client_len);
54 if (to_client_len < len)
56 gnutls_transport_set_global_errno (EAGAIN);
57 return -1;
60 memcpy (data, to_client, len);
62 memmove (to_client, to_client + len, to_client_len - len);
63 to_client_len -= len;
65 return len;
68 static ssize_t
69 client_push (gnutls_transport_ptr_t tr, const void *data, size_t len)
71 size_t newlen = to_server_len + len;
72 char *tmp;
74 success ("client_push len %d has %d\n", (int)len, (int)to_server_len);
75 hexprint (data, len);
77 tmp = realloc (to_server, newlen);
78 if (!tmp)
80 fail ("Memory allocation failure...\n");
81 exit (1);
83 to_server = tmp;
85 memcpy (to_server + to_server_len, data, len);
86 to_server_len = newlen;
88 return len;
91 static ssize_t
92 server_pull (gnutls_transport_ptr_t tr, void *data, size_t len)
94 success ("server_pull len %d has %d\n", (int)len, (int)to_server_len);
96 if (to_server_len < len)
98 gnutls_transport_set_global_errno (EAGAIN);
99 return -1;
102 memcpy (data, to_server, len);
104 memmove (to_server, to_server + len, to_server_len - len);
105 to_server_len -= len;
107 return len;
110 static ssize_t
111 server_push (gnutls_transport_ptr_t tr, const void *data, size_t len)
113 size_t newlen = to_client_len + len;
114 char *tmp;
116 success ("server_push len %d has %d\n", (int)len, (int)to_client_len);
118 hexprint (data, len);
120 tmp = realloc (to_client, newlen);
121 if (!tmp)
123 fail ("Memory allocation failure...\n");
124 exit (1);
126 to_client = tmp;
128 memcpy (to_client + to_client_len, data, len);
129 to_client_len = newlen;
131 return len;
134 static void
135 client_finished_callback (gnutls_session_t session,
136 const void *finished, size_t len)
138 success ("client finished (length %d)\n", (int)len);
139 hexprint (finished, len);
142 static void
143 server_finished_callback (gnutls_session_t session,
144 const void *finished, size_t len)
146 success ("server finished (length %d)\n", (int)len);
147 hexprint (finished, len);
150 #define MAX_BUF 1024
151 #define MSG "Hello TLS"
153 void
154 doit (void)
156 /* Server stuff. */
157 gnutls_anon_server_credentials_t s_anoncred;
158 const gnutls_datum_t p3 = { (char *) pkcs3, strlen (pkcs3) };
159 static gnutls_dh_params_t dh_params;
160 gnutls_session_t server;
161 int sret = GNUTLS_E_AGAIN;
162 /* Client stuff. */
163 gnutls_anon_client_credentials_t c_anoncred;
164 gnutls_session_t client;
165 int n, cret = GNUTLS_E_AGAIN;
166 /* Need to enable anonymous KX specifically. */
167 const int kx_prio[] = { GNUTLS_KX_ANON_DH, 0 };
168 char buffer[MAX_BUF + 1];
169 ssize_t ns;
170 int ret;
172 /* General init. */
173 gnutls_global_init ();
174 gnutls_global_set_log_function (tls_log_func);
175 if (debug)
176 gnutls_global_set_log_level (4711);
178 /* Init server */
179 gnutls_anon_allocate_server_credentials (&s_anoncred);
180 gnutls_dh_params_init (&dh_params);
181 gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
182 gnutls_anon_set_server_dh_params (s_anoncred, dh_params);
183 gnutls_init (&server, GNUTLS_SERVER);
184 gnutls_set_default_priority (server);
185 gnutls_kx_set_priority (server, kx_prio);
186 gnutls_credentials_set (server, GNUTLS_CRD_ANON, s_anoncred);
187 gnutls_dh_set_prime_bits (server, 1024);
188 gnutls_transport_set_push_function (server, server_push);
189 gnutls_transport_set_pull_function (server, server_pull);
190 gnutls_session_set_finished_function (server, server_finished_callback);
192 /* Init client */
193 gnutls_anon_allocate_client_credentials (&c_anoncred);
194 gnutls_init (&client, GNUTLS_CLIENT);
195 gnutls_set_default_priority (client);
196 gnutls_kx_set_priority (client, kx_prio);
197 gnutls_credentials_set (client, GNUTLS_CRD_ANON, c_anoncred);
198 gnutls_transport_set_push_function (client, client_push);
199 gnutls_transport_set_pull_function (client, client_pull);
200 gnutls_session_set_finished_function (client, client_finished_callback);
204 if (cret == GNUTLS_E_AGAIN)
206 success ("loop invoking client:\n");
207 cret = gnutls_handshake (client);
208 success ("client %d: %s\n", cret, gnutls_strerror (cret));
211 if (sret == GNUTLS_E_AGAIN)
213 success ("loop invoking server:\n");
214 sret = gnutls_handshake (server);
215 success ("server %d: %s\n", sret, gnutls_strerror (sret));
218 while (cret == GNUTLS_E_AGAIN || sret == GNUTLS_E_AGAIN);
220 success ("Handshake established\n");
222 ns = gnutls_record_send (client, MSG, strlen (MSG));
223 success ("client: sent %d\n", (int)ns);
225 ret = gnutls_record_recv (server, buffer, MAX_BUF);
226 if (ret == 0)
227 fail ("server: didn't receive any data\n");
228 else if (ret < 0)
229 fail ("server: error: %s\n", gnutls_strerror (ret));
230 else
232 printf ("server: received %d: ", ret);
233 for (n = 0; n < ret; n++)
234 fputc (buffer[n], stdout);
235 fputs ("\n", stdout);
238 ns = gnutls_record_send (server, MSG, strlen (MSG));
239 success ("server: sent %d\n", (int)ns);
241 ret = gnutls_record_recv (client, buffer, MAX_BUF);
242 if (ret == 0)
244 fail ("client: Peer has closed the TLS connection\n");
246 else if (ret < 0)
248 fail ("client: Error: %s\n", gnutls_strerror (ret));
250 else
252 printf ("client: received %d: ", ret);
253 for (n = 0; n < ret; n++)
254 fputc (buffer[n], stdout);
255 fputs ("\n", stdout);
258 gnutls_bye (client, GNUTLS_SHUT_RDWR);
259 gnutls_bye (server, GNUTLS_SHUT_RDWR);
261 gnutls_deinit (client);
262 gnutls_deinit (server);
264 free (to_server);
265 free (to_client);
267 gnutls_anon_free_client_credentials (c_anoncred);
268 gnutls_anon_free_server_credentials (s_anoncred);
270 gnutls_dh_params_deinit (dh_params);
272 gnutls_global_deinit ();