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 #include <openssl/x509.h>
14 struct tls_connection;
17 const u8 *client_random;
18 size_t client_random_len;
19 const u8 *server_random;
20 size_t server_random_len;
24 TLS_CERT_CHAIN_SUCCESS,
25 TLS_CERT_CHAIN_FAILURE,
31 * Note: These are used as identifier with external programs and as such, the
32 * values must not be changed.
34 enum tls_fail_reason {
35 TLS_FAIL_UNSPECIFIED = 0,
36 TLS_FAIL_UNTRUSTED = 1,
38 TLS_FAIL_NOT_YET_VALID = 3,
40 TLS_FAIL_SUBJECT_MISMATCH = 5,
41 TLS_FAIL_ALTSUBJECT_MISMATCH = 6,
42 TLS_FAIL_BAD_CERTIFICATE = 7,
43 TLS_FAIL_SERVER_CHAIN_PROBE = 8,
44 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH = 9,
45 TLS_FAIL_DOMAIN_MISMATCH = 10,
49 #define TLS_MAX_ALT_SUBJECT 10
51 union tls_event_data {
55 enum tls_fail_reason reason;
56 const char *reason_txt;
57 const struct wpabuf *cert;
63 const struct wpabuf *cert;
66 const char *altsubject[TLS_MAX_ALT_SUBJECT];
73 const char *description;
78 const char *opensc_engine_path;
79 const char *pkcs11_engine_path;
80 const char *pkcs11_module_path;
83 const char *openssl_ciphers;
84 unsigned int tls_session_lifetime;
86 void (*event_cb)(void *ctx, enum tls_event ev,
87 union tls_event_data *data);
91 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
92 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
93 #define TLS_CONN_DISABLE_SESSION_TICKET BIT(2)
94 #define TLS_CONN_REQUEST_OCSP BIT(3)
95 #define TLS_CONN_REQUIRE_OCSP BIT(4)
96 #define TLS_CONN_DISABLE_TLSv1_1 BIT(5)
97 #define TLS_CONN_DISABLE_TLSv1_2 BIT(6)
98 #define TLS_CONN_EAP_FAST BIT(7)
99 #define TLS_CONN_DISABLE_TLSv1_0 BIT(8)
101 struct X509; /* from OpenSSL */
104 * struct tls_connection_params - Parameters for TLS connection
105 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
107 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
108 * @ca_cert_blob_len: ca_cert_blob length
109 * @ca_path: Path to CA certificates (OpenSSL specific)
110 * @subject_match: String to match in the subject of the peer certificate or
111 * %NULL to allow all subjects
112 * @altsubject_match: String to match in the alternative subject of the peer
113 * certificate or %NULL to allow all alternative subjects
114 * @suffix_match: String to suffix match in the dNSName or CN of the peer
115 * certificate or %NULL to allow all domain names. This may allow subdomains an
116 * wildcard certificates. Each domain name label must have a full match.
117 * @domain_match: String to match in the dNSName or CN of the peer
118 * certificate or %NULL to allow all domain names. This requires a full,
119 * case-insensitive match.
120 * @client_cert: File or reference name for client X.509 certificate in PEM or
122 * @client_cert_blob: client_cert as inlined data or %NULL if not used
123 * @client_cert_blob_len: client_cert_blob length
124 * @private_key: File or reference name for client private key in PEM or DER
125 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
126 * @private_key_blob: private_key as inlined data or %NULL if not used
127 * @private_key_blob_len: private_key_blob length
128 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
129 * passphrase is used.
130 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
131 * @dh_blob: dh_file as inlined data or %NULL if not used
132 * @dh_blob_len: dh_blob length
133 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
134 * (this is OpenSSL specific for now)
135 * @engine_id: engine id string (this is OpenSSL specific for now)
136 * @ppin: pointer to the pin variable in the configuration
137 * (this is OpenSSL specific for now)
138 * @key_id: the private key's id when using engine (this is OpenSSL
140 * @cert_id: the certificate's id when using engine
141 * @ca_cert_id: the CA certificate's id when using engine
142 * @openssl_ciphers: OpenSSL cipher configuration
143 * @flags: Parameter options (TLS_CONN_*)
144 * @ocsp_stapling_response: DER encoded file with cached OCSP stapling response
145 * or %NULL if OCSP is not enabled
146 * @server_cert_cb: Optional callback to be used to validate server certificate
147 * when no CA or path was specified.
148 * @server_cert_ctx: Optional context arg for server_cert_cb.
150 * TLS connection parameters to be configured with tls_connection_set_params()
151 * and tls_global_set_params().
153 * Certificates and private key can be configured either as a reference name
154 * (file path or reference to certificate store) or by providing the same data
155 * as a pointer to the data in memory. Only one option will be used for each
158 struct tls_connection_params {
160 const u8 *ca_cert_blob;
161 size_t ca_cert_blob_len;
163 const char *subject_match;
164 const char *altsubject_match;
165 const char *suffix_match;
166 const char *domain_match;
167 const char *client_cert;
168 const u8 *client_cert_blob;
169 size_t client_cert_blob_len;
170 const char *private_key;
171 const u8 *private_key_blob;
172 size_t private_key_blob_len;
173 const char *private_key_passwd;
178 /* OpenSSL specific variables */
180 const char *engine_id;
184 const char *ca_cert_id;
185 const char *openssl_ciphers;
188 const char *ocsp_stapling_response;
191 * If non-null, specifies a callback method that can be used to
192 * confirm the validity of a peer certificate.
194 int (*server_cert_cb)(int ok_so_far, X509* cert, void *ca_ctx);
195 void *server_cert_ctx;
200 * tls_init - Initialize TLS library
201 * @conf: Configuration data for TLS library
202 * Returns: Context data to be used as tls_ctx in calls to other functions,
203 * or %NULL on failure.
205 * Called once during program startup and once for each RSN pre-authentication
206 * session. In other words, there can be two concurrent TLS contexts. If global
207 * library initialization is needed (i.e., one that is shared between both
208 * authentication types), the TLS library wrapper should maintain a reference
209 * counter and do global initialization only when moving from 0 to 1 reference.
211 void * tls_init(const struct tls_config *conf);
214 * tls_deinit - Deinitialize TLS library
215 * @tls_ctx: TLS context data from tls_init()
217 * Called once during program shutdown and once for each RSN pre-authentication
218 * session. If global library deinitialization is needed (i.e., one that is
219 * shared between both authentication types), the TLS library wrapper should
220 * maintain a reference counter and do global deinitialization only when moving
221 * from 1 to 0 references.
223 void tls_deinit(void *tls_ctx);
226 * tls_get_errors - Process pending errors
227 * @tls_ctx: TLS context data from tls_init()
228 * Returns: Number of found error, 0 if no errors detected.
230 * Process all pending TLS errors.
232 int tls_get_errors(void *tls_ctx);
235 * tls_connection_init - Initialize a new TLS connection
236 * @tls_ctx: TLS context data from tls_init()
237 * Returns: Connection context data, conn for other function calls
239 struct tls_connection * tls_connection_init(void *tls_ctx);
242 * tls_connection_deinit - Free TLS connection data
243 * @tls_ctx: TLS context data from tls_init()
244 * @conn: Connection context data from tls_connection_init()
246 * Release all resources allocated for TLS connection.
248 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
251 * tls_connection_established - Has the TLS connection been completed?
252 * @tls_ctx: TLS context data from tls_init()
253 * @conn: Connection context data from tls_connection_init()
254 * Returns: 1 if TLS connection has been completed, 0 if not.
256 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
259 * tls_connection_shutdown - Shutdown TLS connection
260 * @tls_ctx: TLS context data from tls_init()
261 * @conn: Connection context data from tls_connection_init()
262 * Returns: 0 on success, -1 on failure
264 * Shutdown current TLS connection without releasing all resources. New
265 * connection can be started by using the same conn without having to call
266 * tls_connection_init() or setting certificates etc. again. The new
267 * connection should try to use session resumption.
269 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
272 TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN = -4,
273 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
274 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
278 * tls_connection_set_params - Set TLS connection parameters
279 * @tls_ctx: TLS context data from tls_init()
280 * @conn: Connection context data from tls_connection_init()
281 * @params: Connection parameters
282 * Returns: 0 on success, -1 on failure,
283 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
285 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
286 * PKCS#11 engine private key, or
287 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
291 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
292 const struct tls_connection_params *params);
295 * tls_global_set_params - Set TLS parameters for all TLS connection
296 * @tls_ctx: TLS context data from tls_init()
297 * @params: Global TLS parameters
298 * Returns: 0 on success, -1 on failure,
299 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
301 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
302 * PKCS#11 engine private key, or
303 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
306 int __must_check tls_global_set_params(
307 void *tls_ctx, const struct tls_connection_params *params);
310 * tls_global_set_verify - Set global certificate verification options
311 * @tls_ctx: TLS context data from tls_init()
312 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
313 * 2 = verify CRL for all certificates
314 * Returns: 0 on success, -1 on failure
316 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
319 * tls_connection_set_verify - Set certificate verification options
320 * @tls_ctx: TLS context data from tls_init()
321 * @conn: Connection context data from tls_connection_init()
322 * @verify_peer: 1 = verify peer certificate
323 * @flags: Connection flags (TLS_CONN_*)
324 * @session_ctx: Session caching context or %NULL to use default
325 * @session_ctx_len: Length of @session_ctx in bytes.
326 * Returns: 0 on success, -1 on failure
328 int __must_check tls_connection_set_verify(void *tls_ctx,
329 struct tls_connection *conn,
332 const u8 *session_ctx,
333 size_t session_ctx_len);
336 * tls_connection_get_random - Get random data from TLS connection
337 * @tls_ctx: TLS context data from tls_init()
338 * @conn: Connection context data from tls_connection_init()
339 * @data: Structure of client/server random data (filled on success)
340 * Returns: 0 on success, -1 on failure
342 int __must_check tls_connection_get_random(void *tls_ctx,
343 struct tls_connection *conn,
344 struct tls_random *data);
347 * tls_connection_prf - Use TLS-PRF to derive keying material
348 * @tls_ctx: TLS context data from tls_init()
349 * @conn: Connection context data from tls_connection_init()
350 * @label: Label (e.g., description of the key) for PRF
351 * @server_random_first: seed is 0 = client_random|server_random,
352 * 1 = server_random|client_random
353 * @skip_keyblock: Skip TLS key block from the beginning of PRF output
354 * @out: Buffer for output data from TLS-PRF
355 * @out_len: Length of the output buffer
356 * Returns: 0 on success, -1 on failure
358 * tls_connection_prf() is required so that further keying material can be
359 * derived from the master secret. Example implementation of this function is in
360 * tls_prf_sha1_md5() when it is called with seed set to
361 * client_random|server_random (or server_random|client_random). For TLSv1.2 and
362 * newer, a different PRF is needed, though.
364 int __must_check tls_connection_prf(void *tls_ctx,
365 struct tls_connection *conn,
367 int server_random_first,
369 u8 *out, size_t out_len);
372 * tls_connection_handshake - Process TLS handshake (client side)
373 * @tls_ctx: TLS context data from tls_init()
374 * @conn: Connection context data from tls_connection_init()
375 * @in_data: Input data from TLS server
376 * @appl_data: Pointer to application data pointer, or %NULL if dropped
377 * Returns: Output data, %NULL on failure
379 * The caller is responsible for freeing the returned output data. If the final
380 * handshake message includes application data, this is decrypted and
381 * appl_data (if not %NULL) is set to point this data. The caller is
382 * responsible for freeing appl_data.
384 * This function is used during TLS handshake. The first call is done with
385 * in_data == %NULL and the library is expected to return ClientHello packet.
386 * This packet is then send to the server and a response from server is given
387 * to TLS library by calling this function again with in_data pointing to the
388 * TLS message from the server.
390 * If the TLS handshake fails, this function may return %NULL. However, if the
391 * TLS library has a TLS alert to send out, that should be returned as the
392 * output data. In this case, tls_connection_get_failed() must return failure
395 * tls_connection_established() should return 1 once the TLS handshake has been
396 * completed successfully.
398 struct wpabuf * tls_connection_handshake(void *tls_ctx,
399 struct tls_connection *conn,
400 const struct wpabuf *in_data,
401 struct wpabuf **appl_data);
403 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
404 struct tls_connection *conn,
405 const struct wpabuf *in_data,
406 struct wpabuf **appl_data,
407 int *more_data_needed);
410 * tls_connection_server_handshake - Process TLS handshake (server side)
411 * @tls_ctx: TLS context data from tls_init()
412 * @conn: Connection context data from tls_connection_init()
413 * @in_data: Input data from TLS peer
414 * @appl_data: Pointer to application data pointer, or %NULL if dropped
415 * Returns: Output data, %NULL on failure
417 * The caller is responsible for freeing the returned output data.
419 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
420 struct tls_connection *conn,
421 const struct wpabuf *in_data,
422 struct wpabuf **appl_data);
425 * tls_connection_encrypt - Encrypt data into TLS tunnel
426 * @tls_ctx: TLS context data from tls_init()
427 * @conn: Connection context data from tls_connection_init()
428 * @in_data: Plaintext data to be encrypted
429 * Returns: Encrypted TLS data or %NULL on failure
431 * This function is used after TLS handshake has been completed successfully to
432 * send data in the encrypted tunnel. The caller is responsible for freeing the
433 * returned output data.
435 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
436 struct tls_connection *conn,
437 const struct wpabuf *in_data);
440 * tls_connection_decrypt - Decrypt data from TLS tunnel
441 * @tls_ctx: TLS context data from tls_init()
442 * @conn: Connection context data from tls_connection_init()
443 * @in_data: Encrypted TLS data
444 * Returns: Decrypted TLS data or %NULL on failure
446 * This function is used after TLS handshake has been completed successfully to
447 * receive data from the encrypted tunnel. The caller is responsible for
448 * freeing the returned output data.
450 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
451 struct tls_connection *conn,
452 const struct wpabuf *in_data);
454 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
455 struct tls_connection *conn,
456 const struct wpabuf *in_data,
457 int *more_data_needed);
460 * tls_connection_resumed - Was session resumption used
461 * @tls_ctx: TLS context data from tls_init()
462 * @conn: Connection context data from tls_connection_init()
463 * Returns: 1 if current session used session resumption, 0 if not
465 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
469 TLS_CIPHER_RC4_SHA /* 0x0005 */,
470 TLS_CIPHER_AES128_SHA /* 0x002f */,
471 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
472 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
476 * tls_connection_set_cipher_list - Configure acceptable cipher suites
477 * @tls_ctx: TLS context data from tls_init()
478 * @conn: Connection context data from tls_connection_init()
479 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
481 * Returns: 0 on success, -1 on failure
483 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
484 struct tls_connection *conn,
488 * tls_get_version - Get the current TLS version number
489 * @tls_ctx: TLS context data from tls_init()
490 * @conn: Connection context data from tls_connection_init()
491 * @buf: Buffer for returning the TLS version number
493 * Returns: 0 on success, -1 on failure
495 * Get the currently used TLS version number.
497 int __must_check tls_get_version(void *tls_ctx, struct tls_connection *conn,
498 char *buf, size_t buflen);
501 * tls_get_cipher - Get current cipher name
502 * @tls_ctx: TLS context data from tls_init()
503 * @conn: Connection context data from tls_connection_init()
504 * @buf: Buffer for the cipher name
506 * Returns: 0 on success, -1 on failure
508 * Get the name of the currently used cipher.
510 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
511 char *buf, size_t buflen);
514 * tls_connection_enable_workaround - Enable TLS workaround options
515 * @tls_ctx: TLS context data from tls_init()
516 * @conn: Connection context data from tls_connection_init()
517 * Returns: 0 on success, -1 on failure
519 * This function is used to enable connection-specific workaround options for
520 * buffer SSL/TLS implementations.
522 int __must_check tls_connection_enable_workaround(void *tls_ctx,
523 struct tls_connection *conn);
526 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
527 * @tls_ctx: TLS context data from tls_init()
528 * @conn: Connection context data from tls_connection_init()
529 * @ext_type: Extension type
530 * @data: Extension payload (%NULL to remove extension)
531 * @data_len: Extension payload length
532 * Returns: 0 on success, -1 on failure
534 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
535 struct tls_connection *conn,
536 int ext_type, const u8 *data,
540 * tls_connection_get_failed - Get connection failure status
541 * @tls_ctx: TLS context data from tls_init()
542 * @conn: Connection context data from tls_connection_init()
544 * Returns >0 if connection has failed, 0 if not.
546 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
549 * tls_connection_get_read_alerts - Get connection read 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 read (remote end reported error) has
553 * happened during this connection.
555 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
558 * tls_connection_get_write_alerts - Get connection write alert status
559 * @tls_ctx: TLS context data from tls_init()
560 * @conn: Connection context data from tls_connection_init()
561 * Returns: Number of times a fatal write (locally detected error) has happened
562 * during this connection.
564 int tls_connection_get_write_alerts(void *tls_ctx,
565 struct tls_connection *conn);
567 typedef int (*tls_session_ticket_cb)
568 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
569 const u8 *server_random, u8 *master_secret);
571 int __must_check tls_connection_set_session_ticket_cb(
572 void *tls_ctx, struct tls_connection *conn,
573 tls_session_ticket_cb cb, void *ctx);
575 void tls_connection_set_log_cb(struct tls_connection *conn,
576 void (*log_cb)(void *ctx, const char *msg),
579 #define TLS_BREAK_VERIFY_DATA BIT(0)
580 #define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
581 #define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
582 #define TLS_DHE_PRIME_511B BIT(3)
583 #define TLS_DHE_PRIME_767B BIT(4)
584 #define TLS_DHE_PRIME_15 BIT(5)
585 #define TLS_DHE_PRIME_58B BIT(6)
586 #define TLS_DHE_NON_PRIME BIT(7)
588 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
590 int tls_get_library_version(char *buf, size_t buf_len);
592 void tls_connection_set_success_data(struct tls_connection *conn,
593 struct wpabuf *data);
595 void tls_connection_set_success_data_resumed(struct tls_connection *conn);
597 const struct wpabuf *
598 tls_connection_get_success_data(struct tls_connection *conn);
600 void tls_connection_remove_session(struct tls_connection *conn);