tlsv1_client_read.c 29 KB

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