tlsv1_server_write.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * TLSv1 server - write handshake message
  3. * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto/md5.h"
  17. #include "crypto/sha1.h"
  18. #include "crypto/tls.h"
  19. #include "x509v3.h"
  20. #include "tlsv1_common.h"
  21. #include "tlsv1_record.h"
  22. #include "tlsv1_server.h"
  23. #include "tlsv1_server_i.h"
  24. static size_t tls_server_cert_chain_der_len(struct tlsv1_server *conn)
  25. {
  26. size_t len = 0;
  27. struct x509_certificate *cert;
  28. cert = conn->cred->cert;
  29. while (cert) {
  30. len += 3 + cert->cert_len;
  31. if (x509_certificate_self_signed(cert))
  32. break;
  33. cert = x509_certificate_get_subject(conn->cred->trusted_certs,
  34. &cert->issuer);
  35. }
  36. return len;
  37. }
  38. static int tls_write_server_hello(struct tlsv1_server *conn,
  39. u8 **msgpos, u8 *end)
  40. {
  41. u8 *pos, *rhdr, *hs_start, *hs_length;
  42. struct os_time now;
  43. size_t rlen;
  44. pos = *msgpos;
  45. wpa_printf(MSG_DEBUG, "TLSv1: Send ServerHello");
  46. rhdr = pos;
  47. pos += TLS_RECORD_HEADER_LEN;
  48. os_get_time(&now);
  49. WPA_PUT_BE32(conn->server_random, now.sec);
  50. if (os_get_random(conn->server_random + 4, TLS_RANDOM_LEN - 4)) {
  51. wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
  52. "server_random");
  53. return -1;
  54. }
  55. wpa_hexdump(MSG_MSGDUMP, "TLSv1: server_random",
  56. conn->server_random, TLS_RANDOM_LEN);
  57. conn->session_id_len = TLS_SESSION_ID_MAX_LEN;
  58. if (os_get_random(conn->session_id, conn->session_id_len)) {
  59. wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
  60. "session_id");
  61. return -1;
  62. }
  63. wpa_hexdump(MSG_MSGDUMP, "TLSv1: session_id",
  64. conn->session_id, conn->session_id_len);
  65. /* opaque fragment[TLSPlaintext.length] */
  66. /* Handshake */
  67. hs_start = pos;
  68. /* HandshakeType msg_type */
  69. *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO;
  70. /* uint24 length (to be filled) */
  71. hs_length = pos;
  72. pos += 3;
  73. /* body - ServerHello */
  74. /* ProtocolVersion server_version */
  75. WPA_PUT_BE16(pos, TLS_VERSION);
  76. pos += 2;
  77. /* Random random: uint32 gmt_unix_time, opaque random_bytes */
  78. os_memcpy(pos, conn->server_random, TLS_RANDOM_LEN);
  79. pos += TLS_RANDOM_LEN;
  80. /* SessionID session_id */
  81. *pos++ = conn->session_id_len;
  82. os_memcpy(pos, conn->session_id, conn->session_id_len);
  83. pos += conn->session_id_len;
  84. /* CipherSuite cipher_suite */
  85. WPA_PUT_BE16(pos, conn->cipher_suite);
  86. pos += 2;
  87. /* CompressionMethod compression_method */
  88. *pos++ = TLS_COMPRESSION_NULL;
  89. if (conn->session_ticket && conn->session_ticket_cb) {
  90. int res = conn->session_ticket_cb(
  91. conn->session_ticket_cb_ctx,
  92. conn->session_ticket, conn->session_ticket_len,
  93. conn->client_random, conn->server_random,
  94. conn->master_secret);
  95. if (res < 0) {
  96. wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback "
  97. "indicated failure");
  98. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  99. TLS_ALERT_HANDSHAKE_FAILURE);
  100. return -1;
  101. }
  102. conn->use_session_ticket = res;
  103. if (conn->use_session_ticket) {
  104. if (tlsv1_server_derive_keys(conn, NULL, 0) < 0) {
  105. wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
  106. "derive keys");
  107. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  108. TLS_ALERT_INTERNAL_ERROR);
  109. return -1;
  110. }
  111. }
  112. /*
  113. * RFC 4507 specifies that server would include an empty
  114. * SessionTicket extension in ServerHello and a
  115. * NewSessionTicket message after the ServerHello. However,
  116. * EAP-FAST (RFC 4851), i.e., the only user of SessionTicket
  117. * extension at the moment, does not use such extensions.
  118. *
  119. * TODO: Add support for configuring RFC 4507 behavior and make
  120. * EAP-FAST disable it.
  121. */
  122. }
  123. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  124. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  125. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  126. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  127. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
  128. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  129. TLS_ALERT_INTERNAL_ERROR);
  130. return -1;
  131. }
  132. pos = rhdr + rlen;
  133. *msgpos = pos;
  134. return 0;
  135. }
  136. static int tls_write_server_certificate(struct tlsv1_server *conn,
  137. u8 **msgpos, u8 *end)
  138. {
  139. u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
  140. size_t rlen;
  141. struct x509_certificate *cert;
  142. const struct tls_cipher_suite *suite;
  143. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  144. if (suite && suite->key_exchange == TLS_KEY_X_DH_anon) {
  145. wpa_printf(MSG_DEBUG, "TLSv1: Do not send Certificate when "
  146. "using anonymous DH");
  147. return 0;
  148. }
  149. pos = *msgpos;
  150. wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
  151. rhdr = pos;
  152. pos += TLS_RECORD_HEADER_LEN;
  153. /* opaque fragment[TLSPlaintext.length] */
  154. /* Handshake */
  155. hs_start = pos;
  156. /* HandshakeType msg_type */
  157. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
  158. /* uint24 length (to be filled) */
  159. hs_length = pos;
  160. pos += 3;
  161. /* body - Certificate */
  162. /* uint24 length (to be filled) */
  163. cert_start = pos;
  164. pos += 3;
  165. cert = conn->cred->cert;
  166. while (cert) {
  167. if (pos + 3 + cert->cert_len > end) {
  168. wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
  169. "for Certificate (cert_len=%lu left=%lu)",
  170. (unsigned long) cert->cert_len,
  171. (unsigned long) (end - pos));
  172. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  173. TLS_ALERT_INTERNAL_ERROR);
  174. return -1;
  175. }
  176. WPA_PUT_BE24(pos, cert->cert_len);
  177. pos += 3;
  178. os_memcpy(pos, cert->cert_start, cert->cert_len);
  179. pos += cert->cert_len;
  180. if (x509_certificate_self_signed(cert))
  181. break;
  182. cert = x509_certificate_get_subject(conn->cred->trusted_certs,
  183. &cert->issuer);
  184. }
  185. if (cert == conn->cred->cert || cert == NULL) {
  186. /*
  187. * Server was not configured with all the needed certificates
  188. * to form a full certificate chain. The client may fail to
  189. * validate the chain unless it is configured with all the
  190. * missing CA certificates.
  191. */
  192. wpa_printf(MSG_DEBUG, "TLSv1: Full server certificate chain "
  193. "not configured - validation may fail");
  194. }
  195. WPA_PUT_BE24(cert_start, pos - cert_start - 3);
  196. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  197. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  198. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  199. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  200. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  201. TLS_ALERT_INTERNAL_ERROR);
  202. return -1;
  203. }
  204. pos = rhdr + rlen;
  205. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  206. *msgpos = pos;
  207. return 0;
  208. }
  209. static int tls_write_server_key_exchange(struct tlsv1_server *conn,
  210. u8 **msgpos, u8 *end)
  211. {
  212. tls_key_exchange keyx;
  213. const struct tls_cipher_suite *suite;
  214. u8 *pos, *rhdr, *hs_start, *hs_length;
  215. size_t rlen;
  216. u8 *dh_ys;
  217. size_t dh_ys_len;
  218. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  219. if (suite == NULL)
  220. keyx = TLS_KEY_X_NULL;
  221. else
  222. keyx = suite->key_exchange;
  223. if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
  224. wpa_printf(MSG_DEBUG, "TLSv1: No ServerKeyExchange needed");
  225. return 0;
  226. }
  227. if (keyx != TLS_KEY_X_DH_anon) {
  228. /* TODO? */
  229. wpa_printf(MSG_DEBUG, "TLSv1: ServerKeyExchange not yet "
  230. "supported with key exchange type %d", keyx);
  231. return -1;
  232. }
  233. if (conn->cred == NULL || conn->cred->dh_p == NULL ||
  234. conn->cred->dh_g == NULL) {
  235. wpa_printf(MSG_DEBUG, "TLSv1: No DH parameters available for "
  236. "ServerKeyExhcange");
  237. return -1;
  238. }
  239. os_free(conn->dh_secret);
  240. conn->dh_secret_len = conn->cred->dh_p_len;
  241. conn->dh_secret = os_malloc(conn->dh_secret_len);
  242. if (conn->dh_secret == NULL) {
  243. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  244. "memory for secret (Diffie-Hellman)");
  245. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  246. TLS_ALERT_INTERNAL_ERROR);
  247. return -1;
  248. }
  249. if (os_get_random(conn->dh_secret, conn->dh_secret_len)) {
  250. wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
  251. "data for Diffie-Hellman");
  252. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  253. TLS_ALERT_INTERNAL_ERROR);
  254. os_free(conn->dh_secret);
  255. conn->dh_secret = NULL;
  256. return -1;
  257. }
  258. if (os_memcmp(conn->dh_secret, conn->cred->dh_p, conn->dh_secret_len) >
  259. 0)
  260. conn->dh_secret[0] = 0; /* make sure secret < p */
  261. pos = conn->dh_secret;
  262. while (pos + 1 < conn->dh_secret + conn->dh_secret_len && *pos == 0)
  263. pos++;
  264. if (pos != conn->dh_secret) {
  265. os_memmove(conn->dh_secret, pos,
  266. conn->dh_secret_len - (pos - conn->dh_secret));
  267. conn->dh_secret_len -= pos - conn->dh_secret;
  268. }
  269. wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH server's secret value",
  270. conn->dh_secret, conn->dh_secret_len);
  271. /* Ys = g^secret mod p */
  272. dh_ys_len = conn->cred->dh_p_len;
  273. dh_ys = os_malloc(dh_ys_len);
  274. if (dh_ys == NULL) {
  275. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate memory for "
  276. "Diffie-Hellman");
  277. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  278. TLS_ALERT_INTERNAL_ERROR);
  279. return -1;
  280. }
  281. if (crypto_mod_exp(conn->cred->dh_g, conn->cred->dh_g_len,
  282. conn->dh_secret, conn->dh_secret_len,
  283. conn->cred->dh_p, conn->cred->dh_p_len,
  284. dh_ys, &dh_ys_len)) {
  285. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  286. TLS_ALERT_INTERNAL_ERROR);
  287. os_free(dh_ys);
  288. return -1;
  289. }
  290. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
  291. dh_ys, dh_ys_len);
  292. /*
  293. * struct {
  294. * select (KeyExchangeAlgorithm) {
  295. * case diffie_hellman:
  296. * ServerDHParams params;
  297. * Signature signed_params;
  298. * case rsa:
  299. * ServerRSAParams params;
  300. * Signature signed_params;
  301. * };
  302. * } ServerKeyExchange;
  303. *
  304. * struct {
  305. * opaque dh_p<1..2^16-1>;
  306. * opaque dh_g<1..2^16-1>;
  307. * opaque dh_Ys<1..2^16-1>;
  308. * } ServerDHParams;
  309. */
  310. pos = *msgpos;
  311. wpa_printf(MSG_DEBUG, "TLSv1: Send ServerKeyExchange");
  312. rhdr = pos;
  313. pos += TLS_RECORD_HEADER_LEN;
  314. /* opaque fragment[TLSPlaintext.length] */
  315. /* Handshake */
  316. hs_start = pos;
  317. /* HandshakeType msg_type */
  318. *pos++ = TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE;
  319. /* uint24 length (to be filled) */
  320. hs_length = pos;
  321. pos += 3;
  322. /* body - ServerDHParams */
  323. /* dh_p */
  324. if (pos + 2 + conn->cred->dh_p_len > end) {
  325. wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
  326. "dh_p");
  327. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  328. TLS_ALERT_INTERNAL_ERROR);
  329. os_free(dh_ys);
  330. return -1;
  331. }
  332. WPA_PUT_BE16(pos, conn->cred->dh_p_len);
  333. pos += 2;
  334. os_memcpy(pos, conn->cred->dh_p, conn->cred->dh_p_len);
  335. pos += conn->cred->dh_p_len;
  336. /* dh_g */
  337. if (pos + 2 + conn->cred->dh_g_len > end) {
  338. wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
  339. "dh_g");
  340. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  341. TLS_ALERT_INTERNAL_ERROR);
  342. os_free(dh_ys);
  343. return -1;
  344. }
  345. WPA_PUT_BE16(pos, conn->cred->dh_g_len);
  346. pos += 2;
  347. os_memcpy(pos, conn->cred->dh_g, conn->cred->dh_g_len);
  348. pos += conn->cred->dh_g_len;
  349. /* dh_Ys */
  350. if (pos + 2 + dh_ys_len > end) {
  351. wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space for "
  352. "dh_Ys");
  353. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  354. TLS_ALERT_INTERNAL_ERROR);
  355. os_free(dh_ys);
  356. return -1;
  357. }
  358. WPA_PUT_BE16(pos, dh_ys_len);
  359. pos += 2;
  360. os_memcpy(pos, dh_ys, dh_ys_len);
  361. pos += dh_ys_len;
  362. os_free(dh_ys);
  363. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  364. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  365. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  366. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  367. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  368. TLS_ALERT_INTERNAL_ERROR);
  369. return -1;
  370. }
  371. pos = rhdr + rlen;
  372. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  373. *msgpos = pos;
  374. return 0;
  375. }
  376. static int tls_write_server_certificate_request(struct tlsv1_server *conn,
  377. u8 **msgpos, u8 *end)
  378. {
  379. u8 *pos, *rhdr, *hs_start, *hs_length;
  380. size_t rlen;
  381. if (!conn->verify_peer) {
  382. wpa_printf(MSG_DEBUG, "TLSv1: No CertificateRequest needed");
  383. return 0;
  384. }
  385. pos = *msgpos;
  386. wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateRequest");
  387. rhdr = pos;
  388. pos += TLS_RECORD_HEADER_LEN;
  389. /* opaque fragment[TLSPlaintext.length] */
  390. /* Handshake */
  391. hs_start = pos;
  392. /* HandshakeType msg_type */
  393. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST;
  394. /* uint24 length (to be filled) */
  395. hs_length = pos;
  396. pos += 3;
  397. /* body - CertificateRequest */
  398. /*
  399. * enum {
  400. * rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
  401. * (255)
  402. * } ClientCertificateType;
  403. * ClientCertificateType certificate_types<1..2^8-1>
  404. */
  405. *pos++ = 1;
  406. *pos++ = 1; /* rsa_sign */
  407. /*
  408. * opaque DistinguishedName<1..2^16-1>
  409. * DistinguishedName certificate_authorities<3..2^16-1>
  410. */
  411. /* TODO: add support for listing DNs for trusted CAs */
  412. WPA_PUT_BE16(pos, 0);
  413. pos += 2;
  414. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  415. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  416. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  417. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  418. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  419. TLS_ALERT_INTERNAL_ERROR);
  420. return -1;
  421. }
  422. pos = rhdr + rlen;
  423. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  424. *msgpos = pos;
  425. return 0;
  426. }
  427. static int tls_write_server_hello_done(struct tlsv1_server *conn,
  428. u8 **msgpos, u8 *end)
  429. {
  430. u8 *pos, *rhdr, *hs_start, *hs_length;
  431. size_t rlen;
  432. pos = *msgpos;
  433. wpa_printf(MSG_DEBUG, "TLSv1: Send ServerHelloDone");
  434. rhdr = pos;
  435. pos += TLS_RECORD_HEADER_LEN;
  436. /* opaque fragment[TLSPlaintext.length] */
  437. /* Handshake */
  438. hs_start = pos;
  439. /* HandshakeType msg_type */
  440. *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE;
  441. /* uint24 length (to be filled) */
  442. hs_length = pos;
  443. pos += 3;
  444. /* body - ServerHelloDone (empty) */
  445. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  446. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  447. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  448. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  449. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  450. TLS_ALERT_INTERNAL_ERROR);
  451. return -1;
  452. }
  453. pos = rhdr + rlen;
  454. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  455. *msgpos = pos;
  456. return 0;
  457. }
  458. static int tls_write_server_change_cipher_spec(struct tlsv1_server *conn,
  459. u8 **msgpos, u8 *end)
  460. {
  461. u8 *pos, *rhdr;
  462. size_t rlen;
  463. pos = *msgpos;
  464. wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
  465. rhdr = pos;
  466. pos += TLS_RECORD_HEADER_LEN;
  467. *pos = TLS_CHANGE_CIPHER_SPEC;
  468. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
  469. rhdr, end - rhdr, 1, &rlen) < 0) {
  470. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  471. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  472. TLS_ALERT_INTERNAL_ERROR);
  473. return -1;
  474. }
  475. if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
  476. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
  477. "record layer");
  478. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  479. TLS_ALERT_INTERNAL_ERROR);
  480. return -1;
  481. }
  482. *msgpos = rhdr + rlen;
  483. return 0;
  484. }
  485. static int tls_write_server_finished(struct tlsv1_server *conn,
  486. u8 **msgpos, u8 *end)
  487. {
  488. u8 *pos, *rhdr, *hs_start, *hs_length;
  489. size_t rlen, hlen;
  490. u8 verify_data[TLS_VERIFY_DATA_LEN];
  491. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  492. pos = *msgpos;
  493. wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
  494. /* Encrypted Handshake Message: Finished */
  495. hlen = MD5_MAC_LEN;
  496. if (conn->verify.md5_server == NULL ||
  497. crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
  498. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  499. TLS_ALERT_INTERNAL_ERROR);
  500. conn->verify.md5_server = NULL;
  501. crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
  502. conn->verify.sha1_server = NULL;
  503. return -1;
  504. }
  505. conn->verify.md5_server = NULL;
  506. hlen = SHA1_MAC_LEN;
  507. if (conn->verify.sha1_server == NULL ||
  508. crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
  509. &hlen) < 0) {
  510. conn->verify.sha1_server = NULL;
  511. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  512. TLS_ALERT_INTERNAL_ERROR);
  513. return -1;
  514. }
  515. conn->verify.sha1_server = NULL;
  516. if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
  517. "server finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
  518. verify_data, TLS_VERIFY_DATA_LEN)) {
  519. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
  520. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  521. TLS_ALERT_INTERNAL_ERROR);
  522. return -1;
  523. }
  524. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
  525. verify_data, TLS_VERIFY_DATA_LEN);
  526. rhdr = pos;
  527. pos += TLS_RECORD_HEADER_LEN;
  528. /* Handshake */
  529. hs_start = pos;
  530. /* HandshakeType msg_type */
  531. *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
  532. /* uint24 length (to be filled) */
  533. hs_length = pos;
  534. pos += 3;
  535. os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
  536. pos += TLS_VERIFY_DATA_LEN;
  537. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  538. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  539. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  540. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  541. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  542. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  543. TLS_ALERT_INTERNAL_ERROR);
  544. return -1;
  545. }
  546. pos = rhdr + rlen;
  547. *msgpos = pos;
  548. return 0;
  549. }
  550. static u8 * tls_send_server_hello(struct tlsv1_server *conn, size_t *out_len)
  551. {
  552. u8 *msg, *end, *pos;
  553. size_t msglen;
  554. *out_len = 0;
  555. msglen = 1000 + tls_server_cert_chain_der_len(conn);
  556. msg = os_malloc(msglen);
  557. if (msg == NULL)
  558. return NULL;
  559. pos = msg;
  560. end = msg + msglen;
  561. if (tls_write_server_hello(conn, &pos, end) < 0) {
  562. os_free(msg);
  563. return NULL;
  564. }
  565. if (conn->use_session_ticket) {
  566. /* Abbreviated handshake using session ticket; RFC 4507 */
  567. if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
  568. tls_write_server_finished(conn, &pos, end) < 0) {
  569. os_free(msg);
  570. return NULL;
  571. }
  572. *out_len = pos - msg;
  573. conn->state = CHANGE_CIPHER_SPEC;
  574. return msg;
  575. }
  576. /* Full handshake */
  577. if (tls_write_server_certificate(conn, &pos, end) < 0 ||
  578. tls_write_server_key_exchange(conn, &pos, end) < 0 ||
  579. tls_write_server_certificate_request(conn, &pos, end) < 0 ||
  580. tls_write_server_hello_done(conn, &pos, end) < 0) {
  581. os_free(msg);
  582. return NULL;
  583. }
  584. *out_len = pos - msg;
  585. conn->state = CLIENT_CERTIFICATE;
  586. return msg;
  587. }
  588. static u8 * tls_send_change_cipher_spec(struct tlsv1_server *conn,
  589. size_t *out_len)
  590. {
  591. u8 *msg, *end, *pos;
  592. *out_len = 0;
  593. msg = os_malloc(1000);
  594. if (msg == NULL)
  595. return NULL;
  596. pos = msg;
  597. end = msg + 1000;
  598. if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
  599. tls_write_server_finished(conn, &pos, end) < 0) {
  600. os_free(msg);
  601. return NULL;
  602. }
  603. *out_len = pos - msg;
  604. wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed successfully");
  605. conn->state = ESTABLISHED;
  606. return msg;
  607. }
  608. u8 * tlsv1_server_handshake_write(struct tlsv1_server *conn, size_t *out_len)
  609. {
  610. switch (conn->state) {
  611. case SERVER_HELLO:
  612. return tls_send_server_hello(conn, out_len);
  613. case SERVER_CHANGE_CIPHER_SPEC:
  614. return tls_send_change_cipher_spec(conn, out_len);
  615. default:
  616. if (conn->state == ESTABLISHED && conn->use_session_ticket) {
  617. /* Abbreviated handshake was already completed. */
  618. return NULL;
  619. }
  620. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
  621. "generating reply", conn->state);
  622. return NULL;
  623. }
  624. }
  625. u8 * tlsv1_server_send_alert(struct tlsv1_server *conn, u8 level,
  626. u8 description, size_t *out_len)
  627. {
  628. u8 *alert, *pos, *length;
  629. wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
  630. *out_len = 0;
  631. alert = os_malloc(10);
  632. if (alert == NULL)
  633. return NULL;
  634. pos = alert;
  635. /* TLSPlaintext */
  636. /* ContentType type */
  637. *pos++ = TLS_CONTENT_TYPE_ALERT;
  638. /* ProtocolVersion version */
  639. WPA_PUT_BE16(pos, TLS_VERSION);
  640. pos += 2;
  641. /* uint16 length (to be filled) */
  642. length = pos;
  643. pos += 2;
  644. /* opaque fragment[TLSPlaintext.length] */
  645. /* Alert */
  646. /* AlertLevel level */
  647. *pos++ = level;
  648. /* AlertDescription description */
  649. *pos++ = description;
  650. WPA_PUT_BE16(length, pos - length - 2);
  651. *out_len = pos - alert;
  652. return alert;
  653. }