tlsv1_client_read.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. u16 val;
  381. tlsv1_client_free_dh(conn);
  382. pos = buf;
  383. end = buf + len;
  384. if (end - pos < 3)
  385. goto fail;
  386. server_params = pos;
  387. val = WPA_GET_BE16(pos);
  388. pos += 2;
  389. if (val == 0 || val > (size_t) (end - pos)) {
  390. wpa_printf(MSG_DEBUG, "TLSv1: Invalid dh_p length %u", val);
  391. goto fail;
  392. }
  393. conn->dh_p_len = val;
  394. bits = count_bits(pos, conn->dh_p_len);
  395. if (bits < 768) {
  396. wpa_printf(MSG_INFO, "TLSv1: Reject under 768-bit DH prime (insecure; only %u bits)",
  397. bits);
  398. wpa_hexdump(MSG_DEBUG, "TLSv1: Rejected DH prime",
  399. pos, conn->dh_p_len);
  400. goto fail;
  401. }
  402. conn->dh_p = os_malloc(conn->dh_p_len);
  403. if (conn->dh_p == NULL)
  404. goto fail;
  405. os_memcpy(conn->dh_p, pos, conn->dh_p_len);
  406. pos += conn->dh_p_len;
  407. wpa_hexdump(MSG_DEBUG, "TLSv1: DH p (prime)",
  408. conn->dh_p, conn->dh_p_len);
  409. if (end - pos < 3)
  410. goto fail;
  411. val = WPA_GET_BE16(pos);
  412. pos += 2;
  413. if (val == 0 || val > (size_t) (end - pos))
  414. goto fail;
  415. conn->dh_g_len = val;
  416. conn->dh_g = os_malloc(conn->dh_g_len);
  417. if (conn->dh_g == NULL)
  418. goto fail;
  419. os_memcpy(conn->dh_g, pos, conn->dh_g_len);
  420. pos += conn->dh_g_len;
  421. wpa_hexdump(MSG_DEBUG, "TLSv1: DH g (generator)",
  422. conn->dh_g, conn->dh_g_len);
  423. if (conn->dh_g_len == 1 && conn->dh_g[0] < 2)
  424. goto fail;
  425. if (end - pos < 3)
  426. goto fail;
  427. val = WPA_GET_BE16(pos);
  428. pos += 2;
  429. if (val == 0 || val > (size_t) (end - pos))
  430. goto fail;
  431. conn->dh_ys_len = val;
  432. conn->dh_ys = os_malloc(conn->dh_ys_len);
  433. if (conn->dh_ys == NULL)
  434. goto fail;
  435. os_memcpy(conn->dh_ys, pos, conn->dh_ys_len);
  436. pos += conn->dh_ys_len;
  437. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
  438. conn->dh_ys, conn->dh_ys_len);
  439. server_params_end = pos;
  440. if (key_exchange == TLS_KEY_X_DHE_RSA) {
  441. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  442. int hlen;
  443. if (conn->rl.tls_version == TLS_VERSION_1_2) {
  444. #ifdef CONFIG_TLSV12
  445. /*
  446. * RFC 5246, 4.7:
  447. * TLS v1.2 adds explicit indication of the used
  448. * signature and hash algorithms.
  449. *
  450. * struct {
  451. * HashAlgorithm hash;
  452. * SignatureAlgorithm signature;
  453. * } SignatureAndHashAlgorithm;
  454. */
  455. if (end - pos < 2)
  456. goto fail;
  457. if (pos[0] != TLS_HASH_ALG_SHA256 ||
  458. pos[1] != TLS_SIGN_ALG_RSA) {
  459. wpa_printf(MSG_DEBUG, "TLSv1.2: Unsupported hash(%u)/signature(%u) algorithm",
  460. pos[0], pos[1]);
  461. goto fail;
  462. }
  463. pos += 2;
  464. hlen = tlsv12_key_x_server_params_hash(
  465. conn->rl.tls_version, conn->client_random,
  466. conn->server_random, server_params,
  467. server_params_end - server_params, hash);
  468. #else /* CONFIG_TLSV12 */
  469. goto fail;
  470. #endif /* CONFIG_TLSV12 */
  471. } else {
  472. hlen = tls_key_x_server_params_hash(
  473. conn->rl.tls_version, conn->client_random,
  474. conn->server_random, server_params,
  475. server_params_end - server_params, hash);
  476. }
  477. if (hlen < 0)
  478. goto fail;
  479. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ServerKeyExchange hash",
  480. hash, hlen);
  481. if (tls_verify_signature(conn->rl.tls_version,
  482. conn->server_rsa_key,
  483. hash, hlen, pos, end - pos,
  484. &alert) < 0)
  485. goto fail;
  486. }
  487. return 0;
  488. fail:
  489. wpa_printf(MSG_DEBUG, "TLSv1: Processing DH params failed");
  490. tlsv1_client_free_dh(conn);
  491. return -1;
  492. }
  493. static int tls_process_server_key_exchange(struct tlsv1_client *conn, u8 ct,
  494. const u8 *in_data, size_t *in_len)
  495. {
  496. const u8 *pos, *end;
  497. size_t left, len;
  498. u8 type;
  499. const struct tls_cipher_suite *suite;
  500. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  501. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  502. "received content type 0x%x", ct);
  503. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  504. TLS_ALERT_UNEXPECTED_MESSAGE);
  505. return -1;
  506. }
  507. pos = in_data;
  508. left = *in_len;
  509. if (left < 4) {
  510. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerKeyExchange "
  511. "(Left=%lu)", (unsigned long) left);
  512. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  513. return -1;
  514. }
  515. type = *pos++;
  516. len = WPA_GET_BE24(pos);
  517. pos += 3;
  518. left -= 4;
  519. if (len > left) {
  520. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerKeyExchange "
  521. "length (len=%lu != left=%lu)",
  522. (unsigned long) len, (unsigned long) left);
  523. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  524. return -1;
  525. }
  526. end = pos + len;
  527. if (type == TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST)
  528. return tls_process_certificate_request(conn, ct, in_data,
  529. in_len);
  530. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  531. return tls_process_server_hello_done(conn, ct, in_data,
  532. in_len);
  533. if (type != TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE) {
  534. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  535. "message %d (expected ServerKeyExchange/"
  536. "CertificateRequest/ServerHelloDone)", type);
  537. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  538. TLS_ALERT_UNEXPECTED_MESSAGE);
  539. return -1;
  540. }
  541. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerKeyExchange");
  542. if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
  543. wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not allowed "
  544. "with the selected cipher suite");
  545. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  546. TLS_ALERT_UNEXPECTED_MESSAGE);
  547. return -1;
  548. }
  549. wpa_hexdump(MSG_DEBUG, "TLSv1: ServerKeyExchange", pos, len);
  550. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  551. if (suite && (suite->key_exchange == TLS_KEY_X_DH_anon ||
  552. suite->key_exchange == TLS_KEY_X_DHE_RSA)) {
  553. if (tlsv1_process_diffie_hellman(conn, pos, len,
  554. suite->key_exchange) < 0) {
  555. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  556. TLS_ALERT_DECODE_ERROR);
  557. return -1;
  558. }
  559. } else {
  560. wpa_printf(MSG_DEBUG, "TLSv1: UnexpectedServerKeyExchange");
  561. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  562. TLS_ALERT_UNEXPECTED_MESSAGE);
  563. return -1;
  564. }
  565. *in_len = end - in_data;
  566. conn->state = SERVER_CERTIFICATE_REQUEST;
  567. return 0;
  568. }
  569. static int tls_process_certificate_request(struct tlsv1_client *conn, u8 ct,
  570. const u8 *in_data, size_t *in_len)
  571. {
  572. const u8 *pos, *end;
  573. size_t left, len;
  574. u8 type;
  575. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  576. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  577. "received content type 0x%x", ct);
  578. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  579. TLS_ALERT_UNEXPECTED_MESSAGE);
  580. return -1;
  581. }
  582. pos = in_data;
  583. left = *in_len;
  584. if (left < 4) {
  585. wpa_printf(MSG_DEBUG, "TLSv1: Too short CertificateRequest "
  586. "(left=%lu)", (unsigned long) left);
  587. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  588. return -1;
  589. }
  590. type = *pos++;
  591. len = WPA_GET_BE24(pos);
  592. pos += 3;
  593. left -= 4;
  594. if (len > left) {
  595. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in CertificateRequest "
  596. "length (len=%lu != left=%lu)",
  597. (unsigned long) len, (unsigned long) left);
  598. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  599. return -1;
  600. }
  601. end = pos + len;
  602. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  603. return tls_process_server_hello_done(conn, ct, in_data,
  604. in_len);
  605. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST) {
  606. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  607. "message %d (expected CertificateRequest/"
  608. "ServerHelloDone)", type);
  609. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  610. TLS_ALERT_UNEXPECTED_MESSAGE);
  611. return -1;
  612. }
  613. wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateRequest");
  614. conn->certificate_requested = 1;
  615. *in_len = end - in_data;
  616. conn->state = SERVER_HELLO_DONE;
  617. return 0;
  618. }
  619. static int tls_process_server_hello_done(struct tlsv1_client *conn, u8 ct,
  620. const u8 *in_data, size_t *in_len)
  621. {
  622. const u8 *pos, *end;
  623. size_t left, len;
  624. u8 type;
  625. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  626. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  627. "received content type 0x%x", ct);
  628. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  629. TLS_ALERT_UNEXPECTED_MESSAGE);
  630. return -1;
  631. }
  632. pos = in_data;
  633. left = *in_len;
  634. if (left < 4) {
  635. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerHelloDone "
  636. "(left=%lu)", (unsigned long) left);
  637. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  638. return -1;
  639. }
  640. type = *pos++;
  641. len = WPA_GET_BE24(pos);
  642. pos += 3;
  643. left -= 4;
  644. if (len > left) {
  645. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerHelloDone "
  646. "length (len=%lu != left=%lu)",
  647. (unsigned long) len, (unsigned long) left);
  648. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  649. return -1;
  650. }
  651. end = pos + len;
  652. if (type != TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE) {
  653. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  654. "message %d (expected ServerHelloDone)", type);
  655. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  656. TLS_ALERT_UNEXPECTED_MESSAGE);
  657. return -1;
  658. }
  659. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerHelloDone");
  660. *in_len = end - in_data;
  661. conn->state = CLIENT_KEY_EXCHANGE;
  662. return 0;
  663. }
  664. static int tls_process_server_change_cipher_spec(struct tlsv1_client *conn,
  665. u8 ct, const u8 *in_data,
  666. size_t *in_len)
  667. {
  668. const u8 *pos;
  669. size_t left;
  670. if (ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
  671. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  672. "received content type 0x%x", ct);
  673. if (conn->use_session_ticket) {
  674. int res;
  675. wpa_printf(MSG_DEBUG, "TLSv1: Server may have "
  676. "rejected SessionTicket");
  677. conn->use_session_ticket = 0;
  678. /* Notify upper layers that SessionTicket failed */
  679. res = conn->session_ticket_cb(
  680. conn->session_ticket_cb_ctx, NULL, 0, NULL,
  681. NULL, NULL);
  682. if (res < 0) {
  683. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket "
  684. "callback indicated failure");
  685. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  686. TLS_ALERT_HANDSHAKE_FAILURE);
  687. return -1;
  688. }
  689. conn->state = SERVER_CERTIFICATE;
  690. return tls_process_certificate(conn, ct, in_data,
  691. in_len);
  692. }
  693. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  694. TLS_ALERT_UNEXPECTED_MESSAGE);
  695. return -1;
  696. }
  697. pos = in_data;
  698. left = *in_len;
  699. if (left < 1) {
  700. wpa_printf(MSG_DEBUG, "TLSv1: Too short ChangeCipherSpec");
  701. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  702. return -1;
  703. }
  704. if (*pos != TLS_CHANGE_CIPHER_SPEC) {
  705. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  706. "received data 0x%x", *pos);
  707. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  708. TLS_ALERT_UNEXPECTED_MESSAGE);
  709. return -1;
  710. }
  711. wpa_printf(MSG_DEBUG, "TLSv1: Received ChangeCipherSpec");
  712. if (tlsv1_record_change_read_cipher(&conn->rl) < 0) {
  713. wpa_printf(MSG_DEBUG, "TLSv1: Failed to change read cipher "
  714. "for record layer");
  715. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  716. TLS_ALERT_INTERNAL_ERROR);
  717. return -1;
  718. }
  719. *in_len = pos + 1 - in_data;
  720. conn->state = SERVER_FINISHED;
  721. return 0;
  722. }
  723. static int tls_process_server_finished(struct tlsv1_client *conn, u8 ct,
  724. const u8 *in_data, size_t *in_len)
  725. {
  726. const u8 *pos, *end;
  727. size_t left, len, hlen;
  728. u8 verify_data[TLS_VERIFY_DATA_LEN];
  729. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  730. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  731. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; "
  732. "received content type 0x%x", ct);
  733. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  734. TLS_ALERT_UNEXPECTED_MESSAGE);
  735. return -1;
  736. }
  737. pos = in_data;
  738. left = *in_len;
  739. if (left < 4) {
  740. wpa_printf(MSG_DEBUG, "TLSv1: Too short record (left=%lu) for "
  741. "Finished",
  742. (unsigned long) left);
  743. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  744. TLS_ALERT_DECODE_ERROR);
  745. return -1;
  746. }
  747. if (pos[0] != TLS_HANDSHAKE_TYPE_FINISHED) {
  748. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; received "
  749. "type 0x%x", pos[0]);
  750. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  751. TLS_ALERT_UNEXPECTED_MESSAGE);
  752. return -1;
  753. }
  754. len = WPA_GET_BE24(pos + 1);
  755. pos += 4;
  756. left -= 4;
  757. if (len > left) {
  758. wpa_printf(MSG_DEBUG, "TLSv1: Too short buffer for Finished "
  759. "(len=%lu > left=%lu)",
  760. (unsigned long) len, (unsigned long) left);
  761. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  762. TLS_ALERT_DECODE_ERROR);
  763. return -1;
  764. }
  765. end = pos + len;
  766. if (len != TLS_VERIFY_DATA_LEN) {
  767. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected verify_data length "
  768. "in Finished: %lu (expected %d)",
  769. (unsigned long) len, TLS_VERIFY_DATA_LEN);
  770. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  771. TLS_ALERT_DECODE_ERROR);
  772. return -1;
  773. }
  774. wpa_hexdump(MSG_MSGDUMP, "TLSv1: verify_data in Finished",
  775. pos, TLS_VERIFY_DATA_LEN);
  776. #ifdef CONFIG_TLSV12
  777. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  778. hlen = SHA256_MAC_LEN;
  779. if (conn->verify.sha256_server == NULL ||
  780. crypto_hash_finish(conn->verify.sha256_server, hash, &hlen)
  781. < 0) {
  782. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  783. TLS_ALERT_INTERNAL_ERROR);
  784. conn->verify.sha256_server = NULL;
  785. return -1;
  786. }
  787. conn->verify.sha256_server = NULL;
  788. } else {
  789. #endif /* CONFIG_TLSV12 */
  790. hlen = MD5_MAC_LEN;
  791. if (conn->verify.md5_server == NULL ||
  792. crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
  793. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  794. TLS_ALERT_INTERNAL_ERROR);
  795. conn->verify.md5_server = NULL;
  796. crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
  797. conn->verify.sha1_server = NULL;
  798. return -1;
  799. }
  800. conn->verify.md5_server = NULL;
  801. hlen = SHA1_MAC_LEN;
  802. if (conn->verify.sha1_server == NULL ||
  803. crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
  804. &hlen) < 0) {
  805. conn->verify.sha1_server = NULL;
  806. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  807. TLS_ALERT_INTERNAL_ERROR);
  808. return -1;
  809. }
  810. conn->verify.sha1_server = NULL;
  811. hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
  812. #ifdef CONFIG_TLSV12
  813. }
  814. #endif /* CONFIG_TLSV12 */
  815. if (tls_prf(conn->rl.tls_version,
  816. conn->master_secret, TLS_MASTER_SECRET_LEN,
  817. "server finished", hash, hlen,
  818. verify_data, TLS_VERIFY_DATA_LEN)) {
  819. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive verify_data");
  820. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  821. TLS_ALERT_DECRYPT_ERROR);
  822. return -1;
  823. }
  824. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
  825. verify_data, TLS_VERIFY_DATA_LEN);
  826. if (os_memcmp_const(pos, verify_data, TLS_VERIFY_DATA_LEN) != 0) {
  827. wpa_printf(MSG_INFO, "TLSv1: Mismatch in verify_data");
  828. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  829. TLS_ALERT_DECRYPT_ERROR);
  830. return -1;
  831. }
  832. wpa_printf(MSG_DEBUG, "TLSv1: Received Finished");
  833. *in_len = end - in_data;
  834. conn->state = (conn->session_resumed || conn->use_session_ticket) ?
  835. CHANGE_CIPHER_SPEC : ACK_FINISHED;
  836. return 0;
  837. }
  838. static int tls_process_application_data(struct tlsv1_client *conn, u8 ct,
  839. const u8 *in_data, size_t *in_len,
  840. u8 **out_data, size_t *out_len)
  841. {
  842. const u8 *pos;
  843. size_t left;
  844. if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
  845. wpa_printf(MSG_DEBUG, "TLSv1: Expected Application Data; "
  846. "received content type 0x%x", ct);
  847. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  848. TLS_ALERT_UNEXPECTED_MESSAGE);
  849. return -1;
  850. }
  851. pos = in_data;
  852. left = *in_len;
  853. wpa_hexdump(MSG_DEBUG, "TLSv1: Application Data included in Handshake",
  854. pos, left);
  855. *out_data = os_malloc(left);
  856. if (*out_data) {
  857. os_memcpy(*out_data, pos, left);
  858. *out_len = left;
  859. }
  860. return 0;
  861. }
  862. int tlsv1_client_process_handshake(struct tlsv1_client *conn, u8 ct,
  863. const u8 *buf, size_t *len,
  864. u8 **out_data, size_t *out_len)
  865. {
  866. if (ct == TLS_CONTENT_TYPE_ALERT) {
  867. if (*len < 2) {
  868. wpa_printf(MSG_DEBUG, "TLSv1: Alert underflow");
  869. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  870. TLS_ALERT_DECODE_ERROR);
  871. return -1;
  872. }
  873. wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
  874. buf[0], buf[1]);
  875. *len = 2;
  876. conn->state = FAILED;
  877. return -1;
  878. }
  879. if (ct == TLS_CONTENT_TYPE_HANDSHAKE && *len >= 4 &&
  880. buf[0] == TLS_HANDSHAKE_TYPE_HELLO_REQUEST) {
  881. size_t hr_len = WPA_GET_BE24(buf + 1);
  882. if (hr_len > *len - 4) {
  883. wpa_printf(MSG_DEBUG, "TLSv1: HelloRequest underflow");
  884. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  885. TLS_ALERT_DECODE_ERROR);
  886. return -1;
  887. }
  888. wpa_printf(MSG_DEBUG, "TLSv1: Ignored HelloRequest");
  889. *len = 4 + hr_len;
  890. return 0;
  891. }
  892. switch (conn->state) {
  893. case SERVER_HELLO:
  894. if (tls_process_server_hello(conn, ct, buf, len))
  895. return -1;
  896. break;
  897. case SERVER_CERTIFICATE:
  898. if (tls_process_certificate(conn, ct, buf, len))
  899. return -1;
  900. break;
  901. case SERVER_KEY_EXCHANGE:
  902. if (tls_process_server_key_exchange(conn, ct, buf, len))
  903. return -1;
  904. break;
  905. case SERVER_CERTIFICATE_REQUEST:
  906. if (tls_process_certificate_request(conn, ct, buf, len))
  907. return -1;
  908. break;
  909. case SERVER_HELLO_DONE:
  910. if (tls_process_server_hello_done(conn, ct, buf, len))
  911. return -1;
  912. break;
  913. case SERVER_CHANGE_CIPHER_SPEC:
  914. if (tls_process_server_change_cipher_spec(conn, ct, buf, len))
  915. return -1;
  916. break;
  917. case SERVER_FINISHED:
  918. if (tls_process_server_finished(conn, ct, buf, len))
  919. return -1;
  920. break;
  921. case ACK_FINISHED:
  922. if (out_data &&
  923. tls_process_application_data(conn, ct, buf, len, out_data,
  924. out_len))
  925. return -1;
  926. break;
  927. default:
  928. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d "
  929. "while processing received message",
  930. conn->state);
  931. return -1;
  932. }
  933. if (ct == TLS_CONTENT_TYPE_HANDSHAKE)
  934. tls_verify_hash_add(&conn->verify, buf, *len);
  935. return 0;
  936. }