tlsv1_client_write.c 23 KB

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