tls.h 19 KB

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