tlsv1_client_read.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /*
  2. * TLS v1.0 (RFC 2246) and v1.1 (RFC 4346) client - read handshake message
  3. * Copyright (c) 2006-2011, 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. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto/md5.h"
  17. #include "crypto/sha1.h"
  18. #include "crypto/tls.h"
  19. #include "x509v3.h"
  20. #include "tlsv1_common.h"
  21. #include "tlsv1_record.h"
  22. #include "tlsv1_client.h"
  23. #include "tlsv1_client_i.h"
  24. static int tls_process_server_key_exchange(struct tlsv1_client *conn, u8 ct,
  25. const u8 *in_data, size_t *in_len);
  26. static int tls_process_certificate_request(struct tlsv1_client *conn, u8 ct,
  27. const u8 *in_data, size_t *in_len);
  28. static int tls_process_server_hello_done(struct tlsv1_client *conn, u8 ct,
  29. const u8 *in_data, size_t *in_len);
  30. static int tls_process_server_hello(struct tlsv1_client *conn, u8 ct,
  31. const u8 *in_data, size_t *in_len)
  32. {
  33. const u8 *pos, *end;
  34. size_t left, len, i;
  35. u16 cipher_suite;
  36. u16 tls_version;
  37. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  38. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  39. "received content type 0x%x", ct);
  40. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  41. TLS_ALERT_UNEXPECTED_MESSAGE);
  42. return -1;
  43. }
  44. pos = in_data;
  45. left = *in_len;
  46. if (left < 4)
  47. goto decode_error;
  48. /* HandshakeType msg_type */
  49. if (*pos != TLS_HANDSHAKE_TYPE_SERVER_HELLO) {
  50. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  51. "message %d (expected ServerHello)", *pos);
  52. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  53. TLS_ALERT_UNEXPECTED_MESSAGE);
  54. return -1;
  55. }
  56. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerHello");
  57. pos++;
  58. /* uint24 length */
  59. len = WPA_GET_BE24(pos);
  60. pos += 3;
  61. left -= 4;
  62. if (len > left)
  63. goto decode_error;
  64. /* body - ServerHello */
  65. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ServerHello", pos, len);
  66. end = pos + len;
  67. /* ProtocolVersion server_version */
  68. if (end - pos < 2)
  69. goto decode_error;
  70. tls_version = WPA_GET_BE16(pos);
  71. if (tls_version != TLS_VERSION_1 &&
  72. (tls_version != TLS_VERSION_1_1 ||
  73. TLS_VERSION == TLS_VERSION_1)) {
  74. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected protocol version in "
  75. "ServerHello %u.%u", pos[0], pos[1]);
  76. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  77. TLS_ALERT_PROTOCOL_VERSION);
  78. return -1;
  79. }
  80. pos += 2;
  81. wpa_printf(MSG_DEBUG, "TLSv1: Using TLS v%s",
  82. tls_version == TLS_VERSION_1_1 ? "1.1" : "1.0");
  83. conn->rl.tls_version = tls_version;
  84. /* Random random */
  85. if (end - pos < TLS_RANDOM_LEN)
  86. goto decode_error;
  87. os_memcpy(conn->server_random, pos, TLS_RANDOM_LEN);
  88. pos += TLS_RANDOM_LEN;
  89. wpa_hexdump(MSG_MSGDUMP, "TLSv1: server_random",
  90. conn->server_random, TLS_RANDOM_LEN);
  91. /* SessionID session_id */
  92. if (end - pos < 1)
  93. goto decode_error;
  94. if (end - pos < 1 + *pos || *pos > TLS_SESSION_ID_MAX_LEN)
  95. goto decode_error;
  96. if (conn->session_id_len && conn->session_id_len == *pos &&
  97. os_memcmp(conn->session_id, pos + 1, conn->session_id_len) == 0) {
  98. pos += 1 + conn->session_id_len;
  99. wpa_printf(MSG_DEBUG, "TLSv1: Resuming old session");
  100. conn->session_resumed = 1;
  101. } else {
  102. conn->session_id_len = *pos;
  103. pos++;
  104. os_memcpy(conn->session_id, pos, conn->session_id_len);
  105. pos += conn->session_id_len;
  106. }
  107. wpa_hexdump(MSG_MSGDUMP, "TLSv1: session_id",
  108. conn->session_id, conn->session_id_len);
  109. /* CipherSuite cipher_suite */
  110. if (end - pos < 2)
  111. goto decode_error;
  112. cipher_suite = WPA_GET_BE16(pos);
  113. pos += 2;
  114. for (i = 0; i < conn->num_cipher_suites; i++) {
  115. if (cipher_suite == conn->cipher_suites[i])
  116. break;
  117. }
  118. if (i == conn->num_cipher_suites) {
  119. wpa_printf(MSG_INFO, "TLSv1: Server selected unexpected "
  120. "cipher suite 0x%04x", cipher_suite);
  121. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  122. TLS_ALERT_ILLEGAL_PARAMETER);
  123. return -1;
  124. }
  125. if (conn->session_resumed && cipher_suite != conn->prev_cipher_suite) {
  126. wpa_printf(MSG_DEBUG, "TLSv1: Server selected a different "
  127. "cipher suite for a resumed connection (0x%04x != "
  128. "0x%04x)", cipher_suite, conn->prev_cipher_suite);
  129. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  130. TLS_ALERT_ILLEGAL_PARAMETER);
  131. return -1;
  132. }
  133. if (tlsv1_record_set_cipher_suite(&conn->rl, cipher_suite) < 0) {
  134. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set CipherSuite for "
  135. "record layer");
  136. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  137. TLS_ALERT_INTERNAL_ERROR);
  138. return -1;
  139. }
  140. conn->prev_cipher_suite = cipher_suite;
  141. /* CompressionMethod compression_method */
  142. if (end - pos < 1)
  143. goto decode_error;
  144. if (*pos != TLS_COMPRESSION_NULL) {
  145. wpa_printf(MSG_INFO, "TLSv1: Server selected unexpected "
  146. "compression 0x%02x", *pos);
  147. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  148. TLS_ALERT_ILLEGAL_PARAMETER);
  149. return -1;
  150. }
  151. pos++;
  152. if (end != pos) {
  153. /* TODO: ServerHello extensions */
  154. wpa_hexdump(MSG_DEBUG, "TLSv1: Unexpected extra data in the "
  155. "end of ServerHello", pos, end - pos);
  156. goto decode_error;
  157. }
  158. if (conn->session_ticket_included && conn->session_ticket_cb) {
  159. /* TODO: include SessionTicket extension if one was included in
  160. * ServerHello */
  161. int res = conn->session_ticket_cb(
  162. conn->session_ticket_cb_ctx, NULL, 0,
  163. conn->client_random, conn->server_random,
  164. conn->master_secret);
  165. if (res < 0) {
  166. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback "
  167. "indicated failure");
  168. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  169. TLS_ALERT_HANDSHAKE_FAILURE);
  170. return -1;
  171. }
  172. conn->use_session_ticket = !!res;
  173. }
  174. if ((conn->session_resumed || conn->use_session_ticket) &&
  175. tls_derive_keys(conn, NULL, 0)) {
  176. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  177. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  178. TLS_ALERT_INTERNAL_ERROR);
  179. return -1;
  180. }
  181. *in_len = end - in_data;
  182. conn->state = (conn->session_resumed || conn->use_session_ticket) ?
  183. SERVER_CHANGE_CIPHER_SPEC : SERVER_CERTIFICATE;
  184. return 0;
  185. decode_error:
  186. wpa_printf(MSG_DEBUG, "TLSv1: Failed to decode ServerHello");
  187. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  188. return -1;
  189. }
  190. static int tls_process_certificate(struct tlsv1_client *conn, u8 ct,
  191. const u8 *in_data, size_t *in_len)
  192. {
  193. const u8 *pos, *end;
  194. size_t left, len, list_len, cert_len, idx;
  195. u8 type;
  196. struct x509_certificate *chain = NULL, *last = NULL, *cert;
  197. int reason;
  198. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  199. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  200. "received content type 0x%x", ct);
  201. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  202. TLS_ALERT_UNEXPECTED_MESSAGE);
  203. return -1;
  204. }
  205. pos = in_data;
  206. left = *in_len;
  207. if (left < 4) {
  208. wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate message "
  209. "(len=%lu)", (unsigned long) left);
  210. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  211. return -1;
  212. }
  213. type = *pos++;
  214. len = WPA_GET_BE24(pos);
  215. pos += 3;
  216. left -= 4;
  217. if (len > left) {
  218. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected Certificate message "
  219. "length (len=%lu != left=%lu)",
  220. (unsigned long) len, (unsigned long) left);
  221. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  222. return -1;
  223. }
  224. if (type == TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE)
  225. return tls_process_server_key_exchange(conn, ct, in_data,
  226. in_len);
  227. if (type == TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST)
  228. return tls_process_certificate_request(conn, ct, in_data,
  229. in_len);
  230. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  231. return tls_process_server_hello_done(conn, ct, in_data,
  232. in_len);
  233. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE) {
  234. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  235. "message %d (expected Certificate/"
  236. "ServerKeyExchange/CertificateRequest/"
  237. "ServerHelloDone)", type);
  238. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  239. TLS_ALERT_UNEXPECTED_MESSAGE);
  240. return -1;
  241. }
  242. wpa_printf(MSG_DEBUG,
  243. "TLSv1: Received Certificate (certificate_list len %lu)",
  244. (unsigned long) len);
  245. /*
  246. * opaque ASN.1Cert<2^24-1>;
  247. *
  248. * struct {
  249. * ASN.1Cert certificate_list<1..2^24-1>;
  250. * } Certificate;
  251. */
  252. end = pos + len;
  253. if (end - pos < 3) {
  254. wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate "
  255. "(left=%lu)", (unsigned long) left);
  256. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  257. return -1;
  258. }
  259. list_len = WPA_GET_BE24(pos);
  260. pos += 3;
  261. if ((size_t) (end - pos) != list_len) {
  262. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate_list "
  263. "length (len=%lu left=%lu)",
  264. (unsigned long) list_len,
  265. (unsigned long) (end - pos));
  266. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  267. return -1;
  268. }
  269. idx = 0;
  270. while (pos < end) {
  271. if (end - pos < 3) {
  272. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  273. "certificate_list");
  274. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  275. TLS_ALERT_DECODE_ERROR);
  276. x509_certificate_chain_free(chain);
  277. return -1;
  278. }
  279. cert_len = WPA_GET_BE24(pos);
  280. pos += 3;
  281. if ((size_t) (end - pos) < cert_len) {
  282. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate "
  283. "length (len=%lu left=%lu)",
  284. (unsigned long) cert_len,
  285. (unsigned long) (end - pos));
  286. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  287. TLS_ALERT_DECODE_ERROR);
  288. x509_certificate_chain_free(chain);
  289. return -1;
  290. }
  291. wpa_printf(MSG_DEBUG, "TLSv1: Certificate %lu (len %lu)",
  292. (unsigned long) idx, (unsigned long) cert_len);
  293. if (idx == 0) {
  294. crypto_public_key_free(conn->server_rsa_key);
  295. if (tls_parse_cert(pos, cert_len,
  296. &conn->server_rsa_key)) {
  297. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  298. "the certificate");
  299. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  300. TLS_ALERT_BAD_CERTIFICATE);
  301. x509_certificate_chain_free(chain);
  302. return -1;
  303. }
  304. }
  305. cert = x509_certificate_parse(pos, cert_len);
  306. if (cert == NULL) {
  307. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  308. "the certificate");
  309. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  310. TLS_ALERT_BAD_CERTIFICATE);
  311. x509_certificate_chain_free(chain);
  312. return -1;
  313. }
  314. if (last == NULL)
  315. chain = cert;
  316. else
  317. last->next = cert;
  318. last = cert;
  319. idx++;
  320. pos += cert_len;
  321. }
  322. if (conn->cred &&
  323. x509_certificate_chain_validate(conn->cred->trusted_certs, chain,
  324. &reason, conn->disable_time_checks)
  325. < 0) {
  326. int tls_reason;
  327. wpa_printf(MSG_DEBUG, "TLSv1: Server certificate chain "
  328. "validation failed (reason=%d)", reason);
  329. switch (reason) {
  330. case X509_VALIDATE_BAD_CERTIFICATE:
  331. tls_reason = TLS_ALERT_BAD_CERTIFICATE;
  332. break;
  333. case X509_VALIDATE_UNSUPPORTED_CERTIFICATE:
  334. tls_reason = TLS_ALERT_UNSUPPORTED_CERTIFICATE;
  335. break;
  336. case X509_VALIDATE_CERTIFICATE_REVOKED:
  337. tls_reason = TLS_ALERT_CERTIFICATE_REVOKED;
  338. break;
  339. case X509_VALIDATE_CERTIFICATE_EXPIRED:
  340. tls_reason = TLS_ALERT_CERTIFICATE_EXPIRED;
  341. break;
  342. case X509_VALIDATE_CERTIFICATE_UNKNOWN:
  343. tls_reason = TLS_ALERT_CERTIFICATE_UNKNOWN;
  344. break;
  345. case X509_VALIDATE_UNKNOWN_CA:
  346. tls_reason = TLS_ALERT_UNKNOWN_CA;
  347. break;
  348. default:
  349. tls_reason = TLS_ALERT_BAD_CERTIFICATE;
  350. break;
  351. }
  352. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, tls_reason);
  353. x509_certificate_chain_free(chain);
  354. return -1;
  355. }
  356. x509_certificate_chain_free(chain);
  357. *in_len = end - in_data;
  358. conn->state = SERVER_KEY_EXCHANGE;
  359. return 0;
  360. }
  361. static int tlsv1_process_diffie_hellman(struct tlsv1_client *conn,
  362. const u8 *buf, size_t len)
  363. {
  364. const u8 *pos, *end;
  365. tlsv1_client_free_dh(conn);
  366. pos = buf;
  367. end = buf + len;
  368. if (end - pos < 3)
  369. goto fail;
  370. conn->dh_p_len = WPA_GET_BE16(pos);
  371. pos += 2;
  372. if (conn->dh_p_len == 0 || end - pos < (int) conn->dh_p_len) {
  373. wpa_printf(MSG_DEBUG, "TLSv1: Invalid dh_p length %lu",
  374. (unsigned long) conn->dh_p_len);
  375. goto fail;
  376. }
  377. conn->dh_p = os_malloc(conn->dh_p_len);
  378. if (conn->dh_p == NULL)
  379. goto fail;
  380. os_memcpy(conn->dh_p, pos, conn->dh_p_len);
  381. pos += conn->dh_p_len;
  382. wpa_hexdump(MSG_DEBUG, "TLSv1: DH p (prime)",
  383. conn->dh_p, conn->dh_p_len);
  384. if (end - pos < 3)
  385. goto fail;
  386. conn->dh_g_len = WPA_GET_BE16(pos);
  387. pos += 2;
  388. if (conn->dh_g_len == 0 || end - pos < (int) conn->dh_g_len)
  389. goto fail;
  390. conn->dh_g = os_malloc(conn->dh_g_len);
  391. if (conn->dh_g == NULL)
  392. goto fail;
  393. os_memcpy(conn->dh_g, pos, conn->dh_g_len);
  394. pos += conn->dh_g_len;
  395. wpa_hexdump(MSG_DEBUG, "TLSv1: DH g (generator)",
  396. conn->dh_g, conn->dh_g_len);
  397. if (conn->dh_g_len == 1 && conn->dh_g[0] < 2)
  398. goto fail;
  399. if (end - pos < 3)
  400. goto fail;
  401. conn->dh_ys_len = WPA_GET_BE16(pos);
  402. pos += 2;
  403. if (conn->dh_ys_len == 0 || end - pos < (int) conn->dh_ys_len)
  404. goto fail;
  405. conn->dh_ys = os_malloc(conn->dh_ys_len);
  406. if (conn->dh_ys == NULL)
  407. goto fail;
  408. os_memcpy(conn->dh_ys, pos, conn->dh_ys_len);
  409. pos += conn->dh_ys_len;
  410. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
  411. conn->dh_ys, conn->dh_ys_len);
  412. return 0;
  413. fail:
  414. wpa_printf(MSG_DEBUG, "TLSv1: Processing DH params failed");
  415. tlsv1_client_free_dh(conn);
  416. return -1;
  417. }
  418. static int tls_process_server_key_exchange(struct tlsv1_client *conn, u8 ct,
  419. const u8 *in_data, size_t *in_len)
  420. {
  421. const u8 *pos, *end;
  422. size_t left, len;
  423. u8 type;
  424. const struct tls_cipher_suite *suite;
  425. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  426. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  427. "received content type 0x%x", ct);
  428. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  429. TLS_ALERT_UNEXPECTED_MESSAGE);
  430. return -1;
  431. }
  432. pos = in_data;
  433. left = *in_len;
  434. if (left < 4) {
  435. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerKeyExchange "
  436. "(Left=%lu)", (unsigned long) left);
  437. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  438. return -1;
  439. }
  440. type = *pos++;
  441. len = WPA_GET_BE24(pos);
  442. pos += 3;
  443. left -= 4;
  444. if (len > left) {
  445. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerKeyExchange "
  446. "length (len=%lu != left=%lu)",
  447. (unsigned long) len, (unsigned long) left);
  448. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  449. return -1;
  450. }
  451. end = pos + len;
  452. if (type == TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST)
  453. return tls_process_certificate_request(conn, ct, in_data,
  454. in_len);
  455. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  456. return tls_process_server_hello_done(conn, ct, in_data,
  457. in_len);
  458. if (type != TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE) {
  459. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  460. "message %d (expected ServerKeyExchange/"
  461. "CertificateRequest/ServerHelloDone)", type);
  462. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  463. TLS_ALERT_UNEXPECTED_MESSAGE);
  464. return -1;
  465. }
  466. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerKeyExchange");
  467. if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
  468. wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not allowed "
  469. "with the selected cipher suite");
  470. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  471. TLS_ALERT_UNEXPECTED_MESSAGE);
  472. return -1;
  473. }
  474. wpa_hexdump(MSG_DEBUG, "TLSv1: ServerKeyExchange", pos, len);
  475. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  476. if (suite && suite->key_exchange == TLS_KEY_X_DH_anon) {
  477. if (tlsv1_process_diffie_hellman(conn, pos, len) < 0) {
  478. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  479. TLS_ALERT_DECODE_ERROR);
  480. return -1;
  481. }
  482. } else {
  483. wpa_printf(MSG_DEBUG, "TLSv1: UnexpectedServerKeyExchange");
  484. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  485. TLS_ALERT_UNEXPECTED_MESSAGE);
  486. return -1;
  487. }
  488. *in_len = end - in_data;
  489. conn->state = SERVER_CERTIFICATE_REQUEST;
  490. return 0;
  491. }
  492. static int tls_process_certificate_request(struct tlsv1_client *conn, u8 ct,
  493. const u8 *in_data, size_t *in_len)
  494. {
  495. const u8 *pos, *end;
  496. size_t left, len;
  497. u8 type;
  498. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  499. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  500. "received content type 0x%x", ct);
  501. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  502. TLS_ALERT_UNEXPECTED_MESSAGE);
  503. return -1;
  504. }
  505. pos = in_data;
  506. left = *in_len;
  507. if (left < 4) {
  508. wpa_printf(MSG_DEBUG, "TLSv1: Too short CertificateRequest "
  509. "(left=%lu)", (unsigned long) left);
  510. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  511. return -1;
  512. }
  513. type = *pos++;
  514. len = WPA_GET_BE24(pos);
  515. pos += 3;
  516. left -= 4;
  517. if (len > left) {
  518. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in CertificateRequest "
  519. "length (len=%lu != left=%lu)",
  520. (unsigned long) len, (unsigned long) left);
  521. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  522. return -1;
  523. }
  524. end = pos + len;
  525. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  526. return tls_process_server_hello_done(conn, ct, in_data,
  527. in_len);
  528. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST) {
  529. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  530. "message %d (expected CertificateRequest/"
  531. "ServerHelloDone)", type);
  532. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  533. TLS_ALERT_UNEXPECTED_MESSAGE);
  534. return -1;
  535. }
  536. wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateRequest");
  537. conn->certificate_requested = 1;
  538. *in_len = end - in_data;
  539. conn->state = SERVER_HELLO_DONE;
  540. return 0;
  541. }
  542. static int tls_process_server_hello_done(struct tlsv1_client *conn, u8 ct,
  543. const u8 *in_data, size_t *in_len)
  544. {
  545. const u8 *pos, *end;
  546. size_t left, len;
  547. u8 type;
  548. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  549. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  550. "received content type 0x%x", ct);
  551. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  552. TLS_ALERT_UNEXPECTED_MESSAGE);
  553. return -1;
  554. }
  555. pos = in_data;
  556. left = *in_len;
  557. if (left < 4) {
  558. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerHelloDone "
  559. "(left=%lu)", (unsigned long) left);
  560. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  561. return -1;
  562. }
  563. type = *pos++;
  564. len = WPA_GET_BE24(pos);
  565. pos += 3;
  566. left -= 4;
  567. if (len > left) {
  568. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerHelloDone "
  569. "length (len=%lu != left=%lu)",
  570. (unsigned long) len, (unsigned long) left);
  571. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  572. return -1;
  573. }
  574. end = pos + len;
  575. if (type != TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE) {
  576. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  577. "message %d (expected ServerHelloDone)", type);
  578. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  579. TLS_ALERT_UNEXPECTED_MESSAGE);
  580. return -1;
  581. }
  582. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerHelloDone");
  583. *in_len = end - in_data;
  584. conn->state = CLIENT_KEY_EXCHANGE;
  585. return 0;
  586. }
  587. static int tls_process_server_change_cipher_spec(struct tlsv1_client *conn,
  588. u8 ct, const u8 *in_data,
  589. size_t *in_len)
  590. {
  591. const u8 *pos;
  592. size_t left;
  593. if (ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
  594. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  595. "received content type 0x%x", ct);
  596. if (conn->use_session_ticket) {
  597. int res;
  598. wpa_printf(MSG_DEBUG, "TLSv1: Server may have "
  599. "rejected SessionTicket");
  600. conn->use_session_ticket = 0;
  601. /* Notify upper layers that SessionTicket failed */
  602. res = conn->session_ticket_cb(
  603. conn->session_ticket_cb_ctx, NULL, 0, NULL,
  604. NULL, NULL);
  605. if (res < 0) {
  606. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket "
  607. "callback indicated failure");
  608. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  609. TLS_ALERT_HANDSHAKE_FAILURE);
  610. return -1;
  611. }
  612. conn->state = SERVER_CERTIFICATE;
  613. return tls_process_certificate(conn, ct, in_data,
  614. in_len);
  615. }
  616. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  617. TLS_ALERT_UNEXPECTED_MESSAGE);
  618. return -1;
  619. }
  620. pos = in_data;
  621. left = *in_len;
  622. if (left < 1) {
  623. wpa_printf(MSG_DEBUG, "TLSv1: Too short ChangeCipherSpec");
  624. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  625. return -1;
  626. }
  627. if (*pos != TLS_CHANGE_CIPHER_SPEC) {
  628. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  629. "received data 0x%x", *pos);
  630. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  631. TLS_ALERT_UNEXPECTED_MESSAGE);
  632. return -1;
  633. }
  634. wpa_printf(MSG_DEBUG, "TLSv1: Received ChangeCipherSpec");
  635. if (tlsv1_record_change_read_cipher(&conn->rl) < 0) {
  636. wpa_printf(MSG_DEBUG, "TLSv1: Failed to change read cipher "
  637. "for record layer");
  638. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  639. TLS_ALERT_INTERNAL_ERROR);
  640. return -1;
  641. }
  642. *in_len = pos + 1 - in_data;
  643. conn->state = SERVER_FINISHED;
  644. return 0;
  645. }
  646. static int tls_process_server_finished(struct tlsv1_client *conn, u8 ct,
  647. const u8 *in_data, size_t *in_len)
  648. {
  649. const u8 *pos, *end;
  650. size_t left, len, hlen;
  651. u8 verify_data[TLS_VERIFY_DATA_LEN];
  652. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  653. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  654. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; "
  655. "received content type 0x%x", ct);
  656. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  657. TLS_ALERT_UNEXPECTED_MESSAGE);
  658. return -1;
  659. }
  660. pos = in_data;
  661. left = *in_len;
  662. if (left < 4) {
  663. wpa_printf(MSG_DEBUG, "TLSv1: Too short record (left=%lu) for "
  664. "Finished",
  665. (unsigned long) left);
  666. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  667. TLS_ALERT_DECODE_ERROR);
  668. return -1;
  669. }
  670. if (pos[0] != TLS_HANDSHAKE_TYPE_FINISHED) {
  671. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; received "
  672. "type 0x%x", pos[0]);
  673. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  674. TLS_ALERT_UNEXPECTED_MESSAGE);
  675. return -1;
  676. }
  677. len = WPA_GET_BE24(pos + 1);
  678. pos += 4;
  679. left -= 4;
  680. if (len > left) {
  681. wpa_printf(MSG_DEBUG, "TLSv1: Too short buffer for Finished "
  682. "(len=%lu > left=%lu)",
  683. (unsigned long) len, (unsigned long) left);
  684. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  685. TLS_ALERT_DECODE_ERROR);
  686. return -1;
  687. }
  688. end = pos + len;
  689. if (len != TLS_VERIFY_DATA_LEN) {
  690. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected verify_data length "
  691. "in Finished: %lu (expected %d)",
  692. (unsigned long) len, TLS_VERIFY_DATA_LEN);
  693. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  694. TLS_ALERT_DECODE_ERROR);
  695. return -1;
  696. }
  697. wpa_hexdump(MSG_MSGDUMP, "TLSv1: verify_data in Finished",
  698. pos, TLS_VERIFY_DATA_LEN);
  699. hlen = MD5_MAC_LEN;
  700. if (conn->verify.md5_server == NULL ||
  701. crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
  702. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  703. TLS_ALERT_INTERNAL_ERROR);
  704. conn->verify.md5_server = NULL;
  705. crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
  706. conn->verify.sha1_server = NULL;
  707. return -1;
  708. }
  709. conn->verify.md5_server = NULL;
  710. hlen = SHA1_MAC_LEN;
  711. if (conn->verify.sha1_server == NULL ||
  712. crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
  713. &hlen) < 0) {
  714. conn->verify.sha1_server = NULL;
  715. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  716. TLS_ALERT_INTERNAL_ERROR);
  717. return -1;
  718. }
  719. conn->verify.sha1_server = NULL;
  720. if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
  721. "server finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
  722. verify_data, TLS_VERIFY_DATA_LEN)) {
  723. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive verify_data");
  724. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  725. TLS_ALERT_DECRYPT_ERROR);
  726. return -1;
  727. }
  728. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
  729. verify_data, TLS_VERIFY_DATA_LEN);
  730. if (os_memcmp(pos, verify_data, TLS_VERIFY_DATA_LEN) != 0) {
  731. wpa_printf(MSG_INFO, "TLSv1: Mismatch in verify_data");
  732. return -1;
  733. }
  734. wpa_printf(MSG_DEBUG, "TLSv1: Received Finished");
  735. *in_len = end - in_data;
  736. conn->state = (conn->session_resumed || conn->use_session_ticket) ?
  737. CHANGE_CIPHER_SPEC : ACK_FINISHED;
  738. return 0;
  739. }
  740. static int tls_process_application_data(struct tlsv1_client *conn, u8 ct,
  741. const u8 *in_data, size_t *in_len,
  742. u8 **out_data, size_t *out_len)
  743. {
  744. const u8 *pos;
  745. size_t left;
  746. if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
  747. wpa_printf(MSG_DEBUG, "TLSv1: Expected Application Data; "
  748. "received content type 0x%x", ct);
  749. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  750. TLS_ALERT_UNEXPECTED_MESSAGE);
  751. return -1;
  752. }
  753. pos = in_data;
  754. left = *in_len;
  755. wpa_hexdump(MSG_DEBUG, "TLSv1: Application Data included in Handshake",
  756. pos, left);
  757. *out_data = os_malloc(left);
  758. if (*out_data) {
  759. os_memcpy(*out_data, pos, left);
  760. *out_len = left;
  761. }
  762. return 0;
  763. }
  764. int tlsv1_client_process_handshake(struct tlsv1_client *conn, u8 ct,
  765. const u8 *buf, size_t *len,
  766. u8 **out_data, size_t *out_len)
  767. {
  768. if (ct == TLS_CONTENT_TYPE_ALERT) {
  769. if (*len < 2) {
  770. wpa_printf(MSG_DEBUG, "TLSv1: Alert underflow");
  771. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  772. TLS_ALERT_DECODE_ERROR);
  773. return -1;
  774. }
  775. wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
  776. buf[0], buf[1]);
  777. *len = 2;
  778. conn->state = FAILED;
  779. return -1;
  780. }
  781. if (ct == TLS_CONTENT_TYPE_HANDSHAKE && *len >= 4 &&
  782. buf[0] == TLS_HANDSHAKE_TYPE_HELLO_REQUEST) {
  783. size_t hr_len = WPA_GET_BE24(buf + 1);
  784. if (hr_len > *len - 4) {
  785. wpa_printf(MSG_DEBUG, "TLSv1: HelloRequest underflow");
  786. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  787. TLS_ALERT_DECODE_ERROR);
  788. return -1;
  789. }
  790. wpa_printf(MSG_DEBUG, "TLSv1: Ignored HelloRequest");
  791. *len = 4 + hr_len;
  792. return 0;
  793. }
  794. switch (conn->state) {
  795. case SERVER_HELLO:
  796. if (tls_process_server_hello(conn, ct, buf, len))
  797. return -1;
  798. break;
  799. case SERVER_CERTIFICATE:
  800. if (tls_process_certificate(conn, ct, buf, len))
  801. return -1;
  802. break;
  803. case SERVER_KEY_EXCHANGE:
  804. if (tls_process_server_key_exchange(conn, ct, buf, len))
  805. return -1;
  806. break;
  807. case SERVER_CERTIFICATE_REQUEST:
  808. if (tls_process_certificate_request(conn, ct, buf, len))
  809. return -1;
  810. break;
  811. case SERVER_HELLO_DONE:
  812. if (tls_process_server_hello_done(conn, ct, buf, len))
  813. return -1;
  814. break;
  815. case SERVER_CHANGE_CIPHER_SPEC:
  816. if (tls_process_server_change_cipher_spec(conn, ct, buf, len))
  817. return -1;
  818. break;
  819. case SERVER_FINISHED:
  820. if (tls_process_server_finished(conn, ct, buf, len))
  821. return -1;
  822. break;
  823. case ACK_FINISHED:
  824. if (out_data &&
  825. tls_process_application_data(conn, ct, buf, len, out_data,
  826. out_len))
  827. return -1;
  828. break;
  829. default:
  830. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d "
  831. "while processing received message",
  832. conn->state);
  833. return -1;
  834. }
  835. if (ct == TLS_CONTENT_TYPE_HANDSHAKE)
  836. tls_verify_hash_add(&conn->verify, buf, *len);
  837. return 0;
  838. }