updated news
[gnutls.git] / doc / examples / ex-serv-psk.c
blobd2b907f4e11d1ea4588446b5e1a8b4d7bd10571b
1 /* This example code is placed in the public domain. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include <netinet/in.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <gnutls/gnutls.h>
18 #define KEYFILE "key.pem"
19 #define CERTFILE "cert.pem"
20 #define CAFILE "ca.pem"
21 #define CRLFILE "crl.pem"
23 /* This is a sample TLS echo server, supporting X.509 and PSK
24 authentication.
28 #define SA struct sockaddr
29 #define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
30 #define MAX_BUF 1024
31 #define PORT 5556 /* listen to 5556 port */
32 #define DH_BITS 1024
34 /* These are global */
35 gnutls_certificate_credentials_t x509_cred;
36 gnutls_psk_server_credentials_t psk_cred;
37 gnutls_priority_t priority_cache;
39 static gnutls_session_t
40 initialize_tls_session (void)
42 gnutls_session_t session;
44 gnutls_init (&session, GNUTLS_SERVER);
46 gnutls_priority_set (session, priority_cache);
48 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
49 gnutls_credentials_set (session, GNUTLS_CRD_PSK, psk_cred);
51 /* request client certificate if any.
53 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
55 return session;
58 static gnutls_dh_params_t dh_params;
60 static int
61 generate_dh_params (void)
64 /* Generate Diffie-Hellman parameters - for use with DHE
65 * kx algorithms. When short bit length is used, it might
66 * be wise to regenerate parameters.
68 * Check the ex-serv-export.c example for using static
69 * parameters.
71 gnutls_dh_params_init (&dh_params);
72 gnutls_dh_params_generate2 (dh_params, DH_BITS);
74 return 0;
77 static int
78 pskfunc (gnutls_session_t session, const char *username, gnutls_datum_t * key)
80 printf ("psk: username %s\n", username);
81 key->data = gnutls_malloc (4);
82 key->data[0] = 0xDE;
83 key->data[1] = 0xAD;
84 key->data[2] = 0xBE;
85 key->data[3] = 0xEF;
86 key->size = 4;
87 return 0;
90 int
91 main (void)
93 int err, listen_sd;
94 int sd, ret;
95 struct sockaddr_in sa_serv;
96 struct sockaddr_in sa_cli;
97 int client_len;
98 char topbuf[512];
99 gnutls_session_t session;
100 char buffer[MAX_BUF + 1];
101 int optval = 1;
103 /* this must be called once in the program
105 gnutls_global_init ();
107 gnutls_certificate_allocate_credentials (&x509_cred);
108 gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE,
109 GNUTLS_X509_FMT_PEM);
111 gnutls_certificate_set_x509_crl_file (x509_cred, CRLFILE,
112 GNUTLS_X509_FMT_PEM);
114 gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
115 GNUTLS_X509_FMT_PEM);
117 gnutls_psk_allocate_server_credentials (&psk_cred);
118 gnutls_psk_set_server_credentials_function (psk_cred, pskfunc);
120 generate_dh_params ();
122 gnutls_priority_init (&priority_cache, "NORMAL:PSK", NULL);
125 gnutls_certificate_set_dh_params (x509_cred, dh_params);
127 /* Socket operations
129 listen_sd = socket (AF_INET, SOCK_STREAM, 0);
130 SOCKET_ERR (listen_sd, "socket");
132 memset (&sa_serv, '\0', sizeof (sa_serv));
133 sa_serv.sin_family = AF_INET;
134 sa_serv.sin_addr.s_addr = INADDR_ANY;
135 sa_serv.sin_port = htons (PORT); /* Server Port number */
137 setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, (void *) &optval,
138 sizeof (int));
140 err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
141 SOCKET_ERR (err, "bind");
142 err = listen (listen_sd, 1024);
143 SOCKET_ERR (err, "listen");
145 printf ("Server ready. Listening to port '%d'.\n\n", PORT);
147 client_len = sizeof (sa_cli);
148 for (;;)
150 session = initialize_tls_session ();
152 sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
154 printf ("- connection from %s, port %d\n",
155 inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
156 sizeof (topbuf)), ntohs (sa_cli.sin_port));
158 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
159 ret = gnutls_handshake (session);
160 if (ret < 0)
162 close (sd);
163 gnutls_deinit (session);
164 fprintf (stderr, "*** Handshake has failed (%s)\n\n",
165 gnutls_strerror (ret));
166 continue;
168 printf ("- Handshake was completed\n");
170 /* see the Getting peer's information example */
171 /* print_info(session); */
173 for (;;)
175 memset (buffer, 0, MAX_BUF + 1);
176 ret = gnutls_record_recv (session, buffer, MAX_BUF);
178 if (ret == 0)
180 printf ("\n- Peer has closed the GnuTLS connection\n");
181 break;
183 else if (ret < 0)
185 fprintf (stderr, "\n*** Received corrupted "
186 "data(%d). Closing the connection.\n\n", ret);
187 break;
189 else if (ret > 0)
191 /* echo data back to the client
193 gnutls_record_send (session, buffer, strlen (buffer));
196 printf ("\n");
197 /* do not wait for the peer to close the connection.
199 gnutls_bye (session, GNUTLS_SHUT_WR);
201 close (sd);
202 gnutls_deinit (session);
205 close (listen_sd);
207 gnutls_certificate_free_credentials (x509_cred);
208 gnutls_psk_free_server_credentials (psk_cred);
210 gnutls_priority_deinit (priority_cache);
212 gnutls_global_deinit ();
214 return 0;