1 /* This example code is placed in the public domain. */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include <netinet/in.h>
16 #include <gnutls/gnutls.h>
18 #define SRP_PASSWD "tpasswd"
19 #define SRP_PASSWD_CONF "tpasswd.conf"
21 #define KEYFILE "key.pem"
22 #define CERTFILE "cert.pem"
23 #define CAFILE "/etc/ssl/certs/ca-certificates.crt"
25 /* This is a sample TLS-SRP echo server.
28 #define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
30 #define PORT 5556 /* listen to 5556 port */
32 /* These are global */
33 gnutls_srp_server_credentials_t srp_cred
;
34 gnutls_certificate_credentials_t cert_cred
;
36 static gnutls_session_t
37 initialize_tls_session (void)
39 gnutls_session_t session
;
41 gnutls_init (&session
, GNUTLS_SERVER
);
43 gnutls_priority_set_direct (session
, "NORMAL:-KX-ALL:+SRP:+SRP-DSS:+SRP-RSA", NULL
);
45 gnutls_credentials_set (session
, GNUTLS_CRD_SRP
, srp_cred
);
46 /* for the certificate authenticated ciphersuites.
48 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, cert_cred
);
50 /* request client certificate if any.
52 gnutls_certificate_server_set_request (session
, GNUTLS_CERT_IGNORE
);
62 struct sockaddr_in sa_serv
;
63 struct sockaddr_in sa_cli
;
66 gnutls_session_t session
;
67 char buffer
[MAX_BUF
+ 1];
71 strcpy (name
, "Echo Server");
73 gnutls_global_init ();
75 /* SRP_PASSWD a password file (created with the included srptool utility)
77 gnutls_srp_allocate_server_credentials (&srp_cred
);
78 gnutls_srp_set_server_credentials_file (srp_cred
, SRP_PASSWD
,
81 gnutls_certificate_allocate_credentials (&cert_cred
);
82 gnutls_certificate_set_x509_trust_file (cert_cred
, CAFILE
,
84 gnutls_certificate_set_x509_key_file (cert_cred
, CERTFILE
, KEYFILE
,
87 /* TCP socket operations
89 listen_sd
= socket (AF_INET
, SOCK_STREAM
, 0);
90 SOCKET_ERR (listen_sd
, "socket");
92 memset (&sa_serv
, '\0', sizeof (sa_serv
));
93 sa_serv
.sin_family
= AF_INET
;
94 sa_serv
.sin_addr
.s_addr
= INADDR_ANY
;
95 sa_serv
.sin_port
= htons (PORT
); /* Server Port number */
97 setsockopt (listen_sd
, SOL_SOCKET
, SO_REUSEADDR
, (void *) &optval
,
100 err
= bind (listen_sd
, (struct sockaddr
*) & sa_serv
, sizeof (sa_serv
));
101 SOCKET_ERR (err
, "bind");
102 err
= listen (listen_sd
, 1024);
103 SOCKET_ERR (err
, "listen");
105 printf ("%s ready. Listening to port '%d'.\n\n", name
, PORT
);
107 client_len
= sizeof (sa_cli
);
110 session
= initialize_tls_session ();
112 sd
= accept (listen_sd
, (struct sockaddr
*) & sa_cli
, &client_len
);
114 printf ("- connection from %s, port %d\n",
115 inet_ntop (AF_INET
, &sa_cli
.sin_addr
, topbuf
,
116 sizeof (topbuf
)), ntohs (sa_cli
.sin_port
));
118 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
122 ret
= gnutls_handshake (session
);
124 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
129 gnutls_deinit (session
);
130 fprintf (stderr
, "*** Handshake has failed (%s)\n\n",
131 gnutls_strerror (ret
));
134 printf ("- Handshake was completed\n");
135 printf ("- User %s was connected\n", gnutls_srp_server_get_username(session
));
137 /* print_info(session); */
141 memset (buffer
, 0, MAX_BUF
+ 1);
142 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
146 printf ("\n- Peer has closed the GnuTLS connection\n");
151 fprintf (stderr
, "\n*** Received corrupted "
152 "data(%d). Closing the connection.\n\n", ret
);
157 /* echo data back to the client
159 gnutls_record_send (session
, buffer
, strlen (buffer
));
163 /* do not wait for the peer to close the connection. */
164 gnutls_bye (session
, GNUTLS_SHUT_WR
);
167 gnutls_deinit (session
);
172 gnutls_srp_free_server_credentials (srp_cred
);
173 gnutls_certificate_free_credentials (cert_cred
);
175 gnutls_global_deinit ();