tlsv1_server_read.c 31 KB

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