tlsv1_server_read.c 30 KB

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