1 /*
2  * Copyright (C) 2016 Tim Kosse
3  *
4  * Author: Tim Kosse
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 General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #if defined(_WIN32)
31 
main()32 int main()
33 {
34 	exit(77);
35 }
36 
37 #else
38 
39 #include <string.h>
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <sys/wait.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <time.h>
47 #include <gnutls/gnutls.h>
48 #include <gnutls/dtls.h>
49 #include <signal.h>
50 
51 #include "utils.h"
52 
53 /* This program tests that handshakes fail if the server does not include the
54  * requested certificate status with the server certificate having
55  * TLS feature 5 (status request).
56  *
57  * See RFC 7633 section 4.2.3.1 paragraph 1
58  *
59  * Remark: Doesn't the MUST in section 4.3.3 para. 1 overrule the SHOULD of 4.2.3.1 para. 1?
60  */
61 
mytime(time_t * t)62 static time_t mytime(time_t * t)
63 {
64 	time_t then = 1464610242;
65 	if (t)
66 		*t = then;
67 
68 	return then;
69 }
70 
server_log_func(int level,const char * str)71 static void server_log_func(int level, const char *str)
72 {
73 	fprintf(stderr, "server|<%d>| %s", level, str);
74 }
75 
client_log_func(int level,const char * str)76 static void client_log_func(int level, const char *str)
77 {
78 	fprintf(stderr, "client|<%d>| %s", level, str);
79 }
80 
81 static unsigned char server_cert_pem[] =
82 		"-----BEGIN CERTIFICATE-----\n"
83 		"MIICBzCCAXCgAwIBAgIMVpjt8TL5Io/frpvkMA0GCSqGSIb3DQEBCwUAMCIxIDAe\n"
84 		"BgNVBAMTF0dudVRMUyB0ZXN0IGNlcnRpZmljYXRlMB4XDTE2MDExNTEzMDI0MVoX\n"
85 		"DTMyMDYxOTEzMDI0MVowIjEgMB4GA1UEAxMXR251VExTIHRlc3QgY2VydGlmaWNh\n"
86 		"dGUwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANe6XK+jDPAuqSdWqlOOqOt/\n"
87 		"gXVMa5i+Suq3HvhLw2rE2g0AuOpMEx82FpPecu/GpF6ybrbKCohVdZCW7aZXvAw7\n"
88 		"dg2XHr3p7H/Tqez7hWSga6BIznd+c5wxE/89yK6lYG7Ztoxamm+2vp9qvafwoDMn\n"
89 		"9bcdkuWWnHNS1p/WyI6xAgMBAAGjQjBAMBEGCCsGAQUFBwEYBAUwAwIBBTAMBgNV\n"
90 		"HRMBAf8EAjAAMB0GA1UdDgQWBBRTSzvcXshETAIgvzlIb0z+zSVSEDANBgkqhkiG\n"
91 		"9w0BAQsFAAOBgQB+VcJuLPL2PMog0HZ8RRbqVvLU5d209ROg3s1oXUBFW8+AV+71\n"
92 		"CsHg9Xx7vqKVwyKGI9ghds1B44lNPxGH2Sk1v2czjKbzwujo9+kLnDS6i0jyrDdn\n"
93 		"um4ivpkwmlUFSQVXvENLwe9gTlIgN4+0I9WLcMTCDtHWkcxMRwCm2BMsXw==\n"
94 		"-----END CERTIFICATE-----\n";
95 
96 
97 const gnutls_datum_t server_cert = { server_cert_pem,
98 	sizeof(server_cert_pem)
99 };
100 
101 static unsigned char server_key_pem[] =
102     "-----BEGIN RSA PRIVATE KEY-----\n"
103     "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
104     "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
105     "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
106     "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
107     "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
108     "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
109     "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
110     "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
111     "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
112     "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
113     "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
114     "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
115     "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
116     "-----END RSA PRIVATE KEY-----\n";
117 
118 const gnutls_datum_t server_key = { server_key_pem,
119 	sizeof(server_key_pem)
120 };
121 
122 static int received = 0;
123 
handshake_callback(gnutls_session_t session,unsigned int htype,unsigned post,unsigned int incoming,const gnutls_datum_t * msg)124 static int handshake_callback(gnutls_session_t session, unsigned int htype,
125 	unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
126 {
127 	fail("received status request\n");
128 	received = 1;
129 	return 0;
130 }
131 
132 #define MAX_BUF 1024
133 
client(int fd,const char * prio)134 static void client(int fd, const char *prio)
135 {
136 	int ret;
137 	unsigned int status;
138 	gnutls_certificate_credentials_t x509_cred;
139 	gnutls_session_t session;
140 	/* Need to enable anonymous KX specifically. */
141 
142 	gnutls_global_set_time_function(mytime);
143 	global_init();
144 
145 	if (debug) {
146 		gnutls_global_set_log_function(client_log_func);
147 		gnutls_global_set_log_level(7);
148 	}
149 
150 	gnutls_certificate_allocate_credentials(&x509_cred);
151 
152 	/* Initialize TLS session
153 	 */
154 	gnutls_init(&session, GNUTLS_CLIENT);
155 
156 	/* Use default priorities */
157 	gnutls_priority_set_direct(session, prio, NULL);
158 
159 	gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_CERTIFICATE_STATUS,
160 					   GNUTLS_HOOK_POST,
161 					   handshake_callback);
162 
163 	/* put the anonymous credentials to the current session
164 	 */
165 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
166 
167 	gnutls_transport_set_int(session, fd);
168 
169 	/* Perform the TLS handshake
170 	 */
171 	do {
172 		ret = gnutls_handshake(session);
173 	}
174 	while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
175 
176 	if (ret == GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM) {
177 		/* success */
178 		goto end;
179 	}
180 
181 	if (ret < 0) {
182 		fail("client: Handshake failed: %s\n", gnutls_strerror(ret));
183 	} else {
184 		if (debug)
185 			success("client: Handshake was completed\n");
186 	}
187 
188 	if (debug)
189 		success("client: TLS version is: %s\n",
190 			gnutls_protocol_get_name
191 			(gnutls_protocol_get_version(session)));
192 
193 	if (received == 1) {
194 		fail("client: received certificate status when we shouldn't.\n");
195 	}
196 
197 	ret = gnutls_certificate_verify_peers2(session, &status);
198 	if (ret != GNUTLS_E_SUCCESS) {
199 		fail("client: Peer certificate validation failed: %s\n", gnutls_strerror(ret));
200 	}
201 	else {
202 		if (status & GNUTLS_CERT_MISSING_OCSP_STATUS) {
203 			success("client: Validation failed with GNUTLS_CERT_MISSING_OCSP_STATUS\n");
204 		}
205 		else {
206 			fail("client: Validation status does not include GNUTLS_CERT_MISSING_OCSP_STATUS. Status is %d\n", status);
207 		}
208 	}
209 
210 	gnutls_bye(session, GNUTLS_SHUT_WR);
211 
212       end:
213 
214 	close(fd);
215 
216 	gnutls_deinit(session);
217 
218 	gnutls_certificate_free_credentials(x509_cred);
219 
220 	gnutls_global_deinit();
221 }
222 
223 
server(int fd,const char * prio)224 static void server(int fd, const char *prio)
225 {
226 	int ret;
227 	char buffer[MAX_BUF + 1];
228 	gnutls_session_t session;
229 	gnutls_certificate_credentials_t x509_cred;
230 
231 	/* this must be called once in the program
232 	 */
233 	global_init();
234 	memset(buffer, 0, sizeof(buffer));
235 
236 	if (debug) {
237 		gnutls_global_set_log_function(server_log_func);
238 		gnutls_global_set_log_level(4711);
239 	}
240 
241 	gnutls_certificate_allocate_credentials(&x509_cred);
242 	gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
243 					    &server_key,
244 					    GNUTLS_X509_FMT_PEM);
245 
246 	gnutls_init(&session, GNUTLS_SERVER);
247 
248 	/* avoid calling all the priority functions, since the defaults
249 	 * are adequate.
250 	 */
251 	gnutls_priority_set_direct(session, prio, NULL);
252 
253 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
254 
255 	gnutls_transport_set_int(session, fd);
256 
257 	do {
258 		ret = gnutls_handshake(session);
259 	} while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
260 	if (ret < 0) {
261 		/* failure is expected here */
262 		goto end;
263 	}
264 
265 	if (debug) {
266 		success("server: Handshake was completed\n");
267 	}
268 
269 	if (debug)
270 		success("server: TLS version is: %s\n",
271 			gnutls_protocol_get_name
272 			(gnutls_protocol_get_version(session)));
273 
274 	/* do not wait for the peer to close the connection.
275 	 */
276 	gnutls_bye(session, GNUTLS_SHUT_WR);
277 
278  end:
279 	close(fd);
280 	gnutls_deinit(session);
281 
282 	gnutls_certificate_free_credentials(x509_cred);
283 
284 	gnutls_global_deinit();
285 
286 	if (debug)
287 		success("server: finished\n");
288 }
289 
ch_handler(int sig)290 static void ch_handler(int sig)
291 {
292 	return;
293 }
294 
start(const char * name,const char * prio)295 static void start(const char *name, const char *prio)
296 {
297 	pid_t child;
298 	int fd[2];
299 	int ret, status = 0;
300 
301 	signal(SIGCHLD, ch_handler);
302 	signal(SIGPIPE, SIG_IGN);
303 
304 	received = 0;
305 	success("running: %s\n", name);
306 
307 	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
308 	if (ret < 0) {
309 		perror("socketpair");
310 		exit(1);
311 	}
312 
313 	child = fork();
314 	if (child < 0) {
315 		perror("fork");
316 		fail("fork");
317 		exit(1);
318 	}
319 
320 	if (child) {
321 		/* parent */
322 		close(fd[1]);
323 		client(fd[0], prio);
324 		waitpid(child, &status, 0);
325 		check_wait_status(status);
326 	} else {
327 		close(fd[0]);
328 		server(fd[1], prio);
329 		exit(0);
330 	}
331 
332 	return;
333 }
334 
doit(void)335 void doit(void)
336 {
337 	start("tls1.2", "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.2");
338 	start("tls1.3", "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3");
339 	start("default", "NORMAL");
340 }
341 
342 #endif				/* _WIN32 */
343