tlsv1_client_write.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * TLSv1 client - write handshake message
  3. * Copyright (c) 2006-2011, 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_anon_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) {
  346. if (tlsv1_key_x_anon_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. enum { SIGN_ALG_RSA, SIGN_ALG_DSA } alg = SIGN_ALG_RSA;
  373. pos = *msgpos;
  374. wpa_printf(MSG_DEBUG, "TLSv1: Send CertificateVerify");
  375. rhdr = pos;
  376. pos += TLS_RECORD_HEADER_LEN;
  377. /* Handshake */
  378. hs_start = pos;
  379. /* HandshakeType msg_type */
  380. *pos++ = TLS_HANDSHAKE_TYPE_CERTIFICATE_VERIFY;
  381. /* uint24 length (to be filled) */
  382. hs_length = pos;
  383. pos += 3;
  384. /*
  385. * RFC 2246: 7.4.3 and 7.4.8:
  386. * Signature signature
  387. *
  388. * RSA:
  389. * digitally-signed struct {
  390. * opaque md5_hash[16];
  391. * opaque sha_hash[20];
  392. * };
  393. *
  394. * DSA:
  395. * digitally-signed struct {
  396. * opaque sha_hash[20];
  397. * };
  398. *
  399. * The hash values are calculated over all handshake messages sent or
  400. * received starting at ClientHello up to, but not including, this
  401. * CertificateVerify message, including the type and length fields of
  402. * the handshake messages.
  403. */
  404. hpos = hash;
  405. #ifdef CONFIG_TLSV12
  406. if (conn->rl.tls_version == TLS_VERSION_1_2) {
  407. hlen = SHA256_MAC_LEN;
  408. if (conn->verify.sha256_cert == NULL ||
  409. crypto_hash_finish(conn->verify.sha256_cert, hpos, &hlen) <
  410. 0) {
  411. conn->verify.sha256_cert = NULL;
  412. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  413. TLS_ALERT_INTERNAL_ERROR);
  414. return -1;
  415. }
  416. conn->verify.sha256_cert = NULL;
  417. /*
  418. * RFC 3447, A.2.4 RSASSA-PKCS1-v1_5
  419. *
  420. * DigestInfo ::= SEQUENCE {
  421. * digestAlgorithm DigestAlgorithm,
  422. * digest OCTET STRING
  423. * }
  424. *
  425. * SHA-256 OID: sha256WithRSAEncryption ::= {pkcs-1 11}
  426. *
  427. * DER encoded DigestInfo for SHA256 per RFC 3447:
  428. * 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 ||
  429. * H
  430. */
  431. os_memmove(hash + 19, hash, hlen);
  432. hlen += 19;
  433. os_memcpy(hash, "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65"
  434. "\x03\x04\x02\x01\x05\x00\x04\x20", 19);
  435. } else {
  436. #endif /* CONFIG_TLSV12 */
  437. if (alg == SIGN_ALG_RSA) {
  438. hlen = MD5_MAC_LEN;
  439. if (conn->verify.md5_cert == NULL ||
  440. crypto_hash_finish(conn->verify.md5_cert, hpos, &hlen) < 0)
  441. {
  442. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  443. TLS_ALERT_INTERNAL_ERROR);
  444. conn->verify.md5_cert = NULL;
  445. crypto_hash_finish(conn->verify.sha1_cert, NULL, NULL);
  446. conn->verify.sha1_cert = NULL;
  447. return -1;
  448. }
  449. hpos += MD5_MAC_LEN;
  450. } else
  451. crypto_hash_finish(conn->verify.md5_cert, NULL, NULL);
  452. conn->verify.md5_cert = NULL;
  453. hlen = SHA1_MAC_LEN;
  454. if (conn->verify.sha1_cert == NULL ||
  455. crypto_hash_finish(conn->verify.sha1_cert, hpos, &hlen) < 0) {
  456. conn->verify.sha1_cert = NULL;
  457. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  458. TLS_ALERT_INTERNAL_ERROR);
  459. return -1;
  460. }
  461. conn->verify.sha1_cert = NULL;
  462. if (alg == SIGN_ALG_RSA)
  463. hlen += MD5_MAC_LEN;
  464. #ifdef CONFIG_TLSV12
  465. }
  466. #endif /* CONFIG_TLSV12 */
  467. wpa_hexdump(MSG_MSGDUMP, "TLSv1: CertificateVerify hash", hash, hlen);
  468. #ifdef CONFIG_TLSV12
  469. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  470. /*
  471. * RFC 5246, 4.7:
  472. * TLS v1.2 adds explicit indication of the used signature and
  473. * hash algorithms.
  474. *
  475. * struct {
  476. * HashAlgorithm hash;
  477. * SignatureAlgorithm signature;
  478. * } SignatureAndHashAlgorithm;
  479. */
  480. *pos++ = TLS_HASH_ALG_SHA256;
  481. *pos++ = TLS_SIGN_ALG_RSA;
  482. }
  483. #endif /* CONFIG_TLSV12 */
  484. /*
  485. * RFC 2246, 4.7:
  486. * In digital signing, one-way hash functions are used as input for a
  487. * signing algorithm. A digitally-signed element is encoded as an
  488. * opaque vector <0..2^16-1>, where the length is specified by the
  489. * signing algorithm and key.
  490. *
  491. * In RSA signing, a 36-byte structure of two hashes (one SHA and one
  492. * MD5) is signed (encrypted with the private key). It is encoded with
  493. * PKCS #1 block type 0 or type 1 as described in [PKCS1].
  494. */
  495. signed_start = pos; /* length to be filled */
  496. pos += 2;
  497. clen = end - pos;
  498. if (conn->cred == NULL ||
  499. crypto_private_key_sign_pkcs1(conn->cred->key, hash, hlen,
  500. pos, &clen) < 0) {
  501. wpa_printf(MSG_DEBUG, "TLSv1: Failed to sign hash (PKCS #1)");
  502. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  503. TLS_ALERT_INTERNAL_ERROR);
  504. return -1;
  505. }
  506. WPA_PUT_BE16(signed_start, clen);
  507. pos += clen;
  508. WPA_PUT_BE24(hs_length, pos - hs_length - 3);
  509. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  510. rhdr, end - rhdr, hs_start, pos - hs_start,
  511. &rlen) < 0) {
  512. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate a record");
  513. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  514. TLS_ALERT_INTERNAL_ERROR);
  515. return -1;
  516. }
  517. pos = rhdr + rlen;
  518. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  519. *msgpos = pos;
  520. return 0;
  521. }
  522. static int tls_write_client_change_cipher_spec(struct tlsv1_client *conn,
  523. u8 **msgpos, u8 *end)
  524. {
  525. size_t rlen;
  526. u8 payload[1];
  527. wpa_printf(MSG_DEBUG, "TLSv1: Send ChangeCipherSpec");
  528. payload[0] = TLS_CHANGE_CIPHER_SPEC;
  529. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC,
  530. *msgpos, end - *msgpos, payload, sizeof(payload),
  531. &rlen) < 0) {
  532. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  533. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  534. TLS_ALERT_INTERNAL_ERROR);
  535. return -1;
  536. }
  537. if (tlsv1_record_change_write_cipher(&conn->rl) < 0) {
  538. wpa_printf(MSG_DEBUG, "TLSv1: Failed to set write cipher for "
  539. "record layer");
  540. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  541. TLS_ALERT_INTERNAL_ERROR);
  542. return -1;
  543. }
  544. *msgpos += rlen;
  545. return 0;
  546. }
  547. static int tls_write_client_finished(struct tlsv1_client *conn,
  548. u8 **msgpos, u8 *end)
  549. {
  550. u8 *pos, *hs_start;
  551. size_t rlen, hlen;
  552. u8 verify_data[1 + 3 + TLS_VERIFY_DATA_LEN];
  553. u8 hash[MD5_MAC_LEN + SHA1_MAC_LEN];
  554. wpa_printf(MSG_DEBUG, "TLSv1: Send Finished");
  555. /* Encrypted Handshake Message: Finished */
  556. #ifdef CONFIG_TLSV12
  557. if (conn->rl.tls_version >= TLS_VERSION_1_2) {
  558. hlen = SHA256_MAC_LEN;
  559. if (conn->verify.sha256_client == NULL ||
  560. crypto_hash_finish(conn->verify.sha256_client, hash, &hlen)
  561. < 0) {
  562. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  563. TLS_ALERT_INTERNAL_ERROR);
  564. conn->verify.sha256_client = NULL;
  565. return -1;
  566. }
  567. conn->verify.sha256_client = NULL;
  568. } else {
  569. #endif /* CONFIG_TLSV12 */
  570. hlen = MD5_MAC_LEN;
  571. if (conn->verify.md5_client == NULL ||
  572. crypto_hash_finish(conn->verify.md5_client, hash, &hlen) < 0) {
  573. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  574. TLS_ALERT_INTERNAL_ERROR);
  575. conn->verify.md5_client = NULL;
  576. crypto_hash_finish(conn->verify.sha1_client, NULL, NULL);
  577. conn->verify.sha1_client = NULL;
  578. return -1;
  579. }
  580. conn->verify.md5_client = NULL;
  581. hlen = SHA1_MAC_LEN;
  582. if (conn->verify.sha1_client == NULL ||
  583. crypto_hash_finish(conn->verify.sha1_client, hash + MD5_MAC_LEN,
  584. &hlen) < 0) {
  585. conn->verify.sha1_client = NULL;
  586. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  587. TLS_ALERT_INTERNAL_ERROR);
  588. return -1;
  589. }
  590. conn->verify.sha1_client = NULL;
  591. hlen = MD5_MAC_LEN + SHA1_MAC_LEN;
  592. #ifdef CONFIG_TLSV12
  593. }
  594. #endif /* CONFIG_TLSV12 */
  595. if (tls_prf(conn->rl.tls_version,
  596. conn->master_secret, TLS_MASTER_SECRET_LEN,
  597. "client finished", hash, hlen,
  598. verify_data + 1 + 3, TLS_VERIFY_DATA_LEN)) {
  599. wpa_printf(MSG_DEBUG, "TLSv1: Failed to generate verify_data");
  600. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  601. TLS_ALERT_INTERNAL_ERROR);
  602. return -1;
  603. }
  604. wpa_hexdump_key(MSG_DEBUG, "TLSv1: verify_data (client)",
  605. verify_data + 1 + 3, TLS_VERIFY_DATA_LEN);
  606. /* Handshake */
  607. pos = hs_start = verify_data;
  608. /* HandshakeType msg_type */
  609. *pos++ = TLS_HANDSHAKE_TYPE_FINISHED;
  610. /* uint24 length */
  611. WPA_PUT_BE24(pos, TLS_VERIFY_DATA_LEN);
  612. pos += 3;
  613. pos += TLS_VERIFY_DATA_LEN; /* verify_data already in place */
  614. tls_verify_hash_add(&conn->verify, hs_start, pos - hs_start);
  615. if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_HANDSHAKE,
  616. *msgpos, end - *msgpos, hs_start, pos - hs_start,
  617. &rlen) < 0) {
  618. wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
  619. tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
  620. TLS_ALERT_INTERNAL_ERROR);
  621. return -1;
  622. }
  623. *msgpos += rlen;
  624. return 0;
  625. }
  626. static u8 * tls_send_client_key_exchange(struct tlsv1_client *conn,
  627. size_t *out_len)
  628. {
  629. u8 *msg, *end, *pos;
  630. size_t msglen;
  631. *out_len = 0;
  632. msglen = 2000;
  633. if (conn->certificate_requested)
  634. msglen += tls_client_cert_chain_der_len(conn);
  635. msg = os_malloc(msglen);
  636. if (msg == NULL)
  637. return NULL;
  638. pos = msg;
  639. end = msg + msglen;
  640. if (conn->certificate_requested) {
  641. if (tls_write_client_certificate(conn, &pos, end) < 0) {
  642. os_free(msg);
  643. return NULL;
  644. }
  645. }
  646. if (tls_write_client_key_exchange(conn, &pos, end) < 0 ||
  647. (conn->certificate_requested && conn->cred && conn->cred->key &&
  648. tls_write_client_certificate_verify(conn, &pos, end) < 0) ||
  649. tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
  650. tls_write_client_finished(conn, &pos, end) < 0) {
  651. os_free(msg);
  652. return NULL;
  653. }
  654. *out_len = pos - msg;
  655. conn->state = SERVER_CHANGE_CIPHER_SPEC;
  656. return msg;
  657. }
  658. static u8 * tls_send_change_cipher_spec(struct tlsv1_client *conn,
  659. size_t *out_len)
  660. {
  661. u8 *msg, *end, *pos;
  662. *out_len = 0;
  663. msg = os_malloc(1000);
  664. if (msg == NULL)
  665. return NULL;
  666. pos = msg;
  667. end = msg + 1000;
  668. if (tls_write_client_change_cipher_spec(conn, &pos, end) < 0 ||
  669. tls_write_client_finished(conn, &pos, end) < 0) {
  670. os_free(msg);
  671. return NULL;
  672. }
  673. *out_len = pos - msg;
  674. wpa_printf(MSG_DEBUG, "TLSv1: Session resumption completed "
  675. "successfully");
  676. conn->state = ESTABLISHED;
  677. return msg;
  678. }
  679. u8 * tlsv1_client_handshake_write(struct tlsv1_client *conn, size_t *out_len,
  680. int no_appl_data)
  681. {
  682. switch (conn->state) {
  683. case CLIENT_KEY_EXCHANGE:
  684. return tls_send_client_key_exchange(conn, out_len);
  685. case CHANGE_CIPHER_SPEC:
  686. return tls_send_change_cipher_spec(conn, out_len);
  687. case ACK_FINISHED:
  688. wpa_printf(MSG_DEBUG, "TLSv1: Handshake completed "
  689. "successfully");
  690. conn->state = ESTABLISHED;
  691. *out_len = 0;
  692. if (no_appl_data) {
  693. /* Need to return something to get final TLS ACK. */
  694. return os_malloc(1);
  695. }
  696. return NULL;
  697. default:
  698. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected state %d while "
  699. "generating reply", conn->state);
  700. return NULL;
  701. }
  702. }
  703. u8 * tlsv1_client_send_alert(struct tlsv1_client *conn, u8 level,
  704. u8 description, size_t *out_len)
  705. {
  706. u8 *alert, *pos, *length;
  707. wpa_printf(MSG_DEBUG, "TLSv1: Send Alert(%d:%d)", level, description);
  708. *out_len = 0;
  709. alert = os_malloc(10);
  710. if (alert == NULL)
  711. return NULL;
  712. pos = alert;
  713. /* TLSPlaintext */
  714. /* ContentType type */
  715. *pos++ = TLS_CONTENT_TYPE_ALERT;
  716. /* ProtocolVersion version */
  717. WPA_PUT_BE16(pos, conn->rl.tls_version ? conn->rl.tls_version :
  718. TLS_VERSION);
  719. pos += 2;
  720. /* uint16 length (to be filled) */
  721. length = pos;
  722. pos += 2;
  723. /* opaque fragment[TLSPlaintext.length] */
  724. /* Alert */
  725. /* AlertLevel level */
  726. *pos++ = level;
  727. /* AlertDescription description */
  728. *pos++ = description;
  729. WPA_PUT_BE16(length, pos - length - 2);
  730. *out_len = pos - alert;
  731. return alert;
  732. }