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