tlsv1_client_write.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 "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_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. /* ClientDiffieHellmanPublic */
  179. u8 *csecret, *csecret_start, *dh_yc, *shared;
  180. size_t csecret_len, dh_yc_len, shared_len;
  181. csecret_len = conn->dh_p_len;
  182. csecret = os_malloc(csecret_len);
  183. if (csecret == NULL) {
  184. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  185. "memory for Yc (Diffie-Hellman)");
  186. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  187. TLS_ALERT_INTERNAL_ERROR);
  188. return -1;
  189. }
  190. if (os_get_random(csecret, csecret_len)) {
  191. wpa_printf(MSG_DEBUG, "TLSv1: Failed to get random "
  192. "data for Diffie-Hellman");
  193. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  194. TLS_ALERT_INTERNAL_ERROR);
  195. os_free(csecret);
  196. return -1;
  197. }
  198. if (os_memcmp(csecret, conn->dh_p, csecret_len) > 0)
  199. csecret[0] = 0; /* make sure Yc < p */
  200. csecret_start = csecret;
  201. while (csecret_len > 1 && *csecret_start == 0) {
  202. csecret_start++;
  203. csecret_len--;
  204. }
  205. wpa_hexdump_key(MSG_DEBUG, "TLSv1: DH client's secret value",
  206. csecret_start, csecret_len);
  207. /* Yc = g^csecret mod p */
  208. dh_yc_len = conn->dh_p_len;
  209. dh_yc = os_malloc(dh_yc_len);
  210. if (dh_yc == NULL) {
  211. wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
  212. "memory for Diffie-Hellman");
  213. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  214. TLS_ALERT_INTERNAL_ERROR);
  215. os_free(csecret);
  216. return -1;
  217. }
  218. if (crypto_mod_exp(conn->dh_g, conn->dh_g_len,
  219. csecret_start, csecret_len,
  220. conn->dh_p, conn->dh_p_len,
  221. dh_yc, &dh_yc_len)) {
  222. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  223. TLS_ALERT_INTERNAL_ERROR);
  224. os_free(csecret);
  225. os_free(dh_yc);
  226. return -1;
  227. }
  228. wpa_hexdump(MSG_DEBUG, "TLSv1: DH Yc (client's public value)",
  229. dh_yc, dh_yc_len);
  230. WPA_PUT_BE16(*pos, dh_yc_len);
  231. *pos += 2;
  232. if (*pos + dh_yc_len > end) {
  233. wpa_printf(MSG_DEBUG, "TLSv1: Not enough room in the "
  234. "message buffer for Yc");
  235. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  236. TLS_ALERT_INTERNAL_ERROR);
  237. os_free(csecret);
  238. os_free(dh_yc);
  239. return -1;
  240. }
  241. os_memcpy(*pos, dh_yc, dh_yc_len);
  242. *pos += dh_yc_len;
  243. os_free(dh_yc);
  244. shared_len = conn->dh_p_len;
  245. shared = os_malloc(shared_len);
  246. if (shared == NULL) {
  247. wpa_printf(MSG_DEBUG, "TLSv1: Could not allocate memory for "
  248. "DH");
  249. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  250. TLS_ALERT_INTERNAL_ERROR);
  251. os_free(csecret);
  252. return -1;
  253. }
  254. /* shared = Ys^csecret mod p */
  255. if (crypto_mod_exp(conn->dh_ys, conn->dh_ys_len,
  256. csecret_start, csecret_len,
  257. conn->dh_p, conn->dh_p_len,
  258. shared, &shared_len)) {
  259. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  260. TLS_ALERT_INTERNAL_ERROR);
  261. os_free(csecret);
  262. os_free(shared);
  263. return -1;
  264. }
  265. wpa_hexdump_key(MSG_DEBUG, "TLSv1: Shared secret from DH key exchange",
  266. shared, shared_len);
  267. os_memset(csecret_start, 0, csecret_len);
  268. os_free(csecret);
  269. if (tls_derive_keys(conn, shared, shared_len)) {
  270. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  271. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  272. TLS_ALERT_INTERNAL_ERROR);
  273. os_free(shared);
  274. return -1;
  275. }
  276. os_memset(shared, 0, shared_len);
  277. os_free(shared);
  278. tlsv1_client_free_dh(conn);
  279. return 0;
  280. }
  281. static int tlsv1_key_x_rsa(struct tlsv1_client *conn, u8 **pos, u8 *end)
  282. {
  283. u8 pre_master_secret[TLS_PRE_MASTER_SECRET_LEN];
  284. size_t clen;
  285. int res;
  286. if (tls_derive_pre_master_secret(pre_master_secret) < 0 ||
  287. tls_derive_keys(conn, pre_master_secret,
  288. TLS_PRE_MASTER_SECRET_LEN)) {
  289. wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive keys");
  290. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  291. TLS_ALERT_INTERNAL_ERROR);
  292. return -1;
  293. }
  294. /* EncryptedPreMasterSecret */
  295. if (conn->server_rsa_key == NULL) {
  296. wpa_printf(MSG_DEBUG, "TLSv1: No server RSA key to "
  297. "use for encrypting pre-master secret");
  298. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  299. TLS_ALERT_INTERNAL_ERROR);
  300. return -1;
  301. }
  302. /* RSA encrypted value is encoded with PKCS #1 v1.5 block type 2. */
  303. *pos += 2;
  304. clen = end - *pos;
  305. res = crypto_public_key_encrypt_pkcs1_v15(
  306. conn->server_rsa_key,
  307. pre_master_secret, TLS_PRE_MASTER_SECRET_LEN,
  308. *pos, &clen);
  309. os_memset(pre_master_secret, 0, TLS_PRE_MASTER_SECRET_LEN);
  310. if (res < 0) {
  311. wpa_printf(MSG_DEBUG, "TLSv1: RSA encryption failed");
  312. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  313. TLS_ALERT_INTERNAL_ERROR);
  314. return -1;
  315. }
  316. WPA_PUT_BE16(*pos - 2, clen);
  317. wpa_hexdump(MSG_MSGDUMP, "TLSv1: Encrypted pre_master_secret",
  318. *pos, clen);
  319. *pos += clen;
  320. return 0;
  321. }
  322. static int tls_write_client_key_exchange(struct tlsv1_client *conn,
  323. u8 **msgpos, u8 *end)
  324. {
  325. u8 *pos, *rhdr, *hs_start, *hs_length;
  326. size_t rlen;
  327. tls_key_exchange keyx;
  328. const struct tls_cipher_suite *suite;
  329. suite = tls_get_cipher_suite(conn->rl.cipher_suite);
  330. if (suite == NULL)
  331. keyx = TLS_KEY_X_NULL;
  332. else
  333. keyx = suite->key_exchange;
  334. pos = *msgpos;
  335. wpa_printf(MSG_DEBUG, "TLSv1: Send ClientKeyExchange");
  336. rhdr = pos;
  337. pos += TLS_RECORD_HEADER_LEN;
  338. /* opaque fragment[TLSPlaintext.length] */
  339. /* Handshake */
  340. hs_start = pos;
  341. /* HandshakeType msg_type */
  342. *pos++ = TLS_HANDSHAKE_TYPE_CLIENT_KEY_EXCHANGE;
  343. /* uint24 length (to be filled) */
  344. hs_length = pos;
  345. pos += 3;
  346. /* body - ClientKeyExchange */
  347. if (keyx == TLS_KEY_X_DH_anon) {
  348. if (tlsv1_key_x_anon_dh(conn, &pos, end) < 0)
  349. return -1;
  350. } else {
  351. if (tlsv1_key_x_rsa(conn, &pos, end) < 0)
  352. return -1;
  353. }
  354. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  355. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  356. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  357. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  358. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  359. TLS_ALERT_INTERNAL_ERROR);
  360. return -1;
  361. }
  362. pos = rhdr + rlen;
  363. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  364. *msgpos = pos;
  365. return 0;
  366. }
  367. static int tls_write_client_certificate_verify(struct tlsv1_client *conn,
  368. u8 **msgpos, u8 *end)
  369. {
  370. u8 *pos, *rhdr, *hs_start, *hs_length, *signed_start;
  371. size_t rlen, hlen, clen;
  372. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN], *hpos;
  373. enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
  374. pos = *msgpos;
  375. wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateVerify");
  376. rhdr = pos;
  377. pos += TLS_RECORD_HEADER_LEN;
  378. /* Handshake */
  379. hs_start = pos;
  380. /* HandshakeType msg_type */
  381. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY;
  382. /* uint24 length (to be filled) */
  383. hs_length = pos;
  384. pos += 3;
  385. /*
  386. * RFC 2246: 7.4.3 and 7.4.8:
  387. * Signature signature
  388. *
  389. * RSA:
  390. * digitally-signed struct {
  391. * opaque md5_hash[16];
  392. * opaque sha_hash[20];
  393. * };
  394. *
  395. * DSA:
  396. * digitally-signed struct {
  397. * opaque sha_hash[20];
  398. * };
  399. *
  400. * The hash values are calculated over all handshake messages sent or
  401. * received starting at ClientHello up to, but not including, this
  402. * CertificateVerify message, including the type and length fields of
  403. * the handshake messages.
  404. */
  405. hpos = hash;
  406. if (alg == SIGN_ALG_RSA) {
  407. hlen = MD5_MAC_LEN;
  408. if (conn->verify.md5_cert == NULL ||
  409. crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
  410. {
  411. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  412. TLS_ALERT_INTERNAL_ERROR);
  413. conn->verify.md5_cert = NULL;
  414. crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
  415. conn->verify.sha1_cert = NULL;
  416. return -1;
  417. }
  418. hpos += MD5_MAC_LEN;
  419. } else
  420. crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
  421. conn->verify.md5_cert = NULL;
  422. hlen = SHA1_MAC_LEN;
  423. if (conn->verify.sha1_cert == NULL ||
  424. crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
  425. conn->verify.sha1_cert = NULL;
  426. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  427. TLS_ALERT_INTERNAL_ERROR);
  428. return -1;
  429. }
  430. conn->verify.sha1_cert = NULL;
  431. if (alg == SIGN_ALG_RSA)
  432. hlen += MD5_MAC_LEN;
  433. wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
  434. /*
  435. * RFC 2246, 4.7:
  436. * In digital signing, one-way hash functions are used as input for a
  437. * signing algorithm. A digitally-signed element is encoded as an
  438. * opaque vector <0..2^16-1>, where the length is specified by the
  439. * signing algorithm and key.
  440. *
  441. * In RSA signing, a 36-byte structure of two hashes (one SHA and one
  442. * MD5) is signed (encrypted with the private key). It is encoded with
  443. * PKCS #1 block type 0 or type 1 as described in [PKCS1].
  444. */
  445. signed_start = pos; /* length to be filled */
  446. pos += 2;
  447. clen = end - pos;
  448. if (conn->cred == NULL ||
  449. crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
  450. pos, &clen) < 0) {
  451. wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
  452. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  453. TLS_ALERT_INTERNAL_ERROR);
  454. return -1;
  455. }
  456. WPA_PUT_BE16(signed_start, clen);
  457. pos += clen;
  458. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  459. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  460. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  461. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  462. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  463. TLS_ALERT_INTERNAL_ERROR);
  464. return -1;
  465. }
  466. pos = rhdr + rlen;
  467. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  468. *msgpos = pos;
  469. return 0;
  470. }
  471. static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
  472. u8 **msgpos, u8 *end)
  473. {
  474. u8 *pos, *rhdr;
  475. size_t rlen;
  476. pos = *msgpos;
  477. wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
  478. rhdr = pos;
  479. pos += TLS_RECORD_HEADER_LEN;
  480. *pos = TLS_CHANGE_CIPHER_SPEC;
  481. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
  482. rhdr, end - rhdr, 1, &rlen) < 0) {
  483. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  484. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  485. TLS_ALERT_INTERNAL_ERROR);
  486. return -1;
  487. }
  488. if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
  489. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
  490. "record layer");
  491. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  492. TLS_ALERT_INTERNAL_ERROR);
  493. return -1;
  494. }
  495. *msgpos = rhdr + rlen;
  496. return 0;
  497. }
  498. static int tls_write_client_finished(struct tlsv1_client *conn,
  499. u8 **msgpos, u8 *end)
  500. {
  501. u8 *pos, *rhdr, *hs_start, *hs_length;
  502. size_t rlen, hlen;
  503. u8 verify_data[TLS_VERIFY_DATA_LEN];
  504. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  505. pos = *msgpos;
  506. wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
  507. /* Encrypted Handshake Message: Finished */
  508. hlen = MD5_MAC_LEN;
  509. if (conn->verify.md5_client == NULL ||
  510. crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
  511. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  512. TLS_ALERT_INTERNAL_ERROR);
  513. conn->verify.md5_client = NULL;
  514. crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
  515. conn->verify.sha1_client = NULL;
  516. return -1;
  517. }
  518. conn->verify.md5_client = NULL;
  519. hlen = SHA1_MAC_LEN;
  520. if (conn->verify.sha1_client == NULL ||
  521. crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
  522. &hlen) < 0) {
  523. conn->verify.sha1_client = NULL;
  524. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  525. TLS_ALERT_INTERNAL_ERROR);
  526. return -1;
  527. }
  528. conn->verify.sha1_client = NULL;
  529. if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
  530. "client finished", hash, MD5_MAC_LEN + SHA1_MAC_LEN,
  531. verify_data, TLS_VERIFY_DATA_LEN)) {
  532. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
  533. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  534. TLS_ALERT_INTERNAL_ERROR);
  535. return -1;
  536. }
  537. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
  538. verify_data, TLS_VERIFY_DATA_LEN);
  539. rhdr = pos;
  540. pos += TLS_RECORD_HEADER_LEN;
  541. /* Handshake */
  542. hs_start = pos;
  543. /* HandshakeType msg_type */
  544. *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
  545. /* uint24 length (to be filled) */
  546. hs_length = pos;
  547. pos += 3;
  548. os_memcpy(pos, verify_data, TLS_VERIFY_DATA_LEN);
  549. pos += TLS_VERIFY_DATA_LEN;
  550. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  551. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  552. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  553. rhdr, end - rhdr, pos - hs_start, &rlen) < 0) {
  554. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  555. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  556. TLS_ALERT_INTERNAL_ERROR);
  557. return -1;
  558. }
  559. pos = rhdr + rlen;
  560. *msgpos = pos;
  561. return 0;
  562. }
  563. static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
  564. size_t *out_len)
  565. {
  566. u8 *msg, *end, *pos;
  567. size_t msglen;
  568. *out_len = 0;
  569. msglen = 1000;
  570. if (conn->certificate_requested)
  571. msglen += tls_client_cert_chain_der_len(conn);
  572. msg = os_malloc(msglen);
  573. if (msg == NULL)
  574. return NULL;
  575. pos = msg;
  576. end = msg + msglen;
  577. if (conn->certificate_requested) {
  578. if (tls_write_client_certificate(conn, &pos, end) < 0) {
  579. os_free(msg);
  580. return NULL;
  581. }
  582. }
  583. if (tls_write_client_key_exchange(conn, &pos, end) < 0 ||
  584. (conn->certificate_requested && conn->cred && conn->cred->key &&
  585. tls_write_client_certificate_verify(conn, &pos, end) < 0) ||
  586. tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
  587. tls_write_client_finished(conn, &pos, end) < 0) {
  588. os_free(msg);
  589. return NULL;
  590. }
  591. *out_len = pos - msg;
  592. conn->state = SERVER_CHANGE_CIPHER_SPEC;
  593. return msg;
  594. }
  595. static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
  596. size_t *out_len)
  597. {
  598. u8 *msg, *end, *pos;
  599. *out_len = 0;
  600. msg = os_malloc(1000);
  601. if (msg == NULL)
  602. return NULL;
  603. pos = msg;
  604. end = msg + 1000;
  605. if (tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
  606. tls_write_client_finished(conn, &pos, end) < 0) {
  607. os_free(msg);
  608. return NULL;
  609. }
  610. *out_len = pos - msg;
  611. wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
  612. "successfully");
  613. conn->state = ESTABLISHED;
  614. return msg;
  615. }
  616. u8 * tlsv1_client_handshake_write(struct tlsv1_client *conn, size_t *out_len,
  617. int no_appl_data)
  618. {
  619. switch (conn->state) {
  620. case CLIENT_KEY_EXCHANGE:
  621. return tls_send_client_key_exchange(conn, out_len);
  622. case CHANGE_CIPHER_SPEC:
  623. return tls_send_change_cipher_spec(conn, out_len);
  624. case ACK_FINISHED:
  625. wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed "
  626. "successfully");
  627. conn->state = ESTABLISHED;
  628. *out_len = 0;
  629. if (no_appl_data) {
  630. /* Need to return something to get final TLS ACK. */
  631. return os_malloc(1);
  632. }
  633. return NULL;
  634. default:
  635. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
  636. "generating reply", conn->state);
  637. return NULL;
  638. }
  639. }
  640. u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
  641. u8 description, size_t *out_len)
  642. {
  643. u8 *alert, *pos, *length;
  644. wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
  645. *out_len = 0;
  646. alert = os_malloc(10);
  647. if (alert == NULL)
  648. return NULL;
  649. pos = alert;
  650. /* TLSPlaintext */
  651. /* ContentType type */
  652. *pos++ = TLS_CONTENT_TYPE_ALERT;
  653. /* ProtocolVersion version */
  654. WPA_PUT_BE16(pos, TLS_VERSION);
  655. pos += 2;
  656. /* uint16 length (to be filled) */
  657. length = pos;
  658. pos += 2;
  659. /* opaque fragment[TLSPlaintext.length] */
  660. /* Alert */
  661. /* AlertLevel level */
  662. *pos++ = level;
  663. /* AlertDescription description */
  664. *pos++ = description;
  665. WPA_PUT_BE16(length, pos - length - 2);
  666. *out_len = pos - alert;
  667. return alert;
  668. }