Merge with wpa_supplicant 1.0 stable release
[profile/ivi/wpa_supplicant.git] / src / crypto / tls.h
1 /*
2  * SSL/TLS interface definition
3  * Copyright (c) 2004-2010, 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 };
28
29 enum tls_event {
30         TLS_CERT_CHAIN_FAILURE,
31         TLS_PEER_CERTIFICATE
32 };
33
34 /*
35  * Note: These are used as identifier with external programs and as such, the
36  * values must not be changed.
37  */
38 enum tls_fail_reason {
39         TLS_FAIL_UNSPECIFIED = 0,
40         TLS_FAIL_UNTRUSTED = 1,
41         TLS_FAIL_REVOKED = 2,
42         TLS_FAIL_NOT_YET_VALID = 3,
43         TLS_FAIL_EXPIRED = 4,
44         TLS_FAIL_SUBJECT_MISMATCH = 5,
45         TLS_FAIL_ALTSUBJECT_MISMATCH = 6,
46         TLS_FAIL_BAD_CERTIFICATE = 7,
47         TLS_FAIL_SERVER_CHAIN_PROBE = 8
48 };
49
50 union tls_event_data {
51         struct {
52                 int depth;
53                 const char *subject;
54                 enum tls_fail_reason reason;
55                 const char *reason_txt;
56                 const struct wpabuf *cert;
57         } cert_fail;
58
59         struct {
60                 int depth;
61                 const char *subject;
62                 const struct wpabuf *cert;
63                 const u8 *hash;
64                 size_t hash_len;
65         } peer_cert;
66 };
67
68 struct tls_config {
69         const char *opensc_engine_path;
70         const char *pkcs11_engine_path;
71         const char *pkcs11_module_path;
72         int fips_mode;
73         int cert_in_cb;
74
75         void (*event_cb)(void *ctx, enum tls_event ev,
76                          union tls_event_data *data);
77         void *cb_ctx;
78 };
79
80 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
81 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
82
83 /**
84  * struct tls_connection_params - Parameters for TLS connection
85  * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
86  * format
87  * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
88  * @ca_cert_blob_len: ca_cert_blob length
89  * @ca_path: Path to CA certificates (OpenSSL specific)
90  * @subject_match: String to match in the subject of the peer certificate or
91  * %NULL to allow all subjects
92  * @altsubject_match: String to match in the alternative subject of the peer
93  * certificate or %NULL to allow all alternative subjects
94  * @client_cert: File or reference name for client X.509 certificate in PEM or
95  * DER format
96  * @client_cert_blob: client_cert as inlined data or %NULL if not used
97  * @client_cert_blob_len: client_cert_blob length
98  * @private_key: File or reference name for client private key in PEM or DER
99  * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
100  * @private_key_blob: private_key as inlined data or %NULL if not used
101  * @private_key_blob_len: private_key_blob length
102  * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
103  * passphrase is used.
104  * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
105  * @dh_blob: dh_file as inlined data or %NULL if not used
106  * @dh_blob_len: dh_blob length
107  * @engine: 1 = use engine (e.g., a smartcard) for private key operations
108  * (this is OpenSSL specific for now)
109  * @engine_id: engine id string (this is OpenSSL specific for now)
110  * @ppin: pointer to the pin variable in the configuration
111  * (this is OpenSSL specific for now)
112  * @key_id: the private key's id when using engine (this is OpenSSL
113  * specific for now)
114  * @cert_id: the certificate's id when using engine
115  * @ca_cert_id: the CA certificate's id when using engine
116  * @flags: Parameter options (TLS_CONN_*)
117  *
118  * TLS connection parameters to be configured with tls_connection_set_params()
119  * and tls_global_set_params().
120  *
121  * Certificates and private key can be configured either as a reference name
122  * (file path or reference to certificate store) or by providing the same data
123  * as a pointer to the data in memory. Only one option will be used for each
124  * field.
125  */
126 struct tls_connection_params {
127         const char *ca_cert;
128         const u8 *ca_cert_blob;
129         size_t ca_cert_blob_len;
130         const char *ca_path;
131         const char *subject_match;
132         const char *altsubject_match;
133         const char *client_cert;
134         const u8 *client_cert_blob;
135         size_t client_cert_blob_len;
136         const char *private_key;
137         const u8 *private_key_blob;
138         size_t private_key_blob_len;
139         const char *private_key_passwd;
140         const char *dh_file;
141         const u8 *dh_blob;
142         size_t dh_blob_len;
143
144         /* OpenSSL specific variables */
145         int engine;
146         const char *engine_id;
147         const char *pin;
148         const char *key_id;
149         const char *cert_id;
150         const char *ca_cert_id;
151
152         unsigned int flags;
153 };
154
155
156 /**
157  * tls_init - Initialize TLS library
158  * @conf: Configuration data for TLS library
159  * Returns: Context data to be used as tls_ctx in calls to other functions,
160  * or %NULL on failure.
161  *
162  * Called once during program startup and once for each RSN pre-authentication
163  * session. In other words, there can be two concurrent TLS contexts. If global
164  * library initialization is needed (i.e., one that is shared between both
165  * authentication types), the TLS library wrapper should maintain a reference
166  * counter and do global initialization only when moving from 0 to 1 reference.
167  */
168 void * tls_init(const struct tls_config *conf);
169
170 /**
171  * tls_deinit - Deinitialize TLS library
172  * @tls_ctx: TLS context data from tls_init()
173  *
174  * Called once during program shutdown and once for each RSN pre-authentication
175  * session. If global library deinitialization is needed (i.e., one that is
176  * shared between both authentication types), the TLS library wrapper should
177  * maintain a reference counter and do global deinitialization only when moving
178  * from 1 to 0 references.
179  */
180 void tls_deinit(void *tls_ctx);
181
182 /**
183  * tls_get_errors - Process pending errors
184  * @tls_ctx: TLS context data from tls_init()
185  * Returns: Number of found error, 0 if no errors detected.
186  *
187  * Process all pending TLS errors.
188  */
189 int tls_get_errors(void *tls_ctx);
190
191 /**
192  * tls_connection_init - Initialize a new TLS connection
193  * @tls_ctx: TLS context data from tls_init()
194  * Returns: Connection context data, conn for other function calls
195  */
196 struct tls_connection * tls_connection_init(void *tls_ctx);
197
198 /**
199  * tls_connection_deinit - Free TLS connection data
200  * @tls_ctx: TLS context data from tls_init()
201  * @conn: Connection context data from tls_connection_init()
202  *
203  * Release all resources allocated for TLS connection.
204  */
205 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
206
207 /**
208  * tls_connection_established - Has the TLS connection been completed?
209  * @tls_ctx: TLS context data from tls_init()
210  * @conn: Connection context data from tls_connection_init()
211  * Returns: 1 if TLS connection has been completed, 0 if not.
212  */
213 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
214
215 /**
216  * tls_connection_shutdown - Shutdown TLS connection
217  * @tls_ctx: TLS context data from tls_init()
218  * @conn: Connection context data from tls_connection_init()
219  * Returns: 0 on success, -1 on failure
220  *
221  * Shutdown current TLS connection without releasing all resources. New
222  * connection can be started by using the same conn without having to call
223  * tls_connection_init() or setting certificates etc. again. The new
224  * connection should try to use session resumption.
225  */
226 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
227
228 enum {
229         TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
230         TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
231 };
232
233 /**
234  * tls_connection_set_params - Set TLS connection parameters
235  * @tls_ctx: TLS context data from tls_init()
236  * @conn: Connection context data from tls_connection_init()
237  * @params: Connection parameters
238  * Returns: 0 on success, -1 on failure,
239  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
240  * PKCS#11 engine failure, or
241  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
242  * PKCS#11 engine private key.
243  */
244 int __must_check
245 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
246                           const struct tls_connection_params *params);
247
248 /**
249  * tls_global_set_params - Set TLS parameters for all TLS connection
250  * @tls_ctx: TLS context data from tls_init()
251  * @params: Global TLS parameters
252  * Returns: 0 on success, -1 on failure,
253  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
254  * PKCS#11 engine failure, or
255  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
256  * PKCS#11 engine private key.
257  */
258 int __must_check tls_global_set_params(
259         void *tls_ctx, const struct tls_connection_params *params);
260
261 /**
262  * tls_global_set_verify - Set global certificate verification options
263  * @tls_ctx: TLS context data from tls_init()
264  * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
265  * 2 = verify CRL for all certificates
266  * Returns: 0 on success, -1 on failure
267  */
268 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
269
270 /**
271  * tls_connection_set_verify - Set certificate verification options
272  * @tls_ctx: TLS context data from tls_init()
273  * @conn: Connection context data from tls_connection_init()
274  * @verify_peer: 1 = verify peer certificate
275  * Returns: 0 on success, -1 on failure
276  */
277 int __must_check tls_connection_set_verify(void *tls_ctx,
278                                            struct tls_connection *conn,
279                                            int verify_peer);
280
281 /**
282  * tls_connection_get_keys - Get master key and random data from TLS connection
283  * @tls_ctx: TLS context data from tls_init()
284  * @conn: Connection context data from tls_connection_init()
285  * @keys: Structure of key/random data (filled on success)
286  * Returns: 0 on success, -1 on failure
287  */
288 int __must_check tls_connection_get_keys(void *tls_ctx,
289                                          struct tls_connection *conn,
290                                          struct tls_keys *keys);
291
292 /**
293  * tls_connection_prf - Use TLS-PRF to derive keying material
294  * @tls_ctx: TLS context data from tls_init()
295  * @conn: Connection context data from tls_connection_init()
296  * @label: Label (e.g., description of the key) for PRF
297  * @server_random_first: seed is 0 = client_random|server_random,
298  * 1 = server_random|client_random
299  * @out: Buffer for output data from TLS-PRF
300  * @out_len: Length of the output buffer
301  * Returns: 0 on success, -1 on failure
302  *
303  * This function is optional to implement if tls_connection_get_keys() provides
304  * access to master secret and server/client random values. If these values are
305  * not exported from the TLS library, tls_connection_prf() is required so that
306  * further keying material can be derived from the master secret. If not
307  * implemented, the function will still need to be defined, but it can just
308  * return -1. Example implementation of this function is in tls_prf() function
309  * when it is called with seed set to client_random|server_random (or
310  * server_random|client_random).
311  */
312 int __must_check  tls_connection_prf(void *tls_ctx,
313                                      struct tls_connection *conn,
314                                      const char *label,
315                                      int server_random_first,
316                                      u8 *out, size_t out_len);
317
318 /**
319  * tls_connection_handshake - Process TLS handshake (client side)
320  * @tls_ctx: TLS context data from tls_init()
321  * @conn: Connection context data from tls_connection_init()
322  * @in_data: Input data from TLS server
323  * @appl_data: Pointer to application data pointer, or %NULL if dropped
324  * Returns: Output data, %NULL on failure
325  *
326  * The caller is responsible for freeing the returned output data. If the final
327  * handshake message includes application data, this is decrypted and
328  * appl_data (if not %NULL) is set to point this data. The caller is
329  * responsible for freeing appl_data.
330  *
331  * This function is used during TLS handshake. The first call is done with
332  * in_data == %NULL and the library is expected to return ClientHello packet.
333  * This packet is then send to the server and a response from server is given
334  * to TLS library by calling this function again with in_data pointing to the
335  * TLS message from the server.
336  *
337  * If the TLS handshake fails, this function may return %NULL. However, if the
338  * TLS library has a TLS alert to send out, that should be returned as the
339  * output data. In this case, tls_connection_get_failed() must return failure
340  * (> 0).
341  *
342  * tls_connection_established() should return 1 once the TLS handshake has been
343  * completed successfully.
344  */
345 struct wpabuf * tls_connection_handshake(void *tls_ctx,
346                                          struct tls_connection *conn,
347                                          const struct wpabuf *in_data,
348                                          struct wpabuf **appl_data);
349
350 /**
351  * tls_connection_server_handshake - Process TLS handshake (server side)
352  * @tls_ctx: TLS context data from tls_init()
353  * @conn: Connection context data from tls_connection_init()
354  * @in_data: Input data from TLS peer
355  * @appl_data: Pointer to application data pointer, or %NULL if dropped
356  * Returns: Output data, %NULL on failure
357  *
358  * The caller is responsible for freeing the returned output data.
359  */
360 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
361                                                 struct tls_connection *conn,
362                                                 const struct wpabuf *in_data,
363                                                 struct wpabuf **appl_data);
364
365 /**
366  * tls_connection_encrypt - Encrypt data into TLS tunnel
367  * @tls_ctx: TLS context data from tls_init()
368  * @conn: Connection context data from tls_connection_init()
369  * @in_data: Plaintext data to be encrypted
370  * Returns: Encrypted TLS data or %NULL on failure
371  *
372  * This function is used after TLS handshake has been completed successfully to
373  * send data in the encrypted tunnel. The caller is responsible for freeing the
374  * returned output data.
375  */
376 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
377                                        struct tls_connection *conn,
378                                        const struct wpabuf *in_data);
379
380 /**
381  * tls_connection_decrypt - Decrypt data from TLS tunnel
382  * @tls_ctx: TLS context data from tls_init()
383  * @conn: Connection context data from tls_connection_init()
384  * @in_data: Encrypted TLS data
385  * Returns: Decrypted TLS data or %NULL on failure
386  *
387  * This function is used after TLS handshake has been completed successfully to
388  * receive data from the encrypted tunnel. The caller is responsible for
389  * freeing the returned output data.
390  */
391 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
392                                        struct tls_connection *conn,
393                                        const struct wpabuf *in_data);
394
395 /**
396  * tls_connection_resumed - Was session resumption used
397  * @tls_ctx: TLS context data from tls_init()
398  * @conn: Connection context data from tls_connection_init()
399  * Returns: 1 if current session used session resumption, 0 if not
400  */
401 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
402
403 enum {
404         TLS_CIPHER_NONE,
405         TLS_CIPHER_RC4_SHA /* 0x0005 */,
406         TLS_CIPHER_AES128_SHA /* 0x002f */,
407         TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
408         TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
409 };
410
411 /**
412  * tls_connection_set_cipher_list - Configure acceptable cipher suites
413  * @tls_ctx: TLS context data from tls_init()
414  * @conn: Connection context data from tls_connection_init()
415  * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
416  * (TLS_CIPHER_*).
417  * Returns: 0 on success, -1 on failure
418  */
419 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
420                                                 struct tls_connection *conn,
421                                                 u8 *ciphers);
422
423 /**
424  * tls_get_cipher - Get current cipher name
425  * @tls_ctx: TLS context data from tls_init()
426  * @conn: Connection context data from tls_connection_init()
427  * @buf: Buffer for the cipher name
428  * @buflen: buf size
429  * Returns: 0 on success, -1 on failure
430  *
431  * Get the name of the currently used cipher.
432  */
433 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
434                                 char *buf, size_t buflen);
435
436 /**
437  * tls_connection_enable_workaround - Enable TLS workaround options
438  * @tls_ctx: TLS context data from tls_init()
439  * @conn: Connection context data from tls_connection_init()
440  * Returns: 0 on success, -1 on failure
441  *
442  * This function is used to enable connection-specific workaround options for
443  * buffer SSL/TLS implementations.
444  */
445 int __must_check tls_connection_enable_workaround(void *tls_ctx,
446                                                   struct tls_connection *conn);
447
448 /**
449  * tls_connection_client_hello_ext - Set TLS extension for ClientHello
450  * @tls_ctx: TLS context data from tls_init()
451  * @conn: Connection context data from tls_connection_init()
452  * @ext_type: Extension type
453  * @data: Extension payload (%NULL to remove extension)
454  * @data_len: Extension payload length
455  * Returns: 0 on success, -1 on failure
456  */
457 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
458                                                  struct tls_connection *conn,
459                                                  int ext_type, const u8 *data,
460                                                  size_t data_len);
461
462 /**
463  * tls_connection_get_failed - Get connection failure status
464  * @tls_ctx: TLS context data from tls_init()
465  * @conn: Connection context data from tls_connection_init()
466  *
467  * Returns >0 if connection has failed, 0 if not.
468  */
469 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
470
471 /**
472  * tls_connection_get_read_alerts - Get connection read alert status
473  * @tls_ctx: TLS context data from tls_init()
474  * @conn: Connection context data from tls_connection_init()
475  * Returns: Number of times a fatal read (remote end reported error) has
476  * happened during this connection.
477  */
478 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
479
480 /**
481  * tls_connection_get_write_alerts - Get connection write alert status
482  * @tls_ctx: TLS context data from tls_init()
483  * @conn: Connection context data from tls_connection_init()
484  * Returns: Number of times a fatal write (locally detected error) has happened
485  * during this connection.
486  */
487 int tls_connection_get_write_alerts(void *tls_ctx,
488                                     struct tls_connection *conn);
489
490 /**
491  * tls_connection_get_keyblock_size - Get TLS key_block size
492  * @tls_ctx: TLS context data from tls_init()
493  * @conn: Connection context data from tls_connection_init()
494  * Returns: Size of the key_block for the negotiated cipher suite or -1 on
495  * failure
496  */
497 int tls_connection_get_keyblock_size(void *tls_ctx,
498                                      struct tls_connection *conn);
499
500 /**
501  * tls_capabilities - Get supported TLS capabilities
502  * @tls_ctx: TLS context data from tls_init()
503  * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*)
504  */
505 unsigned int tls_capabilities(void *tls_ctx);
506
507 typedef int (*tls_session_ticket_cb)
508 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
509  const u8 *server_random, u8 *master_secret);
510
511 int __must_check  tls_connection_set_session_ticket_cb(
512         void *tls_ctx, struct tls_connection *conn,
513         tls_session_ticket_cb cb, void *ctx);
514
515 #endif /* TLS_H */