1 /*
2  * Copyright (C) 2016 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 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 #include <string.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <gnutls/gnutls.h>
33 #include "utils.h"
34 #include "eagain-common.h"
35 
36 /* This test checks whether the client switching certificates is detected
37  * by the server */
38 
39 const char *side;
40 
tls_log_func(int level,const char * str)41 static void tls_log_func(int level, const char *str)
42 {
43 	fprintf(stderr, "%s|<%d>| %s", side, level, str);
44 }
45 
46 #include "cert-common.h"
47 
48 #define SENT 0
49 #define NOT_SENT 1
50 
51 enum {
52 	INCORRECT_CA_FORCE = 0,
53 	INCORRECT_CA = 1,
54 	CORRECT_CA = 2,
55 	NO_CA = 3
56 };
57 
try(const char * prio,unsigned expect,unsigned ca_type)58 static void try(const char *prio, unsigned expect, unsigned ca_type)
59 {
60 	int ret;
61 	/* Server stuff. */
62 	gnutls_certificate_credentials_t serverx509cred;
63 	gnutls_dh_params_t dh_params;
64 	const gnutls_datum_t p3 =
65 	    { (unsigned char *) pkcs3, strlen(pkcs3) };
66 	gnutls_session_t server;
67 	int sret = GNUTLS_E_AGAIN;
68 	/* Client stuff. */
69 	gnutls_certificate_credentials_t clientx509cred;
70 	unsigned flags = 0;
71 	gnutls_session_t client;
72 	int cret = GNUTLS_E_AGAIN;
73 
74 	/* General init. */
75 	gnutls_global_set_log_function(tls_log_func);
76 	if (debug)
77 		gnutls_global_set_log_level(6);
78 
79 	/* Init server */
80 	gnutls_certificate_allocate_credentials(&serverx509cred);
81 	gnutls_certificate_set_x509_key_mem(serverx509cred,
82 					    &server_ca3_cert_chain, &server_ca3_key,
83 					    GNUTLS_X509_FMT_PEM);
84 
85 	gnutls_dh_params_init(&dh_params);
86 	gnutls_dh_params_import_pkcs3(dh_params, &p3, GNUTLS_X509_FMT_PEM);
87 	gnutls_certificate_set_dh_params(serverx509cred, dh_params);
88 
89 	gnutls_init(&server, GNUTLS_SERVER);
90 	gnutls_certificate_server_set_request(server, GNUTLS_CERT_REQUEST);
91 
92 	if (ca_type == CORRECT_CA) {
93 		ret = gnutls_certificate_set_x509_trust_mem(serverx509cred, &ca3_cert, GNUTLS_X509_FMT_PEM);
94 	} else if (ca_type == INCORRECT_CA || ca_type == INCORRECT_CA_FORCE) {
95 		ret = gnutls_certificate_set_x509_trust_mem(serverx509cred, &unknown_ca_cert, GNUTLS_X509_FMT_PEM);
96 	} else if (ca_type == NO_CA) {
97 		ret = 0;
98 	} else {
99 		abort();
100 	}
101 		ret = 0;
102 	if (ret < 0)
103 		exit(1);
104 
105 
106 	gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE,
107 				serverx509cred);
108 
109 	assert(gnutls_priority_set_direct(server, prio, NULL) >= 0);
110 	gnutls_transport_set_push_function(server, server_push);
111 	gnutls_transport_set_pull_function(server, server_pull);
112 	gnutls_transport_set_ptr(server, server);
113 
114 	/* Init client */
115 
116 	ret = gnutls_certificate_allocate_credentials(&clientx509cred);
117 	if (ret < 0)
118 		exit(1);
119 
120 	ret = gnutls_certificate_set_x509_key_mem(clientx509cred,
121 						  &cli_ca3_cert_chain, &cli_ca3_key,
122 						  GNUTLS_X509_FMT_PEM);
123 	if (ret < 0)
124 		exit(1);
125 
126 
127 	ret = gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca3_cert, GNUTLS_X509_FMT_PEM);
128 	if (ret < 0)
129 		exit(1);
130 
131 	if (ca_type == INCORRECT_CA_FORCE) {
132 		flags |= GNUTLS_FORCE_CLIENT_CERT;
133 	}
134 
135 	ret = gnutls_init(&client, GNUTLS_CLIENT|flags);
136 	if (ret < 0)
137 		exit(1);
138 
139 	ret = gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE,
140 				clientx509cred);
141 	if (ret < 0)
142 		exit(1);
143 
144 	ret = gnutls_priority_set_direct(client, prio, NULL);
145 	if (ret < 0)
146 		exit(1);
147 
148 	gnutls_transport_set_push_function(client, client_push);
149 	gnutls_transport_set_pull_function(client, client_pull);
150 	gnutls_transport_set_ptr(client, client);
151 
152 	success("Testing CA type %d, expecting %s\n", ca_type, expect==SENT?"sent":"not sent");
153 
154 	HANDSHAKE(client, server);
155 
156 	if (expect == SENT) {
157 		if (gnutls_certificate_get_ours(client) == NULL) {
158 			fail("Test %d: client didn't send any certificate\n", ca_type);
159 			exit(1);
160 		}
161 	} else {
162 		if (gnutls_certificate_get_ours(client) != NULL) {
163 			fail("Test %d: client sent a certificate, although not expected\n", ca_type);
164 			exit(1);
165 		}
166 	}
167 
168 	gnutls_deinit(client);
169 	gnutls_deinit(server);
170 
171 	gnutls_certificate_free_credentials(serverx509cred);
172 	gnutls_certificate_free_credentials(clientx509cred);
173 	gnutls_dh_params_deinit(dh_params);
174 }
175 
start(const char * prio)176 static void start(const char *prio)
177 {
178 	global_init();
179 
180 	success("trying %s\n", prio);
181 
182 	try(prio, SENT, NO_CA);
183 	try(prio, SENT, CORRECT_CA);
184 	try(prio, NOT_SENT, INCORRECT_CA);
185 	try(prio, SENT, INCORRECT_CA_FORCE);
186 	gnutls_global_deinit();
187 }
188 
doit(void)189 void doit(void)
190 {
191 	start("NORMAL:-VERS-ALL:+VERS-TLS1.2");
192 	start("NORMAL:-VERS-ALL:+VERS-TLS1.3");
193 	start("NORMAL");
194 }
195