Use pcidevs.h.
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / tls.h
1 /*
2  * WPA Supplicant / SSL/TLS interface definition
3  * Copyright (c) 2004-2006, Jouni Malinen <jkmaline@cc.hut.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;
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
28         /*
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).
33          */
34         const u8 *eap_tls_prf;
35         size_t eap_tls_prf_len;
36 };
37
38 struct tls_config {
39         const char *opensc_engine_path;
40         const char *pkcs11_engine_path;
41         const char *pkcs11_module_path;
42 };
43
44 /**
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
47  * format
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
56  * DER format
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
64  * passphrase is used.
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)
74  *
75  * TLS connection parameters to be configured with tls_connection_set_params().
76  *
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
80  * field.
81  */
82 struct tls_connection_params {
83         const char *ca_cert;
84         const u8 *ca_cert_blob;
85         size_t ca_cert_blob_len;
86         const char *ca_path;
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;
96         const char *dh_file;
97         const u8 *dh_blob;
98         size_t dh_blob_len;
99
100         /* OpenSSL specific variables */
101         int engine;
102         const char *engine_id;
103         const char *pin;
104         const char *key_id;
105 };
106
107
108 /**
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.
113  *
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.
119  */
120 void * tls_init(const struct tls_config *conf);
121
122 /**
123  * tls_deinit - Deinitialize TLS library
124  * @tls_ctx: TLS context data from tls_init()
125  *
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.
131  */
132 void tls_deinit(void *tls_ctx);
133
134 /**
135  * tls_get_errors - Process pending errors
136  * @tls_ctx: TLS context data from tls_init()
137  *
138  * Returns: Number of found error, 0 if no errors detected.
139  *
140  * Process all pending TLS errors.
141  */
142 int tls_get_errors(void *tls_ctx);
143
144 /**
145  * tls_connection_init - Initialize a new TLS connection
146  * @tls_ctx: TLS context data from tls_init()
147  *
148  * Returns: Connection context data, conn for other function calls
149  */
150 struct tls_connection * tls_connection_init(void *tls_ctx);
151
152 /**
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()
156  *
157  * Release all resources allocated for TLS connection.
158  */
159 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
160
161 /**
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()
165  *
166  * Returns: 1 if TLS connection has been completed, 0 if not.
167  */
168 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
169
170 /**
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()
174  *
175  * Returns: 0 on success, -1 on failure
176  *
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.
181  */
182 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
183
184 enum {
185         TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
186         TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
187 };
188 /**
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
193  *
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.
199  */
200 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
201                               const struct tls_connection_params *params);
202
203 /**
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
208  *
209  * Returns: 0 on success, -1 on failure
210  */
211 int tls_global_ca_cert(void *tls_ctx, const char *ca_cert);
212
213 /**
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
218  *
219  * Returns: 0 on success, -1 on failure
220  */
221 int tls_global_set_verify(void *tls_ctx, int check_crl);
222
223 /**
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
228  *
229  * Returns: 0 on success, -1 on failure
230  */
231 int tls_connection_set_verify(void *tls_ctx, struct tls_connection *conn,
232                               int verify_peer);
233
234 /**
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
238  *
239  * Returns: 0 on success, -1 on failure
240  */
241 int tls_global_client_cert(void *tls_ctx, const char *client_cert);
242
243 /**
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.
249  *
250  * Returns: 0 on success, -1 on failure
251  */
252 int tls_global_private_key(void *tls_ctx, const char *private_key,
253                            const char *private_key_passwd);
254
255 /**
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  *
261  * Returns: 0 on success, -1 on failure
262  */
263 int tls_connection_get_keys(void *tls_ctx, struct tls_connection *conn,
264                             struct tls_keys *keys);
265
266 /**
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.
273  *
274  * Returns: Pointer to output data, %NULL on failure
275  *
276  * Caller is responsible for freeing returned output data.
277  *
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.
283  *
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
287  * (> 0).
288  *
289  * tls_connection_established() should return 1 once the TLS handshake has been
290  * completed successfully.
291  */
292 u8 * tls_connection_handshake(void *tls_ctx, struct tls_connection *conn,
293                               const u8 *in_data, size_t in_len,
294                               size_t *out_len);
295
296 /**
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.
303  *
304  * Returns: pointer to output data, %NULL on failure
305  *
306  * Caller is responsible for freeing returned output data.
307  */
308 u8 * tls_connection_server_handshake(void *tls_ctx,
309                                      struct tls_connection *conn,
310                                      const u8 *in_data, size_t in_len,
311                                      size_t *out_len);
312
313 /**
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 
321  *
322  * Returns: Number of bytes written to out_data, -1 on failure
323  *
324  * This function is used after TLS handshake has been completed successfully to
325  * send data in the encrypted tunnel.
326  */
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);
330
331 /**
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
339  *
340  * Returns: Number of bytes written to out_data, -1 on failure
341  *
342  * This function is used after TLS handshake has been completed successfully to
343  * receive data from the encrypted tunnel.
344  */
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);
348
349 /**
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()
353  *
354  * Returns: 1 if current session used session resumption, 0 if not
355  */
356 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
357
358 /**
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
364  *
365  * Returns: 0 on success, -1 on failure
366  */
367 int tls_connection_set_master_key(void *tls_ctx, struct tls_connection *conn,
368                                   const u8 *key, size_t key_len);
369
370 /**
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()
374  *
375  * Returns: 0 on success, -1 on failure
376  *
377  * TODO: consider changing this to more generic routine for configuring allowed
378  * ciphers
379  */
380 int tls_connection_set_anon_dh(void *tls_ctx, struct tls_connection *conn);
381
382 /**
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
387  * @buflen: buf size
388  *
389  * Returns: 0 on success, -1 on failure
390  *
391  * Get the name of the currently used cipher.
392  */
393 int tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
394                    char *buf, size_t buflen);
395
396 /**
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()
400  *
401  * Returns: 0 on success, -1 on failure
402  *
403  * This function is used to enable connection-specific workaround options for
404  * buffer SSL/TLS implementations.
405  */
406 int tls_connection_enable_workaround(void *tls_ctx,
407                                      struct tls_connection *conn);
408
409 /**
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
416  *
417  * Returns: 0 on success, -1 on failure
418  */
419 int tls_connection_client_hello_ext(void *tls_ctx, struct tls_connection *conn,
420                                     int ext_type, const u8 *data,
421                                     size_t data_len);
422
423 /**
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()
427  *
428  * Returns >0 if connection has failed, 0 if not.
429  */
430 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
431
432 /**
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()
436  *
437  * Returns: Number of times a fatal read (remote end reported error) has
438  * happened during this connection.
439  */
440 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
441
442 /**
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()
446  *
447  * Returns: Number of times a fatal write (locally detected error) has happened
448  * during this connection.
449  */
450 int tls_connection_get_write_alerts(void *tls_ctx,
451                                     struct tls_connection *conn);
452
453 /**
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
458  * failure
459  */
460 int tls_connection_get_keyblock_size(void *tls_ctx,
461                                      struct tls_connection *conn);
462
463 #endif /* TLS_H */