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