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;
82 unsigned int tls_session_lifetime;
84 void (*event_cb)(void *ctx, enum tls_event ev,
85 union tls_event_data *data);
89 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
90 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
91 #define TLS_CONN_DISABLE_SESSION_TICKET BIT(2)
92 #define TLS_CONN_REQUEST_OCSP BIT(3)
93 #define TLS_CONN_REQUIRE_OCSP BIT(4)
94 #define TLS_CONN_DISABLE_TLSv1_1 BIT(5)
95 #define TLS_CONN_DISABLE_TLSv1_2 BIT(6)
96 #define TLS_CONN_EAP_FAST BIT(7)
97 #define TLS_CONN_DISABLE_TLSv1_0 BIT(8)
98 #define TLS_CONN_EXT_CERT_CHECK BIT(9)
101 * struct tls_connection_params - Parameters for TLS connection
102 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
104 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
105 * @ca_cert_blob_len: ca_cert_blob length
106 * @ca_path: Path to CA certificates (OpenSSL specific)
107 * @subject_match: String to match in the subject of the peer certificate or
108 * %NULL to allow all subjects
109 * @altsubject_match: String to match in the alternative subject of the peer
110 * certificate or %NULL to allow all alternative subjects
111 * @suffix_match: String to suffix match in the dNSName or CN of the peer
112 * certificate or %NULL to allow all domain names. This may allow subdomains an
113 * wildcard certificates. Each domain name label must have a full match.
114 * @domain_match: String to match in the dNSName or CN of the peer
115 * certificate or %NULL to allow all domain names. This requires a full,
116 * case-insensitive match.
117 * @client_cert: File or reference name for client X.509 certificate in PEM or
119 * @client_cert_blob: client_cert as inlined data or %NULL if not used
120 * @client_cert_blob_len: client_cert_blob length
121 * @private_key: File or reference name for client private key in PEM or DER
122 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
123 * @private_key_blob: private_key as inlined data or %NULL if not used
124 * @private_key_blob_len: private_key_blob length
125 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
126 * passphrase is used.
127 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
128 * @dh_blob: dh_file as inlined data or %NULL if not used
129 * @dh_blob_len: dh_blob length
130 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
131 * (this is OpenSSL specific for now)
132 * @engine_id: engine id string (this is OpenSSL specific for now)
133 * @ppin: pointer to the pin variable in the configuration
134 * (this is OpenSSL specific for now)
135 * @key_id: the private key's id when using engine (this is OpenSSL
137 * @cert_id: the certificate's id when using engine
138 * @ca_cert_id: the CA certificate's id when using engine
139 * @openssl_ciphers: OpenSSL cipher configuration
140 * @flags: Parameter options (TLS_CONN_*)
141 * @ocsp_stapling_response: DER encoded file with cached OCSP stapling response
142 * or %NULL if OCSP is not enabled
143 * @ocsp_stapling_response_multi: DER encoded file with cached OCSP stapling
144 * response list (OCSPResponseList for ocsp_multi in RFC 6961) or %NULL if
145 * ocsp_multi is not enabled
147 * TLS connection parameters to be configured with tls_connection_set_params()
148 * and tls_global_set_params().
150 * Certificates and private key can be configured either as a reference name
151 * (file path or reference to certificate store) or by providing the same data
152 * as a pointer to the data in memory. Only one option will be used for each
155 struct tls_connection_params {
157 const u8 *ca_cert_blob;
158 size_t ca_cert_blob_len;
160 const char *subject_match;
161 const char *altsubject_match;
162 const char *suffix_match;
163 const char *domain_match;
164 const char *client_cert;
165 const u8 *client_cert_blob;
166 size_t client_cert_blob_len;
167 const char *private_key;
168 const u8 *private_key_blob;
169 size_t private_key_blob_len;
170 const char *private_key_passwd;
175 /* OpenSSL specific variables */
177 const char *engine_id;
181 const char *ca_cert_id;
182 const char *openssl_ciphers;
185 const char *ocsp_stapling_response;
186 const char *ocsp_stapling_response_multi;
191 * tls_init - Initialize TLS library
192 * @conf: Configuration data for TLS library
193 * Returns: Context data to be used as tls_ctx in calls to other functions,
194 * or %NULL on failure.
196 * Called once during program startup and once for each RSN pre-authentication
197 * session. In other words, there can be two concurrent TLS contexts. If global
198 * library initialization is needed (i.e., one that is shared between both
199 * authentication types), the TLS library wrapper should maintain a reference
200 * counter and do global initialization only when moving from 0 to 1 reference.
202 void * tls_init(const struct tls_config *conf);
205 * tls_deinit - Deinitialize TLS library
206 * @tls_ctx: TLS context data from tls_init()
208 * Called once during program shutdown and once for each RSN pre-authentication
209 * session. If global library deinitialization is needed (i.e., one that is
210 * shared between both authentication types), the TLS library wrapper should
211 * maintain a reference counter and do global deinitialization only when moving
212 * from 1 to 0 references.
214 void tls_deinit(void *tls_ctx);
217 * tls_get_errors - Process pending errors
218 * @tls_ctx: TLS context data from tls_init()
219 * Returns: Number of found error, 0 if no errors detected.
221 * Process all pending TLS errors.
223 int tls_get_errors(void *tls_ctx);
226 * tls_connection_init - Initialize a new TLS connection
227 * @tls_ctx: TLS context data from tls_init()
228 * Returns: Connection context data, conn for other function calls
230 struct tls_connection * tls_connection_init(void *tls_ctx);
233 * tls_connection_deinit - Free TLS connection data
234 * @tls_ctx: TLS context data from tls_init()
235 * @conn: Connection context data from tls_connection_init()
237 * Release all resources allocated for TLS connection.
239 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
242 * tls_connection_established - Has the TLS connection been completed?
243 * @tls_ctx: TLS context data from tls_init()
244 * @conn: Connection context data from tls_connection_init()
245 * Returns: 1 if TLS connection has been completed, 0 if not.
247 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
250 * tls_connection_shutdown - Shutdown TLS connection
251 * @tls_ctx: TLS context data from tls_init()
252 * @conn: Connection context data from tls_connection_init()
253 * Returns: 0 on success, -1 on failure
255 * Shutdown current TLS connection without releasing all resources. New
256 * connection can be started by using the same conn without having to call
257 * tls_connection_init() or setting certificates etc. again. The new
258 * connection should try to use session resumption.
260 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
263 TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN = -4,
264 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
265 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
269 * tls_connection_set_params - Set TLS connection parameters
270 * @tls_ctx: TLS context data from tls_init()
271 * @conn: Connection context data from tls_connection_init()
272 * @params: Connection parameters
273 * Returns: 0 on success, -1 on failure,
274 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
276 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
277 * PKCS#11 engine private key, or
278 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
282 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
283 const struct tls_connection_params *params);
286 * tls_global_set_params - Set TLS parameters for all TLS connection
287 * @tls_ctx: TLS context data from tls_init()
288 * @params: Global TLS parameters
289 * Returns: 0 on success, -1 on failure,
290 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
292 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
293 * PKCS#11 engine private key, or
294 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
297 int __must_check tls_global_set_params(
298 void *tls_ctx, const struct tls_connection_params *params);
301 * tls_global_set_verify - Set global certificate verification options
302 * @tls_ctx: TLS context data from tls_init()
303 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
304 * 2 = verify CRL for all certificates
305 * Returns: 0 on success, -1 on failure
307 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
310 * tls_connection_set_verify - Set certificate verification options
311 * @tls_ctx: TLS context data from tls_init()
312 * @conn: Connection context data from tls_connection_init()
313 * @verify_peer: 1 = verify peer certificate
314 * @flags: Connection flags (TLS_CONN_*)
315 * @session_ctx: Session caching context or %NULL to use default
316 * @session_ctx_len: Length of @session_ctx in bytes.
317 * Returns: 0 on success, -1 on failure
319 int __must_check tls_connection_set_verify(void *tls_ctx,
320 struct tls_connection *conn,
323 const u8 *session_ctx,
324 size_t session_ctx_len);
327 * tls_connection_get_random - Get random data from TLS connection
328 * @tls_ctx: TLS context data from tls_init()
329 * @conn: Connection context data from tls_connection_init()
330 * @data: Structure of client/server random data (filled on success)
331 * Returns: 0 on success, -1 on failure
333 int __must_check tls_connection_get_random(void *tls_ctx,
334 struct tls_connection *conn,
335 struct tls_random *data);
338 * tls_connection_prf - Use TLS-PRF to derive keying material
339 * @tls_ctx: TLS context data from tls_init()
340 * @conn: Connection context data from tls_connection_init()
341 * @label: Label (e.g., description of the key) for PRF
342 * @server_random_first: seed is 0 = client_random|server_random,
343 * 1 = server_random|client_random
344 * @skip_keyblock: Skip TLS key block from the beginning of PRF output
345 * @out: Buffer for output data from TLS-PRF
346 * @out_len: Length of the output buffer
347 * Returns: 0 on success, -1 on failure
349 * tls_connection_prf() is required so that further keying material can be
350 * derived from the master secret. Example implementation of this function is in
351 * tls_prf_sha1_md5() when it is called with seed set to
352 * client_random|server_random (or server_random|client_random). For TLSv1.2 and
353 * newer, a different PRF is needed, though.
355 int __must_check tls_connection_prf(void *tls_ctx,
356 struct tls_connection *conn,
358 int server_random_first,
360 u8 *out, size_t out_len);
363 * tls_connection_handshake - Process TLS handshake (client side)
364 * @tls_ctx: TLS context data from tls_init()
365 * @conn: Connection context data from tls_connection_init()
366 * @in_data: Input data from TLS server
367 * @appl_data: Pointer to application data pointer, or %NULL if dropped
368 * Returns: Output data, %NULL on failure
370 * The caller is responsible for freeing the returned output data. If the final
371 * handshake message includes application data, this is decrypted and
372 * appl_data (if not %NULL) is set to point this data. The caller is
373 * responsible for freeing appl_data.
375 * This function is used during TLS handshake. The first call is done with
376 * in_data == %NULL and the library is expected to return ClientHello packet.
377 * This packet is then send to the server and a response from server is given
378 * to TLS library by calling this function again with in_data pointing to the
379 * TLS message from the server.
381 * If the TLS handshake fails, this function may return %NULL. However, if the
382 * TLS library has a TLS alert to send out, that should be returned as the
383 * output data. In this case, tls_connection_get_failed() must return failure
386 * tls_connection_established() should return 1 once the TLS handshake has been
387 * completed successfully.
389 struct wpabuf * tls_connection_handshake(void *tls_ctx,
390 struct tls_connection *conn,
391 const struct wpabuf *in_data,
392 struct wpabuf **appl_data);
394 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
395 struct tls_connection *conn,
396 const struct wpabuf *in_data,
397 struct wpabuf **appl_data,
398 int *more_data_needed);
401 * tls_connection_server_handshake - Process TLS handshake (server side)
402 * @tls_ctx: TLS context data from tls_init()
403 * @conn: Connection context data from tls_connection_init()
404 * @in_data: Input data from TLS peer
405 * @appl_data: Pointer to application data pointer, or %NULL if dropped
406 * Returns: Output data, %NULL on failure
408 * The caller is responsible for freeing the returned output data.
410 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
411 struct tls_connection *conn,
412 const struct wpabuf *in_data,
413 struct wpabuf **appl_data);
416 * tls_connection_encrypt - Encrypt data into TLS tunnel
417 * @tls_ctx: TLS context data from tls_init()
418 * @conn: Connection context data from tls_connection_init()
419 * @in_data: Plaintext data to be encrypted
420 * Returns: Encrypted TLS data or %NULL on failure
422 * This function is used after TLS handshake has been completed successfully to
423 * send data in the encrypted tunnel. The caller is responsible for freeing the
424 * returned output data.
426 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
427 struct tls_connection *conn,
428 const struct wpabuf *in_data);
431 * tls_connection_decrypt - Decrypt data from TLS tunnel
432 * @tls_ctx: TLS context data from tls_init()
433 * @conn: Connection context data from tls_connection_init()
434 * @in_data: Encrypted TLS data
435 * Returns: Decrypted TLS data or %NULL on failure
437 * This function is used after TLS handshake has been completed successfully to
438 * receive data from the encrypted tunnel. The caller is responsible for
439 * freeing the returned output data.
441 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
442 struct tls_connection *conn,
443 const struct wpabuf *in_data);
445 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
446 struct tls_connection *conn,
447 const struct wpabuf *in_data,
448 int *more_data_needed);
451 * tls_connection_resumed - Was session resumption used
452 * @tls_ctx: TLS context data from tls_init()
453 * @conn: Connection context data from tls_connection_init()
454 * Returns: 1 if current session used session resumption, 0 if not
456 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
460 TLS_CIPHER_RC4_SHA /* 0x0005 */,
461 TLS_CIPHER_AES128_SHA /* 0x002f */,
462 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
463 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
467 * tls_connection_set_cipher_list - Configure acceptable cipher suites
468 * @tls_ctx: TLS context data from tls_init()
469 * @conn: Connection context data from tls_connection_init()
470 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
472 * Returns: 0 on success, -1 on failure
474 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
475 struct tls_connection *conn,
479 * tls_get_version - Get the current TLS version number
480 * @tls_ctx: TLS context data from tls_init()
481 * @conn: Connection context data from tls_connection_init()
482 * @buf: Buffer for returning the TLS version number
484 * Returns: 0 on success, -1 on failure
486 * Get the currently used TLS version number.
488 int __must_check tls_get_version(void *tls_ctx, struct tls_connection *conn,
489 char *buf, size_t buflen);
492 * tls_get_cipher - Get current cipher name
493 * @tls_ctx: TLS context data from tls_init()
494 * @conn: Connection context data from tls_connection_init()
495 * @buf: Buffer for the cipher name
497 * Returns: 0 on success, -1 on failure
499 * Get the name of the currently used cipher.
501 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
502 char *buf, size_t buflen);
505 * tls_connection_enable_workaround - Enable TLS workaround options
506 * @tls_ctx: TLS context data from tls_init()
507 * @conn: Connection context data from tls_connection_init()
508 * Returns: 0 on success, -1 on failure
510 * This function is used to enable connection-specific workaround options for
511 * buffer SSL/TLS implementations.
513 int __must_check tls_connection_enable_workaround(void *tls_ctx,
514 struct tls_connection *conn);
517 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
518 * @tls_ctx: TLS context data from tls_init()
519 * @conn: Connection context data from tls_connection_init()
520 * @ext_type: Extension type
521 * @data: Extension payload (%NULL to remove extension)
522 * @data_len: Extension payload length
523 * Returns: 0 on success, -1 on failure
525 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
526 struct tls_connection *conn,
527 int ext_type, const u8 *data,
531 * tls_connection_get_failed - Get connection failure status
532 * @tls_ctx: TLS context data from tls_init()
533 * @conn: Connection context data from tls_connection_init()
535 * Returns >0 if connection has failed, 0 if not.
537 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
540 * tls_connection_get_read_alerts - Get connection read alert status
541 * @tls_ctx: TLS context data from tls_init()
542 * @conn: Connection context data from tls_connection_init()
543 * Returns: Number of times a fatal read (remote end reported error) has
544 * happened during this connection.
546 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
549 * tls_connection_get_write_alerts - Get connection write alert status
550 * @tls_ctx: TLS context data from tls_init()
551 * @conn: Connection context data from tls_connection_init()
552 * Returns: Number of times a fatal write (locally detected error) has happened
553 * during this connection.
555 int tls_connection_get_write_alerts(void *tls_ctx,
556 struct tls_connection *conn);
558 typedef int (*tls_session_ticket_cb)
559 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
560 const u8 *server_random, u8 *master_secret);
562 int __must_check tls_connection_set_session_ticket_cb(
563 void *tls_ctx, struct tls_connection *conn,
564 tls_session_ticket_cb cb, void *ctx);
566 void tls_connection_set_log_cb(struct tls_connection *conn,
567 void (*log_cb)(void *ctx, const char *msg),
570 #define TLS_BREAK_VERIFY_DATA BIT(0)
571 #define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
572 #define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
573 #define TLS_DHE_PRIME_511B BIT(3)
574 #define TLS_DHE_PRIME_767B BIT(4)
575 #define TLS_DHE_PRIME_15 BIT(5)
576 #define TLS_DHE_PRIME_58B BIT(6)
577 #define TLS_DHE_NON_PRIME BIT(7)
579 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
581 int tls_get_library_version(char *buf, size_t buf_len);
583 void tls_connection_set_success_data(struct tls_connection *conn,
584 struct wpabuf *data);
586 void tls_connection_set_success_data_resumed(struct tls_connection *conn);
588 const struct wpabuf *
589 tls_connection_get_success_data(struct tls_connection *conn);
591 void tls_connection_remove_session(struct tls_connection *conn);