tlsv1_record.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * TLSv1 Record Protocol
  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 "tlsv1_common.h"
  14. #include "tlsv1_record.h"
  15. /**
  16. * tlsv1_record_set_cipher_suite - TLS record layer: Set cipher suite
  17. * @rl: Pointer to TLS record layer data
  18. * @cipher_suite: New cipher suite
  19. * Returns: 0 on success, -1 on failure
  20. *
  21. * This function is used to prepare TLS record layer for cipher suite change.
  22. * tlsv1_record_change_write_cipher() and
  23. * tlsv1_record_change_read_cipher() functions can then be used to change the
  24. * currently used ciphers.
  25. */
  26. int tlsv1_record_set_cipher_suite(struct tlsv1_record_layer *rl,
  27. u16 cipher_suite)
  28. {
  29. const struct tls_cipher_suite *suite;
  30. const struct tls_cipher_data *data;
  31. wpa_printf(MSG_DEBUG, "TLSv1: Selected cipher suite: 0x%04x",
  32. cipher_suite);
  33. rl->cipher_suite = cipher_suite;
  34. suite = tls_get_cipher_suite(cipher_suite);
  35. if (suite == NULL)
  36. return -1;
  37. if (suite->hash == TLS_HASH_MD5) {
  38. rl->hash_alg = CRYPTO_HASH_ALG_HMAC_MD5;
  39. rl->hash_size = MD5_MAC_LEN;
  40. } else if (suite->hash == TLS_HASH_SHA) {
  41. rl->hash_alg = CRYPTO_HASH_ALG_HMAC_SHA1;
  42. rl->hash_size = SHA1_MAC_LEN;
  43. } else if (suite->hash == TLS_HASH_SHA256) {
  44. rl->hash_alg = CRYPTO_HASH_ALG_HMAC_SHA256;
  45. rl->hash_size = SHA256_MAC_LEN;
  46. }
  47. data = tls_get_cipher_data(suite->cipher);
  48. if (data == NULL)
  49. return -1;
  50. rl->key_material_len = data->key_material;
  51. rl->iv_size = data->block_size;
  52. rl->cipher_alg = data->alg;
  53. return 0;
  54. }
  55. /**
  56. * tlsv1_record_change_write_cipher - TLS record layer: Change write cipher
  57. * @rl: Pointer to TLS record layer data
  58. * Returns: 0 on success (cipher changed), -1 on failure
  59. *
  60. * This function changes TLS record layer to use the new cipher suite
  61. * configured with tlsv1_record_set_cipher_suite() for writing.
  62. */
  63. int tlsv1_record_change_write_cipher(struct tlsv1_record_layer *rl)
  64. {
  65. wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - New write cipher suite "
  66. "0x%04x", rl->cipher_suite);
  67. rl->write_cipher_suite = rl->cipher_suite;
  68. os_memset(rl->write_seq_num, 0, TLS_SEQ_NUM_LEN);
  69. if (rl->write_cbc) {
  70. crypto_cipher_deinit(rl->write_cbc);
  71. rl->write_cbc = NULL;
  72. }
  73. if (rl->cipher_alg != CRYPTO_CIPHER_NULL) {
  74. rl->write_cbc = crypto_cipher_init(rl->cipher_alg,
  75. rl->write_iv, rl->write_key,
  76. rl->key_material_len);
  77. if (rl->write_cbc == NULL) {
  78. wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize "
  79. "cipher");
  80. return -1;
  81. }
  82. }
  83. return 0;
  84. }
  85. /**
  86. * tlsv1_record_change_read_cipher - TLS record layer: Change read cipher
  87. * @rl: Pointer to TLS record layer data
  88. * Returns: 0 on success (cipher changed), -1 on failure
  89. *
  90. * This function changes TLS record layer to use the new cipher suite
  91. * configured with tlsv1_record_set_cipher_suite() for reading.
  92. */
  93. int tlsv1_record_change_read_cipher(struct tlsv1_record_layer *rl)
  94. {
  95. wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - New read cipher suite "
  96. "0x%04x", rl->cipher_suite);
  97. rl->read_cipher_suite = rl->cipher_suite;
  98. os_memset(rl->read_seq_num, 0, TLS_SEQ_NUM_LEN);
  99. if (rl->read_cbc) {
  100. crypto_cipher_deinit(rl->read_cbc);
  101. rl->read_cbc = NULL;
  102. }
  103. if (rl->cipher_alg != CRYPTO_CIPHER_NULL) {
  104. rl->read_cbc = crypto_cipher_init(rl->cipher_alg,
  105. rl->read_iv, rl->read_key,
  106. rl->key_material_len);
  107. if (rl->read_cbc == NULL) {
  108. wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize "
  109. "cipher");
  110. return -1;
  111. }
  112. }
  113. return 0;
  114. }
  115. /**
  116. * tlsv1_record_send - TLS record layer: Send a message
  117. * @rl: Pointer to TLS record layer data
  118. * @content_type: Content type (TLS_CONTENT_TYPE_*)
  119. * @buf: Buffer for the generated TLS message (needs to have extra space for
  120. * header, IV (TLS v1.1), and HMAC)
  121. * @buf_size: Maximum buf size
  122. * @payload: Payload to be sent
  123. * @payload_len: Length of the payload
  124. * @out_len: Buffer for returning the used buf length
  125. * Returns: 0 on success, -1 on failure
  126. *
  127. * This function fills in the TLS record layer header, adds HMAC, and encrypts
  128. * the data using the current write cipher.
  129. */
  130. int tlsv1_record_send(struct tlsv1_record_layer *rl, u8 content_type, u8 *buf,
  131. size_t buf_size, const u8 *payload, size_t payload_len,
  132. size_t *out_len)
  133. {
  134. u8 *pos, *ct_start, *length, *cpayload;
  135. struct crypto_hash *hmac;
  136. size_t clen;
  137. int explicit_iv;
  138. pos = buf;
  139. if (pos + TLS_RECORD_HEADER_LEN > buf + buf_size)
  140. return -1;
  141. /* ContentType type */
  142. ct_start = pos;
  143. *pos++ = content_type;
  144. /* ProtocolVersion version */
  145. WPA_PUT_BE16(pos, rl->tls_version);
  146. pos += 2;
  147. /* uint16 length */
  148. length = pos;
  149. WPA_PUT_BE16(length, payload_len);
  150. pos += 2;
  151. cpayload = pos;
  152. explicit_iv = rl->write_cipher_suite != TLS_NULL_WITH_NULL_NULL &&
  153. rl->iv_size && rl->tls_version >= TLS_VERSION_1_1;
  154. if (explicit_iv) {
  155. /* opaque IV[Cipherspec.block_length] */
  156. if (pos + rl->iv_size > buf + buf_size)
  157. return -1;
  158. /*
  159. * Use random number R per the RFC 4346, 6.2.3.2 CBC Block
  160. * Cipher option 2a.
  161. */
  162. if (os_get_random(pos, rl->iv_size))
  163. return -1;
  164. pos += rl->iv_size;
  165. }
  166. /*
  167. * opaque fragment[TLSPlaintext.length]
  168. * (opaque content[TLSCompressed.length] in GenericBlockCipher)
  169. */
  170. if (pos + payload_len > buf + buf_size)
  171. return -1;
  172. os_memmove(pos, payload, payload_len);
  173. pos += payload_len;
  174. if (rl->write_cipher_suite != TLS_NULL_WITH_NULL_NULL) {
  175. /*
  176. * MAC calculated over seq_num + TLSCompressed.type +
  177. * TLSCompressed.version + TLSCompressed.length +
  178. * TLSCompressed.fragment
  179. */
  180. hmac = crypto_hash_init(rl->hash_alg, rl->write_mac_secret,
  181. rl->hash_size);
  182. if (hmac == NULL) {
  183. wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - Failed "
  184. "to initialize HMAC");
  185. return -1;
  186. }
  187. crypto_hash_update(hmac, rl->write_seq_num, TLS_SEQ_NUM_LEN);
  188. /* type + version + length + fragment */
  189. crypto_hash_update(hmac, ct_start, TLS_RECORD_HEADER_LEN);
  190. crypto_hash_update(hmac, payload, payload_len);
  191. clen = buf + buf_size - pos;
  192. if (clen < rl->hash_size) {
  193. wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - Not "
  194. "enough room for MAC");
  195. crypto_hash_finish(hmac, NULL, NULL);
  196. return -1;
  197. }
  198. if (crypto_hash_finish(hmac, pos, &clen) < 0) {
  199. wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - Failed "
  200. "to calculate HMAC");
  201. return -1;
  202. }
  203. wpa_hexdump(MSG_MSGDUMP, "TLSv1: Record Layer - Write HMAC",
  204. pos, clen);
  205. pos += clen;
  206. if (rl->iv_size) {
  207. size_t len = pos - cpayload;
  208. size_t pad;
  209. pad = (len + 1) % rl->iv_size;
  210. if (pad)
  211. pad = rl->iv_size - pad;
  212. if (pos + pad + 1 > buf + buf_size) {
  213. wpa_printf(MSG_DEBUG, "TLSv1: No room for "
  214. "block cipher padding");
  215. return -1;
  216. }
  217. os_memset(pos, pad, pad + 1);
  218. pos += pad + 1;
  219. }
  220. if (crypto_cipher_encrypt(rl->write_cbc, cpayload,
  221. cpayload, pos - cpayload) < 0)
  222. return -1;
  223. }
  224. WPA_PUT_BE16(length, pos - length - 2);
  225. inc_byte_array(rl->write_seq_num, TLS_SEQ_NUM_LEN);
  226. *out_len = pos - buf;
  227. return 0;
  228. }
  229. /**
  230. * tlsv1_record_receive - TLS record layer: Process a received message
  231. * @rl: Pointer to TLS record layer data
  232. * @in_data: Received data
  233. * @in_len: Length of the received data
  234. * @out_data: Buffer for output data (must be at least as long as in_data)
  235. * @out_len: Set to maximum out_data length by caller; used to return the
  236. * length of the used data
  237. * @alert: Buffer for returning an alert value on failure
  238. * Returns: Number of bytes used from in_data on success, 0 if record was not
  239. * complete (more data needed), or -1 on failure
  240. *
  241. * This function decrypts the received message, verifies HMAC and TLS record
  242. * layer header.
  243. */
  244. int tlsv1_record_receive(struct tlsv1_record_layer *rl,
  245. const u8 *in_data, size_t in_len,
  246. u8 *out_data, size_t *out_len, u8 *alert)
  247. {
  248. size_t i, rlen, hlen;
  249. u8 padlen;
  250. struct crypto_hash *hmac;
  251. u8 len[2], hash[100];
  252. int force_mac_error = 0;
  253. u8 ct;
  254. if (in_len < TLS_RECORD_HEADER_LEN) {
  255. wpa_printf(MSG_DEBUG, "TLSv1: Too short record (in_len=%lu) - "
  256. "need more data",
  257. (unsigned long) in_len);
  258. wpa_hexdump(MSG_MSGDUMP, "TLSv1: Record Layer - Received",
  259. in_data, in_len);
  260. return 0;
  261. }
  262. ct = in_data[0];
  263. rlen = WPA_GET_BE16(in_data + 3);
  264. wpa_printf(MSG_DEBUG, "TLSv1: Received content type %d version %d.%d "
  265. "length %d", ct, in_data[1], in_data[2], (int) rlen);
  266. /*
  267. * TLS v1.0 and v1.1 RFCs were not exactly clear on the use of the
  268. * protocol version in record layer. As such, accept any {03,xx} value
  269. * to remain compatible with existing implementations.
  270. */
  271. if (in_data[1] != 0x03) {
  272. wpa_printf(MSG_DEBUG, "TLSv1: Unexpected protocol version "
  273. "%u.%u", in_data[1], in_data[2]);
  274. *alert = TLS_ALERT_PROTOCOL_VERSION;
  275. return -1;
  276. }
  277. /* TLSCiphertext must not be more than 2^14+2048 bytes */
  278. if (TLS_RECORD_HEADER_LEN + rlen > 18432) {
  279. wpa_printf(MSG_DEBUG, "TLSv1: Record overflow (len=%lu)",
  280. (unsigned long) (TLS_RECORD_HEADER_LEN + rlen));
  281. *alert = TLS_ALERT_RECORD_OVERFLOW;
  282. return -1;
  283. }
  284. in_data += TLS_RECORD_HEADER_LEN;
  285. in_len -= TLS_RECORD_HEADER_LEN;
  286. if (rlen > in_len) {
  287. wpa_printf(MSG_DEBUG, "TLSv1: Not all record data included "
  288. "(rlen=%lu > in_len=%lu)",
  289. (unsigned long) rlen, (unsigned long) in_len);
  290. return 0;
  291. }
  292. wpa_hexdump(MSG_MSGDUMP, "TLSv1: Record Layer - Received",
  293. in_data, rlen);
  294. if (ct != TLS_CONTENT_TYPE_HANDSHAKE &&
  295. ct != TLS_CONTENT_TYPE_CHANGE_CIPHER_SPEC &&
  296. ct != TLS_CONTENT_TYPE_ALERT &&
  297. ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
  298. wpa_printf(MSG_DEBUG, "TLSv1: Ignore record with unknown "
  299. "content type 0x%x", ct);
  300. *alert = TLS_ALERT_UNEXPECTED_MESSAGE;
  301. return -1;
  302. }
  303. in_len = rlen;
  304. if (*out_len < in_len) {
  305. wpa_printf(MSG_DEBUG, "TLSv1: Not enough output buffer for "
  306. "processing received record");
  307. *alert = TLS_ALERT_INTERNAL_ERROR;
  308. return -1;
  309. }
  310. if (rl->read_cipher_suite != TLS_NULL_WITH_NULL_NULL) {
  311. size_t plen;
  312. if (crypto_cipher_decrypt(rl->read_cbc, in_data,
  313. out_data, in_len) < 0) {
  314. *alert = TLS_ALERT_DECRYPTION_FAILED;
  315. return -1;
  316. }
  317. plen = in_len;
  318. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Record Layer - Decrypted "
  319. "data", out_data, plen);
  320. if (rl->iv_size) {
  321. /*
  322. * TLS v1.0 defines different alert values for various
  323. * failures. That may information to aid in attacks, so
  324. * use the same bad_record_mac alert regardless of the
  325. * issues.
  326. *
  327. * In addition, instead of returning immediately on
  328. * error, run through the MAC check to make timing
  329. * attacks more difficult.
  330. */
  331. if (rl->tls_version >= TLS_VERSION_1_1) {
  332. /* Remove opaque IV[Cipherspec.block_length] */
  333. if (plen < rl->iv_size) {
  334. wpa_printf(MSG_DEBUG, "TLSv1.1: Not "
  335. "enough room for IV");
  336. force_mac_error = 1;
  337. goto check_mac;
  338. }
  339. os_memmove(out_data, out_data + rl->iv_size,
  340. plen - rl->iv_size);
  341. plen -= rl->iv_size;
  342. }
  343. /* Verify and remove padding */
  344. if (plen == 0) {
  345. wpa_printf(MSG_DEBUG, "TLSv1: Too short record"
  346. " (no pad)");
  347. force_mac_error = 1;
  348. goto check_mac;
  349. }
  350. padlen = out_data[plen - 1];
  351. if (padlen >= plen) {
  352. wpa_printf(MSG_DEBUG, "TLSv1: Incorrect pad "
  353. "length (%u, plen=%lu) in "
  354. "received record",
  355. padlen, (unsigned long) plen);
  356. force_mac_error = 1;
  357. goto check_mac;
  358. }
  359. for (i = plen - padlen - 1; i < plen - 1; i++) {
  360. if (out_data[i] != padlen) {
  361. wpa_hexdump(MSG_DEBUG,
  362. "TLSv1: Invalid pad in "
  363. "received record",
  364. out_data + plen - padlen -
  365. 1, padlen + 1);
  366. force_mac_error = 1;
  367. goto check_mac;
  368. }
  369. }
  370. plen -= padlen + 1;
  371. wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Record Layer - "
  372. "Decrypted data with IV and padding "
  373. "removed", out_data, plen);
  374. }
  375. check_mac:
  376. if (plen < rl->hash_size) {
  377. wpa_printf(MSG_DEBUG, "TLSv1: Too short record; no "
  378. "hash value");
  379. *alert = TLS_ALERT_BAD_RECORD_MAC;
  380. return -1;
  381. }
  382. plen -= rl->hash_size;
  383. hmac = crypto_hash_init(rl->hash_alg, rl->read_mac_secret,
  384. rl->hash_size);
  385. if (hmac == NULL) {
  386. wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - Failed "
  387. "to initialize HMAC");
  388. *alert = TLS_ALERT_INTERNAL_ERROR;
  389. return -1;
  390. }
  391. crypto_hash_update(hmac, rl->read_seq_num, TLS_SEQ_NUM_LEN);
  392. /* type + version + length + fragment */
  393. crypto_hash_update(hmac, in_data - TLS_RECORD_HEADER_LEN, 3);
  394. WPA_PUT_BE16(len, plen);
  395. crypto_hash_update(hmac, len, 2);
  396. crypto_hash_update(hmac, out_data, plen);
  397. hlen = sizeof(hash);
  398. if (crypto_hash_finish(hmac, hash, &hlen) < 0) {
  399. wpa_printf(MSG_DEBUG, "TLSv1: Record Layer - Failed "
  400. "to calculate HMAC");
  401. *alert = TLS_ALERT_INTERNAL_ERROR;
  402. return -1;
  403. }
  404. if (hlen != rl->hash_size ||
  405. os_memcmp_const(hash, out_data + plen, hlen) != 0 ||
  406. force_mac_error) {
  407. wpa_printf(MSG_DEBUG, "TLSv1: Invalid HMAC value in "
  408. "received message (force_mac_error=%d)",
  409. force_mac_error);
  410. *alert = TLS_ALERT_BAD_RECORD_MAC;
  411. return -1;
  412. }
  413. *out_len = plen;
  414. } else {
  415. os_memcpy(out_data, in_data, in_len);
  416. *out_len = in_len;
  417. }
  418. /* TLSCompressed must not be more than 2^14+1024 bytes */
  419. if (TLS_RECORD_HEADER_LEN + *out_len > 17408) {
  420. wpa_printf(MSG_DEBUG, "TLSv1: Record overflow (len=%lu)",
  421. (unsigned long) (TLS_RECORD_HEADER_LEN + *out_len));
  422. *alert = TLS_ALERT_RECORD_OVERFLOW;
  423. return -1;
  424. }
  425. inc_byte_array(rl->read_seq_num, TLS_SEQ_NUM_LEN);
  426. return TLS_RECORD_HEADER_LEN + rlen;
  427. }