1 /*
2  * Copyright (C) 2008-2012 Free Software Foundation, Inc.
3  * Copyright (C) 2018 Red Hat, Inc.
4  *
5  * Author: Simon Josefsson, Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS.
8  *
9  * GnuTLS is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * GnuTLS is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program.  If not, see <https://www.gnu.org/licenses/>
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 <gnutls/gnutls.h>
32 #include <gnutls/crypto.h>
33 #include <gnutls/abstract.h>
34 
35 #define RANDOMIZE
36 #include "cert-common.h"
37 #include "cmocka-common.h"
38 
39 /* This tests operation under non-blocking mode in TLS1.2/TLS1.3
40  * rekey/rehandshake.
41  */
tls_log_func(int level,const char * str)42 static void tls_log_func(int level, const char *str)
43 {
44 	fprintf(stderr, "<%d>| %s", level, str);
45 }
46 
47 #define MAX_BUF 1024
48 #define MSG "Hello TLS, and hi and how are you and more data here... and more... and even more and even more more data..."
49 
50 static unsigned int cert_asked = 0;
51 
cert_callback(gnutls_session_t session,const gnutls_datum_t * req_ca_rdn,int nreqs,const gnutls_pk_algorithm_t * sign_algos,int sign_algos_length,gnutls_pcert_st ** pcert,unsigned int * pcert_length,gnutls_privkey_t * pkey)52 static int cert_callback(gnutls_session_t session,
53 			 const gnutls_datum_t * req_ca_rdn, int nreqs,
54 			 const gnutls_pk_algorithm_t * sign_algos,
55 			 int sign_algos_length, gnutls_pcert_st ** pcert,
56 			 unsigned int *pcert_length, gnutls_privkey_t * pkey)
57 {
58 	cert_asked = 1;
59 	*pcert_length = 0;
60 	*pcert = NULL;
61 	*pkey = NULL;
62 
63 	return 0;
64 }
65 
async_handshake(void ** glob_state,const char * prio,unsigned rehsk)66 static void async_handshake(void **glob_state, const char *prio, unsigned rehsk)
67 {
68 	/* Server stuff. */
69 	gnutls_certificate_credentials_t serverx509cred;
70 	gnutls_session_t server;
71 	int sret, cret;
72 	/* Client stuff. */
73 	gnutls_certificate_credentials_t clientx509cred;
74 	gnutls_session_t client;
75 	/* Need to enable anonymous KX specifically. */
76 	char buffer[MAX_BUF + 1];
77 	int ret, transferred = 0, msglen;
78 
79 	/* General init. */
80 	reset_buffers();
81 	cert_asked = 0;
82 	gnutls_global_init();
83 	gnutls_global_set_log_function(tls_log_func);
84 
85 	/* Init server */
86 	assert_return_code(gnutls_certificate_allocate_credentials(&serverx509cred), 0);
87 	assert_return_code(gnutls_certificate_set_x509_key_mem(serverx509cred,
88 					    &server_cert, &server_key,
89 					    GNUTLS_X509_FMT_PEM), 0);
90 	ret = gnutls_init(&server, GNUTLS_SERVER|GNUTLS_POST_HANDSHAKE_AUTH);
91 	assert_return_code(ret, 0);
92 
93 	ret =
94 	    gnutls_priority_set_direct(server,
95 					prio,
96 					NULL);
97 	assert_return_code(ret, 0);
98 
99 
100 	ret = gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, serverx509cred);
101 	assert_return_code(ret, 0);
102 
103 	gnutls_transport_set_push_function(server, server_push);
104 	gnutls_transport_set_pull_function(server, server_pull);
105 	gnutls_transport_set_ptr(server, server);
106 
107 	/* Init client */
108 
109 	ret = gnutls_certificate_allocate_credentials(&clientx509cred);
110 	assert_return_code(ret, 0);
111 
112 	gnutls_certificate_set_retrieve_function2(clientx509cred, cert_callback);
113 
114 	ret = gnutls_init(&client, GNUTLS_CLIENT|GNUTLS_AUTO_REAUTH|GNUTLS_POST_HANDSHAKE_AUTH);
115 	ret =
116 	    gnutls_priority_set_direct(client,
117 					prio,
118 					NULL);
119 	assert_return_code(ret, 0);
120 
121 	ret = gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, clientx509cred);
122 	assert_return_code(ret, 0);
123 
124 	gnutls_transport_set_push_function(client, client_push);
125 	gnutls_transport_set_pull_function(client, client_pull);
126 	gnutls_transport_set_ptr(client, client);
127 
128 	HANDSHAKE(client, server);
129 
130 	if (rehsk == 1) {
131 		char b[1];
132 		unsigned hstarted = 0;
133 
134 		do {
135 			sret = gnutls_rehandshake(server);
136 		} while (sret == GNUTLS_E_AGAIN || sret == GNUTLS_E_INTERRUPTED);
137 		assert_true(sret == 0);
138 		assert_true(gnutls_record_get_direction(server)==1);
139 
140 		sret = cret = GNUTLS_E_AGAIN;
141 		do {
142 			if (!hstarted) {
143 				sret = gnutls_record_recv(server, b, 1);
144 				if (sret == GNUTLS_E_INTERRUPTED) sret = GNUTLS_E_AGAIN;
145 
146 				if (sret == GNUTLS_E_REHANDSHAKE) {
147 					hstarted = 1;
148 					sret = GNUTLS_E_AGAIN;
149 				}
150 				assert_true(sret == GNUTLS_E_AGAIN);
151 			}
152 
153 			if (sret == GNUTLS_E_AGAIN && hstarted) {
154 				sret = gnutls_handshake (server);
155 				if (sret == GNUTLS_E_INTERRUPTED) sret = GNUTLS_E_AGAIN;
156 				assert_true(sret == GNUTLS_E_AGAIN || sret == 0);
157 			}
158 
159 			/* we are done in client side */
160 			if (hstarted && gnutls_record_get_direction(client) == 0 && to_client_len == 0)
161 				cret = 0;
162 
163 			if (cret == GNUTLS_E_AGAIN) {
164 				cret = gnutls_record_recv(client, b, 1);
165 				if (cret == GNUTLS_E_INTERRUPTED) cret = GNUTLS_E_AGAIN;
166 			}
167 			assert_true(cret == GNUTLS_E_AGAIN || cret >= 0);
168 
169 		} while (cret == GNUTLS_E_AGAIN || sret == GNUTLS_E_AGAIN);
170 		assert_true(hstarted != 0);
171 	} else {
172 		char b[1];
173 
174 		gnutls_certificate_server_set_request(server, GNUTLS_CERT_REQUEST);
175 
176 		do {
177 			sret = gnutls_reauth(server, 0);
178 		} while (sret == GNUTLS_E_INTERRUPTED);
179 
180 		assert_true(sret == GNUTLS_E_AGAIN || sret >= 0);
181 
182 		cret = GNUTLS_E_AGAIN;
183 		do {
184 			if (cret == GNUTLS_E_AGAIN) {
185 				cret = gnutls_record_recv(client, b, 1);
186 				if (cret == GNUTLS_E_INTERRUPTED) cret = GNUTLS_E_AGAIN;
187 			}
188 
189 			if (sret == GNUTLS_E_AGAIN) {
190 				sret = gnutls_reauth(server, 0);
191 				if (sret == GNUTLS_E_INTERRUPTED) sret = GNUTLS_E_AGAIN;
192 			}
193 
194 			/* we are done in client side */
195 			if (gnutls_record_get_direction(client) == 0 && to_client_len == 0)
196 				cret = 0;
197 		} while (cret == GNUTLS_E_AGAIN || sret == GNUTLS_E_AGAIN);
198 	}
199 	assert_return_code(cret, 0);
200 	assert_return_code(sret, 0);
201 	assert_return_code(cert_asked, 1);
202 
203 	msglen = strlen(MSG);
204 	TRANSFER(client, server, MSG, msglen, buffer, MAX_BUF);
205 
206 	assert_true(gnutls_bye(client, GNUTLS_SHUT_WR)>=0);
207 	assert_true(gnutls_bye(server, GNUTLS_SHUT_WR)>=0);
208 
209 	gnutls_deinit(client);
210 	gnutls_deinit(server);
211 
212 	gnutls_certificate_free_credentials(serverx509cred);
213 	gnutls_certificate_free_credentials(clientx509cred);
214 
215 	gnutls_global_deinit();
216 }
217 
tls12_async_handshake(void ** glob_state)218 static void tls12_async_handshake(void **glob_state)
219 {
220 	async_handshake(glob_state, "NORMAL:-VERS-ALL:+VERS-TLS1.2", 1);
221 }
222 
tls13_async_handshake(void ** glob_state)223 static void tls13_async_handshake(void **glob_state)
224 {
225 	async_handshake(glob_state, "NORMAL:-VERS-ALL:+VERS-TLS1.3", 0);
226 }
227 
main(void)228 int main(void)
229 {
230 	const struct CMUnitTest tests[] = {
231 		cmocka_unit_test(tls12_async_handshake),
232 		cmocka_unit_test(tls13_async_handshake),
233 	};
234 	return cmocka_run_group_tests(tests, NULL, NULL);
235 }
236