tlsv1_server_write.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. * TLSv1 server - write handshake message
  3. * Copyright (c) 2006-2014, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/md5.h"
  11. #include "crypto/sha1.h"
  12. #include "crypto/sha256.h"
  13. #include "crypto/tls.h"
  14. #include "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. tlsv1_server_log(conn, "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. tlsv1_server_log(conn, "SessionTicket callback indicated failure");
  93. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  94. TLS_ALERT_HANDSHAKE_FAILURE);
  95. return -1;
  96. }
  97. conn->use_session_ticket = res;
  98. if (conn->use_session_ticket) {
  99. if (tlsv1_server_derive_keys(conn, NULL, 0) < 0) {
  100. wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
  101. "derive keys");
  102. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  103. TLS_ALERT_INTERNAL_ERROR);
  104. return -1;
  105. }
  106. }
  107. /*
  108. * RFC 4507 specifies that server would include an empty
  109. * SessionTicket extension in ServerHello and a
  110. * NewSessionTicket message after the ServerHello. However,
  111. * EAP-FAST (RFC 4851), i.e., the only user of SessionTicket
  112. * extension at the moment, does not use such extensions.
  113. *
  114. * TODO: Add support for configuring RFC 4507 behavior and make
  115. * EAP-FAST disable it.
  116. */
  117. }
  118. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  119. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  120. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  121. rhdr, end - rhdr, hs_start, pos - hs_start,
  122. &rlen) < 0) {
  123. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
  124. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  125. TLS_ALERT_INTERNAL_ERROR);
  126. return -1;
  127. }
  128. pos = rhdr + rlen;
  129. *msgpos = pos;
  130. return 0;
  131. }
  132. static int tls_write_server_certificate(struct tlsv1_server *conn,
  133. u8 **msgpos, u8 *end)
  134. {
  135. u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
  136. size_t rlen;
  137. struct x509_certificate *cert;
  138. const struct tls_cipher_suite *suite;
  139. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  140. if (suite && suite->key_exchange == TLS_KEY_X_DH_anon) {
  141. wpa_printf(MSG_DEBUG, "TLSv1: Do not send Certificate when "
  142. "using anonymous DH");
  143. return 0;
  144. }
  145. pos = *msgpos;
  146. tlsv1_server_log(conn, "Send Certificate");
  147. rhdr = pos;
  148. pos += TLS_RECORD_HEADER_LEN;
  149. /* opaque fragment[TLSPlaintext.length] */
  150. /* Handshake */
  151. hs_start = pos;
  152. /* HandshakeType msg_type */
  153. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
  154. /* uint24 length (to be filled) */
  155. hs_length = pos;
  156. pos += 3;
  157. /* body - Certificate */
  158. /* uint24 length (to be filled) */
  159. cert_start = pos;
  160. pos += 3;
  161. cert = conn->cred->cert;
  162. while (cert) {
  163. if (pos + 3 + cert->cert_len > end) {
  164. wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
  165. "for Certificate (cert_len=%lu left=%lu)",
  166. (unsigned long) cert->cert_len,
  167. (unsigned long) (end - pos));
  168. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  169. TLS_ALERT_INTERNAL_ERROR);
  170. return -1;
  171. }
  172. WPA_PUT_BE24(pos, cert->cert_len);
  173. pos += 3;
  174. os_memcpy(pos, cert->cert_start, cert->cert_len);
  175. pos += cert->cert_len;
  176. if (x509_certificate_self_signed(cert))
  177. break;
  178. cert = x509_certificate_get_subject(conn->cred->trusted_certs,
  179. &cert->issuer);
  180. }
  181. if (cert == conn->cred->cert || cert == NULL) {
  182. /*
  183. * Server was not configured with all the needed certificates
  184. * to form a full certificate chain. The client may fail to
  185. * validate the chain unless it is configured with all the
  186. * missing CA certificates.
  187. */
  188. wpa_printf(MSG_DEBUG, "TLSv1: Full server certificate chain "
  189. "not configured - validation may fail");
  190. }
  191. WPA_PUT_BE24(cert_start, pos - cert_start - 3);
  192. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  193. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  194. rhdr, end - rhdr, hs_start, pos - hs_start,
  195. &rlen) < 0) {
  196. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  197. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  198. TLS_ALERT_INTERNAL_ERROR);
  199. return -1;
  200. }
  201. pos = rhdr + rlen;
  202. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  203. *msgpos = pos;
  204. return 0;
  205. }
  206. static int tls_write_server_key_exchange(struct tlsv1_server *conn,
  207. u8 **msgpos, u8 *end)
  208. {
  209. tls_key_exchange keyx;
  210. const struct tls_cipher_suite *suite;
  211. u8 *pos, *rhdr, *hs_start, *hs_length, *server_params;
  212. size_t rlen;
  213. u8 *dh_ys;
  214. size_t dh_ys_len;
  215. const u8 *dh_p;
  216. size_t dh_p_len;
  217. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  218. if (suite == NULL)
  219. keyx = TLS_KEY_X_NULL;
  220. else
  221. keyx = suite->key_exchange;
  222. if (!tls_server_key_exchange_allowed(conn->rl.cipher_suite)) {
  223. wpa_printf(MSG_DEBUG, "TLSv1: No ServerKeyExchange needed");
  224. return 0;
  225. }
  226. if (keyx != TLS_KEY_X_DH_anon && keyx != TLS_KEY_X_DHE_RSA) {
  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. tlsv1_server_get_dh_p(conn, &dh_p, &dh_p_len);
  238. os_free(conn->dh_secret);
  239. conn->dh_secret_len = dh_p_len;
  240. conn->dh_secret = os_malloc(conn->dh_secret_len);
  241. if (conn->dh_secret == NULL) {
  242. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  243. "memory for secret (Diffie-Hellman)");
  244. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  245. TLS_ALERT_INTERNAL_ERROR);
  246. return -1;
  247. }
  248. if (random_get_bytes(conn->dh_secret, conn->dh_secret_len)) {
  249. wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
  250. "data for Diffie-Hellman");
  251. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  252. TLS_ALERT_INTERNAL_ERROR);
  253. os_free(conn->dh_secret);
  254. conn->dh_secret = NULL;
  255. return -1;
  256. }
  257. if (os_memcmp(conn->dh_secret, dh_p, conn->dh_secret_len) > 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 = 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. dh_p, dh_p_len, dh_ys, &dh_ys_len)) {
  282. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  283. TLS_ALERT_INTERNAL_ERROR);
  284. os_free(dh_ys);
  285. return -1;
  286. }
  287. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Ys (server's public value)",
  288. dh_ys, dh_ys_len);
  289. /*
  290. * struct {
  291. * select (KeyExchangeAlgorithm) {
  292. * case diffie_hellman:
  293. * ServerDHParams params;
  294. * Signature signed_params;
  295. * case rsa:
  296. * ServerRSAParams params;
  297. * Signature signed_params;
  298. * };
  299. * } ServerKeyExchange;
  300. *
  301. * struct {
  302. * opaque dh_p<1..2^16-1>;
  303. * opaque dh_g<1..2^16-1>;
  304. * opaque dh_Ys<1..2^16-1>;
  305. * } ServerDHParams;
  306. */
  307. pos = *msgpos;
  308. tlsv1_server_log(conn, "Send ServerKeyExchange");
  309. rhdr = pos;
  310. pos += TLS_RECORD_HEADER_LEN;
  311. /* opaque fragment[TLSPlaintext.length] */
  312. /* Handshake */
  313. hs_start = pos;
  314. /* HandshakeType msg_type */
  315. *pos++ = TLS_HANDSHAKE_TYPE_SERVER_KEY_EXCHANGE;
  316. /* uint24 length (to be filled) */
  317. hs_length = pos;
  318. pos += 3;
  319. /* body - ServerDHParams */
  320. server_params = pos;
  321. /* dh_p */
  322. if (pos + 2 + 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, dh_p_len);
  331. pos += 2;
  332. os_memcpy(pos, dh_p, dh_p_len);
  333. pos += 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. /*
  362. * select (SignatureAlgorithm)
  363. * { case anonymous: struct { };
  364. * case rsa:
  365. * digitally-signed struct {
  366. * opaque md5_hash[16];
  367. * opaque sha_hash[20];
  368. * };
  369. * case dsa:
  370. * digitally-signed struct {
  371. * opaque sha_hash[20];
  372. * };
  373. * } Signature;
  374. *
  375. * md5_hash
  376. * MD5(ClientHello.random + ServerHello.random + ServerParams);
  377. *
  378. * sha_hash
  379. * SHA(ClientHello.random + ServerHello.random + ServerParams);
  380. */
  381. if (keyx == TLS_KEY_X_DHE_RSA) {
  382. u8 hash[100];
  383. u8 *signed_start;
  384. size_t clen;
  385. int hlen;
  386. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  387. #ifdef CONFIG_TLSV12
  388. hlen = tlsv12_key_x_server_params_hash(
  389. conn->rl.tls_version, conn->client_random,
  390. conn->server_random, server_params,
  391. pos - server_params, hash + 19);
  392. /*
  393. * RFC 5246, 4.7:
  394. * TLS v1.2 adds explicit indication of the used
  395. * signature and hash algorithms.
  396. *
  397. * struct {
  398. * HashAlgorithm hash;
  399. * SignatureAlgorithm signature;
  400. * } SignatureAndHashAlgorithm;
  401. */
  402. if (hlen < 0 || pos + 2 > end) {
  403. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  404. TLS_ALERT_INTERNAL_ERROR);
  405. return -1;
  406. }
  407. *pos++ = TLS_HASH_ALG_SHA256;
  408. *pos++ = TLS_SIGN_ALG_RSA;
  409. /*
  410. * RFC 3447, A.2.4 RSASSA-PKCS1-v1_5
  411. *
  412. * DigestInfo ::= SEQUENCE {
  413. * digestAlgorithm DigestAlgorithm,
  414. * digest OCTET STRING
  415. * }
  416. *
  417. * SHA-256 OID: sha256WithRSAEncryption ::= {pkcs-1 11}
  418. *
  419. * DER encoded DigestInfo for SHA256 per RFC 3447:
  420. * 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00
  421. * 04 20 || H
  422. */
  423. hlen += 19;
  424. os_memcpy(hash,
  425. "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65"
  426. "\x03\x04\x02\x01\x05\x00\x04\x20", 19);
  427. #else /* CONFIG_TLSV12 */
  428. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  429. TLS_ALERT_INTERNAL_ERROR);
  430. return -1;
  431. #endif /* CONFIG_TLSV12 */
  432. } else {
  433. hlen = tls_key_x_server_params_hash(
  434. conn->rl.tls_version, conn->client_random,
  435. conn->server_random, server_params,
  436. pos - server_params, hash);
  437. }
  438. if (hlen < 0) {
  439. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  440. TLS_ALERT_INTERNAL_ERROR);
  441. return -1;
  442. }
  443. wpa_hexdump(MSG_MSGDUMP, "TLS: ServerKeyExchange signed_params hash",
  444. hash, hlen);
  445. #ifdef CONFIG_TESTING_OPTIONS
  446. if (conn->test_flags & TLS_BREAK_SRV_KEY_X_HASH) {
  447. tlsv1_server_log(conn, "TESTING: Break ServerKeyExchange signed params hash");
  448. hash[hlen - 1] ^= 0x80;
  449. }
  450. #endif /* CONFIG_TESTING_OPTIONS */
  451. /*
  452. * RFC 2246, 4.7:
  453. * In digital signing, one-way hash functions are used as input
  454. * for a signing algorithm. A digitally-signed element is
  455. * encoded as an opaque vector <0..2^16-1>, where the length is
  456. * specified by the signing algorithm and key.
  457. *
  458. * In RSA signing, a 36-byte structure of two hashes (one SHA
  459. * and one MD5) is signed (encrypted with the private key). It
  460. * is encoded with PKCS #1 block type 0 or type 1 as described
  461. * in [PKCS1].
  462. */
  463. signed_start = pos; /* length to be filled */
  464. pos += 2;
  465. clen = end - pos;
  466. if (conn->cred == NULL ||
  467. crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
  468. pos, &clen) < 0) {
  469. wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
  470. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  471. TLS_ALERT_INTERNAL_ERROR);
  472. return -1;
  473. }
  474. WPA_PUT_BE16(signed_start, clen);
  475. #ifdef CONFIG_TESTING_OPTIONS
  476. if (conn->test_flags & TLS_BREAK_SRV_KEY_X_SIGNATURE) {
  477. tlsv1_server_log(conn, "TESTING: Break ServerKeyExchange signed params signature");
  478. pos[clen - 1] ^= 0x80;
  479. }
  480. #endif /* CONFIG_TESTING_OPTIONS */
  481. pos += clen;
  482. }
  483. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  484. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  485. rhdr, end - rhdr, hs_start, pos - hs_start,
  486. &rlen) < 0) {
  487. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  488. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  489. TLS_ALERT_INTERNAL_ERROR);
  490. return -1;
  491. }
  492. pos = rhdr + rlen;
  493. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  494. *msgpos = pos;
  495. return 0;
  496. }
  497. static int tls_write_server_certificate_request(struct tlsv1_server *conn,
  498. u8 **msgpos, u8 *end)
  499. {
  500. u8 *pos, *rhdr, *hs_start, *hs_length;
  501. size_t rlen;
  502. if (!conn->verify_peer) {
  503. wpa_printf(MSG_DEBUG, "TLSv1: No CertificateRequest needed");
  504. return 0;
  505. }
  506. pos = *msgpos;
  507. tlsv1_server_log(conn, "Send CertificateRequest");
  508. rhdr = pos;
  509. pos += TLS_RECORD_HEADER_LEN;
  510. /* opaque fragment[TLSPlaintext.length] */
  511. /* Handshake */
  512. hs_start = pos;
  513. /* HandshakeType msg_type */
  514. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_REQUEST;
  515. /* uint24 length (to be filled) */
  516. hs_length = pos;
  517. pos += 3;
  518. /* body - CertificateRequest */
  519. /*
  520. * enum {
  521. * rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
  522. * (255)
  523. * } ClientCertificateType;
  524. * ClientCertificateType certificate_types<1..2^8-1>
  525. */
  526. *pos++ = 1;
  527. *pos++ = 1; /* rsa_sign */
  528. /*
  529. * opaque DistinguishedName<1..2^16-1>
  530. * DistinguishedName certificate_authorities<3..2^16-1>
  531. */
  532. /* TODO: add support for listing DNs for trusted CAs */
  533. WPA_PUT_BE16(pos, 0);
  534. pos += 2;
  535. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  536. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  537. rhdr, end - rhdr, hs_start, pos - hs_start,
  538. &rlen) < 0) {
  539. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  540. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  541. TLS_ALERT_INTERNAL_ERROR);
  542. return -1;
  543. }
  544. pos = rhdr + rlen;
  545. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  546. *msgpos = pos;
  547. return 0;
  548. }
  549. static int tls_write_server_hello_done(struct tlsv1_server *conn,
  550. u8 **msgpos, u8 *end)
  551. {
  552. u8 *pos;
  553. size_t rlen;
  554. u8 payload[4];
  555. tlsv1_server_log(conn, "Send ServerHelloDone");
  556. /* opaque fragment[TLSPlaintext.length] */
  557. /* Handshake */
  558. pos = payload;
  559. /* HandshakeType msg_type */
  560. *pos++ = TLS_HANDSHAKE_TYPE_SERVER_HELLO_DONE;
  561. /* uint24 length */
  562. WPA_PUT_BE24(pos, 0);
  563. pos += 3;
  564. /* body - ServerHelloDone (empty) */
  565. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  566. *msgpos, end - *msgpos, payload, pos - payload,
  567. &rlen) < 0) {
  568. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  569. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  570. TLS_ALERT_INTERNAL_ERROR);
  571. return -1;
  572. }
  573. tls_verify_hash_add(&conn->verify, payload, pos - payload);
  574. *msgpos += rlen;
  575. return 0;
  576. }
  577. static int tls_write_server_change_cipher_spec(struct tlsv1_server *conn,
  578. u8 **msgpos, u8 *end)
  579. {
  580. size_t rlen;
  581. u8 payload[1];
  582. tlsv1_server_log(conn, "Send ChangeCipherSpec");
  583. payload[0] = TLS_CHANGE_CIPHER_SPEC;
  584. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
  585. *msgpos, end - *msgpos, payload, sizeof(payload),
  586. &rlen) < 0) {
  587. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  588. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  589. TLS_ALERT_INTERNAL_ERROR);
  590. return -1;
  591. }
  592. if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
  593. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
  594. "record layer");
  595. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  596. TLS_ALERT_INTERNAL_ERROR);
  597. return -1;
  598. }
  599. *msgpos += rlen;
  600. return 0;
  601. }
  602. static int tls_write_server_finished(struct tlsv1_server *conn,
  603. u8 **msgpos, u8 *end)
  604. {
  605. u8 *pos, *hs_start;
  606. size_t rlen, hlen;
  607. u8 verify_data[1 + 3 + TLS_VERIFY_DATA_LEN];
  608. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  609. pos = *msgpos;
  610. tlsv1_server_log(conn, "Send Finished");
  611. /* Encrypted Handshake Message: Finished */
  612. #ifdef CONFIG_TLSV12
  613. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  614. hlen = SHA256_MAC_LEN;
  615. if (conn->verify.sha256_server == NULL ||
  616. crypto_hash_finish(conn->verify.sha256_server, hash, &hlen)
  617. < 0) {
  618. conn->verify.sha256_server = NULL;
  619. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  620. TLS_ALERT_INTERNAL_ERROR);
  621. return -1;
  622. }
  623. conn->verify.sha256_server = NULL;
  624. } else {
  625. #endif /* CONFIG_TLSV12 */
  626. hlen = MD5_MAC_LEN;
  627. if (conn->verify.md5_server == NULL ||
  628. crypto_hash_finish(conn->verify.md5_server, hash, &hlen) < 0) {
  629. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  630. TLS_ALERT_INTERNAL_ERROR);
  631. conn->verify.md5_server = NULL;
  632. crypto_hash_finish(conn->verify.sha1_server, NULL, NULL);
  633. conn->verify.sha1_server = NULL;
  634. return -1;
  635. }
  636. conn->verify.md5_server = NULL;
  637. hlen = SHA1_MAC_LEN;
  638. if (conn->verify.sha1_server == NULL ||
  639. crypto_hash_finish(conn->verify.sha1_server, hash + MD5_MAC_LEN,
  640. &hlen) < 0) {
  641. conn->verify.sha1_server = NULL;
  642. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  643. TLS_ALERT_INTERNAL_ERROR);
  644. return -1;
  645. }
  646. conn->verify.sha1_server = NULL;
  647. hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
  648. #ifdef CONFIG_TLSV12
  649. }
  650. #endif /* CONFIG_TLSV12 */
  651. if (tls_prf(conn->rl.tls_version,
  652. conn->master_secret, TLS_MASTER_SECRET_LEN,
  653. "server finished", hash, hlen,
  654. verify_data + 1 + 3, TLS_VERIFY_DATA_LEN)) {
  655. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
  656. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  657. TLS_ALERT_INTERNAL_ERROR);
  658. return -1;
  659. }
  660. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (server)",
  661. verify_data + 1 + 3, TLS_VERIFY_DATA_LEN);
  662. #ifdef CONFIG_TESTING_OPTIONS
  663. if (conn->test_flags & TLS_BREAK_VERIFY_DATA) {
  664. tlsv1_server_log(conn, "TESTING: Break verify_data (server)");
  665. verify_data[1 + 3 + 1] ^= 0x80;
  666. }
  667. #endif /* CONFIG_TESTING_OPTIONS */
  668. /* Handshake */
  669. pos = hs_start = verify_data;
  670. /* HandshakeType msg_type */
  671. *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
  672. /* uint24 length */
  673. WPA_PUT_BE24(pos, TLS_VERIFY_DATA_LEN);
  674. pos += 3;
  675. pos += TLS_VERIFY_DATA_LEN;
  676. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  677. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  678. *msgpos, end - *msgpos, hs_start, pos - hs_start,
  679. &rlen) < 0) {
  680. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  681. tlsv1_server_alert(conn, TLS_ALERT_LEVEL_FATAL,
  682. TLS_ALERT_INTERNAL_ERROR);
  683. return -1;
  684. }
  685. *msgpos += rlen;
  686. return 0;
  687. }
  688. static u8 * tls_send_server_hello(struct tlsv1_server *conn, size_t *out_len)
  689. {
  690. u8 *msg, *end, *pos;
  691. size_t msglen;
  692. *out_len = 0;
  693. msglen = 1000 + tls_server_cert_chain_der_len(conn);
  694. msg = os_malloc(msglen);
  695. if (msg == NULL)
  696. return NULL;
  697. pos = msg;
  698. end = msg + msglen;
  699. if (tls_write_server_hello(conn, &pos, end) < 0) {
  700. os_free(msg);
  701. return NULL;
  702. }
  703. if (conn->use_session_ticket) {
  704. /* Abbreviated handshake using session ticket; RFC 4507 */
  705. if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
  706. tls_write_server_finished(conn, &pos, end) < 0) {
  707. os_free(msg);
  708. return NULL;
  709. }
  710. *out_len = pos - msg;
  711. conn->state = CHANGE_CIPHER_SPEC;
  712. return msg;
  713. }
  714. /* Full handshake */
  715. if (tls_write_server_certificate(conn, &pos, end) < 0 ||
  716. tls_write_server_key_exchange(conn, &pos, end) < 0 ||
  717. tls_write_server_certificate_request(conn, &pos, end) < 0 ||
  718. tls_write_server_hello_done(conn, &pos, end) < 0) {
  719. os_free(msg);
  720. return NULL;
  721. }
  722. *out_len = pos - msg;
  723. conn->state = CLIENT_CERTIFICATE;
  724. return msg;
  725. }
  726. static u8 * tls_send_change_cipher_spec(struct tlsv1_server *conn,
  727. size_t *out_len)
  728. {
  729. u8 *msg, *end, *pos;
  730. *out_len = 0;
  731. msg = os_malloc(1000);
  732. if (msg == NULL)
  733. return NULL;
  734. pos = msg;
  735. end = msg + 1000;
  736. if (tls_write_server_change_cipher_spec(conn, &pos, end) < 0 ||
  737. tls_write_server_finished(conn, &pos, end) < 0) {
  738. os_free(msg);
  739. return NULL;
  740. }
  741. *out_len = pos - msg;
  742. tlsv1_server_log(conn, "Handshake completed successfully");
  743. conn->state = ESTABLISHED;
  744. return msg;
  745. }
  746. u8 * tlsv1_server_handshake_write(struct tlsv1_server *conn, size_t *out_len)
  747. {
  748. switch (conn->state) {
  749. case SERVER_HELLO:
  750. return tls_send_server_hello(conn, out_len);
  751. case SERVER_CHANGE_CIPHER_SPEC:
  752. return tls_send_change_cipher_spec(conn, out_len);
  753. default:
  754. if (conn->state == ESTABLISHED && conn->use_session_ticket) {
  755. /* Abbreviated handshake was already completed. */
  756. return NULL;
  757. }
  758. tlsv1_server_log(conn, "Unexpected state %d while generating reply",
  759. conn->state);
  760. return NULL;
  761. }
  762. }
  763. u8 * tlsv1_server_send_alert(struct tlsv1_server *conn, u8 level,
  764. u8 description, size_t *out_len)
  765. {
  766. u8 *alert, *pos, *length;
  767. tlsv1_server_log(conn, "Send Alert(%d:%d)", level, description);
  768. *out_len = 0;
  769. alert = os_malloc(10);
  770. if (alert == NULL)
  771. return NULL;
  772. pos = alert;
  773. /* TLSPlaintext */
  774. /* ContentType type */
  775. *pos++ = TLS_CONTENT_TYPE_ALERT;
  776. /* ProtocolVersion version */
  777. WPA_PUT_BE16(pos, conn->rl.tls_version ? conn->rl.tls_version :
  778. TLS_VERSION);
  779. pos += 2;
  780. /* uint16 length (to be filled) */
  781. length = pos;
  782. pos += 2;
  783. /* opaque fragment[TLSPlaintext.length] */
  784. /* Alert */
  785. /* AlertLevel level */
  786. *pos++ = level;
  787. /* AlertDescription description */
  788. *pos++ = description;
  789. WPA_PUT_BE16(length, pos - length - 2);
  790. *out_len = pos - alert;
  791. return alert;
  792. }