2 * WPA Supplicant / SSL/TLS interface definition
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 struct tls_connection;
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;
32 const char *opensc_engine_path;
33 const char *pkcs11_engine_path;
34 const char *pkcs11_module_path;
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
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
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
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)
69 * TLS connection parameters to be configured with tls_connection_set_params()
70 * and tls_global_set_params().
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
77 struct tls_connection_params {
79 const u8 *ca_cert_blob;
80 size_t ca_cert_blob_len;
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;
96 /* OpenSSL specific variables */
98 const char *engine_id;
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.
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.
116 void * tls_init(const struct tls_config *conf);
119 * tls_deinit - Deinitialize TLS library
120 * @tls_ctx: TLS context data from tls_init()
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.
128 void tls_deinit(void *tls_ctx);
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.
135 * Process all pending TLS errors.
137 int tls_get_errors(void *tls_ctx);
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
144 struct tls_connection * tls_connection_init(void *tls_ctx);
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()
151 * Release all resources allocated for TLS connection.
153 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
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.
161 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
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
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.
174 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
177 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
178 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
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.
193 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
194 const struct tls_connection_params *params);
197 * tls_global_set_params - Set TLS parameters for all TLS connection
198 * @tls_ctx: TLS context data from tls_init()
199 * @params: Global TLS parameters
200 * Returns: 0 on success, -1 on failure,
201 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
202 * PKCS#11 engine failure, or
203 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
204 * PKCS#11 engine private key.
206 int __must_check tls_global_set_params(
207 void *tls_ctx, const struct tls_connection_params *params);
210 * tls_global_set_verify - Set global certificate verification options
211 * @tls_ctx: TLS context data from tls_init()
212 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
213 * 2 = verify CRL for all certificates
214 * Returns: 0 on success, -1 on failure
216 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
219 * tls_connection_set_verify - Set certificate verification options
220 * @tls_ctx: TLS context data from tls_init()
221 * @conn: Connection context data from tls_connection_init()
222 * @verify_peer: 1 = verify peer certificate
223 * Returns: 0 on success, -1 on failure
225 int __must_check tls_connection_set_verify(void *tls_ctx,
226 struct tls_connection *conn,
230 * tls_connection_set_ia - Set TLS/IA parameters
231 * @tls_ctx: TLS context data from tls_init()
232 * @conn: Connection context data from tls_connection_init()
233 * @tls_ia: 1 = enable TLS/IA
234 * Returns: 0 on success, -1 on failure
236 * This function is used to configure TLS/IA in server mode where
237 * tls_connection_set_params() is not used.
239 int __must_check tls_connection_set_ia(void *tls_ctx,
240 struct tls_connection *conn,
244 * tls_connection_get_keys - Get master key and random data from TLS connection
245 * @tls_ctx: TLS context data from tls_init()
246 * @conn: Connection context data from tls_connection_init()
247 * @keys: Structure of key/random data (filled on success)
248 * Returns: 0 on success, -1 on failure
250 int __must_check tls_connection_get_keys(void *tls_ctx,
251 struct tls_connection *conn,
252 struct tls_keys *keys);
255 * tls_connection_prf - Use TLS-PRF to derive keying material
256 * @tls_ctx: TLS context data from tls_init()
257 * @conn: Connection context data from tls_connection_init()
258 * @label: Label (e.g., description of the key) for PRF
259 * @server_random_first: seed is 0 = client_random|server_random,
260 * 1 = server_random|client_random
261 * @out: Buffer for output data from TLS-PRF
262 * @out_len: Length of the output buffer
263 * Returns: 0 on success, -1 on failure
265 * This function is optional to implement if tls_connection_get_keys() provides
266 * access to master secret and server/client random values. If these values are
267 * not exported from the TLS library, tls_connection_prf() is required so that
268 * further keying material can be derived from the master secret. If not
269 * implemented, the function will still need to be defined, but it can just
270 * return -1. Example implementation of this function is in tls_prf() function
271 * when it is called with seed set to client_random|server_random (or
272 * server_random|client_random).
274 int __must_check tls_connection_prf(void *tls_ctx,
275 struct tls_connection *conn,
277 int server_random_first,
278 u8 *out, size_t out_len);
281 * tls_connection_handshake - Process TLS handshake (client side)
282 * @tls_ctx: TLS context data from tls_init()
283 * @conn: Connection context data from tls_connection_init()
284 * @in_data: Input data from TLS peer
285 * @in_len: Input data length
286 * @out_len: Length of the output buffer.
287 * @appl_data: Pointer to application data pointer, or %NULL if dropped
288 * @appl_data_len: Pointer to variable that is set to appl_data length
289 * Returns: Pointer to output data, %NULL on failure
291 * Caller is responsible for freeing returned output data. If the final
292 * handshake message includes application data, this is decrypted and
293 * appl_data (if not %NULL) is set to point this data. Caller is responsible
294 * for freeing appl_data.
296 * This function is used during TLS handshake. The first call is done with
297 * in_data == %NULL and the library is expected to return ClientHello packet.
298 * This packet is then send to the server and a response from server is given
299 * to TLS library by calling this function again with in_data pointing to the
300 * TLS message from the server.
302 * If the TLS handshake fails, this function may return %NULL. However, if the
303 * TLS library has a TLS alert to send out, that should be returned as the
304 * output data. In this case, tls_connection_get_failed() must return failure
307 * tls_connection_established() should return 1 once the TLS handshake has been
308 * completed successfully.
310 u8 * tls_connection_handshake(void *tls_ctx, struct tls_connection *conn,
311 const u8 *in_data, size_t in_len,
312 size_t *out_len, u8 **appl_data,
313 size_t *appl_data_len);
316 * tls_connection_server_handshake - Process TLS handshake (server side)
317 * @tls_ctx: TLS context data from tls_init()
318 * @conn: Connection context data from tls_connection_init()
319 * @in_data: Input data from TLS peer
320 * @in_len: Input data length
321 * @out_len: Length of the output buffer.
322 * Returns: pointer to output data, %NULL on failure
324 * Caller is responsible for freeing returned output data.
326 u8 * tls_connection_server_handshake(void *tls_ctx,
327 struct tls_connection *conn,
328 const u8 *in_data, size_t in_len,
332 * tls_connection_encrypt - Encrypt data into TLS tunnel
333 * @tls_ctx: TLS context data from tls_init()
334 * @conn: Connection context data from tls_connection_init()
335 * @in_data: Pointer to plaintext data to be encrypted
336 * @in_len: Input buffer length
337 * @out_data: Pointer to output buffer (encrypted TLS data)
338 * @out_len: Maximum out_data length
339 * Returns: Number of bytes written to out_data, -1 on failure
341 * This function is used after TLS handshake has been completed successfully to
342 * send data in the encrypted tunnel.
344 int __must_check tls_connection_encrypt(void *tls_ctx,
345 struct tls_connection *conn,
346 const u8 *in_data, size_t in_len,
347 u8 *out_data, size_t out_len);
350 * tls_connection_decrypt - Decrypt data from TLS tunnel
351 * @tls_ctx: TLS context data from tls_init()
352 * @conn: Connection context data from tls_connection_init()
353 * @in_data: Pointer to input buffer (encrypted TLS data)
354 * @in_len: Input buffer length
355 * @out_data: Pointer to output buffer (decrypted data from TLS tunnel)
356 * @out_len: Maximum out_data length
357 * Returns: Number of bytes written to out_data, -1 on failure
359 * This function is used after TLS handshake has been completed successfully to
360 * receive data from the encrypted tunnel.
362 int __must_check tls_connection_decrypt(void *tls_ctx,
363 struct tls_connection *conn,
364 const u8 *in_data, size_t in_len,
365 u8 *out_data, size_t out_len);
368 * tls_connection_resumed - Was session resumption used
369 * @tls_ctx: TLS context data from tls_init()
370 * @conn: Connection context data from tls_connection_init()
371 * Returns: 1 if current session used session resumption, 0 if not
373 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
377 TLS_CIPHER_RC4_SHA /* 0x0005 */,
378 TLS_CIPHER_AES128_SHA /* 0x002f */,
379 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
380 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
384 * tls_connection_set_cipher_list - Configure acceptable cipher suites
385 * @tls_ctx: TLS context data from tls_init()
386 * @conn: Connection context data from tls_connection_init()
387 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
389 * Returns: 0 on success, -1 on failure
391 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
392 struct tls_connection *conn,
396 * tls_get_cipher - Get current cipher name
397 * @tls_ctx: TLS context data from tls_init()
398 * @conn: Connection context data from tls_connection_init()
399 * @buf: Buffer for the cipher name
401 * Returns: 0 on success, -1 on failure
403 * Get the name of the currently used cipher.
405 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
406 char *buf, size_t buflen);
409 * tls_connection_enable_workaround - Enable TLS workaround options
410 * @tls_ctx: TLS context data from tls_init()
411 * @conn: Connection context data from tls_connection_init()
412 * Returns: 0 on success, -1 on failure
414 * This function is used to enable connection-specific workaround options for
415 * buffer SSL/TLS implementations.
417 int __must_check tls_connection_enable_workaround(void *tls_ctx,
418 struct tls_connection *conn);
421 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
422 * @tls_ctx: TLS context data from tls_init()
423 * @conn: Connection context data from tls_connection_init()
424 * @ext_type: Extension type
425 * @data: Extension payload (%NULL to remove extension)
426 * @data_len: Extension payload length
427 * Returns: 0 on success, -1 on failure
429 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
430 struct tls_connection *conn,
431 int ext_type, const u8 *data,
435 * tls_connection_get_failed - Get connection failure status
436 * @tls_ctx: TLS context data from tls_init()
437 * @conn: Connection context data from tls_connection_init()
439 * Returns >0 if connection has failed, 0 if not.
441 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
444 * tls_connection_get_read_alerts - Get connection read alert status
445 * @tls_ctx: TLS context data from tls_init()
446 * @conn: Connection context data from tls_connection_init()
447 * Returns: Number of times a fatal read (remote end reported error) has
448 * happened during this connection.
450 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
453 * tls_connection_get_write_alerts - Get connection write alert status
454 * @tls_ctx: TLS context data from tls_init()
455 * @conn: Connection context data from tls_connection_init()
456 * Returns: Number of times a fatal write (locally detected error) has happened
457 * during this connection.
459 int tls_connection_get_write_alerts(void *tls_ctx,
460 struct tls_connection *conn);
463 * tls_connection_get_keyblock_size - Get TLS key_block size
464 * @tls_ctx: TLS context data from tls_init()
465 * @conn: Connection context data from tls_connection_init()
466 * Returns: Size of the key_block for the negotiated cipher suite or -1 on
469 int tls_connection_get_keyblock_size(void *tls_ctx,
470 struct tls_connection *conn);
472 #define TLS_CAPABILITY_IA 0x0001 /* TLS Inner Application (TLS/IA) */
474 * tls_capabilities - Get supported TLS capabilities
475 * @tls_ctx: TLS context data from tls_init()
476 * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*)
478 unsigned int tls_capabilities(void *tls_ctx);
481 * tls_connection_ia_send_phase_finished - Send a TLS/IA PhaseFinished message
482 * @tls_ctx: TLS context data from tls_init()
483 * @conn: Connection context data from tls_connection_init()
484 * @final: 1 = FinalPhaseFinished, 0 = IntermediatePhaseFinished
485 * @out_data: Pointer to output buffer (encrypted TLS/IA data)
486 * @out_len: Maximum out_data length
487 * Returns: Number of bytes written to out_data on success, -1 on failure
489 * This function is used to send the TLS/IA end phase message, e.g., when the
490 * EAP server completes EAP-TTLSv1.
492 int __must_check tls_connection_ia_send_phase_finished(
493 void *tls_ctx, struct tls_connection *conn, int final,
494 u8 *out_data, size_t out_len);
497 * tls_connection_ia_final_phase_finished - Has final phase been completed
498 * @tls_ctx: TLS context data from tls_init()
499 * @conn: Connection context data from tls_connection_init()
500 * Returns: 1 if valid FinalPhaseFinished has been received, 0 if not, or -1
503 int __must_check tls_connection_ia_final_phase_finished(
504 void *tls_ctx, struct tls_connection *conn);
507 * tls_connection_ia_permute_inner_secret - Permute TLS/IA inner secret
508 * @tls_ctx: TLS context data from tls_init()
509 * @conn: Connection context data from tls_connection_init()
510 * @key: Session key material (session_key vectors with 2-octet length), or
511 * %NULL if no session key was generating in the current phase
512 * @key_len: Length of session key material
513 * Returns: 0 on success, -1 on failure
515 int __must_check tls_connection_ia_permute_inner_secret(
516 void *tls_ctx, struct tls_connection *conn,
517 const u8 *key, size_t key_len);
519 typedef int (*tls_session_ticket_cb)
520 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
521 const u8 *server_random, u8 *master_secret);
523 int __must_check tls_connection_set_session_ticket_cb(
524 void *tls_ctx, struct tls_connection *conn,
525 tls_session_ticket_cb cb, void *ctx);