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 "utils.h"
52 
53 /* This program checks whether a TLS 1.3 client will detect
54  * a TLS 1.2 rollback attempt via the server random value.
55  */
56 
server_log_func(int level,const char * str)57 static void server_log_func(int level, const char *str)
58 {
59 	fprintf(stderr, "server|<%d>| %s", level, str);
60 }
61 
client_log_func(int level,const char * str)62 static void client_log_func(int level, const char *str)
63 {
64 	fprintf(stderr, "client|<%d>| %s", level, str);
65 }
66 
67 
68 
client(int fd)69 static void client(int fd)
70 {
71 	int ret;
72 	gnutls_certificate_credentials_t x509_cred;
73 	gnutls_session_t session;
74 
75 	global_init();
76 
77 	if (debug) {
78 		gnutls_global_set_log_function(client_log_func);
79 		gnutls_global_set_log_level(7);
80 	}
81 
82 	gnutls_certificate_allocate_credentials(&x509_cred);
83 	gnutls_certificate_set_x509_key_mem(x509_cred, &cli_ca3_cert,
84 					    &cli_ca3_key,
85 					    GNUTLS_X509_FMT_PEM);
86 
87 	/* Initialize TLS session
88 	 */
89 	gnutls_init(&session, GNUTLS_CLIENT);
90 
91 	gnutls_handshake_set_timeout(session, 20 * 1000);
92 
93 	ret = gnutls_priority_set_direct(session, "NORMAL:-VERS-ALL:+VERS-TLS1.3:+VERS-TLS1.2:+VERS-TLS1.1:+VERS-TLS1.0", NULL);
94 	if (ret < 0)
95 		fail("cannot set TLS 1.2 priorities\n");
96 
97 	/* put the anonymous credentials to the current session
98 	 */
99 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
100 
101 	gnutls_transport_set_int(session, fd);
102 
103 	/* Perform the TLS handshake
104 	 */
105 	do {
106 		ret = gnutls_handshake(session);
107 	}
108 	while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
109 
110 	if (ret != GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER) {
111 		fail("unexpected error during rollback: %s\n", gnutls_strerror(ret));
112 	}
113 
114 	close(fd);
115 
116 	gnutls_deinit(session);
117 
118 	gnutls_certificate_free_credentials(x509_cred);
119 
120 	gnutls_global_deinit();
121 }
122 
123 #ifdef TLS12
124 # define RND tls12_rnd
125 # define PRIO "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.2"
126 #elif TLS11
127 # define RND tls11_rnd
128 # define PRIO "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.1:+VERS-TLS1.0"
129 #else
130 # error unknown version to test
131 #endif
132 
133 gnutls_datum_t tls12_rnd = {(void*)"\x00\x00\x00\x04\x00\x00\x00\x04"
134 			      "\x00\x00\x00\x04\x00\x00\x00\x04"
135 			      "\x00\x00\x00\x04\x00\x00\x00\x04"
136 			      "\x44\x4F\x57\x4E\x47\x52\x44\x01",
137 			      32};
138 
139 gnutls_datum_t tls11_rnd = {(void*)"\x00\x00\x00\x04\x00\x00\x00\x04"
140 			      "\x00\x00\x00\x04\x00\x00\x00\x04"
141 			      "\x00\x00\x00\x04\x00\x00\x00\x04"
142 			      "\x44\x4F\x57\x4E\x47\x52\x44\x00",
143 			      32};
144 
server(int fd)145 static void server(int fd)
146 {
147 	int ret;
148 	gnutls_session_t session;
149 	gnutls_certificate_credentials_t x509_cred;
150 
151 	/* this must be called once in the program
152 	 */
153 	global_init();
154 
155 	if (debug) {
156 		gnutls_global_set_log_function(server_log_func);
157 		gnutls_global_set_log_level(4711);
158 	}
159 
160 	gnutls_certificate_allocate_credentials(&x509_cred);
161 	gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
162 					    &server_key,
163 					    GNUTLS_X509_FMT_PEM);
164 
165 	gnutls_init(&session, GNUTLS_SERVER);
166 
167 	gnutls_handshake_set_timeout(session, 20 * 1000);
168 	gnutls_handshake_set_random(session, &RND);
169 
170 	assert(gnutls_priority_set_direct(session, PRIO, NULL)>=0);
171 
172 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
173 
174 	gnutls_transport_set_int(session, fd);
175 
176 	do {
177 		ret = gnutls_handshake(session);
178 		if (ret == GNUTLS_E_INTERRUPTED) { /* expected */
179 			break;
180 		}
181 	} while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
182 
183 
184 	close(fd);
185 	gnutls_deinit(session);
186 
187 	gnutls_certificate_free_credentials(x509_cred);
188 
189 	gnutls_global_deinit();
190 
191 	if (debug)
192 		success("server: client/server hello were verified\n");
193 }
194 
ch_handler(int sig)195 static void ch_handler(int sig)
196 {
197 	int status = 0;
198 	wait(&status);
199 	check_wait_status(status);
200 	return;
201 }
202 
doit(void)203 void doit(void)
204 {
205 	int fd[2];
206 	int ret;
207 	pid_t child;
208 
209 	signal(SIGCHLD, ch_handler);
210 	signal(SIGPIPE, SIG_IGN);
211 
212 	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
213 	if (ret < 0) {
214 		perror("socketpair");
215 		exit(1);
216 	}
217 
218 	child = fork();
219 	if (child < 0) {
220 		perror("fork");
221 		fail("fork");
222 		exit(1);
223 	}
224 
225 	if (child) {
226 		/* parent */
227 		close(fd[1]);
228 		client(fd[0]);
229 		kill(child, SIGTERM);
230 	} else {
231 		close(fd[0]);
232 		server(fd[1]);
233 		exit(0);
234 	}
235 }
236 
237 #endif				/* _WIN32 */
238