blob: e215762ff27884cedde47ede5f2d47f8751acba8 [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,
Sunil Ravia04bd252022-05-02 22:54:18 -070025 TLS_ALERT,
26 TLS_UNSAFE_RENEGOTIATION_DISABLED,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027};
28
29/*
30 * Note: These are used as identifier with external programs and as such, the
31 * values must not be changed.
32 */
33enum tls_fail_reason {
34 TLS_FAIL_UNSPECIFIED = 0,
35 TLS_FAIL_UNTRUSTED = 1,
36 TLS_FAIL_REVOKED = 2,
37 TLS_FAIL_NOT_YET_VALID = 3,
38 TLS_FAIL_EXPIRED = 4,
39 TLS_FAIL_SUBJECT_MISMATCH = 5,
40 TLS_FAIL_ALTSUBJECT_MISMATCH = 6,
41 TLS_FAIL_BAD_CERTIFICATE = 7,
Dmitry Shmidt051af732013-10-22 13:52:46 -070042 TLS_FAIL_SERVER_CHAIN_PROBE = 8,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080043 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH = 9,
44 TLS_FAIL_DOMAIN_MISMATCH = 10,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070045 TLS_FAIL_INSUFFICIENT_KEY_LEN = 11,
Hai Shalom021b0b52019-04-10 11:17:58 -070046 TLS_FAIL_DN_MISMATCH = 12,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047};
48
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080049
50#define TLS_MAX_ALT_SUBJECT 10
51
Hai Shalom81f62d82019-07-22 12:10:00 -070052struct tls_cert_data {
53 int depth;
54 const char *subject;
55 const struct wpabuf *cert;
56 const u8 *hash;
57 size_t hash_len;
58 const char *altsubject[TLS_MAX_ALT_SUBJECT];
59 int num_altsubject;
60 const char *serial_num;
61 int tod;
62};
63
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064union tls_event_data {
65 struct {
66 int depth;
67 const char *subject;
68 enum tls_fail_reason reason;
69 const char *reason_txt;
70 const struct wpabuf *cert;
71 } cert_fail;
72
Hai Shalom81f62d82019-07-22 12:10:00 -070073 struct tls_cert_data peer_cert;
Dmitry Shmidt04949592012-07-19 12:16:46 -070074
75 struct {
76 int is_local;
77 const char *type;
78 const char *description;
79 } alert;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080};
81
82struct tls_config {
83 const char *opensc_engine_path;
84 const char *pkcs11_engine_path;
85 const char *pkcs11_module_path;
86 int fips_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080087 int cert_in_cb;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080088 const char *openssl_ciphers;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080089 unsigned int tls_session_lifetime;
Hai Shalom74f70d42019-02-11 14:42:39 -080090 unsigned int crl_reload_interval;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070091 unsigned int tls_flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092
93 void (*event_cb)(void *ctx, enum tls_event ev,
94 union tls_event_data *data);
95 void *cb_ctx;
96};
97
98#define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
99#define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100#define TLS_CONN_DISABLE_SESSION_TICKET BIT(2)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700101#define TLS_CONN_REQUEST_OCSP BIT(3)
102#define TLS_CONN_REQUIRE_OCSP BIT(4)
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800103#define TLS_CONN_DISABLE_TLSv1_1 BIT(5)
104#define TLS_CONN_DISABLE_TLSv1_2 BIT(6)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800105#define TLS_CONN_EAP_FAST BIT(7)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800106#define TLS_CONN_DISABLE_TLSv1_0 BIT(8)
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800107#define TLS_CONN_EXT_CERT_CHECK BIT(9)
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800108#define TLS_CONN_REQUIRE_OCSP_ALL BIT(10)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700109#define TLS_CONN_SUITEB BIT(11)
110#define TLS_CONN_SUITEB_NO_ECDH BIT(12)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700111#define TLS_CONN_DISABLE_TLSv1_3 BIT(13)
Hai Shalom74f70d42019-02-11 14:42:39 -0800112#define TLS_CONN_ENABLE_TLSv1_0 BIT(14)
113#define TLS_CONN_ENABLE_TLSv1_1 BIT(15)
114#define TLS_CONN_ENABLE_TLSv1_2 BIT(16)
Hai Shalom81f62d82019-07-22 12:10:00 -0700115#define TLS_CONN_TEAP_ANON_DH BIT(17)
Sunil Ravia04bd252022-05-02 22:54:18 -0700116#define TLS_CONN_ALLOW_UNSAFE_RENEGOTIATION BIT(18)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117
118/**
119 * struct tls_connection_params - Parameters for TLS connection
120 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
121 * format
122 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
123 * @ca_cert_blob_len: ca_cert_blob length
124 * @ca_path: Path to CA certificates (OpenSSL specific)
125 * @subject_match: String to match in the subject of the peer certificate or
126 * %NULL to allow all subjects
127 * @altsubject_match: String to match in the alternative subject of the peer
128 * certificate or %NULL to allow all alternative subjects
Hai Shalom021b0b52019-04-10 11:17:58 -0700129 * @suffix_match: Semicolon deliminated string of values to suffix match against
130 * the dNSName or CN of the peer certificate or %NULL to allow all domain names.
131 * This may allow subdomains and wildcard certificates. Each domain name label
132 * must have a full case-insensitive match.
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800133 * @domain_match: String to match in the dNSName or CN of the peer
134 * certificate or %NULL to allow all domain names. This requires a full,
135 * case-insensitive match.
Hai Shalom021b0b52019-04-10 11:17:58 -0700136 *
137 * More than one match string can be provided by using semicolons to
138 * separate the strings (e.g., example.org;example.com). When multiple
139 * strings are specified, a match with any one of the values is
140 * considered a sufficient match for the certificate, i.e., the
141 * conditions are ORed together.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 * @client_cert: File or reference name for client X.509 certificate in PEM or
143 * DER format
144 * @client_cert_blob: client_cert as inlined data or %NULL if not used
145 * @client_cert_blob_len: client_cert_blob length
146 * @private_key: File or reference name for client private key in PEM or DER
147 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
148 * @private_key_blob: private_key as inlined data or %NULL if not used
149 * @private_key_blob_len: private_key_blob length
150 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
151 * passphrase is used.
152 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
154 * (this is OpenSSL specific for now)
155 * @engine_id: engine id string (this is OpenSSL specific for now)
156 * @ppin: pointer to the pin variable in the configuration
157 * (this is OpenSSL specific for now)
158 * @key_id: the private key's id when using engine (this is OpenSSL
159 * specific for now)
160 * @cert_id: the certificate's id when using engine
161 * @ca_cert_id: the CA certificate's id when using engine
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800162 * @openssl_ciphers: OpenSSL cipher configuration
Hai Shalom74f70d42019-02-11 14:42:39 -0800163 * @openssl_ecdh_curves: OpenSSL ECDH curve configuration. %NULL for auto if
164 * supported, empty string to disable, or a colon-separated curve list.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 * @flags: Parameter options (TLS_CONN_*)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700166 * @ocsp_stapling_response: DER encoded file with cached OCSP stapling response
167 * or %NULL if OCSP is not enabled
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800168 * @ocsp_stapling_response_multi: DER encoded file with cached OCSP stapling
169 * response list (OCSPResponseList for ocsp_multi in RFC 6961) or %NULL if
170 * ocsp_multi is not enabled
Hai Shalom021b0b52019-04-10 11:17:58 -0700171 * @check_cert_subject: Client certificate subject name matching string
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172 *
173 * TLS connection parameters to be configured with tls_connection_set_params()
174 * and tls_global_set_params().
175 *
176 * Certificates and private key can be configured either as a reference name
177 * (file path or reference to certificate store) or by providing the same data
178 * as a pointer to the data in memory. Only one option will be used for each
179 * field.
180 */
181struct tls_connection_params {
182 const char *ca_cert;
183 const u8 *ca_cert_blob;
184 size_t ca_cert_blob_len;
185 const char *ca_path;
186 const char *subject_match;
187 const char *altsubject_match;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700188 const char *suffix_match;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800189 const char *domain_match;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 const char *client_cert;
Hai Shalom81f62d82019-07-22 12:10:00 -0700191 const char *client_cert2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 const u8 *client_cert_blob;
193 size_t client_cert_blob_len;
194 const char *private_key;
Hai Shalom81f62d82019-07-22 12:10:00 -0700195 const char *private_key2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 const u8 *private_key_blob;
197 size_t private_key_blob_len;
198 const char *private_key_passwd;
Hai Shalom81f62d82019-07-22 12:10:00 -0700199 const char *private_key_passwd2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 const char *dh_file;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201
202 /* OpenSSL specific variables */
203 int engine;
204 const char *engine_id;
205 const char *pin;
206 const char *key_id;
207 const char *cert_id;
208 const char *ca_cert_id;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800209 const char *openssl_ciphers;
Hai Shalom74f70d42019-02-11 14:42:39 -0800210 const char *openssl_ecdh_curves;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211
212 unsigned int flags;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700213 const char *ocsp_stapling_response;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800214 const char *ocsp_stapling_response_multi;
Hai Shalom021b0b52019-04-10 11:17:58 -0700215 const char *check_cert_subject;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216};
217
218
219/**
220 * tls_init - Initialize TLS library
221 * @conf: Configuration data for TLS library
222 * Returns: Context data to be used as tls_ctx in calls to other functions,
223 * or %NULL on failure.
224 *
225 * Called once during program startup and once for each RSN pre-authentication
226 * session. In other words, there can be two concurrent TLS contexts. If global
227 * library initialization is needed (i.e., one that is shared between both
228 * authentication types), the TLS library wrapper should maintain a reference
229 * counter and do global initialization only when moving from 0 to 1 reference.
230 */
231void * tls_init(const struct tls_config *conf);
232
233/**
234 * tls_deinit - Deinitialize TLS library
235 * @tls_ctx: TLS context data from tls_init()
236 *
237 * Called once during program shutdown and once for each RSN pre-authentication
238 * session. If global library deinitialization is needed (i.e., one that is
239 * shared between both authentication types), the TLS library wrapper should
240 * maintain a reference counter and do global deinitialization only when moving
241 * from 1 to 0 references.
242 */
243void tls_deinit(void *tls_ctx);
244
245/**
246 * tls_get_errors - Process pending errors
247 * @tls_ctx: TLS context data from tls_init()
248 * Returns: Number of found error, 0 if no errors detected.
249 *
250 * Process all pending TLS errors.
251 */
252int tls_get_errors(void *tls_ctx);
253
254/**
255 * tls_connection_init - Initialize a new TLS connection
256 * @tls_ctx: TLS context data from tls_init()
257 * Returns: Connection context data, conn for other function calls
258 */
259struct tls_connection * tls_connection_init(void *tls_ctx);
260
261/**
262 * tls_connection_deinit - Free TLS connection data
263 * @tls_ctx: TLS context data from tls_init()
264 * @conn: Connection context data from tls_connection_init()
265 *
266 * Release all resources allocated for TLS connection.
267 */
268void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
269
270/**
271 * tls_connection_established - Has the TLS connection been completed?
272 * @tls_ctx: TLS context data from tls_init()
273 * @conn: Connection context data from tls_connection_init()
274 * Returns: 1 if TLS connection has been completed, 0 if not.
275 */
276int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
277
278/**
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800279 * tls_connection_peer_serial_num - Fetch peer certificate serial number
280 * @tls_ctx: TLS context data from tls_init()
281 * @conn: Connection context data from tls_connection_init()
282 * Returns: Allocated string buffer containing the peer certificate serial
283 * number or %NULL on error.
284 *
285 * The caller is responsible for freeing the returned buffer with os_free().
286 */
287char * tls_connection_peer_serial_num(void *tls_ctx,
288 struct tls_connection *conn);
289
290/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 * tls_connection_shutdown - Shutdown TLS connection
292 * @tls_ctx: TLS context data from tls_init()
293 * @conn: Connection context data from tls_connection_init()
294 * Returns: 0 on success, -1 on failure
295 *
296 * Shutdown current TLS connection without releasing all resources. New
297 * connection can be started by using the same conn without having to call
298 * tls_connection_init() or setting certificates etc. again. The new
299 * connection should try to use session resumption.
300 */
301int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
302
303enum {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700304 TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN = -4,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
306 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
307};
308
309/**
310 * tls_connection_set_params - Set TLS connection parameters
311 * @tls_ctx: TLS context data from tls_init()
312 * @conn: Connection context data from tls_connection_init()
313 * @params: Connection parameters
314 * Returns: 0 on success, -1 on failure,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700315 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
316 * failure, or
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700318 * PKCS#11 engine private key, or
319 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
320 * failure.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 */
322int __must_check
323tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
324 const struct tls_connection_params *params);
325
326/**
327 * tls_global_set_params - Set TLS parameters for all TLS connection
328 * @tls_ctx: TLS context data from tls_init()
329 * @params: Global TLS parameters
330 * Returns: 0 on success, -1 on failure,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700331 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
332 * failure, or
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700334 * PKCS#11 engine private key, or
335 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
336 * failure.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 */
338int __must_check tls_global_set_params(
339 void *tls_ctx, const struct tls_connection_params *params);
340
341/**
342 * tls_global_set_verify - Set global certificate verification options
343 * @tls_ctx: TLS context data from tls_init()
344 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
345 * 2 = verify CRL for all certificates
Hai Shalom74f70d42019-02-11 14:42:39 -0800346 * @strict: 0 = allow CRL time errors, 1 = do not allow CRL time errors
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 * Returns: 0 on success, -1 on failure
348 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800349int __must_check tls_global_set_verify(void *tls_ctx, int check_crl,
350 int strict);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351
352/**
353 * tls_connection_set_verify - Set certificate verification options
354 * @tls_ctx: TLS context data from tls_init()
355 * @conn: Connection context data from tls_connection_init()
356 * @verify_peer: 1 = verify peer certificate
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800357 * @flags: Connection flags (TLS_CONN_*)
358 * @session_ctx: Session caching context or %NULL to use default
359 * @session_ctx_len: Length of @session_ctx in bytes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 * Returns: 0 on success, -1 on failure
361 */
362int __must_check tls_connection_set_verify(void *tls_ctx,
363 struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800364 int verify_peer,
365 unsigned int flags,
366 const u8 *session_ctx,
367 size_t session_ctx_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368
369/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800370 * tls_connection_get_random - Get random data from TLS connection
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 * @tls_ctx: TLS context data from tls_init()
372 * @conn: Connection context data from tls_connection_init()
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800373 * @data: Structure of client/server random data (filled on success)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374 * Returns: 0 on success, -1 on failure
375 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800376int __must_check tls_connection_get_random(void *tls_ctx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 struct tls_connection *conn,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800378 struct tls_random *data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379
380/**
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700381 * tls_connection_export_key - Derive keying material from a TLS connection
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 * @tls_ctx: TLS context data from tls_init()
383 * @conn: Connection context data from tls_connection_init()
384 * @label: Label (e.g., description of the key) for PRF
Hai Shalom021b0b52019-04-10 11:17:58 -0700385 * @context: Optional extra upper-layer context (max len 2^16)
386 * @context_len: The length of the context value
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 * @out: Buffer for output data from TLS-PRF
388 * @out_len: Length of the output buffer
389 * Returns: 0 on success, -1 on failure
390 *
Hai Shalom021b0b52019-04-10 11:17:58 -0700391 * Exports keying material using the mechanism described in RFC 5705. If
392 * context is %NULL, context is not provided; otherwise, context is provided
393 * (including the case of empty context with context_len == 0).
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700395int __must_check tls_connection_export_key(void *tls_ctx,
396 struct tls_connection *conn,
397 const char *label,
Hai Shalom021b0b52019-04-10 11:17:58 -0700398 const u8 *context,
399 size_t context_len,
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700400 u8 *out, size_t out_len);
401
402/**
403 * tls_connection_get_eap_fast_key - Derive key material for EAP-FAST
404 * @tls_ctx: TLS context data from tls_init()
405 * @conn: Connection context data from tls_connection_init()
406 * @out: Buffer for output data from TLS-PRF
407 * @out_len: Length of the output buffer
408 * Returns: 0 on success, -1 on failure
409 *
410 * Exports key material after the normal TLS key block for use with
411 * EAP-FAST. Most callers will want tls_connection_export_key(), but EAP-FAST
412 * uses a different legacy mechanism.
413 */
414int __must_check tls_connection_get_eap_fast_key(void *tls_ctx,
415 struct tls_connection *conn,
416 u8 *out, size_t out_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417
418/**
419 * tls_connection_handshake - Process TLS handshake (client side)
420 * @tls_ctx: TLS context data from tls_init()
421 * @conn: Connection context data from tls_connection_init()
422 * @in_data: Input data from TLS server
423 * @appl_data: Pointer to application data pointer, or %NULL if dropped
424 * Returns: Output data, %NULL on failure
425 *
426 * The caller is responsible for freeing the returned output data. If the final
427 * handshake message includes application data, this is decrypted and
428 * appl_data (if not %NULL) is set to point this data. The caller is
429 * responsible for freeing appl_data.
430 *
431 * This function is used during TLS handshake. The first call is done with
432 * in_data == %NULL and the library is expected to return ClientHello packet.
433 * This packet is then send to the server and a response from server is given
434 * to TLS library by calling this function again with in_data pointing to the
435 * TLS message from the server.
436 *
437 * If the TLS handshake fails, this function may return %NULL. However, if the
438 * TLS library has a TLS alert to send out, that should be returned as the
439 * output data. In this case, tls_connection_get_failed() must return failure
440 * (> 0).
441 *
442 * tls_connection_established() should return 1 once the TLS handshake has been
443 * completed successfully.
444 */
445struct wpabuf * tls_connection_handshake(void *tls_ctx,
446 struct tls_connection *conn,
447 const struct wpabuf *in_data,
448 struct wpabuf **appl_data);
449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800450struct wpabuf * tls_connection_handshake2(void *tls_ctx,
451 struct tls_connection *conn,
452 const struct wpabuf *in_data,
453 struct wpabuf **appl_data,
454 int *more_data_needed);
455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456/**
457 * tls_connection_server_handshake - Process TLS handshake (server side)
458 * @tls_ctx: TLS context data from tls_init()
459 * @conn: Connection context data from tls_connection_init()
460 * @in_data: Input data from TLS peer
461 * @appl_data: Pointer to application data pointer, or %NULL if dropped
462 * Returns: Output data, %NULL on failure
463 *
464 * The caller is responsible for freeing the returned output data.
465 */
466struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
467 struct tls_connection *conn,
468 const struct wpabuf *in_data,
469 struct wpabuf **appl_data);
470
471/**
472 * tls_connection_encrypt - Encrypt data into TLS tunnel
473 * @tls_ctx: TLS context data from tls_init()
474 * @conn: Connection context data from tls_connection_init()
475 * @in_data: Plaintext data to be encrypted
476 * Returns: Encrypted TLS data or %NULL on failure
477 *
478 * This function is used after TLS handshake has been completed successfully to
479 * send data in the encrypted tunnel. The caller is responsible for freeing the
480 * returned output data.
481 */
482struct wpabuf * tls_connection_encrypt(void *tls_ctx,
483 struct tls_connection *conn,
484 const struct wpabuf *in_data);
485
486/**
487 * tls_connection_decrypt - Decrypt data from TLS tunnel
488 * @tls_ctx: TLS context data from tls_init()
489 * @conn: Connection context data from tls_connection_init()
490 * @in_data: Encrypted TLS data
491 * Returns: Decrypted TLS data or %NULL on failure
492 *
493 * This function is used after TLS handshake has been completed successfully to
494 * receive data from the encrypted tunnel. The caller is responsible for
495 * freeing the returned output data.
496 */
497struct wpabuf * tls_connection_decrypt(void *tls_ctx,
498 struct tls_connection *conn,
499 const struct wpabuf *in_data);
500
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800501struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
502 struct tls_connection *conn,
503 const struct wpabuf *in_data,
504 int *more_data_needed);
505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506/**
507 * tls_connection_resumed - Was session resumption used
508 * @tls_ctx: TLS context data from tls_init()
509 * @conn: Connection context data from tls_connection_init()
510 * Returns: 1 if current session used session resumption, 0 if not
511 */
512int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
513
514enum {
515 TLS_CIPHER_NONE,
516 TLS_CIPHER_RC4_SHA /* 0x0005 */,
517 TLS_CIPHER_AES128_SHA /* 0x002f */,
518 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800519 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */,
520 TLS_CIPHER_RSA_DHE_AES256_SHA /* 0x0039 */,
521 TLS_CIPHER_AES256_SHA /* 0x0035 */,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522};
523
524/**
525 * tls_connection_set_cipher_list - Configure acceptable cipher suites
526 * @tls_ctx: TLS context data from tls_init()
527 * @conn: Connection context data from tls_connection_init()
528 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
529 * (TLS_CIPHER_*).
530 * Returns: 0 on success, -1 on failure
531 */
532int __must_check tls_connection_set_cipher_list(void *tls_ctx,
533 struct tls_connection *conn,
534 u8 *ciphers);
535
536/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800537 * tls_get_version - Get the current TLS version number
538 * @tls_ctx: TLS context data from tls_init()
539 * @conn: Connection context data from tls_connection_init()
540 * @buf: Buffer for returning the TLS version number
541 * @buflen: buf size
542 * Returns: 0 on success, -1 on failure
543 *
544 * Get the currently used TLS version number.
545 */
546int __must_check tls_get_version(void *tls_ctx, struct tls_connection *conn,
547 char *buf, size_t buflen);
548
549/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550 * tls_get_cipher - Get current cipher name
551 * @tls_ctx: TLS context data from tls_init()
552 * @conn: Connection context data from tls_connection_init()
553 * @buf: Buffer for the cipher name
554 * @buflen: buf size
555 * Returns: 0 on success, -1 on failure
556 *
557 * Get the name of the currently used cipher.
558 */
559int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
560 char *buf, size_t buflen);
561
562/**
563 * tls_connection_enable_workaround - Enable TLS workaround options
564 * @tls_ctx: TLS context data from tls_init()
565 * @conn: Connection context data from tls_connection_init()
566 * Returns: 0 on success, -1 on failure
567 *
568 * This function is used to enable connection-specific workaround options for
569 * buffer SSL/TLS implementations.
570 */
571int __must_check tls_connection_enable_workaround(void *tls_ctx,
572 struct tls_connection *conn);
573
574/**
575 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
576 * @tls_ctx: TLS context data from tls_init()
577 * @conn: Connection context data from tls_connection_init()
578 * @ext_type: Extension type
579 * @data: Extension payload (%NULL to remove extension)
580 * @data_len: Extension payload length
581 * Returns: 0 on success, -1 on failure
582 */
583int __must_check tls_connection_client_hello_ext(void *tls_ctx,
584 struct tls_connection *conn,
585 int ext_type, const u8 *data,
586 size_t data_len);
587
588/**
589 * tls_connection_get_failed - Get connection failure status
590 * @tls_ctx: TLS context data from tls_init()
591 * @conn: Connection context data from tls_connection_init()
592 *
593 * Returns >0 if connection has failed, 0 if not.
594 */
595int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
596
597/**
598 * tls_connection_get_read_alerts - Get connection read alert status
599 * @tls_ctx: TLS context data from tls_init()
600 * @conn: Connection context data from tls_connection_init()
601 * Returns: Number of times a fatal read (remote end reported error) has
602 * happened during this connection.
603 */
604int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
605
606/**
607 * tls_connection_get_write_alerts - Get connection write alert status
608 * @tls_ctx: TLS context data from tls_init()
609 * @conn: Connection context data from tls_connection_init()
610 * Returns: Number of times a fatal write (locally detected error) has happened
611 * during this connection.
612 */
613int tls_connection_get_write_alerts(void *tls_ctx,
614 struct tls_connection *conn);
615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616typedef int (*tls_session_ticket_cb)
617(void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
618 const u8 *server_random, u8 *master_secret);
619
620int __must_check tls_connection_set_session_ticket_cb(
621 void *tls_ctx, struct tls_connection *conn,
622 tls_session_ticket_cb cb, void *ctx);
623
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700624void tls_connection_set_log_cb(struct tls_connection *conn,
625 void (*log_cb)(void *ctx, const char *msg),
626 void *ctx);
627
628#define TLS_BREAK_VERIFY_DATA BIT(0)
629#define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
630#define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700631#define TLS_DHE_PRIME_511B BIT(3)
632#define TLS_DHE_PRIME_767B BIT(4)
633#define TLS_DHE_PRIME_15 BIT(5)
634#define TLS_DHE_PRIME_58B BIT(6)
635#define TLS_DHE_NON_PRIME BIT(7)
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700636
637void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
638
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800639int tls_get_library_version(char *buf, size_t buf_len);
640
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800641void tls_connection_set_success_data(struct tls_connection *conn,
642 struct wpabuf *data);
643
644void tls_connection_set_success_data_resumed(struct tls_connection *conn);
645
646const struct wpabuf *
647tls_connection_get_success_data(struct tls_connection *conn);
648
649void tls_connection_remove_session(struct tls_connection *conn);
650
Hai Shalom81f62d82019-07-22 12:10:00 -0700651/**
652 * tls_get_tls_unique - Fetch "tls-unique" for channel binding
653 * @conn: Connection context data from tls_connection_init()
654 * @buf: Buffer for returning the value
655 * @max_len: Maximum length of the buffer in bytes
656 * Returns: Number of bytes written to buf or -1 on error
657 *
658 * This function can be used to fetch "tls-unique" (RFC 5929, Section 3) which
659 * is the first TLS Finished message sent in the most recent TLS handshake of
660 * the TLS connection.
661 */
662int tls_get_tls_unique(struct tls_connection *conn, u8 *buf, size_t max_len);
663
664/**
665 * tls_connection_get_cipher_suite - Get current TLS cipher suite
666 * @conn: Connection context data from tls_connection_init()
667 * Returns: TLS cipher suite of the current connection or 0 on error
668 */
669u16 tls_connection_get_cipher_suite(struct tls_connection *conn);
670
Hai Shalom899fcc72020-10-19 14:38:18 -0700671/**
672 * tls_connection_get_peer_subject - Get peer subject
673 * @conn: Connection context data from tls_connection_init()
674 * Returns: Peer subject or %NULL if not authenticated or not available
675 */
676const char * tls_connection_get_peer_subject(struct tls_connection *conn);
677
678/**
679 * tls_connection_get_own_cert_used - Was own certificate used
680 * @conn: Connection context data from tls_connection_init()
681 * Returns: true if own certificate was used during authentication
682 */
683bool tls_connection_get_own_cert_used(struct tls_connection *conn);
684
Gabriel Birena5bdf372022-12-15 20:54:33 +0000685/**
686 * tls_register_cert_callback - Register a callback to retrieve certificates
687 * @cb: Callback object to register
688 */
689typedef ssize_t (*tls_get_certificate_cb)
690(void* ctx, const char* alias, uint8_t** value);
691
692void tls_register_cert_callback(tls_get_certificate_cb cb);
693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694#endif /* TLS_H */