tlsv1_client.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * TLS v1.0/v1.1/v1.2 client (RFC 2246, RFC 4346, RFC 5246)
  3. * Copyright (c) 2006-2014, 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. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/sha1.h"
  11. #include "crypto/tls.h"
  12. #include "tlsv1_common.h"
  13. #include "tlsv1_record.h"
  14. #include "tlsv1_client.h"
  15. #include "tlsv1_client_i.h"
  16. /* TODO:
  17. * Support for a message fragmented across several records (RFC 2246, 6.2.1)
  18. */
  19. void tls_alert(struct tlsv1_client *conn, u8 level, u8 description)
  20. {
  21. conn->alert_level = level;
  22. conn->alert_description = description;
  23. }
  24. void tlsv1_client_free_dh(struct tlsv1_client *conn)
  25. {
  26. os_free(conn->dh_p);
  27. os_free(conn->dh_g);
  28. os_free(conn->dh_ys);
  29. conn->dh_p = conn->dh_g = conn->dh_ys = NULL;
  30. }
  31. int tls_derive_pre_master_secret(u8 *pre_master_secret)
  32. {
  33. WPA_PUT_BE16(pre_master_secret, TLS_VERSION);
  34. if (os_get_random(pre_master_secret + 2,
  35. TLS_PRE_MASTER_SECRET_LEN - 2))
  36. return -1;
  37. return 0;
  38. }
  39. int tls_derive_keys(struct tlsv1_client *conn,
  40. const u8 *pre_master_secret, size_t pre_master_secret_len)
  41. {
  42. u8 seed[2 * TLS_RANDOM_LEN];
  43. u8 key_block[TLS_MAX_KEY_BLOCK_LEN];
  44. u8 *pos;
  45. size_t key_block_len;
  46. if (pre_master_secret) {
  47. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: pre_master_secret",
  48. pre_master_secret, pre_master_secret_len);
  49. os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
  50. os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
  51. TLS_RANDOM_LEN);
  52. if (tls_prf(conn->rl.tls_version,
  53. pre_master_secret, pre_master_secret_len,
  54. "master secret", seed, 2 * TLS_RANDOM_LEN,
  55. conn->master_secret, TLS_MASTER_SECRET_LEN)) {
  56. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive "
  57. "master_secret");
  58. return -1;
  59. }
  60. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: master_secret",
  61. conn->master_secret, TLS_MASTER_SECRET_LEN);
  62. }
  63. os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
  64. os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random, TLS_RANDOM_LEN);
  65. key_block_len = 2 * (conn->rl.hash_size + conn->rl.key_material_len);
  66. if (conn->rl.tls_version == TLS_VERSION_1)
  67. key_block_len += 2 * conn->rl.iv_size;
  68. if (tls_prf(conn->rl.tls_version,
  69. conn->master_secret, TLS_MASTER_SECRET_LEN,
  70. "key expansion", seed, 2 * TLS_RANDOM_LEN,
  71. key_block, key_block_len)) {
  72. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive key_block");
  73. return -1;
  74. }
  75. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: key_block",
  76. key_block, key_block_len);
  77. pos = key_block;
  78. /* client_write_MAC_secret */
  79. os_memcpy(conn->rl.write_mac_secret, pos, conn->rl.hash_size);
  80. pos += conn->rl.hash_size;
  81. /* server_write_MAC_secret */
  82. os_memcpy(conn->rl.read_mac_secret, pos, conn->rl.hash_size);
  83. pos += conn->rl.hash_size;
  84. /* client_write_key */
  85. os_memcpy(conn->rl.write_key, pos, conn->rl.key_material_len);
  86. pos += conn->rl.key_material_len;
  87. /* server_write_key */
  88. os_memcpy(conn->rl.read_key, pos, conn->rl.key_material_len);
  89. pos += conn->rl.key_material_len;
  90. if (conn->rl.tls_version == TLS_VERSION_1) {
  91. /* client_write_IV */
  92. os_memcpy(conn->rl.write_iv, pos, conn->rl.iv_size);
  93. pos += conn->rl.iv_size;
  94. /* server_write_IV */
  95. os_memcpy(conn->rl.read_iv, pos, conn->rl.iv_size);
  96. pos += conn->rl.iv_size;
  97. } else {
  98. /*
  99. * Use IV field to set the mask value for TLS v1.1. A fixed
  100. * mask of zero is used per the RFC 4346, 6.2.3.2 CBC Block
  101. * Cipher option 2a.
  102. */
  103. os_memset(conn->rl.write_iv, 0, conn->rl.iv_size);
  104. }
  105. return 0;
  106. }
  107. /**
  108. * tlsv1_client_handshake - Process TLS handshake
  109. * @conn: TLSv1 client connection data from tlsv1_client_init()
  110. * @in_data: Input data from TLS peer
  111. * @in_len: Input data length
  112. * @out_len: Length of the output buffer.
  113. * @appl_data: Pointer to application data pointer, or %NULL if dropped
  114. * @appl_data_len: Pointer to variable that is set to appl_data length
  115. * @need_more_data: Set to 1 if more data would be needed to complete
  116. * processing
  117. * Returns: Pointer to output data, %NULL on failure
  118. */
  119. u8 * tlsv1_client_handshake(struct tlsv1_client *conn,
  120. const u8 *in_data, size_t in_len,
  121. size_t *out_len, u8 **appl_data,
  122. size_t *appl_data_len, int *need_more_data)
  123. {
  124. const u8 *pos, *end;
  125. u8 *msg = NULL, *in_msg = NULL, *in_pos, *in_end, alert, ct;
  126. size_t in_msg_len;
  127. int no_appl_data;
  128. int used;
  129. if (need_more_data)
  130. *need_more_data = 0;
  131. if (conn->state == CLIENT_HELLO) {
  132. if (in_len)
  133. return NULL;
  134. return tls_send_client_hello(conn, out_len);
  135. }
  136. if (conn->partial_input) {
  137. if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
  138. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  139. "memory for pending record");
  140. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  141. TLS_ALERT_INTERNAL_ERROR);
  142. goto failed;
  143. }
  144. wpabuf_put_data(conn->partial_input, in_data, in_len);
  145. in_data = wpabuf_head(conn->partial_input);
  146. in_len = wpabuf_len(conn->partial_input);
  147. }
  148. if (in_data == NULL || in_len == 0)
  149. return NULL;
  150. pos = in_data;
  151. end = in_data + in_len;
  152. in_msg = os_malloc(in_len);
  153. if (in_msg == NULL)
  154. return NULL;
  155. /* Each received packet may include multiple records */
  156. while (pos < end) {
  157. in_msg_len = in_len;
  158. used = tlsv1_record_receive(&conn->rl, pos, end - pos,
  159. in_msg, &in_msg_len, &alert);
  160. if (used < 0) {
  161. wpa_printf(MSG_DEBUG, "TLSv1: Processing received "
  162. "record failed");
  163. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
  164. goto failed;
  165. }
  166. if (used == 0) {
  167. struct wpabuf *partial;
  168. wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
  169. partial = wpabuf_alloc_copy(pos, end - pos);
  170. wpabuf_free(conn->partial_input);
  171. conn->partial_input = partial;
  172. if (conn->partial_input == NULL) {
  173. wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
  174. "allocate memory for pending "
  175. "record");
  176. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  177. TLS_ALERT_INTERNAL_ERROR);
  178. goto failed;
  179. }
  180. os_free(in_msg);
  181. if (need_more_data)
  182. *need_more_data = 1;
  183. return NULL;
  184. }
  185. ct = pos[0];
  186. in_pos = in_msg;
  187. in_end = in_msg + in_msg_len;
  188. /* Each received record may include multiple messages of the
  189. * same ContentType. */
  190. while (in_pos < in_end) {
  191. in_msg_len = in_end - in_pos;
  192. if (tlsv1_client_process_handshake(conn, ct, in_pos,
  193. &in_msg_len,
  194. appl_data,
  195. appl_data_len) < 0)
  196. goto failed;
  197. in_pos += in_msg_len;
  198. }
  199. pos += used;
  200. }
  201. os_free(in_msg);
  202. in_msg = NULL;
  203. no_appl_data = appl_data == NULL || *appl_data == NULL;
  204. msg = tlsv1_client_handshake_write(conn, out_len, no_appl_data);
  205. failed:
  206. os_free(in_msg);
  207. if (conn->alert_level) {
  208. wpabuf_free(conn->partial_input);
  209. conn->partial_input = NULL;
  210. conn->state = FAILED;
  211. os_free(msg);
  212. msg = tlsv1_client_send_alert(conn, conn->alert_level,
  213. conn->alert_description,
  214. out_len);
  215. } else if (msg == NULL) {
  216. msg = os_zalloc(1);
  217. *out_len = 0;
  218. }
  219. if (need_more_data == NULL || !(*need_more_data)) {
  220. wpabuf_free(conn->partial_input);
  221. conn->partial_input = NULL;
  222. }
  223. return msg;
  224. }
  225. /**
  226. * tlsv1_client_encrypt - Encrypt data into TLS tunnel
  227. * @conn: TLSv1 client connection data from tlsv1_client_init()
  228. * @in_data: Pointer to plaintext data to be encrypted
  229. * @in_len: Input buffer length
  230. * @out_data: Pointer to output buffer (encrypted TLS data)
  231. * @out_len: Maximum out_data length
  232. * Returns: Number of bytes written to out_data, -1 on failure
  233. *
  234. * This function is used after TLS handshake has been completed successfully to
  235. * send data in the encrypted tunnel.
  236. */
  237. int tlsv1_client_encrypt(struct tlsv1_client *conn,
  238. const u8 *in_data, size_t in_len,
  239. u8 *out_data, size_t out_len)
  240. {
  241. size_t rlen;
  242. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Plaintext AppData",
  243. in_data, in_len);
  244. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_APPLICATION_DATA,
  245. out_data, out_len, in_data, in_len, &rlen) < 0) {
  246. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  247. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  248. TLS_ALERT_INTERNAL_ERROR);
  249. return -1;
  250. }
  251. return rlen;
  252. }
  253. /**
  254. * tlsv1_client_decrypt - Decrypt data from TLS tunnel
  255. * @conn: TLSv1 client connection data from tlsv1_client_init()
  256. * @in_data: Pointer to input buffer (encrypted TLS data)
  257. * @in_len: Input buffer length
  258. * @need_more_data: Set to 1 if more data would be needed to complete
  259. * processing
  260. * Returns: Decrypted data or %NULL on failure
  261. *
  262. * This function is used after TLS handshake has been completed successfully to
  263. * receive data from the encrypted tunnel.
  264. */
  265. struct wpabuf * tlsv1_client_decrypt(struct tlsv1_client *conn,
  266. const u8 *in_data, size_t in_len,
  267. int *need_more_data)
  268. {
  269. const u8 *in_end, *pos;
  270. int used;
  271. u8 alert, *out_pos, ct;
  272. size_t olen;
  273. struct wpabuf *buf = NULL;
  274. if (need_more_data)
  275. *need_more_data = 0;
  276. if (conn->partial_input) {
  277. if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
  278. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  279. "memory for pending record");
  280. alert = TLS_ALERT_INTERNAL_ERROR;
  281. goto fail;
  282. }
  283. wpabuf_put_data(conn->partial_input, in_data, in_len);
  284. in_data = wpabuf_head(conn->partial_input);
  285. in_len = wpabuf_len(conn->partial_input);
  286. }
  287. pos = in_data;
  288. in_end = in_data + in_len;
  289. while (pos < in_end) {
  290. ct = pos[0];
  291. if (wpabuf_resize(&buf, in_end - pos) < 0) {
  292. alert = TLS_ALERT_INTERNAL_ERROR;
  293. goto fail;
  294. }
  295. out_pos = wpabuf_put(buf, 0);
  296. olen = wpabuf_tailroom(buf);
  297. used = tlsv1_record_receive(&conn->rl, pos, in_end - pos,
  298. out_pos, &olen, &alert);
  299. if (used < 0) {
  300. wpa_printf(MSG_DEBUG, "TLSv1: Record layer processing "
  301. "failed");
  302. goto fail;
  303. }
  304. if (used == 0) {
  305. struct wpabuf *partial;
  306. wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
  307. partial = wpabuf_alloc_copy(pos, in_end - pos);
  308. wpabuf_free(conn->partial_input);
  309. conn->partial_input = partial;
  310. if (conn->partial_input == NULL) {
  311. wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
  312. "allocate memory for pending "
  313. "record");
  314. alert = TLS_ALERT_INTERNAL_ERROR;
  315. goto fail;
  316. }
  317. if (need_more_data)
  318. *need_more_data = 1;
  319. return buf;
  320. }
  321. if (ct == TLS_CONTENT_TYPE_ALERT) {
  322. if (olen < 2) {
  323. wpa_printf(MSG_DEBUG, "TLSv1: Alert "
  324. "underflow");
  325. alert = TLS_ALERT_DECODE_ERROR;
  326. goto fail;
  327. }
  328. wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
  329. out_pos[0], out_pos[1]);
  330. if (out_pos[0] == TLS_ALERT_LEVEL_WARNING) {
  331. /* Continue processing */
  332. pos += used;
  333. continue;
  334. }
  335. alert = out_pos[1];
  336. goto fail;
  337. }
  338. if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
  339. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected content type "
  340. "0x%x when decrypting application data",
  341. pos[0]);
  342. alert = TLS_ALERT_UNEXPECTED_MESSAGE;
  343. goto fail;
  344. }
  345. wpabuf_put(buf, olen);
  346. pos += used;
  347. }
  348. wpabuf_free(conn->partial_input);
  349. conn->partial_input = NULL;
  350. return buf;
  351. fail:
  352. wpabuf_free(buf);
  353. wpabuf_free(conn->partial_input);
  354. conn->partial_input = NULL;
  355. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
  356. return NULL;
  357. }
  358. /**
  359. * tlsv1_client_global_init - Initialize TLSv1 client
  360. * Returns: 0 on success, -1 on failure
  361. *
  362. * This function must be called before using any other TLSv1 client functions.
  363. */
  364. int tlsv1_client_global_init(void)
  365. {
  366. return crypto_global_init();
  367. }
  368. /**
  369. * tlsv1_client_global_deinit - Deinitialize TLSv1 client
  370. *
  371. * This function can be used to deinitialize the TLSv1 client that was
  372. * initialized by calling tlsv1_client_global_init(). No TLSv1 client functions
  373. * can be called after this before calling tlsv1_client_global_init() again.
  374. */
  375. void tlsv1_client_global_deinit(void)
  376. {
  377. crypto_global_deinit();
  378. }
  379. /**
  380. * tlsv1_client_init - Initialize TLSv1 client connection
  381. * Returns: Pointer to TLSv1 client connection data or %NULL on failure
  382. */
  383. struct tlsv1_client * tlsv1_client_init(void)
  384. {
  385. struct tlsv1_client *conn;
  386. size_t count;
  387. u16 *suites;
  388. conn = os_zalloc(sizeof(*conn));
  389. if (conn == NULL)
  390. return NULL;
  391. conn->state = CLIENT_HELLO;
  392. if (tls_verify_hash_init(&conn->verify) < 0) {
  393. wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize verify "
  394. "hash");
  395. os_free(conn);
  396. return NULL;
  397. }
  398. count = 0;
  399. suites = conn->cipher_suites;
  400. suites[count++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA256;
  401. suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA256;
  402. suites[count++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
  403. suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA;
  404. suites[count++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA256;
  405. suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
  406. suites[count++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA;
  407. suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA;
  408. suites[count++] = TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA;
  409. suites[count++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA;
  410. suites[count++] = TLS_RSA_WITH_RC4_128_SHA;
  411. suites[count++] = TLS_RSA_WITH_RC4_128_MD5;
  412. conn->num_cipher_suites = count;
  413. conn->rl.tls_version = TLS_VERSION;
  414. return conn;
  415. }
  416. /**
  417. * tlsv1_client_deinit - Deinitialize TLSv1 client connection
  418. * @conn: TLSv1 client connection data from tlsv1_client_init()
  419. */
  420. void tlsv1_client_deinit(struct tlsv1_client *conn)
  421. {
  422. crypto_public_key_free(conn->server_rsa_key);
  423. tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
  424. tlsv1_record_change_write_cipher(&conn->rl);
  425. tlsv1_record_change_read_cipher(&conn->rl);
  426. tls_verify_hash_free(&conn->verify);
  427. os_free(conn->client_hello_ext);
  428. tlsv1_client_free_dh(conn);
  429. tlsv1_cred_free(conn->cred);
  430. wpabuf_free(conn->partial_input);
  431. os_free(conn);
  432. }
  433. /**
  434. * tlsv1_client_established - Check whether connection has been established
  435. * @conn: TLSv1 client connection data from tlsv1_client_init()
  436. * Returns: 1 if connection is established, 0 if not
  437. */
  438. int tlsv1_client_established(struct tlsv1_client *conn)
  439. {
  440. return conn->state == ESTABLISHED;
  441. }
  442. /**
  443. * tlsv1_client_prf - Use TLS-PRF to derive keying material
  444. * @conn: TLSv1 client connection data from tlsv1_client_init()
  445. * @label: Label (e.g., description of the key) for PRF
  446. * @server_random_first: seed is 0 = client_random|server_random,
  447. * 1 = server_random|client_random
  448. * @out: Buffer for output data from TLS-PRF
  449. * @out_len: Length of the output buffer
  450. * Returns: 0 on success, -1 on failure
  451. */
  452. int tlsv1_client_prf(struct tlsv1_client *conn, const char *label,
  453. int server_random_first, u8 *out, size_t out_len)
  454. {
  455. u8 seed[2 * TLS_RANDOM_LEN];
  456. if (conn->state != ESTABLISHED)
  457. return -1;
  458. if (server_random_first) {
  459. os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
  460. os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random,
  461. TLS_RANDOM_LEN);
  462. } else {
  463. os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
  464. os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
  465. TLS_RANDOM_LEN);
  466. }
  467. return tls_prf(conn->rl.tls_version,
  468. conn->master_secret, TLS_MASTER_SECRET_LEN,
  469. label, seed, 2 * TLS_RANDOM_LEN, out, out_len);
  470. }
  471. /**
  472. * tlsv1_client_get_cipher - Get current cipher name
  473. * @conn: TLSv1 client connection data from tlsv1_client_init()
  474. * @buf: Buffer for the cipher name
  475. * @buflen: buf size
  476. * Returns: 0 on success, -1 on failure
  477. *
  478. * Get the name of the currently used cipher.
  479. */
  480. int tlsv1_client_get_cipher(struct tlsv1_client *conn, char *buf,
  481. size_t buflen)
  482. {
  483. char *cipher;
  484. switch (conn->rl.cipher_suite) {
  485. case TLS_RSA_WITH_RC4_128_MD5:
  486. cipher = "RC4-MD5";
  487. break;
  488. case TLS_RSA_WITH_RC4_128_SHA:
  489. cipher = "RC4-SHA";
  490. break;
  491. case TLS_RSA_WITH_DES_CBC_SHA:
  492. cipher = "DES-CBC-SHA";
  493. break;
  494. case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
  495. cipher = "DES-CBC3-SHA";
  496. break;
  497. case TLS_DHE_RSA_WITH_DES_CBC_SHA:
  498. cipher = "DHE-RSA-DES-CBC-SHA";
  499. break;
  500. case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  501. cipher = "DHE-RSA-DES-CBC3-SHA";
  502. break;
  503. case TLS_DH_anon_WITH_RC4_128_MD5:
  504. cipher = "ADH-RC4-MD5";
  505. break;
  506. case TLS_DH_anon_WITH_DES_CBC_SHA:
  507. cipher = "ADH-DES-SHA";
  508. break;
  509. case TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:
  510. cipher = "ADH-DES-CBC3-SHA";
  511. break;
  512. case TLS_RSA_WITH_AES_128_CBC_SHA:
  513. cipher = "AES-128-SHA";
  514. break;
  515. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  516. cipher = "DHE-RSA-AES-128-SHA";
  517. break;
  518. case TLS_DH_anon_WITH_AES_128_CBC_SHA:
  519. cipher = "ADH-AES-128-SHA";
  520. break;
  521. case TLS_RSA_WITH_AES_256_CBC_SHA:
  522. cipher = "AES-256-SHA";
  523. break;
  524. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  525. cipher = "DHE-RSA-AES-256-SHA";
  526. break;
  527. case TLS_DH_anon_WITH_AES_256_CBC_SHA:
  528. cipher = "ADH-AES-256-SHA";
  529. break;
  530. case TLS_RSA_WITH_AES_128_CBC_SHA256:
  531. cipher = "AES-128-SHA256";
  532. break;
  533. case TLS_RSA_WITH_AES_256_CBC_SHA256:
  534. cipher = "AES-256-SHA256";
  535. break;
  536. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  537. cipher = "DHE-RSA-AES-128-SHA256";
  538. break;
  539. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  540. cipher = "DHE-RSA-AES-256-SHA256";
  541. break;
  542. case TLS_DH_anon_WITH_AES_128_CBC_SHA256:
  543. cipher = "ADH-AES-128-SHA256";
  544. break;
  545. case TLS_DH_anon_WITH_AES_256_CBC_SHA256:
  546. cipher = "ADH-AES-256-SHA256";
  547. break;
  548. default:
  549. return -1;
  550. }
  551. if (os_strlcpy(buf, cipher, buflen) >= buflen)
  552. return -1;
  553. return 0;
  554. }
  555. /**
  556. * tlsv1_client_shutdown - Shutdown TLS connection
  557. * @conn: TLSv1 client connection data from tlsv1_client_init()
  558. * Returns: 0 on success, -1 on failure
  559. */
  560. int tlsv1_client_shutdown(struct tlsv1_client *conn)
  561. {
  562. conn->state = CLIENT_HELLO;
  563. if (tls_verify_hash_init(&conn->verify) < 0) {
  564. wpa_printf(MSG_DEBUG, "TLSv1: Failed to re-initialize verify "
  565. "hash");
  566. return -1;
  567. }
  568. tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
  569. tlsv1_record_change_write_cipher(&conn->rl);
  570. tlsv1_record_change_read_cipher(&conn->rl);
  571. conn->certificate_requested = 0;
  572. crypto_public_key_free(conn->server_rsa_key);
  573. conn->server_rsa_key = NULL;
  574. conn->session_resumed = 0;
  575. return 0;
  576. }
  577. /**
  578. * tlsv1_client_resumed - Was session resumption used
  579. * @conn: TLSv1 client connection data from tlsv1_client_init()
  580. * Returns: 1 if current session used session resumption, 0 if not
  581. */
  582. int tlsv1_client_resumed(struct tlsv1_client *conn)
  583. {
  584. return !!conn->session_resumed;
  585. }
  586. /**
  587. * tlsv1_client_hello_ext - Set TLS extension for ClientHello
  588. * @conn: TLSv1 client connection data from tlsv1_client_init()
  589. * @ext_type: Extension type
  590. * @data: Extension payload (%NULL to remove extension)
  591. * @data_len: Extension payload length
  592. * Returns: 0 on success, -1 on failure
  593. */
  594. int tlsv1_client_hello_ext(struct tlsv1_client *conn, int ext_type,
  595. const u8 *data, size_t data_len)
  596. {
  597. u8 *pos;
  598. conn->session_ticket_included = 0;
  599. os_free(conn->client_hello_ext);
  600. conn->client_hello_ext = NULL;
  601. conn->client_hello_ext_len = 0;
  602. if (data == NULL || data_len == 0)
  603. return 0;
  604. pos = conn->client_hello_ext = os_malloc(6 + data_len);
  605. if (pos == NULL)
  606. return -1;
  607. WPA_PUT_BE16(pos, 4 + data_len);
  608. pos += 2;
  609. WPA_PUT_BE16(pos, ext_type);
  610. pos += 2;
  611. WPA_PUT_BE16(pos, data_len);
  612. pos += 2;
  613. os_memcpy(pos, data, data_len);
  614. conn->client_hello_ext_len = 6 + data_len;
  615. if (ext_type == TLS_EXT_PAC_OPAQUE) {
  616. conn->session_ticket_included = 1;
  617. wpa_printf(MSG_DEBUG, "TLSv1: Using session ticket");
  618. }
  619. return 0;
  620. }
  621. /**
  622. * tlsv1_client_get_keys - Get master key and random data from TLS connection
  623. * @conn: TLSv1 client connection data from tlsv1_client_init()
  624. * @keys: Structure of key/random data (filled on success)
  625. * Returns: 0 on success, -1 on failure
  626. */
  627. int tlsv1_client_get_keys(struct tlsv1_client *conn, struct tls_keys *keys)
  628. {
  629. os_memset(keys, 0, sizeof(*keys));
  630. if (conn->state == CLIENT_HELLO)
  631. return -1;
  632. keys->client_random = conn->client_random;
  633. keys->client_random_len = TLS_RANDOM_LEN;
  634. if (conn->state != SERVER_HELLO) {
  635. keys->server_random = conn->server_random;
  636. keys->server_random_len = TLS_RANDOM_LEN;
  637. }
  638. return 0;
  639. }
  640. /**
  641. * tlsv1_client_get_keyblock_size - Get TLS key_block size
  642. * @conn: TLSv1 client connection data from tlsv1_client_init()
  643. * Returns: Size of the key_block for the negotiated cipher suite or -1 on
  644. * failure
  645. */
  646. int tlsv1_client_get_keyblock_size(struct tlsv1_client *conn)
  647. {
  648. if (conn->state == CLIENT_HELLO || conn->state == SERVER_HELLO)
  649. return -1;
  650. return 2 * (conn->rl.hash_size + conn->rl.key_material_len +
  651. conn->rl.iv_size);
  652. }
  653. /**
  654. * tlsv1_client_set_cipher_list - Configure acceptable cipher suites
  655. * @conn: TLSv1 client connection data from tlsv1_client_init()
  656. * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
  657. * (TLS_CIPHER_*).
  658. * Returns: 0 on success, -1 on failure
  659. */
  660. int tlsv1_client_set_cipher_list(struct tlsv1_client *conn, u8 *ciphers)
  661. {
  662. size_t count;
  663. u16 *suites;
  664. /* TODO: implement proper configuration of cipher suites */
  665. if (ciphers[0] == TLS_CIPHER_ANON_DH_AES128_SHA) {
  666. count = 0;
  667. suites = conn->cipher_suites;
  668. suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA256;
  669. suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA;
  670. suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA256;
  671. suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
  672. suites[count++] = TLS_DH_anon_WITH_3DES_EDE_CBC_SHA;
  673. suites[count++] = TLS_DH_anon_WITH_RC4_128_MD5;
  674. suites[count++] = TLS_DH_anon_WITH_DES_CBC_SHA;
  675. /*
  676. * Cisco AP (at least 350 and 1200 series) local authentication
  677. * server does not know how to search cipher suites from the
  678. * list and seem to require that the last entry in the list is
  679. * the one that it wants to use. However, TLS specification
  680. * requires the list to be in the client preference order. As a
  681. * workaround, add anon-DH AES-128-SHA1 again at the end of the
  682. * list to allow the Cisco code to find it.
  683. */
  684. suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
  685. conn->num_cipher_suites = count;
  686. }
  687. return 0;
  688. }
  689. /**
  690. * tlsv1_client_set_cred - Set client credentials
  691. * @conn: TLSv1 client connection data from tlsv1_client_init()
  692. * @cred: Credentials from tlsv1_cred_alloc()
  693. * Returns: 0 on success, -1 on failure
  694. *
  695. * On success, the client takes ownership of the credentials block and caller
  696. * must not free it. On failure, caller is responsible for freeing the
  697. * credential block.
  698. */
  699. int tlsv1_client_set_cred(struct tlsv1_client *conn,
  700. struct tlsv1_credentials *cred)
  701. {
  702. tlsv1_cred_free(conn->cred);
  703. conn->cred = cred;
  704. return 0;
  705. }
  706. void tlsv1_client_set_time_checks(struct tlsv1_client *conn, int enabled)
  707. {
  708. conn->disable_time_checks = !enabled;
  709. }
  710. void tlsv1_client_set_session_ticket_cb(struct tlsv1_client *conn,
  711. tlsv1_client_session_ticket_cb cb,
  712. void *ctx)
  713. {
  714. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback set %p (ctx %p)",
  715. cb, ctx);
  716. conn->session_ticket_cb = cb;
  717. conn->session_ticket_cb_ctx = ctx;
  718. }