1 /*
2  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
3  * Copyright (C) 2013 Adam Sampson <ats@offog.org>
4  * Copyright (C) 2013-2017 Nikos Mavrogiannopoulos
5  *
6  * Author: Nikos Mavrogiannopoulos
7  *
8  * This file is part of GnuTLS.
9  *
10  * GnuTLS is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * GnuTLS is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with GnuTLS; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 /* Tests the RSA-PSK ciphersuites under TLS1.2 */
26 
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 
34 #if defined(_WIN32)
35 
36 /* socketpair isn't supported on Win32. */
main(int argc,char ** argv)37 int main(int argc, char **argv)
38 {
39 	exit(77);
40 }
41 
42 #else
43 
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/wait.h>
48 #include <unistd.h>
49 #include <assert.h>
50 #include <gnutls/gnutls.h>
51 #include "cert-common.h"
52 #include "utils.h"
53 
54 /* A very basic TLS client, with PSK authentication.
55  */
56 
57 const char *side = "";
58 
tls_log_func(int level,const char * str)59 static void tls_log_func(int level, const char *str)
60 {
61 	fprintf(stderr, "%s|<%d>| %s", side, level, str);
62 }
63 
64 #define MAX_BUF 1024
65 #define MSG "Hello TLS"
66 
client(int sd)67 static void client(int sd)
68 {
69 	int ret, ii;
70 	gnutls_session_t session;
71 	gnutls_certificate_credentials_t clientx509cred;
72 	char buffer[MAX_BUF + 1];
73 	gnutls_psk_client_credentials_t pskcred;
74 	/* Need to enable anonymous KX specifically. */
75 	const gnutls_datum_t key = { (void *) "DEADBEEF", 8 };
76 
77 	global_init();
78 	gnutls_global_set_log_function(tls_log_func);
79 	if (debug)
80 		gnutls_global_set_log_level(4711);
81 
82 	side = "client";
83 
84 	gnutls_certificate_allocate_credentials(&clientx509cred);
85 
86 	gnutls_psk_allocate_client_credentials(&pskcred);
87 	gnutls_psk_set_client_credentials(pskcred, "test", &key,
88 					  GNUTLS_PSK_KEY_HEX);
89 
90 	/* Initialize TLS session
91 	 */
92 	gnutls_init(&session, GNUTLS_CLIENT);
93 
94 	/* Use default priorities */
95 	gnutls_priority_set_direct(session, "NORMAL:-VERS-ALL:+VERS-TLS1.2:-KX-ALL:+RSA-PSK",
96 				   NULL);
97 
98 	/* put the anonymous credentials to the current session
99 	 */
100 	gnutls_credentials_set(session, GNUTLS_CRD_PSK, pskcred);
101 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
102 				clientx509cred);
103 
104 	gnutls_handshake_set_timeout(session, 20 * 1000);
105 	gnutls_transport_set_int(session, sd);
106 
107 	/* Perform the TLS handshake
108 	 */
109 	ret = gnutls_handshake(session);
110 
111 	if (ret < 0) {
112 		fail("client: Handshake failed\n");
113 		gnutls_perror(ret);
114 		goto end;
115 	} else {
116 		if (debug)
117 			success("client: Handshake was completed\n");
118 	}
119 
120 	gnutls_record_send(session, MSG, strlen(MSG));
121 
122 	ret = gnutls_record_recv(session, buffer, MAX_BUF);
123 	if (ret == 0) {
124 		if (debug)
125 			success
126 			    ("client: Peer has closed the TLS connection\n");
127 		goto end;
128 	} else if (ret < 0) {
129 		fail("client: Error: %s\n", gnutls_strerror(ret));
130 		goto end;
131 	}
132 
133 	if (debug) {
134 		printf("- Received %d bytes: ", ret);
135 		for (ii = 0; ii < ret; ii++) {
136 			fputc(buffer[ii], stdout);
137 		}
138 		fputs("\n", stdout);
139 	}
140 
141 	gnutls_bye(session, GNUTLS_SHUT_RDWR);
142 
143       end:
144 
145 	close(sd);
146 
147 	gnutls_deinit(session);
148 
149 	gnutls_psk_free_client_credentials(pskcred);
150 	gnutls_certificate_free_credentials(clientx509cred);
151 
152 	gnutls_global_deinit();
153 }
154 
155 /* This is a sample TLS 1.0 echo server, for PSK authentication.
156  */
157 
158 #define MAX_BUF 1024
159 
160 static int
psk_server_func(gnutls_session_t session,const char * username,gnutls_datum_t * key)161 psk_server_func(gnutls_session_t session, const char *username,
162 	gnutls_datum_t * key)
163 {
164 	if (debug)
165 		printf("psk: username %s\n", username);
166 
167 	if (strcmp(username, "test") != 0) {
168 		fail("error in received username: '%s'\n", username);
169 	}
170 
171 	key->data = gnutls_malloc(4);
172 	assert(key->data != NULL);
173 	key->data[0] = 0xDE;
174 	key->data[1] = 0xAD;
175 	key->data[2] = 0xBE;
176 	key->data[3] = 0xEF;
177 	key->size = 4;
178 	return 0;
179 }
180 
181 int err, ret;
182 char topbuf[512];
183 gnutls_session_t session;
184 char buffer[MAX_BUF + 1];
185 int optval = 1;
186 
server(int sd)187 static void server(int sd)
188 {
189 	gnutls_certificate_credentials_t serverx509cred;
190 	gnutls_psk_server_credentials_t server_pskcred;
191 
192 	/* this must be called once in the program
193 	 */
194 	global_init();
195 	gnutls_global_set_log_function(tls_log_func);
196 	if (debug)
197 		gnutls_global_set_log_level(4711);
198 
199 	side = "server";
200 
201 	gnutls_psk_allocate_server_credentials(&server_pskcred);
202 	gnutls_psk_set_server_credentials_function(server_pskcred,
203 						   psk_server_func);
204 	gnutls_psk_set_server_credentials_hint(server_pskcred, "hint");
205 	gnutls_certificate_allocate_credentials(&serverx509cred);
206 	gnutls_certificate_set_x509_key_mem(serverx509cred,
207 					    &server_cert, &server_key,
208 					    GNUTLS_X509_FMT_PEM);
209 
210 	gnutls_init(&session, GNUTLS_SERVER);
211 
212 	/* avoid calling all the priority functions, since the defaults
213 	 * are adequate.
214 	 */
215 	gnutls_priority_set_direct(session, "NORMAL:-VERS-ALL:+VERS-TLS1.2:-KX-ALL:+RSA-PSK",
216 				   NULL);
217 
218 
219 	gnutls_credentials_set(session, GNUTLS_CRD_PSK, server_pskcred);
220 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
221 				serverx509cred);
222 
223 	gnutls_handshake_set_timeout(session, 20 * 1000);
224 	gnutls_transport_set_int(session, sd);
225 	ret = gnutls_handshake(session);
226 	if (ret < 0) {
227 		close(sd);
228 		gnutls_deinit(session);
229 		fail("server: Handshake has failed (%s)\n\n",
230 		     gnutls_strerror(ret));
231 		return;
232 	}
233 	if (debug)
234 		success("server: Handshake was completed\n");
235 
236 	/* see the Getting peer's information example */
237 	/* print_info(session); */
238 
239 	for (;;) {
240 		memset(buffer, 0, MAX_BUF + 1);
241 		ret = gnutls_record_recv(session, buffer, MAX_BUF);
242 
243 		if (ret == 0) {
244 			if (debug)
245 				success
246 				    ("server: Peer has closed the GnuTLS connection\n");
247 			break;
248 		} else if (ret < 0) {
249 			fail("server: Received corrupted data(%d). Closing...\n", ret);
250 			break;
251 		} else if (ret > 0) {
252 			/* echo data back to the client
253 			 */
254 			gnutls_record_send(session, buffer,
255 					   strlen(buffer));
256 		}
257 	}
258 	/* do not wait for the peer to close the connection.
259 	 */
260 	gnutls_bye(session, GNUTLS_SHUT_WR);
261 
262 	close(sd);
263 	gnutls_deinit(session);
264 
265 	gnutls_psk_free_server_credentials(server_pskcred);
266 	gnutls_certificate_free_credentials(serverx509cred);
267 
268 	gnutls_global_deinit();
269 
270 	if (debug)
271 		success("server: finished\n");
272 }
273 
doit(void)274 void doit(void)
275 {
276 	pid_t child;
277 	int sockets[2];
278 
279 	err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
280 	if (err == -1) {
281 		perror("socketpair");
282 		fail("socketpair failed\n");
283 		return;
284 	}
285 
286 	child = fork();
287 	if (child < 0) {
288 		perror("fork");
289 		fail("fork");
290 		return;
291 	}
292 
293 	if (child) {
294 		int status;
295 		/* parent */
296 		close(sockets[1]);
297 		server(sockets[0]);
298 		wait(&status);
299 		check_wait_status(status);
300 	} else {
301 		close(sockets[0]);
302 		client(sockets[1]);
303 		exit(0);
304 	}
305 }
306 
307 #endif				/* _WIN32 */
308