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