xref: /freebsd/contrib/wpa/src/crypto/tls.h (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
1 /*
2  * WPA Supplicant / SSL/TLS interface definition
3  * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #ifndef TLS_H
16 #define TLS_H
17 
18 struct tls_connection;
19 
20 struct tls_keys {
21 	const u8 *master_key; /* TLS master secret */
22 	size_t master_key_len;
23 	const u8 *client_random;
24 	size_t client_random_len;
25 	const u8 *server_random;
26 	size_t server_random_len;
27 	const u8 *inner_secret; /* TLS/IA inner secret */
28 	size_t inner_secret_len;
29 };
30 
31 struct tls_config {
32 	const char *opensc_engine_path;
33 	const char *pkcs11_engine_path;
34 	const char *pkcs11_module_path;
35 };
36 
37 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
38 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
39 
40 /**
41  * struct tls_connection_params - Parameters for TLS connection
42  * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
43  * format
44  * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
45  * @ca_cert_blob_len: ca_cert_blob length
46  * @ca_path: Path to CA certificates (OpenSSL specific)
47  * @subject_match: String to match in the subject of the peer certificate or
48  * %NULL to allow all subjects
49  * @altsubject_match: String to match in the alternative subject of the peer
50  * certificate or %NULL to allow all alternative subjects
51  * @client_cert: File or reference name for client X.509 certificate in PEM or
52  * DER format
53  * @client_cert_blob: client_cert as inlined data or %NULL if not used
54  * @client_cert_blob_len: client_cert_blob length
55  * @private_key: File or reference name for client private key in PEM or DER
56  * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
57  * @private_key_blob: private_key as inlined data or %NULL if not used
58  * @private_key_blob_len: private_key_blob length
59  * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
60  * passphrase is used.
61  * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
62  * @dh_blob: dh_file as inlined data or %NULL if not used
63  * @dh_blob_len: dh_blob length
64  * @engine: 1 = use engine (e.g., a smartcard) for private key operations
65  * (this is OpenSSL specific for now)
66  * @engine_id: engine id string (this is OpenSSL specific for now)
67  * @ppin: pointer to the pin variable in the configuration
68  * (this is OpenSSL specific for now)
69  * @key_id: the private key's id when using engine (this is OpenSSL
70  * specific for now)
71  * @cert_id: the certificate's id when using engine
72  * @ca_cert_id: the CA certificate's id when using engine
73  * @tls_ia: Whether to enable TLS/IA (for EAP-TTLSv1)
74  * @flags: Parameter options (TLS_CONN_*)
75  *
76  * TLS connection parameters to be configured with tls_connection_set_params()
77  * and tls_global_set_params().
78  *
79  * Certificates and private key can be configured either as a reference name
80  * (file path or reference to certificate store) or by providing the same data
81  * as a pointer to the data in memory. Only one option will be used for each
82  * field.
83  */
84 struct tls_connection_params {
85 	const char *ca_cert;
86 	const u8 *ca_cert_blob;
87 	size_t ca_cert_blob_len;
88 	const char *ca_path;
89 	const char *subject_match;
90 	const char *altsubject_match;
91 	const char *client_cert;
92 	const u8 *client_cert_blob;
93 	size_t client_cert_blob_len;
94 	const char *private_key;
95 	const u8 *private_key_blob;
96 	size_t private_key_blob_len;
97 	const char *private_key_passwd;
98 	const char *dh_file;
99 	const u8 *dh_blob;
100 	size_t dh_blob_len;
101 	int tls_ia;
102 
103 	/* OpenSSL specific variables */
104 	int engine;
105 	const char *engine_id;
106 	const char *pin;
107 	const char *key_id;
108 	const char *cert_id;
109 	const char *ca_cert_id;
110 
111 	unsigned int flags;
112 };
113 
114 
115 /**
116  * tls_init - Initialize TLS library
117  * @conf: Configuration data for TLS library
118  * Returns: Context data to be used as tls_ctx in calls to other functions,
119  * or %NULL on failure.
120  *
121  * Called once during program startup and once for each RSN pre-authentication
122  * session. In other words, there can be two concurrent TLS contexts. If global
123  * library initialization is needed (i.e., one that is shared between both
124  * authentication types), the TLS library wrapper should maintain a reference
125  * counter and do global initialization only when moving from 0 to 1 reference.
126  */
127 void * tls_init(const struct tls_config *conf);
128 
129 /**
130  * tls_deinit - Deinitialize TLS library
131  * @tls_ctx: TLS context data from tls_init()
132  *
133  * Called once during program shutdown and once for each RSN pre-authentication
134  * session. If global library deinitialization is needed (i.e., one that is
135  * shared between both authentication types), the TLS library wrapper should
136  * maintain a reference counter and do global deinitialization only when moving
137  * from 1 to 0 references.
138  */
139 void tls_deinit(void *tls_ctx);
140 
141 /**
142  * tls_get_errors - Process pending errors
143  * @tls_ctx: TLS context data from tls_init()
144  * Returns: Number of found error, 0 if no errors detected.
145  *
146  * Process all pending TLS errors.
147  */
148 int tls_get_errors(void *tls_ctx);
149 
150 /**
151  * tls_connection_init - Initialize a new TLS connection
152  * @tls_ctx: TLS context data from tls_init()
153  * Returns: Connection context data, conn for other function calls
154  */
155 struct tls_connection * tls_connection_init(void *tls_ctx);
156 
157 /**
158  * tls_connection_deinit - Free TLS connection data
159  * @tls_ctx: TLS context data from tls_init()
160  * @conn: Connection context data from tls_connection_init()
161  *
162  * Release all resources allocated for TLS connection.
163  */
164 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
165 
166 /**
167  * tls_connection_established - Has the TLS connection been completed?
168  * @tls_ctx: TLS context data from tls_init()
169  * @conn: Connection context data from tls_connection_init()
170  * Returns: 1 if TLS connection has been completed, 0 if not.
171  */
172 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
173 
174 /**
175  * tls_connection_shutdown - Shutdown TLS connection
176  * @tls_ctx: TLS context data from tls_init()
177  * @conn: Connection context data from tls_connection_init()
178  * Returns: 0 on success, -1 on failure
179  *
180  * Shutdown current TLS connection without releasing all resources. New
181  * connection can be started by using the same conn without having to call
182  * tls_connection_init() or setting certificates etc. again. The new
183  * connection should try to use session resumption.
184  */
185 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
186 
187 enum {
188 	TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
189 	TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
190 };
191 
192 /**
193  * tls_connection_set_params - Set TLS connection parameters
194  * @tls_ctx: TLS context data from tls_init()
195  * @conn: Connection context data from tls_connection_init()
196  * @params: Connection parameters
197  * Returns: 0 on success, -1 on failure,
198  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
199  * PKCS#11 engine failure, or
200  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
201  * PKCS#11 engine private key.
202  */
203 int __must_check
204 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
205 			  const struct tls_connection_params *params);
206 
207 /**
208  * tls_global_set_params - Set TLS parameters for all TLS connection
209  * @tls_ctx: TLS context data from tls_init()
210  * @params: Global TLS parameters
211  * Returns: 0 on success, -1 on failure,
212  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
213  * PKCS#11 engine failure, or
214  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
215  * PKCS#11 engine private key.
216  */
217 int __must_check tls_global_set_params(
218 	void *tls_ctx, const struct tls_connection_params *params);
219 
220 /**
221  * tls_global_set_verify - Set global certificate verification options
222  * @tls_ctx: TLS context data from tls_init()
223  * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
224  * 2 = verify CRL for all certificates
225  * Returns: 0 on success, -1 on failure
226  */
227 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
228 
229 /**
230  * tls_connection_set_verify - Set certificate verification options
231  * @tls_ctx: TLS context data from tls_init()
232  * @conn: Connection context data from tls_connection_init()
233  * @verify_peer: 1 = verify peer certificate
234  * Returns: 0 on success, -1 on failure
235  */
236 int __must_check tls_connection_set_verify(void *tls_ctx,
237 					   struct tls_connection *conn,
238 					   int verify_peer);
239 
240 /**
241  * tls_connection_set_ia - Set TLS/IA parameters
242  * @tls_ctx: TLS context data from tls_init()
243  * @conn: Connection context data from tls_connection_init()
244  * @tls_ia: 1 = enable TLS/IA
245  * Returns: 0 on success, -1 on failure
246  *
247  * This function is used to configure TLS/IA in server mode where
248  * tls_connection_set_params() is not used.
249  */
250 int __must_check tls_connection_set_ia(void *tls_ctx,
251 				       struct tls_connection *conn,
252 				       int tls_ia);
253 
254 /**
255  * tls_connection_get_keys - Get master key and random data from TLS connection
256  * @tls_ctx: TLS context data from tls_init()
257  * @conn: Connection context data from tls_connection_init()
258  * @keys: Structure of key/random data (filled on success)
259  * Returns: 0 on success, -1 on failure
260  */
261 int __must_check tls_connection_get_keys(void *tls_ctx,
262 					 struct tls_connection *conn,
263 					 struct tls_keys *keys);
264 
265 /**
266  * tls_connection_prf - Use TLS-PRF to derive keying material
267  * @tls_ctx: TLS context data from tls_init()
268  * @conn: Connection context data from tls_connection_init()
269  * @label: Label (e.g., description of the key) for PRF
270  * @server_random_first: seed is 0 = client_random|server_random,
271  * 1 = server_random|client_random
272  * @out: Buffer for output data from TLS-PRF
273  * @out_len: Length of the output buffer
274  * Returns: 0 on success, -1 on failure
275  *
276  * This function is optional to implement if tls_connection_get_keys() provides
277  * access to master secret and server/client random values. If these values are
278  * not exported from the TLS library, tls_connection_prf() is required so that
279  * further keying material can be derived from the master secret. If not
280  * implemented, the function will still need to be defined, but it can just
281  * return -1. Example implementation of this function is in tls_prf() function
282  * when it is called with seed set to client_random|server_random (or
283  * server_random|client_random).
284  */
285 int __must_check  tls_connection_prf(void *tls_ctx,
286 				     struct tls_connection *conn,
287 				     const char *label,
288 				     int server_random_first,
289 				     u8 *out, size_t out_len);
290 
291 /**
292  * tls_connection_handshake - Process TLS handshake (client side)
293  * @tls_ctx: TLS context data from tls_init()
294  * @conn: Connection context data from tls_connection_init()
295  * @in_data: Input data from TLS peer
296  * @in_len: Input data length
297  * @out_len: Length of the output buffer.
298  * @appl_data: Pointer to application data pointer, or %NULL if dropped
299  * @appl_data_len: Pointer to variable that is set to appl_data length
300  * Returns: Pointer to output data, %NULL on failure
301  *
302  * Caller is responsible for freeing returned output data. If the final
303  * handshake message includes application data, this is decrypted and
304  * appl_data (if not %NULL) is set to point this data. Caller is responsible
305  * for freeing appl_data.
306  *
307  * This function is used during TLS handshake. The first call is done with
308  * in_data == %NULL and the library is expected to return ClientHello packet.
309  * This packet is then send to the server and a response from server is given
310  * to TLS library by calling this function again with in_data pointing to the
311  * TLS message from the server.
312  *
313  * If the TLS handshake fails, this function may return %NULL. However, if the
314  * TLS library has a TLS alert to send out, that should be returned as the
315  * output data. In this case, tls_connection_get_failed() must return failure
316  * (> 0).
317  *
318  * tls_connection_established() should return 1 once the TLS handshake has been
319  * completed successfully.
320  */
321 u8 * tls_connection_handshake(void *tls_ctx, struct tls_connection *conn,
322 			      const u8 *in_data, size_t in_len,
323 			      size_t *out_len, u8 **appl_data,
324 			      size_t *appl_data_len);
325 
326 /**
327  * tls_connection_server_handshake - Process TLS handshake (server side)
328  * @tls_ctx: TLS context data from tls_init()
329  * @conn: Connection context data from tls_connection_init()
330  * @in_data: Input data from TLS peer
331  * @in_len: Input data length
332  * @out_len: Length of the output buffer.
333  * Returns: pointer to output data, %NULL on failure
334  *
335  * Caller is responsible for freeing returned output data.
336  */
337 u8 * tls_connection_server_handshake(void *tls_ctx,
338 				     struct tls_connection *conn,
339 				     const u8 *in_data, size_t in_len,
340 				     size_t *out_len);
341 
342 /**
343  * tls_connection_encrypt - Encrypt data into TLS tunnel
344  * @tls_ctx: TLS context data from tls_init()
345  * @conn: Connection context data from tls_connection_init()
346  * @in_data: Pointer to plaintext data to be encrypted
347  * @in_len: Input buffer length
348  * @out_data: Pointer to output buffer (encrypted TLS data)
349  * @out_len: Maximum out_data length
350  * Returns: Number of bytes written to out_data, -1 on failure
351  *
352  * This function is used after TLS handshake has been completed successfully to
353  * send data in the encrypted tunnel.
354  */
355 int __must_check tls_connection_encrypt(void *tls_ctx,
356 					struct tls_connection *conn,
357 					const u8 *in_data, size_t in_len,
358 					u8 *out_data, size_t out_len);
359 
360 /**
361  * tls_connection_decrypt - Decrypt data from TLS tunnel
362  * @tls_ctx: TLS context data from tls_init()
363  * @conn: Connection context data from tls_connection_init()
364  * @in_data: Pointer to input buffer (encrypted TLS data)
365  * @in_len: Input buffer length
366  * @out_data: Pointer to output buffer (decrypted data from TLS tunnel)
367  * @out_len: Maximum out_data length
368  * Returns: Number of bytes written to out_data, -1 on failure
369  *
370  * This function is used after TLS handshake has been completed successfully to
371  * receive data from the encrypted tunnel.
372  */
373 int __must_check tls_connection_decrypt(void *tls_ctx,
374 					struct tls_connection *conn,
375 					const u8 *in_data, size_t in_len,
376 					u8 *out_data, size_t out_len);
377 
378 /**
379  * tls_connection_resumed - Was session resumption used
380  * @tls_ctx: TLS context data from tls_init()
381  * @conn: Connection context data from tls_connection_init()
382  * Returns: 1 if current session used session resumption, 0 if not
383  */
384 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
385 
386 enum {
387 	TLS_CIPHER_NONE,
388 	TLS_CIPHER_RC4_SHA /* 0x0005 */,
389 	TLS_CIPHER_AES128_SHA /* 0x002f */,
390 	TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
391 	TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
392 };
393 
394 /**
395  * tls_connection_set_cipher_list - Configure acceptable cipher suites
396  * @tls_ctx: TLS context data from tls_init()
397  * @conn: Connection context data from tls_connection_init()
398  * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
399  * (TLS_CIPHER_*).
400  * Returns: 0 on success, -1 on failure
401  */
402 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
403 						struct tls_connection *conn,
404 						u8 *ciphers);
405 
406 /**
407  * tls_get_cipher - Get current cipher name
408  * @tls_ctx: TLS context data from tls_init()
409  * @conn: Connection context data from tls_connection_init()
410  * @buf: Buffer for the cipher name
411  * @buflen: buf size
412  * Returns: 0 on success, -1 on failure
413  *
414  * Get the name of the currently used cipher.
415  */
416 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
417 				char *buf, size_t buflen);
418 
419 /**
420  * tls_connection_enable_workaround - Enable TLS workaround options
421  * @tls_ctx: TLS context data from tls_init()
422  * @conn: Connection context data from tls_connection_init()
423  * Returns: 0 on success, -1 on failure
424  *
425  * This function is used to enable connection-specific workaround options for
426  * buffer SSL/TLS implementations.
427  */
428 int __must_check tls_connection_enable_workaround(void *tls_ctx,
429 						  struct tls_connection *conn);
430 
431 /**
432  * tls_connection_client_hello_ext - Set TLS extension for ClientHello
433  * @tls_ctx: TLS context data from tls_init()
434  * @conn: Connection context data from tls_connection_init()
435  * @ext_type: Extension type
436  * @data: Extension payload (%NULL to remove extension)
437  * @data_len: Extension payload length
438  * Returns: 0 on success, -1 on failure
439  */
440 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
441 						 struct tls_connection *conn,
442 						 int ext_type, const u8 *data,
443 						 size_t data_len);
444 
445 /**
446  * tls_connection_get_failed - Get connection failure status
447  * @tls_ctx: TLS context data from tls_init()
448  * @conn: Connection context data from tls_connection_init()
449  *
450  * Returns >0 if connection has failed, 0 if not.
451  */
452 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
453 
454 /**
455  * tls_connection_get_read_alerts - Get connection read alert status
456  * @tls_ctx: TLS context data from tls_init()
457  * @conn: Connection context data from tls_connection_init()
458  * Returns: Number of times a fatal read (remote end reported error) has
459  * happened during this connection.
460  */
461 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
462 
463 /**
464  * tls_connection_get_write_alerts - Get connection write alert status
465  * @tls_ctx: TLS context data from tls_init()
466  * @conn: Connection context data from tls_connection_init()
467  * Returns: Number of times a fatal write (locally detected error) has happened
468  * during this connection.
469  */
470 int tls_connection_get_write_alerts(void *tls_ctx,
471 				    struct tls_connection *conn);
472 
473 /**
474  * tls_connection_get_keyblock_size - Get TLS key_block size
475  * @tls_ctx: TLS context data from tls_init()
476  * @conn: Connection context data from tls_connection_init()
477  * Returns: Size of the key_block for the negotiated cipher suite or -1 on
478  * failure
479  */
480 int tls_connection_get_keyblock_size(void *tls_ctx,
481 				     struct tls_connection *conn);
482 
483 #define TLS_CAPABILITY_IA 0x0001 /* TLS Inner Application (TLS/IA) */
484 /**
485  * tls_capabilities - Get supported TLS capabilities
486  * @tls_ctx: TLS context data from tls_init()
487  * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*)
488  */
489 unsigned int tls_capabilities(void *tls_ctx);
490 
491 /**
492  * tls_connection_ia_send_phase_finished - Send a TLS/IA PhaseFinished message
493  * @tls_ctx: TLS context data from tls_init()
494  * @conn: Connection context data from tls_connection_init()
495  * @final: 1 = FinalPhaseFinished, 0 = IntermediatePhaseFinished
496  * @out_data: Pointer to output buffer (encrypted TLS/IA data)
497  * @out_len: Maximum out_data length
498  * Returns: Number of bytes written to out_data on success, -1 on failure
499  *
500  * This function is used to send the TLS/IA end phase message, e.g., when the
501  * EAP server completes EAP-TTLSv1.
502  */
503 int __must_check tls_connection_ia_send_phase_finished(
504 	void *tls_ctx, struct tls_connection *conn, int final,
505 	u8 *out_data, size_t out_len);
506 
507 /**
508  * tls_connection_ia_final_phase_finished - Has final phase been completed
509  * @tls_ctx: TLS context data from tls_init()
510  * @conn: Connection context data from tls_connection_init()
511  * Returns: 1 if valid FinalPhaseFinished has been received, 0 if not, or -1
512  * on failure
513  */
514 int __must_check tls_connection_ia_final_phase_finished(
515 	void *tls_ctx, struct tls_connection *conn);
516 
517 /**
518  * tls_connection_ia_permute_inner_secret - Permute TLS/IA inner secret
519  * @tls_ctx: TLS context data from tls_init()
520  * @conn: Connection context data from tls_connection_init()
521  * @key: Session key material (session_key vectors with 2-octet length), or
522  * %NULL if no session key was generating in the current phase
523  * @key_len: Length of session key material
524  * Returns: 0 on success, -1 on failure
525  */
526 int __must_check tls_connection_ia_permute_inner_secret(
527 	void *tls_ctx, struct tls_connection *conn,
528 	const u8 *key, size_t key_len);
529 
530 typedef int (*tls_session_ticket_cb)
531 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
532  const u8 *server_random, u8 *master_secret);
533 
534 int __must_check  tls_connection_set_session_ticket_cb(
535 	void *tls_ctx, struct tls_connection *conn,
536 	tls_session_ticket_cb cb, void *ctx);
537 
538 #endif /* TLS_H */
539