2 * WPA Supplicant / SSL/TLS interface definition
3 * Copyright (c) 2004-2006, Jouni Malinen <jkmaline@cc.hut.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
;
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
;
29 * If TLS library does not provide access to master_key, but only to
30 * EAP key block, this pointer can be set to point to the result of
31 * PRF(master_secret, "client EAP encryption",
32 * client_random + server_random).
34 const u8
*eap_tls_prf
;
35 size_t eap_tls_prf_len
;
39 const char *opensc_engine_path
;
40 const char *pkcs11_engine_path
;
41 const char *pkcs11_module_path
;
45 * struct tls_connection_params - Parameters for TLS connection
46 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
48 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
49 * @ca_cert_blob_len: ca_cert_blob length
50 * @ca_path: Path to CA certificates (OpenSSL specific)
51 * @subject_match: String to match in the subject of the peer certificate or
52 * %NULL to allow all subjects
53 * @altsubject_match: String to match in the alternative subject of the peer
54 * certificate or %NULL to allow all alternative subjects
55 * @client_cert: File or reference name for client X.509 certificate in PEM or
57 * @client_cert_blob: client_cert as inlined data or %NULL if not used
58 * @client_cert_blob_len: client_cert_blob length
59 * @private_key: File or reference name for client private key in PEM or DER
60 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
61 * @private_key_blob: private_key as inlined data or %NULL if not used
62 * @private_key_blob_len: private_key_blob length
63 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
65 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
66 * @dh_blob: dh_file as inlined data or %NULL if not used
67 * @dh_blob_len: dh_blob length
68 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
69 * (this is OpenSSL specific for now)
70 * @engine_id: engine id string (this is OpenSSL specific for now)
71 * @ppin: pointer to the pin variable in the configuration
72 * (this is OpenSSL specific for now)
73 * @key_id: the private key's key id (this is OpenSSL specific for now)
75 * TLS connection parameters to be configured with tls_connection_set_params().
77 * Certificates and private key can be configured either as a reference name
78 * (file path or reference to certificate store) or by providing the same data
79 * as a pointer to the data in memory. Only one option will be used for each
82 struct tls_connection_params
{
84 const u8
*ca_cert_blob
;
85 size_t ca_cert_blob_len
;
87 const char *subject_match
;
88 const char *altsubject_match
;
89 const char *client_cert
;
90 const u8
*client_cert_blob
;
91 size_t client_cert_blob_len
;
92 const char *private_key
;
93 const u8
*private_key_blob
;
94 size_t private_key_blob_len
;
95 const char *private_key_passwd
;
100 /* OpenSSL specific variables */
102 const char *engine_id
;
109 * tls_init - Initialize TLS library
110 * @conf: Configuration data for TLS library
111 * Returns: Context data to be used as tls_ctx in calls to other functions,
112 * or %NULL on failure.
114 * Called once during program startup and once for each RSN pre-authentication
115 * session. In other words, there can be two concurrent TLS contexts. If global
116 * library initialization is needed (i.e., one that is shared between both
117 * authentication types), the TLS library wrapper should maintain a reference
118 * counter and do global initialization only when moving from 0 to 1 reference.
120 void * tls_init(const struct tls_config
*conf
);
123 * tls_deinit - Deinitialize TLS library
124 * @tls_ctx: TLS context data from tls_init()
126 * Called once during program shutdown and once for each RSN pre-authentication
127 * session. If global library deinitialization is needed (i.e., one that is
128 * shared between both authentication types), the TLS library wrapper should
129 * maintain a reference counter and do global deinitialization only when moving
130 * from 1 to 0 references.
132 void tls_deinit(void *tls_ctx
);
135 * tls_get_errors - Process pending errors
136 * @tls_ctx: TLS context data from tls_init()
138 * Returns: Number of found error, 0 if no errors detected.
140 * Process all pending TLS errors.
142 int tls_get_errors(void *tls_ctx
);
145 * tls_connection_init - Initialize a new TLS connection
146 * @tls_ctx: TLS context data from tls_init()
148 * Returns: Connection context data, conn for other function calls
150 struct tls_connection
* tls_connection_init(void *tls_ctx
);
153 * tls_connection_deinit - Free TLS connection data
154 * @tls_ctx: TLS context data from tls_init()
155 * @conn: Connection context data from tls_connection_init()
157 * Release all resources allocated for TLS connection.
159 void tls_connection_deinit(void *tls_ctx
, struct tls_connection
*conn
);
162 * tls_connection_established - Has the TLS connection been completed?
163 * @tls_ctx: TLS context data from tls_init()
164 * @conn: Connection context data from tls_connection_init()
166 * Returns: 1 if TLS connection has been completed, 0 if not.
168 int tls_connection_established(void *tls_ctx
, struct tls_connection
*conn
);
171 * tls_connection_shutdown - Shutdown TLS connection data.
172 * @tls_ctx: TLS context data from tls_init()
173 * @conn: Connection context data from tls_connection_init()
175 * Returns: 0 on success, -1 on failure
177 * Shutdown current TLS connection without releasing all resources. New
178 * connection can be started by using the same conn without having to call
179 * tls_connection_init() or setting certificates etc. again. The new
180 * connection should try to use session resumption.
182 int tls_connection_shutdown(void *tls_ctx
, struct tls_connection
*conn
);
185 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
= -3,
186 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
= -2
189 * tls_connection_set_params - Set TLS connection parameters
190 * @tls_ctx: TLS context data from tls_init()
191 * @conn: Connection context data from tls_connection_init()
192 * @params: Connection parameters
194 * Returns: 0 on success, -1 on failure,
195 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
196 * PKCS#11 engine failure, or
197 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
198 * PKCS#11 engine private key.
200 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
201 const struct tls_connection_params
*params
);
204 * tls_global_ca_cert - Set trusted CA certificate for all TLS connections
205 * @tls_ctx: TLS context data from tls_init()
206 * @ca_cert: File name for CA certificate in PEM or DER format
207 * %NULL to allow all subjects
209 * Returns: 0 on success, -1 on failure
211 int tls_global_ca_cert(void *tls_ctx
, const char *ca_cert
);
214 * tls_global_set_verify - Set global certificate verification options
215 * @tls_ctx: TLS context data from tls_init()
216 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
217 * 2 = verify CRL for all certificates
219 * Returns: 0 on success, -1 on failure
221 int tls_global_set_verify(void *tls_ctx
, int check_crl
);
224 * tls_connection_set_verify - Set certificate verification options
225 * @tls_ctx: TLS context data from tls_init()
226 * @conn: Connection context data from tls_connection_init()
227 * @verify_peer: 1 = verify peer certificate
229 * Returns: 0 on success, -1 on failure
231 int tls_connection_set_verify(void *tls_ctx
, struct tls_connection
*conn
,
235 * tls_global_client_cert - Set client certificate for all TLS connections
236 * @tls_ctx: TLS context data from tls_init()
237 * @client_cert: File name for client certificate in PEM or DER format
239 * Returns: 0 on success, -1 on failure
241 int tls_global_client_cert(void *tls_ctx
, const char *client_cert
);
244 * tls_global_private_key - Set private key for all TLS connections
245 * @tls_ctx: TLS context data from tls_init()
246 * @private_key: File name for client private key in PEM or DER format
247 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
248 * passphrase is used.
250 * Returns: 0 on success, -1 on failure
252 int tls_global_private_key(void *tls_ctx
, const char *private_key
,
253 const char *private_key_passwd
);
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)
261 * Returns: 0 on success, -1 on failure
263 int tls_connection_get_keys(void *tls_ctx
, struct tls_connection
*conn
,
264 struct tls_keys
*keys
);
267 * tls_connection_handshake - Process TLS handshake (client side)
268 * @tls_ctx: TLS context data from tls_init()
269 * @conn: Connection context data from tls_connection_init()
270 * @in_data: Input data from TLS peer
271 * @in_len: Input data length
272 * @out_len: Length of the output buffer.
274 * Returns: Pointer to output data, %NULL on failure
276 * Caller is responsible for freeing returned output data.
278 * This function is used during TLS handshake. The first call is done with
279 * in_data == %NULL and the library is expected to return ClientHello packet.
280 * This packet is then send to the server and a response from server is given
281 * to TLS library by calling this function again with in_data pointing to the
282 * TLS message from the server.
284 * If the TLS handshake fails, this function may return %NULL. However, if the
285 * TLS library has a TLS alert to send out, that should be returned as the
286 * output data. In this case, tls_connection_get_failed() must return failure
289 * tls_connection_established() should return 1 once the TLS handshake has been
290 * completed successfully.
292 u8
* tls_connection_handshake(void *tls_ctx
, struct tls_connection
*conn
,
293 const u8
*in_data
, size_t in_len
,
297 * tls_connection_server_handshake - Process TLS handshake (server side)
298 * @tls_ctx: TLS context data from tls_init()
299 * @conn: Connection context data from tls_connection_init()
300 * @in_data: Input data from TLS peer
301 * @in_len: Input data length
302 * @out_len: Length of the output buffer.
304 * Returns: pointer to output data, %NULL on failure
306 * Caller is responsible for freeing returned output data.
308 u8
* tls_connection_server_handshake(void *tls_ctx
,
309 struct tls_connection
*conn
,
310 const u8
*in_data
, size_t in_len
,
314 * tls_connection_encrypt - Encrypt data into TLS tunnel
315 * @tls_ctx: TLS context data from tls_init()
316 * @conn: Connection context data from tls_connection_init()
317 * @in_data: Pointer to plaintext data to be encrypted
318 * @in_len: Input buffer length
319 * @out_data: Pointer to output buffer (encrypted TLS data)
320 * @out_len: Maximum out_data length
322 * Returns: Number of bytes written to out_data, -1 on failure
324 * This function is used after TLS handshake has been completed successfully to
325 * send data in the encrypted tunnel.
327 int tls_connection_encrypt(void *tls_ctx
, struct tls_connection
*conn
,
328 const u8
*in_data
, size_t in_len
,
329 u8
*out_data
, size_t out_len
);
332 * tls_connection_decrypt - Decrypt data from 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 input buffer (encrypted TLS data)
336 * @in_len: Input buffer length
337 * @out_data: Pointer to output buffer (decrypted data from TLS tunnel)
338 * @out_len: Maximum out_data length
340 * Returns: Number of bytes written to out_data, -1 on failure
342 * This function is used after TLS handshake has been completed successfully to
343 * receive data from the encrypted tunnel.
345 int tls_connection_decrypt(void *tls_ctx
, struct tls_connection
*conn
,
346 const u8
*in_data
, size_t in_len
,
347 u8
*out_data
, size_t out_len
);
350 * tls_connection_resumed - Was session resumption used
351 * @tls_ctx: TLS context data from tls_init()
352 * @conn: Connection context data from tls_connection_init()
354 * Returns: 1 if current session used session resumption, 0 if not
356 int tls_connection_resumed(void *tls_ctx
, struct tls_connection
*conn
);
359 * tls_connection_set_master_key - Configure master secret for TLS connection
360 * @tls_ctx: TLS context data from tls_init()
361 * @conn: Connection context data from tls_connection_init()
362 * @key: TLS pre-master-secret
363 * @key_len: length of key in bytes
365 * Returns: 0 on success, -1 on failure
367 int tls_connection_set_master_key(void *tls_ctx
, struct tls_connection
*conn
,
368 const u8
*key
, size_t key_len
);
371 * tls_connection_set_anon_dh - Configure TLS connection to use anonymous DH
372 * @tls_ctx: TLS context data from tls_init()
373 * @conn: Connection context data from tls_connection_init()
375 * Returns: 0 on success, -1 on failure
377 * TODO: consider changing this to more generic routine for configuring allowed
380 int tls_connection_set_anon_dh(void *tls_ctx
, struct tls_connection
*conn
);
383 * tls_get_cipher - Get current cipher name
384 * @tls_ctx: TLS context data from tls_init()
385 * @conn: Connection context data from tls_connection_init()
386 * @buf: Buffer for the cipher name
389 * Returns: 0 on success, -1 on failure
391 * Get the name of the currently used cipher.
393 int tls_get_cipher(void *tls_ctx
, struct tls_connection
*conn
,
394 char *buf
, size_t buflen
);
397 * tls_connection_enable_workaround - Enable TLS workaround options
398 * @tls_ctx: TLS context data from tls_init()
399 * @conn: Connection context data from tls_connection_init()
401 * Returns: 0 on success, -1 on failure
403 * This function is used to enable connection-specific workaround options for
404 * buffer SSL/TLS implementations.
406 int tls_connection_enable_workaround(void *tls_ctx
,
407 struct tls_connection
*conn
);
410 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
411 * @tls_ctx: TLS context data from tls_init()
412 * @conn: Connection context data from tls_connection_init()
413 * @ext_type: Extension type
414 * @data: Extension payload (NULL to remove extension)
415 * @data_len: Extension payload length
417 * Returns: 0 on success, -1 on failure
419 int tls_connection_client_hello_ext(void *tls_ctx
, struct tls_connection
*conn
,
420 int ext_type
, const u8
*data
,
424 * tls_connection_get_failed - Get connection failure status
425 * @tls_ctx: TLS context data from tls_init()
426 * @conn: Connection context data from tls_connection_init()
428 * Returns >0 if connection has failed, 0 if not.
430 int tls_connection_get_failed(void *tls_ctx
, struct tls_connection
*conn
);
433 * tls_connection_get_read_alerts - Get connection read alert status
434 * @tls_ctx: TLS context data from tls_init()
435 * @conn: Connection context data from tls_connection_init()
437 * Returns: Number of times a fatal read (remote end reported error) has
438 * happened during this connection.
440 int tls_connection_get_read_alerts(void *tls_ctx
, struct tls_connection
*conn
);
443 * tls_connection_get_write_alerts - Get connection write alert status
444 * @tls_ctx: TLS context data from tls_init()
445 * @conn: Connection context data from tls_connection_init()
447 * Returns: Number of times a fatal write (locally detected error) has happened
448 * during this connection.
450 int tls_connection_get_write_alerts(void *tls_ctx
,
451 struct tls_connection
*conn
);
454 * tls_connection_get_keyblock_size - Get TLS key_block size
455 * @tls_ctx: TLS context data from tls_init()
456 * @conn: Connection context data from tls_connection_init()
457 * Returns: Size of the key_block for the negotiated cipher suite or -1 on
460 int tls_connection_get_keyblock_size(void *tls_ctx
,
461 struct tls_connection
*conn
);