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