/src/hostap/src/crypto/tls_internal.c
Line | Count | Source |
1 | | /* |
2 | | * TLS interface functions and an internal TLS implementation |
3 | | * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi> |
4 | | * |
5 | | * This software may be distributed under the terms of the BSD license. |
6 | | * See README for more details. |
7 | | * |
8 | | * This file interface functions for hostapd/wpa_supplicant to use the |
9 | | * integrated TLSv1 implementation. |
10 | | */ |
11 | | |
12 | | #include "includes.h" |
13 | | |
14 | | #include "common.h" |
15 | | #include "tls.h" |
16 | | #include "tls/tlsv1_client.h" |
17 | | #include "tls/tlsv1_server.h" |
18 | | |
19 | | |
20 | | static int tls_ref_count = 0; |
21 | | |
22 | | struct tls_global { |
23 | | int server; |
24 | | struct tlsv1_credentials *server_cred; |
25 | | int check_crl; |
26 | | |
27 | | void (*event_cb)(void *ctx, enum tls_event ev, |
28 | | union tls_event_data *data); |
29 | | void *cb_ctx; |
30 | | int cert_in_cb; |
31 | | }; |
32 | | |
33 | | struct tls_connection { |
34 | | struct tlsv1_client *client; |
35 | | struct tlsv1_server *server; |
36 | | struct tls_global *global; |
37 | | }; |
38 | | |
39 | | |
40 | | void * tls_init(const struct tls_config *conf) |
41 | 0 | { |
42 | 0 | struct tls_global *global; |
43 | |
|
44 | 0 | if (tls_ref_count == 0) { |
45 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
46 | 0 | if (tlsv1_client_global_init()) |
47 | 0 | return NULL; |
48 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
49 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
50 | 0 | if (tlsv1_server_global_init()) |
51 | 0 | return NULL; |
52 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
53 | 0 | } |
54 | 0 | tls_ref_count++; |
55 | |
|
56 | 0 | global = os_zalloc(sizeof(*global)); |
57 | 0 | if (global == NULL) |
58 | 0 | return NULL; |
59 | 0 | if (conf) { |
60 | 0 | global->event_cb = conf->event_cb; |
61 | 0 | global->cb_ctx = conf->cb_ctx; |
62 | 0 | global->cert_in_cb = conf->cert_in_cb; |
63 | 0 | } |
64 | |
|
65 | 0 | return global; |
66 | 0 | } |
67 | | |
68 | | void tls_deinit(void *ssl_ctx) |
69 | 0 | { |
70 | 0 | struct tls_global *global = ssl_ctx; |
71 | 0 | tls_ref_count--; |
72 | 0 | if (tls_ref_count == 0) { |
73 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
74 | 0 | tlsv1_client_global_deinit(); |
75 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
76 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
77 | 0 | tlsv1_server_global_deinit(); |
78 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
79 | 0 | } |
80 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
81 | 0 | tlsv1_cred_free(global->server_cred); |
82 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
83 | 0 | os_free(global); |
84 | 0 | } |
85 | | |
86 | | |
87 | | int tls_get_errors(void *tls_ctx) |
88 | 0 | { |
89 | 0 | return 0; |
90 | 0 | } |
91 | | |
92 | | |
93 | | struct tls_connection * tls_connection_init(void *tls_ctx) |
94 | 0 | { |
95 | 0 | struct tls_connection *conn; |
96 | 0 | struct tls_global *global = tls_ctx; |
97 | |
|
98 | 0 | conn = os_zalloc(sizeof(*conn)); |
99 | 0 | if (conn == NULL) |
100 | 0 | return NULL; |
101 | 0 | conn->global = global; |
102 | |
|
103 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
104 | 0 | if (!global->server) { |
105 | 0 | conn->client = tlsv1_client_init(); |
106 | 0 | if (conn->client == NULL) { |
107 | 0 | os_free(conn); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | 0 | tlsv1_client_set_cb(conn->client, global->event_cb, |
111 | 0 | global->cb_ctx, global->cert_in_cb); |
112 | 0 | } |
113 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
114 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
115 | 0 | if (global->server) { |
116 | 0 | conn->server = tlsv1_server_init(global->server_cred); |
117 | 0 | if (conn->server == NULL) { |
118 | 0 | os_free(conn); |
119 | 0 | return NULL; |
120 | 0 | } |
121 | 0 | } |
122 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
123 | | |
124 | 0 | return conn; |
125 | 0 | } |
126 | | |
127 | | |
128 | | #ifdef CONFIG_TESTING_OPTIONS |
129 | | #ifdef CONFIG_TLS_INTERNAL_SERVER |
130 | | void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags) |
131 | | { |
132 | | if (conn->server) |
133 | | tlsv1_server_set_test_flags(conn->server, flags); |
134 | | } |
135 | | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
136 | | #endif /* CONFIG_TESTING_OPTIONS */ |
137 | | |
138 | | |
139 | | void tls_connection_set_log_cb(struct tls_connection *conn, |
140 | | void (*log_cb)(void *ctx, const char *msg), |
141 | | void *ctx) |
142 | 0 | { |
143 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
144 | 0 | if (conn->server) |
145 | 0 | tlsv1_server_set_log_cb(conn->server, log_cb, ctx); |
146 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
147 | 0 | } |
148 | | |
149 | | |
150 | | void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn) |
151 | 0 | { |
152 | 0 | if (conn == NULL) |
153 | 0 | return; |
154 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
155 | 0 | if (conn->client) |
156 | 0 | tlsv1_client_deinit(conn->client); |
157 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
158 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
159 | 0 | if (conn->server) |
160 | 0 | tlsv1_server_deinit(conn->server); |
161 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
162 | 0 | os_free(conn); |
163 | 0 | } |
164 | | |
165 | | |
166 | | int tls_connection_established(void *tls_ctx, struct tls_connection *conn) |
167 | 0 | { |
168 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
169 | 0 | if (conn->client) |
170 | 0 | return tlsv1_client_established(conn->client); |
171 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
172 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
173 | 0 | if (conn->server) |
174 | 0 | return tlsv1_server_established(conn->server); |
175 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
176 | 0 | return 0; |
177 | 0 | } |
178 | | |
179 | | |
180 | | char * tls_connection_peer_serial_num(void *tls_ctx, |
181 | | struct tls_connection *conn) |
182 | 0 | { |
183 | | /* TODO */ |
184 | 0 | return NULL; |
185 | 0 | } |
186 | | |
187 | | |
188 | | int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn) |
189 | 0 | { |
190 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
191 | 0 | if (conn->client) |
192 | 0 | return tlsv1_client_shutdown(conn->client); |
193 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
194 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
195 | 0 | if (conn->server) |
196 | 0 | return tlsv1_server_shutdown(conn->server); |
197 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
198 | 0 | return -1; |
199 | 0 | } |
200 | | |
201 | | |
202 | | int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, |
203 | | const struct tls_connection_params *params) |
204 | 0 | { |
205 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
206 | 0 | struct tlsv1_credentials *cred; |
207 | |
|
208 | 0 | if (conn->client == NULL) |
209 | 0 | return -1; |
210 | | |
211 | 0 | if (params->flags & TLS_CONN_EXT_CERT_CHECK) { |
212 | 0 | wpa_printf(MSG_INFO, |
213 | 0 | "TLS: tls_ext_cert_check=1 not supported"); |
214 | 0 | return -1; |
215 | 0 | } |
216 | | |
217 | 0 | cred = tlsv1_cred_alloc(); |
218 | 0 | if (cred == NULL) |
219 | 0 | return -1; |
220 | | |
221 | 0 | if (params->subject_match) { |
222 | 0 | wpa_printf(MSG_INFO, "TLS: subject_match not supported"); |
223 | 0 | tlsv1_cred_free(cred); |
224 | 0 | return -1; |
225 | 0 | } |
226 | | |
227 | 0 | if (params->altsubject_match) { |
228 | 0 | wpa_printf(MSG_INFO, "TLS: altsubject_match not supported"); |
229 | 0 | tlsv1_cred_free(cred); |
230 | 0 | return -1; |
231 | 0 | } |
232 | | |
233 | 0 | if (params->suffix_match) { |
234 | 0 | wpa_printf(MSG_INFO, "TLS: suffix_match not supported"); |
235 | 0 | tlsv1_cred_free(cred); |
236 | 0 | return -1; |
237 | 0 | } |
238 | | |
239 | 0 | if (params->domain_match) { |
240 | 0 | wpa_printf(MSG_INFO, "TLS: domain_match not supported"); |
241 | 0 | tlsv1_cred_free(cred); |
242 | 0 | return -1; |
243 | 0 | } |
244 | | |
245 | 0 | if (params->openssl_ciphers) { |
246 | 0 | wpa_printf(MSG_INFO, "TLS: openssl_ciphers not supported"); |
247 | 0 | tlsv1_cred_free(cred); |
248 | 0 | return -1; |
249 | 0 | } |
250 | | |
251 | 0 | if (params->openssl_ecdh_curves) { |
252 | 0 | wpa_printf(MSG_INFO, "TLS: openssl_ecdh_curves not supported"); |
253 | 0 | tlsv1_cred_free(cred); |
254 | 0 | return -1; |
255 | 0 | } |
256 | | |
257 | 0 | if (params->openssl_sigalgs) { |
258 | 0 | wpa_printf(MSG_INFO, "TLS: openssl_sigalgs not supported"); |
259 | 0 | tlsv1_cred_free(cred); |
260 | 0 | return -1; |
261 | 0 | } |
262 | | |
263 | 0 | if (tlsv1_set_ca_cert(cred, params->ca_cert, |
264 | 0 | params->ca_cert_blob, params->ca_cert_blob_len, |
265 | 0 | params->ca_path)) { |
266 | 0 | wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA " |
267 | 0 | "certificates"); |
268 | 0 | tlsv1_cred_free(cred); |
269 | 0 | return -1; |
270 | 0 | } |
271 | | |
272 | 0 | if (tlsv1_set_cert(cred, params->client_cert, |
273 | 0 | params->client_cert_blob, |
274 | 0 | params->client_cert_blob_len)) { |
275 | 0 | wpa_printf(MSG_INFO, "TLS: Failed to configure client " |
276 | 0 | "certificate"); |
277 | 0 | tlsv1_cred_free(cred); |
278 | 0 | return -1; |
279 | 0 | } |
280 | | |
281 | 0 | if (tlsv1_set_private_key(cred, params->private_key, |
282 | 0 | params->private_key_passwd, |
283 | 0 | params->private_key_blob, |
284 | 0 | params->private_key_blob_len)) { |
285 | 0 | wpa_printf(MSG_INFO, "TLS: Failed to load private key"); |
286 | 0 | tlsv1_cred_free(cred); |
287 | 0 | return -1; |
288 | 0 | } |
289 | | |
290 | 0 | if (tlsv1_client_set_cred(conn->client, cred) < 0) { |
291 | 0 | tlsv1_cred_free(cred); |
292 | 0 | return -1; |
293 | 0 | } |
294 | | |
295 | 0 | tlsv1_client_set_flags(conn->client, params->flags); |
296 | |
|
297 | 0 | return 0; |
298 | | #else /* CONFIG_TLS_INTERNAL_CLIENT */ |
299 | | return -1; |
300 | | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
301 | 0 | } |
302 | | |
303 | | |
304 | | int tls_global_set_params(void *tls_ctx, |
305 | | const struct tls_connection_params *params) |
306 | 0 | { |
307 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
308 | 0 | struct tls_global *global = tls_ctx; |
309 | 0 | struct tlsv1_credentials *cred; |
310 | |
|
311 | 0 | if (params->check_cert_subject) |
312 | 0 | return -1; /* not yet supported */ |
313 | | |
314 | | /* Currently, global parameters are only set when running in server |
315 | | * mode. */ |
316 | 0 | global->server = 1; |
317 | 0 | tlsv1_cred_free(global->server_cred); |
318 | 0 | global->server_cred = cred = tlsv1_cred_alloc(); |
319 | 0 | if (cred == NULL) |
320 | 0 | return -1; |
321 | | |
322 | 0 | if (tlsv1_set_ca_cert(cred, params->ca_cert, params->ca_cert_blob, |
323 | 0 | params->ca_cert_blob_len, params->ca_path)) { |
324 | 0 | wpa_printf(MSG_INFO, "TLS: Failed to configure trusted CA " |
325 | 0 | "certificates"); |
326 | 0 | return -1; |
327 | 0 | } |
328 | | |
329 | 0 | if (tlsv1_set_cert(cred, params->client_cert, params->client_cert_blob, |
330 | 0 | params->client_cert_blob_len)) { |
331 | 0 | wpa_printf(MSG_INFO, "TLS: Failed to configure server " |
332 | 0 | "certificate"); |
333 | 0 | return -1; |
334 | 0 | } |
335 | | |
336 | 0 | if (tlsv1_set_private_key(cred, params->private_key, |
337 | 0 | params->private_key_passwd, |
338 | 0 | params->private_key_blob, |
339 | 0 | params->private_key_blob_len)) { |
340 | 0 | wpa_printf(MSG_INFO, "TLS: Failed to load private key"); |
341 | 0 | return -1; |
342 | 0 | } |
343 | | |
344 | 0 | if (tlsv1_set_dhparams(cred, params->dh_file, NULL, 0)) { |
345 | 0 | wpa_printf(MSG_INFO, "TLS: Failed to load DH parameters"); |
346 | 0 | return -1; |
347 | 0 | } |
348 | | |
349 | 0 | if (params->ocsp_stapling_response) |
350 | 0 | cred->ocsp_stapling_response = |
351 | 0 | os_strdup(params->ocsp_stapling_response); |
352 | 0 | if (params->ocsp_stapling_response_multi) |
353 | 0 | cred->ocsp_stapling_response_multi = |
354 | 0 | os_strdup(params->ocsp_stapling_response_multi); |
355 | |
|
356 | 0 | return 0; |
357 | | #else /* CONFIG_TLS_INTERNAL_SERVER */ |
358 | | return -1; |
359 | | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
360 | 0 | } |
361 | | |
362 | | |
363 | | int tls_global_set_verify(void *tls_ctx, int check_crl, int strict) |
364 | 0 | { |
365 | 0 | struct tls_global *global = tls_ctx; |
366 | 0 | global->check_crl = check_crl; |
367 | 0 | return 0; |
368 | 0 | } |
369 | | |
370 | | |
371 | | int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn, |
372 | | int verify_peer, unsigned int flags, |
373 | | const u8 *session_ctx, size_t session_ctx_len) |
374 | 0 | { |
375 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
376 | 0 | if (conn->server) |
377 | 0 | return tlsv1_server_set_verify(conn->server, verify_peer); |
378 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
379 | 0 | return -1; |
380 | 0 | } |
381 | | |
382 | | |
383 | | int tls_connection_get_random(void *tls_ctx, struct tls_connection *conn, |
384 | | struct tls_random *data) |
385 | 0 | { |
386 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
387 | 0 | if (conn->client) |
388 | 0 | return tlsv1_client_get_random(conn->client, data); |
389 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
390 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
391 | 0 | if (conn->server) |
392 | 0 | return tlsv1_server_get_random(conn->server, data); |
393 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
394 | 0 | return -1; |
395 | 0 | } |
396 | | |
397 | | |
398 | | static int tls_get_keyblock_size(struct tls_connection *conn) |
399 | 0 | { |
400 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
401 | 0 | if (conn->client) |
402 | 0 | return tlsv1_client_get_keyblock_size(conn->client); |
403 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
404 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
405 | 0 | if (conn->server) |
406 | 0 | return tlsv1_server_get_keyblock_size(conn->server); |
407 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
408 | 0 | return -1; |
409 | 0 | } |
410 | | |
411 | | |
412 | | static int tls_connection_prf(void *tls_ctx, struct tls_connection *conn, |
413 | | const char *label, const u8 *context, |
414 | | size_t context_len, int server_random_first, |
415 | | int skip_keyblock, u8 *out, size_t out_len) |
416 | 0 | { |
417 | 0 | int ret = -1, skip = 0; |
418 | 0 | u8 *tmp_out = NULL; |
419 | 0 | u8 *_out = out; |
420 | |
|
421 | 0 | if (skip_keyblock) { |
422 | 0 | skip = tls_get_keyblock_size(conn); |
423 | 0 | if (skip < 0) |
424 | 0 | return -1; |
425 | 0 | tmp_out = os_malloc(skip + out_len); |
426 | 0 | if (!tmp_out) |
427 | 0 | return -1; |
428 | 0 | _out = tmp_out; |
429 | 0 | } |
430 | | |
431 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
432 | 0 | if (conn->client) { |
433 | 0 | ret = tlsv1_client_prf(conn->client, label, context, |
434 | 0 | context_len, server_random_first, |
435 | 0 | _out, skip + out_len); |
436 | 0 | } |
437 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
438 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
439 | 0 | if (conn->server) { |
440 | 0 | ret = tlsv1_server_prf(conn->server, label, context, |
441 | 0 | context_len, server_random_first, |
442 | 0 | _out, skip + out_len); |
443 | 0 | } |
444 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
445 | 0 | if (ret == 0 && skip_keyblock) |
446 | 0 | os_memcpy(out, _out + skip, out_len); |
447 | 0 | bin_clear_free(tmp_out, skip); |
448 | |
|
449 | 0 | return ret; |
450 | 0 | } |
451 | | |
452 | | |
453 | | int tls_connection_export_key(void *tls_ctx, struct tls_connection *conn, |
454 | | const char *label, const u8 *context, |
455 | | size_t context_len, u8 *out, size_t out_len) |
456 | 0 | { |
457 | 0 | return tls_connection_prf(tls_ctx, conn, label, context, context_len, |
458 | 0 | 0, 0, out, out_len); |
459 | 0 | } |
460 | | |
461 | | |
462 | | int tls_connection_get_eap_fast_key(void *tls_ctx, struct tls_connection *conn, |
463 | | u8 *out, size_t out_len) |
464 | 0 | { |
465 | 0 | return tls_connection_prf(tls_ctx, conn, "key expansion", NULL, 0, |
466 | 0 | 1, 1, out, out_len); |
467 | 0 | } |
468 | | |
469 | | |
470 | | struct wpabuf * tls_connection_handshake(void *tls_ctx, |
471 | | struct tls_connection *conn, |
472 | | const struct wpabuf *in_data, |
473 | | struct wpabuf **appl_data) |
474 | 0 | { |
475 | 0 | return tls_connection_handshake2(tls_ctx, conn, in_data, appl_data, |
476 | 0 | NULL); |
477 | 0 | } |
478 | | |
479 | | |
480 | | struct wpabuf * tls_connection_handshake2(void *tls_ctx, |
481 | | struct tls_connection *conn, |
482 | | const struct wpabuf *in_data, |
483 | | struct wpabuf **appl_data, |
484 | | int *need_more_data) |
485 | 0 | { |
486 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
487 | 0 | u8 *res, *ad; |
488 | 0 | size_t res_len, ad_len; |
489 | 0 | struct wpabuf *out; |
490 | |
|
491 | 0 | if (conn->client == NULL) |
492 | 0 | return NULL; |
493 | | |
494 | 0 | ad = NULL; |
495 | 0 | res = tlsv1_client_handshake(conn->client, |
496 | 0 | in_data ? wpabuf_head(in_data) : NULL, |
497 | 0 | in_data ? wpabuf_len(in_data) : 0, |
498 | 0 | &res_len, &ad, &ad_len, need_more_data); |
499 | 0 | if (res == NULL) |
500 | 0 | return NULL; |
501 | 0 | out = wpabuf_alloc_ext_data(res, res_len); |
502 | 0 | if (out == NULL) { |
503 | 0 | os_free(res); |
504 | 0 | os_free(ad); |
505 | 0 | return NULL; |
506 | 0 | } |
507 | 0 | if (appl_data) { |
508 | 0 | if (ad) { |
509 | 0 | *appl_data = wpabuf_alloc_ext_data(ad, ad_len); |
510 | 0 | if (*appl_data == NULL) |
511 | 0 | os_free(ad); |
512 | 0 | } else |
513 | 0 | *appl_data = NULL; |
514 | 0 | } else |
515 | 0 | os_free(ad); |
516 | |
|
517 | 0 | return out; |
518 | | #else /* CONFIG_TLS_INTERNAL_CLIENT */ |
519 | | return NULL; |
520 | | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
521 | 0 | } |
522 | | |
523 | | |
524 | | struct wpabuf * tls_connection_server_handshake(void *tls_ctx, |
525 | | struct tls_connection *conn, |
526 | | const struct wpabuf *in_data, |
527 | | struct wpabuf **appl_data) |
528 | 0 | { |
529 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
530 | 0 | u8 *res; |
531 | 0 | size_t res_len; |
532 | 0 | struct wpabuf *out; |
533 | |
|
534 | 0 | if (conn->server == NULL) |
535 | 0 | return NULL; |
536 | | |
537 | 0 | if (appl_data) |
538 | 0 | *appl_data = NULL; |
539 | |
|
540 | 0 | res = tlsv1_server_handshake(conn->server, wpabuf_head(in_data), |
541 | 0 | wpabuf_len(in_data), &res_len); |
542 | 0 | if (res == NULL && tlsv1_server_established(conn->server)) |
543 | 0 | return wpabuf_alloc(0); |
544 | 0 | if (res == NULL) |
545 | 0 | return NULL; |
546 | 0 | out = wpabuf_alloc_ext_data(res, res_len); |
547 | 0 | if (out == NULL) { |
548 | 0 | os_free(res); |
549 | 0 | return NULL; |
550 | 0 | } |
551 | | |
552 | 0 | return out; |
553 | | #else /* CONFIG_TLS_INTERNAL_SERVER */ |
554 | | return NULL; |
555 | | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
556 | 0 | } |
557 | | |
558 | | |
559 | | struct wpabuf * tls_connection_encrypt(void *tls_ctx, |
560 | | struct tls_connection *conn, |
561 | | const struct wpabuf *in_data) |
562 | 0 | { |
563 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
564 | 0 | if (conn->client) { |
565 | 0 | struct wpabuf *buf; |
566 | 0 | int res; |
567 | 0 | buf = wpabuf_alloc(wpabuf_len(in_data) + 300); |
568 | 0 | if (buf == NULL) |
569 | 0 | return NULL; |
570 | 0 | res = tlsv1_client_encrypt(conn->client, wpabuf_head(in_data), |
571 | 0 | wpabuf_len(in_data), |
572 | 0 | wpabuf_mhead(buf), |
573 | 0 | wpabuf_size(buf)); |
574 | 0 | if (res < 0) { |
575 | 0 | wpabuf_free(buf); |
576 | 0 | return NULL; |
577 | 0 | } |
578 | 0 | wpabuf_put(buf, res); |
579 | 0 | return buf; |
580 | 0 | } |
581 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
582 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
583 | 0 | if (conn->server) { |
584 | 0 | struct wpabuf *buf; |
585 | 0 | int res; |
586 | 0 | buf = wpabuf_alloc(wpabuf_len(in_data) + 300); |
587 | 0 | if (buf == NULL) |
588 | 0 | return NULL; |
589 | 0 | res = tlsv1_server_encrypt(conn->server, wpabuf_head(in_data), |
590 | 0 | wpabuf_len(in_data), |
591 | 0 | wpabuf_mhead(buf), |
592 | 0 | wpabuf_size(buf)); |
593 | 0 | if (res < 0) { |
594 | 0 | wpabuf_free(buf); |
595 | 0 | return NULL; |
596 | 0 | } |
597 | 0 | wpabuf_put(buf, res); |
598 | 0 | return buf; |
599 | 0 | } |
600 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
601 | 0 | return NULL; |
602 | 0 | } |
603 | | |
604 | | |
605 | | struct wpabuf * tls_connection_decrypt(void *tls_ctx, |
606 | | struct tls_connection *conn, |
607 | | const struct wpabuf *in_data) |
608 | 0 | { |
609 | 0 | return tls_connection_decrypt2(tls_ctx, conn, in_data, NULL); |
610 | 0 | } |
611 | | |
612 | | |
613 | | struct wpabuf * tls_connection_decrypt2(void *tls_ctx, |
614 | | struct tls_connection *conn, |
615 | | const struct wpabuf *in_data, |
616 | | int *need_more_data) |
617 | 0 | { |
618 | 0 | if (need_more_data) |
619 | 0 | *need_more_data = 0; |
620 | |
|
621 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
622 | 0 | if (conn->client) { |
623 | 0 | return tlsv1_client_decrypt(conn->client, wpabuf_head(in_data), |
624 | 0 | wpabuf_len(in_data), |
625 | 0 | need_more_data); |
626 | 0 | } |
627 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
628 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
629 | 0 | if (conn->server) { |
630 | 0 | struct wpabuf *buf; |
631 | 0 | int res; |
632 | 0 | buf = wpabuf_alloc((wpabuf_len(in_data) + 500) * 3); |
633 | 0 | if (buf == NULL) |
634 | 0 | return NULL; |
635 | 0 | res = tlsv1_server_decrypt(conn->server, wpabuf_head(in_data), |
636 | 0 | wpabuf_len(in_data), |
637 | 0 | wpabuf_mhead(buf), |
638 | 0 | wpabuf_size(buf)); |
639 | 0 | if (res < 0) { |
640 | 0 | wpabuf_free(buf); |
641 | 0 | return NULL; |
642 | 0 | } |
643 | 0 | wpabuf_put(buf, res); |
644 | 0 | return buf; |
645 | 0 | } |
646 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
647 | 0 | return NULL; |
648 | 0 | } |
649 | | |
650 | | |
651 | | int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn) |
652 | 0 | { |
653 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
654 | 0 | if (conn->client) |
655 | 0 | return tlsv1_client_resumed(conn->client); |
656 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
657 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
658 | 0 | if (conn->server) |
659 | 0 | return tlsv1_server_resumed(conn->server); |
660 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
661 | 0 | return -1; |
662 | 0 | } |
663 | | |
664 | | |
665 | | int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn, |
666 | | u8 *ciphers) |
667 | 0 | { |
668 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
669 | 0 | if (conn->client) |
670 | 0 | return tlsv1_client_set_cipher_list(conn->client, ciphers); |
671 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
672 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
673 | 0 | if (conn->server) |
674 | 0 | return tlsv1_server_set_cipher_list(conn->server, ciphers); |
675 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
676 | 0 | return -1; |
677 | 0 | } |
678 | | |
679 | | |
680 | | int tls_get_version(void *ssl_ctx, struct tls_connection *conn, |
681 | | char *buf, size_t buflen) |
682 | 0 | { |
683 | 0 | if (conn == NULL) |
684 | 0 | return -1; |
685 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
686 | 0 | if (conn->client) |
687 | 0 | return tlsv1_client_get_version(conn->client, buf, buflen); |
688 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
689 | 0 | return -1; |
690 | 0 | } |
691 | | |
692 | | |
693 | | int tls_get_cipher(void *tls_ctx, struct tls_connection *conn, |
694 | | char *buf, size_t buflen) |
695 | 0 | { |
696 | 0 | if (conn == NULL) |
697 | 0 | return -1; |
698 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
699 | 0 | if (conn->client) |
700 | 0 | return tlsv1_client_get_cipher(conn->client, buf, buflen); |
701 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
702 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
703 | 0 | if (conn->server) |
704 | 0 | return tlsv1_server_get_cipher(conn->server, buf, buflen); |
705 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
706 | 0 | return -1; |
707 | 0 | } |
708 | | |
709 | | |
710 | | int tls_connection_enable_workaround(void *tls_ctx, |
711 | | struct tls_connection *conn) |
712 | 0 | { |
713 | 0 | return -1; |
714 | 0 | } |
715 | | |
716 | | |
717 | | int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn, |
718 | | int ext_type, const u8 *data, |
719 | | size_t data_len) |
720 | 0 | { |
721 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
722 | 0 | if (conn->client) { |
723 | 0 | return tlsv1_client_hello_ext(conn->client, ext_type, |
724 | 0 | data, data_len); |
725 | 0 | } |
726 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
727 | 0 | return -1; |
728 | 0 | } |
729 | | |
730 | | |
731 | | int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn) |
732 | 0 | { |
733 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
734 | 0 | if (conn->server) |
735 | 0 | return tlsv1_server_get_failed(conn->server); |
736 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
737 | 0 | return 0; |
738 | 0 | } |
739 | | |
740 | | |
741 | | int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn) |
742 | 0 | { |
743 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
744 | 0 | if (conn->server) |
745 | 0 | return tlsv1_server_get_read_alerts(conn->server); |
746 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
747 | 0 | return 0; |
748 | 0 | } |
749 | | |
750 | | |
751 | | int tls_connection_get_write_alerts(void *tls_ctx, |
752 | | struct tls_connection *conn) |
753 | 0 | { |
754 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
755 | 0 | if (conn->server) |
756 | 0 | return tlsv1_server_get_write_alerts(conn->server); |
757 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
758 | 0 | return 0; |
759 | 0 | } |
760 | | |
761 | | |
762 | | int tls_connection_set_session_ticket_cb(void *tls_ctx, |
763 | | struct tls_connection *conn, |
764 | | tls_session_ticket_cb cb, |
765 | | void *ctx) |
766 | 0 | { |
767 | 0 | #ifdef CONFIG_TLS_INTERNAL_CLIENT |
768 | 0 | if (conn->client) { |
769 | 0 | tlsv1_client_set_session_ticket_cb(conn->client, cb, ctx); |
770 | 0 | return 0; |
771 | 0 | } |
772 | 0 | #endif /* CONFIG_TLS_INTERNAL_CLIENT */ |
773 | 0 | #ifdef CONFIG_TLS_INTERNAL_SERVER |
774 | 0 | if (conn->server) { |
775 | 0 | tlsv1_server_set_session_ticket_cb(conn->server, cb, ctx); |
776 | 0 | return 0; |
777 | 0 | } |
778 | 0 | #endif /* CONFIG_TLS_INTERNAL_SERVER */ |
779 | 0 | return -1; |
780 | 0 | } |
781 | | |
782 | | |
783 | | int tls_get_library_version(char *buf, size_t buf_len) |
784 | 0 | { |
785 | 0 | return os_snprintf(buf, buf_len, "internal"); |
786 | 0 | } |
787 | | |
788 | | |
789 | | void tls_connection_set_success_data(struct tls_connection *conn, |
790 | | struct wpabuf *data) |
791 | 0 | { |
792 | 0 | wpabuf_free(data); |
793 | 0 | } |
794 | | |
795 | | |
796 | | void tls_connection_set_success_data_resumed(struct tls_connection *conn) |
797 | 0 | { |
798 | 0 | } |
799 | | |
800 | | |
801 | | const struct wpabuf * |
802 | | tls_connection_get_success_data(struct tls_connection *conn) |
803 | 0 | { |
804 | 0 | return NULL; |
805 | 0 | } |
806 | | |
807 | | |
808 | | void tls_connection_remove_session(struct tls_connection *conn) |
809 | 0 | { |
810 | 0 | } |