tlsv1_client_read.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  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 || !cert)
  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_memdup(pos, conn->dh_p_len);
  594. if (conn->dh_p == NULL)
  595. goto fail;
  596. pos += conn->dh_p_len;
  597. wpa_hexdump(MSG_DEBUG, "TLSv1: DH p (prime)",
  598. conn->dh_p, conn->dh_p_len);
  599. if (end - pos < 3)
  600. goto fail;
  601. val = WPA_GET_BE16(pos);
  602. pos += 2;
  603. if (val == 0 || val > (size_t) (end - pos))
  604. goto fail;
  605. conn->dh_g_len = val;
  606. conn->dh_g = os_memdup(pos, conn->dh_g_len);
  607. if (conn->dh_g == NULL)
  608. goto fail;
  609. pos += conn->dh_g_len;
  610. wpa_hexdump(MSG_DEBUG, "TLSv1: DH g (generator)",
  611. conn->dh_g, conn->dh_g_len);
  612. if (conn->dh_g_len == 1 && conn->dh_g[0] < 2)
  613. goto fail;
  614. if (end - pos < 3)
  615. goto fail;
  616. val = WPA_GET_BE16(pos);
  617. pos += 2;
  618. if (val == 0 || val > (size_t) (end - pos))
  619. goto fail;
  620. conn->dh_ys_len = val;
  621. conn->dh_ys = os_memdup(pos, conn->dh_ys_len);
  622. if (conn->dh_ys == NULL)
  623. goto fail;
  624. pos += conn->dh_ys_len;
  625. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
  626. conn->dh_ys, conn->dh_ys_len);
  627. server_params_end = pos;
  628. if (key_exchange == TLS_KEY_X_DHE_RSA) {
  629. u8 hash[64];
  630. int hlen;
  631. if (conn->rl.tls_version == TLS_VERSION_1_2) {
  632. #ifdef CONFIG_TLSV12
  633. /*
  634. * RFC 5246, 4.7:
  635. * TLS v1.2 adds explicit indication of the used
  636. * signature and hash algorithms.
  637. *
  638. * struct {
  639. * HashAlgorithm hash;
  640. * SignatureAlgorithm signature;
  641. * } SignatureAndHashAlgorithm;
  642. */
  643. if (end - pos < 2)
  644. goto fail;
  645. if ((pos[0] != TLS_HASH_ALG_SHA256 &&
  646. pos[0] != TLS_HASH_ALG_SHA384 &&
  647. pos[0] != TLS_HASH_ALG_SHA512) ||
  648. pos[1] != TLS_SIGN_ALG_RSA) {
  649. wpa_printf(MSG_DEBUG, "TLSv1.2: Unsupported hash(%u)/signature(%u) algorithm",
  650. pos[0], pos[1]);
  651. goto fail;
  652. }
  653. hlen = tlsv12_key_x_server_params_hash(
  654. conn->rl.tls_version, pos[0],
  655. conn->client_random,
  656. conn->server_random, server_params,
  657. server_params_end - server_params, hash);
  658. pos += 2;
  659. #else /* CONFIG_TLSV12 */
  660. goto fail;
  661. #endif /* CONFIG_TLSV12 */
  662. } else {
  663. hlen = tls_key_x_server_params_hash(
  664. conn->rl.tls_version, conn->client_random,
  665. conn->server_random, server_params,
  666. server_params_end - server_params, hash);
  667. }
  668. if (hlen < 0)
  669. goto fail;
  670. wpa_hexdump(MSG_MSGDUMP, "TLSv1: ServerKeyExchange hash",
  671. hash, hlen);
  672. if (tls_verify_signature(conn->rl.tls_version,
  673. conn->server_rsa_key,
  674. hash, hlen, pos, end - pos,
  675. &alert) < 0)
  676. goto fail;
  677. }
  678. return 0;
  679. fail:
  680. wpa_printf(MSG_DEBUG, "TLSv1: Processing DH params failed");
  681. tlsv1_client_free_dh(conn);
  682. return -1;
  683. }
  684. static enum tls_ocsp_result
  685. tls_process_certificate_status_ocsp_response(struct tlsv1_client *conn,
  686. const u8 *pos, size_t len)
  687. {
  688. const u8 *end = pos + len;
  689. u32 ocsp_resp_len;
  690. /* opaque OCSPResponse<1..2^24-1>; */
  691. if (end - pos < 3) {
  692. wpa_printf(MSG_INFO, "TLSv1: Too short OCSPResponse");
  693. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  694. return TLS_OCSP_INVALID;
  695. }
  696. ocsp_resp_len = WPA_GET_BE24(pos);
  697. pos += 3;
  698. if (end - pos < ocsp_resp_len) {
  699. wpa_printf(MSG_INFO, "TLSv1: Truncated OCSPResponse");
  700. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  701. return TLS_OCSP_INVALID;
  702. }
  703. return tls_process_ocsp_response(conn, pos, ocsp_resp_len);
  704. }
  705. static int tls_process_certificate_status(struct tlsv1_client *conn, u8 ct,
  706. const u8 *in_data, size_t *in_len)
  707. {
  708. const u8 *pos, *end;
  709. size_t left, len;
  710. u8 type, status_type;
  711. enum tls_ocsp_result res;
  712. struct x509_certificate *cert;
  713. int depth;
  714. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  715. wpa_printf(MSG_DEBUG,
  716. "TLSv1: Expected Handshake; received content type 0x%x",
  717. ct);
  718. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  719. TLS_ALERT_UNEXPECTED_MESSAGE);
  720. return -1;
  721. }
  722. pos = in_data;
  723. left = *in_len;
  724. if (left < 4) {
  725. wpa_printf(MSG_DEBUG,
  726. "TLSv1: Too short CertificateStatus (left=%lu)",
  727. (unsigned long) left);
  728. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  729. return -1;
  730. }
  731. type = *pos++;
  732. len = WPA_GET_BE24(pos);
  733. pos += 3;
  734. left -= 4;
  735. if (len > left) {
  736. wpa_printf(MSG_DEBUG,
  737. "TLSv1: Mismatch in CertificateStatus length (len=%lu != left=%lu)",
  738. (unsigned long) len, (unsigned long) left);
  739. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  740. return -1;
  741. }
  742. end = pos + len;
  743. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_STATUS) {
  744. wpa_printf(MSG_DEBUG,
  745. "TLSv1: Received unexpected handshake message %d (expected CertificateStatus)",
  746. type);
  747. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  748. TLS_ALERT_UNEXPECTED_MESSAGE);
  749. return -1;
  750. }
  751. wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateStatus");
  752. /*
  753. * struct {
  754. * CertificateStatusType status_type;
  755. * select (status_type) {
  756. * case ocsp: OCSPResponse;
  757. * case ocsp_multi: OCSPResponseList;
  758. * } response;
  759. * } CertificateStatus;
  760. */
  761. if (end - pos < 1) {
  762. wpa_printf(MSG_INFO, "TLSv1: Too short CertificateStatus");
  763. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  764. return -1;
  765. }
  766. status_type = *pos++;
  767. wpa_printf(MSG_DEBUG, "TLSv1: CertificateStatus status_type %u",
  768. status_type);
  769. if (status_type == 1 /* ocsp */) {
  770. res = tls_process_certificate_status_ocsp_response(
  771. conn, pos, end - pos);
  772. } else if (status_type == 2 /* ocsp_multi */) {
  773. int good = 0, revoked = 0;
  774. u32 resp_len;
  775. res = TLS_OCSP_NO_RESPONSE;
  776. /*
  777. * opaque OCSPResponse<0..2^24-1>;
  778. *
  779. * struct {
  780. * OCSPResponse ocsp_response_list<1..2^24-1>;
  781. * } OCSPResponseList;
  782. */
  783. if (end - pos < 3) {
  784. wpa_printf(MSG_DEBUG,
  785. "TLSv1: Truncated OCSPResponseList");
  786. res = TLS_OCSP_INVALID;
  787. goto done;
  788. }
  789. resp_len = WPA_GET_BE24(pos);
  790. pos += 3;
  791. if (end - pos < resp_len) {
  792. wpa_printf(MSG_DEBUG,
  793. "TLSv1: Truncated OCSPResponseList(len=%u)",
  794. resp_len);
  795. res = TLS_OCSP_INVALID;
  796. goto done;
  797. }
  798. end = pos + resp_len;
  799. while (end - pos >= 3) {
  800. resp_len = WPA_GET_BE24(pos);
  801. pos += 3;
  802. if (resp_len > end - pos) {
  803. wpa_printf(MSG_DEBUG,
  804. "TLSv1: Truncated OCSPResponse(len=%u; left=%d) in ocsp_multi",
  805. resp_len, (int) (end - pos));
  806. res = TLS_OCSP_INVALID;
  807. break;
  808. }
  809. if (!resp_len)
  810. continue; /* Skip an empty response */
  811. res = tls_process_certificate_status_ocsp_response(
  812. conn, pos - 3, resp_len + 3);
  813. if (res == TLS_OCSP_REVOKED)
  814. revoked++;
  815. else if (res == TLS_OCSP_GOOD)
  816. good++;
  817. pos += resp_len;
  818. }
  819. if (revoked)
  820. res = TLS_OCSP_REVOKED;
  821. else if (good)
  822. res = TLS_OCSP_GOOD;
  823. } else {
  824. wpa_printf(MSG_DEBUG,
  825. "TLSv1: Ignore unsupported CertificateStatus");
  826. goto skip;
  827. }
  828. done:
  829. if (res == TLS_OCSP_REVOKED) {
  830. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  831. TLS_ALERT_CERTIFICATE_REVOKED);
  832. for (cert = conn->server_cert, depth = 0; cert;
  833. cert = cert->next, depth++) {
  834. if (cert->ocsp_revoked) {
  835. tls_cert_chain_failure_event(
  836. conn, depth, cert, TLS_FAIL_REVOKED,
  837. "certificate revoked");
  838. }
  839. }
  840. return -1;
  841. }
  842. if (conn->flags & TLS_CONN_REQUIRE_OCSP_ALL) {
  843. /*
  844. * Verify that each certificate on the chain that is not part
  845. * of the trusted certificates has a good status. If not,
  846. * terminate handshake.
  847. */
  848. for (cert = conn->server_cert, depth = 0; cert;
  849. cert = cert->next, depth++) {
  850. if (!cert->ocsp_good) {
  851. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  852. TLS_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE);
  853. tls_cert_chain_failure_event(
  854. conn, depth, cert,
  855. TLS_FAIL_UNSPECIFIED,
  856. "bad certificate status response");
  857. return -1;
  858. }
  859. if (cert->issuer_trusted)
  860. break;
  861. }
  862. }
  863. if ((conn->flags & TLS_CONN_REQUIRE_OCSP) && res != TLS_OCSP_GOOD) {
  864. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  865. res == TLS_OCSP_INVALID ? TLS_ALERT_DECODE_ERROR :
  866. TLS_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE);
  867. if (conn->server_cert)
  868. tls_cert_chain_failure_event(
  869. conn, 0, conn->server_cert,
  870. TLS_FAIL_UNSPECIFIED,
  871. "bad certificate status response");
  872. return -1;
  873. }
  874. conn->ocsp_resp_received = 1;
  875. skip:
  876. *in_len = end - in_data;
  877. conn->state = SERVER_KEY_EXCHANGE;
  878. return 0;
  879. }
  880. static int tls_process_server_key_exchange(struct tlsv1_client *conn, u8 ct,
  881. const u8 *in_data, size_t *in_len)
  882. {
  883. const u8 *pos, *end;
  884. size_t left, len;
  885. u8 type;
  886. const struct tls_cipher_suite *suite;
  887. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  888. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  889. "received content type 0x%x", ct);
  890. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  891. TLS_ALERT_UNEXPECTED_MESSAGE);
  892. return -1;
  893. }
  894. pos = in_data;
  895. left = *in_len;
  896. if (left < 4) {
  897. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerKeyExchange "
  898. "(Left=%lu)", (unsigned long) left);
  899. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  900. return -1;
  901. }
  902. type = *pos++;
  903. len = WPA_GET_BE24(pos);
  904. pos += 3;
  905. left -= 4;
  906. if (len > left) {
  907. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerKeyExchange "
  908. "length (len=%lu != left=%lu)",
  909. (unsigned long) len, (unsigned long) left);
  910. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  911. return -1;
  912. }
  913. end = pos + len;
  914. if ((conn->flags & TLS_CONN_REQUEST_OCSP) &&
  915. type == TLS_HANDSHAKE_TYPE_CERTIFICATE_STATUS)
  916. return tls_process_certificate_status(conn, ct, in_data,
  917. in_len);
  918. if (type == TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST)
  919. return tls_process_certificate_request(conn, ct, in_data,
  920. in_len);
  921. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  922. return tls_process_server_hello_done(conn, ct, in_data,
  923. in_len);
  924. if (type != TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE) {
  925. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  926. "message %d (expected ServerKeyExchange/"
  927. "CertificateRequest/ServerHelloDone%s)", type,
  928. (conn->flags & TLS_CONN_REQUEST_OCSP) ?
  929. "/CertificateStatus" : "");
  930. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  931. TLS_ALERT_UNEXPECTED_MESSAGE);
  932. return -1;
  933. }
  934. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerKeyExchange");
  935. if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
  936. wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not allowed "
  937. "with the selected cipher suite");
  938. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  939. TLS_ALERT_UNEXPECTED_MESSAGE);
  940. return -1;
  941. }
  942. wpa_hexdump(MSG_DEBUG, "TLSv1: ServerKeyExchange", pos, len);
  943. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  944. if (suite && (suite->key_exchange == TLS_KEY_X_DH_anon ||
  945. suite->key_exchange == TLS_KEY_X_DHE_RSA)) {
  946. if (tlsv1_process_diffie_hellman(conn, pos, len,
  947. suite->key_exchange) < 0) {
  948. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  949. TLS_ALERT_DECODE_ERROR);
  950. return -1;
  951. }
  952. } else {
  953. wpa_printf(MSG_DEBUG, "TLSv1: UnexpectedServerKeyExchange");
  954. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  955. TLS_ALERT_UNEXPECTED_MESSAGE);
  956. return -1;
  957. }
  958. *in_len = end - in_data;
  959. conn->state = SERVER_CERTIFICATE_REQUEST;
  960. return 0;
  961. }
  962. static int tls_process_certificate_request(struct tlsv1_client *conn, u8 ct,
  963. const u8 *in_data, size_t *in_len)
  964. {
  965. const u8 *pos, *end;
  966. size_t left, len;
  967. u8 type;
  968. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  969. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  970. "received content type 0x%x", ct);
  971. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  972. TLS_ALERT_UNEXPECTED_MESSAGE);
  973. return -1;
  974. }
  975. pos = in_data;
  976. left = *in_len;
  977. if (left < 4) {
  978. wpa_printf(MSG_DEBUG, "TLSv1: Too short CertificateRequest "
  979. "(left=%lu)", (unsigned long) left);
  980. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  981. return -1;
  982. }
  983. type = *pos++;
  984. len = WPA_GET_BE24(pos);
  985. pos += 3;
  986. left -= 4;
  987. if (len > left) {
  988. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in CertificateRequest "
  989. "length (len=%lu != left=%lu)",
  990. (unsigned long) len, (unsigned long) left);
  991. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  992. return -1;
  993. }
  994. end = pos + len;
  995. if (type == TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE)
  996. return tls_process_server_hello_done(conn, ct, in_data,
  997. in_len);
  998. if (type != TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST) {
  999. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  1000. "message %d (expected CertificateRequest/"
  1001. "ServerHelloDone)", type);
  1002. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1003. TLS_ALERT_UNEXPECTED_MESSAGE);
  1004. return -1;
  1005. }
  1006. wpa_printf(MSG_DEBUG, "TLSv1: Received CertificateRequest");
  1007. conn->certificate_requested = 1;
  1008. *in_len = end - in_data;
  1009. conn->state = SERVER_HELLO_DONE;
  1010. return 0;
  1011. }
  1012. static int tls_process_server_hello_done(struct tlsv1_client *conn, u8 ct,
  1013. const u8 *in_data, size_t *in_len)
  1014. {
  1015. const u8 *pos, *end;
  1016. size_t left, len;
  1017. u8 type;
  1018. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  1019. wpa_printf(MSG_DEBUG, "TLSv1: Expected Handshake; "
  1020. "received content type 0x%x", ct);
  1021. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1022. TLS_ALERT_UNEXPECTED_MESSAGE);
  1023. return -1;
  1024. }
  1025. pos = in_data;
  1026. left = *in_len;
  1027. if (left < 4) {
  1028. wpa_printf(MSG_DEBUG, "TLSv1: Too short ServerHelloDone "
  1029. "(left=%lu)", (unsigned long) left);
  1030. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  1031. return -1;
  1032. }
  1033. type = *pos++;
  1034. len = WPA_GET_BE24(pos);
  1035. pos += 3;
  1036. left -= 4;
  1037. if (len > left) {
  1038. wpa_printf(MSG_DEBUG, "TLSv1: Mismatch in ServerHelloDone "
  1039. "length (len=%lu != left=%lu)",
  1040. (unsigned long) len, (unsigned long) left);
  1041. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  1042. return -1;
  1043. }
  1044. end = pos + len;
  1045. if (type != TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE) {
  1046. wpa_printf(MSG_DEBUG, "TLSv1: Received unexpected handshake "
  1047. "message %d (expected ServerHelloDone)", type);
  1048. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1049. TLS_ALERT_UNEXPECTED_MESSAGE);
  1050. return -1;
  1051. }
  1052. wpa_printf(MSG_DEBUG, "TLSv1: Received ServerHelloDone");
  1053. if ((conn->flags & TLS_CONN_REQUIRE_OCSP) &&
  1054. !conn->ocsp_resp_received) {
  1055. wpa_printf(MSG_INFO,
  1056. "TLSv1: No OCSP response received - reject handshake");
  1057. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1058. TLS_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE);
  1059. return -1;
  1060. }
  1061. *in_len = end - in_data;
  1062. conn->state = CLIENT_KEY_EXCHANGE;
  1063. return 0;
  1064. }
  1065. static int tls_process_server_change_cipher_spec(struct tlsv1_client *conn,
  1066. u8 ct, const u8 *in_data,
  1067. size_t *in_len)
  1068. {
  1069. const u8 *pos;
  1070. size_t left;
  1071. if (ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC) {
  1072. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  1073. "received content type 0x%x", ct);
  1074. if (conn->use_session_ticket) {
  1075. int res;
  1076. wpa_printf(MSG_DEBUG, "TLSv1: Server may have "
  1077. "rejected SessionTicket");
  1078. conn->use_session_ticket = 0;
  1079. /* Notify upper layers that SessionTicket failed */
  1080. res = conn->session_ticket_cb(
  1081. conn->session_ticket_cb_ctx, NULL, 0, NULL,
  1082. NULL, NULL);
  1083. if (res < 0) {
  1084. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket "
  1085. "callback indicated failure");
  1086. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1087. TLS_ALERT_HANDSHAKE_FAILURE);
  1088. return -1;
  1089. }
  1090. conn->state = SERVER_CERTIFICATE;
  1091. return tls_process_certificate(conn, ct, in_data,
  1092. in_len);
  1093. }
  1094. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1095. TLS_ALERT_UNEXPECTED_MESSAGE);
  1096. return -1;
  1097. }
  1098. pos = in_data;
  1099. left = *in_len;
  1100. if (left < 1) {
  1101. wpa_printf(MSG_DEBUG, "TLSv1: Too short ChangeCipherSpec");
  1102. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_DECODE_ERROR);
  1103. return -1;
  1104. }
  1105. if (*pos != TLS_CHANGE_CIPHER_SPEC) {
  1106. wpa_printf(MSG_DEBUG, "TLSv1: Expected ChangeCipherSpec; "
  1107. "received data 0x%x", *pos);
  1108. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1109. TLS_ALERT_UNEXPECTED_MESSAGE);
  1110. return -1;
  1111. }
  1112. wpa_printf(MSG_DEBUG, "TLSv1: Received ChangeCipherSpec");
  1113. if (tlsv1_record_change_read_cipher(&conn->rl) < 0) {
  1114. wpa_printf(MSG_DEBUG, "TLSv1: Failed to change read cipher "
  1115. "for record layer");
  1116. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1117. TLS_ALERT_INTERNAL_ERROR);
  1118. return -1;
  1119. }
  1120. *in_len = pos + 1 - in_data;
  1121. conn->state = SERVER_FINISHED;
  1122. return 0;
  1123. }
  1124. static int tls_process_server_finished(struct tlsv1_client *conn, u8 ct,
  1125. const u8 *in_data, size_t *in_len)
  1126. {
  1127. const u8 *pos, *end;
  1128. size_t left, len, hlen;
  1129. u8 verify_data[TLS_VERIFY_DATA_LEN];
  1130. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  1131. if (ct != TLS_CONTENT_TYPE_HANDSHAKE) {
  1132. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; "
  1133. "received content type 0x%x", ct);
  1134. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1135. TLS_ALERT_UNEXPECTED_MESSAGE);
  1136. return -1;
  1137. }
  1138. pos = in_data;
  1139. left = *in_len;
  1140. if (left < 4) {
  1141. wpa_printf(MSG_DEBUG, "TLSv1: Too short record (left=%lu) for "
  1142. "Finished",
  1143. (unsigned long) left);
  1144. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1145. TLS_ALERT_DECODE_ERROR);
  1146. return -1;
  1147. }
  1148. if (pos[0] != TLS_HANDSHAKE_TYPE_FINISHED) {
  1149. wpa_printf(MSG_DEBUG, "TLSv1: Expected Finished; received "
  1150. "type 0x%x", pos[0]);
  1151. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1152. TLS_ALERT_UNEXPECTED_MESSAGE);
  1153. return -1;
  1154. }
  1155. len = WPA_GET_BE24(pos + 1);
  1156. pos += 4;
  1157. left -= 4;
  1158. if (len > left) {
  1159. wpa_printf(MSG_DEBUG, "TLSv1: Too short buffer for Finished "
  1160. "(len=%lu > left=%lu)",
  1161. (unsigned long) len, (unsigned long) left);
  1162. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1163. TLS_ALERT_DECODE_ERROR);
  1164. return -1;
  1165. }
  1166. end = pos + len;
  1167. if (len != TLS_VERIFY_DATA_LEN) {
  1168. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected verify_data length "
  1169. "in Finished: %lu (expected %d)",
  1170. (unsigned long) len, TLS_VERIFY_DATA_LEN);
  1171. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1172. TLS_ALERT_DECODE_ERROR);
  1173. return -1;
  1174. }
  1175. wpa_hexdump(MSG_MSGDUMP, "TLSv1: verify_data in Finished",
  1176. pos, TLS_VERIFY_DATA_LEN);
  1177. #ifdef CONFIG_TLSV12
  1178. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  1179. hlen = SHA256_MAC_LEN;
  1180. if (conn->verify.sha256_server == NULL ||
  1181. crypto_hash_finish(conn->verify.sha256_server, hash, &hlen)
  1182. < 0) {
  1183. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1184. TLS_ALERT_INTERNAL_ERROR);
  1185. conn->verify.sha256_server = NULL;
  1186. return -1;
  1187. }
  1188. conn->verify.sha256_server = NULL;
  1189. } else {
  1190. #endif /* CONFIG_TLSV12 */
  1191. hlen = MD5_MAC_LEN;
  1192. if (conn->verify.md5_server == NULL ||
  1193. crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
  1194. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1195. TLS_ALERT_INTERNAL_ERROR);
  1196. conn->verify.md5_server = NULL;
  1197. crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
  1198. conn->verify.sha1_server = NULL;
  1199. return -1;
  1200. }
  1201. conn->verify.md5_server = NULL;
  1202. hlen = SHA1_MAC_LEN;
  1203. if (conn->verify.sha1_server == NULL ||
  1204. crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
  1205. &hlen) < 0) {
  1206. conn->verify.sha1_server = NULL;
  1207. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1208. TLS_ALERT_INTERNAL_ERROR);
  1209. return -1;
  1210. }
  1211. conn->verify.sha1_server = NULL;
  1212. hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
  1213. #ifdef CONFIG_TLSV12
  1214. }
  1215. #endif /* CONFIG_TLSV12 */
  1216. if (tls_prf(conn->rl.tls_version,
  1217. conn->master_secret, TLS_MASTER_SECRET_LEN,
  1218. "server finished", hash, hlen,
  1219. verify_data, TLS_VERIFY_DATA_LEN)) {
  1220. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive verify_data");
  1221. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1222. TLS_ALERT_DECRYPT_ERROR);
  1223. return -1;
  1224. }
  1225. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
  1226. verify_data, TLS_VERIFY_DATA_LEN);
  1227. if (os_memcmp_const(pos, verify_data, TLS_VERIFY_DATA_LEN) != 0) {
  1228. wpa_printf(MSG_INFO, "TLSv1: Mismatch in verify_data");
  1229. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1230. TLS_ALERT_DECRYPT_ERROR);
  1231. return -1;
  1232. }
  1233. wpa_printf(MSG_DEBUG, "TLSv1: Received Finished");
  1234. *in_len = end - in_data;
  1235. conn->state = (conn->session_resumed || conn->use_session_ticket) ?
  1236. CHANGE_CIPHER_SPEC : ACK_FINISHED;
  1237. return 0;
  1238. }
  1239. static int tls_process_application_data(struct tlsv1_client *conn, u8 ct,
  1240. const u8 *in_data, size_t *in_len,
  1241. u8 **out_data, size_t *out_len)
  1242. {
  1243. const u8 *pos;
  1244. size_t left;
  1245. if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
  1246. wpa_printf(MSG_DEBUG, "TLSv1: Expected Application Data; "
  1247. "received content type 0x%x", ct);
  1248. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1249. TLS_ALERT_UNEXPECTED_MESSAGE);
  1250. return -1;
  1251. }
  1252. pos = in_data;
  1253. left = *in_len;
  1254. wpa_hexdump(MSG_DEBUG, "TLSv1: Application Data included in Handshake",
  1255. pos, left);
  1256. *out_data = os_malloc(left);
  1257. if (*out_data) {
  1258. os_memcpy(*out_data, pos, left);
  1259. *out_len = left;
  1260. }
  1261. return 0;
  1262. }
  1263. int tlsv1_client_process_handshake(struct tlsv1_client *conn, u8 ct,
  1264. const u8 *buf, size_t *len,
  1265. u8 **out_data, size_t *out_len)
  1266. {
  1267. if (ct == TLS_CONTENT_TYPE_ALERT) {
  1268. if (*len < 2) {
  1269. wpa_printf(MSG_DEBUG, "TLSv1: Alert underflow");
  1270. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1271. TLS_ALERT_DECODE_ERROR);
  1272. return -1;
  1273. }
  1274. wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
  1275. buf[0], buf[1]);
  1276. *len = 2;
  1277. conn->state = FAILED;
  1278. return -1;
  1279. }
  1280. if (ct == TLS_CONTENT_TYPE_HANDSHAKE && *len >= 4 &&
  1281. buf[0] == TLS_HANDSHAKE_TYPE_HELLO_REQUEST) {
  1282. size_t hr_len = WPA_GET_BE24(buf + 1);
  1283. if (hr_len > *len - 4) {
  1284. wpa_printf(MSG_DEBUG, "TLSv1: HelloRequest underflow");
  1285. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  1286. TLS_ALERT_DECODE_ERROR);
  1287. return -1;
  1288. }
  1289. wpa_printf(MSG_DEBUG, "TLSv1: Ignored HelloRequest");
  1290. *len = 4 + hr_len;
  1291. return 0;
  1292. }
  1293. switch (conn->state) {
  1294. case SERVER_HELLO:
  1295. if (tls_process_server_hello(conn, ct, buf, len))
  1296. return -1;
  1297. break;
  1298. case SERVER_CERTIFICATE:
  1299. if (tls_process_certificate(conn, ct, buf, len))
  1300. return -1;
  1301. break;
  1302. case SERVER_KEY_EXCHANGE:
  1303. if (tls_process_server_key_exchange(conn, ct, buf, len))
  1304. return -1;
  1305. break;
  1306. case SERVER_CERTIFICATE_REQUEST:
  1307. if (tls_process_certificate_request(conn, ct, buf, len))
  1308. return -1;
  1309. break;
  1310. case SERVER_HELLO_DONE:
  1311. if (tls_process_server_hello_done(conn, ct, buf, len))
  1312. return -1;
  1313. break;
  1314. case SERVER_CHANGE_CIPHER_SPEC:
  1315. if (tls_process_server_change_cipher_spec(conn, ct, buf, len))
  1316. return -1;
  1317. break;
  1318. case SERVER_FINISHED:
  1319. if (tls_process_server_finished(conn, ct, buf, len))
  1320. return -1;
  1321. break;
  1322. case ACK_FINISHED:
  1323. if (out_data &&
  1324. tls_process_application_data(conn, ct, buf, len, out_data,
  1325. out_len))
  1326. return -1;
  1327. break;
  1328. default:
  1329. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d "
  1330. "while processing received message",
  1331. conn->state);
  1332. return -1;
  1333. }
  1334. if (ct == TLS_CONTENT_TYPE_HANDSHAKE)
  1335. tls_verify_hash_add(&conn->verify, buf, *len);
  1336. return 0;
  1337. }