tlsv1_client_read.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /*
  2. * TLSv1 client - read handshake message
  3. * Copyright (c) 2006-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/md5.h"
  11. #include "crypto/sha1.h"
  12. #include "crypto/sha256.h"
  13. #include "crypto/tls.h"
  14. #include "x509v3.h"
  15. #include "tlsv1_common.h"
  16. #include "tlsv1_record.h"
  17. #include "tlsv1_client.h"
  18. #include "tlsv1_client_i.h"
  19. static int tls_process_server_key_exchange(struct tlsv1_client *conn, u8 ct,
  20. const u8 *in_data, size_t *in_len);
  21. static int tls_process_certificate_request(struct tlsv1_client *conn, u8 ct,
  22. const u8 *in_data, size_t *in_len);
  23. static int tls_process_server_hello_done(struct tlsv1_client *conn, u8 ct,
  24. const u8 *in_data, size_t *in_len);
  25. static int tls_version_disabled(struct tlsv1_client *conn, u16 ver)
  26. {
  27. return (((conn->flags & TLS_CONN_DISABLE_TLSv1_0) &&
  28. ver == TLS_VERSION_1) ||
  29. ((conn->flags & TLS_CONN_DISABLE_TLSv1_1) &&
  30. ver == TLS_VERSION_1_1) ||
  31. ((conn->flags & TLS_CONN_DISABLE_TLSv1_2) &&
  32. ver == TLS_VERSION_1_2));
  33. }
  34. static int tls_process_server_hello_extensions(struct tlsv1_client *conn,
  35. const u8 *pos, size_t len)
  36. {
  37. const u8 *end = pos + len;
  38. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ServerHello extensions",
  39. pos, len);
  40. while (pos < end) {
  41. u16 ext, elen;
  42. if (end - pos < 4) {
  43. wpa_printf(MSG_INFO, "TLSv1: Truncated ServerHello extension header");
  44. return -1;
  45. }
  46. ext = WPA_GET_BE16(pos);
  47. pos += 2;
  48. elen = WPA_GET_BE16(pos);
  49. pos += 2;
  50. if (elen > end - pos) {
  51. wpa_printf(MSG_INFO, "TLSv1: Truncated ServerHello extension");
  52. return -1;
  53. }
  54. wpa_printf(MSG_DEBUG, "TLSv1: ServerHello ExtensionType %u",
  55. ext);
  56. wpa_hexdump(MSG_DEBUG, "TLSv1: ServerHello extension data",
  57. pos, elen);
  58. pos += elen;
  59. }
  60. return 0;
  61. }
  62. static int tls_process_server_hello(struct tlsv1_client *conn, u8 ct,
  63. const u8 *in_data, size_t *in_len)
  64. {
  65. const u8 *pos, *end;
  66. size_t left, len, i;
  67. u16 cipher_suite;
  68. u16 tls_version;
  69. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  70. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  71. "received content type 0x%x", ct);
  72. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  73. TLS_ALERT_UNEXPECTED_MESSAGE);
  74. return -1;
  75. }
  76. pos = in_data;
  77. left = *in_len;
  78. if (left < 4)
  79. goto decode_error;
  80. /* HandshakeType msg_type */
  81. if (*pos != TLS_HANDSHAKE_TYPE_SERVER_HELLO) {
  82. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  83. "message %d (expected ServerHello)", *pos);
  84. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  85. TLS_ALERT_UNEXPECTED_MESSAGE);
  86. return -1;
  87. }
  88. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerHello");
  89. pos++;
  90. /* uint24 length */
  91. len = WPA_GET_BE24(pos);
  92. pos += 3;
  93. left -= 4;
  94. if (len > left)
  95. goto decode_error;
  96. /* body - ServerHello */
  97. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ServerHello", pos, len);
  98. end = pos + len;
  99. /* ProtocolVersion server_version */
  100. if (end - pos < 2)
  101. goto decode_error;
  102. tls_version = WPA_GET_BE16(pos);
  103. if (!tls_version_ok(tls_version) ||
  104. tls_version_disabled(conn, tls_version)) {
  105. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected protocol version in "
  106. "ServerHello %u.%u", pos[0], pos[1]);
  107. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  108. TLS_ALERT_PROTOCOL_VERSION);
  109. return -1;
  110. }
  111. pos += 2;
  112. wpa_printf(MSG_DEBUG, "TLSv1: Using TLS v%s",
  113. tls_version_str(tls_version));
  114. conn->rl.tls_version = tls_version;
  115. /* Random random */
  116. if (end - pos < TLS_RANDOM_LEN)
  117. goto decode_error;
  118. os_memcpy(conn->server_random, pos, TLS_RANDOM_LEN);
  119. pos += TLS_RANDOM_LEN;
  120. wpa_hexdump(MSG_MSGDUMP, "TLSv1: server_random",
  121. conn->server_random, TLS_RANDOM_LEN);
  122. /* SessionID session_id */
  123. if (end - pos < 1)
  124. goto decode_error;
  125. if (end - pos < 1 + *pos || *pos > TLS_SESSION_ID_MAX_LEN)
  126. goto decode_error;
  127. if (conn->session_id_len && conn->session_id_len == *pos &&
  128. os_memcmp(conn->session_id, pos + 1, conn->session_id_len) == 0) {
  129. pos += 1 + conn->session_id_len;
  130. wpa_printf(MSG_DEBUG, "TLSv1: Resuming old session");
  131. conn->session_resumed = 1;
  132. } else {
  133. conn->session_id_len = *pos;
  134. pos++;
  135. os_memcpy(conn->session_id, pos, conn->session_id_len);
  136. pos += conn->session_id_len;
  137. }
  138. wpa_hexdump(MSG_MSGDUMP, "TLSv1: session_id",
  139. conn->session_id, conn->session_id_len);
  140. /* CipherSuite cipher_suite */
  141. if (end - pos < 2)
  142. goto decode_error;
  143. cipher_suite = WPA_GET_BE16(pos);
  144. pos += 2;
  145. for (i = 0; i < conn->num_cipher_suites; i++) {
  146. if (cipher_suite == conn->cipher_suites[i])
  147. break;
  148. }
  149. if (i == conn->num_cipher_suites) {
  150. wpa_printf(MSG_INFO, "TLSv1: Server selected unexpected "
  151. "cipher suite 0x%04x", cipher_suite);
  152. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  153. TLS_ALERT_ILLEGAL_PARAMETER);
  154. return -1;
  155. }
  156. if (conn->session_resumed && cipher_suite != conn->prev_cipher_suite) {
  157. wpa_printf(MSG_DEBUG, "TLSv1: Server selected a different "
  158. "cipher suite for a resumed connection (0x%04x != "
  159. "0x%04x)", cipher_suite, conn->prev_cipher_suite);
  160. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  161. TLS_ALERT_ILLEGAL_PARAMETER);
  162. return -1;
  163. }
  164. if (tlsv1_record_set_cipher_suite(&conn->rl, cipher_suite) < 0) {
  165. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set CipherSuite for "
  166. "record layer");
  167. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  168. TLS_ALERT_INTERNAL_ERROR);
  169. return -1;
  170. }
  171. conn->prev_cipher_suite = cipher_suite;
  172. /* CompressionMethod compression_method */
  173. if (end - pos < 1)
  174. goto decode_error;
  175. if (*pos != TLS_COMPRESSION_NULL) {
  176. wpa_printf(MSG_INFO, "TLSv1: Server selected unexpected "
  177. "compression 0x%02x", *pos);
  178. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  179. TLS_ALERT_ILLEGAL_PARAMETER);
  180. return -1;
  181. }
  182. pos++;
  183. if (end - pos >= 2) {
  184. u16 ext_len;
  185. ext_len = WPA_GET_BE16(pos);
  186. pos += 2;
  187. if (end - pos < ext_len) {
  188. wpa_printf(MSG_INFO,
  189. "TLSv1: Invalid ServerHello extension length: %u (left: %u)",
  190. ext_len, (unsigned int) (end - pos));
  191. goto decode_error;
  192. }
  193. if (tls_process_server_hello_extensions(conn, pos, ext_len))
  194. goto decode_error;
  195. pos += ext_len;
  196. }
  197. if (end != pos) {
  198. wpa_hexdump(MSG_DEBUG, "TLSv1: Unexpected extra data in the "
  199. "end of ServerHello", pos, end - pos);
  200. goto decode_error;
  201. }
  202. if (conn->session_ticket_included && conn->session_ticket_cb) {
  203. /* TODO: include SessionTicket extension if one was included in
  204. * ServerHello */
  205. int res = conn->session_ticket_cb(
  206. conn->session_ticket_cb_ctx, NULL, 0,
  207. conn->client_random, conn->server_random,
  208. conn->master_secret);
  209. if (res < 0) {
  210. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback "
  211. "indicated failure");
  212. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  213. TLS_ALERT_HANDSHAKE_FAILURE);
  214. return -1;
  215. }
  216. conn->use_session_ticket = !!res;
  217. }
  218. if ((conn->session_resumed || conn->use_session_ticket) &&
  219. tls_derive_keys(conn, NULL, 0)) {
  220. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  221. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  222. TLS_ALERT_INTERNAL_ERROR);
  223. return -1;
  224. }
  225. *in_len = end - in_data;
  226. conn->state = (conn->session_resumed || conn->use_session_ticket) ?
  227. SERVER_CHANGE_CIPHER_SPEC : SERVER_CERTIFICATE;
  228. return 0;
  229. decode_error:
  230. wpa_printf(MSG_DEBUG, "TLSv1: Failed to decode ServerHello");
  231. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  232. return -1;
  233. }
  234. static void tls_peer_cert_event(struct tlsv1_client *conn, int depth,
  235. struct x509_certificate *cert)
  236. {
  237. union tls_event_data ev;
  238. struct wpabuf *cert_buf = NULL;
  239. #ifdef CONFIG_SHA256
  240. u8 hash[32];
  241. #endif /* CONFIG_SHA256 */
  242. char subject[128];
  243. if (!conn->event_cb)
  244. return;
  245. os_memset(&ev, 0, sizeof(ev));
  246. if (conn->cred->cert_probe || conn->cert_in_cb) {
  247. cert_buf = wpabuf_alloc_copy(cert->cert_start,
  248. cert->cert_len);
  249. ev.peer_cert.cert = cert_buf;
  250. }
  251. #ifdef CONFIG_SHA256
  252. if (cert_buf) {
  253. const u8 *addr[1];
  254. size_t len[1];
  255. addr[0] = wpabuf_head(cert_buf);
  256. len[0] = wpabuf_len(cert_buf);
  257. if (sha256_vector(1, addr, len, hash) == 0) {
  258. ev.peer_cert.hash = hash;
  259. ev.peer_cert.hash_len = sizeof(hash);
  260. }
  261. }
  262. #endif /* CONFIG_SHA256 */
  263. ev.peer_cert.depth = depth;
  264. x509_name_string(&cert->subject, subject, sizeof(subject));
  265. ev.peer_cert.subject = subject;
  266. conn->event_cb(conn->cb_ctx, TLS_PEER_CERTIFICATE, &ev);
  267. wpabuf_free(cert_buf);
  268. }
  269. static void tls_cert_chain_failure_event(struct tlsv1_client *conn, int depth,
  270. struct x509_certificate *cert,
  271. enum tls_fail_reason reason,
  272. const char *reason_txt)
  273. {
  274. struct wpabuf *cert_buf = NULL;
  275. union tls_event_data ev;
  276. char subject[128];
  277. if (!conn->event_cb)
  278. return;
  279. os_memset(&ev, 0, sizeof(ev));
  280. ev.cert_fail.depth = depth;
  281. x509_name_string(&cert->subject, subject, sizeof(subject));
  282. ev.peer_cert.subject = subject;
  283. ev.cert_fail.reason = reason;
  284. ev.cert_fail.reason_txt = reason_txt;
  285. cert_buf = wpabuf_alloc_copy(cert->cert_start,
  286. cert->cert_len);
  287. ev.cert_fail.cert = cert_buf;
  288. conn->event_cb(conn->cb_ctx, TLS_CERT_CHAIN_FAILURE, &ev);
  289. wpabuf_free(cert_buf);
  290. }
  291. static int tls_process_certificate(struct tlsv1_client *conn, u8 ct,
  292. const u8 *in_data, size_t *in_len)
  293. {
  294. const u8 *pos, *end;
  295. size_t left, len, list_len, cert_len, idx;
  296. u8 type;
  297. struct x509_certificate *chain = NULL, *last = NULL, *cert;
  298. int reason;
  299. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  300. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  301. "received content type 0x%x", ct);
  302. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  303. TLS_ALERT_UNEXPECTED_MESSAGE);
  304. return -1;
  305. }
  306. pos = in_data;
  307. left = *in_len;
  308. if (left < 4) {
  309. wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate message "
  310. "(len=%lu)", (unsigned long) left);
  311. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  312. return -1;
  313. }
  314. type = *pos++;
  315. len = WPA_GET_BE24(pos);
  316. pos += 3;
  317. left -= 4;
  318. if (len > left) {
  319. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected Certificate message "
  320. "length (len=%lu != left=%lu)",
  321. (unsigned long) len, (unsigned long) left);
  322. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  323. return -1;
  324. }
  325. if (type == TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE)
  326. return tls_process_server_key_exchange(conn, ct, in_data,
  327. in_len);
  328. if (type == TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST)
  329. return tls_process_certificate_request(conn, ct, in_data,
  330. in_len);
  331. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  332. return tls_process_server_hello_done(conn, ct, in_data,
  333. in_len);
  334. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE) {
  335. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  336. "message %d (expected Certificate/"
  337. "ServerKeyExchange/CertificateRequest/"
  338. "ServerHelloDone)", type);
  339. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  340. TLS_ALERT_UNEXPECTED_MESSAGE);
  341. return -1;
  342. }
  343. wpa_printf(MSG_DEBUG,
  344. "TLSv1: Received Certificate (certificate_list len %lu)",
  345. (unsigned long) len);
  346. /*
  347. * opaque ASN.1Cert<2^24-1>;
  348. *
  349. * struct {
  350. * ASN.1Cert certificate_list<1..2^24-1>;
  351. * } Certificate;
  352. */
  353. end = pos + len;
  354. if (end - pos < 3) {
  355. wpa_printf(MSG_DEBUG, "TLSv1: Too short Certificate "
  356. "(left=%lu)", (unsigned long) left);
  357. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  358. return -1;
  359. }
  360. list_len = WPA_GET_BE24(pos);
  361. pos += 3;
  362. if ((size_t) (end - pos) != list_len) {
  363. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate_list "
  364. "length (len=%lu left=%lu)",
  365. (unsigned long) list_len,
  366. (unsigned long) (end - pos));
  367. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  368. return -1;
  369. }
  370. idx = 0;
  371. while (pos < end) {
  372. if (end - pos < 3) {
  373. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  374. "certificate_list");
  375. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  376. TLS_ALERT_DECODE_ERROR);
  377. x509_certificate_chain_free(chain);
  378. return -1;
  379. }
  380. cert_len = WPA_GET_BE24(pos);
  381. pos += 3;
  382. if ((size_t) (end - pos) < cert_len) {
  383. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected certificate "
  384. "length (len=%lu left=%lu)",
  385. (unsigned long) cert_len,
  386. (unsigned long) (end - pos));
  387. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  388. TLS_ALERT_DECODE_ERROR);
  389. x509_certificate_chain_free(chain);
  390. return -1;
  391. }
  392. wpa_printf(MSG_DEBUG, "TLSv1: Certificate %lu (len %lu)",
  393. (unsigned long) idx, (unsigned long) cert_len);
  394. if (idx == 0) {
  395. crypto_public_key_free(conn->server_rsa_key);
  396. if (tls_parse_cert(pos, cert_len,
  397. &conn->server_rsa_key)) {
  398. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  399. "the certificate");
  400. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  401. TLS_ALERT_BAD_CERTIFICATE);
  402. x509_certificate_chain_free(chain);
  403. return -1;
  404. }
  405. }
  406. cert = x509_certificate_parse(pos, cert_len);
  407. if (cert == NULL) {
  408. wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse "
  409. "the certificate");
  410. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  411. TLS_ALERT_BAD_CERTIFICATE);
  412. x509_certificate_chain_free(chain);
  413. return -1;
  414. }
  415. tls_peer_cert_event(conn, idx, cert);
  416. if (last == NULL)
  417. chain = cert;
  418. else
  419. last->next = cert;
  420. last = cert;
  421. idx++;
  422. pos += cert_len;
  423. }
  424. if (conn->cred && conn->cred->server_cert_only && chain) {
  425. u8 hash[SHA256_MAC_LEN];
  426. char buf[128];
  427. wpa_printf(MSG_DEBUG,
  428. "TLSv1: Validate server certificate hash");
  429. x509_name_string(&chain->subject, buf, sizeof(buf));
  430. wpa_printf(MSG_DEBUG, "TLSv1: 0: %s", buf);
  431. if (sha256_vector(1, &chain->cert_start, &chain->cert_len,
  432. hash) < 0 ||
  433. os_memcmp(conn->cred->srv_cert_hash, hash,
  434. SHA256_MAC_LEN) != 0) {
  435. wpa_printf(MSG_DEBUG,
  436. "TLSv1: Server certificate hash mismatch");
  437. wpa_hexdump(MSG_MSGDUMP, "TLSv1: SHA256 hash",
  438. hash, SHA256_MAC_LEN);
  439. if (conn->event_cb) {
  440. union tls_event_data ev;
  441. os_memset(&ev, 0, sizeof(ev));
  442. ev.cert_fail.reason = TLS_FAIL_UNSPECIFIED;
  443. ev.cert_fail.reason_txt =
  444. "Server certificate mismatch";
  445. ev.cert_fail.subject = buf;
  446. conn->event_cb(conn->cb_ctx,
  447. TLS_CERT_CHAIN_FAILURE, &ev);
  448. }
  449. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  450. TLS_ALERT_BAD_CERTIFICATE);
  451. x509_certificate_chain_free(chain);
  452. return -1;
  453. }
  454. } else if (conn->cred && conn->cred->cert_probe) {
  455. wpa_printf(MSG_DEBUG,
  456. "TLSv1: Reject server certificate on probe-only rune");
  457. if (conn->event_cb) {
  458. union tls_event_data ev;
  459. char buf[128];
  460. os_memset(&ev, 0, sizeof(ev));
  461. ev.cert_fail.reason = TLS_FAIL_SERVER_CHAIN_PROBE;
  462. ev.cert_fail.reason_txt =
  463. "Server certificate chain probe";
  464. if (chain) {
  465. x509_name_string(&chain->subject, buf,
  466. sizeof(buf));
  467. ev.cert_fail.subject = buf;
  468. }
  469. conn->event_cb(conn->cb_ctx, TLS_CERT_CHAIN_FAILURE,
  470. &ev);
  471. }
  472. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  473. TLS_ALERT_BAD_CERTIFICATE);
  474. x509_certificate_chain_free(chain);
  475. return -1;
  476. } else if (conn->cred && conn->cred->ca_cert_verify &&
  477. x509_certificate_chain_validate(
  478. conn->cred->trusted_certs, chain, &reason,
  479. !!(conn->flags & TLS_CONN_DISABLE_TIME_CHECKS))
  480. < 0) {
  481. int tls_reason;
  482. wpa_printf(MSG_DEBUG, "TLSv1: Server certificate chain "
  483. "validation failed (reason=%d)", reason);
  484. switch (reason) {
  485. case X509_VALIDATE_BAD_CERTIFICATE:
  486. tls_reason = TLS_ALERT_BAD_CERTIFICATE;
  487. tls_cert_chain_failure_event(
  488. conn, 0, chain, TLS_FAIL_BAD_CERTIFICATE,
  489. "bad certificate");
  490. break;
  491. case X509_VALIDATE_UNSUPPORTED_CERTIFICATE:
  492. tls_reason = TLS_ALERT_UNSUPPORTED_CERTIFICATE;
  493. break;
  494. case X509_VALIDATE_CERTIFICATE_REVOKED:
  495. tls_reason = TLS_ALERT_CERTIFICATE_REVOKED;
  496. tls_cert_chain_failure_event(
  497. conn, 0, chain, TLS_FAIL_REVOKED,
  498. "certificate revoked");
  499. break;
  500. case X509_VALIDATE_CERTIFICATE_EXPIRED:
  501. tls_reason = TLS_ALERT_CERTIFICATE_EXPIRED;
  502. tls_cert_chain_failure_event(
  503. conn, 0, chain, TLS_FAIL_EXPIRED,
  504. "certificate has expired or is not yet valid");
  505. break;
  506. case X509_VALIDATE_CERTIFICATE_UNKNOWN:
  507. tls_reason = TLS_ALERT_CERTIFICATE_UNKNOWN;
  508. break;
  509. case X509_VALIDATE_UNKNOWN_CA:
  510. tls_reason = TLS_ALERT_UNKNOWN_CA;
  511. tls_cert_chain_failure_event(
  512. conn, 0, chain, TLS_FAIL_UNTRUSTED,
  513. "unknown CA");
  514. break;
  515. default:
  516. tls_reason = TLS_ALERT_BAD_CERTIFICATE;
  517. break;
  518. }
  519. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, tls_reason);
  520. x509_certificate_chain_free(chain);
  521. return -1;
  522. }
  523. if (conn->cred && !conn->cred->server_cert_only && chain &&
  524. (chain->extensions_present & X509_EXT_EXT_KEY_USAGE) &&
  525. !(chain->ext_key_usage &
  526. (X509_EXT_KEY_USAGE_ANY | X509_EXT_KEY_USAGE_SERVER_AUTH))) {
  527. tls_cert_chain_failure_event(
  528. conn, 0, chain, TLS_FAIL_BAD_CERTIFICATE,
  529. "certificate not allowed for server authentication");
  530. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  531. TLS_ALERT_BAD_CERTIFICATE);
  532. x509_certificate_chain_free(chain);
  533. return -1;
  534. }
  535. if (conn->flags & TLS_CONN_REQUEST_OCSP) {
  536. x509_certificate_chain_free(conn->server_cert);
  537. conn->server_cert = chain;
  538. } else {
  539. x509_certificate_chain_free(chain);
  540. }
  541. *in_len = end - in_data;
  542. conn->state = SERVER_KEY_EXCHANGE;
  543. return 0;
  544. }
  545. static unsigned int count_bits(const u8 *val, size_t len)
  546. {
  547. size_t i;
  548. unsigned int bits;
  549. u8 tmp;
  550. for (i = 0; i < len; i++) {
  551. if (val[i])
  552. break;
  553. }
  554. if (i == len)
  555. return 0;
  556. bits = (len - i - 1) * 8;
  557. tmp = val[i];
  558. while (tmp) {
  559. bits++;
  560. tmp >>= 1;
  561. }
  562. return bits;
  563. }
  564. static int tlsv1_process_diffie_hellman(struct tlsv1_client *conn,
  565. const u8 *buf, size_t len,
  566. tls_key_exchange key_exchange)
  567. {
  568. const u8 *pos, *end, *server_params, *server_params_end;
  569. u8 alert;
  570. unsigned int bits;
  571. u16 val;
  572. tlsv1_client_free_dh(conn);
  573. pos = buf;
  574. end = buf + len;
  575. if (end - pos < 3)
  576. goto fail;
  577. server_params = pos;
  578. val = WPA_GET_BE16(pos);
  579. pos += 2;
  580. if (val == 0 || val > (size_t) (end - pos)) {
  581. wpa_printf(MSG_DEBUG, "TLSv1: Invalid dh_p length %u", val);
  582. goto fail;
  583. }
  584. conn->dh_p_len = val;
  585. bits = count_bits(pos, conn->dh_p_len);
  586. if (bits < 768) {
  587. wpa_printf(MSG_INFO, "TLSv1: Reject under 768-bit DH prime (insecure; only %u bits)",
  588. bits);
  589. wpa_hexdump(MSG_DEBUG, "TLSv1: Rejected DH prime",
  590. pos, conn->dh_p_len);
  591. goto fail;
  592. }
  593. conn->dh_p = os_malloc(conn->dh_p_len);
  594. if (conn->dh_p == NULL)
  595. goto fail;
  596. os_memcpy(conn->dh_p, pos, conn->dh_p_len);
  597. pos += conn->dh_p_len;
  598. wpa_hexdump(MSG_DEBUG, "TLSv1: DH p (prime)",
  599. conn->dh_p, conn->dh_p_len);
  600. if (end - pos < 3)
  601. goto fail;
  602. val = WPA_GET_BE16(pos);
  603. pos += 2;
  604. if (val == 0 || val > (size_t) (end - pos))
  605. goto fail;
  606. conn->dh_g_len = val;
  607. conn->dh_g = os_malloc(conn->dh_g_len);
  608. if (conn->dh_g == NULL)
  609. goto fail;
  610. os_memcpy(conn->dh_g, pos, conn->dh_g_len);
  611. pos += conn->dh_g_len;
  612. wpa_hexdump(MSG_DEBUG, "TLSv1: DH g (generator)",
  613. conn->dh_g, conn->dh_g_len);
  614. if (conn->dh_g_len == 1 && conn->dh_g[0] < 2)
  615. goto fail;
  616. if (end - pos < 3)
  617. goto fail;
  618. val = WPA_GET_BE16(pos);
  619. pos += 2;
  620. if (val == 0 || val > (size_t) (end - pos))
  621. goto fail;
  622. conn->dh_ys_len = val;
  623. conn->dh_ys = os_malloc(conn->dh_ys_len);
  624. if (conn->dh_ys == NULL)
  625. goto fail;
  626. os_memcpy(conn->dh_ys, pos, conn->dh_ys_len);
  627. pos += conn->dh_ys_len;
  628. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
  629. conn->dh_ys, conn->dh_ys_len);
  630. server_params_end = pos;
  631. if (key_exchange == TLS_KEY_X_DHE_RSA) {
  632. u8 hash[64];
  633. int hlen;
  634. if (conn->rl.tls_version == TLS_VERSION_1_2) {
  635. #ifdef CONFIG_TLSV12
  636. /*
  637. * RFC 5246, 4.7:
  638. * TLS v1.2 adds explicit indication of the used
  639. * signature and hash algorithms.
  640. *
  641. * struct {
  642. * HashAlgorithm hash;
  643. * SignatureAlgorithm signature;
  644. * } SignatureAndHashAlgorithm;
  645. */
  646. if (end - pos < 2)
  647. goto fail;
  648. if ((pos[0] != TLS_HASH_ALG_SHA256 &&
  649. pos[0] != TLS_HASH_ALG_SHA384 &&
  650. pos[0] != TLS_HASH_ALG_SHA512) ||
  651. pos[1] != TLS_SIGN_ALG_RSA) {
  652. wpa_printf(MSG_DEBUG, "TLSv1.2: Unsupported hash(%u)/signature(%u) algorithm",
  653. pos[0], pos[1]);
  654. goto fail;
  655. }
  656. hlen = tlsv12_key_x_server_params_hash(
  657. conn->rl.tls_version, pos[0],
  658. conn->client_random,
  659. conn->server_random, server_params,
  660. server_params_end - server_params, hash);
  661. pos += 2;
  662. #else /* CONFIG_TLSV12 */
  663. goto fail;
  664. #endif /* CONFIG_TLSV12 */
  665. } else {
  666. hlen = tls_key_x_server_params_hash(
  667. conn->rl.tls_version, conn->client_random,
  668. conn->server_random, server_params,
  669. server_params_end - server_params, hash);
  670. }
  671. if (hlen < 0)
  672. goto fail;
  673. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ServerKeyExchange hash",
  674. hash, hlen);
  675. if (tls_verify_signature(conn->rl.tls_version,
  676. conn->server_rsa_key,
  677. hash, hlen, pos, end - pos,
  678. &alert) < 0)
  679. goto fail;
  680. }
  681. return 0;
  682. fail:
  683. wpa_printf(MSG_DEBUG, "TLSv1: Processing DH params failed");
  684. tlsv1_client_free_dh(conn);
  685. return -1;
  686. }
  687. static enum tls_ocsp_result
  688. tls_process_certificate_status_ocsp_response(struct tlsv1_client *conn,
  689. const u8 *pos, size_t len)
  690. {
  691. const u8 *end = pos + len;
  692. u32 ocsp_resp_len;
  693. /* opaque OCSPResponse<1..2^24-1>; */
  694. if (end - pos < 3) {
  695. wpa_printf(MSG_INFO, "TLSv1: Too short OCSPResponse");
  696. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  697. return TLS_OCSP_INVALID;
  698. }
  699. ocsp_resp_len = WPA_GET_BE24(pos);
  700. pos += 3;
  701. if (end - pos < ocsp_resp_len) {
  702. wpa_printf(MSG_INFO, "TLSv1: Truncated OCSPResponse");
  703. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  704. return TLS_OCSP_INVALID;
  705. }
  706. return tls_process_ocsp_response(conn, pos, ocsp_resp_len);
  707. }
  708. static int tls_process_certificate_status(struct tlsv1_client *conn, u8 ct,
  709. const u8 *in_data, size_t *in_len)
  710. {
  711. const u8 *pos, *end;
  712. size_t left, len;
  713. u8 type, status_type;
  714. enum tls_ocsp_result res;
  715. struct x509_certificate *cert;
  716. int depth;
  717. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  718. wpa_printf(MSG_DEBUG,
  719. "TLSv1: Expected Handshake; received content type 0x%x",
  720. ct);
  721. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  722. TLS_ALERT_UNEXPECTED_MESSAGE);
  723. return -1;
  724. }
  725. pos = in_data;
  726. left = *in_len;
  727. if (left < 4) {
  728. wpa_printf(MSG_DEBUG,
  729. "TLSv1: Too short CertificateStatus (left=%lu)",
  730. (unsigned long) left);
  731. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  732. return -1;
  733. }
  734. type = *pos++;
  735. len = WPA_GET_BE24(pos);
  736. pos += 3;
  737. left -= 4;
  738. if (len > left) {
  739. wpa_printf(MSG_DEBUG,
  740. "TLSv1: Mismatch in CertificateStatus length (len=%lu != left=%lu)",
  741. (unsigned long) len, (unsigned long) left);
  742. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  743. return -1;
  744. }
  745. end = pos + len;
  746. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_STATUS) {
  747. wpa_printf(MSG_DEBUG,
  748. "TLSv1: Received unexpected handshake message %d (expected CertificateStatus)",
  749. type);
  750. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  751. TLS_ALERT_UNEXPECTED_MESSAGE);
  752. return -1;
  753. }
  754. wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateStatus");
  755. /*
  756. * struct {
  757. * CertificateStatusType status_type;
  758. * select (status_type) {
  759. * case ocsp: OCSPResponse;
  760. * case ocsp_multi: OCSPResponseList;
  761. * } response;
  762. * } CertificateStatus;
  763. */
  764. if (end - pos < 1) {
  765. wpa_printf(MSG_INFO, "TLSv1: Too short CertificateStatus");
  766. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  767. return -1;
  768. }
  769. status_type = *pos++;
  770. wpa_printf(MSG_DEBUG, "TLSv1: CertificateStatus status_type %u",
  771. status_type);
  772. if (status_type == 1 /* ocsp */) {
  773. res = tls_process_certificate_status_ocsp_response(
  774. conn, pos, end - pos);
  775. } else if (status_type == 2 /* ocsp_multi */) {
  776. int good = 0, revoked = 0;
  777. u32 resp_len;
  778. res = TLS_OCSP_NO_RESPONSE;
  779. /*
  780. * opaque OCSPResponse<0..2^24-1>;
  781. *
  782. * struct {
  783. * OCSPResponse ocsp_response_list<1..2^24-1>;
  784. * } OCSPResponseList;
  785. */
  786. if (end - pos < 3) {
  787. wpa_printf(MSG_DEBUG,
  788. "TLSv1: Truncated OCSPResponseList");
  789. res = TLS_OCSP_INVALID;
  790. goto done;
  791. }
  792. resp_len = WPA_GET_BE24(pos);
  793. pos += 3;
  794. if (end - pos < resp_len) {
  795. wpa_printf(MSG_DEBUG,
  796. "TLSv1: Truncated OCSPResponseList(len=%u)",
  797. resp_len);
  798. res = TLS_OCSP_INVALID;
  799. goto done;
  800. }
  801. end = pos + resp_len;
  802. while (end - pos >= 3) {
  803. resp_len = WPA_GET_BE24(pos);
  804. pos += 3;
  805. if (resp_len > end - pos) {
  806. wpa_printf(MSG_DEBUG,
  807. "TLSv1: Truncated OCSPResponse(len=%u; left=%d) in ocsp_multi",
  808. resp_len, (int) (end - pos));
  809. res = TLS_OCSP_INVALID;
  810. break;
  811. }
  812. if (!resp_len)
  813. continue; /* Skip an empty response */
  814. res = tls_process_certificate_status_ocsp_response(
  815. conn, pos - 3, resp_len + 3);
  816. if (res == TLS_OCSP_REVOKED)
  817. revoked++;
  818. else if (res == TLS_OCSP_GOOD)
  819. good++;
  820. pos += resp_len;
  821. }
  822. if (revoked)
  823. res = TLS_OCSP_REVOKED;
  824. else if (good)
  825. res = TLS_OCSP_GOOD;
  826. } else {
  827. wpa_printf(MSG_DEBUG,
  828. "TLSv1: Ignore unsupported CertificateStatus");
  829. goto skip;
  830. }
  831. done:
  832. if (res == TLS_OCSP_REVOKED) {
  833. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  834. TLS_ALERT_CERTIFICATE_REVOKED);
  835. for (cert = conn->server_cert, depth = 0; cert;
  836. cert = cert->next, depth++) {
  837. if (cert->ocsp_revoked) {
  838. tls_cert_chain_failure_event(
  839. conn, depth, cert, TLS_FAIL_REVOKED,
  840. "certificate revoked");
  841. }
  842. }
  843. return -1;
  844. }
  845. if (conn->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
  846. /*
  847. * Verify that each certificate on the chain that is not part
  848. * of the trusted certificates has a good status. If not,
  849. * terminate handshake.
  850. */
  851. for (cert = conn->server_cert, depth = 0; cert;
  852. cert = cert->next, depth++) {
  853. if (!cert->ocsp_good) {
  854. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  855. TLS_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE);
  856. tls_cert_chain_failure_event(
  857. conn, depth, cert,
  858. TLS_FAIL_UNSPECIFIED,
  859. "bad certificate status response");
  860. return -1;
  861. }
  862. if (cert->issuer_trusted)
  863. break;
  864. }
  865. }
  866. if ((conn->flags & TLS_CONN_REQUIRE_OCSP) && res != TLS_OCSP_GOOD) {
  867. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  868. res == TLS_OCSP_INVALID ? TLS_ALERT_DECODE_ERROR :
  869. TLS_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE);
  870. if (conn->server_cert)
  871. tls_cert_chain_failure_event(
  872. conn, 0, conn->server_cert,
  873. TLS_FAIL_UNSPECIFIED,
  874. "bad certificate status response");
  875. return -1;
  876. }
  877. conn->ocsp_resp_received = 1;
  878. skip:
  879. *in_len = end - in_data;
  880. conn->state = SERVER_KEY_EXCHANGE;
  881. return 0;
  882. }
  883. static int tls_process_server_key_exchange(struct tlsv1_client *conn, u8 ct,
  884. const u8 *in_data, size_t *in_len)
  885. {
  886. const u8 *pos, *end;
  887. size_t left, len;
  888. u8 type;
  889. const struct tls_cipher_suite *suite;
  890. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  891. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  892. "received content type 0x%x", ct);
  893. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  894. TLS_ALERT_UNEXPECTED_MESSAGE);
  895. return -1;
  896. }
  897. pos = in_data;
  898. left = *in_len;
  899. if (left < 4) {
  900. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerKeyExchange "
  901. "(Left=%lu)", (unsigned long) left);
  902. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  903. return -1;
  904. }
  905. type = *pos++;
  906. len = WPA_GET_BE24(pos);
  907. pos += 3;
  908. left -= 4;
  909. if (len > left) {
  910. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerKeyExchange "
  911. "length (len=%lu != left=%lu)",
  912. (unsigned long) len, (unsigned long) left);
  913. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  914. return -1;
  915. }
  916. end = pos + len;
  917. if ((conn->flags & TLS_CONN_REQUEST_OCSP) &&
  918. type == TLS_HANDSHAKE_TYPE_CERTIFICATE_STATUS)
  919. return tls_process_certificate_status(conn, ct, in_data,
  920. in_len);
  921. if (type == TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST)
  922. return tls_process_certificate_request(conn, ct, in_data,
  923. in_len);
  924. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  925. return tls_process_server_hello_done(conn, ct, in_data,
  926. in_len);
  927. if (type != TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE) {
  928. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  929. "message %d (expected ServerKeyExchange/"
  930. "CertificateRequest/ServerHelloDone%s)", type,
  931. (conn->flags & TLS_CONN_REQUEST_OCSP) ?
  932. "/CertificateStatus" : "");
  933. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  934. TLS_ALERT_UNEXPECTED_MESSAGE);
  935. return -1;
  936. }
  937. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerKeyExchange");
  938. if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
  939. wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not allowed "
  940. "with the selected cipher suite");
  941. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  942. TLS_ALERT_UNEXPECTED_MESSAGE);
  943. return -1;
  944. }
  945. wpa_hexdump(MSG_DEBUG, "TLSv1: ServerKeyExchange", pos, len);
  946. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  947. if (suite && (suite->key_exchange == TLS_KEY_X_DH_anon ||
  948. suite->key_exchange == TLS_KEY_X_DHE_RSA)) {
  949. if (tlsv1_process_diffie_hellman(conn, pos, len,
  950. suite->key_exchange) < 0) {
  951. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  952. TLS_ALERT_DECODE_ERROR);
  953. return -1;
  954. }
  955. } else {
  956. wpa_printf(MSG_DEBUG, "TLSv1: UnexpectedServerKeyExchange");
  957. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  958. TLS_ALERT_UNEXPECTED_MESSAGE);
  959. return -1;
  960. }
  961. *in_len = end - in_data;
  962. conn->state = SERVER_CERTIFICATE_REQUEST;
  963. return 0;
  964. }
  965. static int tls_process_certificate_request(struct tlsv1_client *conn, u8 ct,
  966. const u8 *in_data, size_t *in_len)
  967. {
  968. const u8 *pos, *end;
  969. size_t left, len;
  970. u8 type;
  971. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  972. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  973. "received content type 0x%x", ct);
  974. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  975. TLS_ALERT_UNEXPECTED_MESSAGE);
  976. return -1;
  977. }
  978. pos = in_data;
  979. left = *in_len;
  980. if (left < 4) {
  981. wpa_printf(MSG_DEBUG, "TLSv1: Too short CertificateRequest "
  982. "(left=%lu)", (unsigned long) left);
  983. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  984. return -1;
  985. }
  986. type = *pos++;
  987. len = WPA_GET_BE24(pos);
  988. pos += 3;
  989. left -= 4;
  990. if (len > left) {
  991. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in CertificateRequest "
  992. "length (len=%lu != left=%lu)",
  993. (unsigned long) len, (unsigned long) left);
  994. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  995. return -1;
  996. }
  997. end = pos + len;
  998. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  999. return tls_process_server_hello_done(conn, ct, in_data,
  1000. in_len);
  1001. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST) {
  1002. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  1003. "message %d (expected CertificateRequest/"
  1004. "ServerHelloDone)", type);
  1005. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1006. TLS_ALERT_UNEXPECTED_MESSAGE);
  1007. return -1;
  1008. }
  1009. wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateRequest");
  1010. conn->certificate_requested = 1;
  1011. *in_len = end - in_data;
  1012. conn->state = SERVER_HELLO_DONE;
  1013. return 0;
  1014. }
  1015. static int tls_process_server_hello_done(struct tlsv1_client *conn, u8 ct,
  1016. const u8 *in_data, size_t *in_len)
  1017. {
  1018. const u8 *pos, *end;
  1019. size_t left, len;
  1020. u8 type;
  1021. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  1022. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  1023. "received content type 0x%x", ct);
  1024. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1025. TLS_ALERT_UNEXPECTED_MESSAGE);
  1026. return -1;
  1027. }
  1028. pos = in_data;
  1029. left = *in_len;
  1030. if (left < 4) {
  1031. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerHelloDone "
  1032. "(left=%lu)", (unsigned long) left);
  1033. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  1034. return -1;
  1035. }
  1036. type = *pos++;
  1037. len = WPA_GET_BE24(pos);
  1038. pos += 3;
  1039. left -= 4;
  1040. if (len > left) {
  1041. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerHelloDone "
  1042. "length (len=%lu != left=%lu)",
  1043. (unsigned long) len, (unsigned long) left);
  1044. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  1045. return -1;
  1046. }
  1047. end = pos + len;
  1048. if (type != TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE) {
  1049. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  1050. "message %d (expected ServerHelloDone)", type);
  1051. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1052. TLS_ALERT_UNEXPECTED_MESSAGE);
  1053. return -1;
  1054. }
  1055. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerHelloDone");
  1056. if ((conn->flags & TLS_CONN_REQUIRE_OCSP) &&
  1057. !conn->ocsp_resp_received) {
  1058. wpa_printf(MSG_INFO,
  1059. "TLSv1: No OCSP response received - reject handshake");
  1060. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1061. TLS_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE);
  1062. return -1;
  1063. }
  1064. *in_len = end - in_data;
  1065. conn->state = CLIENT_KEY_EXCHANGE;
  1066. return 0;
  1067. }
  1068. static int tls_process_server_change_cipher_spec(struct tlsv1_client *conn,
  1069. u8 ct, const u8 *in_data,
  1070. size_t *in_len)
  1071. {
  1072. const u8 *pos;
  1073. size_t left;
  1074. if (ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
  1075. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  1076. "received content type 0x%x", ct);
  1077. if (conn->use_session_ticket) {
  1078. int res;
  1079. wpa_printf(MSG_DEBUG, "TLSv1: Server may have "
  1080. "rejected SessionTicket");
  1081. conn->use_session_ticket = 0;
  1082. /* Notify upper layers that SessionTicket failed */
  1083. res = conn->session_ticket_cb(
  1084. conn->session_ticket_cb_ctx, NULL, 0, NULL,
  1085. NULL, NULL);
  1086. if (res < 0) {
  1087. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket "
  1088. "callback indicated failure");
  1089. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1090. TLS_ALERT_HANDSHAKE_FAILURE);
  1091. return -1;
  1092. }
  1093. conn->state = SERVER_CERTIFICATE;
  1094. return tls_process_certificate(conn, ct, in_data,
  1095. in_len);
  1096. }
  1097. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1098. TLS_ALERT_UNEXPECTED_MESSAGE);
  1099. return -1;
  1100. }
  1101. pos = in_data;
  1102. left = *in_len;
  1103. if (left < 1) {
  1104. wpa_printf(MSG_DEBUG, "TLSv1: Too short ChangeCipherSpec");
  1105. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  1106. return -1;
  1107. }
  1108. if (*pos != TLS_CHANGE_CIPHER_SPEC) {
  1109. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  1110. "received data 0x%x", *pos);
  1111. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1112. TLS_ALERT_UNEXPECTED_MESSAGE);
  1113. return -1;
  1114. }
  1115. wpa_printf(MSG_DEBUG, "TLSv1: Received ChangeCipherSpec");
  1116. if (tlsv1_record_change_read_cipher(&conn->rl) < 0) {
  1117. wpa_printf(MSG_DEBUG, "TLSv1: Failed to change read cipher "
  1118. "for record layer");
  1119. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1120. TLS_ALERT_INTERNAL_ERROR);
  1121. return -1;
  1122. }
  1123. *in_len = pos + 1 - in_data;
  1124. conn->state = SERVER_FINISHED;
  1125. return 0;
  1126. }
  1127. static int tls_process_server_finished(struct tlsv1_client *conn, u8 ct,
  1128. const u8 *in_data, size_t *in_len)
  1129. {
  1130. const u8 *pos, *end;
  1131. size_t left, len, hlen;
  1132. u8 verify_data[TLS_VERIFY_DATA_LEN];
  1133. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  1134. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  1135. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; "
  1136. "received content type 0x%x", ct);
  1137. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1138. TLS_ALERT_UNEXPECTED_MESSAGE);
  1139. return -1;
  1140. }
  1141. pos = in_data;
  1142. left = *in_len;
  1143. if (left < 4) {
  1144. wpa_printf(MSG_DEBUG, "TLSv1: Too short record (left=%lu) for "
  1145. "Finished",
  1146. (unsigned long) left);
  1147. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1148. TLS_ALERT_DECODE_ERROR);
  1149. return -1;
  1150. }
  1151. if (pos[0] != TLS_HANDSHAKE_TYPE_FINISHED) {
  1152. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; received "
  1153. "type 0x%x", pos[0]);
  1154. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1155. TLS_ALERT_UNEXPECTED_MESSAGE);
  1156. return -1;
  1157. }
  1158. len = WPA_GET_BE24(pos + 1);
  1159. pos += 4;
  1160. left -= 4;
  1161. if (len > left) {
  1162. wpa_printf(MSG_DEBUG, "TLSv1: Too short buffer for Finished "
  1163. "(len=%lu > left=%lu)",
  1164. (unsigned long) len, (unsigned long) left);
  1165. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1166. TLS_ALERT_DECODE_ERROR);
  1167. return -1;
  1168. }
  1169. end = pos + len;
  1170. if (len != TLS_VERIFY_DATA_LEN) {
  1171. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected verify_data length "
  1172. "in Finished: %lu (expected %d)",
  1173. (unsigned long) len, TLS_VERIFY_DATA_LEN);
  1174. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1175. TLS_ALERT_DECODE_ERROR);
  1176. return -1;
  1177. }
  1178. wpa_hexdump(MSG_MSGDUMP, "TLSv1: verify_data in Finished",
  1179. pos, TLS_VERIFY_DATA_LEN);
  1180. #ifdef CONFIG_TLSV12
  1181. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  1182. hlen = SHA256_MAC_LEN;
  1183. if (conn->verify.sha256_server == NULL ||
  1184. crypto_hash_finish(conn->verify.sha256_server, hash, &hlen)
  1185. < 0) {
  1186. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1187. TLS_ALERT_INTERNAL_ERROR);
  1188. conn->verify.sha256_server = NULL;
  1189. return -1;
  1190. }
  1191. conn->verify.sha256_server = NULL;
  1192. } else {
  1193. #endif /* CONFIG_TLSV12 */
  1194. hlen = MD5_MAC_LEN;
  1195. if (conn->verify.md5_server == NULL ||
  1196. crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
  1197. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1198. TLS_ALERT_INTERNAL_ERROR);
  1199. conn->verify.md5_server = NULL;
  1200. crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
  1201. conn->verify.sha1_server = NULL;
  1202. return -1;
  1203. }
  1204. conn->verify.md5_server = NULL;
  1205. hlen = SHA1_MAC_LEN;
  1206. if (conn->verify.sha1_server == NULL ||
  1207. crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
  1208. &hlen) < 0) {
  1209. conn->verify.sha1_server = NULL;
  1210. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1211. TLS_ALERT_INTERNAL_ERROR);
  1212. return -1;
  1213. }
  1214. conn->verify.sha1_server = NULL;
  1215. hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
  1216. #ifdef CONFIG_TLSV12
  1217. }
  1218. #endif /* CONFIG_TLSV12 */
  1219. if (tls_prf(conn->rl.tls_version,
  1220. conn->master_secret, TLS_MASTER_SECRET_LEN,
  1221. "server finished", hash, hlen,
  1222. verify_data, TLS_VERIFY_DATA_LEN)) {
  1223. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive verify_data");
  1224. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1225. TLS_ALERT_DECRYPT_ERROR);
  1226. return -1;
  1227. }
  1228. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
  1229. verify_data, TLS_VERIFY_DATA_LEN);
  1230. if (os_memcmp_const(pos, verify_data, TLS_VERIFY_DATA_LEN) != 0) {
  1231. wpa_printf(MSG_INFO, "TLSv1: Mismatch in verify_data");
  1232. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1233. TLS_ALERT_DECRYPT_ERROR);
  1234. return -1;
  1235. }
  1236. wpa_printf(MSG_DEBUG, "TLSv1: Received Finished");
  1237. *in_len = end - in_data;
  1238. conn->state = (conn->session_resumed || conn->use_session_ticket) ?
  1239. CHANGE_CIPHER_SPEC : ACK_FINISHED;
  1240. return 0;
  1241. }
  1242. static int tls_process_application_data(struct tlsv1_client *conn, u8 ct,
  1243. const u8 *in_data, size_t *in_len,
  1244. u8 **out_data, size_t *out_len)
  1245. {
  1246. const u8 *pos;
  1247. size_t left;
  1248. if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
  1249. wpa_printf(MSG_DEBUG, "TLSv1: Expected Application Data; "
  1250. "received content type 0x%x", ct);
  1251. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1252. TLS_ALERT_UNEXPECTED_MESSAGE);
  1253. return -1;
  1254. }
  1255. pos = in_data;
  1256. left = *in_len;
  1257. wpa_hexdump(MSG_DEBUG, "TLSv1: Application Data included in Handshake",
  1258. pos, left);
  1259. *out_data = os_malloc(left);
  1260. if (*out_data) {
  1261. os_memcpy(*out_data, pos, left);
  1262. *out_len = left;
  1263. }
  1264. return 0;
  1265. }
  1266. int tlsv1_client_process_handshake(struct tlsv1_client *conn, u8 ct,
  1267. const u8 *buf, size_t *len,
  1268. u8 **out_data, size_t *out_len)
  1269. {
  1270. if (ct == TLS_CONTENT_TYPE_ALERT) {
  1271. if (*len < 2) {
  1272. wpa_printf(MSG_DEBUG, "TLSv1: Alert underflow");
  1273. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1274. TLS_ALERT_DECODE_ERROR);
  1275. return -1;
  1276. }
  1277. wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
  1278. buf[0], buf[1]);
  1279. *len = 2;
  1280. conn->state = FAILED;
  1281. return -1;
  1282. }
  1283. if (ct == TLS_CONTENT_TYPE_HANDSHAKE && *len >= 4 &&
  1284. buf[0] == TLS_HANDSHAKE_TYPE_HELLO_REQUEST) {
  1285. size_t hr_len = WPA_GET_BE24(buf + 1);
  1286. if (hr_len > *len - 4) {
  1287. wpa_printf(MSG_DEBUG, "TLSv1: HelloRequest underflow");
  1288. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1289. TLS_ALERT_DECODE_ERROR);
  1290. return -1;
  1291. }
  1292. wpa_printf(MSG_DEBUG, "TLSv1: Ignored HelloRequest");
  1293. *len = 4 + hr_len;
  1294. return 0;
  1295. }
  1296. switch (conn->state) {
  1297. case SERVER_HELLO:
  1298. if (tls_process_server_hello(conn, ct, buf, len))
  1299. return -1;
  1300. break;
  1301. case SERVER_CERTIFICATE:
  1302. if (tls_process_certificate(conn, ct, buf, len))
  1303. return -1;
  1304. break;
  1305. case SERVER_KEY_EXCHANGE:
  1306. if (tls_process_server_key_exchange(conn, ct, buf, len))
  1307. return -1;
  1308. break;
  1309. case SERVER_CERTIFICATE_REQUEST:
  1310. if (tls_process_certificate_request(conn, ct, buf, len))
  1311. return -1;
  1312. break;
  1313. case SERVER_HELLO_DONE:
  1314. if (tls_process_server_hello_done(conn, ct, buf, len))
  1315. return -1;
  1316. break;
  1317. case SERVER_CHANGE_CIPHER_SPEC:
  1318. if (tls_process_server_change_cipher_spec(conn, ct, buf, len))
  1319. return -1;
  1320. break;
  1321. case SERVER_FINISHED:
  1322. if (tls_process_server_finished(conn, ct, buf, len))
  1323. return -1;
  1324. break;
  1325. case ACK_FINISHED:
  1326. if (out_data &&
  1327. tls_process_application_data(conn, ct, buf, len, out_data,
  1328. out_len))
  1329. return -1;
  1330. break;
  1331. default:
  1332. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d "
  1333. "while processing received message",
  1334. conn->state);
  1335. return -1;
  1336. }
  1337. if (ct == TLS_CONTENT_TYPE_HANDSHAKE)
  1338. tls_verify_hash_add(&conn->verify, buf, *len);
  1339. return 0;
  1340. }