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