]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/wpa/src/crypto/tls.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / wpa / 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 id when using engine (this is OpenSSL
67  * specific for now)
68  * @cert_id: the certificate's id when using engine
69  * @ca_cert_id: the CA certificate's id when using engine
70  * @tls_ia: Whether to enable TLS/IA (for EAP-TTLSv1)
71  *
72  * TLS connection parameters to be configured with tls_connection_set_params()
73  * and tls_global_set_params().
74  *
75  * Certificates and private key can be configured either as a reference name
76  * (file path or reference to certificate store) or by providing the same data
77  * as a pointer to the data in memory. Only one option will be used for each
78  * field.
79  */
80 struct tls_connection_params {
81         const char *ca_cert;
82         const u8 *ca_cert_blob;
83         size_t ca_cert_blob_len;
84         const char *ca_path;
85         const char *subject_match;
86         const char *altsubject_match;
87         const char *client_cert;
88         const u8 *client_cert_blob;
89         size_t client_cert_blob_len;
90         const char *private_key;
91         const u8 *private_key_blob;
92         size_t private_key_blob_len;
93         const char *private_key_passwd;
94         const char *dh_file;
95         const u8 *dh_blob;
96         size_t dh_blob_len;
97         int tls_ia;
98
99         /* OpenSSL specific variables */
100         int engine;
101         const char *engine_id;
102         const char *pin;
103         const char *key_id;
104         const char *cert_id;
105         const char *ca_cert_id;
106 };
107
108
109 /**
110  * tls_init - Initialize TLS library
111  * @conf: Configuration data for TLS library
112  * Returns: Context data to be used as tls_ctx in calls to other functions,
113  * or %NULL on failure.
114  *
115  * Called once during program startup and once for each RSN pre-authentication
116  * session. In other words, there can be two concurrent TLS contexts. If global
117  * library initialization is needed (i.e., one that is shared between both
118  * authentication types), the TLS library wrapper should maintain a reference
119  * counter and do global initialization only when moving from 0 to 1 reference.
120  */
121 void * tls_init(const struct tls_config *conf);
122
123 /**
124  * tls_deinit - Deinitialize TLS library
125  * @tls_ctx: TLS context data from tls_init()
126  *
127  * Called once during program shutdown and once for each RSN pre-authentication
128  * session. If global library deinitialization is needed (i.e., one that is
129  * shared between both authentication types), the TLS library wrapper should
130  * maintain a reference counter and do global deinitialization only when moving
131  * from 1 to 0 references.
132  */
133 void tls_deinit(void *tls_ctx);
134
135 /**
136  * tls_get_errors - Process pending errors
137  * @tls_ctx: TLS context data from tls_init()
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  * Returns: Connection context data, conn for other function calls
148  */
149 struct tls_connection * tls_connection_init(void *tls_ctx);
150
151 /**
152  * tls_connection_deinit - Free TLS connection data
153  * @tls_ctx: TLS context data from tls_init()
154  * @conn: Connection context data from tls_connection_init()
155  *
156  * Release all resources allocated for TLS connection.
157  */
158 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
159
160 /**
161  * tls_connection_established - Has the TLS connection been completed?
162  * @tls_ctx: TLS context data from tls_init()
163  * @conn: Connection context data from tls_connection_init()
164  * Returns: 1 if TLS connection has been completed, 0 if not.
165  */
166 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
167
168 /**
169  * tls_connection_shutdown - Shutdown TLS connection
170  * @tls_ctx: TLS context data from tls_init()
171  * @conn: Connection context data from tls_connection_init()
172  * Returns: 0 on success, -1 on failure
173  *
174  * Shutdown current TLS connection without releasing all resources. New
175  * connection can be started by using the same conn without having to call
176  * tls_connection_init() or setting certificates etc. again. The new
177  * connection should try to use session resumption.
178  */
179 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
180
181 enum {
182         TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
183         TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
184 };
185
186 /**
187  * tls_connection_set_params - Set TLS connection parameters
188  * @tls_ctx: TLS context data from tls_init()
189  * @conn: Connection context data from tls_connection_init()
190  * @params: Connection parameters
191  * Returns: 0 on success, -1 on failure,
192  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
193  * PKCS#11 engine failure, or
194  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
195  * PKCS#11 engine private key.
196  */
197 int __must_check
198 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
199                           const struct tls_connection_params *params);
200
201 /**
202  * tls_global_set_params - Set TLS parameters for all TLS connection
203  * @tls_ctx: TLS context data from tls_init()
204  * @params: Global TLS parameters
205  * Returns: 0 on success, -1 on failure,
206  * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
207  * PKCS#11 engine failure, or
208  * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
209  * PKCS#11 engine private key.
210  */
211 int __must_check tls_global_set_params(
212         void *tls_ctx, const struct tls_connection_params *params);
213
214 /**
215  * tls_global_set_verify - Set global certificate verification options
216  * @tls_ctx: TLS context data from tls_init()
217  * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
218  * 2 = verify CRL for all certificates
219  * Returns: 0 on success, -1 on failure
220  */
221 int __must_check 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  * Returns: 0 on success, -1 on failure
229  */
230 int __must_check tls_connection_set_verify(void *tls_ctx,
231                                            struct tls_connection *conn,
232                                            int verify_peer);
233
234 /**
235  * tls_connection_set_ia - Set TLS/IA parameters
236  * @tls_ctx: TLS context data from tls_init()
237  * @conn: Connection context data from tls_connection_init()
238  * @tls_ia: 1 = enable TLS/IA
239  * Returns: 0 on success, -1 on failure
240  *
241  * This function is used to configure TLS/IA in server mode where
242  * tls_connection_set_params() is not used.
243  */
244 int __must_check tls_connection_set_ia(void *tls_ctx,
245                                        struct tls_connection *conn,
246                                        int tls_ia);
247
248 /**
249  * tls_connection_get_keys - Get master key and random data from TLS connection
250  * @tls_ctx: TLS context data from tls_init()
251  * @conn: Connection context data from tls_connection_init()
252  * @keys: Structure of key/random data (filled on success)
253  * Returns: 0 on success, -1 on failure
254  */
255 int __must_check tls_connection_get_keys(void *tls_ctx,
256                                          struct tls_connection *conn,
257                                          struct tls_keys *keys);
258
259 /**
260  * tls_connection_prf - Use TLS-PRF to derive keying material
261  * @tls_ctx: TLS context data from tls_init()
262  * @conn: Connection context data from tls_connection_init()
263  * @label: Label (e.g., description of the key) for PRF
264  * @server_random_first: seed is 0 = client_random|server_random,
265  * 1 = server_random|client_random
266  * @out: Buffer for output data from TLS-PRF
267  * @out_len: Length of the output buffer
268  * Returns: 0 on success, -1 on failure
269  *
270  * This function is optional to implement if tls_connection_get_keys() provides
271  * access to master secret and server/client random values. If these values are
272  * not exported from the TLS library, tls_connection_prf() is required so that
273  * further keying material can be derived from the master secret. If not
274  * implemented, the function will still need to be defined, but it can just
275  * return -1. Example implementation of this function is in tls_prf() function
276  * when it is called with seed set to client_random|server_random (or
277  * server_random|client_random).
278  */
279 int __must_check  tls_connection_prf(void *tls_ctx,
280                                      struct tls_connection *conn,
281                                      const char *label,
282                                      int server_random_first,
283                                      u8 *out, size_t out_len);
284
285 /**
286  * tls_connection_handshake - Process TLS handshake (client side)
287  * @tls_ctx: TLS context data from tls_init()
288  * @conn: Connection context data from tls_connection_init()
289  * @in_data: Input data from TLS peer
290  * @in_len: Input data length
291  * @out_len: Length of the output buffer.
292  * @appl_data: Pointer to application data pointer, or %NULL if dropped
293  * @appl_data_len: Pointer to variable that is set to appl_data length
294  * Returns: Pointer to output data, %NULL on failure
295  *
296  * Caller is responsible for freeing returned output data. If the final
297  * handshake message includes application data, this is decrypted and
298  * appl_data (if not %NULL) is set to point this data. Caller is responsible
299  * for freeing appl_data.
300  *
301  * This function is used during TLS handshake. The first call is done with
302  * in_data == %NULL and the library is expected to return ClientHello packet.
303  * This packet is then send to the server and a response from server is given
304  * to TLS library by calling this function again with in_data pointing to the
305  * TLS message from the server.
306  *
307  * If the TLS handshake fails, this function may return %NULL. However, if the
308  * TLS library has a TLS alert to send out, that should be returned as the
309  * output data. In this case, tls_connection_get_failed() must return failure
310  * (> 0).
311  *
312  * tls_connection_established() should return 1 once the TLS handshake has been
313  * completed successfully.
314  */
315 u8 * tls_connection_handshake(void *tls_ctx, struct tls_connection *conn,
316                               const u8 *in_data, size_t in_len,
317                               size_t *out_len, u8 **appl_data,
318                               size_t *appl_data_len);
319
320 /**
321  * tls_connection_server_handshake - Process TLS handshake (server side)
322  * @tls_ctx: TLS context data from tls_init()
323  * @conn: Connection context data from tls_connection_init()
324  * @in_data: Input data from TLS peer
325  * @in_len: Input data length
326  * @out_len: Length of the output buffer.
327  * Returns: pointer to output data, %NULL on failure
328  *
329  * Caller is responsible for freeing returned output data.
330  */
331 u8 * tls_connection_server_handshake(void *tls_ctx,
332                                      struct tls_connection *conn,
333                                      const u8 *in_data, size_t in_len,
334                                      size_t *out_len);
335
336 /**
337  * tls_connection_encrypt - Encrypt data into TLS tunnel
338  * @tls_ctx: TLS context data from tls_init()
339  * @conn: Connection context data from tls_connection_init()
340  * @in_data: Pointer to plaintext data to be encrypted
341  * @in_len: Input buffer length
342  * @out_data: Pointer to output buffer (encrypted TLS data)
343  * @out_len: Maximum out_data length 
344  * Returns: Number of bytes written to out_data, -1 on failure
345  *
346  * This function is used after TLS handshake has been completed successfully to
347  * send data in the encrypted tunnel.
348  */
349 int __must_check tls_connection_encrypt(void *tls_ctx,
350                                         struct tls_connection *conn,
351                                         const u8 *in_data, size_t in_len,
352                                         u8 *out_data, size_t out_len);
353
354 /**
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: Pointer to input buffer (encrypted TLS data)
359  * @in_len: Input buffer length
360  * @out_data: Pointer to output buffer (decrypted data from TLS tunnel)
361  * @out_len: Maximum out_data length
362  * Returns: Number of bytes written to out_data, -1 on failure
363  *
364  * This function is used after TLS handshake has been completed successfully to
365  * receive data from the encrypted tunnel.
366  */
367 int __must_check tls_connection_decrypt(void *tls_ctx,
368                                         struct tls_connection *conn,
369                                         const u8 *in_data, size_t in_len,
370                                         u8 *out_data, size_t out_len);
371
372 /**
373  * tls_connection_resumed - Was session resumption used
374  * @tls_ctx: TLS context data from tls_init()
375  * @conn: Connection context data from tls_connection_init()
376  * Returns: 1 if current session used session resumption, 0 if not
377  */
378 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
379
380 enum {
381         TLS_CIPHER_NONE,
382         TLS_CIPHER_RC4_SHA /* 0x0005 */,
383         TLS_CIPHER_AES128_SHA /* 0x002f */,
384         TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
385         TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
386 };
387
388 /**
389  * tls_connection_set_cipher_list - Configure acceptable cipher suites
390  * @tls_ctx: TLS context data from tls_init()
391  * @conn: Connection context data from tls_connection_init()
392  * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
393  * (TLS_CIPHER_*).
394  * Returns: 0 on success, -1 on failure
395  */
396 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
397                                                 struct tls_connection *conn,
398                                                 u8 *ciphers);
399
400 /**
401  * tls_get_cipher - Get current cipher name
402  * @tls_ctx: TLS context data from tls_init()
403  * @conn: Connection context data from tls_connection_init()
404  * @buf: Buffer for the cipher name
405  * @buflen: buf size
406  * Returns: 0 on success, -1 on failure
407  *
408  * Get the name of the currently used cipher.
409  */
410 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
411                                 char *buf, size_t buflen);
412
413 /**
414  * tls_connection_enable_workaround - Enable TLS workaround options
415  * @tls_ctx: TLS context data from tls_init()
416  * @conn: Connection context data from tls_connection_init()
417  * Returns: 0 on success, -1 on failure
418  *
419  * This function is used to enable connection-specific workaround options for
420  * buffer SSL/TLS implementations.
421  */
422 int __must_check tls_connection_enable_workaround(void *tls_ctx,
423                                                   struct tls_connection *conn);
424
425 /**
426  * tls_connection_client_hello_ext - Set TLS extension for ClientHello
427  * @tls_ctx: TLS context data from tls_init()
428  * @conn: Connection context data from tls_connection_init()
429  * @ext_type: Extension type
430  * @data: Extension payload (%NULL to remove extension)
431  * @data_len: Extension payload length
432  * Returns: 0 on success, -1 on failure
433  */
434 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
435                                                  struct tls_connection *conn,
436                                                  int ext_type, const u8 *data,
437                                                  size_t data_len);
438
439 /**
440  * tls_connection_get_failed - Get connection failure status
441  * @tls_ctx: TLS context data from tls_init()
442  * @conn: Connection context data from tls_connection_init()
443  *
444  * Returns >0 if connection has failed, 0 if not.
445  */
446 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
447
448 /**
449  * tls_connection_get_read_alerts - Get connection read alert status
450  * @tls_ctx: TLS context data from tls_init()
451  * @conn: Connection context data from tls_connection_init()
452  * Returns: Number of times a fatal read (remote end reported error) has
453  * happened during this connection.
454  */
455 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
456
457 /**
458  * tls_connection_get_write_alerts - Get connection write alert status
459  * @tls_ctx: TLS context data from tls_init()
460  * @conn: Connection context data from tls_connection_init()
461  * Returns: Number of times a fatal write (locally detected error) has happened
462  * during this connection.
463  */
464 int tls_connection_get_write_alerts(void *tls_ctx,
465                                     struct tls_connection *conn);
466
467 /**
468  * tls_connection_get_keyblock_size - Get TLS key_block size
469  * @tls_ctx: TLS context data from tls_init()
470  * @conn: Connection context data from tls_connection_init()
471  * Returns: Size of the key_block for the negotiated cipher suite or -1 on
472  * failure
473  */
474 int tls_connection_get_keyblock_size(void *tls_ctx,
475                                      struct tls_connection *conn);
476
477 #define TLS_CAPABILITY_IA 0x0001 /* TLS Inner Application (TLS/IA) */
478 /**
479  * tls_capabilities - Get supported TLS capabilities
480  * @tls_ctx: TLS context data from tls_init()
481  * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*)
482  */
483 unsigned int tls_capabilities(void *tls_ctx);
484
485 /**
486  * tls_connection_ia_send_phase_finished - Send a TLS/IA PhaseFinished message
487  * @tls_ctx: TLS context data from tls_init()
488  * @conn: Connection context data from tls_connection_init()
489  * @final: 1 = FinalPhaseFinished, 0 = IntermediatePhaseFinished
490  * @out_data: Pointer to output buffer (encrypted TLS/IA data)
491  * @out_len: Maximum out_data length 
492  * Returns: Number of bytes written to out_data on success, -1 on failure
493  *
494  * This function is used to send the TLS/IA end phase message, e.g., when the
495  * EAP server completes EAP-TTLSv1.
496  */
497 int __must_check tls_connection_ia_send_phase_finished(
498         void *tls_ctx, struct tls_connection *conn, int final,
499         u8 *out_data, size_t out_len);
500
501 /**
502  * tls_connection_ia_final_phase_finished - Has final phase been completed
503  * @tls_ctx: TLS context data from tls_init()
504  * @conn: Connection context data from tls_connection_init()
505  * Returns: 1 if valid FinalPhaseFinished has been received, 0 if not, or -1
506  * on failure
507  */
508 int __must_check tls_connection_ia_final_phase_finished(
509         void *tls_ctx, struct tls_connection *conn);
510
511 /**
512  * tls_connection_ia_permute_inner_secret - Permute TLS/IA inner secret
513  * @tls_ctx: TLS context data from tls_init()
514  * @conn: Connection context data from tls_connection_init()
515  * @key: Session key material (session_key vectors with 2-octet length), or
516  * %NULL if no session key was generating in the current phase
517  * @key_len: Length of session key material
518  * Returns: 0 on success, -1 on failure
519  */
520 int __must_check tls_connection_ia_permute_inner_secret(
521         void *tls_ctx, struct tls_connection *conn,
522         const u8 *key, size_t key_len);
523
524 typedef int (*tls_session_ticket_cb)
525 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
526  const u8 *server_random, u8 *master_secret);
527
528 int __must_check  tls_connection_set_session_ticket_cb(
529         void *tls_ctx, struct tls_connection *conn,
530         tls_session_ticket_cb cb, void *ctx);
531
532 #endif /* TLS_H */