0a79166339ac48fcb92841a16d981c938aeea8ce
[libeap.git] / src / crypto / tls.h
1 /*
2  * WPA Supplicant / SSL/TLS interface definition
3  * Copyright (c) 2004-2007, Jouni Malinen <j@w1.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; /* 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;
29 };
30
31 struct tls_config {
32         const char *opensc_engine_path;
33         const char *pkcs11_engine_path;
34         const char *pkcs11_module_path;
35 };
36
37 /**
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
40  * format
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
49  * DER format
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
57  * passphrase is used.
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)
68  *
69  * TLS connection parameters to be configured with tls_connection_set_params()
70  * and tls_global_set_params().
71  *
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
75  * field.
76  */
77 struct tls_connection_params {
78         const char *ca_cert;
79         const u8 *ca_cert_blob;
80         size_t ca_cert_blob_len;
81         const char *ca_path;
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;
91         const char *dh_file;
92         const u8 *dh_blob;
93         size_t dh_blob_len;
94         int tls_ia;
95
96         /* OpenSSL specific variables */
97         int engine;
98         const char *engine_id;
99         const char *pin;
100         const char *key_id;
101 };
102
103
104 /**
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.
109  *
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.
115  */
116 void * tls_init(const struct tls_config *conf);
117
118 /**
119  * tls_deinit - Deinitialize TLS library
120  * @tls_ctx: TLS context data from tls_init()
121  *
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.
127  */
128 void tls_deinit(void *tls_ctx);
129
130 /**
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.
134  *
135  * Process all pending TLS errors.
136  */
137 int tls_get_errors(void *tls_ctx);
138
139 /**
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
143  */
144 struct tls_connection * tls_connection_init(void *tls_ctx);
145
146 /**
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()
150  *
151  * Release all resources allocated for TLS connection.
152  */
153 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
154
155 /**
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.
160  */
161 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
162
163 /**
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
168  *
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.
173  */
174 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
175
176 enum {
177         TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
178         TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
179 };
180
181 /**
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.
191  */
192 int __must_check
193 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
194                           const struct tls_connection_params *params);
195
196 /**
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.
205  */
206 int __must_check tls_global_set_params(
207         void *tls_ctx, const struct tls_connection_params *params);
208
209 /**
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
215  */
216 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
217
218 /**
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
224  */
225 int __must_check tls_connection_set_verify(void *tls_ctx,
226                                            struct tls_connection *conn,
227                                            int verify_peer);
228
229 /**
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
235  *
236  * This function is used to configure TLS/IA in server mode where
237  * tls_connection_set_params() is not used.
238  */
239 int __must_check tls_connection_set_ia(void *tls_ctx,
240                                        struct tls_connection *conn,
241                                        int tls_ia);
242
243 /**
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
249  */
250 int __must_check tls_connection_get_keys(void *tls_ctx,
251                                          struct tls_connection *conn,
252                                          struct tls_keys *keys);
253
254 /**
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
264  *
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).
273  */
274 int __must_check  tls_connection_prf(void *tls_ctx,
275                                      struct tls_connection *conn,
276                                      const char *label,
277                                      int server_random_first,
278                                      u8 *out, size_t out_len);
279
280 /**
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
290  *
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.
295  *
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.
301  *
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
305  * (> 0).
306  *
307  * tls_connection_established() should return 1 once the TLS handshake has been
308  * completed successfully.
309  */
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);
314
315 /**
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
323  *
324  * Caller is responsible for freeing returned output data.
325  */
326 u8 * tls_connection_server_handshake(void *tls_ctx,
327                                      struct tls_connection *conn,
328                                      const u8 *in_data, size_t in_len,
329                                      size_t *out_len);
330
331 /**
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
340  *
341  * This function is used after TLS handshake has been completed successfully to
342  * send data in the encrypted tunnel.
343  */
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);
348
349 /**
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
358  *
359  * This function is used after TLS handshake has been completed successfully to
360  * receive data from the encrypted tunnel.
361  */
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);
366
367 /**
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
372  */
373 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
374
375 enum {
376         TLS_CIPHER_NONE,
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 */
381 };
382
383 /**
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
388  * (TLS_CIPHER_*).
389  * Returns: 0 on success, -1 on failure
390  */
391 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
392                                                 struct tls_connection *conn,
393                                                 u8 *ciphers);
394
395 /**
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
400  * @buflen: buf size
401  * Returns: 0 on success, -1 on failure
402  *
403  * Get the name of the currently used cipher.
404  */
405 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
406                                 char *buf, size_t buflen);
407
408 /**
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
413  *
414  * This function is used to enable connection-specific workaround options for
415  * buffer SSL/TLS implementations.
416  */
417 int __must_check tls_connection_enable_workaround(void *tls_ctx,
418                                                   struct tls_connection *conn);
419
420 /**
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
428  */
429 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
430                                                  struct tls_connection *conn,
431                                                  int ext_type, const u8 *data,
432                                                  size_t data_len);
433
434 /**
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()
438  *
439  * Returns >0 if connection has failed, 0 if not.
440  */
441 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
442
443 /**
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.
449  */
450 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
451
452 /**
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.
458  */
459 int tls_connection_get_write_alerts(void *tls_ctx,
460                                     struct tls_connection *conn);
461
462 /**
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
467  * failure
468  */
469 int tls_connection_get_keyblock_size(void *tls_ctx,
470                                      struct tls_connection *conn);
471
472 #define TLS_CAPABILITY_IA 0x0001 /* TLS Inner Application (TLS/IA) */
473 /**
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_*)
477  */
478 unsigned int tls_capabilities(void *tls_ctx);
479
480 /**
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
488  *
489  * This function is used to send the TLS/IA end phase message, e.g., when the
490  * EAP server completes EAP-TTLSv1.
491  */
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);
495
496 /**
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
501  * on failure
502  */
503 int __must_check tls_connection_ia_final_phase_finished(
504         void *tls_ctx, struct tls_connection *conn);
505
506 /**
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
514  */
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);
518
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);
522
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);
526
527 #endif /* TLS_H */