tlsv1_client_write.c 22 KB

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