1 /*
2  * SSL/TLS interface definition
3  * Copyright (c) 2004-2013, 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 
9 #ifndef TLS_H
10 #define TLS_H
11 
12 struct tls_connection;
13 
14 struct tls_random {
15 	const u8 *client_random;
16 	size_t client_random_len;
17 	const u8 *server_random;
18 	size_t server_random_len;
19 };
20 
21 enum tls_event {
22 	TLS_CERT_CHAIN_SUCCESS,
23 	TLS_CERT_CHAIN_FAILURE,
24 	TLS_PEER_CERTIFICATE,
25 	TLS_ALERT
26 };
27 
28 /*
29  * Note: These are used as identifier with external programs and as such, the
30  * values must not be changed.
31  */
32 enum tls_fail_reason {
33 	TLS_FAIL_UNSPECIFIED = 0,
34 	TLS_FAIL_UNTRUSTED = 1,
35 	TLS_FAIL_REVOKED = 2,
36 	TLS_FAIL_NOT_YET_VALID = 3,
37 	TLS_FAIL_EXPIRED = 4,
38 	TLS_FAIL_SUBJECT_MISMATCH = 5,
39 	TLS_FAIL_ALTSUBJECT_MISMATCH = 6,
40 	TLS_FAIL_BAD_CERTIFICATE = 7,
41 	TLS_FAIL_SERVER_CHAIN_PROBE = 8,
42 	TLS_FAIL_DOMAIN_SUFFIX_MISMATCH = 9,
43 	TLS_FAIL_DOMAIN_MISMATCH = 10,
44 	TLS_FAIL_INSUFFICIENT_KEY_LEN = 11,
45 	TLS_FAIL_DN_MISMATCH = 12,
46 };
47 
48 
49 #define TLS_MAX_ALT_SUBJECT 10
50 
51 struct tls_cert_data {
52 	int depth;
53 	const char *subject;
54 	const struct wpabuf *cert;
55 	const u8 *hash;
56 	size_t hash_len;
57 	const char *altsubject[TLS_MAX_ALT_SUBJECT];
58 	int num_altsubject;
59 	const char *serial_num;
60 	int tod;
61 };
62 
63 union tls_event_data {
64 	struct {
65 		int depth;
66 		const char *subject;
67 		enum tls_fail_reason reason;
68 		const char *reason_txt;
69 		const struct wpabuf *cert;
70 	} cert_fail;
71 
72 	struct tls_cert_data peer_cert;
73 
74 	struct {
75 		int is_local;
76 		const char *type;
77 		const char *description;
78 	} alert;
79 };
80 
81 struct tls_config {
82 	const char *opensc_engine_path;
83 	const char *pkcs11_engine_path;
84 	const char *pkcs11_module_path;
85 	int fips_mode;
86 	int cert_in_cb;
87 	const char *openssl_ciphers;
88 	unsigned int tls_session_lifetime;
89 	unsigned int crl_reload_interval;
90 	unsigned int tls_flags;
91 
92 	void (*event_cb)(void *ctx, enum tls_event ev,
93 			 union tls_event_data *data);
94 	void *cb_ctx;
95 };
96 
97 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
98 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
99 #define TLS_CONN_DISABLE_SESSION_TICKET BIT(2)
100 #define TLS_CONN_REQUEST_OCSP BIT(3)
101 #define TLS_CONN_REQUIRE_OCSP BIT(4)
102 #define TLS_CONN_DISABLE_TLSv1_1 BIT(5)
103 #define TLS_CONN_DISABLE_TLSv1_2 BIT(6)
104 #define TLS_CONN_EAP_FAST BIT(7)
105 #define TLS_CONN_DISABLE_TLSv1_0 BIT(8)
106 #define TLS_CONN_EXT_CERT_CHECK BIT(9)
107 #define TLS_CONN_REQUIRE_OCSP_ALL BIT(10)
108 #define TLS_CONN_SUITEB BIT(11)
109 #define TLS_CONN_SUITEB_NO_ECDH BIT(12)
110 #define TLS_CONN_DISABLE_TLSv1_3 BIT(13)
111 #define TLS_CONN_ENABLE_TLSv1_0 BIT(14)
112 #define TLS_CONN_ENABLE_TLSv1_1 BIT(15)
113 #define TLS_CONN_ENABLE_TLSv1_2 BIT(16)
114 #define TLS_CONN_TEAP_ANON_DH BIT(17)
115 #define TLS_CONN_USE_DEFAULT_CERT_BUNDLE BIT(18)
116 
117 /**
118  * struct tls_connection_params - Parameters for TLS connection
119  * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
120  * format
121  * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
122  * @ca_cert_blob_len: ca_cert_blob length
123  * @ca_path: Path to CA certificates (OpenSSL specific)
124  * @subject_match: String to match in the subject of the peer certificate or
125  * %NULL to allow all subjects
126  * @altsubject_match: String to match in the alternative subject of the peer
127  * certificate or %NULL to allow all alternative subjects
128  * @suffix_match: Semicolon deliminated string of values to suffix match against
129  * the dNSName or CN of the peer certificate or %NULL to allow all domain names.
130  * This may allow subdomains and wildcard certificates. Each domain name label
131  * must have a full case-insensitive match.
132  * @domain_match: String to match in the dNSName or CN of the peer
133  * certificate or %NULL to allow all domain names. This requires a full,
134  * case-insensitive match.
135  *
136  * More than one match string can be provided by using semicolons to
137  * separate the strings (e.g., example.org;example.com). When multiple
138  * strings are specified, a match with any one of the values is
139  * considered a sufficient match for the certificate, i.e., the
140  * conditions are ORed together.
141  * @client_cert: File or reference name for client X.509 certificate in PEM or
142  * DER format
143  * @client_cert_blob: client_cert as inlined data or %NULL if not used
144  * @client_cert_blob_len: client_cert_blob length
145  * @private_key: File or reference name for client private key in PEM or DER
146  * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
147  * @private_key_blob: private_key as inlined data or %NULL if not used
148  * @private_key_blob_len: private_key_blob length
149  * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
150  * passphrase is used.
151  * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
152  * @dh_blob: dh_file as inlined data or %NULL if not used
153  * @dh_blob_len: dh_blob length
154  * @engine: 1 = use engine (e.g., a smartcard) for private key operations
155  * (this is OpenSSL specific for now)
156  * @engine_id: engine id string (this is OpenSSL specific for now)
157  * @ppin: pointer to the pin variable in the configuration
158  * (this is OpenSSL specific for now)
159  * @key_id: the private key's id when using engine (this is OpenSSL
160  * specific for now)
161  * @cert_id: the certificate's id when using engine
162  * @ca_cert_id: the CA certificate's id when using engine
163  * @openssl_ciphers: OpenSSL cipher configuration
164  * @openssl_ecdh_curves: OpenSSL ECDH curve configuration. %NULL for auto if
165  *	supported, empty string to disable, or a colon-separated curve list.
166  * @flags: Parameter options (TLS_CONN_*)
167  * @ocsp_stapling_response: DER encoded file with cached OCSP stapling response
168  *	or %NULL if OCSP is not enabled
169  * @ocsp_stapling_response_multi: DER encoded file with cached OCSP stapling
170  *	response list (OCSPResponseList for ocsp_multi in RFC 6961) or %NULL if
171  *	ocsp_multi is not enabled
172  * @check_cert_subject: Client certificate subject name matching string
173  *
174  * TLS connection parameters to be configured with tls_connection_set_params()
175  * and tls_global_set_params().
176  *
177  * Certificates and private key can be configured either as a reference name
178  * (file path or reference to certificate store) or by providing the same data
179  * as a pointer to the data in memory. Only one option will be used for each
180  * field.
181  */
182 struct tls_connection_params {
183 	const char *ca_cert;
184 	const u8 *ca_cert_blob;
185 	size_t ca_cert_blob_len;
186 	const char *ca_path;
187 	const char *subject_match;
188 	const char *altsubject_match;
189 	const char *suffix_match;
190 	const char *domain_match;
191 	const char *client_cert;
192 	const char *client_cert2;
193 	const u8 *client_cert_blob;
194 	size_t client_cert_blob_len;
195 	const char *private_key;
196 	const char *private_key2;
197 	const u8 *private_key_blob;
198 	size_t private_key_blob_len;
199 	const char *private_key_passwd;
200 	const char *private_key_passwd2;
201 	const char *dh_file;
202 	const u8 *dh_blob;
203 	size_t dh_blob_len;
204 
205 	/* OpenSSL specific variables */
206 	int engine;
207 	const char *engine_id;
208 	const char *pin;
209 	const char *key_id;
210 	const char *cert_id;
211 	const char *ca_cert_id;
212 	const char *openssl_ciphers;
213 	const char *openssl_ecdh_curves;
214 
215 	unsigned int flags;
216 	const char *ocsp_stapling_response;
217 	const char *ocsp_stapling_response_multi;
218 	const char *check_cert_subject;
219 };
220 
221 
222 /**
223  * tls_init - Initialize TLS library
224  * @conf: Configuration data for TLS library
225  * Returns: Context data to be used as tls_ctx in calls to other functions,
226  * or %NULL on failure.
227  *
228  * Called once during program startup and once for each RSN pre-authentication
229  * session. In other words, there can be two concurrent TLS contexts. If global
230  * library initialization is needed (i.e., one that is shared between both
231  * authentication types), the TLS library wrapper should maintain a reference
232  * counter and do global initialization only when moving from 0 to 1 reference.
233  */
234 void * tls_init(const struct tls_config *conf);
235 
236 /**
237  * tls_deinit - Deinitialize TLS library
238  * @tls_ctx: TLS context data from tls_init()
239  *
240  * Called once during program shutdown and once for each RSN pre-authentication
241  * session. If global library deinitialization is needed (i.e., one that is
242  * shared between both authentication types), the TLS library wrapper should
243  * maintain a reference counter and do global deinitialization only when moving
244  * from 1 to 0 references.
245  */
246 void tls_deinit(void *tls_ctx);
247 
248 /**
249  * tls_get_errors - Process pending errors
250  * @tls_ctx: TLS context data from tls_init()
251  * Returns: Number of found error, 0 if no errors detected.
252  *
253  * Process all pending TLS errors.
254  */
255 int tls_get_errors(void *tls_ctx);
256 
257 /**
258  * tls_connection_init - Initialize a new TLS connection
259  * @tls_ctx: TLS context data from tls_init()
260  * Returns: Connection context data, conn for other function calls
261  */
262 struct tls_connection * tls_connection_init(void *tls_ctx);
263 
264 /**
265  * tls_connection_deinit - Free TLS connection data
266  * @tls_ctx: TLS context data from tls_init()
267  * @conn: Connection context data from tls_connection_init()
268  *
269  * Release all resources allocated for TLS connection.
270  */
271 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
272 
273 /**
274  * tls_connection_established - Has the TLS connection been completed?
275  * @tls_ctx: TLS context data from tls_init()
276  * @conn: Connection context data from tls_connection_init()
277  * Returns: 1 if TLS connection has been completed, 0 if not.
278  */
279 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
280 
281 /**
282  * tls_connection_peer_serial_num - Fetch peer certificate serial number
283  * @tls_ctx: TLS context data from tls_init()
284  * @conn: Connection context data from tls_connection_init()
285  * Returns: Allocated string buffer containing the peer certificate serial
286  * number or %NULL on error.
287  *
288  * The caller is responsible for freeing the returned buffer with os_free().
289  */
290 char * tls_connection_peer_serial_num(void *tls_ctx,
291 				      struct tls_connection *conn);
292 
293 /**
294  * tls_connection_shutdown - Shutdown TLS connection
295  * @tls_ctx: TLS context data from tls_init()
296  * @conn: Connection context data from tls_connection_init()
297  * Returns: 0 on success, -1 on failure
298  *
299  * Shutdown current TLS connection without releasing all resources. New
300  * connection can be started by using the same conn without having to call
301  * tls_connection_init() or setting certificates etc. again. The new
302  * connection should try to use session resumption.
303  */
304 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
305 
306 enum {
307 	TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN = -4,
308 	TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
309 	TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
310 };
311 
312 /**
313  * tls_connection_set_params - Set TLS connection parameters
314  * @tls_ctx: TLS context data from tls_init()
315  * @conn: Connection context data from tls_connection_init()
316  * @params: Connection parameters
317  * Returns: 0 on success, -1 on failure,
318  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
319  * failure, or
320  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
321  * PKCS#11 engine private key, or
322  * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
323  * failure.
324  */
325 int __must_check
326 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
327 			  const struct tls_connection_params *params);
328 
329 /**
330  * tls_global_set_params - Set TLS parameters for all TLS connection
331  * @tls_ctx: TLS context data from tls_init()
332  * @params: Global TLS parameters
333  * Returns: 0 on success, -1 on failure,
334  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
335  * failure, or
336  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
337  * PKCS#11 engine private key, or
338  * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
339  * failure.
340  */
341 int __must_check tls_global_set_params(
342 	void *tls_ctx, const struct tls_connection_params *params);
343 
344 /**
345  * tls_global_set_verify - Set global certificate verification options
346  * @tls_ctx: TLS context data from tls_init()
347  * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
348  * 2 = verify CRL for all certificates
349  * @strict: 0 = allow CRL time errors, 1 = do not allow CRL time errors
350  * Returns: 0 on success, -1 on failure
351  */
352 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl,
353 				       int strict);
354 
355 /**
356  * tls_connection_set_verify - Set certificate verification options
357  * @tls_ctx: TLS context data from tls_init()
358  * @conn: Connection context data from tls_connection_init()
359  * @verify_peer: 1 = verify peer certificate
360  * @flags: Connection flags (TLS_CONN_*)
361  * @session_ctx: Session caching context or %NULL to use default
362  * @session_ctx_len: Length of @session_ctx in bytes.
363  * Returns: 0 on success, -1 on failure
364  */
365 int __must_check tls_connection_set_verify(void *tls_ctx,
366 					   struct tls_connection *conn,
367 					   int verify_peer,
368 					   unsigned int flags,
369 					   const u8 *session_ctx,
370 					   size_t session_ctx_len);
371 
372 /**
373  * tls_connection_get_random - Get random data from TLS connection
374  * @tls_ctx: TLS context data from tls_init()
375  * @conn: Connection context data from tls_connection_init()
376  * @data: Structure of client/server random data (filled on success)
377  * Returns: 0 on success, -1 on failure
378  */
379 int __must_check tls_connection_get_random(void *tls_ctx,
380 					 struct tls_connection *conn,
381 					 struct tls_random *data);
382 
383 /**
384  * tls_connection_export_key - Derive keying material from a TLS connection
385  * @tls_ctx: TLS context data from tls_init()
386  * @conn: Connection context data from tls_connection_init()
387  * @label: Label (e.g., description of the key) for PRF
388  * @context: Optional extra upper-layer context (max len 2^16)
389  * @context_len: The length of the context value
390  * @out: Buffer for output data from TLS-PRF
391  * @out_len: Length of the output buffer
392  * Returns: 0 on success, -1 on failure
393  *
394  * Exports keying material using the mechanism described in RFC 5705. If
395  * context is %NULL, context is not provided; otherwise, context is provided
396  * (including the case of empty context with context_len == 0).
397  */
398 int __must_check tls_connection_export_key(void *tls_ctx,
399 					   struct tls_connection *conn,
400 					   const char *label,
401 					   const u8 *context,
402 					   size_t context_len,
403 					   u8 *out, size_t out_len);
404 
405 /**
406  * tls_connection_get_eap_fast_key - Derive key material for EAP-FAST
407  * @tls_ctx: TLS context data from tls_init()
408  * @conn: Connection context data from tls_connection_init()
409  * @out: Buffer for output data from TLS-PRF
410  * @out_len: Length of the output buffer
411  * Returns: 0 on success, -1 on failure
412  *
413  * Exports key material after the normal TLS key block for use with
414  * EAP-FAST. Most callers will want tls_connection_export_key(), but EAP-FAST
415  * uses a different legacy mechanism.
416  */
417 int __must_check tls_connection_get_eap_fast_key(void *tls_ctx,
418 						 struct tls_connection *conn,
419 						 u8 *out, size_t out_len);
420 
421 /**
422  * tls_connection_handshake - Process TLS handshake (client side)
423  * @tls_ctx: TLS context data from tls_init()
424  * @conn: Connection context data from tls_connection_init()
425  * @in_data: Input data from TLS server
426  * @appl_data: Pointer to application data pointer, or %NULL if dropped
427  * Returns: Output data, %NULL on failure
428  *
429  * The caller is responsible for freeing the returned output data. If the final
430  * handshake message includes application data, this is decrypted and
431  * appl_data (if not %NULL) is set to point this data. The caller is
432  * responsible for freeing appl_data.
433  *
434  * This function is used during TLS handshake. The first call is done with
435  * in_data == %NULL and the library is expected to return ClientHello packet.
436  * This packet is then send to the server and a response from server is given
437  * to TLS library by calling this function again with in_data pointing to the
438  * TLS message from the server.
439  *
440  * If the TLS handshake fails, this function may return %NULL. However, if the
441  * TLS library has a TLS alert to send out, that should be returned as the
442  * output data. In this case, tls_connection_get_failed() must return failure
443  * (> 0).
444  *
445  * tls_connection_established() should return 1 once the TLS handshake has been
446  * completed successfully.
447  */
448 struct wpabuf * tls_connection_handshake(void *tls_ctx,
449 					 struct tls_connection *conn,
450 					 const struct wpabuf *in_data,
451 					 struct wpabuf **appl_data);
452 
453 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
454 					  struct tls_connection *conn,
455 					  const struct wpabuf *in_data,
456 					  struct wpabuf **appl_data,
457 					  int *more_data_needed);
458 
459 /**
460  * tls_connection_server_handshake - Process TLS handshake (server side)
461  * @tls_ctx: TLS context data from tls_init()
462  * @conn: Connection context data from tls_connection_init()
463  * @in_data: Input data from TLS peer
464  * @appl_data: Pointer to application data pointer, or %NULL if dropped
465  * Returns: Output data, %NULL on failure
466  *
467  * The caller is responsible for freeing the returned output data.
468  */
469 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
470 						struct tls_connection *conn,
471 						const struct wpabuf *in_data,
472 						struct wpabuf **appl_data);
473 
474 /**
475  * tls_connection_encrypt - Encrypt data into TLS tunnel
476  * @tls_ctx: TLS context data from tls_init()
477  * @conn: Connection context data from tls_connection_init()
478  * @in_data: Plaintext data to be encrypted
479  * Returns: Encrypted TLS data or %NULL on failure
480  *
481  * This function is used after TLS handshake has been completed successfully to
482  * send data in the encrypted tunnel. The caller is responsible for freeing the
483  * returned output data.
484  */
485 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
486 				       struct tls_connection *conn,
487 				       const struct wpabuf *in_data);
488 
489 /**
490  * tls_connection_decrypt - Decrypt data from TLS tunnel
491  * @tls_ctx: TLS context data from tls_init()
492  * @conn: Connection context data from tls_connection_init()
493  * @in_data: Encrypted TLS data
494  * Returns: Decrypted TLS data or %NULL on failure
495  *
496  * This function is used after TLS handshake has been completed successfully to
497  * receive data from the encrypted tunnel. The caller is responsible for
498  * freeing the returned output data.
499  */
500 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
501 				       struct tls_connection *conn,
502 				       const struct wpabuf *in_data);
503 
504 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
505 					struct tls_connection *conn,
506 					const struct wpabuf *in_data,
507 					int *more_data_needed);
508 
509 /**
510  * tls_connection_resumed - Was session resumption used
511  * @tls_ctx: TLS context data from tls_init()
512  * @conn: Connection context data from tls_connection_init()
513  * Returns: 1 if current session used session resumption, 0 if not
514  */
515 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
516 
517 enum {
518 	TLS_CIPHER_NONE,
519 	TLS_CIPHER_RC4_SHA /* 0x0005 */,
520 	TLS_CIPHER_AES128_SHA /* 0x002f */,
521 	TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
522 	TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */,
523 	TLS_CIPHER_RSA_DHE_AES256_SHA /* 0x0039 */,
524 	TLS_CIPHER_AES256_SHA /* 0x0035 */,
525 };
526 
527 /**
528  * tls_connection_set_cipher_list - Configure acceptable cipher suites
529  * @tls_ctx: TLS context data from tls_init()
530  * @conn: Connection context data from tls_connection_init()
531  * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
532  * (TLS_CIPHER_*).
533  * Returns: 0 on success, -1 on failure
534  */
535 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
536 						struct tls_connection *conn,
537 						u8 *ciphers);
538 
539 /**
540  * tls_get_version - Get the current TLS version number
541  * @tls_ctx: TLS context data from tls_init()
542  * @conn: Connection context data from tls_connection_init()
543  * @buf: Buffer for returning the TLS version number
544  * @buflen: buf size
545  * Returns: 0 on success, -1 on failure
546  *
547  * Get the currently used TLS version number.
548  */
549 int __must_check tls_get_version(void *tls_ctx, struct tls_connection *conn,
550 				 char *buf, size_t buflen);
551 
552 /**
553  * tls_get_cipher - Get current cipher name
554  * @tls_ctx: TLS context data from tls_init()
555  * @conn: Connection context data from tls_connection_init()
556  * @buf: Buffer for the cipher name
557  * @buflen: buf size
558  * Returns: 0 on success, -1 on failure
559  *
560  * Get the name of the currently used cipher.
561  */
562 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
563 				char *buf, size_t buflen);
564 
565 /**
566  * tls_connection_enable_workaround - Enable TLS workaround options
567  * @tls_ctx: TLS context data from tls_init()
568  * @conn: Connection context data from tls_connection_init()
569  * Returns: 0 on success, -1 on failure
570  *
571  * This function is used to enable connection-specific workaround options for
572  * buffer SSL/TLS implementations.
573  */
574 int __must_check tls_connection_enable_workaround(void *tls_ctx,
575 						  struct tls_connection *conn);
576 
577 /**
578  * tls_connection_client_hello_ext - Set TLS extension for ClientHello
579  * @tls_ctx: TLS context data from tls_init()
580  * @conn: Connection context data from tls_connection_init()
581  * @ext_type: Extension type
582  * @data: Extension payload (%NULL to remove extension)
583  * @data_len: Extension payload length
584  * Returns: 0 on success, -1 on failure
585  */
586 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
587 						 struct tls_connection *conn,
588 						 int ext_type, const u8 *data,
589 						 size_t data_len);
590 
591 /**
592  * tls_connection_get_failed - Get connection failure status
593  * @tls_ctx: TLS context data from tls_init()
594  * @conn: Connection context data from tls_connection_init()
595  *
596  * Returns >0 if connection has failed, 0 if not.
597  */
598 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
599 
600 /**
601  * tls_connection_get_read_alerts - Get connection read alert status
602  * @tls_ctx: TLS context data from tls_init()
603  * @conn: Connection context data from tls_connection_init()
604  * Returns: Number of times a fatal read (remote end reported error) has
605  * happened during this connection.
606  */
607 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
608 
609 /**
610  * tls_connection_get_write_alerts - Get connection write alert status
611  * @tls_ctx: TLS context data from tls_init()
612  * @conn: Connection context data from tls_connection_init()
613  * Returns: Number of times a fatal write (locally detected error) has happened
614  * during this connection.
615  */
616 int tls_connection_get_write_alerts(void *tls_ctx,
617 				    struct tls_connection *conn);
618 
619 typedef int (*tls_session_ticket_cb)
620 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
621  const u8 *server_random, u8 *master_secret);
622 
623 int __must_check  tls_connection_set_session_ticket_cb(
624 	void *tls_ctx, struct tls_connection *conn,
625 	tls_session_ticket_cb cb, void *ctx);
626 
627 void tls_connection_set_log_cb(struct tls_connection *conn,
628 			       void (*log_cb)(void *ctx, const char *msg),
629 			       void *ctx);
630 
631 #define TLS_BREAK_VERIFY_DATA BIT(0)
632 #define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
633 #define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
634 #define TLS_DHE_PRIME_511B BIT(3)
635 #define TLS_DHE_PRIME_767B BIT(4)
636 #define TLS_DHE_PRIME_15 BIT(5)
637 #define TLS_DHE_PRIME_58B BIT(6)
638 #define TLS_DHE_NON_PRIME BIT(7)
639 
640 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
641 
642 int tls_get_library_version(char *buf, size_t buf_len);
643 
644 void tls_connection_set_success_data(struct tls_connection *conn,
645 				     struct wpabuf *data);
646 
647 void tls_connection_set_success_data_resumed(struct tls_connection *conn);
648 
649 const struct wpabuf *
650 tls_connection_get_success_data(struct tls_connection *conn);
651 
652 void tls_connection_remove_session(struct tls_connection *conn);
653 
654 /**
655  * tls_get_tls_unique - Fetch "tls-unique" for channel binding
656  * @conn: Connection context data from tls_connection_init()
657  * @buf: Buffer for returning the value
658  * @max_len: Maximum length of the buffer in bytes
659  * Returns: Number of bytes written to buf or -1 on error
660  *
661  * This function can be used to fetch "tls-unique" (RFC 5929, Section 3) which
662  * is the first TLS Finished message sent in the most recent TLS handshake of
663  * the TLS connection.
664  */
665 int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len);
666 
667 /**
668  * tls_connection_get_cipher_suite - Get current TLS cipher suite
669  * @conn: Connection context data from tls_connection_init()
670  * Returns: TLS cipher suite of the current connection or 0 on error
671  */
672 u16 tls_connection_get_cipher_suite(struct tls_connection *conn);
673 
674 /**
675  * tls_connection_get_peer_subject - Get peer subject
676  * @conn: Connection context data from tls_connection_init()
677  * Returns: Peer subject or %NULL if not authenticated or not available
678  */
679 const char * tls_connection_get_peer_subject(struct tls_connection *conn);
680 
681 /**
682  * tls_connection_get_own_cert_used - Was own certificate used
683  * @conn: Connection context data from tls_connection_init()
684  * Returns: true if own certificate was used during authentication
685  */
686 bool tls_connection_get_own_cert_used(struct tls_connection *conn);
687 
688 #endif /* TLS_H */
689