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