tlsv1_client_read.c 27 KB

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