2 * Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation
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 /* Parts copied from GnuTLS example programs. */
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
36 #include <arpa/inet.h>
38 #include <gnutls/gnutls.h>
43 tls_log_func (int level
, const char *str
)
45 fprintf (stderr
, "|<%d>| %s", level
, str
);
48 /* A very basic TLS client, with anonymous authentication.
52 #define MSG "Hello TLS"
54 /* Connects to the peer and returns a socket
60 const char *PORT
= "5556";
61 const char *SERVER
= "127.0.0.1";
63 struct sockaddr_in sa
;
67 sd
= socket (AF_INET
, SOCK_STREAM
, 0);
69 memset (&sa
, '\0', sizeof (sa
));
70 sa
.sin_family
= AF_INET
;
71 sa
.sin_port
= htons (atoi (PORT
));
72 inet_pton (AF_INET
, SERVER
, &sa
.sin_addr
);
74 err
= connect (sd
, (struct sockaddr
*) &sa
, sizeof (sa
));
77 fprintf (stderr
, "Connect error\n");
84 /* closes the given socket descriptor.
89 shutdown (sd
, SHUT_RDWR
); /* no more receptions */
97 gnutls_session_t session
;
98 char buffer
[MAX_BUF
+ 1];
99 gnutls_anon_client_credentials_t anoncred
;
100 /* Need to enable anonymous KX specifically. */
101 const int kx_prio
[] = { GNUTLS_KX_ANON_DH
, 0 };
103 gnutls_global_init ();
105 gnutls_global_set_log_function (tls_log_func
);
106 gnutls_global_set_log_level (4711);
108 gnutls_anon_allocate_client_credentials (&anoncred
);
110 /* Initialize TLS session
112 gnutls_init (&session
, GNUTLS_CLIENT
);
114 /* Use default priorities */
115 gnutls_set_default_priority (session
);
116 gnutls_kx_set_priority (session
, kx_prio
);
118 /* put the anonymous credentials to the current session
120 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
122 gnutls_oprfi_enable_client (session
, 3, "foo");
124 /* connect to the peer
128 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
130 /* Perform the TLS handshake
132 ret
= gnutls_handshake (session
);
136 fail ("client: Handshake failed\n");
142 success ("client: Handshake was completed\n");
145 success ("client: TLS version is: %s\n",
146 gnutls_protocol_get_name (gnutls_protocol_get_version (session
)));
148 gnutls_record_send (session
, MSG
, strlen (MSG
));
150 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
153 success ("client: Peer has closed the TLS connection\n");
158 fail ("client: Error: %s\n", gnutls_strerror (ret
));
162 printf ("- Received %d bytes: ", ret
);
163 for (ii
= 0; ii
< ret
; ii
++)
165 fputc (buffer
[ii
], stdout
);
167 fputs ("\n", stdout
);
169 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
175 gnutls_deinit (session
);
177 gnutls_anon_free_client_credentials (anoncred
);
179 gnutls_global_deinit ();
182 /* This is a sample TLS 1.0 echo server, for anonymous authentication only.
185 #define SA struct sockaddr
187 #define PORT 5556 /* listen to 5556 port */
190 /* These are global */
191 gnutls_anon_server_credentials_t anoncred
;
194 oprfi_callback (gnutls_session_t session
,
197 const unsigned char *in_oprfi
,
198 unsigned char *out_oprfi
)
204 for (i
= 0; i
< oprfi_len
; i
++)
205 printf ("OPRF[%d]: %02x %03d %c\n", i
, in_oprfi
[i
],
206 in_oprfi
[i
], in_oprfi
[i
]);
208 memset (out_oprfi
, 42, oprfi_len
);
214 initialize_tls_session (void)
216 gnutls_session_t session
;
217 const int kx_prio
[] = { GNUTLS_KX_ANON_DH
, 0 };
219 gnutls_init (&session
, GNUTLS_SERVER
);
221 /* avoid calling all the priority functions, since the defaults
224 gnutls_set_default_priority (session
);
225 gnutls_kx_set_priority (session
, kx_prio
);
227 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anoncred
);
229 gnutls_dh_set_prime_bits (session
, DH_BITS
);
231 gnutls_oprfi_enable_server (session
, oprfi_callback
, NULL
);
236 static gnutls_dh_params_t dh_params
;
239 generate_dh_params (void)
241 const gnutls_datum_t p3
= { pkcs3
, strlen (pkcs3
) };
242 /* Generate Diffie Hellman parameters - for use with DHE
243 * kx algorithms. These should be discarded and regenerated
244 * once a day, once a week or once a month. Depending on the
245 * security requirements.
247 gnutls_dh_params_init (&dh_params
);
248 return gnutls_dh_params_import_pkcs3 (dh_params
, &p3
, GNUTLS_X509_FMT_PEM
);
251 int err
, listen_sd
, i
;
253 struct sockaddr_in sa_serv
;
254 struct sockaddr_in sa_cli
;
257 gnutls_session_t session
;
258 char buffer
[MAX_BUF
+ 1];
264 /* this must be called once in the program
266 gnutls_global_init ();
268 gnutls_global_set_log_function (tls_log_func
);
269 gnutls_global_set_log_level (4711);
271 gnutls_anon_allocate_server_credentials (&anoncred
);
273 success ("Launched, generating DH parameters...\n");
275 generate_dh_params ();
277 gnutls_anon_set_server_dh_params (anoncred
, dh_params
);
281 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
285 fail ("server: socket failed\n");
289 memset (&sa_serv
, '\0', sizeof (sa_serv
));
290 sa_serv
.sin_family
= AF_INET
;
291 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
292 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
294 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof (int));
296 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
300 fail ("server: bind failed\n");
304 err
= listen (listen_sd
, 1024);
308 fail ("server: listen failed\n");
312 success ("server: ready. Listening to port '%d'.\n", PORT
);
318 client_len
= sizeof (sa_cli
);
320 session
= initialize_tls_session ();
322 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
324 success ("server: connection from %s, port %d\n",
325 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
326 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
328 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
329 ret
= gnutls_handshake (session
);
333 gnutls_deinit (session
);
334 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
337 success ("server: Handshake was completed\n");
339 success ("server: TLS version is: %s\n",
340 gnutls_protocol_get_name (gnutls_protocol_get_version (session
)));
342 /* see the Getting peer's information example */
343 /* print_info(session); */
348 bzero (buffer
, MAX_BUF
+ 1);
349 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
353 success ("server: Peer has closed the GNUTLS connection\n");
358 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
363 /* echo data back to the client
365 gnutls_record_send (session
, buffer
, strlen (buffer
));
368 /* do not wait for the peer to close the connection.
370 gnutls_bye (session
, GNUTLS_SHUT_WR
);
373 gnutls_deinit (session
);
377 gnutls_anon_free_server_credentials (anoncred
);
379 gnutls_global_deinit ();
381 success ("server: finished\n");