tlsv1_server_read.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /*
  2. * TLSv1 server - read handshake message
  3. * Copyright (c) 2006-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto/md5.h"
  17. #include "crypto/sha1.h"
  18. #include "crypto/sha256.h"
  19. #include "crypto/tls.h"
  20. #include "x509v3.h"
  21. #include "tlsv1_common.h"
  22. #include "tlsv1_record.h"
  23. #include "tlsv1_server.h"
  24. #include "tlsv1_server_i.h"
  25. static int tls_process_client_key_exchange(struct tlsv1_server *conn, u8 ct,
  26. const u8 *in_data, size_t *in_len);
  27. static int tls_process_change_cipher_spec(struct tlsv1_server *conn,
  28. u8 ct, const u8 *in_data,
  29. size_t *in_len);
  30. static int tls_process_client_hello(struct tlsv1_server *conn, u8 ct,
  31. const u8 *in_data, size_t *in_len)
  32. {
  33. const u8 *pos, *end, *c;
  34. size_t left, len, i, j;
  35. u16 cipher_suite;
  36. u16 num_suites;
  37. int compr_null_found;
  38. u16 ext_type, ext_len;
  39. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  40. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  41. "received content type 0x%x", ct);
  42. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  43. TLS_ALERT_UNEXPECTED_MESSAGE);
  44. return -1;
  45. }
  46. pos = in_data;
  47. left = *in_len;
  48. if (left < 4)
  49. goto decode_error;
  50. /* HandshakeType msg_type */
  51. if (*pos != TLS_HANDSHAKE_TYPE_CLIENT_HELLO) {
  52. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  53. "message %d (expected ClientHello)", *pos);
  54. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  55. TLS_ALERT_UNEXPECTED_MESSAGE);
  56. return -1;
  57. }
  58. wpa_printf(MSG_DEBUG, "TLSv1: Received ClientHello");
  59. pos++;
  60. /* uint24 length */
  61. len = WPA_GET_BE24(pos);
  62. pos += 3;
  63. left -= 4;
  64. if (len > left)
  65. goto decode_error;
  66. /* body - ClientHello */
  67. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientHello", pos, len);
  68. end = pos + len;
  69. /* ProtocolVersion client_version */
  70. if (end - pos < 2)
  71. goto decode_error;
  72. conn->client_version = WPA_GET_BE16(pos);
  73. wpa_printf(MSG_DEBUG, "TLSv1: Client version %d.%d",
  74. conn->client_version >> 8, conn->client_version & 0xff);
  75. if (conn->client_version < TLS_VERSION_1) {
  76. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected protocol version in "
  77. "ClientHello %u.%u",
  78. conn->client_version >> 8,
  79. conn->client_version & 0xff);
  80. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  81. TLS_ALERT_PROTOCOL_VERSION);
  82. return -1;
  83. }
  84. pos += 2;
  85. if (TLS_VERSION == TLS_VERSION_1)
  86. conn->rl.tls_version = TLS_VERSION_1;
  87. #ifdef CONFIG_TLSV12
  88. else if (conn->client_version >= TLS_VERSION_1_2)
  89. conn->rl.tls_version = TLS_VERSION_1_2;
  90. #endif /* CONFIG_TLSV12 */
  91. else if (conn->client_version > TLS_VERSION_1_1)
  92. conn->rl.tls_version = TLS_VERSION_1_1;
  93. else
  94. conn->rl.tls_version = conn->client_version;
  95. wpa_printf(MSG_DEBUG, "TLSv1: Using TLS v%s",
  96. tls_version_str(conn->rl.tls_version));
  97. /* Random random */
  98. if (end - pos < TLS_RANDOM_LEN)
  99. goto decode_error;
  100. os_memcpy(conn->client_random, pos, TLS_RANDOM_LEN);
  101. pos += TLS_RANDOM_LEN;
  102. wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
  103. conn->client_random, TLS_RANDOM_LEN);
  104. /* SessionID session_id */
  105. if (end - pos < 1)
  106. goto decode_error;
  107. if (end - pos < 1 + *pos || *pos > TLS_SESSION_ID_MAX_LEN)
  108. goto decode_error;
  109. wpa_hexdump(MSG_MSGDUMP, "TLSv1: client session_id", pos + 1, *pos);
  110. pos += 1 + *pos;
  111. /* TODO: add support for session resumption */
  112. /* CipherSuite cipher_suites<2..2^16-1> */
  113. if (end - pos < 2)
  114. goto decode_error;
  115. num_suites = WPA_GET_BE16(pos);
  116. pos += 2;
  117. if (end - pos < num_suites)
  118. goto decode_error;
  119. wpa_hexdump(MSG_MSGDUMP, "TLSv1: client cipher suites",
  120. pos, num_suites);
  121. if (num_suites & 1)
  122. goto decode_error;
  123. num_suites /= 2;
  124. cipher_suite = 0;
  125. for (i = 0; !cipher_suite && i < conn->num_cipher_suites; i++) {
  126. c = pos;
  127. for (j = 0; j < num_suites; j++) {
  128. u16 tmp = WPA_GET_BE16(c);
  129. c += 2;
  130. if (!cipher_suite && tmp == conn->cipher_suites[i]) {
  131. cipher_suite = tmp;
  132. break;
  133. }
  134. }
  135. }
  136. pos += num_suites * 2;
  137. if (!cipher_suite) {
  138. wpa_printf(MSG_INFO, "TLSv1: No supported cipher suite "
  139. "available");
  140. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  141. TLS_ALERT_ILLEGAL_PARAMETER);
  142. return -1;
  143. }
  144. if (tlsv1_record_set_cipher_suite(&conn->rl, cipher_suite) < 0) {
  145. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set CipherSuite for "
  146. "record layer");
  147. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  148. TLS_ALERT_INTERNAL_ERROR);
  149. return -1;
  150. }
  151. conn->cipher_suite = cipher_suite;
  152. /* CompressionMethod compression_methods<1..2^8-1> */
  153. if (end - pos < 1)
  154. goto decode_error;
  155. num_suites = *pos++;
  156. if (end - pos < num_suites)
  157. goto decode_error;
  158. wpa_hexdump(MSG_MSGDUMP, "TLSv1: client compression_methods",
  159. pos, num_suites);
  160. compr_null_found = 0;
  161. for (i = 0; i < num_suites; i++) {
  162. if (*pos++ == TLS_COMPRESSION_NULL)
  163. compr_null_found = 1;
  164. }
  165. if (!compr_null_found) {
  166. wpa_printf(MSG_INFO, "TLSv1: Client does not accept NULL "
  167. "compression");
  168. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  169. TLS_ALERT_ILLEGAL_PARAMETER);
  170. return -1;
  171. }
  172. if (end - pos == 1) {
  173. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected extra octet in the "
  174. "end of ClientHello: 0x%02x", *pos);
  175. goto decode_error;
  176. }
  177. if (end - pos >= 2) {
  178. /* Extension client_hello_extension_list<0..2^16-1> */
  179. ext_len = WPA_GET_BE16(pos);
  180. pos += 2;
  181. wpa_printf(MSG_DEBUG, "TLSv1: %u bytes of ClientHello "
  182. "extensions", ext_len);
  183. if (end - pos != ext_len) {
  184. wpa_printf(MSG_DEBUG, "TLSv1: Invalid ClientHello "
  185. "extension list length %u (expected %u)",
  186. ext_len, (unsigned int) (end - pos));
  187. goto decode_error;
  188. }
  189. /*
  190. * struct {
  191. * ExtensionType extension_type (0..65535)
  192. * opaque extension_data<0..2^16-1>
  193. * } Extension;
  194. */
  195. while (pos < end) {
  196. if (end - pos < 2) {
  197. wpa_printf(MSG_DEBUG, "TLSv1: Invalid "
  198. "extension_type field");
  199. goto decode_error;
  200. }
  201. ext_type = WPA_GET_BE16(pos);
  202. pos += 2;
  203. if (end - pos < 2) {
  204. wpa_printf(MSG_DEBUG, "TLSv1: Invalid "
  205. "extension_data length field");
  206. goto decode_error;
  207. }
  208. ext_len = WPA_GET_BE16(pos);
  209. pos += 2;
  210. if (end - pos < ext_len) {
  211. wpa_printf(MSG_DEBUG, "TLSv1: Invalid "
  212. "extension_data field");
  213. goto decode_error;
  214. }
  215. wpa_printf(MSG_DEBUG, "TLSv1: ClientHello Extension "
  216. "type %u", ext_type);
  217. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientHello "
  218. "Extension data", pos, ext_len);
  219. if (ext_type == TLS_EXT_SESSION_TICKET) {
  220. os_free(conn->session_ticket);
  221. conn->session_ticket = os_malloc(ext_len);
  222. if (conn->session_ticket) {
  223. os_memcpy(conn->session_ticket, pos,
  224. ext_len);
  225. conn->session_ticket_len = ext_len;
  226. }
  227. }
  228. pos += ext_len;
  229. }
  230. }
  231. *in_len = end - in_data;
  232. wpa_printf(MSG_DEBUG, "TLSv1: ClientHello OK - proceed to "
  233. "ServerHello");
  234. conn->state = SERVER_HELLO;
  235. return 0;
  236. decode_error:
  237. wpa_printf(MSG_DEBUG, "TLSv1: Failed to decode ClientHello");
  238. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  239. TLS_ALERT_DECODE_ERROR);
  240. return -1;
  241. }
  242. static int tls_process_certificate(struct tlsv1_server *conn, u8 ct,
  243. const u8 *in_data, size_t *in_len)
  244. {
  245. const u8 *pos, *end;
  246. size_t left, len, list_len, cert_len, idx;
  247. u8 type;
  248. struct x509_certificate *chain = NULL, *last = NULL, *cert;
  249. int reason;
  250. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  251. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  252. "received content type 0x%x", ct);
  253. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  254. TLS_ALERT_UNEXPECTED_MESSAGE);
  255. return -1;
  256. }
  257. pos = in_data;
  258. left = *in_len;
  259. if (left < 4) {
  260. wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate message "
  261. "(len=%lu)", (unsigned long) left);
  262. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  263. TLS_ALERT_DECODE_ERROR);
  264. return -1;
  265. }
  266. type = *pos++;
  267. len = WPA_GET_BE24(pos);
  268. pos += 3;
  269. left -= 4;
  270. if (len > left) {
  271. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected Certificate message "
  272. "length (len=%lu != left=%lu)",
  273. (unsigned long) len, (unsigned long) left);
  274. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  275. TLS_ALERT_DECODE_ERROR);
  276. return -1;
  277. }
  278. if (type == TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE) {
  279. if (conn->verify_peer) {
  280. wpa_printf(MSG_DEBUG, "TLSv1: Client did not include "
  281. "Certificate");
  282. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  283. TLS_ALERT_UNEXPECTED_MESSAGE);
  284. return -1;
  285. }
  286. return tls_process_client_key_exchange(conn, ct, in_data,
  287. in_len);
  288. }
  289. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE) {
  290. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  291. "message %d (expected Certificate/"
  292. "ClientKeyExchange)", type);
  293. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  294. TLS_ALERT_UNEXPECTED_MESSAGE);
  295. return -1;
  296. }
  297. wpa_printf(MSG_DEBUG,
  298. "TLSv1: Received Certificate (certificate_list len %lu)",
  299. (unsigned long) len);
  300. /*
  301. * opaque ASN.1Cert<2^24-1>;
  302. *
  303. * struct {
  304. * ASN.1Cert certificate_list<1..2^24-1>;
  305. * } Certificate;
  306. */
  307. end = pos + len;
  308. if (end - pos < 3) {
  309. wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate "
  310. "(left=%lu)", (unsigned long) left);
  311. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  312. TLS_ALERT_DECODE_ERROR);
  313. return -1;
  314. }
  315. list_len = WPA_GET_BE24(pos);
  316. pos += 3;
  317. if ((size_t) (end - pos) != list_len) {
  318. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate_list "
  319. "length (len=%lu left=%lu)",
  320. (unsigned long) list_len,
  321. (unsigned long) (end - pos));
  322. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  323. TLS_ALERT_DECODE_ERROR);
  324. return -1;
  325. }
  326. idx = 0;
  327. while (pos < end) {
  328. if (end - pos < 3) {
  329. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  330. "certificate_list");
  331. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  332. TLS_ALERT_DECODE_ERROR);
  333. x509_certificate_chain_free(chain);
  334. return -1;
  335. }
  336. cert_len = WPA_GET_BE24(pos);
  337. pos += 3;
  338. if ((size_t) (end - pos) < cert_len) {
  339. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate "
  340. "length (len=%lu left=%lu)",
  341. (unsigned long) cert_len,
  342. (unsigned long) (end - pos));
  343. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  344. TLS_ALERT_DECODE_ERROR);
  345. x509_certificate_chain_free(chain);
  346. return -1;
  347. }
  348. wpa_printf(MSG_DEBUG, "TLSv1: Certificate %lu (len %lu)",
  349. (unsigned long) idx, (unsigned long) cert_len);
  350. if (idx == 0) {
  351. crypto_public_key_free(conn->client_rsa_key);
  352. if (tls_parse_cert(pos, cert_len,
  353. &conn->client_rsa_key)) {
  354. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  355. "the certificate");
  356. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  357. TLS_ALERT_BAD_CERTIFICATE);
  358. x509_certificate_chain_free(chain);
  359. return -1;
  360. }
  361. }
  362. cert = x509_certificate_parse(pos, cert_len);
  363. if (cert == NULL) {
  364. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  365. "the certificate");
  366. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  367. TLS_ALERT_BAD_CERTIFICATE);
  368. x509_certificate_chain_free(chain);
  369. return -1;
  370. }
  371. if (last == NULL)
  372. chain = cert;
  373. else
  374. last->next = cert;
  375. last = cert;
  376. idx++;
  377. pos += cert_len;
  378. }
  379. if (x509_certificate_chain_validate(conn->cred->trusted_certs, chain,
  380. &reason, 0) < 0) {
  381. int tls_reason;
  382. wpa_printf(MSG_DEBUG, "TLSv1: Server certificate chain "
  383. "validation failed (reason=%d)", reason);
  384. switch (reason) {
  385. case X509_VALIDATE_BAD_CERTIFICATE:
  386. tls_reason = TLS_ALERT_BAD_CERTIFICATE;
  387. break;
  388. case X509_VALIDATE_UNSUPPORTED_CERTIFICATE:
  389. tls_reason = TLS_ALERT_UNSUPPORTED_CERTIFICATE;
  390. break;
  391. case X509_VALIDATE_CERTIFICATE_REVOKED:
  392. tls_reason = TLS_ALERT_CERTIFICATE_REVOKED;
  393. break;
  394. case X509_VALIDATE_CERTIFICATE_EXPIRED:
  395. tls_reason = TLS_ALERT_CERTIFICATE_EXPIRED;
  396. break;
  397. case X509_VALIDATE_CERTIFICATE_UNKNOWN:
  398. tls_reason = TLS_ALERT_CERTIFICATE_UNKNOWN;
  399. break;
  400. case X509_VALIDATE_UNKNOWN_CA:
  401. tls_reason = TLS_ALERT_UNKNOWN_CA;
  402. break;
  403. default:
  404. tls_reason = TLS_ALERT_BAD_CERTIFICATE;
  405. break;
  406. }
  407. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL, tls_reason);
  408. x509_certificate_chain_free(chain);
  409. return -1;
  410. }
  411. x509_certificate_chain_free(chain);
  412. *in_len = end - in_data;
  413. conn->state = CLIENT_KEY_EXCHANGE;
  414. return 0;
  415. }
  416. static int tls_process_client_key_exchange_rsa(
  417. struct tlsv1_server *conn, const u8 *pos, const u8 *end)
  418. {
  419. u8 *out;
  420. size_t outlen, outbuflen;
  421. u16 encr_len;
  422. int res;
  423. int use_random = 0;
  424. if (end - pos < 2) {
  425. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  426. TLS_ALERT_DECODE_ERROR);
  427. return -1;
  428. }
  429. encr_len = WPA_GET_BE16(pos);
  430. pos += 2;
  431. if (pos + encr_len > end) {
  432. wpa_printf(MSG_DEBUG, "TLSv1: Invalid ClientKeyExchange "
  433. "format: encr_len=%u left=%u",
  434. encr_len, (unsigned int) (end - pos));
  435. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  436. TLS_ALERT_DECODE_ERROR);
  437. return -1;
  438. }
  439. outbuflen = outlen = end - pos;
  440. out = os_malloc(outlen >= TLS_PRE_MASTER_SECRET_LEN ?
  441. outlen : TLS_PRE_MASTER_SECRET_LEN);
  442. if (out == NULL) {
  443. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  444. TLS_ALERT_INTERNAL_ERROR);
  445. return -1;
  446. }
  447. /*
  448. * struct {
  449. * ProtocolVersion client_version;
  450. * opaque random[46];
  451. * } PreMasterSecret;
  452. *
  453. * struct {
  454. * public-key-encrypted PreMasterSecret pre_master_secret;
  455. * } EncryptedPreMasterSecret;
  456. */
  457. /*
  458. * Note: To avoid Bleichenbacher attack, we do not report decryption or
  459. * parsing errors from EncryptedPreMasterSecret processing to the
  460. * client. Instead, a random pre-master secret is used to force the
  461. * handshake to fail.
  462. */
  463. if (crypto_private_key_decrypt_pkcs1_v15(conn->cred->key,
  464. pos, encr_len,
  465. out, &outlen) < 0) {
  466. wpa_printf(MSG_DEBUG, "TLSv1: Failed to decrypt "
  467. "PreMasterSecret (encr_len=%u outlen=%lu)",
  468. encr_len, (unsigned long) outlen);
  469. use_random = 1;
  470. }
  471. if (!use_random && outlen != TLS_PRE_MASTER_SECRET_LEN) {
  472. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected PreMasterSecret "
  473. "length %lu", (unsigned long) outlen);
  474. use_random = 1;
  475. }
  476. if (!use_random && WPA_GET_BE16(out) != conn->client_version) {
  477. wpa_printf(MSG_DEBUG, "TLSv1: Client version in "
  478. "ClientKeyExchange does not match with version in "
  479. "ClientHello");
  480. use_random = 1;
  481. }
  482. if (use_random) {
  483. wpa_printf(MSG_DEBUG, "TLSv1: Using random premaster secret "
  484. "to avoid revealing information about private key");
  485. outlen = TLS_PRE_MASTER_SECRET_LEN;
  486. if (os_get_random(out, outlen)) {
  487. wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
  488. "data");
  489. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  490. TLS_ALERT_INTERNAL_ERROR);
  491. os_free(out);
  492. return -1;
  493. }
  494. }
  495. res = tlsv1_server_derive_keys(conn, out, outlen);
  496. /* Clear the pre-master secret since it is not needed anymore */
  497. os_memset(out, 0, outbuflen);
  498. os_free(out);
  499. if (res) {
  500. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  501. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  502. TLS_ALERT_INTERNAL_ERROR);
  503. return -1;
  504. }
  505. return 0;
  506. }
  507. static int tls_process_client_key_exchange_dh_anon(
  508. struct tlsv1_server *conn, const u8 *pos, const u8 *end)
  509. {
  510. const u8 *dh_yc;
  511. u16 dh_yc_len;
  512. u8 *shared;
  513. size_t shared_len;
  514. int res;
  515. /*
  516. * struct {
  517. * select (PublicValueEncoding) {
  518. * case implicit: struct { };
  519. * case explicit: opaque dh_Yc<1..2^16-1>;
  520. * } dh_public;
  521. * } ClientDiffieHellmanPublic;
  522. */
  523. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ClientDiffieHellmanPublic",
  524. pos, end - pos);
  525. if (end == pos) {
  526. wpa_printf(MSG_DEBUG, "TLSv1: Implicit public value encoding "
  527. "not supported");
  528. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  529. TLS_ALERT_INTERNAL_ERROR);
  530. return -1;
  531. }
  532. if (end - pos < 3) {
  533. wpa_printf(MSG_DEBUG, "TLSv1: Invalid client public value "
  534. "length");
  535. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  536. TLS_ALERT_DECODE_ERROR);
  537. return -1;
  538. }
  539. dh_yc_len = WPA_GET_BE16(pos);
  540. dh_yc = pos + 2;
  541. if (dh_yc + dh_yc_len > end) {
  542. wpa_printf(MSG_DEBUG, "TLSv1: Client public value overflow "
  543. "(length %d)", dh_yc_len);
  544. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  545. TLS_ALERT_DECODE_ERROR);
  546. return -1;
  547. }
  548. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
  549. dh_yc, dh_yc_len);
  550. if (conn->cred == NULL || conn->cred->dh_p == NULL ||
  551. conn->dh_secret == NULL) {
  552. wpa_printf(MSG_DEBUG, "TLSv1: No DH parameters available");
  553. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  554. TLS_ALERT_INTERNAL_ERROR);
  555. return -1;
  556. }
  557. shared_len = conn->cred->dh_p_len;
  558. shared = os_malloc(shared_len);
  559. if (shared == NULL) {
  560. wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
  561. "DH");
  562. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  563. TLS_ALERT_INTERNAL_ERROR);
  564. return -1;
  565. }
  566. /* shared = Yc^secret mod p */
  567. if (crypto_mod_exp(dh_yc, dh_yc_len, conn->dh_secret,
  568. conn->dh_secret_len,
  569. conn->cred->dh_p, conn->cred->dh_p_len,
  570. shared, &shared_len)) {
  571. os_free(shared);
  572. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  573. TLS_ALERT_INTERNAL_ERROR);
  574. return -1;
  575. }
  576. wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
  577. shared, shared_len);
  578. os_memset(conn->dh_secret, 0, conn->dh_secret_len);
  579. os_free(conn->dh_secret);
  580. conn->dh_secret = NULL;
  581. res = tlsv1_server_derive_keys(conn, shared, shared_len);
  582. /* Clear the pre-master secret since it is not needed anymore */
  583. os_memset(shared, 0, shared_len);
  584. os_free(shared);
  585. if (res) {
  586. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  587. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  588. TLS_ALERT_INTERNAL_ERROR);
  589. return -1;
  590. }
  591. return 0;
  592. }
  593. static int tls_process_client_key_exchange(struct tlsv1_server *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. tls_key_exchange keyx;
  600. const struct tls_cipher_suite *suite;
  601. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  602. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  603. "received content type 0x%x", ct);
  604. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  605. TLS_ALERT_UNEXPECTED_MESSAGE);
  606. return -1;
  607. }
  608. pos = in_data;
  609. left = *in_len;
  610. if (left < 4) {
  611. wpa_printf(MSG_DEBUG, "TLSv1: Too short ClientKeyExchange "
  612. "(Left=%lu)", (unsigned long) left);
  613. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  614. TLS_ALERT_DECODE_ERROR);
  615. return -1;
  616. }
  617. type = *pos++;
  618. len = WPA_GET_BE24(pos);
  619. pos += 3;
  620. left -= 4;
  621. if (len > left) {
  622. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ClientKeyExchange "
  623. "length (len=%lu != left=%lu)",
  624. (unsigned long) len, (unsigned long) left);
  625. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  626. TLS_ALERT_DECODE_ERROR);
  627. return -1;
  628. }
  629. end = pos + len;
  630. if (type != TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE) {
  631. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  632. "message %d (expected ClientKeyExchange)", type);
  633. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  634. TLS_ALERT_UNEXPECTED_MESSAGE);
  635. return -1;
  636. }
  637. wpa_printf(MSG_DEBUG, "TLSv1: Received ClientKeyExchange");
  638. wpa_hexdump(MSG_DEBUG, "TLSv1: ClientKeyExchange", pos, len);
  639. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  640. if (suite == NULL)
  641. keyx = TLS_KEY_X_NULL;
  642. else
  643. keyx = suite->key_exchange;
  644. if (keyx == TLS_KEY_X_DH_anon &&
  645. tls_process_client_key_exchange_dh_anon(conn, pos, end) < 0)
  646. return -1;
  647. if (keyx != TLS_KEY_X_DH_anon &&
  648. tls_process_client_key_exchange_rsa(conn, pos, end) < 0)
  649. return -1;
  650. *in_len = end - in_data;
  651. conn->state = CERTIFICATE_VERIFY;
  652. return 0;
  653. }
  654. static int tls_process_certificate_verify(struct tlsv1_server *conn, u8 ct,
  655. const u8 *in_data, size_t *in_len)
  656. {
  657. const u8 *pos, *end;
  658. size_t left, len;
  659. u8 type;
  660. size_t hlen, buflen;
  661. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos, *buf;
  662. enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
  663. u16 slen;
  664. if (ct == TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
  665. if (conn->verify_peer) {
  666. wpa_printf(MSG_DEBUG, "TLSv1: Client did not include "
  667. "CertificateVerify");
  668. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  669. TLS_ALERT_UNEXPECTED_MESSAGE);
  670. return -1;
  671. }
  672. return tls_process_change_cipher_spec(conn, ct, in_data,
  673. in_len);
  674. }
  675. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  676. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  677. "received content type 0x%x", ct);
  678. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  679. TLS_ALERT_UNEXPECTED_MESSAGE);
  680. return -1;
  681. }
  682. pos = in_data;
  683. left = *in_len;
  684. if (left < 4) {
  685. wpa_printf(MSG_DEBUG, "TLSv1: Too short CertificateVerify "
  686. "message (len=%lu)", (unsigned long) left);
  687. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  688. TLS_ALERT_DECODE_ERROR);
  689. return -1;
  690. }
  691. type = *pos++;
  692. len = WPA_GET_BE24(pos);
  693. pos += 3;
  694. left -= 4;
  695. if (len > left) {
  696. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected CertificateVerify "
  697. "message length (len=%lu != left=%lu)",
  698. (unsigned long) len, (unsigned long) left);
  699. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  700. TLS_ALERT_DECODE_ERROR);
  701. return -1;
  702. }
  703. end = pos + len;
  704. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY) {
  705. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  706. "message %d (expected CertificateVerify)", type);
  707. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  708. TLS_ALERT_UNEXPECTED_MESSAGE);
  709. return -1;
  710. }
  711. wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateVerify");
  712. /*
  713. * struct {
  714. * Signature signature;
  715. * } CertificateVerify;
  716. */
  717. hpos = hash;
  718. #ifdef CONFIG_TLSV12
  719. if (conn->rl.tls_version == TLS_VERSION_1_2) {
  720. /*
  721. * RFC 5246, 4.7:
  722. * TLS v1.2 adds explicit indication of the used signature and
  723. * hash algorithms.
  724. *
  725. * struct {
  726. * HashAlgorithm hash;
  727. * SignatureAlgorithm signature;
  728. * } SignatureAndHashAlgorithm;
  729. */
  730. if (end - pos < 2) {
  731. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  732. TLS_ALERT_DECODE_ERROR);
  733. return -1;
  734. }
  735. if (pos[0] != TLS_HASH_ALG_SHA256 ||
  736. pos[1] != TLS_SIGN_ALG_RSA) {
  737. wpa_printf(MSG_DEBUG, "TLSv1.2: Unsupported hash(%u)/"
  738. "signature(%u) algorithm",
  739. pos[0], pos[1]);
  740. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  741. TLS_ALERT_INTERNAL_ERROR);
  742. return -1;
  743. }
  744. pos += 2;
  745. hlen = SHA256_MAC_LEN;
  746. if (conn->verify.sha256_cert == NULL ||
  747. crypto_hash_finish(conn->verify.sha256_cert, hpos, &hlen) <
  748. 0) {
  749. conn->verify.sha256_cert = NULL;
  750. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  751. TLS_ALERT_INTERNAL_ERROR);
  752. return -1;
  753. }
  754. conn->verify.sha256_cert = NULL;
  755. } else {
  756. #endif /* CONFIG_TLSV12 */
  757. if (alg == SIGN_ALG_RSA) {
  758. hlen = MD5_MAC_LEN;
  759. if (conn->verify.md5_cert == NULL ||
  760. crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
  761. {
  762. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  763. TLS_ALERT_INTERNAL_ERROR);
  764. conn->verify.md5_cert = NULL;
  765. crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
  766. conn->verify.sha1_cert = NULL;
  767. return -1;
  768. }
  769. hpos += MD5_MAC_LEN;
  770. } else
  771. crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
  772. conn->verify.md5_cert = NULL;
  773. hlen = SHA1_MAC_LEN;
  774. if (conn->verify.sha1_cert == NULL ||
  775. crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
  776. conn->verify.sha1_cert = NULL;
  777. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  778. TLS_ALERT_INTERNAL_ERROR);
  779. return -1;
  780. }
  781. conn->verify.sha1_cert = NULL;
  782. if (alg == SIGN_ALG_RSA)
  783. hlen += MD5_MAC_LEN;
  784. #ifdef CONFIG_TLSV12
  785. }
  786. #endif /* CONFIG_TLSV12 */
  787. wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
  788. if (end - pos < 2) {
  789. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  790. TLS_ALERT_DECODE_ERROR);
  791. return -1;
  792. }
  793. slen = WPA_GET_BE16(pos);
  794. pos += 2;
  795. if (end - pos < slen) {
  796. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  797. TLS_ALERT_DECODE_ERROR);
  798. return -1;
  799. }
  800. wpa_hexdump(MSG_MSGDUMP, "TLSv1: Signature", pos, end - pos);
  801. if (conn->client_rsa_key == NULL) {
  802. wpa_printf(MSG_DEBUG, "TLSv1: No client public key to verify "
  803. "signature");
  804. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  805. TLS_ALERT_INTERNAL_ERROR);
  806. return -1;
  807. }
  808. buflen = end - pos;
  809. buf = os_malloc(end - pos);
  810. if (crypto_public_key_decrypt_pkcs1(conn->client_rsa_key,
  811. pos, end - pos, buf, &buflen) < 0)
  812. {
  813. wpa_printf(MSG_DEBUG, "TLSv1: Failed to decrypt signature");
  814. os_free(buf);
  815. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  816. TLS_ALERT_DECRYPT_ERROR);
  817. return -1;
  818. }
  819. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Decrypted Signature",
  820. buf, buflen);
  821. #ifdef CONFIG_TLSV12
  822. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  823. /*
  824. * RFC 3447, A.2.4 RSASSA-PKCS1-v1_5
  825. *
  826. * DigestInfo ::= SEQUENCE {
  827. * digestAlgorithm DigestAlgorithm,
  828. * digest OCTET STRING
  829. * }
  830. *
  831. * SHA-256 OID: sha256WithRSAEncryption ::= {pkcs-1 11}
  832. *
  833. * DER encoded DigestInfo for SHA256 per RFC 3447:
  834. * 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 ||
  835. * H
  836. */
  837. if (buflen >= 19 + 32 &&
  838. os_memcmp(buf, "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01"
  839. "\x65\x03\x04\x02\x01\x05\x00\x04\x20", 19) == 0)
  840. {
  841. wpa_printf(MSG_DEBUG, "TLSv1.2: DigestAlgorithn = "
  842. "SHA-256");
  843. os_memmove(buf, buf + 19, buflen - 19);
  844. buflen -= 19;
  845. } else {
  846. wpa_printf(MSG_DEBUG, "TLSv1.2: Unrecognized "
  847. "DigestInfo");
  848. os_free(buf);
  849. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  850. TLS_ALERT_DECRYPT_ERROR);
  851. return -1;
  852. }
  853. }
  854. #endif /* CONFIG_TLSV12 */
  855. if (buflen != hlen || os_memcmp(buf, hash, buflen) != 0) {
  856. wpa_printf(MSG_DEBUG, "TLSv1: Invalid Signature in "
  857. "CertificateVerify - did not match with calculated "
  858. "hash");
  859. os_free(buf);
  860. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  861. TLS_ALERT_DECRYPT_ERROR);
  862. return -1;
  863. }
  864. os_free(buf);
  865. *in_len = end - in_data;
  866. conn->state = CHANGE_CIPHER_SPEC;
  867. return 0;
  868. }
  869. static int tls_process_change_cipher_spec(struct tlsv1_server *conn,
  870. u8 ct, const u8 *in_data,
  871. size_t *in_len)
  872. {
  873. const u8 *pos;
  874. size_t left;
  875. if (ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
  876. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  877. "received content type 0x%x", ct);
  878. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  879. TLS_ALERT_UNEXPECTED_MESSAGE);
  880. return -1;
  881. }
  882. pos = in_data;
  883. left = *in_len;
  884. if (left < 1) {
  885. wpa_printf(MSG_DEBUG, "TLSv1: Too short ChangeCipherSpec");
  886. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  887. TLS_ALERT_DECODE_ERROR);
  888. return -1;
  889. }
  890. if (*pos != TLS_CHANGE_CIPHER_SPEC) {
  891. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  892. "received data 0x%x", *pos);
  893. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  894. TLS_ALERT_UNEXPECTED_MESSAGE);
  895. return -1;
  896. }
  897. wpa_printf(MSG_DEBUG, "TLSv1: Received ChangeCipherSpec");
  898. if (tlsv1_record_change_read_cipher(&conn->rl) < 0) {
  899. wpa_printf(MSG_DEBUG, "TLSv1: Failed to change read cipher "
  900. "for record layer");
  901. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  902. TLS_ALERT_INTERNAL_ERROR);
  903. return -1;
  904. }
  905. *in_len = pos + 1 - in_data;
  906. conn->state = CLIENT_FINISHED;
  907. return 0;
  908. }
  909. static int tls_process_client_finished(struct tlsv1_server *conn, u8 ct,
  910. const u8 *in_data, size_t *in_len)
  911. {
  912. const u8 *pos, *end;
  913. size_t left, len, hlen;
  914. u8 verify_data[TLS_VERIFY_DATA_LEN];
  915. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  916. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  917. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; "
  918. "received content type 0x%x", ct);
  919. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  920. TLS_ALERT_UNEXPECTED_MESSAGE);
  921. return -1;
  922. }
  923. pos = in_data;
  924. left = *in_len;
  925. if (left < 4) {
  926. wpa_printf(MSG_DEBUG, "TLSv1: Too short record (left=%lu) for "
  927. "Finished",
  928. (unsigned long) left);
  929. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  930. TLS_ALERT_DECODE_ERROR);
  931. return -1;
  932. }
  933. if (pos[0] != TLS_HANDSHAKE_TYPE_FINISHED) {
  934. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; received "
  935. "type 0x%x", pos[0]);
  936. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  937. TLS_ALERT_UNEXPECTED_MESSAGE);
  938. return -1;
  939. }
  940. len = WPA_GET_BE24(pos + 1);
  941. pos += 4;
  942. left -= 4;
  943. if (len > left) {
  944. wpa_printf(MSG_DEBUG, "TLSv1: Too short buffer for Finished "
  945. "(len=%lu > left=%lu)",
  946. (unsigned long) len, (unsigned long) left);
  947. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  948. TLS_ALERT_DECODE_ERROR);
  949. return -1;
  950. }
  951. end = pos + len;
  952. if (len != TLS_VERIFY_DATA_LEN) {
  953. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected verify_data length "
  954. "in Finished: %lu (expected %d)",
  955. (unsigned long) len, TLS_VERIFY_DATA_LEN);
  956. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  957. TLS_ALERT_DECODE_ERROR);
  958. return -1;
  959. }
  960. wpa_hexdump(MSG_MSGDUMP, "TLSv1: verify_data in Finished",
  961. pos, TLS_VERIFY_DATA_LEN);
  962. #ifdef CONFIG_TLSV12
  963. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  964. hlen = SHA256_MAC_LEN;
  965. if (conn->verify.sha256_client == NULL ||
  966. crypto_hash_finish(conn->verify.sha256_client, hash, &hlen)
  967. < 0) {
  968. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  969. TLS_ALERT_INTERNAL_ERROR);
  970. conn->verify.sha256_client = NULL;
  971. return -1;
  972. }
  973. conn->verify.sha256_client = NULL;
  974. } else {
  975. #endif /* CONFIG_TLSV12 */
  976. hlen = MD5_MAC_LEN;
  977. if (conn->verify.md5_client == NULL ||
  978. crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
  979. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  980. TLS_ALERT_INTERNAL_ERROR);
  981. conn->verify.md5_client = NULL;
  982. crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
  983. conn->verify.sha1_client = NULL;
  984. return -1;
  985. }
  986. conn->verify.md5_client = NULL;
  987. hlen = SHA1_MAC_LEN;
  988. if (conn->verify.sha1_client == NULL ||
  989. crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
  990. &hlen) < 0) {
  991. conn->verify.sha1_client = NULL;
  992. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  993. TLS_ALERT_INTERNAL_ERROR);
  994. return -1;
  995. }
  996. conn->verify.sha1_client = NULL;
  997. hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
  998. #ifdef CONFIG_TLSV12
  999. }
  1000. #endif /* CONFIG_TLSV12 */
  1001. if (tls_prf(conn->rl.tls_version,
  1002. conn->master_secret, TLS_MASTER_SECRET_LEN,
  1003. "client finished", hash, hlen,
  1004. verify_data, TLS_VERIFY_DATA_LEN)) {
  1005. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive verify_data");
  1006. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1007. TLS_ALERT_DECRYPT_ERROR);
  1008. return -1;
  1009. }
  1010. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
  1011. verify_data, TLS_VERIFY_DATA_LEN);
  1012. if (os_memcmp(pos, verify_data, TLS_VERIFY_DATA_LEN) != 0) {
  1013. wpa_printf(MSG_INFO, "TLSv1: Mismatch in verify_data");
  1014. return -1;
  1015. }
  1016. wpa_printf(MSG_DEBUG, "TLSv1: Received Finished");
  1017. *in_len = end - in_data;
  1018. if (conn->use_session_ticket) {
  1019. /* Abbreviated handshake using session ticket; RFC 4507 */
  1020. wpa_printf(MSG_DEBUG, "TLSv1: Abbreviated handshake completed "
  1021. "successfully");
  1022. conn->state = ESTABLISHED;
  1023. } else {
  1024. /* Full handshake */
  1025. conn->state = SERVER_CHANGE_CIPHER_SPEC;
  1026. }
  1027. return 0;
  1028. }
  1029. int tlsv1_server_process_handshake(struct tlsv1_server *conn, u8 ct,
  1030. const u8 *buf, size_t *len)
  1031. {
  1032. if (ct == TLS_CONTENT_TYPE_ALERT) {
  1033. if (*len < 2) {
  1034. wpa_printf(MSG_DEBUG, "TLSv1: Alert underflow");
  1035. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1036. TLS_ALERT_DECODE_ERROR);
  1037. return -1;
  1038. }
  1039. wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
  1040. buf[0], buf[1]);
  1041. *len = 2;
  1042. conn->state = FAILED;
  1043. return -1;
  1044. }
  1045. switch (conn->state) {
  1046. case CLIENT_HELLO:
  1047. if (tls_process_client_hello(conn, ct, buf, len))
  1048. return -1;
  1049. break;
  1050. case CLIENT_CERTIFICATE:
  1051. if (tls_process_certificate(conn, ct, buf, len))
  1052. return -1;
  1053. break;
  1054. case CLIENT_KEY_EXCHANGE:
  1055. if (tls_process_client_key_exchange(conn, ct, buf, len))
  1056. return -1;
  1057. break;
  1058. case CERTIFICATE_VERIFY:
  1059. if (tls_process_certificate_verify(conn, ct, buf, len))
  1060. return -1;
  1061. break;
  1062. case CHANGE_CIPHER_SPEC:
  1063. if (tls_process_change_cipher_spec(conn, ct, buf, len))
  1064. return -1;
  1065. break;
  1066. case CLIENT_FINISHED:
  1067. if (tls_process_client_finished(conn, ct, buf, len))
  1068. return -1;
  1069. break;
  1070. default:
  1071. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d "
  1072. "while processing received message",
  1073. conn->state);
  1074. return -1;
  1075. }
  1076. if (ct == TLS_CONTENT_TYPE_HANDSHAKE)
  1077. tls_verify_hash_add(&conn->verify, buf, *len);
  1078. return 0;
  1079. }