2 * SSL/TLS interface definition
3 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 struct tls_connection;
15 const u8 *client_random;
16 size_t client_random_len;
17 const u8 *server_random;
18 size_t server_random_len;
22 TLS_CERT_CHAIN_SUCCESS,
23 TLS_CERT_CHAIN_FAILURE,
29 * Note: These are used as identifier with external programs and as such, the
30 * values must not be changed.
32 enum tls_fail_reason {
33 TLS_FAIL_UNSPECIFIED = 0,
34 TLS_FAIL_UNTRUSTED = 1,
36 TLS_FAIL_NOT_YET_VALID = 3,
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,
47 #define TLS_MAX_ALT_SUBJECT 10
49 union tls_event_data {
53 enum tls_fail_reason reason;
54 const char *reason_txt;
55 const struct wpabuf *cert;
61 const struct wpabuf *cert;
64 const char *altsubject[TLS_MAX_ALT_SUBJECT];
71 const char *description;
76 const char *opensc_engine_path;
77 const char *pkcs11_engine_path;
78 const char *pkcs11_module_path;
81 const char *openssl_ciphers;
83 void (*event_cb)(void *ctx, enum tls_event ev,
84 union tls_event_data *data);
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)
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
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
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
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
142 * TLS connection parameters to be configured with tls_connection_set_params()
143 * and tls_global_set_params().
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
150 struct tls_connection_params {
152 const u8 *ca_cert_blob;
153 size_t ca_cert_blob_len;
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;
170 /* OpenSSL specific variables */
172 const char *engine_id;
176 const char *ca_cert_id;
177 const char *openssl_ciphers;
180 const char *ocsp_stapling_response;
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.
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.
196 void * tls_init(const struct tls_config *conf);
199 * tls_deinit - Deinitialize TLS library
200 * @tls_ctx: TLS context data from tls_init()
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.
208 void tls_deinit(void *tls_ctx);
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.
215 * Process all pending TLS errors.
217 int tls_get_errors(void *tls_ctx);
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
224 struct tls_connection * tls_connection_init(void *tls_ctx);
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()
231 * Release all resources allocated for TLS connection.
233 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
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.
241 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
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
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.
254 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
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
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
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
276 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
277 const struct tls_connection_params *params);
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
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
291 int __must_check tls_global_set_params(
292 void *tls_ctx, const struct tls_connection_params *params);
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
301 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
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
310 int __must_check tls_connection_set_verify(void *tls_ctx,
311 struct tls_connection *conn,
315 * tls_connection_get_keys - Get random data from TLS connection
316 * @tls_ctx: TLS context data from tls_init()
317 * @conn: Connection context data from tls_connection_init()
318 * @keys: Structure of client/server random data (filled on success)
319 * Returns: 0 on success, -1 on failure
321 int __must_check tls_connection_get_keys(void *tls_ctx,
322 struct tls_connection *conn,
323 struct tls_keys *keys);
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
337 * This function is optional to implement if tls_connection_get_keys() provides
338 * access to master secret and server/client random values. If these values are
339 * not exported from the TLS library, tls_connection_prf() is required so that
340 * further keying material can be derived from the master secret. If not
341 * implemented, the function will still need to be defined, but it can just
342 * return -1. Example implementation of this function is in tls_prf_sha1_md5()
343 * when it is called with seed set to client_random|server_random (or
344 * server_random|client_random).
346 int __must_check tls_connection_prf(void *tls_ctx,
347 struct tls_connection *conn,
349 int server_random_first,
351 u8 *out, size_t out_len);
354 * tls_connection_handshake - Process TLS handshake (client side)
355 * @tls_ctx: TLS context data from tls_init()
356 * @conn: Connection context data from tls_connection_init()
357 * @in_data: Input data from TLS server
358 * @appl_data: Pointer to application data pointer, or %NULL if dropped
359 * Returns: Output data, %NULL on failure
361 * The caller is responsible for freeing the returned output data. If the final
362 * handshake message includes application data, this is decrypted and
363 * appl_data (if not %NULL) is set to point this data. The caller is
364 * responsible for freeing appl_data.
366 * This function is used during TLS handshake. The first call is done with
367 * in_data == %NULL and the library is expected to return ClientHello packet.
368 * This packet is then send to the server and a response from server is given
369 * to TLS library by calling this function again with in_data pointing to the
370 * TLS message from the server.
372 * If the TLS handshake fails, this function may return %NULL. However, if the
373 * TLS library has a TLS alert to send out, that should be returned as the
374 * output data. In this case, tls_connection_get_failed() must return failure
377 * tls_connection_established() should return 1 once the TLS handshake has been
378 * completed successfully.
380 struct wpabuf * tls_connection_handshake(void *tls_ctx,
381 struct tls_connection *conn,
382 const struct wpabuf *in_data,
383 struct wpabuf **appl_data);
385 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
386 struct tls_connection *conn,
387 const struct wpabuf *in_data,
388 struct wpabuf **appl_data,
389 int *more_data_needed);
392 * tls_connection_server_handshake - Process TLS handshake (server side)
393 * @tls_ctx: TLS context data from tls_init()
394 * @conn: Connection context data from tls_connection_init()
395 * @in_data: Input data from TLS peer
396 * @appl_data: Pointer to application data pointer, or %NULL if dropped
397 * Returns: Output data, %NULL on failure
399 * The caller is responsible for freeing the returned output data.
401 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
402 struct tls_connection *conn,
403 const struct wpabuf *in_data,
404 struct wpabuf **appl_data);
407 * tls_connection_encrypt - Encrypt data into TLS tunnel
408 * @tls_ctx: TLS context data from tls_init()
409 * @conn: Connection context data from tls_connection_init()
410 * @in_data: Plaintext data to be encrypted
411 * Returns: Encrypted TLS data or %NULL on failure
413 * This function is used after TLS handshake has been completed successfully to
414 * send data in the encrypted tunnel. The caller is responsible for freeing the
415 * returned output data.
417 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
418 struct tls_connection *conn,
419 const struct wpabuf *in_data);
422 * tls_connection_decrypt - Decrypt data from TLS tunnel
423 * @tls_ctx: TLS context data from tls_init()
424 * @conn: Connection context data from tls_connection_init()
425 * @in_data: Encrypted TLS data
426 * Returns: Decrypted TLS data or %NULL on failure
428 * This function is used after TLS handshake has been completed successfully to
429 * receive data from the encrypted tunnel. The caller is responsible for
430 * freeing the returned output data.
432 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
433 struct tls_connection *conn,
434 const struct wpabuf *in_data);
436 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
437 struct tls_connection *conn,
438 const struct wpabuf *in_data,
439 int *more_data_needed);
442 * tls_connection_resumed - Was session resumption used
443 * @tls_ctx: TLS context data from tls_init()
444 * @conn: Connection context data from tls_connection_init()
445 * Returns: 1 if current session used session resumption, 0 if not
447 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
451 TLS_CIPHER_RC4_SHA /* 0x0005 */,
452 TLS_CIPHER_AES128_SHA /* 0x002f */,
453 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
454 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
458 * tls_connection_set_cipher_list - Configure acceptable cipher suites
459 * @tls_ctx: TLS context data from tls_init()
460 * @conn: Connection context data from tls_connection_init()
461 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
463 * Returns: 0 on success, -1 on failure
465 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
466 struct tls_connection *conn,
470 * tls_get_cipher - Get current cipher name
471 * @tls_ctx: TLS context data from tls_init()
472 * @conn: Connection context data from tls_connection_init()
473 * @buf: Buffer for the cipher name
475 * Returns: 0 on success, -1 on failure
477 * Get the name of the currently used cipher.
479 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
480 char *buf, size_t buflen);
483 * tls_connection_enable_workaround - Enable TLS workaround options
484 * @tls_ctx: TLS context data from tls_init()
485 * @conn: Connection context data from tls_connection_init()
486 * Returns: 0 on success, -1 on failure
488 * This function is used to enable connection-specific workaround options for
489 * buffer SSL/TLS implementations.
491 int __must_check tls_connection_enable_workaround(void *tls_ctx,
492 struct tls_connection *conn);
495 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
496 * @tls_ctx: TLS context data from tls_init()
497 * @conn: Connection context data from tls_connection_init()
498 * @ext_type: Extension type
499 * @data: Extension payload (%NULL to remove extension)
500 * @data_len: Extension payload length
501 * Returns: 0 on success, -1 on failure
503 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
504 struct tls_connection *conn,
505 int ext_type, const u8 *data,
509 * tls_connection_get_failed - Get connection failure status
510 * @tls_ctx: TLS context data from tls_init()
511 * @conn: Connection context data from tls_connection_init()
513 * Returns >0 if connection has failed, 0 if not.
515 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
518 * tls_connection_get_read_alerts - Get connection read alert status
519 * @tls_ctx: TLS context data from tls_init()
520 * @conn: Connection context data from tls_connection_init()
521 * Returns: Number of times a fatal read (remote end reported error) has
522 * happened during this connection.
524 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
527 * tls_connection_get_write_alerts - Get connection write alert status
528 * @tls_ctx: TLS context data from tls_init()
529 * @conn: Connection context data from tls_connection_init()
530 * Returns: Number of times a fatal write (locally detected error) has happened
531 * during this connection.
533 int tls_connection_get_write_alerts(void *tls_ctx,
534 struct tls_connection *conn);
536 typedef int (*tls_session_ticket_cb)
537 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
538 const u8 *server_random, u8 *master_secret);
540 int __must_check tls_connection_set_session_ticket_cb(
541 void *tls_ctx, struct tls_connection *conn,
542 tls_session_ticket_cb cb, void *ctx);
544 void tls_connection_set_log_cb(struct tls_connection *conn,
545 void (*log_cb)(void *ctx, const char *msg),
548 #define TLS_BREAK_VERIFY_DATA BIT(0)
549 #define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
550 #define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
551 #define TLS_DHE_PRIME_511B BIT(3)
552 #define TLS_DHE_PRIME_767B BIT(4)
553 #define TLS_DHE_PRIME_15 BIT(5)
554 #define TLS_DHE_PRIME_58B BIT(6)
555 #define TLS_DHE_NON_PRIME BIT(7)
557 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
559 int tls_get_library_version(char *buf, size_t buf_len);