tlsv1_client_write.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * TLSv1 client - 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 "md5.h"
  17. #include "sha1.h"
  18. #include "x509v3.h"
  19. #include "tls.h"
  20. #include "tlsv1_common.h"
  21. #include "tlsv1_record.h"
  22. #include "tlsv1_client.h"
  23. #include "tlsv1_client_i.h"
  24. static size_t tls_client_cert_chain_der_len(struct tlsv1_client *conn)
  25. {
  26. size_t len = 0;
  27. struct x509_certificate *cert;
  28. if (conn->cred == NULL)
  29. return 0;
  30. cert = conn->cred->cert;
  31. while (cert) {
  32. len += 3 + cert->cert_len;
  33. if (x509_certificate_self_signed(cert))
  34. break;
  35. cert = x509_certificate_get_subject(conn->cred->trusted_certs,
  36. &cert->issuer);
  37. }
  38. return len;
  39. }
  40. u8 * tls_send_client_hello(struct tlsv1_client *conn, size_t *out_len)
  41. {
  42. u8 *hello, *end, *pos, *hs_length, *hs_start, *rhdr;
  43. struct os_time now;
  44. size_t len, i;
  45. wpa_printf(MSG_DEBUG, "TLSv1: Send ClientHello");
  46. *out_len = 0;
  47. os_get_time(&now);
  48. WPA_PUT_BE32(conn->client_random, now.sec);
  49. if (os_get_random(conn->client_random + 4, TLS_RANDOM_LEN - 4)) {
  50. wpa_printf(MSG_ERROR, "TLSv1: Could not generate "
  51. "client_random");
  52. return NULL;
  53. }
  54. wpa_hexdump(MSG_MSGDUMP, "TLSv1: client_random",
  55. conn->client_random, TLS_RANDOM_LEN);
  56. len = 100 + conn->num_cipher_suites * 2 + conn->client_hello_ext_len;
  57. hello = os_malloc(len);
  58. if (hello == NULL)
  59. return NULL;
  60. end = hello + len;
  61. rhdr = hello;
  62. pos = rhdr + TLS_RECORD_HEADER_LEN;
  63. /* opaque fragment[TLSPlaintext.length] */
  64. /* Handshake */
  65. hs_start = pos;
  66. /* HandshakeType msg_type */
  67. *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_HELLO;
  68. /* uint24 length (to be filled) */
  69. hs_length = pos;
  70. pos += 3;
  71. /* body - ClientHello */
  72. /* ProtocolVersion client_version */
  73. WPA_PUT_BE16(pos, TLS_VERSION);
  74. pos += 2;
  75. /* Random random: uint32 gmt_unix_time, opaque random_bytes */
  76. os_memcpy(pos, conn->client_random, TLS_RANDOM_LEN);
  77. pos += TLS_RANDOM_LEN;
  78. /* SessionID session_id */
  79. *pos++ = conn->session_id_len;
  80. os_memcpy(pos, conn->session_id, conn->session_id_len);
  81. pos += conn->session_id_len;
  82. /* CipherSuite cipher_suites<2..2^16-1> */
  83. WPA_PUT_BE16(pos, 2 * conn->num_cipher_suites);
  84. pos += 2;
  85. for (i = 0; i < conn->num_cipher_suites; i++) {
  86. WPA_PUT_BE16(pos, conn->cipher_suites[i]);
  87. pos += 2;
  88. }
  89. /* CompressionMethod compression_methods<1..2^8-1> */
  90. *pos++ = 1;
  91. *pos++ = TLS_COMPRESSION_NULL;
  92. if (conn->client_hello_ext) {
  93. os_memcpy(pos, conn->client_hello_ext,
  94. conn->client_hello_ext_len);
  95. pos += conn->client_hello_ext_len;
  96. }
  97. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  98. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  99. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  100. rhdr, end - rhdr, pos - hs_start, out_len) < 0) {
  101. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create TLS record");
  102. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  103. TLS_ALERT_INTERNAL_ERROR);
  104. os_free(hello);
  105. return NULL;
  106. }
  107. conn->state = SERVER_HELLO;
  108. return hello;
  109. }
  110. static int tls_write_client_certificate(struct tlsv1_client *conn,
  111. u8 **msgpos, u8 *end)
  112. {
  113. u8 *pos, *rhdr, *hs_start, *hs_length, *cert_start;
  114. size_t rlen;
  115. struct x509_certificate *cert;
  116. pos = *msgpos;
  117. wpa_printf(MSG_DEBUG, "TLSv1: Send Certificate");
  118. rhdr = pos;
  119. pos += TLS_RECORD_HEADER_LEN;
  120. /* opaque fragment[TLSPlaintext.length] */
  121. /* Handshake */
  122. hs_start = pos;
  123. /* HandshakeType msg_type */
  124. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE;
  125. /* uint24 length (to be filled) */
  126. hs_length = pos;
  127. pos += 3;
  128. /* body - Certificate */
  129. /* uint24 length (to be filled) */
  130. cert_start = pos;
  131. pos += 3;
  132. cert = conn->cred ? conn->cred->cert : NULL;
  133. while (cert) {
  134. if (pos + 3 + cert->cert_len > end) {
  135. wpa_printf(MSG_DEBUG, "TLSv1: Not enough buffer space "
  136. "for Certificate (cert_len=%lu left=%lu)",
  137. (unsigned long) cert->cert_len,
  138. (unsigned long) (end - pos));
  139. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  140. TLS_ALERT_INTERNAL_ERROR);
  141. return -1;
  142. }
  143. WPA_PUT_BE24(pos, cert->cert_len);
  144. pos += 3;
  145. os_memcpy(pos, cert->cert_start, cert->cert_len);
  146. pos += cert->cert_len;
  147. if (x509_certificate_self_signed(cert))
  148. break;
  149. cert = x509_certificate_get_subject(conn->cred->trusted_certs,
  150. &cert->issuer);
  151. }
  152. if (conn->cred == NULL || cert == conn->cred->cert || cert == NULL) {
  153. /*
  154. * Client was not configured with all the needed certificates
  155. * to form a full certificate chain. The server may fail to
  156. * validate the chain unless it is configured with all the
  157. * missing CA certificates.
  158. */
  159. wpa_printf(MSG_DEBUG, "TLSv1: Full client certificate chain "
  160. "not configured - validation may fail");
  161. }
  162. WPA_PUT_BE24(cert_start, pos - cert_start - 3);
  163. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  164. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  165. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  166. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  167. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  168. TLS_ALERT_INTERNAL_ERROR);
  169. return -1;
  170. }
  171. pos = rhdr + rlen;
  172. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  173. *msgpos = pos;
  174. return 0;
  175. }
  176. static int tlsv1_key_x_anon_dh(struct tlsv1_client *conn, u8 **pos, u8 *end)
  177. {
  178. #ifdef EAP_FAST
  179. /* ClientDiffieHellmanPublic */
  180. u8 *csecret, *csecret_start, *dh_yc, *shared;
  181. size_t csecret_len, dh_yc_len, shared_len;
  182. csecret_len = conn->dh_p_len;
  183. csecret = os_malloc(csecret_len);
  184. if (csecret == NULL) {
  185. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  186. "memory for Yc (Diffie-Hellman)");
  187. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  188. TLS_ALERT_INTERNAL_ERROR);
  189. return -1;
  190. }
  191. if (os_get_random(csecret, csecret_len)) {
  192. wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
  193. "data for Diffie-Hellman");
  194. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  195. TLS_ALERT_INTERNAL_ERROR);
  196. os_free(csecret);
  197. return -1;
  198. }
  199. if (os_memcmp(csecret, conn->dh_p, csecret_len) > 0)
  200. csecret[0] = 0; /* make sure Yc < p */
  201. csecret_start = csecret;
  202. while (csecret_len > 1 && *csecret_start == 0) {
  203. csecret_start++;
  204. csecret_len--;
  205. }
  206. wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH client's secret value",
  207. csecret_start, csecret_len);
  208. /* Yc = g^csecret mod p */
  209. dh_yc_len = conn->dh_p_len;
  210. dh_yc = os_malloc(dh_yc_len);
  211. if (dh_yc == NULL) {
  212. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  213. "memory for Diffie-Hellman");
  214. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  215. TLS_ALERT_INTERNAL_ERROR);
  216. os_free(csecret);
  217. return -1;
  218. }
  219. if (crypto_mod_exp(conn->dh_g, conn->dh_g_len,
  220. csecret_start, csecret_len,
  221. conn->dh_p, conn->dh_p_len,
  222. dh_yc, &dh_yc_len)) {
  223. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  224. TLS_ALERT_INTERNAL_ERROR);
  225. os_free(csecret);
  226. os_free(dh_yc);
  227. return -1;
  228. }
  229. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
  230. dh_yc, dh_yc_len);
  231. WPA_PUT_BE16(*pos, dh_yc_len);
  232. *pos += 2;
  233. if (*pos + dh_yc_len > end) {
  234. wpa_printf(MSG_DEBUG, "TLSv1: Not enough room in the "
  235. "message buffer for Yc");
  236. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  237. TLS_ALERT_INTERNAL_ERROR);
  238. os_free(csecret);
  239. os_free(dh_yc);
  240. return -1;
  241. }
  242. os_memcpy(*pos, dh_yc, dh_yc_len);
  243. *pos += dh_yc_len;
  244. os_free(dh_yc);
  245. shared_len = conn->dh_p_len;
  246. shared = os_malloc(shared_len);
  247. if (shared == NULL) {
  248. wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
  249. "DH");
  250. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  251. TLS_ALERT_INTERNAL_ERROR);
  252. os_free(csecret);
  253. return -1;
  254. }
  255. /* shared = Ys^csecret mod p */
  256. if (crypto_mod_exp(conn->dh_ys, conn->dh_ys_len,
  257. csecret_start, csecret_len,
  258. conn->dh_p, conn->dh_p_len,
  259. shared, &shared_len)) {
  260. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  261. TLS_ALERT_INTERNAL_ERROR);
  262. os_free(csecret);
  263. os_free(shared);
  264. return -1;
  265. }
  266. wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
  267. shared, shared_len);
  268. os_memset(csecret_start, 0, csecret_len);
  269. os_free(csecret);
  270. if (tls_derive_keys(conn, shared, shared_len)) {
  271. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  272. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  273. TLS_ALERT_INTERNAL_ERROR);
  274. os_free(shared);
  275. return -1;
  276. }
  277. os_memset(shared, 0, shared_len);
  278. os_free(shared);
  279. tlsv1_client_free_dh(conn);
  280. return 0;
  281. #else /* EAP_FAST */
  282. tls_alert(conn, TLS_ALERT_LEVEL_FATAL, TLS_ALERT_INTERNAL_ERROR);
  283. return -1;
  284. #endif /* EAP_FAST */
  285. }
  286. static int tlsv1_key_x_rsa(struct tlsv1_client *conn, u8 **pos, u8 *end)
  287. {
  288. u8 pre_master_secret[TLS_PRE_MASTER_SECRET_LEN];
  289. size_t clen;
  290. int res;
  291. if (tls_derive_pre_master_secret(pre_master_secret) < 0 ||
  292. tls_derive_keys(conn, pre_master_secret,
  293. TLS_PRE_MASTER_SECRET_LEN)) {
  294. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  295. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  296. TLS_ALERT_INTERNAL_ERROR);
  297. return -1;
  298. }
  299. /* EncryptedPreMasterSecret */
  300. if (conn->server_rsa_key == NULL) {
  301. wpa_printf(MSG_DEBUG, "TLSv1: No server RSA key to "
  302. "use for encrypting pre-master secret");
  303. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  304. TLS_ALERT_INTERNAL_ERROR);
  305. return -1;
  306. }
  307. /* RSA encrypted value is encoded with PKCS #1 v1.5 block type 2. */
  308. *pos += 2;
  309. clen = end - *pos;
  310. res = crypto_public_key_encrypt_pkcs1_v15(
  311. conn->server_rsa_key,
  312. pre_master_secret, TLS_PRE_MASTER_SECRET_LEN,
  313. *pos, &clen);
  314. os_memset(pre_master_secret, 0, TLS_PRE_MASTER_SECRET_LEN);
  315. if (res < 0) {
  316. wpa_printf(MSG_DEBUG, "TLSv1: RSA encryption failed");
  317. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  318. TLS_ALERT_INTERNAL_ERROR);
  319. return -1;
  320. }
  321. WPA_PUT_BE16(*pos - 2, clen);
  322. wpa_hexdump(MSG_MSGDUMP, "TLSv1: Encrypted pre_master_secret",
  323. *pos, clen);
  324. *pos += clen;
  325. return 0;
  326. }
  327. static int tls_write_client_key_exchange(struct tlsv1_client *conn,
  328. u8 **msgpos, u8 *end)
  329. {
  330. u8 *pos, *rhdr, *hs_start, *hs_length;
  331. size_t rlen;
  332. tls_key_exchange keyx;
  333. const struct tls_cipher_suite *suite;
  334. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  335. if (suite == NULL)
  336. keyx = TLS_KEY_X_NULL;
  337. else
  338. keyx = suite->key_exchange;
  339. pos = *msgpos;
  340. wpa_printf(MSG_DEBUG, "TLSv1: Send ClientKeyExchange");
  341. rhdr = pos;
  342. pos += TLS_RECORD_HEADER_LEN;
  343. /* opaque fragment[TLSPlaintext.length] */
  344. /* Handshake */
  345. hs_start = pos;
  346. /* HandshakeType msg_type */
  347. *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE;
  348. /* uint24 length (to be filled) */
  349. hs_length = pos;
  350. pos += 3;
  351. /* body - ClientKeyExchange */
  352. if (keyx == TLS_KEY_X_DH_anon) {
  353. if (tlsv1_key_x_anon_dh(conn, &pos, end) < 0)
  354. return -1;
  355. } else {
  356. if (tlsv1_key_x_rsa(conn, &pos, end) < 0)
  357. return -1;
  358. }
  359. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  360. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  361. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  362. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  363. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  364. TLS_ALERT_INTERNAL_ERROR);
  365. return -1;
  366. }
  367. pos = rhdr + rlen;
  368. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  369. *msgpos = pos;
  370. return 0;
  371. }
  372. static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
  373. u8 **msgpos, u8 *end)
  374. {
  375. u8 *pos, *rhdr, *hs_start, *hs_length, *signed_start;
  376. size_t rlen, hlen, clen;
  377. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos;
  378. enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
  379. pos = *msgpos;
  380. wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateVerify");
  381. rhdr = pos;
  382. pos += TLS_RECORD_HEADER_LEN;
  383. /* Handshake */
  384. hs_start = pos;
  385. /* HandshakeType msg_type */
  386. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY;
  387. /* uint24 length (to be filled) */
  388. hs_length = pos;
  389. pos += 3;
  390. /*
  391. * RFC 2246: 7.4.3 and 7.4.8:
  392. * Signature signature
  393. *
  394. * RSA:
  395. * digitally-signed struct {
  396. * opaque md5_hash[16];
  397. * opaque sha_hash[20];
  398. * };
  399. *
  400. * DSA:
  401. * digitally-signed struct {
  402. * opaque sha_hash[20];
  403. * };
  404. *
  405. * The hash values are calculated over all handshake messages sent or
  406. * received starting at ClientHello up to, but not including, this
  407. * CertificateVerify message, including the type and length fields of
  408. * the handshake messages.
  409. */
  410. hpos = hash;
  411. if (alg == SIGN_ALG_RSA) {
  412. hlen = MD5_MAC_LEN;
  413. if (conn->verify.md5_cert == NULL ||
  414. crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
  415. {
  416. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  417. TLS_ALERT_INTERNAL_ERROR);
  418. conn->verify.md5_cert = NULL;
  419. crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
  420. conn->verify.sha1_cert = NULL;
  421. return -1;
  422. }
  423. hpos += MD5_MAC_LEN;
  424. } else
  425. crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
  426. conn->verify.md5_cert = NULL;
  427. hlen = SHA1_MAC_LEN;
  428. if (conn->verify.sha1_cert == NULL ||
  429. crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
  430. conn->verify.sha1_cert = NULL;
  431. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  432. TLS_ALERT_INTERNAL_ERROR);
  433. return -1;
  434. }
  435. conn->verify.sha1_cert = NULL;
  436. if (alg == SIGN_ALG_RSA)
  437. hlen += MD5_MAC_LEN;
  438. wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
  439. /*
  440. * RFC 2246, 4.7:
  441. * In digital signing, one-way hash functions are used as input for a
  442. * signing algorithm. A digitally-signed element is encoded as an
  443. * opaque vector <0..2^16-1>, where the length is specified by the
  444. * signing algorithm and key.
  445. *
  446. * In RSA signing, a 36-byte structure of two hashes (one SHA and one
  447. * MD5) is signed (encrypted with the private key). It is encoded with
  448. * PKCS #1 block type 0 or type 1 as described in [PKCS1].
  449. */
  450. signed_start = pos; /* length to be filled */
  451. pos += 2;
  452. clen = end - pos;
  453. if (conn->cred == NULL ||
  454. crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
  455. pos, &clen) < 0) {
  456. wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
  457. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  458. TLS_ALERT_INTERNAL_ERROR);
  459. return -1;
  460. }
  461. WPA_PUT_BE16(signed_start, clen);
  462. pos += clen;
  463. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  464. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  465. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  466. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  467. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  468. TLS_ALERT_INTERNAL_ERROR);
  469. return -1;
  470. }
  471. pos = rhdr + rlen;
  472. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  473. *msgpos = pos;
  474. return 0;
  475. }
  476. static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
  477. u8 **msgpos, u8 *end)
  478. {
  479. u8 *pos, *rhdr;
  480. size_t rlen;
  481. pos = *msgpos;
  482. wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
  483. rhdr = pos;
  484. pos += TLS_RECORD_HEADER_LEN;
  485. *pos = TLS_CHANGE_CIPHER_SPEC;
  486. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
  487. rhdr, end - rhdr, 1, &rlen) < 0) {
  488. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  489. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  490. TLS_ALERT_INTERNAL_ERROR);
  491. return -1;
  492. }
  493. if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
  494. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
  495. "record layer");
  496. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  497. TLS_ALERT_INTERNAL_ERROR);
  498. return -1;
  499. }
  500. *msgpos = rhdr + rlen;
  501. return 0;
  502. }
  503. static int tls_write_client_finished(struct tlsv1_client *conn,
  504. u8 **msgpos, u8 *end)
  505. {
  506. u8 *pos, *rhdr, *hs_start, *hs_length;
  507. size_t rlen, hlen;
  508. u8 verify_data[TLS_VERIFY_DATA_LEN];
  509. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  510. pos = *msgpos;
  511. wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
  512. /* Encrypted Handshake Message: Finished */
  513. hlen = MD5_MAC_LEN;
  514. if (conn->verify.md5_client == NULL ||
  515. crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
  516. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  517. TLS_ALERT_INTERNAL_ERROR);
  518. conn->verify.md5_client = NULL;
  519. crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
  520. conn->verify.sha1_client = NULL;
  521. return -1;
  522. }
  523. conn->verify.md5_client = NULL;
  524. hlen = SHA1_MAC_LEN;
  525. if (conn->verify.sha1_client == NULL ||
  526. crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
  527. &hlen) < 0) {
  528. conn->verify.sha1_client = NULL;
  529. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  530. TLS_ALERT_INTERNAL_ERROR);
  531. return -1;
  532. }
  533. conn->verify.sha1_client = NULL;
  534. if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
  535. "client finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
  536. verify_data, TLS_VERIFY_DATA_LEN)) {
  537. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
  538. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  539. TLS_ALERT_INTERNAL_ERROR);
  540. return -1;
  541. }
  542. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
  543. verify_data, TLS_VERIFY_DATA_LEN);
  544. rhdr = pos;
  545. pos += TLS_RECORD_HEADER_LEN;
  546. /* Handshake */
  547. hs_start = pos;
  548. /* HandshakeType msg_type */
  549. *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
  550. /* uint24 length (to be filled) */
  551. hs_length = pos;
  552. pos += 3;
  553. os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
  554. pos += TLS_VERIFY_DATA_LEN;
  555. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  556. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  557. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  558. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  559. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  560. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  561. TLS_ALERT_INTERNAL_ERROR);
  562. return -1;
  563. }
  564. pos = rhdr + rlen;
  565. *msgpos = pos;
  566. return 0;
  567. }
  568. static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
  569. size_t *out_len)
  570. {
  571. u8 *msg, *end, *pos;
  572. size_t msglen;
  573. *out_len = 0;
  574. msglen = 1000;
  575. if (conn->certificate_requested)
  576. msglen += tls_client_cert_chain_der_len(conn);
  577. msg = os_malloc(msglen);
  578. if (msg == NULL)
  579. return NULL;
  580. pos = msg;
  581. end = msg + msglen;
  582. if (conn->certificate_requested) {
  583. if (tls_write_client_certificate(conn, &pos, end) < 0) {
  584. os_free(msg);
  585. return NULL;
  586. }
  587. }
  588. if (tls_write_client_key_exchange(conn, &pos, end) < 0 ||
  589. (conn->certificate_requested && conn->cred && conn->cred->key &&
  590. tls_write_client_certificate_verify(conn, &pos, end) < 0) ||
  591. tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
  592. tls_write_client_finished(conn, &pos, end) < 0) {
  593. os_free(msg);
  594. return NULL;
  595. }
  596. *out_len = pos - msg;
  597. conn->state = SERVER_CHANGE_CIPHER_SPEC;
  598. return msg;
  599. }
  600. static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
  601. size_t *out_len)
  602. {
  603. u8 *msg, *end, *pos;
  604. *out_len = 0;
  605. msg = os_malloc(1000);
  606. if (msg == NULL)
  607. return NULL;
  608. pos = msg;
  609. end = msg + 1000;
  610. if (tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
  611. tls_write_client_finished(conn, &pos, end) < 0) {
  612. os_free(msg);
  613. return NULL;
  614. }
  615. *out_len = pos - msg;
  616. wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
  617. "successfully");
  618. conn->state = ESTABLISHED;
  619. return msg;
  620. }
  621. u8 * tlsv1_client_handshake_write(struct tlsv1_client *conn, size_t *out_len,
  622. int no_appl_data)
  623. {
  624. switch (conn->state) {
  625. case CLIENT_KEY_EXCHANGE:
  626. return tls_send_client_key_exchange(conn, out_len);
  627. case CHANGE_CIPHER_SPEC:
  628. return tls_send_change_cipher_spec(conn, out_len);
  629. case ACK_FINISHED:
  630. wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed "
  631. "successfully");
  632. conn->state = ESTABLISHED;
  633. *out_len = 0;
  634. if (no_appl_data) {
  635. /* Need to return something to get final TLS ACK. */
  636. return os_malloc(1);
  637. }
  638. return NULL;
  639. default:
  640. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
  641. "generating reply", conn->state);
  642. return NULL;
  643. }
  644. }
  645. u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
  646. u8 description, size_t *out_len)
  647. {
  648. u8 *alert, *pos, *length;
  649. wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
  650. *out_len = 0;
  651. alert = os_malloc(10);
  652. if (alert == NULL)
  653. return NULL;
  654. pos = alert;
  655. /* TLSPlaintext */
  656. /* ContentType type */
  657. *pos++ = TLS_CONTENT_TYPE_ALERT;
  658. /* ProtocolVersion version */
  659. WPA_PUT_BE16(pos, TLS_VERSION);
  660. pos += 2;
  661. /* uint16 length (to be filled) */
  662. length = pos;
  663. pos += 2;
  664. /* opaque fragment[TLSPlaintext.length] */
  665. /* Alert */
  666. /* AlertLevel level */
  667. *pos++ = level;
  668. /* AlertDescription description */
  669. *pos++ = description;
  670. WPA_PUT_BE16(length, pos - length - 2);
  671. *out_len = pos - alert;
  672. return alert;
  673. }