tlsv1_server_write.c 20 KB

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