1 /*
2  * Copyright (C) 2017 Red Hat, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 #if defined(_WIN32)
30 
main()31 int main()
32 {
33 	exit(77);
34 }
35 
36 #else
37 
38 #include <string.h>
39 #include <sys/types.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <sys/wait.h>
43 #include <arpa/inet.h>
44 #include <unistd.h>
45 #include <gnutls/gnutls.h>
46 #include <gnutls/dtls.h>
47 #include <signal.h>
48 #include <assert.h>
49 
50 #include "cert-common.h"
51 #include "tls13/ext-parse.h"
52 #include "utils.h"
53 
54 /* This program tests whether the Post Handshake Auth extension is missing
55  * from both hellos, when not enabled by client.
56  */
57 
server_log_func(int level,const char * str)58 static void server_log_func(int level, const char *str)
59 {
60 	fprintf(stderr, "server|<%d>| %s", level, str);
61 }
62 
client_log_func(int level,const char * str)63 static void client_log_func(int level, const char *str)
64 {
65 	fprintf(stderr, "client|<%d>| %s", level, str);
66 }
67 
68 #define MAX_BUF 1024
69 
client(int fd)70 static void client(int fd)
71 {
72 	int ret;
73 	gnutls_certificate_credentials_t x509_cred;
74 	gnutls_session_t session;
75 
76 	global_init();
77 
78 	if (debug) {
79 		gnutls_global_set_log_function(client_log_func);
80 		gnutls_global_set_log_level(7);
81 	}
82 
83 	gnutls_certificate_allocate_credentials(&x509_cred);
84 
85 	assert(gnutls_certificate_set_x509_key_mem(x509_cred, &cli_ca3_cert,
86 						   &cli_ca3_key,
87 						   GNUTLS_X509_FMT_PEM) >= 0);
88 
89 	/* Initialize TLS session
90 	 */
91 	gnutls_init(&session, GNUTLS_CLIENT);
92 
93 	gnutls_handshake_set_timeout(session, 20 * 1000);
94 
95 	ret = gnutls_priority_set_direct(session, "NORMAL:-VERS-ALL:+VERS-TLS1.3:+VERS-TLS1.2:+VERS-TLS1.0", NULL);
96 	if (ret < 0)
97 		fail("cannot set TLS 1.3 priorities\n");
98 
99 	/* put the anonymous credentials to the current session
100 	 */
101 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
102 
103 	gnutls_transport_set_int(session, fd);
104 
105 	/* Perform the TLS handshake
106 	 */
107 	do {
108 		ret = gnutls_handshake(session);
109 	}
110 	while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
111 
112 	/* try if gnutls_reauth() would fail as expected */
113 	ret = gnutls_reauth(session, 0);
114 	if (ret != GNUTLS_E_INVALID_REQUEST)
115 		fail("server: gnutls_reauth did not fail as expected: %s", gnutls_strerror(ret));
116 
117 	close(fd);
118 
119 	gnutls_deinit(session);
120 
121 	gnutls_certificate_free_credentials(x509_cred);
122 
123 	gnutls_global_deinit();
124 }
125 
126 static unsigned server_hello_ok = 0;
127 
128 #define TLS_EXT_POST_HANDSHAKE 49
129 
hellos_callback(gnutls_session_t session,unsigned int htype,unsigned post,unsigned int incoming,const gnutls_datum_t * msg)130 static int hellos_callback(gnutls_session_t session, unsigned int htype,
131 	unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
132 {
133 	if (htype == GNUTLS_HANDSHAKE_SERVER_HELLO && post == GNUTLS_HOOK_POST) {
134 		if (find_server_extension(msg, TLS_EXT_POST_HANDSHAKE, NULL, NULL)) {
135 			fail("Post handshake extension seen in server hello!\n");
136 		}
137 		server_hello_ok = 1;
138 
139 		return GNUTLS_E_INTERRUPTED;
140 	}
141 
142 	if (htype != GNUTLS_HANDSHAKE_CLIENT_HELLO || post != GNUTLS_HOOK_PRE)
143 		return 0;
144 
145 	if (find_client_extension(msg, TLS_EXT_POST_HANDSHAKE, NULL, NULL))
146 		fail("Post handshake extension seen in client hello with no cert!\n");
147 
148 	return 0;
149 }
150 
server(int fd)151 static void server(int fd)
152 {
153 	int ret;
154 	char buffer[MAX_BUF + 1];
155 	gnutls_session_t session;
156 	gnutls_certificate_credentials_t x509_cred;
157 
158 	/* this must be called once in the program
159 	 */
160 	global_init();
161 	memset(buffer, 0, sizeof(buffer));
162 
163 	if (debug) {
164 		gnutls_global_set_log_function(server_log_func);
165 		gnutls_global_set_log_level(4711);
166 	}
167 
168 	gnutls_certificate_allocate_credentials(&x509_cred);
169 	gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
170 					    &server_key,
171 					    GNUTLS_X509_FMT_PEM);
172 
173 	gnutls_init(&session, GNUTLS_SERVER);
174 
175 	gnutls_handshake_set_timeout(session, 20 * 1000);
176 	gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_ANY,
177 					   GNUTLS_HOOK_BOTH,
178 					   hellos_callback);
179 
180 	/* avoid calling all the priority functions, since the defaults
181 	 * are adequate.
182 	 */
183 	gnutls_priority_set_direct(session, "NORMAL:+VERS-TLS1.3", NULL);
184 
185 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
186 
187 	gnutls_transport_set_int(session, fd);
188 
189 	do {
190 		ret = gnutls_handshake(session);
191 		if (ret == GNUTLS_E_INTERRUPTED) { /* expected */
192 			break;
193 		}
194 	} while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
195 
196 	if ((gnutls_session_get_flags(session) & GNUTLS_SFLAGS_POST_HANDSHAKE_AUTH)) {
197 		fail("server: session flags did contain GNUTLS_SFLAGS_POST_HANDSHAKE_AUTH\n");
198 	}
199 
200 	if (server_hello_ok == 0) {
201 		fail("server: did not verify the server hello contents\n");
202 	}
203 
204 	/* try if gnutls_reauth() would fail as expected */
205 	ret = gnutls_reauth(session, 0);
206 	if (ret != GNUTLS_E_INVALID_REQUEST)
207 		fail("server: gnutls_reauth did not fail as expected: %s", gnutls_strerror(ret));
208 
209 	close(fd);
210 	gnutls_deinit(session);
211 
212 	gnutls_certificate_free_credentials(x509_cred);
213 
214 	gnutls_global_deinit();
215 
216 	if (debug)
217 		success("server: client/server hello were verified\n");
218 }
219 
ch_handler(int sig)220 static void ch_handler(int sig)
221 {
222 	int status = 0;
223 	wait(&status);
224 	check_wait_status(status);
225 	return;
226 }
227 
doit(void)228 void doit(void)
229 {
230 	int fd[2];
231 	int ret;
232 	pid_t child;
233 
234 	signal(SIGCHLD, ch_handler);
235 	signal(SIGPIPE, SIG_IGN);
236 
237 	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
238 	if (ret < 0) {
239 		perror("socketpair");
240 		exit(1);
241 	}
242 
243 	child = fork();
244 	if (child < 0) {
245 		perror("fork");
246 		fail("fork");
247 		exit(1);
248 	}
249 
250 	if (child) {
251 		/* parent */
252 		close(fd[1]);
253 		server(fd[0]);
254 		kill(child, SIGTERM);
255 	} else {
256 		close(fd[0]);
257 		client(fd[1]);
258 		exit(0);
259 	}
260 }
261 
262 #endif				/* _WIN32 */
263