2 * SSL/TLS interface definition
3 * Copyright (c) 2004-2010, 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 TLS_CERT_CHAIN_FAILURE
,
37 * Note: These are used as identifier with external programs and as such, the
38 * values must not be changed.
40 enum tls_fail_reason
{
41 TLS_FAIL_UNSPECIFIED
= 0,
42 TLS_FAIL_UNTRUSTED
= 1,
44 TLS_FAIL_NOT_YET_VALID
= 3,
46 TLS_FAIL_SUBJECT_MISMATCH
= 5,
47 TLS_FAIL_ALTSUBJECT_MISMATCH
= 6,
48 TLS_FAIL_BAD_CERTIFICATE
= 7,
49 TLS_FAIL_SERVER_CHAIN_PROBE
= 8
52 union tls_event_data
{
56 enum tls_fail_reason reason
;
57 const char *reason_txt
;
58 const struct wpabuf
*cert
;
64 const struct wpabuf
*cert
;
71 const char *opensc_engine_path
;
72 const char *pkcs11_engine_path
;
73 const char *pkcs11_module_path
;
76 void (*event_cb
)(void *ctx
, enum tls_event ev
,
77 union tls_event_data
*data
);
81 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
82 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
85 * struct tls_connection_params - Parameters for TLS connection
86 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
88 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
89 * @ca_cert_blob_len: ca_cert_blob length
90 * @ca_path: Path to CA certificates (OpenSSL specific)
91 * @subject_match: String to match in the subject of the peer certificate or
92 * %NULL to allow all subjects
93 * @altsubject_match: String to match in the alternative subject of the peer
94 * certificate or %NULL to allow all alternative subjects
95 * @client_cert: File or reference name for client X.509 certificate in PEM or
97 * @client_cert_blob: client_cert as inlined data or %NULL if not used
98 * @client_cert_blob_len: client_cert_blob length
99 * @private_key: File or reference name for client private key in PEM or DER
100 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
101 * @private_key_blob: private_key as inlined data or %NULL if not used
102 * @private_key_blob_len: private_key_blob length
103 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
104 * passphrase is used.
105 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
106 * @dh_blob: dh_file as inlined data or %NULL if not used
107 * @dh_blob_len: dh_blob length
108 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
109 * (this is OpenSSL specific for now)
110 * @engine_id: engine id string (this is OpenSSL specific for now)
111 * @ppin: pointer to the pin variable in the configuration
112 * (this is OpenSSL specific for now)
113 * @key_id: the private key's id when using engine (this is OpenSSL
115 * @cert_id: the certificate's id when using engine
116 * @ca_cert_id: the CA certificate's id when using engine
117 * @tls_ia: Whether to enable TLS/IA (for EAP-TTLSv1)
118 * @flags: Parameter options (TLS_CONN_*)
120 * TLS connection parameters to be configured with tls_connection_set_params()
121 * and tls_global_set_params().
123 * Certificates and private key can be configured either as a reference name
124 * (file path or reference to certificate store) or by providing the same data
125 * as a pointer to the data in memory. Only one option will be used for each
128 struct tls_connection_params
{
130 const u8
*ca_cert_blob
;
131 size_t ca_cert_blob_len
;
133 const char *subject_match
;
134 const char *altsubject_match
;
135 const char *client_cert
;
136 const u8
*client_cert_blob
;
137 size_t client_cert_blob_len
;
138 const char *private_key
;
139 const u8
*private_key_blob
;
140 size_t private_key_blob_len
;
141 const char *private_key_passwd
;
147 /* OpenSSL specific variables */
149 const char *engine_id
;
153 const char *ca_cert_id
;
160 * tls_init - Initialize TLS library
161 * @conf: Configuration data for TLS library
162 * Returns: Context data to be used as tls_ctx in calls to other functions,
163 * or %NULL on failure.
165 * Called once during program startup and once for each RSN pre-authentication
166 * session. In other words, there can be two concurrent TLS contexts. If global
167 * library initialization is needed (i.e., one that is shared between both
168 * authentication types), the TLS library wrapper should maintain a reference
169 * counter and do global initialization only when moving from 0 to 1 reference.
171 void * tls_init(const struct tls_config
*conf
);
174 * tls_deinit - Deinitialize TLS library
175 * @tls_ctx: TLS context data from tls_init()
177 * Called once during program shutdown and once for each RSN pre-authentication
178 * session. If global library deinitialization is needed (i.e., one that is
179 * shared between both authentication types), the TLS library wrapper should
180 * maintain a reference counter and do global deinitialization only when moving
181 * from 1 to 0 references.
183 void tls_deinit(void *tls_ctx
);
186 * tls_get_errors - Process pending errors
187 * @tls_ctx: TLS context data from tls_init()
188 * Returns: Number of found error, 0 if no errors detected.
190 * Process all pending TLS errors.
192 int tls_get_errors(void *tls_ctx
);
195 * tls_connection_init - Initialize a new TLS connection
196 * @tls_ctx: TLS context data from tls_init()
197 * Returns: Connection context data, conn for other function calls
199 struct tls_connection
* tls_connection_init(void *tls_ctx
);
202 * tls_connection_deinit - Free TLS connection data
203 * @tls_ctx: TLS context data from tls_init()
204 * @conn: Connection context data from tls_connection_init()
206 * Release all resources allocated for TLS connection.
208 void tls_connection_deinit(void *tls_ctx
, struct tls_connection
*conn
);
211 * tls_connection_established - Has the TLS connection been completed?
212 * @tls_ctx: TLS context data from tls_init()
213 * @conn: Connection context data from tls_connection_init()
214 * Returns: 1 if TLS connection has been completed, 0 if not.
216 int tls_connection_established(void *tls_ctx
, struct tls_connection
*conn
);
219 * tls_connection_shutdown - Shutdown TLS connection
220 * @tls_ctx: TLS context data from tls_init()
221 * @conn: Connection context data from tls_connection_init()
222 * Returns: 0 on success, -1 on failure
224 * Shutdown current TLS connection without releasing all resources. New
225 * connection can be started by using the same conn without having to call
226 * tls_connection_init() or setting certificates etc. again. The new
227 * connection should try to use session resumption.
229 int tls_connection_shutdown(void *tls_ctx
, struct tls_connection
*conn
);
232 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
= -3,
233 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
= -2
237 * tls_connection_set_params - Set TLS connection parameters
238 * @tls_ctx: TLS context data from tls_init()
239 * @conn: Connection context data from tls_connection_init()
240 * @params: Connection parameters
241 * Returns: 0 on success, -1 on failure,
242 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
243 * PKCS#11 engine failure, or
244 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
245 * PKCS#11 engine private key.
248 tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
249 const struct tls_connection_params
*params
);
252 * tls_global_set_params - Set TLS parameters for all TLS connection
253 * @tls_ctx: TLS context data from tls_init()
254 * @params: Global TLS parameters
255 * Returns: 0 on success, -1 on failure,
256 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
257 * PKCS#11 engine failure, or
258 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
259 * PKCS#11 engine private key.
261 int __must_check
tls_global_set_params(
262 void *tls_ctx
, const struct tls_connection_params
*params
);
265 * tls_global_set_verify - Set global certificate verification options
266 * @tls_ctx: TLS context data from tls_init()
267 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
268 * 2 = verify CRL for all certificates
269 * Returns: 0 on success, -1 on failure
271 int __must_check
tls_global_set_verify(void *tls_ctx
, int check_crl
);
274 * tls_connection_set_verify - Set certificate verification options
275 * @tls_ctx: TLS context data from tls_init()
276 * @conn: Connection context data from tls_connection_init()
277 * @verify_peer: 1 = verify peer certificate
278 * Returns: 0 on success, -1 on failure
280 int __must_check
tls_connection_set_verify(void *tls_ctx
,
281 struct tls_connection
*conn
,
285 * tls_connection_set_ia - Set TLS/IA parameters
286 * @tls_ctx: TLS context data from tls_init()
287 * @conn: Connection context data from tls_connection_init()
288 * @tls_ia: 1 = enable TLS/IA
289 * Returns: 0 on success, -1 on failure
291 * This function is used to configure TLS/IA in server mode where
292 * tls_connection_set_params() is not used.
294 int __must_check
tls_connection_set_ia(void *tls_ctx
,
295 struct tls_connection
*conn
,
299 * tls_connection_get_keys - Get master key and random data from TLS connection
300 * @tls_ctx: TLS context data from tls_init()
301 * @conn: Connection context data from tls_connection_init()
302 * @keys: Structure of key/random data (filled on success)
303 * Returns: 0 on success, -1 on failure
305 int __must_check
tls_connection_get_keys(void *tls_ctx
,
306 struct tls_connection
*conn
,
307 struct tls_keys
*keys
);
310 * tls_connection_prf - Use TLS-PRF to derive keying material
311 * @tls_ctx: TLS context data from tls_init()
312 * @conn: Connection context data from tls_connection_init()
313 * @label: Label (e.g., description of the key) for PRF
314 * @server_random_first: seed is 0 = client_random|server_random,
315 * 1 = server_random|client_random
316 * @out: Buffer for output data from TLS-PRF
317 * @out_len: Length of the output buffer
318 * Returns: 0 on success, -1 on failure
320 * This function is optional to implement if tls_connection_get_keys() provides
321 * access to master secret and server/client random values. If these values are
322 * not exported from the TLS library, tls_connection_prf() is required so that
323 * further keying material can be derived from the master secret. If not
324 * implemented, the function will still need to be defined, but it can just
325 * return -1. Example implementation of this function is in tls_prf() function
326 * when it is called with seed set to client_random|server_random (or
327 * server_random|client_random).
329 int __must_check
tls_connection_prf(void *tls_ctx
,
330 struct tls_connection
*conn
,
332 int server_random_first
,
333 u8
*out
, size_t out_len
);
336 * tls_connection_handshake - Process TLS handshake (client side)
337 * @tls_ctx: TLS context data from tls_init()
338 * @conn: Connection context data from tls_connection_init()
339 * @in_data: Input data from TLS server
340 * @appl_data: Pointer to application data pointer, or %NULL if dropped
341 * Returns: Output data, %NULL on failure
343 * The caller is responsible for freeing the returned output data. If the final
344 * handshake message includes application data, this is decrypted and
345 * appl_data (if not %NULL) is set to point this data. The caller is
346 * responsible for freeing appl_data.
348 * This function is used during TLS handshake. The first call is done with
349 * in_data == %NULL and the library is expected to return ClientHello packet.
350 * This packet is then send to the server and a response from server is given
351 * to TLS library by calling this function again with in_data pointing to the
352 * TLS message from the server.
354 * If the TLS handshake fails, this function may return %NULL. However, if the
355 * TLS library has a TLS alert to send out, that should be returned as the
356 * output data. In this case, tls_connection_get_failed() must return failure
359 * tls_connection_established() should return 1 once the TLS handshake has been
360 * completed successfully.
362 struct wpabuf
* tls_connection_handshake(void *tls_ctx
,
363 struct tls_connection
*conn
,
364 const struct wpabuf
*in_data
,
365 struct wpabuf
**appl_data
);
368 * tls_connection_server_handshake - Process TLS handshake (server side)
369 * @tls_ctx: TLS context data from tls_init()
370 * @conn: Connection context data from tls_connection_init()
371 * @in_data: Input data from TLS peer
372 * @appl_data: Pointer to application data pointer, or %NULL if dropped
373 * Returns: Output data, %NULL on failure
375 * The caller is responsible for freeing the returned output data.
377 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
378 struct tls_connection
*conn
,
379 const struct wpabuf
*in_data
,
380 struct wpabuf
**appl_data
);
383 * tls_connection_encrypt - Encrypt data into TLS tunnel
384 * @tls_ctx: TLS context data from tls_init()
385 * @conn: Connection context data from tls_connection_init()
386 * @in_data: Plaintext data to be encrypted
387 * Returns: Encrypted TLS data or %NULL on failure
389 * This function is used after TLS handshake has been completed successfully to
390 * send data in the encrypted tunnel. The caller is responsible for freeing the
391 * returned output data.
393 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
394 struct tls_connection
*conn
,
395 const struct wpabuf
*in_data
);
398 * tls_connection_decrypt - Decrypt data from TLS tunnel
399 * @tls_ctx: TLS context data from tls_init()
400 * @conn: Connection context data from tls_connection_init()
401 * @in_data: Encrypted TLS data
402 * Returns: Decrypted TLS data or %NULL on failure
404 * This function is used after TLS handshake has been completed successfully to
405 * receive data from the encrypted tunnel. The caller is responsible for
406 * freeing the returned output data.
408 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
409 struct tls_connection
*conn
,
410 const struct wpabuf
*in_data
);
413 * tls_connection_resumed - Was session resumption used
414 * @tls_ctx: TLS context data from tls_init()
415 * @conn: Connection context data from tls_connection_init()
416 * Returns: 1 if current session used session resumption, 0 if not
418 int tls_connection_resumed(void *tls_ctx
, struct tls_connection
*conn
);
422 TLS_CIPHER_RC4_SHA
/* 0x0005 */,
423 TLS_CIPHER_AES128_SHA
/* 0x002f */,
424 TLS_CIPHER_RSA_DHE_AES128_SHA
/* 0x0031 */,
425 TLS_CIPHER_ANON_DH_AES128_SHA
/* 0x0034 */
429 * tls_connection_set_cipher_list - Configure acceptable cipher suites
430 * @tls_ctx: TLS context data from tls_init()
431 * @conn: Connection context data from tls_connection_init()
432 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
434 * Returns: 0 on success, -1 on failure
436 int __must_check
tls_connection_set_cipher_list(void *tls_ctx
,
437 struct tls_connection
*conn
,
441 * tls_get_cipher - Get current cipher name
442 * @tls_ctx: TLS context data from tls_init()
443 * @conn: Connection context data from tls_connection_init()
444 * @buf: Buffer for the cipher name
446 * Returns: 0 on success, -1 on failure
448 * Get the name of the currently used cipher.
450 int __must_check
tls_get_cipher(void *tls_ctx
, struct tls_connection
*conn
,
451 char *buf
, size_t buflen
);
454 * tls_connection_enable_workaround - Enable TLS workaround options
455 * @tls_ctx: TLS context data from tls_init()
456 * @conn: Connection context data from tls_connection_init()
457 * Returns: 0 on success, -1 on failure
459 * This function is used to enable connection-specific workaround options for
460 * buffer SSL/TLS implementations.
462 int __must_check
tls_connection_enable_workaround(void *tls_ctx
,
463 struct tls_connection
*conn
);
466 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
467 * @tls_ctx: TLS context data from tls_init()
468 * @conn: Connection context data from tls_connection_init()
469 * @ext_type: Extension type
470 * @data: Extension payload (%NULL to remove extension)
471 * @data_len: Extension payload length
472 * Returns: 0 on success, -1 on failure
474 int __must_check
tls_connection_client_hello_ext(void *tls_ctx
,
475 struct tls_connection
*conn
,
476 int ext_type
, const u8
*data
,
480 * tls_connection_get_failed - Get connection failure status
481 * @tls_ctx: TLS context data from tls_init()
482 * @conn: Connection context data from tls_connection_init()
484 * Returns >0 if connection has failed, 0 if not.
486 int tls_connection_get_failed(void *tls_ctx
, struct tls_connection
*conn
);
489 * tls_connection_get_read_alerts - Get connection read alert status
490 * @tls_ctx: TLS context data from tls_init()
491 * @conn: Connection context data from tls_connection_init()
492 * Returns: Number of times a fatal read (remote end reported error) has
493 * happened during this connection.
495 int tls_connection_get_read_alerts(void *tls_ctx
, struct tls_connection
*conn
);
498 * tls_connection_get_write_alerts - Get connection write alert status
499 * @tls_ctx: TLS context data from tls_init()
500 * @conn: Connection context data from tls_connection_init()
501 * Returns: Number of times a fatal write (locally detected error) has happened
502 * during this connection.
504 int tls_connection_get_write_alerts(void *tls_ctx
,
505 struct tls_connection
*conn
);
508 * tls_connection_get_keyblock_size - Get TLS key_block size
509 * @tls_ctx: TLS context data from tls_init()
510 * @conn: Connection context data from tls_connection_init()
511 * Returns: Size of the key_block for the negotiated cipher suite or -1 on
514 int tls_connection_get_keyblock_size(void *tls_ctx
,
515 struct tls_connection
*conn
);
517 #define TLS_CAPABILITY_IA 0x0001 /* TLS Inner Application (TLS/IA) */
519 * tls_capabilities - Get supported TLS capabilities
520 * @tls_ctx: TLS context data from tls_init()
521 * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*)
523 unsigned int tls_capabilities(void *tls_ctx
);
526 * tls_connection_ia_send_phase_finished - Send a TLS/IA PhaseFinished message
527 * @tls_ctx: TLS context data from tls_init()
528 * @conn: Connection context data from tls_connection_init()
529 * @final: 1 = FinalPhaseFinished, 0 = IntermediatePhaseFinished
530 * Returns: Encrypted TLS/IA data, %NULL on failure
532 * This function is used to send the TLS/IA end phase message, e.g., when the
533 * EAP server completes EAP-TTLSv1.
535 struct wpabuf
* tls_connection_ia_send_phase_finished(
536 void *tls_ctx
, struct tls_connection
*conn
, int final
);
539 * tls_connection_ia_final_phase_finished - Has final phase been completed
540 * @tls_ctx: TLS context data from tls_init()
541 * @conn: Connection context data from tls_connection_init()
542 * Returns: 1 if valid FinalPhaseFinished has been received, 0 if not, or -1
545 int __must_check
tls_connection_ia_final_phase_finished(
546 void *tls_ctx
, struct tls_connection
*conn
);
549 * tls_connection_ia_permute_inner_secret - Permute TLS/IA inner secret
550 * @tls_ctx: TLS context data from tls_init()
551 * @conn: Connection context data from tls_connection_init()
552 * @key: Session key material (session_key vectors with 2-octet length), or
553 * %NULL if no session key was generating in the current phase
554 * @key_len: Length of session key material
555 * Returns: 0 on success, -1 on failure
557 int __must_check
tls_connection_ia_permute_inner_secret(
558 void *tls_ctx
, struct tls_connection
*conn
,
559 const u8
*key
, size_t key_len
);
561 typedef int (*tls_session_ticket_cb
)
562 (void *ctx
, const u8
*ticket
, size_t len
, const u8
*client_random
,
563 const u8
*server_random
, u8
*master_secret
);
565 int __must_check
tls_connection_set_session_ticket_cb(
566 void *tls_ctx
, struct tls_connection
*conn
,
567 tls_session_ticket_cb cb
, void *ctx
);