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. */
33 #include <gnutls/gnutls.h>
38 tls_log_func (int level
, const char *str
)
40 fprintf (stderr
, "|<%d>| %s", level
, str
);
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
);
60 memcpy (data
, to_client
, len
);
62 memmove (to_client
, to_client
+ len
, to_client_len
- len
);
69 client_push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
71 size_t newlen
= to_server_len
+ len
;
74 success ("client_push len %d has %d\n", (int)len
, (int)to_server_len
);
77 tmp
= realloc (to_server
, newlen
);
80 fail ("Memory allocation failure...\n");
85 memcpy (to_server
+ to_server_len
, data
, len
);
86 to_server_len
= newlen
;
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
);
102 memcpy (data
, to_server
, len
);
104 memmove (to_server
, to_server
+ len
, to_server_len
- len
);
105 to_server_len
-= len
;
111 server_push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
113 size_t newlen
= to_client_len
+ len
;
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
);
123 fail ("Memory allocation failure...\n");
128 memcpy (to_client
+ to_client_len
, data
, len
);
129 to_client_len
= newlen
;
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
);
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
);
151 #define MSG "Hello TLS"
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
;
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];
173 gnutls_global_init ();
174 gnutls_global_set_log_function (tls_log_func
);
176 gnutls_global_set_log_level (4711);
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
);
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
);
227 fail ("server: didn't receive any data\n");
229 fail ("server: error: %s\n", gnutls_strerror (ret
));
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
);
244 fail ("client: Peer has closed the TLS connection\n");
248 fail ("client: Error: %s\n", gnutls_strerror (ret
));
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
);
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 ();