blob: 413cccddc84dc012247e5c6d91271ccdd1666ebc [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * SSL/TLS interface definition
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#ifndef TLS_H
10#define TLS_H
11
12struct tls_connection;
13
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080014struct tls_random {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015 const u8 *client_random;
16 size_t client_random_len;
17 const u8 *server_random;
18 size_t server_random_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019};
20
21enum tls_event {
Dmitry Shmidt04949592012-07-19 12:16:46 -070022 TLS_CERT_CHAIN_SUCCESS,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023 TLS_CERT_CHAIN_FAILURE,
Dmitry Shmidt04949592012-07-19 12:16:46 -070024 TLS_PEER_CERTIFICATE,
25 TLS_ALERT
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026};
27
28/*
29 * Note: These are used as identifier with external programs and as such, the
30 * values must not be changed.
31 */
32enum 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,
Dmitry Shmidt051af732013-10-22 13:52:46 -070041 TLS_FAIL_SERVER_CHAIN_PROBE = 8,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080042 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH = 9,
43 TLS_FAIL_DOMAIN_MISMATCH = 10,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070044 TLS_FAIL_INSUFFICIENT_KEY_LEN = 11,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045};
46
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080047
48#define TLS_MAX_ALT_SUBJECT 10
49
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050union tls_event_data {
51 struct {
52 int depth;
53 const char *subject;
54 enum tls_fail_reason reason;
55 const char *reason_txt;
56 const struct wpabuf *cert;
57 } cert_fail;
58
59 struct {
60 int depth;
61 const char *subject;
62 const struct wpabuf *cert;
63 const u8 *hash;
64 size_t hash_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080065 const char *altsubject[TLS_MAX_ALT_SUBJECT];
66 int num_altsubject;
Hai Shalom39ba6fc2019-01-22 12:40:38 -080067 const char *serial_num;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 } peer_cert;
Dmitry Shmidt04949592012-07-19 12:16:46 -070069
70 struct {
71 int is_local;
72 const char *type;
73 const char *description;
74 } alert;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075};
76
77struct tls_config {
78 const char *opensc_engine_path;
79 const char *pkcs11_engine_path;
80 const char *pkcs11_module_path;
81 int fips_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080082 int cert_in_cb;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080083 const char *openssl_ciphers;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080084 unsigned int tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -080085 unsigned int crl_reload_interval;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070086 unsigned int tls_flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087
88 void (*event_cb)(void *ctx, enum tls_event ev,
89 union tls_event_data *data);
90 void *cb_ctx;
91};
92
93#define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
94#define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070095#define TLS_CONN_DISABLE_SESSION_TICKET BIT(2)
Dmitry Shmidt34af3062013-07-11 10:46:32 -070096#define TLS_CONN_REQUEST_OCSP BIT(3)
97#define TLS_CONN_REQUIRE_OCSP BIT(4)
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -080098#define TLS_CONN_DISABLE_TLSv1_1 BIT(5)
99#define TLS_CONN_DISABLE_TLSv1_2 BIT(6)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800100#define TLS_CONN_EAP_FAST BIT(7)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101#define TLS_CONN_DISABLE_TLSv1_0 BIT(8)
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800102#define TLS_CONN_EXT_CERT_CHECK BIT(9)
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800103#define TLS_CONN_REQUIRE_OCSP_ALL BIT(10)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700104#define TLS_CONN_SUITEB BIT(11)
105#define TLS_CONN_SUITEB_NO_ECDH BIT(12)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700106#define TLS_CONN_DISABLE_TLSv1_3 BIT(13)
Hai Shalom74f70d42019-02-11 14:42:39 -0800107#define TLS_CONN_ENABLE_TLSv1_0 BIT(14)
108#define TLS_CONN_ENABLE_TLSv1_1 BIT(15)
109#define TLS_CONN_ENABLE_TLSv1_2 BIT(16)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110
111/**
112 * struct tls_connection_params - Parameters for TLS connection
113 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
114 * format
115 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
116 * @ca_cert_blob_len: ca_cert_blob length
117 * @ca_path: Path to CA certificates (OpenSSL specific)
118 * @subject_match: String to match in the subject of the peer certificate or
119 * %NULL to allow all subjects
120 * @altsubject_match: String to match in the alternative subject of the peer
121 * certificate or %NULL to allow all alternative subjects
Dmitry Shmidt051af732013-10-22 13:52:46 -0700122 * @suffix_match: String to suffix match in the dNSName or CN of the peer
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800123 * certificate or %NULL to allow all domain names. This may allow subdomains an
124 * wildcard certificates. Each domain name label must have a full match.
125 * @domain_match: String to match in the dNSName or CN of the peer
126 * certificate or %NULL to allow all domain names. This requires a full,
127 * case-insensitive match.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 * @client_cert: File or reference name for client X.509 certificate in PEM or
129 * DER format
130 * @client_cert_blob: client_cert as inlined data or %NULL if not used
131 * @client_cert_blob_len: client_cert_blob length
132 * @private_key: File or reference name for client private key in PEM or DER
133 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
134 * @private_key_blob: private_key as inlined data or %NULL if not used
135 * @private_key_blob_len: private_key_blob length
136 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
137 * passphrase is used.
138 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
139 * @dh_blob: dh_file as inlined data or %NULL if not used
140 * @dh_blob_len: dh_blob length
141 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
142 * (this is OpenSSL specific for now)
143 * @engine_id: engine id string (this is OpenSSL specific for now)
144 * @ppin: pointer to the pin variable in the configuration
145 * (this is OpenSSL specific for now)
146 * @key_id: the private key's id when using engine (this is OpenSSL
147 * specific for now)
148 * @cert_id: the certificate's id when using engine
149 * @ca_cert_id: the CA certificate's id when using engine
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150 * @openssl_ciphers: OpenSSL cipher configuration
Hai Shalom74f70d42019-02-11 14:42:39 -0800151 * @openssl_ecdh_curves: OpenSSL ECDH curve configuration. %NULL for auto if
152 * supported, empty string to disable, or a colon-separated curve list.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 * @flags: Parameter options (TLS_CONN_*)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700154 * @ocsp_stapling_response: DER encoded file with cached OCSP stapling response
155 * or %NULL if OCSP is not enabled
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800156 * @ocsp_stapling_response_multi: DER encoded file with cached OCSP stapling
157 * response list (OCSPResponseList for ocsp_multi in RFC 6961) or %NULL if
158 * ocsp_multi is not enabled
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 *
160 * TLS connection parameters to be configured with tls_connection_set_params()
161 * and tls_global_set_params().
162 *
163 * Certificates and private key can be configured either as a reference name
164 * (file path or reference to certificate store) or by providing the same data
165 * as a pointer to the data in memory. Only one option will be used for each
166 * field.
167 */
168struct tls_connection_params {
169 const char *ca_cert;
170 const u8 *ca_cert_blob;
171 size_t ca_cert_blob_len;
172 const char *ca_path;
173 const char *subject_match;
174 const char *altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700175 const char *suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800176 const char *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 const char *client_cert;
178 const u8 *client_cert_blob;
179 size_t client_cert_blob_len;
180 const char *private_key;
181 const u8 *private_key_blob;
182 size_t private_key_blob_len;
183 const char *private_key_passwd;
184 const char *dh_file;
185 const u8 *dh_blob;
186 size_t dh_blob_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
188 /* OpenSSL specific variables */
189 int engine;
190 const char *engine_id;
191 const char *pin;
192 const char *key_id;
193 const char *cert_id;
194 const char *ca_cert_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800195 const char *openssl_ciphers;
Hai Shalom74f70d42019-02-11 14:42:39 -0800196 const char *openssl_ecdh_curves;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197
198 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700199 const char *ocsp_stapling_response;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800200 const char *ocsp_stapling_response_multi;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201};
202
203
204/**
205 * tls_init - Initialize TLS library
206 * @conf: Configuration data for TLS library
207 * Returns: Context data to be used as tls_ctx in calls to other functions,
208 * or %NULL on failure.
209 *
210 * Called once during program startup and once for each RSN pre-authentication
211 * session. In other words, there can be two concurrent TLS contexts. If global
212 * library initialization is needed (i.e., one that is shared between both
213 * authentication types), the TLS library wrapper should maintain a reference
214 * counter and do global initialization only when moving from 0 to 1 reference.
215 */
216void * tls_init(const struct tls_config *conf);
217
218/**
219 * tls_deinit - Deinitialize TLS library
220 * @tls_ctx: TLS context data from tls_init()
221 *
222 * Called once during program shutdown and once for each RSN pre-authentication
223 * session. If global library deinitialization is needed (i.e., one that is
224 * shared between both authentication types), the TLS library wrapper should
225 * maintain a reference counter and do global deinitialization only when moving
226 * from 1 to 0 references.
227 */
228void tls_deinit(void *tls_ctx);
229
230/**
231 * tls_get_errors - Process pending errors
232 * @tls_ctx: TLS context data from tls_init()
233 * Returns: Number of found error, 0 if no errors detected.
234 *
235 * Process all pending TLS errors.
236 */
237int tls_get_errors(void *tls_ctx);
238
239/**
240 * tls_connection_init - Initialize a new TLS connection
241 * @tls_ctx: TLS context data from tls_init()
242 * Returns: Connection context data, conn for other function calls
243 */
244struct tls_connection * tls_connection_init(void *tls_ctx);
245
246/**
247 * tls_connection_deinit - Free TLS connection data
248 * @tls_ctx: TLS context data from tls_init()
249 * @conn: Connection context data from tls_connection_init()
250 *
251 * Release all resources allocated for TLS connection.
252 */
253void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
254
255/**
256 * tls_connection_established - Has the TLS connection been completed?
257 * @tls_ctx: TLS context data from tls_init()
258 * @conn: Connection context data from tls_connection_init()
259 * Returns: 1 if TLS connection has been completed, 0 if not.
260 */
261int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
262
263/**
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800264 * tls_connection_peer_serial_num - Fetch peer certificate serial number
265 * @tls_ctx: TLS context data from tls_init()
266 * @conn: Connection context data from tls_connection_init()
267 * Returns: Allocated string buffer containing the peer certificate serial
268 * number or %NULL on error.
269 *
270 * The caller is responsible for freeing the returned buffer with os_free().
271 */
272char * tls_connection_peer_serial_num(void *tls_ctx,
273 struct tls_connection *conn);
274
275/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 * tls_connection_shutdown - Shutdown TLS connection
277 * @tls_ctx: TLS context data from tls_init()
278 * @conn: Connection context data from tls_connection_init()
279 * Returns: 0 on success, -1 on failure
280 *
281 * Shutdown current TLS connection without releasing all resources. New
282 * connection can be started by using the same conn without having to call
283 * tls_connection_init() or setting certificates etc. again. The new
284 * connection should try to use session resumption.
285 */
286int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
287
288enum {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700289 TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN = -4,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
291 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
292};
293
294/**
295 * tls_connection_set_params - Set TLS connection parameters
296 * @tls_ctx: TLS context data from tls_init()
297 * @conn: Connection context data from tls_connection_init()
298 * @params: Connection parameters
299 * Returns: 0 on success, -1 on failure,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700300 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
301 * failure, or
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700303 * PKCS#11 engine private key, or
304 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
305 * failure.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 */
307int __must_check
308tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
309 const struct tls_connection_params *params);
310
311/**
312 * tls_global_set_params - Set TLS parameters for all TLS connection
313 * @tls_ctx: TLS context data from tls_init()
314 * @params: Global TLS parameters
315 * Returns: 0 on success, -1 on failure,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700316 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
317 * failure, or
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700319 * PKCS#11 engine private key, or
320 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
321 * failure.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 */
323int __must_check tls_global_set_params(
324 void *tls_ctx, const struct tls_connection_params *params);
325
326/**
327 * tls_global_set_verify - Set global certificate verification options
328 * @tls_ctx: TLS context data from tls_init()
329 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
330 * 2 = verify CRL for all certificates
Hai Shalom74f70d42019-02-11 14:42:39 -0800331 * @strict: 0 = allow CRL time errors, 1 = do not allow CRL time errors
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 * Returns: 0 on success, -1 on failure
333 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800334int __must_check tls_global_set_verify(void *tls_ctx, int check_crl,
335 int strict);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336
337/**
338 * tls_connection_set_verify - Set certificate verification options
339 * @tls_ctx: TLS context data from tls_init()
340 * @conn: Connection context data from tls_connection_init()
341 * @verify_peer: 1 = verify peer certificate
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800342 * @flags: Connection flags (TLS_CONN_*)
343 * @session_ctx: Session caching context or %NULL to use default
344 * @session_ctx_len: Length of @session_ctx in bytes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 * Returns: 0 on success, -1 on failure
346 */
347int __must_check tls_connection_set_verify(void *tls_ctx,
348 struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800349 int verify_peer,
350 unsigned int flags,
351 const u8 *session_ctx,
352 size_t session_ctx_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353
354/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800355 * tls_connection_get_random - Get random data from TLS connection
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 * @tls_ctx: TLS context data from tls_init()
357 * @conn: Connection context data from tls_connection_init()
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800358 * @data: Structure of client/server random data (filled on success)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 * Returns: 0 on success, -1 on failure
360 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800361int __must_check tls_connection_get_random(void *tls_ctx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800363 struct tls_random *data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364
365/**
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700366 * tls_connection_export_key - Derive keying material from a TLS connection
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 * @tls_ctx: TLS context data from tls_init()
368 * @conn: Connection context data from tls_connection_init()
369 * @label: Label (e.g., description of the key) for PRF
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370 * @out: Buffer for output data from TLS-PRF
371 * @out_len: Length of the output buffer
372 * Returns: 0 on success, -1 on failure
373 *
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700374 * Exports keying material using the mechanism described in RFC 5705.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700376int __must_check tls_connection_export_key(void *tls_ctx,
377 struct tls_connection *conn,
378 const char *label,
379 u8 *out, size_t out_len);
380
381/**
382 * tls_connection_get_eap_fast_key - Derive key material for EAP-FAST
383 * @tls_ctx: TLS context data from tls_init()
384 * @conn: Connection context data from tls_connection_init()
385 * @out: Buffer for output data from TLS-PRF
386 * @out_len: Length of the output buffer
387 * Returns: 0 on success, -1 on failure
388 *
389 * Exports key material after the normal TLS key block for use with
390 * EAP-FAST. Most callers will want tls_connection_export_key(), but EAP-FAST
391 * uses a different legacy mechanism.
392 */
393int __must_check tls_connection_get_eap_fast_key(void *tls_ctx,
394 struct tls_connection *conn,
395 u8 *out, size_t out_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396
397/**
398 * tls_connection_handshake - Process TLS handshake (client side)
399 * @tls_ctx: TLS context data from tls_init()
400 * @conn: Connection context data from tls_connection_init()
401 * @in_data: Input data from TLS server
402 * @appl_data: Pointer to application data pointer, or %NULL if dropped
403 * Returns: Output data, %NULL on failure
404 *
405 * The caller is responsible for freeing the returned output data. If the final
406 * handshake message includes application data, this is decrypted and
407 * appl_data (if not %NULL) is set to point this data. The caller is
408 * responsible for freeing appl_data.
409 *
410 * This function is used during TLS handshake. The first call is done with
411 * in_data == %NULL and the library is expected to return ClientHello packet.
412 * This packet is then send to the server and a response from server is given
413 * to TLS library by calling this function again with in_data pointing to the
414 * TLS message from the server.
415 *
416 * If the TLS handshake fails, this function may return %NULL. However, if the
417 * TLS library has a TLS alert to send out, that should be returned as the
418 * output data. In this case, tls_connection_get_failed() must return failure
419 * (> 0).
420 *
421 * tls_connection_established() should return 1 once the TLS handshake has been
422 * completed successfully.
423 */
424struct wpabuf * tls_connection_handshake(void *tls_ctx,
425 struct tls_connection *conn,
426 const struct wpabuf *in_data,
427 struct wpabuf **appl_data);
428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800429struct wpabuf * tls_connection_handshake2(void *tls_ctx,
430 struct tls_connection *conn,
431 const struct wpabuf *in_data,
432 struct wpabuf **appl_data,
433 int *more_data_needed);
434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435/**
436 * tls_connection_server_handshake - Process TLS handshake (server side)
437 * @tls_ctx: TLS context data from tls_init()
438 * @conn: Connection context data from tls_connection_init()
439 * @in_data: Input data from TLS peer
440 * @appl_data: Pointer to application data pointer, or %NULL if dropped
441 * Returns: Output data, %NULL on failure
442 *
443 * The caller is responsible for freeing the returned output data.
444 */
445struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
446 struct tls_connection *conn,
447 const struct wpabuf *in_data,
448 struct wpabuf **appl_data);
449
450/**
451 * tls_connection_encrypt - Encrypt data into TLS tunnel
452 * @tls_ctx: TLS context data from tls_init()
453 * @conn: Connection context data from tls_connection_init()
454 * @in_data: Plaintext data to be encrypted
455 * Returns: Encrypted TLS data or %NULL on failure
456 *
457 * This function is used after TLS handshake has been completed successfully to
458 * send data in the encrypted tunnel. The caller is responsible for freeing the
459 * returned output data.
460 */
461struct wpabuf * tls_connection_encrypt(void *tls_ctx,
462 struct tls_connection *conn,
463 const struct wpabuf *in_data);
464
465/**
466 * tls_connection_decrypt - Decrypt data from TLS tunnel
467 * @tls_ctx: TLS context data from tls_init()
468 * @conn: Connection context data from tls_connection_init()
469 * @in_data: Encrypted TLS data
470 * Returns: Decrypted TLS data or %NULL on failure
471 *
472 * This function is used after TLS handshake has been completed successfully to
473 * receive data from the encrypted tunnel. The caller is responsible for
474 * freeing the returned output data.
475 */
476struct wpabuf * tls_connection_decrypt(void *tls_ctx,
477 struct tls_connection *conn,
478 const struct wpabuf *in_data);
479
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800480struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
481 struct tls_connection *conn,
482 const struct wpabuf *in_data,
483 int *more_data_needed);
484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485/**
486 * tls_connection_resumed - Was session resumption used
487 * @tls_ctx: TLS context data from tls_init()
488 * @conn: Connection context data from tls_connection_init()
489 * Returns: 1 if current session used session resumption, 0 if not
490 */
491int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
492
493enum {
494 TLS_CIPHER_NONE,
495 TLS_CIPHER_RC4_SHA /* 0x0005 */,
496 TLS_CIPHER_AES128_SHA /* 0x002f */,
497 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800498 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */,
499 TLS_CIPHER_RSA_DHE_AES256_SHA /* 0x0039 */,
500 TLS_CIPHER_AES256_SHA /* 0x0035 */,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501};
502
503/**
504 * tls_connection_set_cipher_list - Configure acceptable cipher suites
505 * @tls_ctx: TLS context data from tls_init()
506 * @conn: Connection context data from tls_connection_init()
507 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
508 * (TLS_CIPHER_*).
509 * Returns: 0 on success, -1 on failure
510 */
511int __must_check tls_connection_set_cipher_list(void *tls_ctx,
512 struct tls_connection *conn,
513 u8 *ciphers);
514
515/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800516 * tls_get_version - Get the current TLS version number
517 * @tls_ctx: TLS context data from tls_init()
518 * @conn: Connection context data from tls_connection_init()
519 * @buf: Buffer for returning the TLS version number
520 * @buflen: buf size
521 * Returns: 0 on success, -1 on failure
522 *
523 * Get the currently used TLS version number.
524 */
525int __must_check tls_get_version(void *tls_ctx, struct tls_connection *conn,
526 char *buf, size_t buflen);
527
528/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529 * tls_get_cipher - Get current cipher name
530 * @tls_ctx: TLS context data from tls_init()
531 * @conn: Connection context data from tls_connection_init()
532 * @buf: Buffer for the cipher name
533 * @buflen: buf size
534 * Returns: 0 on success, -1 on failure
535 *
536 * Get the name of the currently used cipher.
537 */
538int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
539 char *buf, size_t buflen);
540
541/**
542 * tls_connection_enable_workaround - Enable TLS workaround options
543 * @tls_ctx: TLS context data from tls_init()
544 * @conn: Connection context data from tls_connection_init()
545 * Returns: 0 on success, -1 on failure
546 *
547 * This function is used to enable connection-specific workaround options for
548 * buffer SSL/TLS implementations.
549 */
550int __must_check tls_connection_enable_workaround(void *tls_ctx,
551 struct tls_connection *conn);
552
553/**
554 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
555 * @tls_ctx: TLS context data from tls_init()
556 * @conn: Connection context data from tls_connection_init()
557 * @ext_type: Extension type
558 * @data: Extension payload (%NULL to remove extension)
559 * @data_len: Extension payload length
560 * Returns: 0 on success, -1 on failure
561 */
562int __must_check tls_connection_client_hello_ext(void *tls_ctx,
563 struct tls_connection *conn,
564 int ext_type, const u8 *data,
565 size_t data_len);
566
567/**
568 * tls_connection_get_failed - Get connection failure status
569 * @tls_ctx: TLS context data from tls_init()
570 * @conn: Connection context data from tls_connection_init()
571 *
572 * Returns >0 if connection has failed, 0 if not.
573 */
574int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
575
576/**
577 * tls_connection_get_read_alerts - Get connection read alert status
578 * @tls_ctx: TLS context data from tls_init()
579 * @conn: Connection context data from tls_connection_init()
580 * Returns: Number of times a fatal read (remote end reported error) has
581 * happened during this connection.
582 */
583int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
584
585/**
586 * tls_connection_get_write_alerts - Get connection write alert status
587 * @tls_ctx: TLS context data from tls_init()
588 * @conn: Connection context data from tls_connection_init()
589 * Returns: Number of times a fatal write (locally detected error) has happened
590 * during this connection.
591 */
592int tls_connection_get_write_alerts(void *tls_ctx,
593 struct tls_connection *conn);
594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595typedef int (*tls_session_ticket_cb)
596(void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
597 const u8 *server_random, u8 *master_secret);
598
599int __must_check tls_connection_set_session_ticket_cb(
600 void *tls_ctx, struct tls_connection *conn,
601 tls_session_ticket_cb cb, void *ctx);
602
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700603void tls_connection_set_log_cb(struct tls_connection *conn,
604 void (*log_cb)(void *ctx, const char *msg),
605 void *ctx);
606
607#define TLS_BREAK_VERIFY_DATA BIT(0)
608#define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
609#define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700610#define TLS_DHE_PRIME_511B BIT(3)
611#define TLS_DHE_PRIME_767B BIT(4)
612#define TLS_DHE_PRIME_15 BIT(5)
613#define TLS_DHE_PRIME_58B BIT(6)
614#define TLS_DHE_NON_PRIME BIT(7)
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700615
616void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
617
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800618int tls_get_library_version(char *buf, size_t buf_len);
619
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800620void tls_connection_set_success_data(struct tls_connection *conn,
621 struct wpabuf *data);
622
623void tls_connection_set_success_data_resumed(struct tls_connection *conn);
624
625const struct wpabuf *
626tls_connection_get_success_data(struct tls_connection *conn);
627
628void tls_connection_remove_session(struct tls_connection *conn);
629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630#endif /* TLS_H */