tls.h revision af9da3180dc20f57df1fc1e1811f3df9fa9e6ab5
1/*
2 * SSL/TLS interface definition
3 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#ifndef TLS_H
10#define TLS_H
11
12struct tls_connection;
13
14struct tls_keys {
15	const u8 *client_random;
16	size_t client_random_len;
17	const u8 *server_random;
18	size_t server_random_len;
19};
20
21enum tls_event {
22	TLS_CERT_CHAIN_SUCCESS,
23	TLS_CERT_CHAIN_FAILURE,
24	TLS_PEER_CERTIFICATE,
25	TLS_ALERT
26};
27
28/*
29 * Note: These are used as identifier with external programs and as such, the
30 * values must not be changed.
31 */
32enum tls_fail_reason {
33	TLS_FAIL_UNSPECIFIED = 0,
34	TLS_FAIL_UNTRUSTED = 1,
35	TLS_FAIL_REVOKED = 2,
36	TLS_FAIL_NOT_YET_VALID = 3,
37	TLS_FAIL_EXPIRED = 4,
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};
45
46
47#define TLS_MAX_ALT_SUBJECT 10
48
49union tls_event_data {
50	struct {
51		int depth;
52		const char *subject;
53		enum tls_fail_reason reason;
54		const char *reason_txt;
55		const struct wpabuf *cert;
56	} cert_fail;
57
58	struct {
59		int depth;
60		const char *subject;
61		const struct wpabuf *cert;
62		const u8 *hash;
63		size_t hash_len;
64		const char *altsubject[TLS_MAX_ALT_SUBJECT];
65		int num_altsubject;
66	} peer_cert;
67
68	struct {
69		int is_local;
70		const char *type;
71		const char *description;
72	} alert;
73};
74
75struct tls_config {
76	const char *opensc_engine_path;
77	const char *pkcs11_engine_path;
78	const char *pkcs11_module_path;
79	int fips_mode;
80	int cert_in_cb;
81	const char *openssl_ciphers;
82
83	void (*event_cb)(void *ctx, enum tls_event ev,
84			 union tls_event_data *data);
85	void *cb_ctx;
86};
87
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
97/**
98 * struct tls_connection_params - Parameters for TLS connection
99 * @ca_cert: File or reference name for CA X.509 certificate in PEM or DER
100 * format
101 * @ca_cert_blob: ca_cert as inlined data or %NULL if not used
102 * @ca_cert_blob_len: ca_cert_blob length
103 * @ca_path: Path to CA certificates (OpenSSL specific)
104 * @subject_match: String to match in the subject of the peer certificate or
105 * %NULL to allow all subjects
106 * @altsubject_match: String to match in the alternative subject of the peer
107 * certificate or %NULL to allow all alternative subjects
108 * @suffix_match: String to suffix match in the dNSName or CN of the peer
109 * certificate or %NULL to allow all domain names. This may allow subdomains an
110 * wildcard certificates. Each domain name label must have a full match.
111 * @domain_match: String to match in the dNSName or CN of the peer
112 * certificate or %NULL to allow all domain names. This requires a full,
113 * case-insensitive match.
114 * @client_cert: File or reference name for client X.509 certificate in PEM or
115 * DER format
116 * @client_cert_blob: client_cert as inlined data or %NULL if not used
117 * @client_cert_blob_len: client_cert_blob length
118 * @private_key: File or reference name for client private key in PEM or DER
119 * format (traditional format (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
120 * @private_key_blob: private_key as inlined data or %NULL if not used
121 * @private_key_blob_len: private_key_blob length
122 * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
123 * passphrase is used.
124 * @dh_file: File name for DH/DSA data in PEM format, or %NULL if not used
125 * @dh_blob: dh_file as inlined data or %NULL if not used
126 * @dh_blob_len: dh_blob length
127 * @engine: 1 = use engine (e.g., a smartcard) for private key operations
128 * (this is OpenSSL specific for now)
129 * @engine_id: engine id string (this is OpenSSL specific for now)
130 * @ppin: pointer to the pin variable in the configuration
131 * (this is OpenSSL specific for now)
132 * @key_id: the private key's id when using engine (this is OpenSSL
133 * specific for now)
134 * @cert_id: the certificate's id when using engine
135 * @ca_cert_id: the CA certificate's id when using engine
136 * @openssl_ciphers: OpenSSL cipher configuration
137 * @flags: Parameter options (TLS_CONN_*)
138 * @ocsp_stapling_response: DER encoded file with cached OCSP stapling response
139 *	or %NULL if OCSP is not enabled
140 *
141 * TLS connection parameters to be configured with tls_connection_set_params()
142 * and tls_global_set_params().
143 *
144 * Certificates and private key can be configured either as a reference name
145 * (file path or reference to certificate store) or by providing the same data
146 * as a pointer to the data in memory. Only one option will be used for each
147 * field.
148 */
149struct tls_connection_params {
150	const char *ca_cert;
151	const u8 *ca_cert_blob;
152	size_t ca_cert_blob_len;
153	const char *ca_path;
154	const char *subject_match;
155	const char *altsubject_match;
156	const char *suffix_match;
157	const char *domain_match;
158	const char *client_cert;
159	const u8 *client_cert_blob;
160	size_t client_cert_blob_len;
161	const char *private_key;
162	const u8 *private_key_blob;
163	size_t private_key_blob_len;
164	const char *private_key_passwd;
165	const char *dh_file;
166	const u8 *dh_blob;
167	size_t dh_blob_len;
168
169	/* OpenSSL specific variables */
170	int engine;
171	const char *engine_id;
172	const char *pin;
173	const char *key_id;
174	const char *cert_id;
175	const char *ca_cert_id;
176	const char *openssl_ciphers;
177
178	unsigned int flags;
179	const char *ocsp_stapling_response;
180};
181
182
183/**
184 * tls_init - Initialize TLS library
185 * @conf: Configuration data for TLS library
186 * Returns: Context data to be used as tls_ctx in calls to other functions,
187 * or %NULL on failure.
188 *
189 * Called once during program startup and once for each RSN pre-authentication
190 * session. In other words, there can be two concurrent TLS contexts. If global
191 * library initialization is needed (i.e., one that is shared between both
192 * authentication types), the TLS library wrapper should maintain a reference
193 * counter and do global initialization only when moving from 0 to 1 reference.
194 */
195void * tls_init(const struct tls_config *conf);
196
197/**
198 * tls_deinit - Deinitialize TLS library
199 * @tls_ctx: TLS context data from tls_init()
200 *
201 * Called once during program shutdown and once for each RSN pre-authentication
202 * session. If global library deinitialization is needed (i.e., one that is
203 * shared between both authentication types), the TLS library wrapper should
204 * maintain a reference counter and do global deinitialization only when moving
205 * from 1 to 0 references.
206 */
207void tls_deinit(void *tls_ctx);
208
209/**
210 * tls_get_errors - Process pending errors
211 * @tls_ctx: TLS context data from tls_init()
212 * Returns: Number of found error, 0 if no errors detected.
213 *
214 * Process all pending TLS errors.
215 */
216int tls_get_errors(void *tls_ctx);
217
218/**
219 * tls_connection_init - Initialize a new TLS connection
220 * @tls_ctx: TLS context data from tls_init()
221 * Returns: Connection context data, conn for other function calls
222 */
223struct tls_connection * tls_connection_init(void *tls_ctx);
224
225/**
226 * tls_connection_deinit - Free TLS connection data
227 * @tls_ctx: TLS context data from tls_init()
228 * @conn: Connection context data from tls_connection_init()
229 *
230 * Release all resources allocated for TLS connection.
231 */
232void tls_connection_deinit(void *tls_ctx, struct tls_connection *conn);
233
234/**
235 * tls_connection_established - Has the TLS connection been completed?
236 * @tls_ctx: TLS context data from tls_init()
237 * @conn: Connection context data from tls_connection_init()
238 * Returns: 1 if TLS connection has been completed, 0 if not.
239 */
240int tls_connection_established(void *tls_ctx, struct tls_connection *conn);
241
242/**
243 * tls_connection_shutdown - Shutdown TLS connection
244 * @tls_ctx: TLS context data from tls_init()
245 * @conn: Connection context data from tls_connection_init()
246 * Returns: 0 on success, -1 on failure
247 *
248 * Shutdown current TLS connection without releasing all resources. New
249 * connection can be started by using the same conn without having to call
250 * tls_connection_init() or setting certificates etc. again. The new
251 * connection should try to use session resumption.
252 */
253int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn);
254
255enum {
256	TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3,
257	TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2
258};
259
260/**
261 * tls_connection_set_params - Set TLS connection parameters
262 * @tls_ctx: TLS context data from tls_init()
263 * @conn: Connection context data from tls_connection_init()
264 * @params: Connection parameters
265 * Returns: 0 on success, -1 on failure,
266 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
267 * PKCS#11 engine failure, or
268 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
269 * PKCS#11 engine private key.
270 */
271int __must_check
272tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
273			  const struct tls_connection_params *params);
274
275/**
276 * tls_global_set_params - Set TLS parameters for all TLS connection
277 * @tls_ctx: TLS context data from tls_init()
278 * @params: Global TLS parameters
279 * Returns: 0 on success, -1 on failure,
280 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on possible PIN error causing
281 * PKCS#11 engine failure, or
282 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the
283 * PKCS#11 engine private key.
284 */
285int __must_check tls_global_set_params(
286	void *tls_ctx, const struct tls_connection_params *params);
287
288/**
289 * tls_global_set_verify - Set global certificate verification options
290 * @tls_ctx: TLS context data from tls_init()
291 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate,
292 * 2 = verify CRL for all certificates
293 * Returns: 0 on success, -1 on failure
294 */
295int __must_check tls_global_set_verify(void *tls_ctx, int check_crl);
296
297/**
298 * tls_connection_set_verify - Set certificate verification options
299 * @tls_ctx: TLS context data from tls_init()
300 * @conn: Connection context data from tls_connection_init()
301 * @verify_peer: 1 = verify peer certificate
302 * Returns: 0 on success, -1 on failure
303 */
304int __must_check tls_connection_set_verify(void *tls_ctx,
305					   struct tls_connection *conn,
306					   int verify_peer);
307
308/**
309 * tls_connection_get_keys - Get random data from TLS connection
310 * @tls_ctx: TLS context data from tls_init()
311 * @conn: Connection context data from tls_connection_init()
312 * @keys: Structure of client/server random data (filled on success)
313 * Returns: 0 on success, -1 on failure
314 */
315int __must_check tls_connection_get_keys(void *tls_ctx,
316					 struct tls_connection *conn,
317					 struct tls_keys *keys);
318
319/**
320 * tls_connection_prf - Use TLS-PRF to derive keying material
321 * @tls_ctx: TLS context data from tls_init()
322 * @conn: Connection context data from tls_connection_init()
323 * @label: Label (e.g., description of the key) for PRF
324 * @server_random_first: seed is 0 = client_random|server_random,
325 * 1 = server_random|client_random
326 * @skip_keyblock: Skip TLS key block from the beginning of PRF output
327 * @out: Buffer for output data from TLS-PRF
328 * @out_len: Length of the output buffer
329 * Returns: 0 on success, -1 on failure
330 *
331 * This function is optional to implement if tls_connection_get_keys() provides
332 * access to master secret and server/client random values. If these values are
333 * not exported from the TLS library, tls_connection_prf() is required so that
334 * further keying material can be derived from the master secret. If not
335 * implemented, the function will still need to be defined, but it can just
336 * return -1. Example implementation of this function is in tls_prf_sha1_md5()
337 * when it is called with seed set to client_random|server_random (or
338 * server_random|client_random).
339 */
340int __must_check  tls_connection_prf(void *tls_ctx,
341				     struct tls_connection *conn,
342				     const char *label,
343				     int server_random_first,
344				     int skip_keyblock,
345				     u8 *out, size_t out_len);
346
347/**
348 * tls_connection_handshake - Process TLS handshake (client side)
349 * @tls_ctx: TLS context data from tls_init()
350 * @conn: Connection context data from tls_connection_init()
351 * @in_data: Input data from TLS server
352 * @appl_data: Pointer to application data pointer, or %NULL if dropped
353 * Returns: Output data, %NULL on failure
354 *
355 * The caller is responsible for freeing the returned output data. If the final
356 * handshake message includes application data, this is decrypted and
357 * appl_data (if not %NULL) is set to point this data. The caller is
358 * responsible for freeing appl_data.
359 *
360 * This function is used during TLS handshake. The first call is done with
361 * in_data == %NULL and the library is expected to return ClientHello packet.
362 * This packet is then send to the server and a response from server is given
363 * to TLS library by calling this function again with in_data pointing to the
364 * TLS message from the server.
365 *
366 * If the TLS handshake fails, this function may return %NULL. However, if the
367 * TLS library has a TLS alert to send out, that should be returned as the
368 * output data. In this case, tls_connection_get_failed() must return failure
369 * (> 0).
370 *
371 * tls_connection_established() should return 1 once the TLS handshake has been
372 * completed successfully.
373 */
374struct wpabuf * tls_connection_handshake(void *tls_ctx,
375					 struct tls_connection *conn,
376					 const struct wpabuf *in_data,
377					 struct wpabuf **appl_data);
378
379struct wpabuf * tls_connection_handshake2(void *tls_ctx,
380					  struct tls_connection *conn,
381					  const struct wpabuf *in_data,
382					  struct wpabuf **appl_data,
383					  int *more_data_needed);
384
385/**
386 * tls_connection_server_handshake - Process TLS handshake (server side)
387 * @tls_ctx: TLS context data from tls_init()
388 * @conn: Connection context data from tls_connection_init()
389 * @in_data: Input data from TLS peer
390 * @appl_data: Pointer to application data pointer, or %NULL if dropped
391 * Returns: Output data, %NULL on failure
392 *
393 * The caller is responsible for freeing the returned output data.
394 */
395struct wpabuf * tls_connection_server_handshake(void *tls_ctx,
396						struct tls_connection *conn,
397						const struct wpabuf *in_data,
398						struct wpabuf **appl_data);
399
400/**
401 * tls_connection_encrypt - Encrypt data into TLS tunnel
402 * @tls_ctx: TLS context data from tls_init()
403 * @conn: Connection context data from tls_connection_init()
404 * @in_data: Plaintext data to be encrypted
405 * Returns: Encrypted TLS data or %NULL on failure
406 *
407 * This function is used after TLS handshake has been completed successfully to
408 * send data in the encrypted tunnel. The caller is responsible for freeing the
409 * returned output data.
410 */
411struct wpabuf * tls_connection_encrypt(void *tls_ctx,
412				       struct tls_connection *conn,
413				       const struct wpabuf *in_data);
414
415/**
416 * tls_connection_decrypt - Decrypt data from TLS tunnel
417 * @tls_ctx: TLS context data from tls_init()
418 * @conn: Connection context data from tls_connection_init()
419 * @in_data: Encrypted TLS data
420 * Returns: Decrypted TLS data or %NULL on failure
421 *
422 * This function is used after TLS handshake has been completed successfully to
423 * receive data from the encrypted tunnel. The caller is responsible for
424 * freeing the returned output data.
425 */
426struct wpabuf * tls_connection_decrypt(void *tls_ctx,
427				       struct tls_connection *conn,
428				       const struct wpabuf *in_data);
429
430struct wpabuf * tls_connection_decrypt2(void *tls_ctx,
431					struct tls_connection *conn,
432					const struct wpabuf *in_data,
433					int *more_data_needed);
434
435/**
436 * tls_connection_resumed - Was session resumption used
437 * @tls_ctx: TLS context data from tls_init()
438 * @conn: Connection context data from tls_connection_init()
439 * Returns: 1 if current session used session resumption, 0 if not
440 */
441int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn);
442
443enum {
444	TLS_CIPHER_NONE,
445	TLS_CIPHER_RC4_SHA /* 0x0005 */,
446	TLS_CIPHER_AES128_SHA /* 0x002f */,
447	TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */,
448	TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */
449};
450
451/**
452 * tls_connection_set_cipher_list - Configure acceptable cipher suites
453 * @tls_ctx: TLS context data from tls_init()
454 * @conn: Connection context data from tls_connection_init()
455 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
456 * (TLS_CIPHER_*).
457 * Returns: 0 on success, -1 on failure
458 */
459int __must_check tls_connection_set_cipher_list(void *tls_ctx,
460						struct tls_connection *conn,
461						u8 *ciphers);
462
463/**
464 * tls_get_cipher - Get current cipher name
465 * @tls_ctx: TLS context data from tls_init()
466 * @conn: Connection context data from tls_connection_init()
467 * @buf: Buffer for the cipher name
468 * @buflen: buf size
469 * Returns: 0 on success, -1 on failure
470 *
471 * Get the name of the currently used cipher.
472 */
473int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn,
474				char *buf, size_t buflen);
475
476/**
477 * tls_connection_enable_workaround - Enable TLS workaround options
478 * @tls_ctx: TLS context data from tls_init()
479 * @conn: Connection context data from tls_connection_init()
480 * Returns: 0 on success, -1 on failure
481 *
482 * This function is used to enable connection-specific workaround options for
483 * buffer SSL/TLS implementations.
484 */
485int __must_check tls_connection_enable_workaround(void *tls_ctx,
486						  struct tls_connection *conn);
487
488/**
489 * tls_connection_client_hello_ext - Set TLS extension for ClientHello
490 * @tls_ctx: TLS context data from tls_init()
491 * @conn: Connection context data from tls_connection_init()
492 * @ext_type: Extension type
493 * @data: Extension payload (%NULL to remove extension)
494 * @data_len: Extension payload length
495 * Returns: 0 on success, -1 on failure
496 */
497int __must_check tls_connection_client_hello_ext(void *tls_ctx,
498						 struct tls_connection *conn,
499						 int ext_type, const u8 *data,
500						 size_t data_len);
501
502/**
503 * tls_connection_get_failed - Get connection failure status
504 * @tls_ctx: TLS context data from tls_init()
505 * @conn: Connection context data from tls_connection_init()
506 *
507 * Returns >0 if connection has failed, 0 if not.
508 */
509int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn);
510
511/**
512 * tls_connection_get_read_alerts - Get connection read alert status
513 * @tls_ctx: TLS context data from tls_init()
514 * @conn: Connection context data from tls_connection_init()
515 * Returns: Number of times a fatal read (remote end reported error) has
516 * happened during this connection.
517 */
518int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn);
519
520/**
521 * tls_connection_get_write_alerts - Get connection write alert status
522 * @tls_ctx: TLS context data from tls_init()
523 * @conn: Connection context data from tls_connection_init()
524 * Returns: Number of times a fatal write (locally detected error) has happened
525 * during this connection.
526 */
527int tls_connection_get_write_alerts(void *tls_ctx,
528				    struct tls_connection *conn);
529
530/**
531 * tls_capabilities - Get supported TLS capabilities
532 * @tls_ctx: TLS context data from tls_init()
533 * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*)
534 */
535unsigned int tls_capabilities(void *tls_ctx);
536
537typedef int (*tls_session_ticket_cb)
538(void *ctx, const u8 *ticket, size_t len, const u8 *client_random,
539 const u8 *server_random, u8 *master_secret);
540
541int __must_check  tls_connection_set_session_ticket_cb(
542	void *tls_ctx, struct tls_connection *conn,
543	tls_session_ticket_cb cb, void *ctx);
544
545void tls_connection_set_log_cb(struct tls_connection *conn,
546			       void (*log_cb)(void *ctx, const char *msg),
547			       void *ctx);
548
549#define TLS_BREAK_VERIFY_DATA BIT(0)
550#define TLS_BREAK_SRV_KEY_X_HASH BIT(1)
551#define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2)
552#define TLS_DHE_PRIME_511B BIT(3)
553#define TLS_DHE_PRIME_767B BIT(4)
554#define TLS_DHE_PRIME_15 BIT(5)
555#define TLS_DHE_PRIME_58B BIT(6)
556#define TLS_DHE_NON_PRIME BIT(7)
557
558void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags);
559
560int tls_get_library_version(char *buf, size_t buf_len);
561
562#endif /* TLS_H */
563