tlsv1_client_read.c 28 KB

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