2 * Copyright (C) 2004, 2005 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 <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include <gnutls/gnutls.h>
42 /* A very basic TLS client, with PSK authentication.
46 #define MSG "Hello TLS"
48 /* Connects to the peer and returns a socket
54 const char *PORT
= "5556";
55 const char *SERVER
= "127.0.0.1";
57 struct sockaddr_in sa
;
61 sd
= socket (AF_INET
, SOCK_STREAM
, 0);
63 memset (&sa
, '\0', sizeof (sa
));
64 sa
.sin_family
= AF_INET
;
65 sa
.sin_port
= htons (atoi (PORT
));
66 inet_pton (AF_INET
, SERVER
, &sa
.sin_addr
);
68 err
= connect (sd
, (struct sockaddr
*) &sa
, sizeof (sa
));
71 fprintf (stderr
, "Connect error\n");
78 /* closes the given socket descriptor.
83 shutdown (sd
, SHUT_RDWR
); /* no more receptions */
91 gnutls_session_t session
;
92 char buffer
[MAX_BUF
+ 1];
93 gnutls_psk_client_credentials_t pskcred
;
94 /* Need to enable anonymous KX specifically. */
95 const int kx_prio
[] = { GNUTLS_KX_PSK
, 0 };
96 const gnutls_datum_t key
= { "DEADBEEF", 8 };
98 gnutls_global_init ();
100 gnutls_psk_allocate_client_credentials (&pskcred
);
101 gnutls_psk_set_client_credentials (pskcred
, "test", &key
,
104 /* Initialize TLS session
106 gnutls_init (&session
, GNUTLS_CLIENT
);
108 /* Use default priorities */
109 gnutls_set_default_priority (session
);
110 gnutls_kx_set_priority (session
, kx_prio
);
112 /* put the anonymous credentials to the current session
114 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, pskcred
);
116 /* connect to the peer
120 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
122 /* Perform the TLS handshake
124 ret
= gnutls_handshake (session
);
128 fail ("client: Handshake failed\n");
134 success ("client: Handshake was completed\n");
137 gnutls_record_send (session
, MSG
, strlen (MSG
));
139 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
142 success ("client: Peer has closed the TLS connection\n");
147 fail ("client: Error: %s\n", gnutls_strerror (ret
));
153 printf ("- Received %d bytes: ", ret
);
154 for (ii
= 0; ii
< ret
; ii
++)
156 fputc (buffer
[ii
], stdout
);
158 fputs ("\n", stdout
);
161 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
167 gnutls_deinit (session
);
169 gnutls_psk_free_client_credentials (pskcred
);
171 gnutls_global_deinit ();
174 /* This is a sample TLS 1.0 echo server, for PSK authentication.
177 #define SA struct sockaddr
179 #define PORT 5556 /* listen to 5556 port */
181 /* These are global */
182 gnutls_psk_server_credentials_t server_pskcred
;
185 initialize_tls_session (void)
187 gnutls_session_t session
;
188 const int kx_prio
[] = { GNUTLS_KX_PSK
, 0 };
190 gnutls_init (&session
, GNUTLS_SERVER
);
192 /* avoid calling all the priority functions, since the defaults
195 gnutls_set_default_priority (session
);
196 gnutls_kx_set_priority (session
, kx_prio
);
198 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, server_pskcred
);
204 pskfunc (gnutls_session_t session
, const char *username
, gnutls_datum_t
* key
)
206 printf ("psk: username %s\n", username
);
207 key
->data
= gnutls_malloc (4);
216 int err
, listen_sd
, i
;
218 struct sockaddr_in sa_serv
;
219 struct sockaddr_in sa_cli
;
222 gnutls_session_t session
;
223 char buffer
[MAX_BUF
+ 1];
229 /* this must be called once in the program
231 gnutls_global_init ();
233 gnutls_psk_allocate_server_credentials (&server_pskcred
);
234 gnutls_psk_set_server_credentials_function (server_pskcred
, pskfunc
);
236 success ("Launched...\n");
240 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
244 fail ("server: socket failed\n");
248 memset (&sa_serv
, '\0', sizeof (sa_serv
));
249 sa_serv
.sin_family
= AF_INET
;
250 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
251 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
253 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof (int));
255 err
= bind (listen_sd
, (SA
*) & sa_serv
, sizeof (sa_serv
));
259 fail ("server: bind failed\n");
263 err
= listen (listen_sd
, 1024);
267 fail ("server: listen failed\n");
271 success ("server: ready. Listening to port '%d'.\n", PORT
);
277 client_len
= sizeof (sa_cli
);
279 session
= initialize_tls_session ();
281 sd
= accept (listen_sd
, (SA
*) & sa_cli
, &client_len
);
283 success ("server: connection from %s, port %d\n",
284 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
285 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
287 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
288 ret
= gnutls_handshake (session
);
292 gnutls_deinit (session
);
293 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret
));
296 success ("server: Handshake was completed\n");
298 /* see the Getting peer's information example */
299 /* print_info(session); */
304 bzero (buffer
, MAX_BUF
+ 1);
305 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
309 success ("server: Peer has closed the GNUTLS connection\n");
314 fail ("server: Received corrupted data(%d). Closing...\n", ret
);
319 /* echo data back to the client
321 gnutls_record_send (session
, buffer
, strlen (buffer
));
324 /* do not wait for the peer to close the connection.
326 gnutls_bye (session
, GNUTLS_SHUT_WR
);
329 gnutls_deinit (session
);
333 gnutls_psk_free_server_credentials (server_pskcred
);
335 gnutls_global_deinit ();
337 success ("server: finished\n");