1*a1157835SDaniel Fojt /*
2*a1157835SDaniel Fojt  * SSL/TLS interface functions for wolfSSL TLS case
3*a1157835SDaniel Fojt  * Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi>
4*a1157835SDaniel Fojt  *
5*a1157835SDaniel Fojt  * This software may be distributed under the terms of the BSD license.
6*a1157835SDaniel Fojt  * See README for more details.
7*a1157835SDaniel Fojt  */
8*a1157835SDaniel Fojt 
9*a1157835SDaniel Fojt #include "includes.h"
10*a1157835SDaniel Fojt 
11*a1157835SDaniel Fojt #include "common.h"
12*a1157835SDaniel Fojt #include "crypto.h"
13*a1157835SDaniel Fojt #include "crypto/sha1.h"
14*a1157835SDaniel Fojt #include "crypto/sha256.h"
15*a1157835SDaniel Fojt #include "tls.h"
16*a1157835SDaniel Fojt 
17*a1157835SDaniel Fojt /* wolfSSL includes */
18*a1157835SDaniel Fojt #include <wolfssl/options.h>
19*a1157835SDaniel Fojt #include <wolfssl/ssl.h>
20*a1157835SDaniel Fojt #include <wolfssl/error-ssl.h>
21*a1157835SDaniel Fojt #include <wolfssl/wolfcrypt/asn.h>
22*a1157835SDaniel Fojt 
23*a1157835SDaniel Fojt #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
24*a1157835SDaniel Fojt #define HAVE_AESGCM
25*a1157835SDaniel Fojt #include <wolfssl/wolfcrypt/aes.h>
26*a1157835SDaniel Fojt #endif
27*a1157835SDaniel Fojt 
28*a1157835SDaniel Fojt #if !defined(CONFIG_FIPS) &&                             \
29*a1157835SDaniel Fojt     (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) ||   \
30*a1157835SDaniel Fojt      defined(EAP_SERVER_FAST))
31*a1157835SDaniel Fojt #define WOLFSSL_NEED_EAP_FAST_PRF
32*a1157835SDaniel Fojt #endif
33*a1157835SDaniel Fojt 
34*a1157835SDaniel Fojt #define SECRET_LEN          48
35*a1157835SDaniel Fojt #define RAN_LEN             32
36*a1157835SDaniel Fojt #define SESSION_TICKET_LEN  256
37*a1157835SDaniel Fojt 
38*a1157835SDaniel Fojt static int tls_ref_count = 0;
39*a1157835SDaniel Fojt 
40*a1157835SDaniel Fojt static int tls_ex_idx_session = 0;
41*a1157835SDaniel Fojt 
42*a1157835SDaniel Fojt 
43*a1157835SDaniel Fojt /* tls input data for wolfSSL Read Callback */
44*a1157835SDaniel Fojt struct tls_in_data {
45*a1157835SDaniel Fojt 	const struct wpabuf *in_data;
46*a1157835SDaniel Fojt 	size_t consumed; /* how many bytes have we used already */
47*a1157835SDaniel Fojt };
48*a1157835SDaniel Fojt 
49*a1157835SDaniel Fojt /* tls output data for wolfSSL Write Callback */
50*a1157835SDaniel Fojt struct tls_out_data {
51*a1157835SDaniel Fojt 	struct wpabuf *out_data;
52*a1157835SDaniel Fojt };
53*a1157835SDaniel Fojt 
54*a1157835SDaniel Fojt struct tls_context {
55*a1157835SDaniel Fojt 	void (*event_cb)(void *ctx, enum tls_event ev,
56*a1157835SDaniel Fojt 			 union tls_event_data *data);
57*a1157835SDaniel Fojt 	void *cb_ctx;
58*a1157835SDaniel Fojt 	int cert_in_cb;
59*a1157835SDaniel Fojt 	char *ocsp_stapling_response;
60*a1157835SDaniel Fojt };
61*a1157835SDaniel Fojt 
62*a1157835SDaniel Fojt static struct tls_context *tls_global = NULL;
63*a1157835SDaniel Fojt 
64*a1157835SDaniel Fojt /* wolfssl tls_connection */
65*a1157835SDaniel Fojt struct tls_connection {
66*a1157835SDaniel Fojt 	struct tls_context *context;
67*a1157835SDaniel Fojt 	WOLFSSL *ssl;
68*a1157835SDaniel Fojt 	int read_alerts;
69*a1157835SDaniel Fojt 	int write_alerts;
70*a1157835SDaniel Fojt 	int failed;
71*a1157835SDaniel Fojt 	struct tls_in_data input;
72*a1157835SDaniel Fojt 	struct tls_out_data output;
73*a1157835SDaniel Fojt 	char *subject_match;
74*a1157835SDaniel Fojt 	char *alt_subject_match;
75*a1157835SDaniel Fojt 	char *suffix_match;
76*a1157835SDaniel Fojt 	char *domain_match;
77*a1157835SDaniel Fojt 
78*a1157835SDaniel Fojt 	u8 srv_cert_hash[32];
79*a1157835SDaniel Fojt 
80*a1157835SDaniel Fojt 	unsigned char client_random[RAN_LEN];
81*a1157835SDaniel Fojt 	unsigned char server_random[RAN_LEN];
82*a1157835SDaniel Fojt 	unsigned int flags;
83*a1157835SDaniel Fojt #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
84*a1157835SDaniel Fojt 	tls_session_ticket_cb session_ticket_cb;
85*a1157835SDaniel Fojt 	void *session_ticket_cb_ctx;
86*a1157835SDaniel Fojt 	byte session_ticket[SESSION_TICKET_LEN];
87*a1157835SDaniel Fojt #endif
88*a1157835SDaniel Fojt 	unsigned int ca_cert_verify:1;
89*a1157835SDaniel Fojt 	unsigned int cert_probe:1;
90*a1157835SDaniel Fojt 	unsigned int server_cert_only:1;
91*a1157835SDaniel Fojt 	unsigned int success_data:1;
92*a1157835SDaniel Fojt 
93*a1157835SDaniel Fojt 	WOLFSSL_X509 *peer_cert;
94*a1157835SDaniel Fojt 	WOLFSSL_X509 *peer_issuer;
95*a1157835SDaniel Fojt 	WOLFSSL_X509 *peer_issuer_issuer;
96*a1157835SDaniel Fojt };
97*a1157835SDaniel Fojt 
98*a1157835SDaniel Fojt 
tls_context_new(const struct tls_config * conf)99*a1157835SDaniel Fojt static struct tls_context * tls_context_new(const struct tls_config *conf)
100*a1157835SDaniel Fojt {
101*a1157835SDaniel Fojt 	struct tls_context *context = os_zalloc(sizeof(*context));
102*a1157835SDaniel Fojt 
103*a1157835SDaniel Fojt 	if (!context)
104*a1157835SDaniel Fojt 		return NULL;
105*a1157835SDaniel Fojt 
106*a1157835SDaniel Fojt 	if (conf) {
107*a1157835SDaniel Fojt 		context->event_cb = conf->event_cb;
108*a1157835SDaniel Fojt 		context->cb_ctx = conf->cb_ctx;
109*a1157835SDaniel Fojt 		context->cert_in_cb = conf->cert_in_cb;
110*a1157835SDaniel Fojt 	}
111*a1157835SDaniel Fojt 
112*a1157835SDaniel Fojt 	return context;
113*a1157835SDaniel Fojt }
114*a1157835SDaniel Fojt 
115*a1157835SDaniel Fojt 
wolfssl_reset_in_data(struct tls_in_data * in,const struct wpabuf * buf)116*a1157835SDaniel Fojt static void wolfssl_reset_in_data(struct tls_in_data *in,
117*a1157835SDaniel Fojt 				  const struct wpabuf *buf)
118*a1157835SDaniel Fojt {
119*a1157835SDaniel Fojt 	/* old one not owned by us so don't free */
120*a1157835SDaniel Fojt 	in->in_data = buf;
121*a1157835SDaniel Fojt 	in->consumed = 0;
122*a1157835SDaniel Fojt }
123*a1157835SDaniel Fojt 
124*a1157835SDaniel Fojt 
wolfssl_reset_out_data(struct tls_out_data * out)125*a1157835SDaniel Fojt static void wolfssl_reset_out_data(struct tls_out_data *out)
126*a1157835SDaniel Fojt {
127*a1157835SDaniel Fojt 	/* old one not owned by us so don't free */
128*a1157835SDaniel Fojt 	out->out_data = wpabuf_alloc_copy("", 0);
129*a1157835SDaniel Fojt }
130*a1157835SDaniel Fojt 
131*a1157835SDaniel Fojt 
132*a1157835SDaniel Fojt /* wolfSSL I/O Receive CallBack */
wolfssl_receive_cb(WOLFSSL * ssl,char * buf,int sz,void * ctx)133*a1157835SDaniel Fojt static int wolfssl_receive_cb(WOLFSSL *ssl, char *buf, int sz, void *ctx)
134*a1157835SDaniel Fojt {
135*a1157835SDaniel Fojt 	size_t get = sz;
136*a1157835SDaniel Fojt 	struct tls_in_data *data = ctx;
137*a1157835SDaniel Fojt 
138*a1157835SDaniel Fojt 	if (!data)
139*a1157835SDaniel Fojt 		return -1;
140*a1157835SDaniel Fojt 
141*a1157835SDaniel Fojt 	if (get > (wpabuf_len(data->in_data) - data->consumed))
142*a1157835SDaniel Fojt 		get = wpabuf_len(data->in_data) - data->consumed;
143*a1157835SDaniel Fojt 
144*a1157835SDaniel Fojt 	os_memcpy(buf, wpabuf_head_u8(data->in_data) + data->consumed, get);
145*a1157835SDaniel Fojt 	data->consumed += get;
146*a1157835SDaniel Fojt 
147*a1157835SDaniel Fojt 	if (get == 0)
148*a1157835SDaniel Fojt 		return -2; /* WANT_READ */
149*a1157835SDaniel Fojt 
150*a1157835SDaniel Fojt 	return (int) get;
151*a1157835SDaniel Fojt }
152*a1157835SDaniel Fojt 
153*a1157835SDaniel Fojt 
154*a1157835SDaniel Fojt /* wolfSSL I/O Send CallBack */
wolfssl_send_cb(WOLFSSL * ssl,char * buf,int sz,void * ctx)155*a1157835SDaniel Fojt static int wolfssl_send_cb(WOLFSSL *ssl, char *buf, int sz, void *ctx)
156*a1157835SDaniel Fojt {
157*a1157835SDaniel Fojt 	struct wpabuf *tmp;
158*a1157835SDaniel Fojt 	struct tls_out_data *data = ctx;
159*a1157835SDaniel Fojt 
160*a1157835SDaniel Fojt 	if (!data)
161*a1157835SDaniel Fojt 		return -1;
162*a1157835SDaniel Fojt 
163*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: adding %d bytes", sz);
164*a1157835SDaniel Fojt 
165*a1157835SDaniel Fojt 	tmp = wpabuf_alloc_copy(buf, sz);
166*a1157835SDaniel Fojt 	if (!tmp)
167*a1157835SDaniel Fojt 		return -1;
168*a1157835SDaniel Fojt 	data->out_data = wpabuf_concat(data->out_data, tmp);
169*a1157835SDaniel Fojt 	if (!data->out_data)
170*a1157835SDaniel Fojt 		return -1;
171*a1157835SDaniel Fojt 
172*a1157835SDaniel Fojt 	return sz;
173*a1157835SDaniel Fojt }
174*a1157835SDaniel Fojt 
175*a1157835SDaniel Fojt 
remove_session_cb(WOLFSSL_CTX * ctx,WOLFSSL_SESSION * sess)176*a1157835SDaniel Fojt static void remove_session_cb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
177*a1157835SDaniel Fojt {
178*a1157835SDaniel Fojt 	struct wpabuf *buf;
179*a1157835SDaniel Fojt 
180*a1157835SDaniel Fojt 	buf = wolfSSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
181*a1157835SDaniel Fojt 	if (!buf)
182*a1157835SDaniel Fojt 		return;
183*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
184*a1157835SDaniel Fojt 		   "wolfSSL: Free application session data %p (sess %p)",
185*a1157835SDaniel Fojt 		   buf, sess);
186*a1157835SDaniel Fojt 	wpabuf_free(buf);
187*a1157835SDaniel Fojt 
188*a1157835SDaniel Fojt 	wolfSSL_SESSION_set_ex_data(sess, tls_ex_idx_session, NULL);
189*a1157835SDaniel Fojt }
190*a1157835SDaniel Fojt 
191*a1157835SDaniel Fojt 
tls_init(const struct tls_config * conf)192*a1157835SDaniel Fojt void * tls_init(const struct tls_config *conf)
193*a1157835SDaniel Fojt {
194*a1157835SDaniel Fojt 	WOLFSSL_CTX *ssl_ctx;
195*a1157835SDaniel Fojt 	struct tls_context *context;
196*a1157835SDaniel Fojt 	const char *ciphers;
197*a1157835SDaniel Fojt 
198*a1157835SDaniel Fojt #ifdef DEBUG_WOLFSSL
199*a1157835SDaniel Fojt 	wolfSSL_Debugging_ON();
200*a1157835SDaniel Fojt #endif /* DEBUG_WOLFSSL */
201*a1157835SDaniel Fojt 
202*a1157835SDaniel Fojt 	context = tls_context_new(conf);
203*a1157835SDaniel Fojt 	if (!context)
204*a1157835SDaniel Fojt 		return NULL;
205*a1157835SDaniel Fojt 
206*a1157835SDaniel Fojt 	if (tls_ref_count == 0) {
207*a1157835SDaniel Fojt 		tls_global = context;
208*a1157835SDaniel Fojt 
209*a1157835SDaniel Fojt 		if (wolfSSL_Init() < 0)
210*a1157835SDaniel Fojt 			return NULL;
211*a1157835SDaniel Fojt 		/* wolfSSL_Debugging_ON(); */
212*a1157835SDaniel Fojt 	}
213*a1157835SDaniel Fojt 
214*a1157835SDaniel Fojt 	tls_ref_count++;
215*a1157835SDaniel Fojt 
216*a1157835SDaniel Fojt 	/* start as client */
217*a1157835SDaniel Fojt 	ssl_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
218*a1157835SDaniel Fojt 	if (!ssl_ctx) {
219*a1157835SDaniel Fojt 		tls_ref_count--;
220*a1157835SDaniel Fojt 		if (context != tls_global)
221*a1157835SDaniel Fojt 			os_free(context);
222*a1157835SDaniel Fojt 		if (tls_ref_count == 0) {
223*a1157835SDaniel Fojt 			os_free(tls_global);
224*a1157835SDaniel Fojt 			tls_global = NULL;
225*a1157835SDaniel Fojt 		}
226*a1157835SDaniel Fojt 	}
227*a1157835SDaniel Fojt 	wolfSSL_SetIORecv(ssl_ctx, wolfssl_receive_cb);
228*a1157835SDaniel Fojt 	wolfSSL_SetIOSend(ssl_ctx, wolfssl_send_cb);
229*a1157835SDaniel Fojt 	wolfSSL_CTX_set_ex_data(ssl_ctx, 0, context);
230*a1157835SDaniel Fojt 
231*a1157835SDaniel Fojt 	if (conf->tls_session_lifetime > 0) {
232*a1157835SDaniel Fojt 		wolfSSL_CTX_set_quiet_shutdown(ssl_ctx, 1);
233*a1157835SDaniel Fojt 		wolfSSL_CTX_set_session_cache_mode(ssl_ctx,
234*a1157835SDaniel Fojt 						   SSL_SESS_CACHE_SERVER);
235*a1157835SDaniel Fojt 		wolfSSL_CTX_set_timeout(ssl_ctx, conf->tls_session_lifetime);
236*a1157835SDaniel Fojt 		wolfSSL_CTX_sess_set_remove_cb(ssl_ctx, remove_session_cb);
237*a1157835SDaniel Fojt 	} else {
238*a1157835SDaniel Fojt 		wolfSSL_CTX_set_session_cache_mode(ssl_ctx,
239*a1157835SDaniel Fojt 						   SSL_SESS_CACHE_CLIENT);
240*a1157835SDaniel Fojt 	}
241*a1157835SDaniel Fojt 
242*a1157835SDaniel Fojt 	if (conf && conf->openssl_ciphers)
243*a1157835SDaniel Fojt 		ciphers = conf->openssl_ciphers;
244*a1157835SDaniel Fojt 	else
245*a1157835SDaniel Fojt 		ciphers = "ALL";
246*a1157835SDaniel Fojt 	if (wolfSSL_CTX_set_cipher_list(ssl_ctx, ciphers) != 1) {
247*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR,
248*a1157835SDaniel Fojt 			   "wolfSSL: Failed to set cipher string '%s'",
249*a1157835SDaniel Fojt 			   ciphers);
250*a1157835SDaniel Fojt 		tls_deinit(ssl_ctx);
251*a1157835SDaniel Fojt 		return NULL;
252*a1157835SDaniel Fojt 	}
253*a1157835SDaniel Fojt 
254*a1157835SDaniel Fojt 	return ssl_ctx;
255*a1157835SDaniel Fojt }
256*a1157835SDaniel Fojt 
257*a1157835SDaniel Fojt 
tls_deinit(void * ssl_ctx)258*a1157835SDaniel Fojt void tls_deinit(void *ssl_ctx)
259*a1157835SDaniel Fojt {
260*a1157835SDaniel Fojt 	struct tls_context *context = wolfSSL_CTX_get_ex_data(ssl_ctx, 0);
261*a1157835SDaniel Fojt 
262*a1157835SDaniel Fojt 	if (context != tls_global)
263*a1157835SDaniel Fojt 		os_free(context);
264*a1157835SDaniel Fojt 
265*a1157835SDaniel Fojt 	wolfSSL_CTX_free((WOLFSSL_CTX *) ssl_ctx);
266*a1157835SDaniel Fojt 
267*a1157835SDaniel Fojt 	tls_ref_count--;
268*a1157835SDaniel Fojt 	if (tls_ref_count == 0) {
269*a1157835SDaniel Fojt 		wolfSSL_Cleanup();
270*a1157835SDaniel Fojt 		os_free(tls_global);
271*a1157835SDaniel Fojt 		tls_global = NULL;
272*a1157835SDaniel Fojt 	}
273*a1157835SDaniel Fojt }
274*a1157835SDaniel Fojt 
275*a1157835SDaniel Fojt 
tls_get_errors(void * tls_ctx)276*a1157835SDaniel Fojt int tls_get_errors(void *tls_ctx)
277*a1157835SDaniel Fojt {
278*a1157835SDaniel Fojt #ifdef DEBUG_WOLFSSL
279*a1157835SDaniel Fojt #if 0
280*a1157835SDaniel Fojt 	unsigned long err;
281*a1157835SDaniel Fojt 
282*a1157835SDaniel Fojt 	err = wolfSSL_ERR_peek_last_error_line(NULL, NULL);
283*a1157835SDaniel Fojt 	if (err != 0) {
284*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "TLS - SSL error: %s",
285*a1157835SDaniel Fojt 			   wolfSSL_ERR_error_string(err, NULL));
286*a1157835SDaniel Fojt 		return 1;
287*a1157835SDaniel Fojt 	}
288*a1157835SDaniel Fojt #endif
289*a1157835SDaniel Fojt #endif /* DEBUG_WOLFSSL */
290*a1157835SDaniel Fojt 	return 0;
291*a1157835SDaniel Fojt }
292*a1157835SDaniel Fojt 
293*a1157835SDaniel Fojt 
tls_connection_init(void * tls_ctx)294*a1157835SDaniel Fojt struct tls_connection * tls_connection_init(void *tls_ctx)
295*a1157835SDaniel Fojt {
296*a1157835SDaniel Fojt 	WOLFSSL_CTX *ssl_ctx = tls_ctx;
297*a1157835SDaniel Fojt 	struct tls_connection *conn;
298*a1157835SDaniel Fojt 
299*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: connection init");
300*a1157835SDaniel Fojt 
301*a1157835SDaniel Fojt 	conn = os_zalloc(sizeof(*conn));
302*a1157835SDaniel Fojt 	if (!conn)
303*a1157835SDaniel Fojt 		return NULL;
304*a1157835SDaniel Fojt 	conn->ssl = wolfSSL_new(ssl_ctx);
305*a1157835SDaniel Fojt 	if (!conn->ssl) {
306*a1157835SDaniel Fojt 		os_free(conn);
307*a1157835SDaniel Fojt 		return NULL;
308*a1157835SDaniel Fojt 	}
309*a1157835SDaniel Fojt 
310*a1157835SDaniel Fojt 	wolfSSL_SetIOReadCtx(conn->ssl,  &conn->input);
311*a1157835SDaniel Fojt 	wolfSSL_SetIOWriteCtx(conn->ssl, &conn->output);
312*a1157835SDaniel Fojt 	wolfSSL_set_ex_data(conn->ssl, 0, conn);
313*a1157835SDaniel Fojt 	conn->context = wolfSSL_CTX_get_ex_data(ssl_ctx, 0);
314*a1157835SDaniel Fojt 
315*a1157835SDaniel Fojt 	/* Need randoms post-hanshake for EAP-FAST, export key and deriving
316*a1157835SDaniel Fojt 	 * session ID in EAP methods. */
317*a1157835SDaniel Fojt 	wolfSSL_KeepArrays(conn->ssl);
318*a1157835SDaniel Fojt 	wolfSSL_KeepHandshakeResources(conn->ssl);
319*a1157835SDaniel Fojt 	wolfSSL_UseClientSuites(conn->ssl);
320*a1157835SDaniel Fojt 
321*a1157835SDaniel Fojt 	return conn;
322*a1157835SDaniel Fojt }
323*a1157835SDaniel Fojt 
324*a1157835SDaniel Fojt 
tls_connection_deinit(void * tls_ctx,struct tls_connection * conn)325*a1157835SDaniel Fojt void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn)
326*a1157835SDaniel Fojt {
327*a1157835SDaniel Fojt 	if (!conn)
328*a1157835SDaniel Fojt 		return;
329*a1157835SDaniel Fojt 
330*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: connection deinit");
331*a1157835SDaniel Fojt 
332*a1157835SDaniel Fojt 	/* parts */
333*a1157835SDaniel Fojt 	wolfSSL_free(conn->ssl);
334*a1157835SDaniel Fojt 	os_free(conn->subject_match);
335*a1157835SDaniel Fojt 	os_free(conn->alt_subject_match);
336*a1157835SDaniel Fojt 	os_free(conn->suffix_match);
337*a1157835SDaniel Fojt 	os_free(conn->domain_match);
338*a1157835SDaniel Fojt 
339*a1157835SDaniel Fojt 	/* self */
340*a1157835SDaniel Fojt 	os_free(conn);
341*a1157835SDaniel Fojt }
342*a1157835SDaniel Fojt 
343*a1157835SDaniel Fojt 
tls_connection_established(void * tls_ctx,struct tls_connection * conn)344*a1157835SDaniel Fojt int tls_connection_established(void *tls_ctx, struct tls_connection *conn)
345*a1157835SDaniel Fojt {
346*a1157835SDaniel Fojt 	return conn ? wolfSSL_is_init_finished(conn->ssl) : 0;
347*a1157835SDaniel Fojt }
348*a1157835SDaniel Fojt 
349*a1157835SDaniel Fojt 
tls_connection_peer_serial_num(void * tls_ctx,struct tls_connection * conn)350*a1157835SDaniel Fojt char * tls_connection_peer_serial_num(void *tls_ctx,
351*a1157835SDaniel Fojt 				      struct tls_connection *conn)
352*a1157835SDaniel Fojt {
353*a1157835SDaniel Fojt 	/* TODO */
354*a1157835SDaniel Fojt 	return NULL;
355*a1157835SDaniel Fojt }
356*a1157835SDaniel Fojt 
357*a1157835SDaniel Fojt 
tls_connection_shutdown(void * tls_ctx,struct tls_connection * conn)358*a1157835SDaniel Fojt int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn)
359*a1157835SDaniel Fojt {
360*a1157835SDaniel Fojt 	WOLFSSL_SESSION *session;
361*a1157835SDaniel Fojt 
362*a1157835SDaniel Fojt 	if (!conn)
363*a1157835SDaniel Fojt 		return -1;
364*a1157835SDaniel Fojt 
365*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: connection shutdown");
366*a1157835SDaniel Fojt 
367*a1157835SDaniel Fojt 	/* Set quiet as OpenSSL does */
368*a1157835SDaniel Fojt 	wolfSSL_set_quiet_shutdown(conn->ssl, 1);
369*a1157835SDaniel Fojt 	wolfSSL_shutdown(conn->ssl);
370*a1157835SDaniel Fojt 
371*a1157835SDaniel Fojt 	session = wolfSSL_get_session(conn->ssl);
372*a1157835SDaniel Fojt 	if (wolfSSL_clear(conn->ssl) != 1)
373*a1157835SDaniel Fojt 		return -1;
374*a1157835SDaniel Fojt 	wolfSSL_set_session(conn->ssl, session);
375*a1157835SDaniel Fojt 
376*a1157835SDaniel Fojt 	return 0;
377*a1157835SDaniel Fojt }
378*a1157835SDaniel Fojt 
379*a1157835SDaniel Fojt 
tls_connection_set_subject_match(struct tls_connection * conn,const char * subject_match,const char * alt_subject_match,const char * suffix_match,const char * domain_match)380*a1157835SDaniel Fojt static int tls_connection_set_subject_match(struct tls_connection *conn,
381*a1157835SDaniel Fojt 					    const char *subject_match,
382*a1157835SDaniel Fojt 					    const char *alt_subject_match,
383*a1157835SDaniel Fojt 					    const char *suffix_match,
384*a1157835SDaniel Fojt 					    const char *domain_match)
385*a1157835SDaniel Fojt {
386*a1157835SDaniel Fojt 	os_free(conn->subject_match);
387*a1157835SDaniel Fojt 	conn->subject_match = NULL;
388*a1157835SDaniel Fojt 	if (subject_match) {
389*a1157835SDaniel Fojt 		conn->subject_match = os_strdup(subject_match);
390*a1157835SDaniel Fojt 		if (!conn->subject_match)
391*a1157835SDaniel Fojt 			return -1;
392*a1157835SDaniel Fojt 	}
393*a1157835SDaniel Fojt 
394*a1157835SDaniel Fojt 	os_free(conn->alt_subject_match);
395*a1157835SDaniel Fojt 	conn->alt_subject_match = NULL;
396*a1157835SDaniel Fojt 	if (alt_subject_match) {
397*a1157835SDaniel Fojt 		conn->alt_subject_match = os_strdup(alt_subject_match);
398*a1157835SDaniel Fojt 		if (!conn->alt_subject_match)
399*a1157835SDaniel Fojt 			return -1;
400*a1157835SDaniel Fojt 	}
401*a1157835SDaniel Fojt 
402*a1157835SDaniel Fojt 	os_free(conn->suffix_match);
403*a1157835SDaniel Fojt 	conn->suffix_match = NULL;
404*a1157835SDaniel Fojt 	if (suffix_match) {
405*a1157835SDaniel Fojt 		conn->suffix_match = os_strdup(suffix_match);
406*a1157835SDaniel Fojt 		if (!conn->suffix_match)
407*a1157835SDaniel Fojt 			return -1;
408*a1157835SDaniel Fojt 	}
409*a1157835SDaniel Fojt 
410*a1157835SDaniel Fojt 	os_free(conn->domain_match);
411*a1157835SDaniel Fojt 	conn->domain_match = NULL;
412*a1157835SDaniel Fojt 	if (domain_match) {
413*a1157835SDaniel Fojt 		conn->domain_match = os_strdup(domain_match);
414*a1157835SDaniel Fojt 		if (!conn->domain_match)
415*a1157835SDaniel Fojt 			return -1;
416*a1157835SDaniel Fojt 	}
417*a1157835SDaniel Fojt 
418*a1157835SDaniel Fojt 	return 0;
419*a1157835SDaniel Fojt }
420*a1157835SDaniel Fojt 
421*a1157835SDaniel Fojt 
tls_connection_dh(struct tls_connection * conn,const char * dh_file,const u8 * dh_blob,size_t blob_len)422*a1157835SDaniel Fojt static int tls_connection_dh(struct tls_connection *conn, const char *dh_file,
423*a1157835SDaniel Fojt 			     const u8 *dh_blob, size_t blob_len)
424*a1157835SDaniel Fojt {
425*a1157835SDaniel Fojt 	if (!dh_file && !dh_blob)
426*a1157835SDaniel Fojt 		return 0;
427*a1157835SDaniel Fojt 
428*a1157835SDaniel Fojt 	wolfSSL_set_accept_state(conn->ssl);
429*a1157835SDaniel Fojt 
430*a1157835SDaniel Fojt 	if (dh_blob) {
431*a1157835SDaniel Fojt 		if (wolfSSL_SetTmpDH_buffer(conn->ssl, dh_blob, blob_len,
432*a1157835SDaniel Fojt 					    SSL_FILETYPE_ASN1) < 0) {
433*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO, "SSL: use DH DER blob failed");
434*a1157835SDaniel Fojt 			return -1;
435*a1157835SDaniel Fojt 		}
436*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: use DH blob OK");
437*a1157835SDaniel Fojt 		return 0;
438*a1157835SDaniel Fojt 	}
439*a1157835SDaniel Fojt 
440*a1157835SDaniel Fojt 	if (dh_file) {
441*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "SSL: use DH PEM file: %s", dh_file);
442*a1157835SDaniel Fojt 		if (wolfSSL_SetTmpDH_file(conn->ssl, dh_file,
443*a1157835SDaniel Fojt 					  SSL_FILETYPE_PEM) < 0) {
444*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO, "SSL: use DH PEM file failed");
445*a1157835SDaniel Fojt 			if (wolfSSL_SetTmpDH_file(conn->ssl, dh_file,
446*a1157835SDaniel Fojt 						  SSL_FILETYPE_ASN1) < 0) {
447*a1157835SDaniel Fojt 				wpa_printf(MSG_INFO,
448*a1157835SDaniel Fojt 					   "SSL: use DH DER file failed");
449*a1157835SDaniel Fojt 				return -1;
450*a1157835SDaniel Fojt 			}
451*a1157835SDaniel Fojt 		}
452*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: use DH file OK");
453*a1157835SDaniel Fojt 		return 0;
454*a1157835SDaniel Fojt 	}
455*a1157835SDaniel Fojt 
456*a1157835SDaniel Fojt 	return 0;
457*a1157835SDaniel Fojt }
458*a1157835SDaniel Fojt 
459*a1157835SDaniel Fojt 
tls_connection_client_cert(struct tls_connection * conn,const char * client_cert,const u8 * client_cert_blob,size_t blob_len)460*a1157835SDaniel Fojt static int tls_connection_client_cert(struct tls_connection *conn,
461*a1157835SDaniel Fojt 				      const char *client_cert,
462*a1157835SDaniel Fojt 				      const u8 *client_cert_blob,
463*a1157835SDaniel Fojt 				      size_t blob_len)
464*a1157835SDaniel Fojt {
465*a1157835SDaniel Fojt 	if (!client_cert && !client_cert_blob)
466*a1157835SDaniel Fojt 		return 0;
467*a1157835SDaniel Fojt 
468*a1157835SDaniel Fojt 	if (client_cert_blob) {
469*a1157835SDaniel Fojt 		if (wolfSSL_use_certificate_chain_buffer_format(
470*a1157835SDaniel Fojt 			    conn->ssl, client_cert_blob, blob_len,
471*a1157835SDaniel Fojt 			    SSL_FILETYPE_ASN1) < 0) {
472*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
473*a1157835SDaniel Fojt 				   "SSL: use client cert DER blob failed");
474*a1157835SDaniel Fojt 			return -1;
475*a1157835SDaniel Fojt 		}
476*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: use client cert blob OK");
477*a1157835SDaniel Fojt 		return 0;
478*a1157835SDaniel Fojt 	}
479*a1157835SDaniel Fojt 
480*a1157835SDaniel Fojt 	if (client_cert) {
481*a1157835SDaniel Fojt 		if (wolfSSL_use_certificate_chain_file(conn->ssl,
482*a1157835SDaniel Fojt 						       client_cert) < 0) {
483*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
484*a1157835SDaniel Fojt 				   "SSL: use client cert PEM file failed");
485*a1157835SDaniel Fojt 			if (wolfSSL_use_certificate_chain_file_format(
486*a1157835SDaniel Fojt 				    conn->ssl, client_cert,
487*a1157835SDaniel Fojt 				    SSL_FILETYPE_ASN1) < 0) {
488*a1157835SDaniel Fojt 				wpa_printf(MSG_INFO,
489*a1157835SDaniel Fojt 					   "SSL: use client cert DER file failed");
490*a1157835SDaniel Fojt 				return -1;
491*a1157835SDaniel Fojt 			}
492*a1157835SDaniel Fojt 		}
493*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: use client cert file OK");
494*a1157835SDaniel Fojt 		return 0;
495*a1157835SDaniel Fojt 	}
496*a1157835SDaniel Fojt 
497*a1157835SDaniel Fojt 	return 0;
498*a1157835SDaniel Fojt }
499*a1157835SDaniel Fojt 
500*a1157835SDaniel Fojt 
tls_passwd_cb(char * buf,int size,int rwflag,void * password)501*a1157835SDaniel Fojt static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
502*a1157835SDaniel Fojt {
503*a1157835SDaniel Fojt 	if (!password)
504*a1157835SDaniel Fojt 		return 0;
505*a1157835SDaniel Fojt 	os_strlcpy(buf, (char *) password, size);
506*a1157835SDaniel Fojt 	return os_strlen(buf);
507*a1157835SDaniel Fojt }
508*a1157835SDaniel Fojt 
509*a1157835SDaniel Fojt 
tls_connection_private_key(void * tls_ctx,struct tls_connection * conn,const char * private_key,const char * private_key_passwd,const u8 * private_key_blob,size_t blob_len)510*a1157835SDaniel Fojt static int tls_connection_private_key(void *tls_ctx,
511*a1157835SDaniel Fojt 				      struct tls_connection *conn,
512*a1157835SDaniel Fojt 				      const char *private_key,
513*a1157835SDaniel Fojt 				      const char *private_key_passwd,
514*a1157835SDaniel Fojt 				      const u8 *private_key_blob,
515*a1157835SDaniel Fojt 				      size_t blob_len)
516*a1157835SDaniel Fojt {
517*a1157835SDaniel Fojt 	WOLFSSL_CTX *ctx = tls_ctx;
518*a1157835SDaniel Fojt 	char *passwd = NULL;
519*a1157835SDaniel Fojt 	int ok = 0;
520*a1157835SDaniel Fojt 
521*a1157835SDaniel Fojt 	if (!private_key && !private_key_blob)
522*a1157835SDaniel Fojt 		return 0;
523*a1157835SDaniel Fojt 
524*a1157835SDaniel Fojt 	if (private_key_passwd) {
525*a1157835SDaniel Fojt 		passwd = os_strdup(private_key_passwd);
526*a1157835SDaniel Fojt 		if (!passwd)
527*a1157835SDaniel Fojt 			return -1;
528*a1157835SDaniel Fojt 	}
529*a1157835SDaniel Fojt 
530*a1157835SDaniel Fojt 	wolfSSL_CTX_set_default_passwd_cb(ctx, tls_passwd_cb);
531*a1157835SDaniel Fojt 	wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, passwd);
532*a1157835SDaniel Fojt 
533*a1157835SDaniel Fojt 	if (private_key_blob) {
534*a1157835SDaniel Fojt 		if (wolfSSL_use_PrivateKey_buffer(conn->ssl,
535*a1157835SDaniel Fojt 						  private_key_blob, blob_len,
536*a1157835SDaniel Fojt 						  SSL_FILETYPE_ASN1) < 0) {
537*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
538*a1157835SDaniel Fojt 				   "SSL: use private DER blob failed");
539*a1157835SDaniel Fojt 		} else {
540*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "SSL: use private key blob OK");
541*a1157835SDaniel Fojt 			ok = 1;
542*a1157835SDaniel Fojt 		}
543*a1157835SDaniel Fojt 	}
544*a1157835SDaniel Fojt 
545*a1157835SDaniel Fojt 	if (!ok && private_key) {
546*a1157835SDaniel Fojt 		if (wolfSSL_use_PrivateKey_file(conn->ssl, private_key,
547*a1157835SDaniel Fojt 						SSL_FILETYPE_PEM) < 0) {
548*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
549*a1157835SDaniel Fojt 				   "SSL: use private key PEM file failed");
550*a1157835SDaniel Fojt 			if (wolfSSL_use_PrivateKey_file(conn->ssl, private_key,
551*a1157835SDaniel Fojt 							SSL_FILETYPE_ASN1) < 0)
552*a1157835SDaniel Fojt 			{
553*a1157835SDaniel Fojt 				wpa_printf(MSG_INFO,
554*a1157835SDaniel Fojt 					   "SSL: use private key DER file failed");
555*a1157835SDaniel Fojt 			} else {
556*a1157835SDaniel Fojt 				ok = 1;
557*a1157835SDaniel Fojt 			}
558*a1157835SDaniel Fojt 		} else {
559*a1157835SDaniel Fojt 			ok = 1;
560*a1157835SDaniel Fojt 		}
561*a1157835SDaniel Fojt 
562*a1157835SDaniel Fojt 		if (ok)
563*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "SSL: use private key file OK");
564*a1157835SDaniel Fojt 	}
565*a1157835SDaniel Fojt 
566*a1157835SDaniel Fojt 	wolfSSL_CTX_set_default_passwd_cb(ctx, NULL);
567*a1157835SDaniel Fojt 	os_free(passwd);
568*a1157835SDaniel Fojt 
569*a1157835SDaniel Fojt 	if (!ok)
570*a1157835SDaniel Fojt 		return -1;
571*a1157835SDaniel Fojt 
572*a1157835SDaniel Fojt 	return 0;
573*a1157835SDaniel Fojt }
574*a1157835SDaniel Fojt 
575*a1157835SDaniel Fojt 
tls_match_alt_subject_component(WOLFSSL_X509 * cert,int type,const char * value,size_t len)576*a1157835SDaniel Fojt static int tls_match_alt_subject_component(WOLFSSL_X509 *cert, int type,
577*a1157835SDaniel Fojt 					   const char *value, size_t len)
578*a1157835SDaniel Fojt {
579*a1157835SDaniel Fojt 	WOLFSSL_ASN1_OBJECT *gen;
580*a1157835SDaniel Fojt 	void *ext;
581*a1157835SDaniel Fojt 	int found = 0;
582*a1157835SDaniel Fojt 	int i;
583*a1157835SDaniel Fojt 
584*a1157835SDaniel Fojt 	ext = wolfSSL_X509_get_ext_d2i(cert, ALT_NAMES_OID, NULL, NULL);
585*a1157835SDaniel Fojt 
586*a1157835SDaniel Fojt 	for (i = 0; ext && i < wolfSSL_sk_num(ext); i++) {
587*a1157835SDaniel Fojt 		gen = wolfSSL_sk_value(ext, i);
588*a1157835SDaniel Fojt 		if (gen->type != type)
589*a1157835SDaniel Fojt 			continue;
590*a1157835SDaniel Fojt 		if (os_strlen((char *) gen->obj) == len &&
591*a1157835SDaniel Fojt 		    os_memcmp(value, gen->obj, len) == 0)
592*a1157835SDaniel Fojt 			found++;
593*a1157835SDaniel Fojt 	}
594*a1157835SDaniel Fojt 
595*a1157835SDaniel Fojt 	wolfSSL_sk_ASN1_OBJECT_free(ext);
596*a1157835SDaniel Fojt 
597*a1157835SDaniel Fojt 	return found;
598*a1157835SDaniel Fojt }
599*a1157835SDaniel Fojt 
600*a1157835SDaniel Fojt 
tls_match_alt_subject(WOLFSSL_X509 * cert,const char * match)601*a1157835SDaniel Fojt static int tls_match_alt_subject(WOLFSSL_X509 *cert, const char *match)
602*a1157835SDaniel Fojt {
603*a1157835SDaniel Fojt 	int type;
604*a1157835SDaniel Fojt 	const char *pos, *end;
605*a1157835SDaniel Fojt 	size_t len;
606*a1157835SDaniel Fojt 
607*a1157835SDaniel Fojt 	pos = match;
608*a1157835SDaniel Fojt 	do {
609*a1157835SDaniel Fojt 		if (os_strncmp(pos, "EMAIL:", 6) == 0) {
610*a1157835SDaniel Fojt 			type = GEN_EMAIL;
611*a1157835SDaniel Fojt 			pos += 6;
612*a1157835SDaniel Fojt 		} else if (os_strncmp(pos, "DNS:", 4) == 0) {
613*a1157835SDaniel Fojt 			type = GEN_DNS;
614*a1157835SDaniel Fojt 			pos += 4;
615*a1157835SDaniel Fojt 		} else if (os_strncmp(pos, "URI:", 4) == 0) {
616*a1157835SDaniel Fojt 			type = GEN_URI;
617*a1157835SDaniel Fojt 			pos += 4;
618*a1157835SDaniel Fojt 		} else {
619*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
620*a1157835SDaniel Fojt 				   "TLS: Invalid altSubjectName match '%s'",
621*a1157835SDaniel Fojt 				   pos);
622*a1157835SDaniel Fojt 			return 0;
623*a1157835SDaniel Fojt 		}
624*a1157835SDaniel Fojt 		end = os_strchr(pos, ';');
625*a1157835SDaniel Fojt 		while (end) {
626*a1157835SDaniel Fojt 			if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
627*a1157835SDaniel Fojt 			    os_strncmp(end + 1, "DNS:", 4) == 0 ||
628*a1157835SDaniel Fojt 			    os_strncmp(end + 1, "URI:", 4) == 0)
629*a1157835SDaniel Fojt 				break;
630*a1157835SDaniel Fojt 			end = os_strchr(end + 1, ';');
631*a1157835SDaniel Fojt 		}
632*a1157835SDaniel Fojt 		if (end)
633*a1157835SDaniel Fojt 			len = end - pos;
634*a1157835SDaniel Fojt 		else
635*a1157835SDaniel Fojt 			len = os_strlen(pos);
636*a1157835SDaniel Fojt 		if (tls_match_alt_subject_component(cert, type, pos, len) > 0)
637*a1157835SDaniel Fojt 			return 1;
638*a1157835SDaniel Fojt 		pos = end + 1;
639*a1157835SDaniel Fojt 	} while (end);
640*a1157835SDaniel Fojt 
641*a1157835SDaniel Fojt 	return 0;
642*a1157835SDaniel Fojt }
643*a1157835SDaniel Fojt 
644*a1157835SDaniel Fojt 
domain_suffix_match(const char * val,size_t len,const char * match,size_t match_len,int full)645*a1157835SDaniel Fojt static int domain_suffix_match(const char *val, size_t len, const char *match,
646*a1157835SDaniel Fojt 			       size_t match_len, int full)
647*a1157835SDaniel Fojt {
648*a1157835SDaniel Fojt 	size_t i;
649*a1157835SDaniel Fojt 
650*a1157835SDaniel Fojt 	/* Check for embedded nuls that could mess up suffix matching */
651*a1157835SDaniel Fojt 	for (i = 0; i < len; i++) {
652*a1157835SDaniel Fojt 		if (val[i] == '\0') {
653*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
654*a1157835SDaniel Fojt 				   "TLS: Embedded null in a string - reject");
655*a1157835SDaniel Fojt 			return 0;
656*a1157835SDaniel Fojt 		}
657*a1157835SDaniel Fojt 	}
658*a1157835SDaniel Fojt 
659*a1157835SDaniel Fojt 	if (match_len > len || (full && match_len != len))
660*a1157835SDaniel Fojt 		return 0;
661*a1157835SDaniel Fojt 
662*a1157835SDaniel Fojt 	if (os_strncasecmp(val + len - match_len, match, match_len) != 0)
663*a1157835SDaniel Fojt 		return 0; /* no match */
664*a1157835SDaniel Fojt 
665*a1157835SDaniel Fojt 	if (match_len == len)
666*a1157835SDaniel Fojt 		return 1; /* exact match */
667*a1157835SDaniel Fojt 
668*a1157835SDaniel Fojt 	if (val[len - match_len - 1] == '.')
669*a1157835SDaniel Fojt 		return 1; /* full label match completes suffix match */
670*a1157835SDaniel Fojt 
671*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "TLS: Reject due to incomplete label match");
672*a1157835SDaniel Fojt 	return 0;
673*a1157835SDaniel Fojt }
674*a1157835SDaniel Fojt 
675*a1157835SDaniel Fojt 
tls_match_suffix_helper(WOLFSSL_X509 * cert,const char * match,size_t match_len,int full)676*a1157835SDaniel Fojt static int tls_match_suffix_helper(WOLFSSL_X509 *cert, const char *match,
677*a1157835SDaniel Fojt 				   size_t match_len, int full)
678*a1157835SDaniel Fojt {
679*a1157835SDaniel Fojt 	WOLFSSL_ASN1_OBJECT *gen;
680*a1157835SDaniel Fojt 	void *ext;
681*a1157835SDaniel Fojt 	int i;
682*a1157835SDaniel Fojt 	int j;
683*a1157835SDaniel Fojt 	int dns_name = 0;
684*a1157835SDaniel Fojt 	WOLFSSL_X509_NAME *name;
685*a1157835SDaniel Fojt 
686*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "TLS: Match domain against %s%s",
687*a1157835SDaniel Fojt 		   full ? "" : "suffix ", match);
688*a1157835SDaniel Fojt 
689*a1157835SDaniel Fojt 	ext = wolfSSL_X509_get_ext_d2i(cert, ALT_NAMES_OID, NULL, NULL);
690*a1157835SDaniel Fojt 
691*a1157835SDaniel Fojt 	for (j = 0; ext && j < wolfSSL_sk_num(ext); j++) {
692*a1157835SDaniel Fojt 		gen = wolfSSL_sk_value(ext, j);
693*a1157835SDaniel Fojt 		if (gen->type != ASN_DNS_TYPE)
694*a1157835SDaniel Fojt 			continue;
695*a1157835SDaniel Fojt 		dns_name++;
696*a1157835SDaniel Fojt 		wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate dNSName",
697*a1157835SDaniel Fojt 				  gen->obj, os_strlen((char *)gen->obj));
698*a1157835SDaniel Fojt 		if (domain_suffix_match((const char *) gen->obj,
699*a1157835SDaniel Fojt 					os_strlen((char *) gen->obj), match,
700*a1157835SDaniel Fojt 					match_len, full) == 1) {
701*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "TLS: %s in dNSName found",
702*a1157835SDaniel Fojt 				   full ? "Match" : "Suffix match");
703*a1157835SDaniel Fojt 			wolfSSL_sk_ASN1_OBJECT_free(ext);
704*a1157835SDaniel Fojt 			return 1;
705*a1157835SDaniel Fojt 		}
706*a1157835SDaniel Fojt 	}
707*a1157835SDaniel Fojt 	wolfSSL_sk_ASN1_OBJECT_free(ext);
708*a1157835SDaniel Fojt 
709*a1157835SDaniel Fojt 	if (dns_name) {
710*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "TLS: None of the dNSName(s) matched");
711*a1157835SDaniel Fojt 		return 0;
712*a1157835SDaniel Fojt 	}
713*a1157835SDaniel Fojt 
714*a1157835SDaniel Fojt 	name = wolfSSL_X509_get_subject_name(cert);
715*a1157835SDaniel Fojt 	i = -1;
716*a1157835SDaniel Fojt 	for (;;) {
717*a1157835SDaniel Fojt 		WOLFSSL_X509_NAME_ENTRY *e;
718*a1157835SDaniel Fojt 		WOLFSSL_ASN1_STRING *cn;
719*a1157835SDaniel Fojt 
720*a1157835SDaniel Fojt 		i = wolfSSL_X509_NAME_get_index_by_NID(name, ASN_COMMON_NAME,
721*a1157835SDaniel Fojt 						       i);
722*a1157835SDaniel Fojt 		if (i == -1)
723*a1157835SDaniel Fojt 			break;
724*a1157835SDaniel Fojt 		e = wolfSSL_X509_NAME_get_entry(name, i);
725*a1157835SDaniel Fojt 		if (!e)
726*a1157835SDaniel Fojt 			continue;
727*a1157835SDaniel Fojt 		cn = wolfSSL_X509_NAME_ENTRY_get_data(e);
728*a1157835SDaniel Fojt 		if (!cn)
729*a1157835SDaniel Fojt 			continue;
730*a1157835SDaniel Fojt 		wpa_hexdump_ascii(MSG_DEBUG, "TLS: Certificate commonName",
731*a1157835SDaniel Fojt 				  cn->data, cn->length);
732*a1157835SDaniel Fojt 		if (domain_suffix_match(cn->data, cn->length,
733*a1157835SDaniel Fojt 					match, match_len, full) == 1) {
734*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "TLS: %s in commonName found",
735*a1157835SDaniel Fojt 				   full ? "Match" : "Suffix match");
736*a1157835SDaniel Fojt 			return 1;
737*a1157835SDaniel Fojt 		}
738*a1157835SDaniel Fojt 	}
739*a1157835SDaniel Fojt 
740*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "TLS: No CommonName %smatch found",
741*a1157835SDaniel Fojt 		   full ? "" : "suffix ");
742*a1157835SDaniel Fojt 	return 0;
743*a1157835SDaniel Fojt }
744*a1157835SDaniel Fojt 
745*a1157835SDaniel Fojt 
tls_match_suffix(WOLFSSL_X509 * cert,const char * match,int full)746*a1157835SDaniel Fojt static int tls_match_suffix(WOLFSSL_X509 *cert, const char *match, int full)
747*a1157835SDaniel Fojt {
748*a1157835SDaniel Fojt 	const char *token, *last = NULL;
749*a1157835SDaniel Fojt 
750*a1157835SDaniel Fojt 	/* Process each match alternative separately until a match is found */
751*a1157835SDaniel Fojt 	while ((token = cstr_token(match, ";", &last))) {
752*a1157835SDaniel Fojt 		if (tls_match_suffix_helper(cert, token, last - token, full))
753*a1157835SDaniel Fojt 			return 1;
754*a1157835SDaniel Fojt 	}
755*a1157835SDaniel Fojt 
756*a1157835SDaniel Fojt 	return 0;
757*a1157835SDaniel Fojt }
758*a1157835SDaniel Fojt 
759*a1157835SDaniel Fojt 
wolfssl_tls_fail_reason(int err)760*a1157835SDaniel Fojt static enum tls_fail_reason wolfssl_tls_fail_reason(int err)
761*a1157835SDaniel Fojt {
762*a1157835SDaniel Fojt 	switch (err) {
763*a1157835SDaniel Fojt 	case X509_V_ERR_CERT_REVOKED:
764*a1157835SDaniel Fojt 		return TLS_FAIL_REVOKED;
765*a1157835SDaniel Fojt 	case ASN_BEFORE_DATE_E:
766*a1157835SDaniel Fojt 	case X509_V_ERR_CERT_NOT_YET_VALID:
767*a1157835SDaniel Fojt 	case X509_V_ERR_CRL_NOT_YET_VALID:
768*a1157835SDaniel Fojt 		return TLS_FAIL_NOT_YET_VALID;
769*a1157835SDaniel Fojt 	case ASN_AFTER_DATE_E:
770*a1157835SDaniel Fojt 	case X509_V_ERR_CERT_HAS_EXPIRED:
771*a1157835SDaniel Fojt 	case X509_V_ERR_CRL_HAS_EXPIRED:
772*a1157835SDaniel Fojt 		return TLS_FAIL_EXPIRED;
773*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
774*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_GET_CRL:
775*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
776*a1157835SDaniel Fojt 	case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
777*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
778*a1157835SDaniel Fojt 	case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
779*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
780*a1157835SDaniel Fojt 	case X509_V_ERR_CERT_CHAIN_TOO_LONG:
781*a1157835SDaniel Fojt 	case X509_V_ERR_PATH_LENGTH_EXCEEDED:
782*a1157835SDaniel Fojt 	case X509_V_ERR_INVALID_CA:
783*a1157835SDaniel Fojt 		return TLS_FAIL_UNTRUSTED;
784*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
785*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
786*a1157835SDaniel Fojt 	case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
787*a1157835SDaniel Fojt 	case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
788*a1157835SDaniel Fojt 	case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
789*a1157835SDaniel Fojt 	case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
790*a1157835SDaniel Fojt 	case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
791*a1157835SDaniel Fojt 	case X509_V_ERR_CERT_UNTRUSTED:
792*a1157835SDaniel Fojt 	case X509_V_ERR_CERT_REJECTED:
793*a1157835SDaniel Fojt 		return TLS_FAIL_BAD_CERTIFICATE;
794*a1157835SDaniel Fojt 	default:
795*a1157835SDaniel Fojt 		return TLS_FAIL_UNSPECIFIED;
796*a1157835SDaniel Fojt 	}
797*a1157835SDaniel Fojt }
798*a1157835SDaniel Fojt 
799*a1157835SDaniel Fojt 
wolfssl_tls_err_string(int err,const char * err_str)800*a1157835SDaniel Fojt static const char * wolfssl_tls_err_string(int err, const char *err_str)
801*a1157835SDaniel Fojt {
802*a1157835SDaniel Fojt 	switch (err) {
803*a1157835SDaniel Fojt 	case ASN_BEFORE_DATE_E:
804*a1157835SDaniel Fojt 		return "certificate is not yet valid";
805*a1157835SDaniel Fojt 	case ASN_AFTER_DATE_E:
806*a1157835SDaniel Fojt 		return "certificate has expired";
807*a1157835SDaniel Fojt 	default:
808*a1157835SDaniel Fojt 		return err_str;
809*a1157835SDaniel Fojt 	}
810*a1157835SDaniel Fojt }
811*a1157835SDaniel Fojt 
812*a1157835SDaniel Fojt 
get_x509_cert(WOLFSSL_X509 * cert)813*a1157835SDaniel Fojt static struct wpabuf * get_x509_cert(WOLFSSL_X509 *cert)
814*a1157835SDaniel Fojt {
815*a1157835SDaniel Fojt 	struct wpabuf *buf = NULL;
816*a1157835SDaniel Fojt 	const u8 *data;
817*a1157835SDaniel Fojt 	int cert_len;
818*a1157835SDaniel Fojt 
819*a1157835SDaniel Fojt 	data = wolfSSL_X509_get_der(cert, &cert_len);
820*a1157835SDaniel Fojt 	if (!data)
821*a1157835SDaniel Fojt 		buf = wpabuf_alloc_copy(data, cert_len);
822*a1157835SDaniel Fojt 
823*a1157835SDaniel Fojt 	return buf;
824*a1157835SDaniel Fojt }
825*a1157835SDaniel Fojt 
826*a1157835SDaniel Fojt 
wolfssl_tls_fail_event(struct tls_connection * conn,WOLFSSL_X509 * err_cert,int err,int depth,const char * subject,const char * err_str,enum tls_fail_reason reason)827*a1157835SDaniel Fojt static void wolfssl_tls_fail_event(struct tls_connection *conn,
828*a1157835SDaniel Fojt 				   WOLFSSL_X509 *err_cert, int err, int depth,
829*a1157835SDaniel Fojt 				   const char *subject, const char *err_str,
830*a1157835SDaniel Fojt 				   enum tls_fail_reason reason)
831*a1157835SDaniel Fojt {
832*a1157835SDaniel Fojt 	union tls_event_data ev;
833*a1157835SDaniel Fojt 	struct wpabuf *cert = NULL;
834*a1157835SDaniel Fojt 	struct tls_context *context = conn->context;
835*a1157835SDaniel Fojt 
836*a1157835SDaniel Fojt 	if (!context->event_cb)
837*a1157835SDaniel Fojt 		return;
838*a1157835SDaniel Fojt 
839*a1157835SDaniel Fojt 	cert = get_x509_cert(err_cert);
840*a1157835SDaniel Fojt 	os_memset(&ev, 0, sizeof(ev));
841*a1157835SDaniel Fojt 	ev.cert_fail.reason = reason != TLS_FAIL_UNSPECIFIED ?
842*a1157835SDaniel Fojt 		reason : wolfssl_tls_fail_reason(err);
843*a1157835SDaniel Fojt 	ev.cert_fail.depth = depth;
844*a1157835SDaniel Fojt 	ev.cert_fail.subject = subject;
845*a1157835SDaniel Fojt 	ev.cert_fail.reason_txt = wolfssl_tls_err_string(err, err_str);
846*a1157835SDaniel Fojt 	ev.cert_fail.cert = cert;
847*a1157835SDaniel Fojt 	context->event_cb(context->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
848*a1157835SDaniel Fojt 	wpabuf_free(cert);
849*a1157835SDaniel Fojt }
850*a1157835SDaniel Fojt 
851*a1157835SDaniel Fojt 
wolfssl_tls_cert_event(struct tls_connection * conn,WOLFSSL_X509 * err_cert,int depth,const char * subject)852*a1157835SDaniel Fojt static void wolfssl_tls_cert_event(struct tls_connection *conn,
853*a1157835SDaniel Fojt 				   WOLFSSL_X509 *err_cert, int depth,
854*a1157835SDaniel Fojt 				   const char *subject)
855*a1157835SDaniel Fojt {
856*a1157835SDaniel Fojt 	struct wpabuf *cert = NULL;
857*a1157835SDaniel Fojt 	union tls_event_data ev;
858*a1157835SDaniel Fojt 	struct tls_context *context = conn->context;
859*a1157835SDaniel Fojt 	char *alt_subject[TLS_MAX_ALT_SUBJECT];
860*a1157835SDaniel Fojt 	int alt, num_alt_subject = 0;
861*a1157835SDaniel Fojt 	WOLFSSL_ASN1_OBJECT *gen;
862*a1157835SDaniel Fojt 	void *ext;
863*a1157835SDaniel Fojt 	int i;
864*a1157835SDaniel Fojt #ifdef CONFIG_SHA256
865*a1157835SDaniel Fojt 	u8 hash[32];
866*a1157835SDaniel Fojt #endif /* CONFIG_SHA256 */
867*a1157835SDaniel Fojt 
868*a1157835SDaniel Fojt 	if (!context->event_cb)
869*a1157835SDaniel Fojt 		return;
870*a1157835SDaniel Fojt 
871*a1157835SDaniel Fojt 	os_memset(&ev, 0, sizeof(ev));
872*a1157835SDaniel Fojt 	if (conn->cert_probe || (conn->flags & TLS_CONN_EXT_CERT_CHECK) ||
873*a1157835SDaniel Fojt 	    context->cert_in_cb) {
874*a1157835SDaniel Fojt 		cert = get_x509_cert(err_cert);
875*a1157835SDaniel Fojt 		ev.peer_cert.cert = cert;
876*a1157835SDaniel Fojt 	}
877*a1157835SDaniel Fojt 
878*a1157835SDaniel Fojt #ifdef CONFIG_SHA256
879*a1157835SDaniel Fojt 	if (cert) {
880*a1157835SDaniel Fojt 		const u8 *addr[1];
881*a1157835SDaniel Fojt 		size_t len[1];
882*a1157835SDaniel Fojt 
883*a1157835SDaniel Fojt 		addr[0] = wpabuf_head(cert);
884*a1157835SDaniel Fojt 		len[0] = wpabuf_len(cert);
885*a1157835SDaniel Fojt 		if (sha256_vector(1, addr, len, hash) == 0) {
886*a1157835SDaniel Fojt 			ev.peer_cert.hash = hash;
887*a1157835SDaniel Fojt 			ev.peer_cert.hash_len = sizeof(hash);
888*a1157835SDaniel Fojt 		}
889*a1157835SDaniel Fojt 	}
890*a1157835SDaniel Fojt #endif /* CONFIG_SHA256 */
891*a1157835SDaniel Fojt 
892*a1157835SDaniel Fojt 	ev.peer_cert.depth = depth;
893*a1157835SDaniel Fojt 	ev.peer_cert.subject = subject;
894*a1157835SDaniel Fojt 
895*a1157835SDaniel Fojt 	ext = wolfSSL_X509_get_ext_d2i(err_cert, ALT_NAMES_OID, NULL, NULL);
896*a1157835SDaniel Fojt 	for (i = 0; ext && i < wolfSSL_sk_num(ext); i++) {
897*a1157835SDaniel Fojt 		char *pos;
898*a1157835SDaniel Fojt 
899*a1157835SDaniel Fojt 		if (num_alt_subject == TLS_MAX_ALT_SUBJECT)
900*a1157835SDaniel Fojt 			break;
901*a1157835SDaniel Fojt 		gen = wolfSSL_sk_value((void *) ext, i);
902*a1157835SDaniel Fojt 		if (gen->type != GEN_EMAIL &&
903*a1157835SDaniel Fojt 		    gen->type != GEN_DNS &&
904*a1157835SDaniel Fojt 		    gen->type != GEN_URI)
905*a1157835SDaniel Fojt 			continue;
906*a1157835SDaniel Fojt 
907*a1157835SDaniel Fojt 		pos = os_malloc(10 + os_strlen((char *) gen->obj) + 1);
908*a1157835SDaniel Fojt 		if (!pos)
909*a1157835SDaniel Fojt 			break;
910*a1157835SDaniel Fojt 		alt_subject[num_alt_subject++] = pos;
911*a1157835SDaniel Fojt 
912*a1157835SDaniel Fojt 		switch (gen->type) {
913*a1157835SDaniel Fojt 		case GEN_EMAIL:
914*a1157835SDaniel Fojt 			os_memcpy(pos, "EMAIL:", 6);
915*a1157835SDaniel Fojt 			pos += 6;
916*a1157835SDaniel Fojt 			break;
917*a1157835SDaniel Fojt 		case GEN_DNS:
918*a1157835SDaniel Fojt 			os_memcpy(pos, "DNS:", 4);
919*a1157835SDaniel Fojt 			pos += 4;
920*a1157835SDaniel Fojt 			break;
921*a1157835SDaniel Fojt 		case GEN_URI:
922*a1157835SDaniel Fojt 			os_memcpy(pos, "URI:", 4);
923*a1157835SDaniel Fojt 			pos += 4;
924*a1157835SDaniel Fojt 			break;
925*a1157835SDaniel Fojt 		}
926*a1157835SDaniel Fojt 
927*a1157835SDaniel Fojt 		os_memcpy(pos, gen->obj, os_strlen((char *)gen->obj));
928*a1157835SDaniel Fojt 		pos += os_strlen((char *)gen->obj);
929*a1157835SDaniel Fojt 		*pos = '\0';
930*a1157835SDaniel Fojt 	}
931*a1157835SDaniel Fojt 	wolfSSL_sk_ASN1_OBJECT_free(ext);
932*a1157835SDaniel Fojt 
933*a1157835SDaniel Fojt 	for (alt = 0; alt < num_alt_subject; alt++)
934*a1157835SDaniel Fojt 		ev.peer_cert.altsubject[alt] = alt_subject[alt];
935*a1157835SDaniel Fojt 	ev.peer_cert.num_altsubject = num_alt_subject;
936*a1157835SDaniel Fojt 
937*a1157835SDaniel Fojt 	context->event_cb(context->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
938*a1157835SDaniel Fojt 	wpabuf_free(cert);
939*a1157835SDaniel Fojt 	for (alt = 0; alt < num_alt_subject; alt++)
940*a1157835SDaniel Fojt 		os_free(alt_subject[alt]);
941*a1157835SDaniel Fojt }
942*a1157835SDaniel Fojt 
943*a1157835SDaniel Fojt 
tls_verify_cb(int preverify_ok,WOLFSSL_X509_STORE_CTX * x509_ctx)944*a1157835SDaniel Fojt static int tls_verify_cb(int preverify_ok, WOLFSSL_X509_STORE_CTX *x509_ctx)
945*a1157835SDaniel Fojt {
946*a1157835SDaniel Fojt 	char buf[256];
947*a1157835SDaniel Fojt 	WOLFSSL_X509 *err_cert;
948*a1157835SDaniel Fojt 	int err, depth;
949*a1157835SDaniel Fojt 	WOLFSSL *ssl;
950*a1157835SDaniel Fojt 	struct tls_connection *conn;
951*a1157835SDaniel Fojt 	struct tls_context *context;
952*a1157835SDaniel Fojt 	char *match, *altmatch, *suffix_match, *domain_match;
953*a1157835SDaniel Fojt 	const char *err_str;
954*a1157835SDaniel Fojt 
955*a1157835SDaniel Fojt 	err_cert = wolfSSL_X509_STORE_CTX_get_current_cert(x509_ctx);
956*a1157835SDaniel Fojt 	if (!err_cert) {
957*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "wolfSSL: No Cert");
958*a1157835SDaniel Fojt 		return 0;
959*a1157835SDaniel Fojt 	}
960*a1157835SDaniel Fojt 
961*a1157835SDaniel Fojt 	err = wolfSSL_X509_STORE_CTX_get_error(x509_ctx);
962*a1157835SDaniel Fojt 	depth = wolfSSL_X509_STORE_CTX_get_error_depth(x509_ctx);
963*a1157835SDaniel Fojt 	ssl = wolfSSL_X509_STORE_CTX_get_ex_data(
964*a1157835SDaniel Fojt 		x509_ctx, wolfSSL_get_ex_data_X509_STORE_CTX_idx());
965*a1157835SDaniel Fojt 	wolfSSL_X509_NAME_oneline(wolfSSL_X509_get_subject_name(err_cert), buf,
966*a1157835SDaniel Fojt 				  sizeof(buf));
967*a1157835SDaniel Fojt 
968*a1157835SDaniel Fojt 	conn = wolfSSL_get_ex_data(ssl, 0);
969*a1157835SDaniel Fojt 	if (!conn) {
970*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "wolfSSL: No ex_data");
971*a1157835SDaniel Fojt 		return 0;
972*a1157835SDaniel Fojt 	}
973*a1157835SDaniel Fojt 
974*a1157835SDaniel Fojt 	if (depth == 0)
975*a1157835SDaniel Fojt 		conn->peer_cert = err_cert;
976*a1157835SDaniel Fojt 	else if (depth == 1)
977*a1157835SDaniel Fojt 		conn->peer_issuer = err_cert;
978*a1157835SDaniel Fojt 	else if (depth == 2)
979*a1157835SDaniel Fojt 		conn->peer_issuer_issuer = err_cert;
980*a1157835SDaniel Fojt 
981*a1157835SDaniel Fojt 	context = conn->context;
982*a1157835SDaniel Fojt 	match = conn->subject_match;
983*a1157835SDaniel Fojt 	altmatch = conn->alt_subject_match;
984*a1157835SDaniel Fojt 	suffix_match = conn->suffix_match;
985*a1157835SDaniel Fojt 	domain_match = conn->domain_match;
986*a1157835SDaniel Fojt 
987*a1157835SDaniel Fojt 	if (!preverify_ok && !conn->ca_cert_verify)
988*a1157835SDaniel Fojt 		preverify_ok = 1;
989*a1157835SDaniel Fojt 	if (!preverify_ok && depth > 0 && conn->server_cert_only)
990*a1157835SDaniel Fojt 		preverify_ok = 1;
991*a1157835SDaniel Fojt 	if (!preverify_ok && (conn->flags & TLS_CONN_DISABLE_TIME_CHECKS) &&
992*a1157835SDaniel Fojt 	    (err == X509_V_ERR_CERT_HAS_EXPIRED ||
993*a1157835SDaniel Fojt 	     err == ASN_AFTER_DATE_E || err == ASN_BEFORE_DATE_E ||
994*a1157835SDaniel Fojt 	     err == X509_V_ERR_CERT_NOT_YET_VALID)) {
995*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
996*a1157835SDaniel Fojt 			   "wolfSSL: Ignore certificate validity time mismatch");
997*a1157835SDaniel Fojt 		preverify_ok = 1;
998*a1157835SDaniel Fojt 	}
999*a1157835SDaniel Fojt 
1000*a1157835SDaniel Fojt 	err_str = wolfSSL_X509_verify_cert_error_string(err);
1001*a1157835SDaniel Fojt 
1002*a1157835SDaniel Fojt #ifdef CONFIG_SHA256
1003*a1157835SDaniel Fojt 	/*
1004*a1157835SDaniel Fojt 	 * Do not require preverify_ok so we can explicity allow otherwise
1005*a1157835SDaniel Fojt 	 * invalid pinned server certificates.
1006*a1157835SDaniel Fojt 	 */
1007*a1157835SDaniel Fojt 	if (depth == 0 && conn->server_cert_only) {
1008*a1157835SDaniel Fojt 		struct wpabuf *cert;
1009*a1157835SDaniel Fojt 
1010*a1157835SDaniel Fojt 		cert = get_x509_cert(err_cert);
1011*a1157835SDaniel Fojt 		if (!cert) {
1012*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1013*a1157835SDaniel Fojt 				   "wolfSSL: Could not fetch server certificate data");
1014*a1157835SDaniel Fojt 			preverify_ok = 0;
1015*a1157835SDaniel Fojt 		} else {
1016*a1157835SDaniel Fojt 			u8 hash[32];
1017*a1157835SDaniel Fojt 			const u8 *addr[1];
1018*a1157835SDaniel Fojt 			size_t len[1];
1019*a1157835SDaniel Fojt 
1020*a1157835SDaniel Fojt 			addr[0] = wpabuf_head(cert);
1021*a1157835SDaniel Fojt 			len[0] = wpabuf_len(cert);
1022*a1157835SDaniel Fojt 			if (sha256_vector(1, addr, len, hash) < 0 ||
1023*a1157835SDaniel Fojt 			    os_memcmp(conn->srv_cert_hash, hash, 32) != 0) {
1024*a1157835SDaniel Fojt 				err_str = "Server certificate mismatch";
1025*a1157835SDaniel Fojt 				err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
1026*a1157835SDaniel Fojt 				preverify_ok = 0;
1027*a1157835SDaniel Fojt 			} else if (!preverify_ok) {
1028*a1157835SDaniel Fojt 				/*
1029*a1157835SDaniel Fojt 				 * Certificate matches pinned certificate, allow
1030*a1157835SDaniel Fojt 				 * regardless of other problems.
1031*a1157835SDaniel Fojt 				 */
1032*a1157835SDaniel Fojt 				wpa_printf(MSG_DEBUG,
1033*a1157835SDaniel Fojt 					   "wolfSSL: Ignore validation issues for a pinned server certificate");
1034*a1157835SDaniel Fojt 				preverify_ok = 1;
1035*a1157835SDaniel Fojt 			}
1036*a1157835SDaniel Fojt 			wpabuf_free(cert);
1037*a1157835SDaniel Fojt 		}
1038*a1157835SDaniel Fojt 	}
1039*a1157835SDaniel Fojt #endif /* CONFIG_SHA256 */
1040*a1157835SDaniel Fojt 
1041*a1157835SDaniel Fojt 	if (!preverify_ok) {
1042*a1157835SDaniel Fojt 		wpa_printf(MSG_WARNING,
1043*a1157835SDaniel Fojt 			   "TLS: Certificate verification failed, error %d (%s) depth %d for '%s'",
1044*a1157835SDaniel Fojt 			   err, err_str, depth, buf);
1045*a1157835SDaniel Fojt 		wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1046*a1157835SDaniel Fojt 				       err_str, TLS_FAIL_UNSPECIFIED);
1047*a1157835SDaniel Fojt 		return preverify_ok;
1048*a1157835SDaniel Fojt 	}
1049*a1157835SDaniel Fojt 
1050*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
1051*a1157835SDaniel Fojt 		   "TLS: %s - preverify_ok=%d err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1052*a1157835SDaniel Fojt 		   __func__, preverify_ok, err, err_str,
1053*a1157835SDaniel Fojt 		   conn->ca_cert_verify, depth, buf);
1054*a1157835SDaniel Fojt 	if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1055*a1157835SDaniel Fojt 		wpa_printf(MSG_WARNING,
1056*a1157835SDaniel Fojt 			   "TLS: Subject '%s' did not match with '%s'",
1057*a1157835SDaniel Fojt 			   buf, match);
1058*a1157835SDaniel Fojt 		preverify_ok = 0;
1059*a1157835SDaniel Fojt 		wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1060*a1157835SDaniel Fojt 				       "Subject mismatch",
1061*a1157835SDaniel Fojt 				       TLS_FAIL_SUBJECT_MISMATCH);
1062*a1157835SDaniel Fojt 	} else if (depth == 0 && altmatch &&
1063*a1157835SDaniel Fojt 		   !tls_match_alt_subject(err_cert, altmatch)) {
1064*a1157835SDaniel Fojt 		wpa_printf(MSG_WARNING,
1065*a1157835SDaniel Fojt 			   "TLS: altSubjectName match '%s' not found",
1066*a1157835SDaniel Fojt 			   altmatch);
1067*a1157835SDaniel Fojt 		preverify_ok = 0;
1068*a1157835SDaniel Fojt 		wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1069*a1157835SDaniel Fojt 				       "AltSubject mismatch",
1070*a1157835SDaniel Fojt 				       TLS_FAIL_ALTSUBJECT_MISMATCH);
1071*a1157835SDaniel Fojt 	} else if (depth == 0 && suffix_match &&
1072*a1157835SDaniel Fojt 		   !tls_match_suffix(err_cert, suffix_match, 0)) {
1073*a1157835SDaniel Fojt 		wpa_printf(MSG_WARNING,
1074*a1157835SDaniel Fojt 			   "TLS: Domain suffix match '%s' not found",
1075*a1157835SDaniel Fojt 			   suffix_match);
1076*a1157835SDaniel Fojt 		preverify_ok = 0;
1077*a1157835SDaniel Fojt 		wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1078*a1157835SDaniel Fojt 				       "Domain suffix mismatch",
1079*a1157835SDaniel Fojt 				       TLS_FAIL_DOMAIN_SUFFIX_MISMATCH);
1080*a1157835SDaniel Fojt 	} else if (depth == 0 && domain_match &&
1081*a1157835SDaniel Fojt 		   !tls_match_suffix(err_cert, domain_match, 1)) {
1082*a1157835SDaniel Fojt 		wpa_printf(MSG_WARNING, "TLS: Domain match '%s' not found",
1083*a1157835SDaniel Fojt 			   domain_match);
1084*a1157835SDaniel Fojt 		preverify_ok = 0;
1085*a1157835SDaniel Fojt 		wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1086*a1157835SDaniel Fojt 				       "Domain mismatch",
1087*a1157835SDaniel Fojt 				       TLS_FAIL_DOMAIN_MISMATCH);
1088*a1157835SDaniel Fojt 	} else {
1089*a1157835SDaniel Fojt 		wolfssl_tls_cert_event(conn, err_cert, depth, buf);
1090*a1157835SDaniel Fojt 	}
1091*a1157835SDaniel Fojt 
1092*a1157835SDaniel Fojt 	if (conn->cert_probe && preverify_ok && depth == 0) {
1093*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1094*a1157835SDaniel Fojt 			   "wolfSSL: Reject server certificate on probe-only run");
1095*a1157835SDaniel Fojt 		preverify_ok = 0;
1096*a1157835SDaniel Fojt 		wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1097*a1157835SDaniel Fojt 				       "Server certificate chain probe",
1098*a1157835SDaniel Fojt 				       TLS_FAIL_SERVER_CHAIN_PROBE);
1099*a1157835SDaniel Fojt 	}
1100*a1157835SDaniel Fojt 
1101*a1157835SDaniel Fojt #ifdef HAVE_OCSP_WOLFSSL
1102*a1157835SDaniel Fojt 	if (depth == 0 && (conn->flags & TLS_CONN_REQUEST_OCSP) &&
1103*a1157835SDaniel Fojt 	    preverify_ok) {
1104*a1157835SDaniel Fojt 		enum ocsp_result res;
1105*a1157835SDaniel Fojt 
1106*a1157835SDaniel Fojt 		res = check_ocsp_resp(conn->ssl_ctx, conn->ssl, err_cert,
1107*a1157835SDaniel Fojt 				      conn->peer_issuer,
1108*a1157835SDaniel Fojt 				      conn->peer_issuer_issuer);
1109*a1157835SDaniel Fojt 		if (res == OCSP_REVOKED) {
1110*a1157835SDaniel Fojt 			preverify_ok = 0;
1111*a1157835SDaniel Fojt 			wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1112*a1157835SDaniel Fojt 					       "certificate revoked",
1113*a1157835SDaniel Fojt 					       TLS_FAIL_REVOKED);
1114*a1157835SDaniel Fojt 			if (err == X509_V_OK)
1115*a1157835SDaniel Fojt 				X509_STORE_CTX_set_error(
1116*a1157835SDaniel Fojt 					x509_ctx, X509_V_ERR_CERT_REVOKED);
1117*a1157835SDaniel Fojt 		} else if (res != OCSP_GOOD &&
1118*a1157835SDaniel Fojt 			   (conn->flags & TLS_CONN_REQUIRE_OCSP)) {
1119*a1157835SDaniel Fojt 			preverify_ok = 0;
1120*a1157835SDaniel Fojt 			wolfssl_tls_fail_event(conn, err_cert, err, depth, buf,
1121*a1157835SDaniel Fojt 					       "bad certificate status response",
1122*a1157835SDaniel Fojt 					       TLS_FAIL_UNSPECIFIED);
1123*a1157835SDaniel Fojt 		}
1124*a1157835SDaniel Fojt 	}
1125*a1157835SDaniel Fojt #endif /* HAVE_OCSP_WOLFSSL */
1126*a1157835SDaniel Fojt 	if (depth == 0 && preverify_ok && context->event_cb != NULL)
1127*a1157835SDaniel Fojt 		context->event_cb(context->cb_ctx,
1128*a1157835SDaniel Fojt 				  TLS_CERT_CHAIN_SUCCESS, NULL);
1129*a1157835SDaniel Fojt 
1130*a1157835SDaniel Fojt 	return preverify_ok;
1131*a1157835SDaniel Fojt }
1132*a1157835SDaniel Fojt 
1133*a1157835SDaniel Fojt 
tls_connection_ca_cert(void * tls_ctx,struct tls_connection * conn,const char * ca_cert,const u8 * ca_cert_blob,size_t blob_len,const char * ca_path)1134*a1157835SDaniel Fojt static int tls_connection_ca_cert(void *tls_ctx, struct tls_connection *conn,
1135*a1157835SDaniel Fojt 				  const char *ca_cert,
1136*a1157835SDaniel Fojt 				  const u8 *ca_cert_blob, size_t blob_len,
1137*a1157835SDaniel Fojt 				  const char *ca_path)
1138*a1157835SDaniel Fojt {
1139*a1157835SDaniel Fojt 	WOLFSSL_CTX *ctx = tls_ctx;
1140*a1157835SDaniel Fojt 
1141*a1157835SDaniel Fojt 	wolfSSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1142*a1157835SDaniel Fojt 	conn->ca_cert_verify = 1;
1143*a1157835SDaniel Fojt 
1144*a1157835SDaniel Fojt 	if (ca_cert && os_strncmp(ca_cert, "probe://", 8) == 0) {
1145*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1146*a1157835SDaniel Fojt 			   "wolfSSL: Probe for server certificate chain");
1147*a1157835SDaniel Fojt 		conn->cert_probe = 1;
1148*a1157835SDaniel Fojt 		conn->ca_cert_verify = 0;
1149*a1157835SDaniel Fojt 		return 0;
1150*a1157835SDaniel Fojt 	}
1151*a1157835SDaniel Fojt 
1152*a1157835SDaniel Fojt 	if (ca_cert && os_strncmp(ca_cert, "hash://", 7) == 0) {
1153*a1157835SDaniel Fojt #ifdef CONFIG_SHA256
1154*a1157835SDaniel Fojt 		const char *pos = ca_cert + 7;
1155*a1157835SDaniel Fojt 
1156*a1157835SDaniel Fojt 		if (os_strncmp(pos, "server/sha256/", 14) != 0) {
1157*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1158*a1157835SDaniel Fojt 				   "wolfSSL: Unsupported ca_cert hash value '%s'",
1159*a1157835SDaniel Fojt 				   ca_cert);
1160*a1157835SDaniel Fojt 			return -1;
1161*a1157835SDaniel Fojt 		}
1162*a1157835SDaniel Fojt 		pos += 14;
1163*a1157835SDaniel Fojt 		if (os_strlen(pos) != 32 * 2) {
1164*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1165*a1157835SDaniel Fojt 				   "wolfSSL: Unexpected SHA256 hash length in ca_cert '%s'",
1166*a1157835SDaniel Fojt 				   ca_cert);
1167*a1157835SDaniel Fojt 			return -1;
1168*a1157835SDaniel Fojt 		}
1169*a1157835SDaniel Fojt 		if (hexstr2bin(pos, conn->srv_cert_hash, 32) < 0) {
1170*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1171*a1157835SDaniel Fojt 				   "wolfSSL: Invalid SHA256 hash value in ca_cert '%s'",
1172*a1157835SDaniel Fojt 				   ca_cert);
1173*a1157835SDaniel Fojt 			return -1;
1174*a1157835SDaniel Fojt 		}
1175*a1157835SDaniel Fojt 		conn->server_cert_only = 1;
1176*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1177*a1157835SDaniel Fojt 			   "wolfSSL: Checking only server certificate match");
1178*a1157835SDaniel Fojt 		return 0;
1179*a1157835SDaniel Fojt #else /* CONFIG_SHA256 */
1180*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
1181*a1157835SDaniel Fojt 			   "No SHA256 included in the build - cannot validate server certificate hash");
1182*a1157835SDaniel Fojt 		return -1;
1183*a1157835SDaniel Fojt #endif /* CONFIG_SHA256 */
1184*a1157835SDaniel Fojt 	}
1185*a1157835SDaniel Fojt 
1186*a1157835SDaniel Fojt 	if (ca_cert_blob) {
1187*a1157835SDaniel Fojt 		if (wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_blob, blob_len,
1188*a1157835SDaniel Fojt 						   SSL_FILETYPE_ASN1) !=
1189*a1157835SDaniel Fojt 		    SSL_SUCCESS) {
1190*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO, "SSL: failed to load CA blob");
1191*a1157835SDaniel Fojt 			return -1;
1192*a1157835SDaniel Fojt 		}
1193*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: use CA cert blob OK");
1194*a1157835SDaniel Fojt 		return 0;
1195*a1157835SDaniel Fojt 	}
1196*a1157835SDaniel Fojt 
1197*a1157835SDaniel Fojt 	if (ca_cert || ca_path) {
1198*a1157835SDaniel Fojt 		WOLFSSL_X509_STORE *cm = wolfSSL_X509_STORE_new();
1199*a1157835SDaniel Fojt 
1200*a1157835SDaniel Fojt 		if (!cm) {
1201*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
1202*a1157835SDaniel Fojt 				   "SSL: failed to create certificate store");
1203*a1157835SDaniel Fojt 			return -1;
1204*a1157835SDaniel Fojt 		}
1205*a1157835SDaniel Fojt 		wolfSSL_CTX_set_cert_store(ctx, cm);
1206*a1157835SDaniel Fojt 
1207*a1157835SDaniel Fojt 		if (wolfSSL_CTX_load_verify_locations(ctx, ca_cert, ca_path) !=
1208*a1157835SDaniel Fojt 		    SSL_SUCCESS) {
1209*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
1210*a1157835SDaniel Fojt 				   "SSL: failed to load ca_cert as PEM");
1211*a1157835SDaniel Fojt 
1212*a1157835SDaniel Fojt 			if (!ca_cert)
1213*a1157835SDaniel Fojt 				return -1;
1214*a1157835SDaniel Fojt 
1215*a1157835SDaniel Fojt 			if (wolfSSL_CTX_der_load_verify_locations(
1216*a1157835SDaniel Fojt 				    ctx, ca_cert, SSL_FILETYPE_ASN1) !=
1217*a1157835SDaniel Fojt 			    SSL_SUCCESS) {
1218*a1157835SDaniel Fojt 				wpa_printf(MSG_INFO,
1219*a1157835SDaniel Fojt 					   "SSL: failed to load ca_cert as DER");
1220*a1157835SDaniel Fojt 				return -1;
1221*a1157835SDaniel Fojt 			}
1222*a1157835SDaniel Fojt 		}
1223*a1157835SDaniel Fojt 		return 0;
1224*a1157835SDaniel Fojt 	}
1225*a1157835SDaniel Fojt 
1226*a1157835SDaniel Fojt 	conn->ca_cert_verify = 0;
1227*a1157835SDaniel Fojt 	return 0;
1228*a1157835SDaniel Fojt }
1229*a1157835SDaniel Fojt 
1230*a1157835SDaniel Fojt 
tls_set_conn_flags(WOLFSSL * ssl,unsigned int flags)1231*a1157835SDaniel Fojt static void tls_set_conn_flags(WOLFSSL *ssl, unsigned int flags)
1232*a1157835SDaniel Fojt {
1233*a1157835SDaniel Fojt #ifdef HAVE_SESSION_TICKET
1234*a1157835SDaniel Fojt #if 0
1235*a1157835SDaniel Fojt 	if (!(flags & TLS_CONN_DISABLE_SESSION_TICKET))
1236*a1157835SDaniel Fojt 		wolfSSL_UseSessionTicket(ssl);
1237*a1157835SDaniel Fojt #endif
1238*a1157835SDaniel Fojt #endif /* HAVE_SESSION_TICKET */
1239*a1157835SDaniel Fojt 
1240*a1157835SDaniel Fojt 	if (flags & TLS_CONN_DISABLE_TLSv1_0)
1241*a1157835SDaniel Fojt 		wolfSSL_set_options(ssl, SSL_OP_NO_TLSv1);
1242*a1157835SDaniel Fojt 	if (flags & TLS_CONN_DISABLE_TLSv1_1)
1243*a1157835SDaniel Fojt 		wolfSSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
1244*a1157835SDaniel Fojt 	if (flags & TLS_CONN_DISABLE_TLSv1_2)
1245*a1157835SDaniel Fojt 		wolfSSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
1246*a1157835SDaniel Fojt }
1247*a1157835SDaniel Fojt 
1248*a1157835SDaniel Fojt 
tls_connection_set_params(void * tls_ctx,struct tls_connection * conn,const struct tls_connection_params * params)1249*a1157835SDaniel Fojt int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
1250*a1157835SDaniel Fojt 			      const struct tls_connection_params *params)
1251*a1157835SDaniel Fojt {
1252*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: set params");
1253*a1157835SDaniel Fojt 
1254*a1157835SDaniel Fojt 	if (tls_connection_set_subject_match(conn, params->subject_match,
1255*a1157835SDaniel Fojt 					     params->altsubject_match,
1256*a1157835SDaniel Fojt 					     params->suffix_match,
1257*a1157835SDaniel Fojt 					     params->domain_match) < 0) {
1258*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Error setting subject match");
1259*a1157835SDaniel Fojt 		return -1;
1260*a1157835SDaniel Fojt 	}
1261*a1157835SDaniel Fojt 
1262*a1157835SDaniel Fojt 	if (tls_connection_ca_cert(tls_ctx, conn, params->ca_cert,
1263*a1157835SDaniel Fojt 				   params->ca_cert_blob,
1264*a1157835SDaniel Fojt 				   params->ca_cert_blob_len,
1265*a1157835SDaniel Fojt 				   params->ca_path) < 0) {
1266*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Error setting CA cert");
1267*a1157835SDaniel Fojt 		return -1;
1268*a1157835SDaniel Fojt 	}
1269*a1157835SDaniel Fojt 
1270*a1157835SDaniel Fojt 	if (tls_connection_client_cert(conn, params->client_cert,
1271*a1157835SDaniel Fojt 				       params->client_cert_blob,
1272*a1157835SDaniel Fojt 				       params->client_cert_blob_len) < 0) {
1273*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Error setting client cert");
1274*a1157835SDaniel Fojt 		return -1;
1275*a1157835SDaniel Fojt 	}
1276*a1157835SDaniel Fojt 
1277*a1157835SDaniel Fojt 	if (tls_connection_private_key(tls_ctx, conn, params->private_key,
1278*a1157835SDaniel Fojt 				       params->private_key_passwd,
1279*a1157835SDaniel Fojt 				       params->private_key_blob,
1280*a1157835SDaniel Fojt 				       params->private_key_blob_len) < 0) {
1281*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Error setting private key");
1282*a1157835SDaniel Fojt 		return -1;
1283*a1157835SDaniel Fojt 	}
1284*a1157835SDaniel Fojt 
1285*a1157835SDaniel Fojt 	if (tls_connection_dh(conn, params->dh_file, params->dh_blob,
1286*a1157835SDaniel Fojt 			      params->dh_blob_len) < 0) {
1287*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Error setting DH");
1288*a1157835SDaniel Fojt 		return -1;
1289*a1157835SDaniel Fojt 	}
1290*a1157835SDaniel Fojt 
1291*a1157835SDaniel Fojt 	if (params->openssl_ciphers &&
1292*a1157835SDaniel Fojt 	    wolfSSL_set_cipher_list(conn->ssl, params->openssl_ciphers) != 1) {
1293*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
1294*a1157835SDaniel Fojt 			   "wolfSSL: Failed to set cipher string '%s'",
1295*a1157835SDaniel Fojt 			   params->openssl_ciphers);
1296*a1157835SDaniel Fojt 		return -1;
1297*a1157835SDaniel Fojt 	}
1298*a1157835SDaniel Fojt 
1299*a1157835SDaniel Fojt 	tls_set_conn_flags(conn->ssl, params->flags);
1300*a1157835SDaniel Fojt 
1301*a1157835SDaniel Fojt #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
1302*a1157835SDaniel Fojt 	if (params->flags & TLS_CONN_REQUEST_OCSP) {
1303*a1157835SDaniel Fojt 		if (wolfSSL_UseOCSPStapling(conn->ssl, WOLFSSL_CSR_OCSP,
1304*a1157835SDaniel Fojt 					    WOLFSSL_CSR_OCSP_USE_NONCE) !=
1305*a1157835SDaniel Fojt 		    SSL_SUCCESS)
1306*a1157835SDaniel Fojt 			return -1;
1307*a1157835SDaniel Fojt 		wolfSSL_CTX_EnableOCSP(tls_ctx, 0);
1308*a1157835SDaniel Fojt 	}
1309*a1157835SDaniel Fojt #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
1310*a1157835SDaniel Fojt #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
1311*a1157835SDaniel Fojt 	if (params->flags & TLS_CONN_REQUEST_OCSP) {
1312*a1157835SDaniel Fojt 		if (wolfSSL_UseOCSPStaplingV2(conn->ssl,
1313*a1157835SDaniel Fojt 					      WOLFSSL_CSR2_OCSP_MULTI, 0) !=
1314*a1157835SDaniel Fojt 		    SSL_SUCCESS)
1315*a1157835SDaniel Fojt 			return -1;
1316*a1157835SDaniel Fojt 		wolfSSL_CTX_EnableOCSP(tls_ctx, 0);
1317*a1157835SDaniel Fojt 	}
1318*a1157835SDaniel Fojt #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
1319*a1157835SDaniel Fojt #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
1320*a1157835SDaniel Fojt     !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
1321*a1157835SDaniel Fojt #ifdef HAVE_OCSP
1322*a1157835SDaniel Fojt 	if (params->flags & TLS_CONN_REQUEST_OCSP)
1323*a1157835SDaniel Fojt 		wolfSSL_CTX_EnableOCSP(ctx, 0);
1324*a1157835SDaniel Fojt #else /* HAVE_OCSP */
1325*a1157835SDaniel Fojt 	if (params->flags & TLS_CONN_REQUIRE_OCSP) {
1326*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
1327*a1157835SDaniel Fojt 			   "wolfSSL: No OCSP support included - reject configuration");
1328*a1157835SDaniel Fojt 		return -1;
1329*a1157835SDaniel Fojt 	}
1330*a1157835SDaniel Fojt 	if (params->flags & TLS_CONN_REQUEST_OCSP) {
1331*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1332*a1157835SDaniel Fojt 			   "wolfSSL: No OCSP support included - allow optional OCSP case to continue");
1333*a1157835SDaniel Fojt 	}
1334*a1157835SDaniel Fojt #endif /* HAVE_OCSP */
1335*a1157835SDaniel Fojt #endif /* !HAVE_CERTIFICATE_STATUS_REQUEST &&
1336*a1157835SDaniel Fojt 	* !HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
1337*a1157835SDaniel Fojt 
1338*a1157835SDaniel Fojt 	conn->flags = params->flags;
1339*a1157835SDaniel Fojt 
1340*a1157835SDaniel Fojt 	return 0;
1341*a1157835SDaniel Fojt }
1342*a1157835SDaniel Fojt 
1343*a1157835SDaniel Fojt 
tls_global_ca_cert(void * ssl_ctx,const char * ca_cert)1344*a1157835SDaniel Fojt static int tls_global_ca_cert(void *ssl_ctx, const char *ca_cert)
1345*a1157835SDaniel Fojt {
1346*a1157835SDaniel Fojt 	WOLFSSL_CTX *ctx = ssl_ctx;
1347*a1157835SDaniel Fojt 
1348*a1157835SDaniel Fojt 	if (ca_cert) {
1349*a1157835SDaniel Fojt 		if (wolfSSL_CTX_load_verify_locations(ctx, ca_cert, NULL) != 1)
1350*a1157835SDaniel Fojt 		{
1351*a1157835SDaniel Fojt 			wpa_printf(MSG_WARNING,
1352*a1157835SDaniel Fojt 				   "Failed to load root certificates");
1353*a1157835SDaniel Fojt 			return -1;
1354*a1157835SDaniel Fojt 		}
1355*a1157835SDaniel Fojt 
1356*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1357*a1157835SDaniel Fojt 			   "TLS: Trusted root certificate(s) loaded");
1358*a1157835SDaniel Fojt 	}
1359*a1157835SDaniel Fojt 
1360*a1157835SDaniel Fojt 	return 0;
1361*a1157835SDaniel Fojt }
1362*a1157835SDaniel Fojt 
1363*a1157835SDaniel Fojt 
tls_global_client_cert(void * ssl_ctx,const char * client_cert)1364*a1157835SDaniel Fojt static int tls_global_client_cert(void *ssl_ctx, const char *client_cert)
1365*a1157835SDaniel Fojt {
1366*a1157835SDaniel Fojt 	WOLFSSL_CTX *ctx = ssl_ctx;
1367*a1157835SDaniel Fojt 
1368*a1157835SDaniel Fojt 	if (!client_cert)
1369*a1157835SDaniel Fojt 		return 0;
1370*a1157835SDaniel Fojt 
1371*a1157835SDaniel Fojt 	if (wolfSSL_CTX_use_certificate_chain_file_format(ctx, client_cert,
1372*a1157835SDaniel Fojt 							  SSL_FILETYPE_ASN1) !=
1373*a1157835SDaniel Fojt 	    SSL_SUCCESS &&
1374*a1157835SDaniel Fojt 	    wolfSSL_CTX_use_certificate_chain_file(ctx, client_cert) !=
1375*a1157835SDaniel Fojt 	    SSL_SUCCESS) {
1376*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Failed to load client certificate");
1377*a1157835SDaniel Fojt 		return -1;
1378*a1157835SDaniel Fojt 	}
1379*a1157835SDaniel Fojt 
1380*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: Loaded global client certificate: %s",
1381*a1157835SDaniel Fojt 		   client_cert);
1382*a1157835SDaniel Fojt 
1383*a1157835SDaniel Fojt 	return 0;
1384*a1157835SDaniel Fojt }
1385*a1157835SDaniel Fojt 
1386*a1157835SDaniel Fojt 
tls_global_private_key(void * ssl_ctx,const char * private_key,const char * private_key_passwd)1387*a1157835SDaniel Fojt static int tls_global_private_key(void *ssl_ctx, const char *private_key,
1388*a1157835SDaniel Fojt 				  const char *private_key_passwd)
1389*a1157835SDaniel Fojt {
1390*a1157835SDaniel Fojt 	WOLFSSL_CTX *ctx = ssl_ctx;
1391*a1157835SDaniel Fojt 	char *passwd = NULL;
1392*a1157835SDaniel Fojt 	int ret = 0;
1393*a1157835SDaniel Fojt 
1394*a1157835SDaniel Fojt 	if (!private_key)
1395*a1157835SDaniel Fojt 		return 0;
1396*a1157835SDaniel Fojt 
1397*a1157835SDaniel Fojt 	if (private_key_passwd) {
1398*a1157835SDaniel Fojt 		passwd = os_strdup(private_key_passwd);
1399*a1157835SDaniel Fojt 		if (!passwd)
1400*a1157835SDaniel Fojt 			return -1;
1401*a1157835SDaniel Fojt 	}
1402*a1157835SDaniel Fojt 
1403*a1157835SDaniel Fojt 	wolfSSL_CTX_set_default_passwd_cb(ctx, tls_passwd_cb);
1404*a1157835SDaniel Fojt 	wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, passwd);
1405*a1157835SDaniel Fojt 
1406*a1157835SDaniel Fojt 	if (wolfSSL_CTX_use_PrivateKey_file(ctx, private_key,
1407*a1157835SDaniel Fojt 					    SSL_FILETYPE_ASN1) != 1 &&
1408*a1157835SDaniel Fojt 	    wolfSSL_CTX_use_PrivateKey_file(ctx, private_key,
1409*a1157835SDaniel Fojt 					    SSL_FILETYPE_PEM) != 1) {
1410*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Failed to load private key");
1411*a1157835SDaniel Fojt 		ret = -1;
1412*a1157835SDaniel Fojt 	}
1413*a1157835SDaniel Fojt 
1414*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: Loaded global private key");
1415*a1157835SDaniel Fojt 
1416*a1157835SDaniel Fojt 	os_free(passwd);
1417*a1157835SDaniel Fojt 	wolfSSL_CTX_set_default_passwd_cb(ctx, NULL);
1418*a1157835SDaniel Fojt 
1419*a1157835SDaniel Fojt 	return ret;
1420*a1157835SDaniel Fojt }
1421*a1157835SDaniel Fojt 
1422*a1157835SDaniel Fojt 
tls_global_dh(void * ssl_ctx,const char * dh_file,const u8 * dh_blob,size_t blob_len)1423*a1157835SDaniel Fojt static int tls_global_dh(void *ssl_ctx, const char *dh_file,
1424*a1157835SDaniel Fojt 			 const u8 *dh_blob, size_t blob_len)
1425*a1157835SDaniel Fojt {
1426*a1157835SDaniel Fojt 	WOLFSSL_CTX *ctx = ssl_ctx;
1427*a1157835SDaniel Fojt 
1428*a1157835SDaniel Fojt 	if (!dh_file && !dh_blob)
1429*a1157835SDaniel Fojt 		return 0;
1430*a1157835SDaniel Fojt 
1431*a1157835SDaniel Fojt 	if (dh_blob) {
1432*a1157835SDaniel Fojt 		if (wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_blob, blob_len,
1433*a1157835SDaniel Fojt 						SSL_FILETYPE_ASN1) < 0) {
1434*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
1435*a1157835SDaniel Fojt 				   "SSL: global use DH DER blob failed");
1436*a1157835SDaniel Fojt 			return -1;
1437*a1157835SDaniel Fojt 		}
1438*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: global use DH blob OK");
1439*a1157835SDaniel Fojt 		return 0;
1440*a1157835SDaniel Fojt 	}
1441*a1157835SDaniel Fojt 
1442*a1157835SDaniel Fojt 	if (dh_file) {
1443*a1157835SDaniel Fojt 		if (wolfSSL_CTX_SetTmpDH_file(ctx, dh_file, SSL_FILETYPE_PEM) <
1444*a1157835SDaniel Fojt 		    0) {
1445*a1157835SDaniel Fojt 			wpa_printf(MSG_INFO,
1446*a1157835SDaniel Fojt 				   "SSL: global use DH PEM file failed");
1447*a1157835SDaniel Fojt 			if (wolfSSL_CTX_SetTmpDH_file(ctx, dh_file,
1448*a1157835SDaniel Fojt 						      SSL_FILETYPE_ASN1) < 0) {
1449*a1157835SDaniel Fojt 				wpa_printf(MSG_INFO,
1450*a1157835SDaniel Fojt 					   "SSL: global use DH DER file failed");
1451*a1157835SDaniel Fojt 				return -1;
1452*a1157835SDaniel Fojt 			}
1453*a1157835SDaniel Fojt 		}
1454*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: global use DH file OK");
1455*a1157835SDaniel Fojt 		return 0;
1456*a1157835SDaniel Fojt 	}
1457*a1157835SDaniel Fojt 
1458*a1157835SDaniel Fojt 	return 0;
1459*a1157835SDaniel Fojt }
1460*a1157835SDaniel Fojt 
1461*a1157835SDaniel Fojt 
1462*a1157835SDaniel Fojt #ifdef HAVE_OCSP
1463*a1157835SDaniel Fojt 
ocsp_status_cb(void * unused,const char * url,int url_sz,unsigned char * request,int request_sz,unsigned char ** response)1464*a1157835SDaniel Fojt int ocsp_status_cb(void *unused, const char *url, int url_sz,
1465*a1157835SDaniel Fojt 		   unsigned char *request, int request_sz,
1466*a1157835SDaniel Fojt 		   unsigned char **response)
1467*a1157835SDaniel Fojt {
1468*a1157835SDaniel Fojt 	size_t len;
1469*a1157835SDaniel Fojt 
1470*a1157835SDaniel Fojt 	(void) unused;
1471*a1157835SDaniel Fojt 
1472*a1157835SDaniel Fojt 	if (!url) {
1473*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1474*a1157835SDaniel Fojt 			   "wolfSSL: OCSP status callback - no response configured");
1475*a1157835SDaniel Fojt 		*response = NULL;
1476*a1157835SDaniel Fojt 		return 0;
1477*a1157835SDaniel Fojt 	}
1478*a1157835SDaniel Fojt 
1479*a1157835SDaniel Fojt 	*response = (unsigned char *) os_readfile(url, &len);
1480*a1157835SDaniel Fojt 	if (!*response) {
1481*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1482*a1157835SDaniel Fojt 			   "wolfSSL: OCSP status callback - could not read response file");
1483*a1157835SDaniel Fojt 		return -1;
1484*a1157835SDaniel Fojt 	}
1485*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
1486*a1157835SDaniel Fojt 		   "wolfSSL: OCSP status callback - send cached response");
1487*a1157835SDaniel Fojt 	return len;
1488*a1157835SDaniel Fojt }
1489*a1157835SDaniel Fojt 
1490*a1157835SDaniel Fojt 
ocsp_resp_free_cb(void * ocsp_stapling_response,unsigned char * response)1491*a1157835SDaniel Fojt void ocsp_resp_free_cb(void *ocsp_stapling_response, unsigned char *response)
1492*a1157835SDaniel Fojt {
1493*a1157835SDaniel Fojt 	os_free(response);
1494*a1157835SDaniel Fojt }
1495*a1157835SDaniel Fojt 
1496*a1157835SDaniel Fojt #endif /* HAVE_OCSP */
1497*a1157835SDaniel Fojt 
1498*a1157835SDaniel Fojt 
tls_global_set_params(void * tls_ctx,const struct tls_connection_params * params)1499*a1157835SDaniel Fojt int tls_global_set_params(void *tls_ctx,
1500*a1157835SDaniel Fojt 			  const struct tls_connection_params *params)
1501*a1157835SDaniel Fojt {
1502*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: global set params");
1503*a1157835SDaniel Fojt 
1504*a1157835SDaniel Fojt 	if (params->check_cert_subject)
1505*a1157835SDaniel Fojt 		return -1; /* not yet supported */
1506*a1157835SDaniel Fojt 
1507*a1157835SDaniel Fojt 	if (tls_global_ca_cert(tls_ctx, params->ca_cert) < 0) {
1508*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "SSL: Failed to load ca cert file '%s'",
1509*a1157835SDaniel Fojt 			   params->ca_cert);
1510*a1157835SDaniel Fojt 		return -1;
1511*a1157835SDaniel Fojt 	}
1512*a1157835SDaniel Fojt 
1513*a1157835SDaniel Fojt 	if (tls_global_client_cert(tls_ctx, params->client_cert) < 0) {
1514*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
1515*a1157835SDaniel Fojt 			   "SSL: Failed to load client cert file '%s'",
1516*a1157835SDaniel Fojt 			   params->client_cert);
1517*a1157835SDaniel Fojt 		return -1;
1518*a1157835SDaniel Fojt 	}
1519*a1157835SDaniel Fojt 
1520*a1157835SDaniel Fojt 	if (tls_global_private_key(tls_ctx, params->private_key,
1521*a1157835SDaniel Fojt 				   params->private_key_passwd) < 0) {
1522*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
1523*a1157835SDaniel Fojt 			   "SSL: Failed to load private key file '%s'",
1524*a1157835SDaniel Fojt 			   params->private_key);
1525*a1157835SDaniel Fojt 		return -1;
1526*a1157835SDaniel Fojt 	}
1527*a1157835SDaniel Fojt 
1528*a1157835SDaniel Fojt 	if (tls_global_dh(tls_ctx, params->dh_file, params->dh_blob,
1529*a1157835SDaniel Fojt 			  params->dh_blob_len) < 0) {
1530*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "SSL: Failed to load DH file '%s'",
1531*a1157835SDaniel Fojt 			   params->dh_file);
1532*a1157835SDaniel Fojt 		return -1;
1533*a1157835SDaniel Fojt 	}
1534*a1157835SDaniel Fojt 
1535*a1157835SDaniel Fojt 	if (params->openssl_ciphers &&
1536*a1157835SDaniel Fojt 	    wolfSSL_CTX_set_cipher_list(tls_ctx,
1537*a1157835SDaniel Fojt 					params->openssl_ciphers) != 1) {
1538*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
1539*a1157835SDaniel Fojt 			   "wolfSSL: Failed to set cipher string '%s'",
1540*a1157835SDaniel Fojt 			   params->openssl_ciphers);
1541*a1157835SDaniel Fojt 		return -1;
1542*a1157835SDaniel Fojt 	}
1543*a1157835SDaniel Fojt 
1544*a1157835SDaniel Fojt 	if (params->openssl_ecdh_curves) {
1545*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO,
1546*a1157835SDaniel Fojt 			   "wolfSSL: openssl_ecdh_curves not supported");
1547*a1157835SDaniel Fojt 		return -1;
1548*a1157835SDaniel Fojt 	}
1549*a1157835SDaniel Fojt 
1550*a1157835SDaniel Fojt #ifdef HAVE_SESSION_TICKET
1551*a1157835SDaniel Fojt 	/* Session ticket is off by default - can't disable once on. */
1552*a1157835SDaniel Fojt 	if (!(params->flags & TLS_CONN_DISABLE_SESSION_TICKET))
1553*a1157835SDaniel Fojt 		wolfSSL_CTX_UseSessionTicket(tls_ctx);
1554*a1157835SDaniel Fojt #endif /* HAVE_SESSION_TICKET */
1555*a1157835SDaniel Fojt 
1556*a1157835SDaniel Fojt #ifdef HAVE_OCSP
1557*a1157835SDaniel Fojt 	if (params->ocsp_stapling_response) {
1558*a1157835SDaniel Fojt 		wolfSSL_CTX_SetOCSP_OverrideURL(tls_ctx,
1559*a1157835SDaniel Fojt 						params->ocsp_stapling_response);
1560*a1157835SDaniel Fojt 		wolfSSL_CTX_SetOCSP_Cb(tls_ctx, ocsp_status_cb,
1561*a1157835SDaniel Fojt 				       ocsp_resp_free_cb, NULL);
1562*a1157835SDaniel Fojt 	}
1563*a1157835SDaniel Fojt #endif /* HAVE_OCSP */
1564*a1157835SDaniel Fojt 
1565*a1157835SDaniel Fojt 	return 0;
1566*a1157835SDaniel Fojt }
1567*a1157835SDaniel Fojt 
1568*a1157835SDaniel Fojt 
tls_global_set_verify(void * tls_ctx,int check_crl,int strict)1569*a1157835SDaniel Fojt int tls_global_set_verify(void *tls_ctx, int check_crl, int strict)
1570*a1157835SDaniel Fojt {
1571*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: global set verify: %d", check_crl);
1572*a1157835SDaniel Fojt 
1573*a1157835SDaniel Fojt 	if (check_crl) {
1574*a1157835SDaniel Fojt 		/* Hack to Enable CRLs. */
1575*a1157835SDaniel Fojt 		wolfSSL_CTX_LoadCRLBuffer(tls_ctx, NULL, 0, SSL_FILETYPE_PEM);
1576*a1157835SDaniel Fojt 	}
1577*a1157835SDaniel Fojt 
1578*a1157835SDaniel Fojt 	return 0;
1579*a1157835SDaniel Fojt }
1580*a1157835SDaniel Fojt 
1581*a1157835SDaniel Fojt 
tls_connection_set_verify(void * ssl_ctx,struct tls_connection * conn,int verify_peer,unsigned int flags,const u8 * session_ctx,size_t session_ctx_len)1582*a1157835SDaniel Fojt int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
1583*a1157835SDaniel Fojt 			      int verify_peer, unsigned int flags,
1584*a1157835SDaniel Fojt 			      const u8 *session_ctx, size_t session_ctx_len)
1585*a1157835SDaniel Fojt {
1586*a1157835SDaniel Fojt 	if (!conn)
1587*a1157835SDaniel Fojt 		return -1;
1588*a1157835SDaniel Fojt 
1589*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: set verify: %d", verify_peer);
1590*a1157835SDaniel Fojt 
1591*a1157835SDaniel Fojt 	if (verify_peer) {
1592*a1157835SDaniel Fojt 		conn->ca_cert_verify = 1;
1593*a1157835SDaniel Fojt 		wolfSSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
1594*a1157835SDaniel Fojt 				   SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1595*a1157835SDaniel Fojt 				   tls_verify_cb);
1596*a1157835SDaniel Fojt 	} else {
1597*a1157835SDaniel Fojt 		conn->ca_cert_verify = 0;
1598*a1157835SDaniel Fojt 		wolfSSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1599*a1157835SDaniel Fojt 	}
1600*a1157835SDaniel Fojt 
1601*a1157835SDaniel Fojt 	wolfSSL_set_accept_state(conn->ssl);
1602*a1157835SDaniel Fojt 
1603*a1157835SDaniel Fojt 	/* TODO: do we need to fake a session like OpenSSL does here? */
1604*a1157835SDaniel Fojt 
1605*a1157835SDaniel Fojt 	return 0;
1606*a1157835SDaniel Fojt }
1607*a1157835SDaniel Fojt 
1608*a1157835SDaniel Fojt 
wolfssl_handshake(struct tls_connection * conn,const struct wpabuf * in_data,int server)1609*a1157835SDaniel Fojt static struct wpabuf * wolfssl_handshake(struct tls_connection *conn,
1610*a1157835SDaniel Fojt 					 const struct wpabuf *in_data,
1611*a1157835SDaniel Fojt 					 int server)
1612*a1157835SDaniel Fojt {
1613*a1157835SDaniel Fojt 	int res;
1614*a1157835SDaniel Fojt 
1615*a1157835SDaniel Fojt 	wolfssl_reset_out_data(&conn->output);
1616*a1157835SDaniel Fojt 
1617*a1157835SDaniel Fojt 	/* Initiate TLS handshake or continue the existing handshake */
1618*a1157835SDaniel Fojt 	if (server) {
1619*a1157835SDaniel Fojt 		wolfSSL_set_accept_state(conn->ssl);
1620*a1157835SDaniel Fojt 		res = wolfSSL_accept(conn->ssl);
1621*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: wolfSSL_accept: %d", res);
1622*a1157835SDaniel Fojt 	} else {
1623*a1157835SDaniel Fojt 		wolfSSL_set_connect_state(conn->ssl);
1624*a1157835SDaniel Fojt 		res = wolfSSL_connect(conn->ssl);
1625*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "SSL: wolfSSL_connect: %d", res);
1626*a1157835SDaniel Fojt 	}
1627*a1157835SDaniel Fojt 
1628*a1157835SDaniel Fojt 	if (res != 1) {
1629*a1157835SDaniel Fojt 		int err = wolfSSL_get_error(conn->ssl, res);
1630*a1157835SDaniel Fojt 
1631*a1157835SDaniel Fojt 		if (err == SSL_ERROR_WANT_READ) {
1632*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1633*a1157835SDaniel Fojt 				   "SSL: wolfSSL_connect - want more data");
1634*a1157835SDaniel Fojt 		} else if (err == SSL_ERROR_WANT_WRITE) {
1635*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1636*a1157835SDaniel Fojt 				   "SSL: wolfSSL_connect - want to write");
1637*a1157835SDaniel Fojt 		} else {
1638*a1157835SDaniel Fojt 			char msg[80];
1639*a1157835SDaniel Fojt 
1640*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1641*a1157835SDaniel Fojt 				   "SSL: wolfSSL_connect - failed %s",
1642*a1157835SDaniel Fojt 				   wolfSSL_ERR_error_string(err, msg));
1643*a1157835SDaniel Fojt 			conn->failed++;
1644*a1157835SDaniel Fojt 		}
1645*a1157835SDaniel Fojt 	}
1646*a1157835SDaniel Fojt 
1647*a1157835SDaniel Fojt 	return conn->output.out_data;
1648*a1157835SDaniel Fojt }
1649*a1157835SDaniel Fojt 
1650*a1157835SDaniel Fojt 
wolfssl_get_appl_data(struct tls_connection * conn,size_t max_len)1651*a1157835SDaniel Fojt static struct wpabuf * wolfssl_get_appl_data(struct tls_connection *conn,
1652*a1157835SDaniel Fojt 					     size_t max_len)
1653*a1157835SDaniel Fojt {
1654*a1157835SDaniel Fojt 	int res;
1655*a1157835SDaniel Fojt 	struct wpabuf *appl_data = wpabuf_alloc(max_len + 100);
1656*a1157835SDaniel Fojt 
1657*a1157835SDaniel Fojt 	if (!appl_data)
1658*a1157835SDaniel Fojt 		return NULL;
1659*a1157835SDaniel Fojt 
1660*a1157835SDaniel Fojt 	res = wolfSSL_read(conn->ssl, wpabuf_mhead(appl_data),
1661*a1157835SDaniel Fojt 			   wpabuf_size(appl_data));
1662*a1157835SDaniel Fojt 	if (res < 0) {
1663*a1157835SDaniel Fojt 		int err = wolfSSL_get_error(conn->ssl, res);
1664*a1157835SDaniel Fojt 
1665*a1157835SDaniel Fojt 		if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
1666*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1667*a1157835SDaniel Fojt 				   "SSL: No Application Data included");
1668*a1157835SDaniel Fojt 		} else {
1669*a1157835SDaniel Fojt 			char msg[80];
1670*a1157835SDaniel Fojt 
1671*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1672*a1157835SDaniel Fojt 				   "Failed to read possible Application Data %s",
1673*a1157835SDaniel Fojt 				   wolfSSL_ERR_error_string(err, msg));
1674*a1157835SDaniel Fojt 		}
1675*a1157835SDaniel Fojt 
1676*a1157835SDaniel Fojt 		wpabuf_free(appl_data);
1677*a1157835SDaniel Fojt 		return NULL;
1678*a1157835SDaniel Fojt 	}
1679*a1157835SDaniel Fojt 
1680*a1157835SDaniel Fojt 	wpabuf_put(appl_data, res);
1681*a1157835SDaniel Fojt 	wpa_hexdump_buf_key(MSG_MSGDUMP,
1682*a1157835SDaniel Fojt 			    "SSL: Application Data in Finished message",
1683*a1157835SDaniel Fojt 			    appl_data);
1684*a1157835SDaniel Fojt 	return appl_data;
1685*a1157835SDaniel Fojt }
1686*a1157835SDaniel Fojt 
1687*a1157835SDaniel Fojt 
1688*a1157835SDaniel Fojt static struct wpabuf *
wolfssl_connection_handshake(struct tls_connection * conn,const struct wpabuf * in_data,struct wpabuf ** appl_data,int server)1689*a1157835SDaniel Fojt wolfssl_connection_handshake(struct tls_connection *conn,
1690*a1157835SDaniel Fojt 			     const struct wpabuf *in_data,
1691*a1157835SDaniel Fojt 			     struct wpabuf **appl_data, int server)
1692*a1157835SDaniel Fojt {
1693*a1157835SDaniel Fojt 	struct wpabuf *out_data;
1694*a1157835SDaniel Fojt 
1695*a1157835SDaniel Fojt 	wolfssl_reset_in_data(&conn->input, in_data);
1696*a1157835SDaniel Fojt 
1697*a1157835SDaniel Fojt 	if (appl_data)
1698*a1157835SDaniel Fojt 		*appl_data = NULL;
1699*a1157835SDaniel Fojt 
1700*a1157835SDaniel Fojt 	out_data = wolfssl_handshake(conn, in_data, server);
1701*a1157835SDaniel Fojt 	if (!out_data)
1702*a1157835SDaniel Fojt 		return NULL;
1703*a1157835SDaniel Fojt 
1704*a1157835SDaniel Fojt 	if (wolfSSL_is_init_finished(conn->ssl)) {
1705*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
1706*a1157835SDaniel Fojt 			   "wolfSSL: Handshake finished - resumed=%d",
1707*a1157835SDaniel Fojt 			   tls_connection_resumed(NULL, conn));
1708*a1157835SDaniel Fojt 		if (appl_data && in_data)
1709*a1157835SDaniel Fojt 			*appl_data = wolfssl_get_appl_data(conn,
1710*a1157835SDaniel Fojt 							   wpabuf_len(in_data));
1711*a1157835SDaniel Fojt 	}
1712*a1157835SDaniel Fojt 
1713*a1157835SDaniel Fojt 	return out_data;
1714*a1157835SDaniel Fojt }
1715*a1157835SDaniel Fojt 
1716*a1157835SDaniel Fojt 
tls_connection_handshake(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data,struct wpabuf ** appl_data)1717*a1157835SDaniel Fojt struct wpabuf * tls_connection_handshake(void *tls_ctx,
1718*a1157835SDaniel Fojt 					 struct tls_connection *conn,
1719*a1157835SDaniel Fojt 					 const struct wpabuf *in_data,
1720*a1157835SDaniel Fojt 					 struct wpabuf **appl_data)
1721*a1157835SDaniel Fojt {
1722*a1157835SDaniel Fojt 	return wolfssl_connection_handshake(conn, in_data, appl_data, 0);
1723*a1157835SDaniel Fojt }
1724*a1157835SDaniel Fojt 
1725*a1157835SDaniel Fojt 
tls_connection_server_handshake(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data,struct wpabuf ** appl_data)1726*a1157835SDaniel Fojt struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
1727*a1157835SDaniel Fojt 						struct tls_connection *conn,
1728*a1157835SDaniel Fojt 						const struct wpabuf *in_data,
1729*a1157835SDaniel Fojt 						struct wpabuf **appl_data)
1730*a1157835SDaniel Fojt {
1731*a1157835SDaniel Fojt 	return wolfssl_connection_handshake(conn, in_data, appl_data, 1);
1732*a1157835SDaniel Fojt }
1733*a1157835SDaniel Fojt 
1734*a1157835SDaniel Fojt 
tls_connection_encrypt(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data)1735*a1157835SDaniel Fojt struct wpabuf * tls_connection_encrypt(void *tls_ctx,
1736*a1157835SDaniel Fojt 				       struct tls_connection *conn,
1737*a1157835SDaniel Fojt 				       const struct wpabuf *in_data)
1738*a1157835SDaniel Fojt {
1739*a1157835SDaniel Fojt 	int res;
1740*a1157835SDaniel Fojt 
1741*a1157835SDaniel Fojt 	if (!conn)
1742*a1157835SDaniel Fojt 		return NULL;
1743*a1157835SDaniel Fojt 
1744*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: encrypt: %ld bytes", wpabuf_len(in_data));
1745*a1157835SDaniel Fojt 
1746*a1157835SDaniel Fojt 	wolfssl_reset_out_data(&conn->output);
1747*a1157835SDaniel Fojt 
1748*a1157835SDaniel Fojt 	res = wolfSSL_write(conn->ssl, wpabuf_head(in_data),
1749*a1157835SDaniel Fojt 			    wpabuf_len(in_data));
1750*a1157835SDaniel Fojt 	if (res < 0) {
1751*a1157835SDaniel Fojt 		int  err = wolfSSL_get_error(conn->ssl, res);
1752*a1157835SDaniel Fojt 		char msg[80];
1753*a1157835SDaniel Fojt 
1754*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Encryption failed - SSL_write: %s",
1755*a1157835SDaniel Fojt 			   wolfSSL_ERR_error_string(err, msg));
1756*a1157835SDaniel Fojt 		return NULL;
1757*a1157835SDaniel Fojt 	}
1758*a1157835SDaniel Fojt 
1759*a1157835SDaniel Fojt 	return conn->output.out_data;
1760*a1157835SDaniel Fojt }
1761*a1157835SDaniel Fojt 
1762*a1157835SDaniel Fojt 
tls_connection_decrypt(void * tls_ctx,struct tls_connection * conn,const struct wpabuf * in_data)1763*a1157835SDaniel Fojt struct wpabuf * tls_connection_decrypt(void *tls_ctx,
1764*a1157835SDaniel Fojt 				       struct tls_connection *conn,
1765*a1157835SDaniel Fojt 				       const struct wpabuf *in_data)
1766*a1157835SDaniel Fojt {
1767*a1157835SDaniel Fojt 	int res;
1768*a1157835SDaniel Fojt 	struct wpabuf *buf;
1769*a1157835SDaniel Fojt 
1770*a1157835SDaniel Fojt 	if (!conn)
1771*a1157835SDaniel Fojt 		return NULL;
1772*a1157835SDaniel Fojt 
1773*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: decrypt");
1774*a1157835SDaniel Fojt 
1775*a1157835SDaniel Fojt 	wolfssl_reset_in_data(&conn->input, in_data);
1776*a1157835SDaniel Fojt 
1777*a1157835SDaniel Fojt 	/* Read decrypted data for further processing */
1778*a1157835SDaniel Fojt 	/*
1779*a1157835SDaniel Fojt 	 * Even though we try to disable TLS compression, it is possible that
1780*a1157835SDaniel Fojt 	 * this cannot be done with all TLS libraries. Add extra buffer space
1781*a1157835SDaniel Fojt 	 * to handle the possibility of the decrypted data being longer than
1782*a1157835SDaniel Fojt 	 * input data.
1783*a1157835SDaniel Fojt 	 */
1784*a1157835SDaniel Fojt 	buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3);
1785*a1157835SDaniel Fojt 	if (!buf)
1786*a1157835SDaniel Fojt 		return NULL;
1787*a1157835SDaniel Fojt 	res = wolfSSL_read(conn->ssl, wpabuf_mhead(buf), wpabuf_size(buf));
1788*a1157835SDaniel Fojt 	if (res < 0) {
1789*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Decryption failed - SSL_read");
1790*a1157835SDaniel Fojt 		wpabuf_free(buf);
1791*a1157835SDaniel Fojt 		return NULL;
1792*a1157835SDaniel Fojt 	}
1793*a1157835SDaniel Fojt 	wpabuf_put(buf, res);
1794*a1157835SDaniel Fojt 
1795*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "SSL: decrypt: %ld bytes", wpabuf_len(buf));
1796*a1157835SDaniel Fojt 
1797*a1157835SDaniel Fojt 	return buf;
1798*a1157835SDaniel Fojt }
1799*a1157835SDaniel Fojt 
1800*a1157835SDaniel Fojt 
tls_connection_resumed(void * tls_ctx,struct tls_connection * conn)1801*a1157835SDaniel Fojt int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn)
1802*a1157835SDaniel Fojt {
1803*a1157835SDaniel Fojt 	return conn ? wolfSSL_session_reused(conn->ssl) : 0;
1804*a1157835SDaniel Fojt }
1805*a1157835SDaniel Fojt 
1806*a1157835SDaniel Fojt 
tls_connection_set_cipher_list(void * tls_ctx,struct tls_connection * conn,u8 * ciphers)1807*a1157835SDaniel Fojt int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
1808*a1157835SDaniel Fojt 				   u8 *ciphers)
1809*a1157835SDaniel Fojt {
1810*a1157835SDaniel Fojt 	char buf[128], *pos, *end;
1811*a1157835SDaniel Fojt 	u8 *c;
1812*a1157835SDaniel Fojt 	int ret;
1813*a1157835SDaniel Fojt 
1814*a1157835SDaniel Fojt 	if (!conn || !conn->ssl || !ciphers)
1815*a1157835SDaniel Fojt 		return -1;
1816*a1157835SDaniel Fojt 
1817*a1157835SDaniel Fojt 	buf[0] = '\0';
1818*a1157835SDaniel Fojt 	pos = buf;
1819*a1157835SDaniel Fojt 	end = pos + sizeof(buf);
1820*a1157835SDaniel Fojt 
1821*a1157835SDaniel Fojt 	c = ciphers;
1822*a1157835SDaniel Fojt 	while (*c != TLS_CIPHER_NONE) {
1823*a1157835SDaniel Fojt 		const char *suite;
1824*a1157835SDaniel Fojt 
1825*a1157835SDaniel Fojt 		switch (*c) {
1826*a1157835SDaniel Fojt 		case TLS_CIPHER_RC4_SHA:
1827*a1157835SDaniel Fojt 			suite = "RC4-SHA";
1828*a1157835SDaniel Fojt 			break;
1829*a1157835SDaniel Fojt 		case TLS_CIPHER_AES128_SHA:
1830*a1157835SDaniel Fojt 			suite = "AES128-SHA";
1831*a1157835SDaniel Fojt 			break;
1832*a1157835SDaniel Fojt 		case TLS_CIPHER_RSA_DHE_AES128_SHA:
1833*a1157835SDaniel Fojt 			suite = "DHE-RSA-AES128-SHA";
1834*a1157835SDaniel Fojt 			break;
1835*a1157835SDaniel Fojt 		case TLS_CIPHER_ANON_DH_AES128_SHA:
1836*a1157835SDaniel Fojt 			suite = "ADH-AES128-SHA";
1837*a1157835SDaniel Fojt 			break;
1838*a1157835SDaniel Fojt 		case TLS_CIPHER_RSA_DHE_AES256_SHA:
1839*a1157835SDaniel Fojt 			suite = "DHE-RSA-AES256-SHA";
1840*a1157835SDaniel Fojt 			break;
1841*a1157835SDaniel Fojt 		case TLS_CIPHER_AES256_SHA:
1842*a1157835SDaniel Fojt 			suite = "AES256-SHA";
1843*a1157835SDaniel Fojt 			break;
1844*a1157835SDaniel Fojt 		default:
1845*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG,
1846*a1157835SDaniel Fojt 				   "TLS: Unsupported cipher selection: %d", *c);
1847*a1157835SDaniel Fojt 			return -1;
1848*a1157835SDaniel Fojt 		}
1849*a1157835SDaniel Fojt 		ret = os_snprintf(pos, end - pos, ":%s", suite);
1850*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
1851*a1157835SDaniel Fojt 			break;
1852*a1157835SDaniel Fojt 		pos += ret;
1853*a1157835SDaniel Fojt 
1854*a1157835SDaniel Fojt 		c++;
1855*a1157835SDaniel Fojt 	}
1856*a1157835SDaniel Fojt 
1857*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "wolfSSL: cipher suites: %s", buf + 1);
1858*a1157835SDaniel Fojt 
1859*a1157835SDaniel Fojt 	if (wolfSSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
1860*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "Cipher suite configuration failed");
1861*a1157835SDaniel Fojt 		return -1;
1862*a1157835SDaniel Fojt 	}
1863*a1157835SDaniel Fojt 
1864*a1157835SDaniel Fojt 	return 0;
1865*a1157835SDaniel Fojt }
1866*a1157835SDaniel Fojt 
1867*a1157835SDaniel Fojt 
tls_get_cipher(void * tls_ctx,struct tls_connection * conn,char * buf,size_t buflen)1868*a1157835SDaniel Fojt int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
1869*a1157835SDaniel Fojt 		   char *buf, size_t buflen)
1870*a1157835SDaniel Fojt {
1871*a1157835SDaniel Fojt 	WOLFSSL_CIPHER *cipher;
1872*a1157835SDaniel Fojt 	const char *name;
1873*a1157835SDaniel Fojt 
1874*a1157835SDaniel Fojt 	if (!conn || !conn->ssl)
1875*a1157835SDaniel Fojt 		return -1;
1876*a1157835SDaniel Fojt 
1877*a1157835SDaniel Fojt 	cipher = wolfSSL_get_current_cipher(conn->ssl);
1878*a1157835SDaniel Fojt 	if (!cipher)
1879*a1157835SDaniel Fojt 		return -1;
1880*a1157835SDaniel Fojt 
1881*a1157835SDaniel Fojt 	name = wolfSSL_CIPHER_get_name(cipher);
1882*a1157835SDaniel Fojt 	if (!name)
1883*a1157835SDaniel Fojt 		return -1;
1884*a1157835SDaniel Fojt 
1885*a1157835SDaniel Fojt 	if (os_strcmp(name, "SSL_RSA_WITH_RC4_128_SHA") == 0)
1886*a1157835SDaniel Fojt 		os_strlcpy(buf, "RC4-SHA", buflen);
1887*a1157835SDaniel Fojt 	else if (os_strcmp(name, "TLS_RSA_WITH_AES_128_CBC_SHA") == 0)
1888*a1157835SDaniel Fojt 		os_strlcpy(buf, "AES128-SHA", buflen);
1889*a1157835SDaniel Fojt 	else if (os_strcmp(name, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA") == 0)
1890*a1157835SDaniel Fojt 		os_strlcpy(buf, "DHE-RSA-AES128-SHA", buflen);
1891*a1157835SDaniel Fojt 	else if (os_strcmp(name, "TLS_DH_anon_WITH_AES_128_CBC_SHA") == 0)
1892*a1157835SDaniel Fojt 		os_strlcpy(buf, "ADH-AES128-SHA", buflen);
1893*a1157835SDaniel Fojt 	else if (os_strcmp(name, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA") == 0)
1894*a1157835SDaniel Fojt 		os_strlcpy(buf, "DHE-RSA-AES256-SHA", buflen);
1895*a1157835SDaniel Fojt 	else if (os_strcmp(name, "TLS_RSA_WITH_AES_256_CBC_SHA") == 0)
1896*a1157835SDaniel Fojt 		os_strlcpy(buf, "AES256-SHA", buflen);
1897*a1157835SDaniel Fojt 	else
1898*a1157835SDaniel Fojt 		os_strlcpy(buf, name, buflen);
1899*a1157835SDaniel Fojt 
1900*a1157835SDaniel Fojt 	return 0;
1901*a1157835SDaniel Fojt }
1902*a1157835SDaniel Fojt 
1903*a1157835SDaniel Fojt 
tls_connection_enable_workaround(void * tls_ctx,struct tls_connection * conn)1904*a1157835SDaniel Fojt int tls_connection_enable_workaround(void *tls_ctx,
1905*a1157835SDaniel Fojt 				     struct tls_connection *conn)
1906*a1157835SDaniel Fojt {
1907*a1157835SDaniel Fojt 	/* no empty fragments in wolfSSL for now */
1908*a1157835SDaniel Fojt 	return 0;
1909*a1157835SDaniel Fojt }
1910*a1157835SDaniel Fojt 
1911*a1157835SDaniel Fojt 
tls_connection_get_failed(void * tls_ctx,struct tls_connection * conn)1912*a1157835SDaniel Fojt int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn)
1913*a1157835SDaniel Fojt {
1914*a1157835SDaniel Fojt 	if (!conn)
1915*a1157835SDaniel Fojt 		return -1;
1916*a1157835SDaniel Fojt 
1917*a1157835SDaniel Fojt 	return conn->failed;
1918*a1157835SDaniel Fojt }
1919*a1157835SDaniel Fojt 
1920*a1157835SDaniel Fojt 
tls_connection_get_read_alerts(void * tls_ctx,struct tls_connection * conn)1921*a1157835SDaniel Fojt int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn)
1922*a1157835SDaniel Fojt {
1923*a1157835SDaniel Fojt 	if (!conn)
1924*a1157835SDaniel Fojt 		return -1;
1925*a1157835SDaniel Fojt 
1926*a1157835SDaniel Fojt 	/* TODO: this is not incremented anywhere */
1927*a1157835SDaniel Fojt 	return conn->read_alerts;
1928*a1157835SDaniel Fojt }
1929*a1157835SDaniel Fojt 
1930*a1157835SDaniel Fojt 
tls_connection_get_write_alerts(void * tls_ctx,struct tls_connection * conn)1931*a1157835SDaniel Fojt int tls_connection_get_write_alerts(void *tls_ctx,
1932*a1157835SDaniel Fojt 				    struct tls_connection *conn)
1933*a1157835SDaniel Fojt {
1934*a1157835SDaniel Fojt 	if (!conn)
1935*a1157835SDaniel Fojt 		return -1;
1936*a1157835SDaniel Fojt 
1937*a1157835SDaniel Fojt 	/* TODO: this is not incremented anywhere */
1938*a1157835SDaniel Fojt 	return conn->write_alerts;
1939*a1157835SDaniel Fojt }
1940*a1157835SDaniel Fojt 
1941*a1157835SDaniel Fojt 
1942*a1157835SDaniel Fojt 
tls_get_library_version(char * buf,size_t buf_len)1943*a1157835SDaniel Fojt int tls_get_library_version(char *buf, size_t buf_len)
1944*a1157835SDaniel Fojt {
1945*a1157835SDaniel Fojt 	return os_snprintf(buf, buf_len, "wolfSSL build=%s run=%s",
1946*a1157835SDaniel Fojt 			   WOLFSSL_VERSION, wolfSSL_lib_version());
1947*a1157835SDaniel Fojt }
1948*a1157835SDaniel Fojt 
tls_get_version(void * ssl_ctx,struct tls_connection * conn,char * buf,size_t buflen)1949*a1157835SDaniel Fojt int tls_get_version(void *ssl_ctx, struct tls_connection *conn,
1950*a1157835SDaniel Fojt 		    char *buf, size_t buflen)
1951*a1157835SDaniel Fojt {
1952*a1157835SDaniel Fojt 	const char *name;
1953*a1157835SDaniel Fojt 
1954*a1157835SDaniel Fojt 	if (!conn || !conn->ssl)
1955*a1157835SDaniel Fojt 		return -1;
1956*a1157835SDaniel Fojt 
1957*a1157835SDaniel Fojt 	name = wolfSSL_get_version(conn->ssl);
1958*a1157835SDaniel Fojt 	if (!name)
1959*a1157835SDaniel Fojt 		return -1;
1960*a1157835SDaniel Fojt 
1961*a1157835SDaniel Fojt 	os_strlcpy(buf, name, buflen);
1962*a1157835SDaniel Fojt 	return 0;
1963*a1157835SDaniel Fojt }
1964*a1157835SDaniel Fojt 
1965*a1157835SDaniel Fojt 
tls_connection_get_random(void * ssl_ctx,struct tls_connection * conn,struct tls_random * keys)1966*a1157835SDaniel Fojt int tls_connection_get_random(void *ssl_ctx, struct tls_connection *conn,
1967*a1157835SDaniel Fojt 			      struct tls_random *keys)
1968*a1157835SDaniel Fojt {
1969*a1157835SDaniel Fojt 	WOLFSSL *ssl;
1970*a1157835SDaniel Fojt 
1971*a1157835SDaniel Fojt 	if (!conn || !keys)
1972*a1157835SDaniel Fojt 		return -1;
1973*a1157835SDaniel Fojt 	ssl = conn->ssl;
1974*a1157835SDaniel Fojt 	if (!ssl)
1975*a1157835SDaniel Fojt 		return -1;
1976*a1157835SDaniel Fojt 
1977*a1157835SDaniel Fojt 	os_memset(keys, 0, sizeof(*keys));
1978*a1157835SDaniel Fojt 	keys->client_random = conn->client_random;
1979*a1157835SDaniel Fojt 	keys->client_random_len = wolfSSL_get_client_random(
1980*a1157835SDaniel Fojt 		ssl, conn->client_random, sizeof(conn->client_random));
1981*a1157835SDaniel Fojt 	keys->server_random = conn->server_random;
1982*a1157835SDaniel Fojt 	keys->server_random_len = wolfSSL_get_server_random(
1983*a1157835SDaniel Fojt 		ssl, conn->server_random, sizeof(conn->server_random));
1984*a1157835SDaniel Fojt 
1985*a1157835SDaniel Fojt 	return 0;
1986*a1157835SDaniel Fojt }
1987*a1157835SDaniel Fojt 
1988*a1157835SDaniel Fojt 
tls_connection_export_key(void * tls_ctx,struct tls_connection * conn,const char * label,const u8 * context,size_t context_len,u8 * out,size_t out_len)1989*a1157835SDaniel Fojt int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn,
1990*a1157835SDaniel Fojt 			      const char *label, const u8 *context,
1991*a1157835SDaniel Fojt 			      size_t context_len, u8 *out, size_t out_len)
1992*a1157835SDaniel Fojt {
1993*a1157835SDaniel Fojt 	if (context)
1994*a1157835SDaniel Fojt 		return -1;
1995*a1157835SDaniel Fojt 	if (!conn || wolfSSL_make_eap_keys(conn->ssl, out, out_len, label) != 0)
1996*a1157835SDaniel Fojt 		return -1;
1997*a1157835SDaniel Fojt 	return 0;
1998*a1157835SDaniel Fojt }
1999*a1157835SDaniel Fojt 
2000*a1157835SDaniel Fojt 
2001*a1157835SDaniel Fojt #define SEED_LEN	(RAN_LEN + RAN_LEN)
2002*a1157835SDaniel Fojt 
tls_connection_get_eap_fast_key(void * tls_ctx,struct tls_connection * conn,u8 * out,size_t out_len)2003*a1157835SDaniel Fojt int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn,
2004*a1157835SDaniel Fojt 				    u8 *out, size_t out_len)
2005*a1157835SDaniel Fojt {
2006*a1157835SDaniel Fojt 	byte seed[SEED_LEN];
2007*a1157835SDaniel Fojt 	int ret = -1;
2008*a1157835SDaniel Fojt 	WOLFSSL *ssl;
2009*a1157835SDaniel Fojt 	byte *tmp_out;
2010*a1157835SDaniel Fojt 	byte *_out;
2011*a1157835SDaniel Fojt 	int skip = 0;
2012*a1157835SDaniel Fojt 	byte *master_key;
2013*a1157835SDaniel Fojt 	unsigned int master_key_len;
2014*a1157835SDaniel Fojt 	byte *server_random;
2015*a1157835SDaniel Fojt 	unsigned int server_len;
2016*a1157835SDaniel Fojt 	byte *client_random;
2017*a1157835SDaniel Fojt 	unsigned int client_len;
2018*a1157835SDaniel Fojt 
2019*a1157835SDaniel Fojt 	if (!conn || !conn->ssl)
2020*a1157835SDaniel Fojt 		return -1;
2021*a1157835SDaniel Fojt 	ssl = conn->ssl;
2022*a1157835SDaniel Fojt 
2023*a1157835SDaniel Fojt 	skip = 2 * (wolfSSL_GetKeySize(ssl) + wolfSSL_GetHmacSize(ssl) +
2024*a1157835SDaniel Fojt 		    wolfSSL_GetIVSize(ssl));
2025*a1157835SDaniel Fojt 
2026*a1157835SDaniel Fojt 	tmp_out = os_malloc(skip + out_len);
2027*a1157835SDaniel Fojt 	if (!tmp_out)
2028*a1157835SDaniel Fojt 		return -1;
2029*a1157835SDaniel Fojt 	_out = tmp_out;
2030*a1157835SDaniel Fojt 
2031*a1157835SDaniel Fojt 	wolfSSL_get_keys(ssl, &master_key, &master_key_len, &server_random,
2032*a1157835SDaniel Fojt 			 &server_len, &client_random, &client_len);
2033*a1157835SDaniel Fojt 	os_memcpy(seed, server_random, RAN_LEN);
2034*a1157835SDaniel Fojt 	os_memcpy(seed + RAN_LEN, client_random, RAN_LEN);
2035*a1157835SDaniel Fojt 
2036*a1157835SDaniel Fojt 	if (wolfSSL_GetVersion(ssl) == WOLFSSL_TLSV1_2) {
2037*a1157835SDaniel Fojt 		tls_prf_sha256(master_key, master_key_len,
2038*a1157835SDaniel Fojt 			       "key expansion", seed, sizeof(seed),
2039*a1157835SDaniel Fojt 			       _out, skip + out_len);
2040*a1157835SDaniel Fojt 		ret = 0;
2041*a1157835SDaniel Fojt 	} else {
2042*a1157835SDaniel Fojt 		ret = tls_prf_sha1_md5(master_key, master_key_len,
2043*a1157835SDaniel Fojt 				       "key expansion", seed, sizeof(seed),
2044*a1157835SDaniel Fojt 				       _out, skip + out_len);
2045*a1157835SDaniel Fojt 	}
2046*a1157835SDaniel Fojt 
2047*a1157835SDaniel Fojt 	forced_memzero(master_key, master_key_len);
2048*a1157835SDaniel Fojt 	if (ret == 0)
2049*a1157835SDaniel Fojt 		os_memcpy(out, _out + skip, out_len);
2050*a1157835SDaniel Fojt 	bin_clear_free(tmp_out, skip + out_len);
2051*a1157835SDaniel Fojt 
2052*a1157835SDaniel Fojt 	return ret;
2053*a1157835SDaniel Fojt }
2054*a1157835SDaniel Fojt 
2055*a1157835SDaniel Fojt 
2056*a1157835SDaniel Fojt #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2057*a1157835SDaniel Fojt 
tls_connection_client_hello_ext(void * ssl_ctx,struct tls_connection * conn,int ext_type,const u8 * data,size_t data_len)2058*a1157835SDaniel Fojt int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
2059*a1157835SDaniel Fojt 				    int ext_type, const u8 *data,
2060*a1157835SDaniel Fojt 				    size_t data_len)
2061*a1157835SDaniel Fojt {
2062*a1157835SDaniel Fojt 	(void) ssl_ctx;
2063*a1157835SDaniel Fojt 
2064*a1157835SDaniel Fojt 	if (!conn || !conn->ssl || ext_type != 35)
2065*a1157835SDaniel Fojt 		return -1;
2066*a1157835SDaniel Fojt 
2067*a1157835SDaniel Fojt 	if (wolfSSL_set_SessionTicket(conn->ssl, data,
2068*a1157835SDaniel Fojt 				      (unsigned int) data_len) != 1)
2069*a1157835SDaniel Fojt 		return -1;
2070*a1157835SDaniel Fojt 
2071*a1157835SDaniel Fojt 	return 0;
2072*a1157835SDaniel Fojt }
2073*a1157835SDaniel Fojt 
2074*a1157835SDaniel Fojt 
tls_sess_sec_cb(WOLFSSL * s,void * secret,int * secret_len,void * arg)2075*a1157835SDaniel Fojt static int tls_sess_sec_cb(WOLFSSL *s, void *secret, int *secret_len, void *arg)
2076*a1157835SDaniel Fojt {
2077*a1157835SDaniel Fojt 	struct tls_connection *conn = arg;
2078*a1157835SDaniel Fojt 	int ret;
2079*a1157835SDaniel Fojt 	unsigned char client_random[RAN_LEN];
2080*a1157835SDaniel Fojt 	unsigned char server_random[RAN_LEN];
2081*a1157835SDaniel Fojt 	word32 ticket_len = sizeof(conn->session_ticket);
2082*a1157835SDaniel Fojt 
2083*a1157835SDaniel Fojt 	if (!conn || !conn->session_ticket_cb)
2084*a1157835SDaniel Fojt 		return 1;
2085*a1157835SDaniel Fojt 
2086*a1157835SDaniel Fojt 	if (wolfSSL_get_client_random(s, client_random,
2087*a1157835SDaniel Fojt 				      sizeof(client_random)) == 0 ||
2088*a1157835SDaniel Fojt 	    wolfSSL_get_server_random(s, server_random,
2089*a1157835SDaniel Fojt 				      sizeof(server_random)) == 0 ||
2090*a1157835SDaniel Fojt 	    wolfSSL_get_SessionTicket(s, conn->session_ticket,
2091*a1157835SDaniel Fojt 				      &ticket_len) != 1)
2092*a1157835SDaniel Fojt 		return 1;
2093*a1157835SDaniel Fojt 
2094*a1157835SDaniel Fojt 	if (ticket_len == 0)
2095*a1157835SDaniel Fojt 		return 0;
2096*a1157835SDaniel Fojt 
2097*a1157835SDaniel Fojt 	ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
2098*a1157835SDaniel Fojt 				      conn->session_ticket, ticket_len,
2099*a1157835SDaniel Fojt 				      client_random, server_random, secret);
2100*a1157835SDaniel Fojt 	if (ret <= 0)
2101*a1157835SDaniel Fojt 		return 1;
2102*a1157835SDaniel Fojt 
2103*a1157835SDaniel Fojt 	*secret_len = SECRET_LEN;
2104*a1157835SDaniel Fojt 	return 0;
2105*a1157835SDaniel Fojt }
2106*a1157835SDaniel Fojt 
2107*a1157835SDaniel Fojt #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2108*a1157835SDaniel Fojt 
2109*a1157835SDaniel Fojt 
tls_connection_set_session_ticket_cb(void * tls_ctx,struct tls_connection * conn,tls_session_ticket_cb cb,void * ctx)2110*a1157835SDaniel Fojt int tls_connection_set_session_ticket_cb(void *tls_ctx,
2111*a1157835SDaniel Fojt 					 struct tls_connection *conn,
2112*a1157835SDaniel Fojt 					 tls_session_ticket_cb cb,
2113*a1157835SDaniel Fojt 					 void *ctx)
2114*a1157835SDaniel Fojt {
2115*a1157835SDaniel Fojt #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2116*a1157835SDaniel Fojt 	conn->session_ticket_cb = cb;
2117*a1157835SDaniel Fojt 	conn->session_ticket_cb_ctx = ctx;
2118*a1157835SDaniel Fojt 
2119*a1157835SDaniel Fojt 	if (cb) {
2120*a1157835SDaniel Fojt 		if (wolfSSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
2121*a1157835SDaniel Fojt 						  conn) != 1)
2122*a1157835SDaniel Fojt 			return -1;
2123*a1157835SDaniel Fojt 	} else {
2124*a1157835SDaniel Fojt 		if (wolfSSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
2125*a1157835SDaniel Fojt 			return -1;
2126*a1157835SDaniel Fojt 	}
2127*a1157835SDaniel Fojt 
2128*a1157835SDaniel Fojt 	return 0;
2129*a1157835SDaniel Fojt #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2130*a1157835SDaniel Fojt 	return -1;
2131*a1157835SDaniel Fojt #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2132*a1157835SDaniel Fojt }
2133*a1157835SDaniel Fojt 
2134*a1157835SDaniel Fojt 
tls_connection_set_success_data_resumed(struct tls_connection * conn)2135*a1157835SDaniel Fojt void tls_connection_set_success_data_resumed(struct tls_connection *conn)
2136*a1157835SDaniel Fojt {
2137*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
2138*a1157835SDaniel Fojt 		   "wolfSSL: Success data accepted for resumed session");
2139*a1157835SDaniel Fojt }
2140*a1157835SDaniel Fojt 
2141*a1157835SDaniel Fojt 
tls_connection_remove_session(struct tls_connection * conn)2142*a1157835SDaniel Fojt void tls_connection_remove_session(struct tls_connection *conn)
2143*a1157835SDaniel Fojt {
2144*a1157835SDaniel Fojt 	WOLFSSL_SESSION *sess;
2145*a1157835SDaniel Fojt 
2146*a1157835SDaniel Fojt 	sess = wolfSSL_get_session(conn->ssl);
2147*a1157835SDaniel Fojt 	if (!sess)
2148*a1157835SDaniel Fojt 		return;
2149*a1157835SDaniel Fojt 
2150*a1157835SDaniel Fojt 	wolfSSL_SSL_SESSION_set_timeout(sess, 0);
2151*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
2152*a1157835SDaniel Fojt 		   "wolfSSL: Removed cached session to disable session resumption");
2153*a1157835SDaniel Fojt }
2154*a1157835SDaniel Fojt 
2155*a1157835SDaniel Fojt 
tls_connection_set_success_data(struct tls_connection * conn,struct wpabuf * data)2156*a1157835SDaniel Fojt void tls_connection_set_success_data(struct tls_connection *conn,
2157*a1157835SDaniel Fojt 				     struct wpabuf *data)
2158*a1157835SDaniel Fojt {
2159*a1157835SDaniel Fojt 	WOLFSSL_SESSION *sess;
2160*a1157835SDaniel Fojt 	struct wpabuf *old;
2161*a1157835SDaniel Fojt 
2162*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "wolfSSL: Set success data");
2163*a1157835SDaniel Fojt 
2164*a1157835SDaniel Fojt 	sess = wolfSSL_get_session(conn->ssl);
2165*a1157835SDaniel Fojt 	if (!sess) {
2166*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
2167*a1157835SDaniel Fojt 			   "wolfSSL: No session found for success data");
2168*a1157835SDaniel Fojt 		goto fail;
2169*a1157835SDaniel Fojt 	}
2170*a1157835SDaniel Fojt 
2171*a1157835SDaniel Fojt 	old = wolfSSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
2172*a1157835SDaniel Fojt 	if (old) {
2173*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "wolfSSL: Replacing old success data %p",
2174*a1157835SDaniel Fojt 			   old);
2175*a1157835SDaniel Fojt 		wpabuf_free(old);
2176*a1157835SDaniel Fojt 	}
2177*a1157835SDaniel Fojt 	if (wolfSSL_SESSION_set_ex_data(sess, tls_ex_idx_session, data) != 1)
2178*a1157835SDaniel Fojt 		goto fail;
2179*a1157835SDaniel Fojt 
2180*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "wolfSSL: Stored success data %p", data);
2181*a1157835SDaniel Fojt 	conn->success_data = 1;
2182*a1157835SDaniel Fojt 	return;
2183*a1157835SDaniel Fojt 
2184*a1157835SDaniel Fojt fail:
2185*a1157835SDaniel Fojt 	wpa_printf(MSG_INFO, "wolfSSL: Failed to store success data");
2186*a1157835SDaniel Fojt 	wpabuf_free(data);
2187*a1157835SDaniel Fojt }
2188*a1157835SDaniel Fojt 
2189*a1157835SDaniel Fojt 
2190*a1157835SDaniel Fojt const struct wpabuf *
tls_connection_get_success_data(struct tls_connection * conn)2191*a1157835SDaniel Fojt tls_connection_get_success_data(struct tls_connection *conn)
2192*a1157835SDaniel Fojt {
2193*a1157835SDaniel Fojt 	WOLFSSL_SESSION *sess;
2194*a1157835SDaniel Fojt 
2195*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "wolfSSL: Get success data");
2196*a1157835SDaniel Fojt 
2197*a1157835SDaniel Fojt 	sess = wolfSSL_get_session(conn->ssl);
2198*a1157835SDaniel Fojt 	if (!sess)
2199*a1157835SDaniel Fojt 		return NULL;
2200*a1157835SDaniel Fojt 	return wolfSSL_SESSION_get_ex_data(sess, tls_ex_idx_session);
2201*a1157835SDaniel Fojt }
2202