2 * SSL/TLS interface definition
3 * Copyright (c) 2004-2009, 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 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
39 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
42 * struct tls_connection_params - Parameters for TLS connection
43 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
45 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
46 * @ca_cert_blob_len: ca_cert_blob length
47 * @ca_path: Path to CA certificates (OpenSSL specific)
48 * @subject_match: String to match in the subject of the peer certificate or
49 * %NULL to allow all subjects
50 * @altsubject_match: String to match in the alternative subject of the peer
51 * certificate or %NULL to allow all alternative subjects
52 * @client_cert: File or reference name for client X.509 certificate in PEM or
54 * @client_cert_blob: client_cert as inlined data or %NULL if not used
55 * @client_cert_blob_len: client_cert_blob length
56 * @private_key: File or reference name for client private key in PEM or DER
57 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
58 * @private_key_blob: private_key as inlined data or %NULL if not used
59 * @private_key_blob_len: private_key_blob length
60 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
62 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
63 * @dh_blob: dh_file as inlined data or %NULL if not used
64 * @dh_blob_len: dh_blob length
65 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
66 * (this is OpenSSL specific for now)
67 * @engine_id: engine id string (this is OpenSSL specific for now)
68 * @ppin: pointer to the pin variable in the configuration
69 * (this is OpenSSL specific for now)
70 * @key_id: the private key's id when using engine (this is OpenSSL
72 * @cert_id: the certificate's id when using engine
73 * @ca_cert_id: the CA certificate's id when using engine
74 * @tls_ia: Whether to enable TLS/IA (for EAP-TTLSv1)
75 * @flags: Parameter options (TLS_CONN_*)
77 * TLS connection parameters to be configured with tls_connection_set_params()
78 * and tls_global_set_params().
80 * Certificates and private key can be configured either as a reference name
81 * (file path or reference to certificate store) or by providing the same data
82 * as a pointer to the data in memory. Only one option will be used for each
85 struct tls_connection_params
{
87 const u8
*ca_cert_blob
;
88 size_t ca_cert_blob_len
;
90 const char *subject_match
;
91 const char *altsubject_match
;
92 const char *client_cert
;
93 const u8
*client_cert_blob
;
94 size_t client_cert_blob_len
;
95 const char *private_key
;
96 const u8
*private_key_blob
;
97 size_t private_key_blob_len
;
98 const char *private_key_passwd
;
104 /* OpenSSL specific variables */
106 const char *engine_id
;
110 const char *ca_cert_id
;
117 * tls_init - Initialize TLS library
118 * @conf: Configuration data for TLS library
119 * Returns: Context data to be used as tls_ctx in calls to other functions,
120 * or %NULL on failure.
122 * Called once during program startup and once for each RSN pre-authentication
123 * session. In other words, there can be two concurrent TLS contexts. If global
124 * library initialization is needed (i.e., one that is shared between both
125 * authentication types), the TLS library wrapper should maintain a reference
126 * counter and do global initialization only when moving from 0 to 1 reference.
128 void * tls_init(const struct tls_config
*conf
);
131 * tls_deinit - Deinitialize TLS library
132 * @tls_ctx: TLS context data from tls_init()
134 * Called once during program shutdown and once for each RSN pre-authentication
135 * session. If global library deinitialization is needed (i.e., one that is
136 * shared between both authentication types), the TLS library wrapper should
137 * maintain a reference counter and do global deinitialization only when moving
138 * from 1 to 0 references.
140 void tls_deinit(void *tls_ctx
);
143 * tls_get_errors - Process pending errors
144 * @tls_ctx: TLS context data from tls_init()
145 * Returns: Number of found error, 0 if no errors detected.
147 * Process all pending TLS errors.
149 int tls_get_errors(void *tls_ctx
);
152 * tls_connection_init - Initialize a new TLS connection
153 * @tls_ctx: TLS context data from tls_init()
154 * Returns: Connection context data, conn for other function calls
156 struct tls_connection
* tls_connection_init(void *tls_ctx
);
159 * tls_connection_deinit - Free TLS connection data
160 * @tls_ctx: TLS context data from tls_init()
161 * @conn: Connection context data from tls_connection_init()
163 * Release all resources allocated for TLS connection.
165 void tls_connection_deinit(void *tls_ctx
, struct tls_connection
*conn
);
168 * tls_connection_established - Has the TLS connection been completed?
169 * @tls_ctx: TLS context data from tls_init()
170 * @conn: Connection context data from tls_connection_init()
171 * Returns: 1 if TLS connection has been completed, 0 if not.
173 int tls_connection_established(void *tls_ctx
, struct tls_connection
*conn
);
176 * tls_connection_shutdown - Shutdown TLS connection
177 * @tls_ctx: TLS context data from tls_init()
178 * @conn: Connection context data from tls_connection_init()
179 * Returns: 0 on success, -1 on failure
181 * Shutdown current TLS connection without releasing all resources. New
182 * connection can be started by using the same conn without having to call
183 * tls_connection_init() or setting certificates etc. again. The new
184 * connection should try to use session resumption.
186 int tls_connection_shutdown(void *tls_ctx
, struct tls_connection
*conn
);
189 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
= -3,
190 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
= -2
194 * tls_connection_set_params - Set TLS connection parameters
195 * @tls_ctx: TLS context data from tls_init()
196 * @conn: Connection context data from tls_connection_init()
197 * @params: Connection parameters
198 * Returns: 0 on success, -1 on failure,
199 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
200 * PKCS#11 engine failure, or
201 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
202 * PKCS#11 engine private key.
205 tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
206 const struct tls_connection_params
*params
);
209 * tls_global_set_params - Set TLS parameters for all TLS connection
210 * @tls_ctx: TLS context data from tls_init()
211 * @params: Global TLS parameters
212 * Returns: 0 on success, -1 on failure,
213 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
214 * PKCS#11 engine failure, or
215 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
216 * PKCS#11 engine private key.
218 int __must_check
tls_global_set_params(
219 void *tls_ctx
, const struct tls_connection_params
*params
);
222 * tls_global_set_verify - Set global certificate verification options
223 * @tls_ctx: TLS context data from tls_init()
224 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
225 * 2 = verify CRL for all certificates
226 * Returns: 0 on success, -1 on failure
228 int __must_check
tls_global_set_verify(void *tls_ctx
, int check_crl
);
231 * tls_connection_set_verify - Set certificate verification options
232 * @tls_ctx: TLS context data from tls_init()
233 * @conn: Connection context data from tls_connection_init()
234 * @verify_peer: 1 = verify peer certificate
235 * Returns: 0 on success, -1 on failure
237 int __must_check
tls_connection_set_verify(void *tls_ctx
,
238 struct tls_connection
*conn
,
242 * tls_connection_set_ia - Set TLS/IA parameters
243 * @tls_ctx: TLS context data from tls_init()
244 * @conn: Connection context data from tls_connection_init()
245 * @tls_ia: 1 = enable TLS/IA
246 * Returns: 0 on success, -1 on failure
248 * This function is used to configure TLS/IA in server mode where
249 * tls_connection_set_params() is not used.
251 int __must_check
tls_connection_set_ia(void *tls_ctx
,
252 struct tls_connection
*conn
,
256 * tls_connection_get_keys - Get master key and random data from TLS connection
257 * @tls_ctx: TLS context data from tls_init()
258 * @conn: Connection context data from tls_connection_init()
259 * @keys: Structure of key/random data (filled on success)
260 * Returns: 0 on success, -1 on failure
262 int __must_check
tls_connection_get_keys(void *tls_ctx
,
263 struct tls_connection
*conn
,
264 struct tls_keys
*keys
);
267 * tls_connection_prf - Use TLS-PRF to derive keying material
268 * @tls_ctx: TLS context data from tls_init()
269 * @conn: Connection context data from tls_connection_init()
270 * @label: Label (e.g., description of the key) for PRF
271 * @server_random_first: seed is 0 = client_random|server_random,
272 * 1 = server_random|client_random
273 * @out: Buffer for output data from TLS-PRF
274 * @out_len: Length of the output buffer
275 * Returns: 0 on success, -1 on failure
277 * This function is optional to implement if tls_connection_get_keys() provides
278 * access to master secret and server/client random values. If these values are
279 * not exported from the TLS library, tls_connection_prf() is required so that
280 * further keying material can be derived from the master secret. If not
281 * implemented, the function will still need to be defined, but it can just
282 * return -1. Example implementation of this function is in tls_prf() function
283 * when it is called with seed set to client_random|server_random (or
284 * server_random|client_random).
286 int __must_check
tls_connection_prf(void *tls_ctx
,
287 struct tls_connection
*conn
,
289 int server_random_first
,
290 u8
*out
, size_t out_len
);
293 * tls_connection_handshake - Process TLS handshake (client side)
294 * @tls_ctx: TLS context data from tls_init()
295 * @conn: Connection context data from tls_connection_init()
296 * @in_data: Input data from TLS server
297 * @appl_data: Pointer to application data pointer, or %NULL if dropped
298 * Returns: Output data, %NULL on failure
300 * The caller is responsible for freeing the returned output data. If the final
301 * handshake message includes application data, this is decrypted and
302 * appl_data (if not %NULL) is set to point this data. The caller is
303 * responsible for freeing appl_data.
305 * This function is used during TLS handshake. The first call is done with
306 * in_data == %NULL and the library is expected to return ClientHello packet.
307 * This packet is then send to the server and a response from server is given
308 * to TLS library by calling this function again with in_data pointing to the
309 * TLS message from the server.
311 * If the TLS handshake fails, this function may return %NULL. However, if the
312 * TLS library has a TLS alert to send out, that should be returned as the
313 * output data. In this case, tls_connection_get_failed() must return failure
316 * tls_connection_established() should return 1 once the TLS handshake has been
317 * completed successfully.
319 struct wpabuf
* tls_connection_handshake(void *tls_ctx
,
320 struct tls_connection
*conn
,
321 const struct wpabuf
*in_data
,
322 struct wpabuf
**appl_data
);
325 * tls_connection_server_handshake - Process TLS handshake (server side)
326 * @tls_ctx: TLS context data from tls_init()
327 * @conn: Connection context data from tls_connection_init()
328 * @in_data: Input data from TLS peer
329 * @appl_data: Pointer to application data pointer, or %NULL if dropped
330 * Returns: Output data, %NULL on failure
332 * The caller is responsible for freeing the returned output data.
334 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
335 struct tls_connection
*conn
,
336 const struct wpabuf
*in_data
,
337 struct wpabuf
**appl_data
);
340 * tls_connection_encrypt - Encrypt data into TLS tunnel
341 * @tls_ctx: TLS context data from tls_init()
342 * @conn: Connection context data from tls_connection_init()
343 * @in_data: Plaintext data to be encrypted
344 * Returns: Encrypted TLS data or %NULL on failure
346 * This function is used after TLS handshake has been completed successfully to
347 * send data in the encrypted tunnel. The caller is responsible for freeing the
348 * returned output data.
350 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
351 struct tls_connection
*conn
,
352 const struct wpabuf
*in_data
);
355 * tls_connection_decrypt - Decrypt data from TLS tunnel
356 * @tls_ctx: TLS context data from tls_init()
357 * @conn: Connection context data from tls_connection_init()
358 * @in_data: Encrypted TLS data
359 * Returns: Decrypted TLS data or %NULL on failure
361 * This function is used after TLS handshake has been completed successfully to
362 * receive data from the encrypted tunnel. The caller is responsible for
363 * freeing the returned output data.
365 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
366 struct tls_connection
*conn
,
367 const struct wpabuf
*in_data
);
370 * tls_connection_resumed - Was session resumption used
371 * @tls_ctx: TLS context data from tls_init()
372 * @conn: Connection context data from tls_connection_init()
373 * Returns: 1 if current session used session resumption, 0 if not
375 int tls_connection_resumed(void *tls_ctx
, struct tls_connection
*conn
);
379 TLS_CIPHER_RC4_SHA
/* 0x0005 */,
380 TLS_CIPHER_AES128_SHA
/* 0x002f */,
381 TLS_CIPHER_RSA_DHE_AES128_SHA
/* 0x0031 */,
382 TLS_CIPHER_ANON_DH_AES128_SHA
/* 0x0034 */
386 * tls_connection_set_cipher_list - Configure acceptable cipher suites
387 * @tls_ctx: TLS context data from tls_init()
388 * @conn: Connection context data from tls_connection_init()
389 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
391 * Returns: 0 on success, -1 on failure
393 int __must_check
tls_connection_set_cipher_list(void *tls_ctx
,
394 struct tls_connection
*conn
,
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
403 * Returns: 0 on success, -1 on failure
405 * Get the name of the currently used cipher.
407 int __must_check
tls_get_cipher(void *tls_ctx
, struct tls_connection
*conn
,
408 char *buf
, size_t buflen
);
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
416 * This function is used to enable connection-specific workaround options for
417 * buffer SSL/TLS implementations.
419 int __must_check
tls_connection_enable_workaround(void *tls_ctx
,
420 struct tls_connection
*conn
);
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
431 int __must_check
tls_connection_client_hello_ext(void *tls_ctx
,
432 struct tls_connection
*conn
,
433 int ext_type
, const u8
*data
,
437 * tls_connection_get_failed - Get connection failure status
438 * @tls_ctx: TLS context data from tls_init()
439 * @conn: Connection context data from tls_connection_init()
441 * Returns >0 if connection has failed, 0 if not.
443 int tls_connection_get_failed(void *tls_ctx
, struct tls_connection
*conn
);
446 * tls_connection_get_read_alerts - Get connection read alert status
447 * @tls_ctx: TLS context data from tls_init()
448 * @conn: Connection context data from tls_connection_init()
449 * Returns: Number of times a fatal read (remote end reported error) has
450 * happened during this connection.
452 int tls_connection_get_read_alerts(void *tls_ctx
, struct tls_connection
*conn
);
455 * tls_connection_get_write_alerts - Get connection write 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 write (locally detected error) has happened
459 * during this connection.
461 int tls_connection_get_write_alerts(void *tls_ctx
,
462 struct tls_connection
*conn
);
465 * tls_connection_get_keyblock_size - Get TLS key_block size
466 * @tls_ctx: TLS context data from tls_init()
467 * @conn: Connection context data from tls_connection_init()
468 * Returns: Size of the key_block for the negotiated cipher suite or -1 on
471 int tls_connection_get_keyblock_size(void *tls_ctx
,
472 struct tls_connection
*conn
);
474 #define TLS_CAPABILITY_IA 0x0001 /* TLS Inner Application (TLS/IA) */
476 * tls_capabilities - Get supported TLS capabilities
477 * @tls_ctx: TLS context data from tls_init()
478 * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*)
480 unsigned int tls_capabilities(void *tls_ctx
);
483 * tls_connection_ia_send_phase_finished - Send a TLS/IA PhaseFinished message
484 * @tls_ctx: TLS context data from tls_init()
485 * @conn: Connection context data from tls_connection_init()
486 * @final: 1 = FinalPhaseFinished, 0 = IntermediatePhaseFinished
487 * Returns: Encrypted TLS/IA data, %NULL 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 struct wpabuf
* tls_connection_ia_send_phase_finished(
493 void *tls_ctx
, struct tls_connection
*conn
, int final
);
496 * tls_connection_ia_final_phase_finished - Has final phase been completed
497 * @tls_ctx: TLS context data from tls_init()
498 * @conn: Connection context data from tls_connection_init()
499 * Returns: 1 if valid FinalPhaseFinished has been received, 0 if not, or -1
502 int __must_check
tls_connection_ia_final_phase_finished(
503 void *tls_ctx
, struct tls_connection
*conn
);
506 * tls_connection_ia_permute_inner_secret - Permute TLS/IA inner secret
507 * @tls_ctx: TLS context data from tls_init()
508 * @conn: Connection context data from tls_connection_init()
509 * @key: Session key material (session_key vectors with 2-octet length), or
510 * %NULL if no session key was generating in the current phase
511 * @key_len: Length of session key material
512 * Returns: 0 on success, -1 on failure
514 int __must_check
tls_connection_ia_permute_inner_secret(
515 void *tls_ctx
, struct tls_connection
*conn
,
516 const u8
*key
, size_t key_len
);
518 typedef int (*tls_session_ticket_cb
)
519 (void *ctx
, const u8
*ticket
, size_t len
, const u8
*client_random
,
520 const u8
*server_random
, u8
*master_secret
);
522 int __must_check
tls_connection_set_session_ticket_cb(
523 void *tls_ctx
, struct tls_connection
*conn
,
524 tls_session_ticket_cb cb
, void *ctx
);