tests: Skip ms_funcs module tests in CONFIG_FIPS=y builds
[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_random {
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_random - 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  * @data: 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_random(void *tls_ctx,
322                                          struct tls_connection *conn,
323                                          struct tls_random *data);
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  * tls_connection_prf() is required so that further keying material can be
338  * derived from the master secret. Example implementation of this function is in
339  * tls_prf_sha1_md5() when it is called with seed set to
340  * client_random|server_random (or server_random|client_random). For TLSv1.2 and
341  * newer, a different PRF is needed, though.
342  */
343 int __must_check  tls_connection_prf(void *tls_ctx,
344                                      struct tls_connection *conn,
345                                      const char *label,
346                                      int server_random_first,
347                                      int skip_keyblock,
348                                      u8 *out, size_t out_len);
349
350 /**
351  * tls_connection_handshake - Process TLS handshake (client 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 server
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. If the final
359  * handshake message includes application data, this is decrypted and
360  * appl_data (if not %NULL) is set to point this data. The caller is
361  * responsible for freeing appl_data.
362  *
363  * This function is used during TLS handshake. The first call is done with
364  * in_data == %NULL and the library is expected to return ClientHello packet.
365  * This packet is then send to the server and a response from server is given
366  * to TLS library by calling this function again with in_data pointing to the
367  * TLS message from the server.
368  *
369  * If the TLS handshake fails, this function may return %NULL. However, if the
370  * TLS library has a TLS alert to send out, that should be returned as the
371  * output data. In this case, tls_connection_get_failed() must return failure
372  * (> 0).
373  *
374  * tls_connection_established() should return 1 once the TLS handshake has been
375  * completed successfully.
376  */
377 struct wpabuf * tls_connection_handshake(void *tls_ctx,
378                                          struct tls_connection *conn,
379                                          const struct wpabuf *in_data,
380                                          struct wpabuf **appl_data);
381
382 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
383                                           struct tls_connection *conn,
384                                           const struct wpabuf *in_data,
385                                           struct wpabuf **appl_data,
386                                           int *more_data_needed);
387
388 /**
389  * tls_connection_server_handshake - Process TLS handshake (server side)
390  * @tls_ctx: TLS context data from tls_init()
391  * @conn: Connection context data from tls_connection_init()
392  * @in_data: Input data from TLS peer
393  * @appl_data: Pointer to application data pointer, or %NULL if dropped
394  * Returns: Output data, %NULL on failure
395  *
396  * The caller is responsible for freeing the returned output data.
397  */
398 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
399                                                 struct tls_connection *conn,
400                                                 const struct wpabuf *in_data,
401                                                 struct wpabuf **appl_data);
402
403 /**
404  * tls_connection_encrypt - Encrypt data into TLS tunnel
405  * @tls_ctx: TLS context data from tls_init()
406  * @conn: Connection context data from tls_connection_init()
407  * @in_data: Plaintext data to be encrypted
408  * Returns: Encrypted TLS data or %NULL on failure
409  *
410  * This function is used after TLS handshake has been completed successfully to
411  * send data in the encrypted tunnel. The caller is responsible for freeing the
412  * returned output data.
413  */
414 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
415                                        struct tls_connection *conn,
416                                        const struct wpabuf *in_data);
417
418 /**
419  * tls_connection_decrypt - Decrypt data from TLS tunnel
420  * @tls_ctx: TLS context data from tls_init()
421  * @conn: Connection context data from tls_connection_init()
422  * @in_data: Encrypted TLS data
423  * Returns: Decrypted TLS data or %NULL on failure
424  *
425  * This function is used after TLS handshake has been completed successfully to
426  * receive data from the encrypted tunnel. The caller is responsible for
427  * freeing the returned output data.
428  */
429 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
430                                        struct tls_connection *conn,
431                                        const struct wpabuf *in_data);
432
433 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
434                                         struct tls_connection *conn,
435                                         const struct wpabuf *in_data,
436                                         int *more_data_needed);
437
438 /**
439  * tls_connection_resumed - Was session resumption used
440  * @tls_ctx: TLS context data from tls_init()
441  * @conn: Connection context data from tls_connection_init()
442  * Returns: 1 if current session used session resumption, 0 if not
443  */
444 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
445
446 enum {
447         TLS_CIPHER_NONE,
448         TLS_CIPHER_RC4_SHA /* 0x0005 */,
449         TLS_CIPHER_AES128_SHA /* 0x002f */,
450         TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
451         TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
452 };
453
454 /**
455  * tls_connection_set_cipher_list - Configure acceptable cipher suites
456  * @tls_ctx: TLS context data from tls_init()
457  * @conn: Connection context data from tls_connection_init()
458  * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
459  * (TLS_CIPHER_*).
460  * Returns: 0 on success, -1 on failure
461  */
462 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
463                                                 struct tls_connection *conn,
464                                                 u8 *ciphers);
465
466 /**
467  * tls_get_version - Get the current TLS version number
468  * @tls_ctx: TLS context data from tls_init()
469  * @conn: Connection context data from tls_connection_init()
470  * @buf: Buffer for returning the TLS version number
471  * @buflen: buf size
472  * Returns: 0 on success, -1 on failure
473  *
474  * Get the currently used TLS version number.
475  */
476 int __must_check tls_get_version(void *tls_ctx, struct tls_connection *conn,
477                                  char *buf, size_t buflen);
478
479 /**
480  * tls_get_cipher - Get current cipher name
481  * @tls_ctx: TLS context data from tls_init()
482  * @conn: Connection context data from tls_connection_init()
483  * @buf: Buffer for the cipher name
484  * @buflen: buf size
485  * Returns: 0 on success, -1 on failure
486  *
487  * Get the name of the currently used cipher.
488  */
489 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
490                                 char *buf, size_t buflen);
491
492 /**
493  * tls_connection_enable_workaround - Enable TLS workaround options
494  * @tls_ctx: TLS context data from tls_init()
495  * @conn: Connection context data from tls_connection_init()
496  * Returns: 0 on success, -1 on failure
497  *
498  * This function is used to enable connection-specific workaround options for
499  * buffer SSL/TLS implementations.
500  */
501 int __must_check tls_connection_enable_workaround(void *tls_ctx,
502                                                   struct tls_connection *conn);
503
504 /**
505  * tls_connection_client_hello_ext - Set TLS extension for ClientHello
506  * @tls_ctx: TLS context data from tls_init()
507  * @conn: Connection context data from tls_connection_init()
508  * @ext_type: Extension type
509  * @data: Extension payload (%NULL to remove extension)
510  * @data_len: Extension payload length
511  * Returns: 0 on success, -1 on failure
512  */
513 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
514                                                  struct tls_connection *conn,
515                                                  int ext_type, const u8 *data,
516                                                  size_t data_len);
517
518 /**
519  * tls_connection_get_failed - Get connection failure status
520  * @tls_ctx: TLS context data from tls_init()
521  * @conn: Connection context data from tls_connection_init()
522  *
523  * Returns >0 if connection has failed, 0 if not.
524  */
525 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
526
527 /**
528  * tls_connection_get_read_alerts - Get connection read alert status
529  * @tls_ctx: TLS context data from tls_init()
530  * @conn: Connection context data from tls_connection_init()
531  * Returns: Number of times a fatal read (remote end reported error) has
532  * happened during this connection.
533  */
534 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
535
536 /**
537  * tls_connection_get_write_alerts - Get connection write alert status
538  * @tls_ctx: TLS context data from tls_init()
539  * @conn: Connection context data from tls_connection_init()
540  * Returns: Number of times a fatal write (locally detected error) has happened
541  * during this connection.
542  */
543 int tls_connection_get_write_alerts(void *tls_ctx,
544                                     struct tls_connection *conn);
545
546 typedef int (*tls_session_ticket_cb)
547 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
548  const u8 *server_random, u8 *master_secret);
549
550 int __must_check  tls_connection_set_session_ticket_cb(
551         void *tls_ctx, struct tls_connection *conn,
552         tls_session_ticket_cb cb, void *ctx);
553
554 void tls_connection_set_log_cb(struct tls_connection *conn,
555                                void (*log_cb)(void *ctx, const char *msg),
556                                void *ctx);
557
558 #define TLS_BREAK_VERIFY_DATA BIT(0)
559 #define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
560 #define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
561 #define TLS_DHE_PRIME_511B BIT(3)
562 #define TLS_DHE_PRIME_767B BIT(4)
563 #define TLS_DHE_PRIME_15 BIT(5)
564 #define TLS_DHE_PRIME_58B BIT(6)
565 #define TLS_DHE_NON_PRIME BIT(7)
566
567 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
568
569 int tls_get_library_version(char *buf, size_t buf_len);
570
571 #endif /* TLS_H */