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