1 | |
2 | |
3 | #ifdef HAVE_CONFIG_H1 |
4 | #include <config.h> |
5 | #endif |
6 | |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <errno(*__errno_location ()).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> |
17 | |
18 | #define SRP_PASSWD"tpasswd" "tpasswd" |
19 | #define SRP_PASSWD_CONF"tpasswd.conf" "tpasswd.conf" |
20 | |
21 | #define KEYFILE"key.pem" "key.pem" |
22 | #define CERTFILE"cert.pem" "cert.pem" |
23 | #define CAFILE"ca.pem" "ca.pem" |
24 | |
25 | |
26 | |
27 | |
28 | #define SAstruct sockaddr struct sockaddr |
29 | #define SOCKET_ERR(err,s)if(err==-1) {perror(s);return(1);} if(err==-1) {perror(s);return(1);} |
30 | #define MAX_BUF1024 1024 |
31 | #define PORT5556 5556 /* listen to 5556 port */ |
32 | |
33 | |
34 | gnutls_srp_server_credentials_t srp_cred; |
35 | gnutls_certificate_credentials_t cert_cred; |
36 | |
37 | static gnutls_session_t |
38 | initialize_tls_session (void) |
39 | { |
40 | gnutls_session_t session; |
41 | |
42 | gnutls_init (&session, GNUTLS_SERVER1); |
43 | |
44 | gnutls_priority_set_direct (session, "NORMAL:-KX-ALL:+SRP:+SRP-DSS:+SRP-RSA", NULL((void*)0)); |
45 | |
46 | gnutls_credentials_set (session, GNUTLS_CRD_SRP, srp_cred); |
47 | |
48 | |
49 | gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, cert_cred); |
50 | |
51 | |
52 | |
53 | gnutls_certificate_server_set_request (session, GNUTLS_CERT_IGNORE); |
54 | |
55 | return session; |
56 | } |
57 | |
58 | int |
59 | main (void) |
60 | { |
61 | int err, listen_sd; |
62 | int sd, ret; |
63 | struct sockaddr_in sa_serv; |
64 | struct sockaddr_in sa_cli; |
65 | int client_len; |
66 | char topbuf[512]; |
67 | gnutls_session_t session; |
68 | char buffer[MAX_BUF1024 + 1]; |
69 | int optval = 1; |
70 | char name[256]; |
71 | |
72 | strcpy (name, "Echo Server"); |
73 | |
74 | gnutls_global_init (); |
75 | |
76 | |
77 | |
78 | gnutls_srp_allocate_server_credentials (&srp_cred); |
79 | gnutls_srp_set_server_credentials_file (srp_cred, SRP_PASSWD"tpasswd", |
80 | SRP_PASSWD_CONF"tpasswd.conf"); |
81 | |
82 | gnutls_certificate_allocate_credentials (&cert_cred); |
83 | gnutls_certificate_set_x509_trust_file (cert_cred, CAFILE"ca.pem", |
84 | GNUTLS_X509_FMT_PEM); |
85 | gnutls_certificate_set_x509_key_file (cert_cred, CERTFILE"cert.pem", KEYFILE"key.pem", |
86 | GNUTLS_X509_FMT_PEM); |
87 | |
88 | |
89 | |
90 | listen_sd = socket (AF_INET2, SOCK_STREAMSOCK_STREAM, 0); |
91 | SOCKET_ERR (listen_sd, "socket")if(listen_sd==-1) {perror("socket");return(1);}; |
92 | |
93 | memset (&sa_serv, '\0', sizeof (sa_serv)); |
94 | sa_serv.sin_family = AF_INET2; |
95 | sa_serv.sin_addr.s_addr = INADDR_ANY((in_addr_t) 0x00000000); |
96 | sa_serv.sin_port = htons (PORT5556); |
97 | |
98 | setsockopt (listen_sd, SOL_SOCKET1, SO_REUSEADDR2, (void *) &optval, |
99 | sizeof (int)); |
100 | |
101 | err = bind (listen_sd, (SAstruct sockaddr *) & sa_serv, sizeof (sa_serv)); |
102 | SOCKET_ERR (err, "bind")if(err==-1) {perror("bind");return(1);}; |
103 | err = listen (listen_sd, 1024); |
104 | SOCKET_ERR (err, "listen")if(err==-1) {perror("listen");return(1);}; |
105 | |
106 | printf ("%s ready. Listening to port '%d'.\n\n", name, PORT5556); |
107 | |
108 | client_len = sizeof (sa_cli); |
109 | for (;;) |
| 1 | Loop condition is true. Entering loop body |
|
110 | { |
111 | session = initialize_tls_session (); |
112 | |
113 | sd = accept (listen_sd, (SAstruct sockaddr *) & sa_cli, &client_len); |
114 | |
115 | printf ("- connection from %s, port %d\n", |
116 | inet_ntop (AF_INET2, &sa_cli.sin_addr, topbuf, |
117 | sizeof (topbuf)), ntohs (sa_cli.sin_port)); |
| 2 | Pass-by-value argument in function call is undefined |
|
118 | |
119 | gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd); |
120 | |
121 | do |
122 | { |
123 | ret = gnutls_handshake (session); |
124 | } |
125 | while (gnutls_error_is_fatal (ret) == 0); |
126 | |
127 | if (ret < 0) |
128 | { |
129 | close (sd); |
130 | gnutls_deinit (session); |
131 | fprintf (stderrstderr, "*** Handshake has failed (%s)\n\n", |
132 | gnutls_strerror (ret)); |
133 | continue; |
134 | } |
135 | printf ("- Handshake was completed\n"); |
136 | printf ("- User %s was connected\n", gnutls_srp_server_get_username(session)); |
137 | |
138 | |
139 | |
140 | for (;;) |
141 | { |
142 | memset (buffer, 0, MAX_BUF1024 + 1); |
143 | ret = gnutls_record_recv (session, buffer, MAX_BUF1024); |
144 | |
145 | if (ret == 0) |
146 | { |
147 | printf ("\n- Peer has closed the GnuTLS connection\n"); |
148 | break; |
149 | } |
150 | else if (ret < 0) |
151 | { |
152 | fprintf (stderrstderr, "\n*** Received corrupted " |
153 | "data(%d). Closing the connection.\n\n", ret); |
154 | break; |
155 | } |
156 | else if (ret > 0) |
157 | { |
158 | |
159 | |
160 | gnutls_record_send (session, buffer, strlen (buffer)); |
161 | } |
162 | } |
163 | printf ("\n"); |
164 | |
165 | gnutls_bye (session, GNUTLS_SHUT_WR); |
166 | |
167 | close (sd); |
168 | gnutls_deinit (session); |
169 | |
170 | } |
171 | close (listen_sd); |
172 | |
173 | gnutls_srp_free_server_credentials (srp_cred); |
174 | gnutls_certificate_free_credentials (cert_cred); |
175 | |
176 | gnutls_global_deinit (); |
177 | |
178 | return 0; |
179 | |
180 | } |