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,
44 TLS_FAIL_INSUFFICIENT_KEY_LEN = 11,
45 TLS_FAIL_DN_MISMATCH = 12,
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];
68 const char *serial_num;
74 const char *description;
79 const char *opensc_engine_path;
80 const char *pkcs11_engine_path;
81 const char *pkcs11_module_path;
84 const char *openssl_ciphers;
85 unsigned int tls_session_lifetime;
86 unsigned int crl_reload_interval;
87 unsigned int tls_flags;
89 void (*event_cb)(void *ctx, enum tls_event ev,
90 union tls_event_data *data);
94 #define TLS_CONN_ALLOW_SIGN_RSA_MD5 BIT(0)
95 #define TLS_CONN_DISABLE_TIME_CHECKS BIT(1)
96 #define TLS_CONN_DISABLE_SESSION_TICKET BIT(2)
97 #define TLS_CONN_REQUEST_OCSP BIT(3)
98 #define TLS_CONN_REQUIRE_OCSP BIT(4)
99 #define TLS_CONN_DISABLE_TLSv1_1 BIT(5)
100 #define TLS_CONN_DISABLE_TLSv1_2 BIT(6)
101 #define TLS_CONN_EAP_FAST BIT(7)
102 #define TLS_CONN_DISABLE_TLSv1_0 BIT(8)
103 #define TLS_CONN_EXT_CERT_CHECK BIT(9)
104 #define TLS_CONN_REQUIRE_OCSP_ALL BIT(10)
105 #define TLS_CONN_SUITEB BIT(11)
106 #define TLS_CONN_SUITEB_NO_ECDH BIT(12)
107 #define TLS_CONN_DISABLE_TLSv1_3 BIT(13)
108 #define TLS_CONN_ENABLE_TLSv1_0 BIT(14)
109 #define TLS_CONN_ENABLE_TLSv1_1 BIT(15)
110 #define TLS_CONN_ENABLE_TLSv1_2 BIT(16)
113 * struct tls_connection_params - Parameters for TLS connection
114 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
116 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
117 * @ca_cert_blob_len: ca_cert_blob length
118 * @ca_path: Path to CA certificates (OpenSSL specific)
119 * @subject_match: String to match in the subject of the peer certificate or
120 * %NULL to allow all subjects
121 * @altsubject_match: String to match in the alternative subject of the peer
122 * certificate or %NULL to allow all alternative subjects
123 * @suffix_match: Semicolon deliminated string of values to suffix match against
124 * the dNSName or CN of the peer certificate or %NULL to allow all domain names.
125 * This may allow subdomains and wildcard certificates. Each domain name label
126 * must have a full case-insensitive match.
127 * @domain_match: String to match in the dNSName or CN of the peer
128 * certificate or %NULL to allow all domain names. This requires a full,
129 * case-insensitive match.
131 * More than one match string can be provided by using semicolons to
132 * separate the strings (e.g., example.org;example.com). When multiple
133 * strings are specified, a match with any one of the values is
134 * considered a sufficient match for the certificate, i.e., the
135 * conditions are ORed together.
136 * @client_cert: File or reference name for client X.509 certificate in PEM or
138 * @client_cert_blob: client_cert as inlined data or %NULL if not used
139 * @client_cert_blob_len: client_cert_blob length
140 * @private_key: File or reference name for client private key in PEM or DER
141 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
142 * @private_key_blob: private_key as inlined data or %NULL if not used
143 * @private_key_blob_len: private_key_blob length
144 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
145 * passphrase is used.
146 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
147 * @dh_blob: dh_file as inlined data or %NULL if not used
148 * @dh_blob_len: dh_blob length
149 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
150 * (this is OpenSSL specific for now)
151 * @engine_id: engine id string (this is OpenSSL specific for now)
152 * @ppin: pointer to the pin variable in the configuration
153 * (this is OpenSSL specific for now)
154 * @key_id: the private key's id when using engine (this is OpenSSL
156 * @cert_id: the certificate's id when using engine
157 * @ca_cert_id: the CA certificate's id when using engine
158 * @openssl_ciphers: OpenSSL cipher configuration
159 * @openssl_ecdh_curves: OpenSSL ECDH curve configuration. %NULL for auto if
160 * supported, empty string to disable, or a colon-separated curve list.
161 * @flags: Parameter options (TLS_CONN_*)
162 * @ocsp_stapling_response: DER encoded file with cached OCSP stapling response
163 * or %NULL if OCSP is not enabled
164 * @ocsp_stapling_response_multi: DER encoded file with cached OCSP stapling
165 * response list (OCSPResponseList for ocsp_multi in RFC 6961) or %NULL if
166 * ocsp_multi is not enabled
167 * @check_cert_subject: Client certificate subject name matching string
169 * TLS connection parameters to be configured with tls_connection_set_params()
170 * and tls_global_set_params().
172 * Certificates and private key can be configured either as a reference name
173 * (file path or reference to certificate store) or by providing the same data
174 * as a pointer to the data in memory. Only one option will be used for each
177 struct tls_connection_params {
179 const u8 *ca_cert_blob;
180 size_t ca_cert_blob_len;
182 const char *subject_match;
183 const char *altsubject_match;
184 const char *suffix_match;
185 const char *domain_match;
186 const char *client_cert;
187 const u8 *client_cert_blob;
188 size_t client_cert_blob_len;
189 const char *private_key;
190 const u8 *private_key_blob;
191 size_t private_key_blob_len;
192 const char *private_key_passwd;
197 /* OpenSSL specific variables */
199 const char *engine_id;
203 const char *ca_cert_id;
204 const char *openssl_ciphers;
205 const char *openssl_ecdh_curves;
208 const char *ocsp_stapling_response;
209 const char *ocsp_stapling_response_multi;
210 const char *check_cert_subject;
215 * tls_init - Initialize TLS library
216 * @conf: Configuration data for TLS library
217 * Returns: Context data to be used as tls_ctx in calls to other functions,
218 * or %NULL on failure.
220 * Called once during program startup and once for each RSN pre-authentication
221 * session. In other words, there can be two concurrent TLS contexts. If global
222 * library initialization is needed (i.e., one that is shared between both
223 * authentication types), the TLS library wrapper should maintain a reference
224 * counter and do global initialization only when moving from 0 to 1 reference.
226 void * tls_init(const struct tls_config *conf);
229 * tls_deinit - Deinitialize TLS library
230 * @tls_ctx: TLS context data from tls_init()
232 * Called once during program shutdown and once for each RSN pre-authentication
233 * session. If global library deinitialization is needed (i.e., one that is
234 * shared between both authentication types), the TLS library wrapper should
235 * maintain a reference counter and do global deinitialization only when moving
236 * from 1 to 0 references.
238 void tls_deinit(void *tls_ctx);
241 * tls_get_errors - Process pending errors
242 * @tls_ctx: TLS context data from tls_init()
243 * Returns: Number of found error, 0 if no errors detected.
245 * Process all pending TLS errors.
247 int tls_get_errors(void *tls_ctx);
250 * tls_connection_init - Initialize a new TLS connection
251 * @tls_ctx: TLS context data from tls_init()
252 * Returns: Connection context data, conn for other function calls
254 struct tls_connection * tls_connection_init(void *tls_ctx);
257 * tls_connection_deinit - Free TLS connection data
258 * @tls_ctx: TLS context data from tls_init()
259 * @conn: Connection context data from tls_connection_init()
261 * Release all resources allocated for TLS connection.
263 void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
266 * tls_connection_established - Has the TLS connection been completed?
267 * @tls_ctx: TLS context data from tls_init()
268 * @conn: Connection context data from tls_connection_init()
269 * Returns: 1 if TLS connection has been completed, 0 if not.
271 int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
274 * tls_connection_peer_serial_num - Fetch peer certificate serial number
275 * @tls_ctx: TLS context data from tls_init()
276 * @conn: Connection context data from tls_connection_init()
277 * Returns: Allocated string buffer containing the peer certificate serial
278 * number or %NULL on error.
280 * The caller is responsible for freeing the returned buffer with os_free().
282 char * tls_connection_peer_serial_num(void *tls_ctx,
283 struct tls_connection *conn);
286 * tls_connection_shutdown - Shutdown TLS connection
287 * @tls_ctx: TLS context data from tls_init()
288 * @conn: Connection context data from tls_connection_init()
289 * Returns: 0 on success, -1 on failure
291 * Shutdown current TLS connection without releasing all resources. New
292 * connection can be started by using the same conn without having to call
293 * tls_connection_init() or setting certificates etc. again. The new
294 * connection should try to use session resumption.
296 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
299 TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN = -4,
300 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
301 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
305 * tls_connection_set_params - Set TLS connection parameters
306 * @tls_ctx: TLS context data from tls_init()
307 * @conn: Connection context data from tls_connection_init()
308 * @params: Connection parameters
309 * Returns: 0 on success, -1 on failure,
310 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
312 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
313 * PKCS#11 engine private key, or
314 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
318 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
319 const struct tls_connection_params *params);
322 * tls_global_set_params - Set TLS parameters for all TLS connection
323 * @tls_ctx: TLS context data from tls_init()
324 * @params: Global TLS parameters
325 * Returns: 0 on success, -1 on failure,
326 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine
328 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
329 * PKCS#11 engine private key, or
330 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine
333 int __must_check tls_global_set_params(
334 void *tls_ctx, const struct tls_connection_params *params);
337 * tls_global_set_verify - Set global certificate verification options
338 * @tls_ctx: TLS context data from tls_init()
339 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
340 * 2 = verify CRL for all certificates
341 * @strict: 0 = allow CRL time errors, 1 = do not allow CRL time errors
342 * Returns: 0 on success, -1 on failure
344 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl,
348 * tls_connection_set_verify - Set certificate verification options
349 * @tls_ctx: TLS context data from tls_init()
350 * @conn: Connection context data from tls_connection_init()
351 * @verify_peer: 1 = verify peer certificate
352 * @flags: Connection flags (TLS_CONN_*)
353 * @session_ctx: Session caching context or %NULL to use default
354 * @session_ctx_len: Length of @session_ctx in bytes.
355 * Returns: 0 on success, -1 on failure
357 int __must_check tls_connection_set_verify(void *tls_ctx,
358 struct tls_connection *conn,
361 const u8 *session_ctx,
362 size_t session_ctx_len);
365 * tls_connection_get_random - Get random data from TLS connection
366 * @tls_ctx: TLS context data from tls_init()
367 * @conn: Connection context data from tls_connection_init()
368 * @data: Structure of client/server random data (filled on success)
369 * Returns: 0 on success, -1 on failure
371 int __must_check tls_connection_get_random(void *tls_ctx,
372 struct tls_connection *conn,
373 struct tls_random *data);
376 * tls_connection_export_key - Derive keying material from a TLS connection
377 * @tls_ctx: TLS context data from tls_init()
378 * @conn: Connection context data from tls_connection_init()
379 * @label: Label (e.g., description of the key) for PRF
380 * @context: Optional extra upper-layer context (max len 2^16)
381 * @context_len: The length of the context value
382 * @out: Buffer for output data from TLS-PRF
383 * @out_len: Length of the output buffer
384 * Returns: 0 on success, -1 on failure
386 * Exports keying material using the mechanism described in RFC 5705. If
387 * context is %NULL, context is not provided; otherwise, context is provided
388 * (including the case of empty context with context_len == 0).
390 int __must_check tls_connection_export_key(void *tls_ctx,
391 struct tls_connection *conn,
395 u8 *out, size_t out_len);
398 * tls_connection_get_eap_fast_key - Derive key material for EAP-FAST
399 * @tls_ctx: TLS context data from tls_init()
400 * @conn: Connection context data from tls_connection_init()
401 * @out: Buffer for output data from TLS-PRF
402 * @out_len: Length of the output buffer
403 * Returns: 0 on success, -1 on failure
405 * Exports key material after the normal TLS key block for use with
406 * EAP-FAST. Most callers will want tls_connection_export_key(), but EAP-FAST
407 * uses a different legacy mechanism.
409 int __must_check tls_connection_get_eap_fast_key(void *tls_ctx,
410 struct tls_connection *conn,
411 u8 *out, size_t out_len);
414 * tls_connection_handshake - Process TLS handshake (client side)
415 * @tls_ctx: TLS context data from tls_init()
416 * @conn: Connection context data from tls_connection_init()
417 * @in_data: Input data from TLS server
418 * @appl_data: Pointer to application data pointer, or %NULL if dropped
419 * Returns: Output data, %NULL on failure
421 * The caller is responsible for freeing the returned output data. If the final
422 * handshake message includes application data, this is decrypted and
423 * appl_data (if not %NULL) is set to point this data. The caller is
424 * responsible for freeing appl_data.
426 * This function is used during TLS handshake. The first call is done with
427 * in_data == %NULL and the library is expected to return ClientHello packet.
428 * This packet is then send to the server and a response from server is given
429 * to TLS library by calling this function again with in_data pointing to the
430 * TLS message from the server.
432 * If the TLS handshake fails, this function may return %NULL. However, if the
433 * TLS library has a TLS alert to send out, that should be returned as the
434 * output data. In this case, tls_connection_get_failed() must return failure
437 * tls_connection_established() should return 1 once the TLS handshake has been
438 * completed successfully.
440 struct wpabuf * tls_connection_handshake(void *tls_ctx,
441 struct tls_connection *conn,
442 const struct wpabuf *in_data,
443 struct wpabuf **appl_data);
445 struct wpabuf * tls_connection_handshake2(void *tls_ctx,
446 struct tls_connection *conn,
447 const struct wpabuf *in_data,
448 struct wpabuf **appl_data,
449 int *more_data_needed);
452 * tls_connection_server_handshake - Process TLS handshake (server side)
453 * @tls_ctx: TLS context data from tls_init()
454 * @conn: Connection context data from tls_connection_init()
455 * @in_data: Input data from TLS peer
456 * @appl_data: Pointer to application data pointer, or %NULL if dropped
457 * Returns: Output data, %NULL on failure
459 * The caller is responsible for freeing the returned output data.
461 struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
462 struct tls_connection *conn,
463 const struct wpabuf *in_data,
464 struct wpabuf **appl_data);
467 * tls_connection_encrypt - Encrypt data into TLS tunnel
468 * @tls_ctx: TLS context data from tls_init()
469 * @conn: Connection context data from tls_connection_init()
470 * @in_data: Plaintext data to be encrypted
471 * Returns: Encrypted TLS data or %NULL on failure
473 * This function is used after TLS handshake has been completed successfully to
474 * send data in the encrypted tunnel. The caller is responsible for freeing the
475 * returned output data.
477 struct wpabuf * tls_connection_encrypt(void *tls_ctx,
478 struct tls_connection *conn,
479 const struct wpabuf *in_data);
482 * tls_connection_decrypt - Decrypt data from TLS tunnel
483 * @tls_ctx: TLS context data from tls_init()
484 * @conn: Connection context data from tls_connection_init()
485 * @in_data: Encrypted TLS data
486 * Returns: Decrypted TLS data or %NULL on failure
488 * This function is used after TLS handshake has been completed successfully to
489 * receive data from the encrypted tunnel. The caller is responsible for
490 * freeing the returned output data.
492 struct wpabuf * tls_connection_decrypt(void *tls_ctx,
493 struct tls_connection *conn,
494 const struct wpabuf *in_data);
496 struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
497 struct tls_connection *conn,
498 const struct wpabuf *in_data,
499 int *more_data_needed);
502 * tls_connection_resumed - Was session resumption used
503 * @tls_ctx: TLS context data from tls_init()
504 * @conn: Connection context data from tls_connection_init()
505 * Returns: 1 if current session used session resumption, 0 if not
507 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
511 TLS_CIPHER_RC4_SHA /* 0x0005 */,
512 TLS_CIPHER_AES128_SHA /* 0x002f */,
513 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
514 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */,
515 TLS_CIPHER_RSA_DHE_AES256_SHA /* 0x0039 */,
516 TLS_CIPHER_AES256_SHA /* 0x0035 */,
520 * tls_connection_set_cipher_list - Configure acceptable cipher suites
521 * @tls_ctx: TLS context data from tls_init()
522 * @conn: Connection context data from tls_connection_init()
523 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
525 * Returns: 0 on success, -1 on failure
527 int __must_check tls_connection_set_cipher_list(void *tls_ctx,
528 struct tls_connection *conn,
532 * tls_get_version - Get the current TLS version number
533 * @tls_ctx: TLS context data from tls_init()
534 * @conn: Connection context data from tls_connection_init()
535 * @buf: Buffer for returning the TLS version number
537 * Returns: 0 on success, -1 on failure
539 * Get the currently used TLS version number.
541 int __must_check tls_get_version(void *tls_ctx, struct tls_connection *conn,
542 char *buf, size_t buflen);
545 * tls_get_cipher - Get current cipher name
546 * @tls_ctx: TLS context data from tls_init()
547 * @conn: Connection context data from tls_connection_init()
548 * @buf: Buffer for the cipher name
550 * Returns: 0 on success, -1 on failure
552 * Get the name of the currently used cipher.
554 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
555 char *buf, size_t buflen);
558 * tls_connection_enable_workaround - Enable TLS workaround options
559 * @tls_ctx: TLS context data from tls_init()
560 * @conn: Connection context data from tls_connection_init()
561 * Returns: 0 on success, -1 on failure
563 * This function is used to enable connection-specific workaround options for
564 * buffer SSL/TLS implementations.
566 int __must_check tls_connection_enable_workaround(void *tls_ctx,
567 struct tls_connection *conn);
570 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
571 * @tls_ctx: TLS context data from tls_init()
572 * @conn: Connection context data from tls_connection_init()
573 * @ext_type: Extension type
574 * @data: Extension payload (%NULL to remove extension)
575 * @data_len: Extension payload length
576 * Returns: 0 on success, -1 on failure
578 int __must_check tls_connection_client_hello_ext(void *tls_ctx,
579 struct tls_connection *conn,
580 int ext_type, const u8 *data,
584 * tls_connection_get_failed - Get connection failure status
585 * @tls_ctx: TLS context data from tls_init()
586 * @conn: Connection context data from tls_connection_init()
588 * Returns >0 if connection has failed, 0 if not.
590 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
593 * tls_connection_get_read_alerts - Get connection read alert status
594 * @tls_ctx: TLS context data from tls_init()
595 * @conn: Connection context data from tls_connection_init()
596 * Returns: Number of times a fatal read (remote end reported error) has
597 * happened during this connection.
599 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
602 * tls_connection_get_write_alerts - Get connection write alert status
603 * @tls_ctx: TLS context data from tls_init()
604 * @conn: Connection context data from tls_connection_init()
605 * Returns: Number of times a fatal write (locally detected error) has happened
606 * during this connection.
608 int tls_connection_get_write_alerts(void *tls_ctx,
609 struct tls_connection *conn);
611 typedef int (*tls_session_ticket_cb)
612 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
613 const u8 *server_random, u8 *master_secret);
615 int __must_check tls_connection_set_session_ticket_cb(
616 void *tls_ctx, struct tls_connection *conn,
617 tls_session_ticket_cb cb, void *ctx);
619 void tls_connection_set_log_cb(struct tls_connection *conn,
620 void (*log_cb)(void *ctx, const char *msg),
623 #define TLS_BREAK_VERIFY_DATA BIT(0)
624 #define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
625 #define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
626 #define TLS_DHE_PRIME_511B BIT(3)
627 #define TLS_DHE_PRIME_767B BIT(4)
628 #define TLS_DHE_PRIME_15 BIT(5)
629 #define TLS_DHE_PRIME_58B BIT(6)
630 #define TLS_DHE_NON_PRIME BIT(7)
632 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
634 int tls_get_library_version(char *buf, size_t buf_len);
636 void tls_connection_set_success_data(struct tls_connection *conn,
637 struct wpabuf *data);
639 void tls_connection_set_success_data_resumed(struct tls_connection *conn);
641 const struct wpabuf *
642 tls_connection_get_success_data(struct tls_connection *conn);
644 void tls_connection_remove_session(struct tls_connection *conn);