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 12 struct tls_connection; 13 14 struct 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 21 enum 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 */ 32 enum 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 49 union 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 75 struct 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 */ 149 struct 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 */ 195 void * 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 */ 207 void 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 */ 216 int 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 */ 223 struct 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 */ 232 void 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 */ 240 int 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 */ 253 int tls_connection_shutdown(void *tls_ctx, struct tls_connection *conn); 254 255 enum { 256 TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN = -4, 257 TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED = -3, 258 TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED = -2 259 }; 260 261 /** 262 * tls_connection_set_params - Set TLS connection parameters 263 * @tls_ctx: TLS context data from tls_init() 264 * @conn: Connection context data from tls_connection_init() 265 * @params: Connection parameters 266 * Returns: 0 on success, -1 on failure, 267 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine 268 * failure, or 269 * TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED (-3) on failure to verify the 270 * PKCS#11 engine private key, or 271 * TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN (-4) on PIN error causing PKCS#11 engine 272 * failure. 273 */ 274 int __must_check 275 tls_connection_set_params(void *tls_ctx, struct tls_connection *conn, 276 const struct tls_connection_params *params); 277 278 /** 279 * tls_global_set_params - Set TLS parameters for all TLS connection 280 * @tls_ctx: TLS context data from tls_init() 281 * @params: Global TLS parameters 282 * Returns: 0 on success, -1 on failure, 283 * TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED (-2) on error causing PKCS#11 engine 284 * failure, or 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 288 * failure. 289 */ 290 int __must_check tls_global_set_params( 291 void *tls_ctx, const struct tls_connection_params *params); 292 293 /** 294 * tls_global_set_verify - Set global certificate verification options 295 * @tls_ctx: TLS context data from tls_init() 296 * @check_crl: 0 = do not verify CRLs, 1 = verify CRL for the user certificate, 297 * 2 = verify CRL for all certificates 298 * Returns: 0 on success, -1 on failure 299 */ 300 int __must_check tls_global_set_verify(void *tls_ctx, int check_crl); 301 302 /** 303 * tls_connection_set_verify - Set certificate verification options 304 * @tls_ctx: TLS context data from tls_init() 305 * @conn: Connection context data from tls_connection_init() 306 * @verify_peer: 1 = verify peer certificate 307 * Returns: 0 on success, -1 on failure 308 */ 309 int __must_check tls_connection_set_verify(void *tls_ctx, 310 struct tls_connection *conn, 311 int verify_peer); 312 313 /** 314 * tls_connection_get_keys - Get random data from TLS connection 315 * @tls_ctx: TLS context data from tls_init() 316 * @conn: Connection context data from tls_connection_init() 317 * @keys: Structure of client/server random data (filled on success) 318 * Returns: 0 on success, -1 on failure 319 */ 320 int __must_check tls_connection_get_keys(void *tls_ctx, 321 struct tls_connection *conn, 322 struct tls_keys *keys); 323 324 /** 325 * tls_connection_prf - Use TLS-PRF to derive keying material 326 * @tls_ctx: TLS context data from tls_init() 327 * @conn: Connection context data from tls_connection_init() 328 * @label: Label (e.g., description of the key) for PRF 329 * @server_random_first: seed is 0 = client_random|server_random, 330 * 1 = server_random|client_random 331 * @skip_keyblock: Skip TLS key block from the beginning of PRF output 332 * @out: Buffer for output data from TLS-PRF 333 * @out_len: Length of the output buffer 334 * Returns: 0 on success, -1 on failure 335 * 336 * This function is optional to implement if tls_connection_get_keys() provides 337 * access to master secret and server/client random values. If these values are 338 * not exported from the TLS library, tls_connection_prf() is required so that 339 * further keying material can be derived from the master secret. If not 340 * implemented, the function will still need to be defined, but it can just 341 * return -1. Example implementation of this function is in tls_prf_sha1_md5() 342 * when it is called with seed set to client_random|server_random (or 343 * server_random|client_random). 344 */ 345 int __must_check tls_connection_prf(void *tls_ctx, 346 struct tls_connection *conn, 347 const char *label, 348 int server_random_first, 349 int skip_keyblock, 350 u8 *out, size_t out_len); 351 352 /** 353 * tls_connection_handshake - Process TLS handshake (client side) 354 * @tls_ctx: TLS context data from tls_init() 355 * @conn: Connection context data from tls_connection_init() 356 * @in_data: Input data from TLS server 357 * @appl_data: Pointer to application data pointer, or %NULL if dropped 358 * Returns: Output data, %NULL on failure 359 * 360 * The caller is responsible for freeing the returned output data. If the final 361 * handshake message includes application data, this is decrypted and 362 * appl_data (if not %NULL) is set to point this data. The caller is 363 * responsible for freeing appl_data. 364 * 365 * This function is used during TLS handshake. The first call is done with 366 * in_data == %NULL and the library is expected to return ClientHello packet. 367 * This packet is then send to the server and a response from server is given 368 * to TLS library by calling this function again with in_data pointing to the 369 * TLS message from the server. 370 * 371 * If the TLS handshake fails, this function may return %NULL. However, if the 372 * TLS library has a TLS alert to send out, that should be returned as the 373 * output data. In this case, tls_connection_get_failed() must return failure 374 * (> 0). 375 * 376 * tls_connection_established() should return 1 once the TLS handshake has been 377 * completed successfully. 378 */ 379 struct wpabuf * tls_connection_handshake(void *tls_ctx, 380 struct tls_connection *conn, 381 const struct wpabuf *in_data, 382 struct wpabuf **appl_data); 383 384 struct wpabuf * tls_connection_handshake2(void *tls_ctx, 385 struct tls_connection *conn, 386 const struct wpabuf *in_data, 387 struct wpabuf **appl_data, 388 int *more_data_needed); 389 390 /** 391 * tls_connection_server_handshake - Process TLS handshake (server side) 392 * @tls_ctx: TLS context data from tls_init() 393 * @conn: Connection context data from tls_connection_init() 394 * @in_data: Input data from TLS peer 395 * @appl_data: Pointer to application data pointer, or %NULL if dropped 396 * Returns: Output data, %NULL on failure 397 * 398 * The caller is responsible for freeing the returned output data. 399 */ 400 struct wpabuf * tls_connection_server_handshake(void *tls_ctx, 401 struct tls_connection *conn, 402 const struct wpabuf *in_data, 403 struct wpabuf **appl_data); 404 405 /** 406 * tls_connection_encrypt - Encrypt data into TLS tunnel 407 * @tls_ctx: TLS context data from tls_init() 408 * @conn: Connection context data from tls_connection_init() 409 * @in_data: Plaintext data to be encrypted 410 * Returns: Encrypted TLS data or %NULL on failure 411 * 412 * This function is used after TLS handshake has been completed successfully to 413 * send data in the encrypted tunnel. The caller is responsible for freeing the 414 * returned output data. 415 */ 416 struct wpabuf * tls_connection_encrypt(void *tls_ctx, 417 struct tls_connection *conn, 418 const struct wpabuf *in_data); 419 420 /** 421 * tls_connection_decrypt - Decrypt data from TLS tunnel 422 * @tls_ctx: TLS context data from tls_init() 423 * @conn: Connection context data from tls_connection_init() 424 * @in_data: Encrypted TLS data 425 * Returns: Decrypted TLS data or %NULL on failure 426 * 427 * This function is used after TLS handshake has been completed successfully to 428 * receive data from the encrypted tunnel. The caller is responsible for 429 * freeing the returned output data. 430 */ 431 struct wpabuf * tls_connection_decrypt(void *tls_ctx, 432 struct tls_connection *conn, 433 const struct wpabuf *in_data); 434 435 struct wpabuf * tls_connection_decrypt2(void *tls_ctx, 436 struct tls_connection *conn, 437 const struct wpabuf *in_data, 438 int *more_data_needed); 439 440 /** 441 * tls_connection_resumed - Was session resumption used 442 * @tls_ctx: TLS context data from tls_init() 443 * @conn: Connection context data from tls_connection_init() 444 * Returns: 1 if current session used session resumption, 0 if not 445 */ 446 int tls_connection_resumed(void *tls_ctx, struct tls_connection *conn); 447 448 enum { 449 TLS_CIPHER_NONE, 450 TLS_CIPHER_RC4_SHA /* 0x0005 */, 451 TLS_CIPHER_AES128_SHA /* 0x002f */, 452 TLS_CIPHER_RSA_DHE_AES128_SHA /* 0x0031 */, 453 TLS_CIPHER_ANON_DH_AES128_SHA /* 0x0034 */ 454 }; 455 456 /** 457 * tls_connection_set_cipher_list - Configure acceptable cipher suites 458 * @tls_ctx: TLS context data from tls_init() 459 * @conn: Connection context data from tls_connection_init() 460 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers 461 * (TLS_CIPHER_*). 462 * Returns: 0 on success, -1 on failure 463 */ 464 int __must_check tls_connection_set_cipher_list(void *tls_ctx, 465 struct tls_connection *conn, 466 u8 *ciphers); 467 468 /** 469 * tls_get_cipher - Get current cipher name 470 * @tls_ctx: TLS context data from tls_init() 471 * @conn: Connection context data from tls_connection_init() 472 * @buf: Buffer for the cipher name 473 * @buflen: buf size 474 * Returns: 0 on success, -1 on failure 475 * 476 * Get the name of the currently used cipher. 477 */ 478 int __must_check tls_get_cipher(void *tls_ctx, struct tls_connection *conn, 479 char *buf, size_t buflen); 480 481 /** 482 * tls_connection_enable_workaround - Enable TLS workaround options 483 * @tls_ctx: TLS context data from tls_init() 484 * @conn: Connection context data from tls_connection_init() 485 * Returns: 0 on success, -1 on failure 486 * 487 * This function is used to enable connection-specific workaround options for 488 * buffer SSL/TLS implementations. 489 */ 490 int __must_check tls_connection_enable_workaround(void *tls_ctx, 491 struct tls_connection *conn); 492 493 /** 494 * tls_connection_client_hello_ext - Set TLS extension for ClientHello 495 * @tls_ctx: TLS context data from tls_init() 496 * @conn: Connection context data from tls_connection_init() 497 * @ext_type: Extension type 498 * @data: Extension payload (%NULL to remove extension) 499 * @data_len: Extension payload length 500 * Returns: 0 on success, -1 on failure 501 */ 502 int __must_check tls_connection_client_hello_ext(void *tls_ctx, 503 struct tls_connection *conn, 504 int ext_type, const u8 *data, 505 size_t data_len); 506 507 /** 508 * tls_connection_get_failed - Get connection failure status 509 * @tls_ctx: TLS context data from tls_init() 510 * @conn: Connection context data from tls_connection_init() 511 * 512 * Returns >0 if connection has failed, 0 if not. 513 */ 514 int tls_connection_get_failed(void *tls_ctx, struct tls_connection *conn); 515 516 /** 517 * tls_connection_get_read_alerts - Get connection read alert status 518 * @tls_ctx: TLS context data from tls_init() 519 * @conn: Connection context data from tls_connection_init() 520 * Returns: Number of times a fatal read (remote end reported error) has 521 * happened during this connection. 522 */ 523 int tls_connection_get_read_alerts(void *tls_ctx, struct tls_connection *conn); 524 525 /** 526 * tls_connection_get_write_alerts - Get connection write alert status 527 * @tls_ctx: TLS context data from tls_init() 528 * @conn: Connection context data from tls_connection_init() 529 * Returns: Number of times a fatal write (locally detected error) has happened 530 * during this connection. 531 */ 532 int tls_connection_get_write_alerts(void *tls_ctx, 533 struct tls_connection *conn); 534 535 /** 536 * tls_capabilities - Get supported TLS capabilities 537 * @tls_ctx: TLS context data from tls_init() 538 * Returns: Bit field of supported TLS capabilities (TLS_CAPABILITY_*) 539 */ 540 unsigned int tls_capabilities(void *tls_ctx); 541 542 typedef int (*tls_session_ticket_cb) 543 (void *ctx, const u8 *ticket, size_t len, const u8 *client_random, 544 const u8 *server_random, u8 *master_secret); 545 546 int __must_check tls_connection_set_session_ticket_cb( 547 void *tls_ctx, struct tls_connection *conn, 548 tls_session_ticket_cb cb, void *ctx); 549 550 void tls_connection_set_log_cb(struct tls_connection *conn, 551 void (*log_cb)(void *ctx, const char *msg), 552 void *ctx); 553 554 #define TLS_BREAK_VERIFY_DATA BIT(0) 555 #define TLS_BREAK_SRV_KEY_X_HASH BIT(1) 556 #define TLS_BREAK_SRV_KEY_X_SIGNATURE BIT(2) 557 #define TLS_DHE_PRIME_511B BIT(3) 558 #define TLS_DHE_PRIME_767B BIT(4) 559 #define TLS_DHE_PRIME_15 BIT(5) 560 #define TLS_DHE_PRIME_58B BIT(6) 561 #define TLS_DHE_NON_PRIME BIT(7) 562 563 void tls_connection_set_test_flags(struct tls_connection *conn, u32 flags); 564 565 int tls_get_library_version(char *buf, size_t buf_len); 566 567 #endif /* TLS_H */ 568