eap_fast_common.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * EAP-FAST common helper functions (RFC 4851)
  3. * Copyright (c) 2008, 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/sha1.h"
  11. #include "crypto/tls.h"
  12. #include "eap_defs.h"
  13. #include "eap_tlv_common.h"
  14. #include "eap_fast_common.h"
  15. void eap_fast_put_tlv_hdr(struct wpabuf *buf, u16 type, u16 len)
  16. {
  17. struct pac_tlv_hdr hdr;
  18. hdr.type = host_to_be16(type);
  19. hdr.len = host_to_be16(len);
  20. wpabuf_put_data(buf, &hdr, sizeof(hdr));
  21. }
  22. void eap_fast_put_tlv(struct wpabuf *buf, u16 type, const void *data,
  23. u16 len)
  24. {
  25. eap_fast_put_tlv_hdr(buf, type, len);
  26. wpabuf_put_data(buf, data, len);
  27. }
  28. void eap_fast_put_tlv_buf(struct wpabuf *buf, u16 type,
  29. const struct wpabuf *data)
  30. {
  31. eap_fast_put_tlv_hdr(buf, type, wpabuf_len(data));
  32. wpabuf_put_buf(buf, data);
  33. }
  34. struct wpabuf * eap_fast_tlv_eap_payload(struct wpabuf *buf)
  35. {
  36. struct wpabuf *e;
  37. if (buf == NULL)
  38. return NULL;
  39. /* Encapsulate EAP packet in EAP-Payload TLV */
  40. wpa_printf(MSG_DEBUG, "EAP-FAST: Add EAP-Payload TLV");
  41. e = wpabuf_alloc(sizeof(struct pac_tlv_hdr) + wpabuf_len(buf));
  42. if (e == NULL) {
  43. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
  44. "for TLV encapsulation");
  45. wpabuf_free(buf);
  46. return NULL;
  47. }
  48. eap_fast_put_tlv_buf(e,
  49. EAP_TLV_TYPE_MANDATORY | EAP_TLV_EAP_PAYLOAD_TLV,
  50. buf);
  51. wpabuf_free(buf);
  52. return e;
  53. }
  54. void eap_fast_derive_master_secret(const u8 *pac_key, const u8 *server_random,
  55. const u8 *client_random, u8 *master_secret)
  56. {
  57. #define TLS_RANDOM_LEN 32
  58. #define TLS_MASTER_SECRET_LEN 48
  59. u8 seed[2 * TLS_RANDOM_LEN];
  60. wpa_hexdump(MSG_DEBUG, "EAP-FAST: client_random",
  61. client_random, TLS_RANDOM_LEN);
  62. wpa_hexdump(MSG_DEBUG, "EAP-FAST: server_random",
  63. server_random, TLS_RANDOM_LEN);
  64. /*
  65. * RFC 4851, Section 5.1:
  66. * master_secret = T-PRF(PAC-Key, "PAC to master secret label hash",
  67. * server_random + client_random, 48)
  68. */
  69. os_memcpy(seed, server_random, TLS_RANDOM_LEN);
  70. os_memcpy(seed + TLS_RANDOM_LEN, client_random, TLS_RANDOM_LEN);
  71. sha1_t_prf(pac_key, EAP_FAST_PAC_KEY_LEN,
  72. "PAC to master secret label hash",
  73. seed, sizeof(seed), master_secret, TLS_MASTER_SECRET_LEN);
  74. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: master_secret",
  75. master_secret, TLS_MASTER_SECRET_LEN);
  76. }
  77. u8 * eap_fast_derive_key(void *ssl_ctx, struct tls_connection *conn,
  78. const char *label, size_t len)
  79. {
  80. struct tls_keys keys;
  81. u8 *rnd = NULL, *out;
  82. int block_size;
  83. block_size = tls_connection_get_keyblock_size(ssl_ctx, conn);
  84. if (block_size < 0)
  85. return NULL;
  86. out = os_malloc(block_size + len);
  87. if (out == NULL)
  88. return NULL;
  89. if (tls_connection_prf(ssl_ctx, conn, label, 1, out, block_size + len)
  90. == 0) {
  91. os_memmove(out, out + block_size, len);
  92. return out;
  93. }
  94. if (tls_connection_get_keys(ssl_ctx, conn, &keys))
  95. goto fail;
  96. rnd = os_malloc(keys.client_random_len + keys.server_random_len);
  97. if (rnd == NULL)
  98. goto fail;
  99. os_memcpy(rnd, keys.server_random, keys.server_random_len);
  100. os_memcpy(rnd + keys.server_random_len, keys.client_random,
  101. keys.client_random_len);
  102. wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: master_secret for key "
  103. "expansion", keys.master_key, keys.master_key_len);
  104. if (tls_prf_sha1_md5(keys.master_key, keys.master_key_len,
  105. label, rnd, keys.client_random_len +
  106. keys.server_random_len, out, block_size + len))
  107. goto fail;
  108. os_free(rnd);
  109. os_memmove(out, out + block_size, len);
  110. return out;
  111. fail:
  112. os_free(rnd);
  113. os_free(out);
  114. return NULL;
  115. }
  116. void eap_fast_derive_eap_msk(const u8 *simck, u8 *msk)
  117. {
  118. /*
  119. * RFC 4851, Section 5.4: EAP Master Session Key Generation
  120. * MSK = T-PRF(S-IMCK[j], "Session Key Generating Function", 64)
  121. */
  122. sha1_t_prf(simck, EAP_FAST_SIMCK_LEN,
  123. "Session Key Generating Function", (u8 *) "", 0,
  124. msk, EAP_FAST_KEY_LEN);
  125. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (MSK)",
  126. msk, EAP_FAST_KEY_LEN);
  127. }
  128. void eap_fast_derive_eap_emsk(const u8 *simck, u8 *emsk)
  129. {
  130. /*
  131. * RFC 4851, Section 5.4: EAP Master Session Key Genreration
  132. * EMSK = T-PRF(S-IMCK[j],
  133. * "Extended Session Key Generating Function", 64)
  134. */
  135. sha1_t_prf(simck, EAP_FAST_SIMCK_LEN,
  136. "Extended Session Key Generating Function", (u8 *) "", 0,
  137. emsk, EAP_EMSK_LEN);
  138. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (EMSK)",
  139. emsk, EAP_EMSK_LEN);
  140. }
  141. int eap_fast_parse_tlv(struct eap_fast_tlv_parse *tlv,
  142. int tlv_type, u8 *pos, int len)
  143. {
  144. switch (tlv_type) {
  145. case EAP_TLV_EAP_PAYLOAD_TLV:
  146. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: EAP-Payload TLV",
  147. pos, len);
  148. if (tlv->eap_payload_tlv) {
  149. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  150. "EAP-Payload TLV in the message");
  151. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  152. return -2;
  153. }
  154. tlv->eap_payload_tlv = pos;
  155. tlv->eap_payload_tlv_len = len;
  156. break;
  157. case EAP_TLV_RESULT_TLV:
  158. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Result TLV", pos, len);
  159. if (tlv->result) {
  160. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  161. "Result TLV in the message");
  162. tlv->result = EAP_TLV_RESULT_FAILURE;
  163. return -2;
  164. }
  165. if (len < 2) {
  166. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  167. "Result TLV");
  168. tlv->result = EAP_TLV_RESULT_FAILURE;
  169. break;
  170. }
  171. tlv->result = WPA_GET_BE16(pos);
  172. if (tlv->result != EAP_TLV_RESULT_SUCCESS &&
  173. tlv->result != EAP_TLV_RESULT_FAILURE) {
  174. wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Result %d",
  175. tlv->result);
  176. tlv->result = EAP_TLV_RESULT_FAILURE;
  177. }
  178. wpa_printf(MSG_DEBUG, "EAP-FAST: Result: %s",
  179. tlv->result == EAP_TLV_RESULT_SUCCESS ?
  180. "Success" : "Failure");
  181. break;
  182. case EAP_TLV_INTERMEDIATE_RESULT_TLV:
  183. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Intermediate Result TLV",
  184. pos, len);
  185. if (len < 2) {
  186. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  187. "Intermediate-Result TLV");
  188. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  189. break;
  190. }
  191. if (tlv->iresult) {
  192. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  193. "Intermediate-Result TLV in the message");
  194. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  195. return -2;
  196. }
  197. tlv->iresult = WPA_GET_BE16(pos);
  198. if (tlv->iresult != EAP_TLV_RESULT_SUCCESS &&
  199. tlv->iresult != EAP_TLV_RESULT_FAILURE) {
  200. wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Intermediate "
  201. "Result %d", tlv->iresult);
  202. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  203. }
  204. wpa_printf(MSG_DEBUG, "EAP-FAST: Intermediate Result: %s",
  205. tlv->iresult == EAP_TLV_RESULT_SUCCESS ?
  206. "Success" : "Failure");
  207. break;
  208. case EAP_TLV_CRYPTO_BINDING_TLV:
  209. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV",
  210. pos, len);
  211. if (tlv->crypto_binding) {
  212. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  213. "Crypto-Binding TLV in the message");
  214. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  215. return -2;
  216. }
  217. tlv->crypto_binding_len = sizeof(struct eap_tlv_hdr) + len;
  218. if (tlv->crypto_binding_len < sizeof(*tlv->crypto_binding)) {
  219. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  220. "Crypto-Binding TLV");
  221. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  222. return -2;
  223. }
  224. tlv->crypto_binding = (struct eap_tlv_crypto_binding_tlv *)
  225. (pos - sizeof(struct eap_tlv_hdr));
  226. break;
  227. case EAP_TLV_REQUEST_ACTION_TLV:
  228. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Request-Action TLV",
  229. pos, len);
  230. if (tlv->request_action) {
  231. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  232. "Request-Action TLV in the message");
  233. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  234. return -2;
  235. }
  236. if (len < 2) {
  237. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  238. "Request-Action TLV");
  239. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  240. break;
  241. }
  242. tlv->request_action = WPA_GET_BE16(pos);
  243. wpa_printf(MSG_DEBUG, "EAP-FAST: Request-Action: %d",
  244. tlv->request_action);
  245. break;
  246. case EAP_TLV_PAC_TLV:
  247. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: PAC TLV", pos, len);
  248. if (tlv->pac) {
  249. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  250. "PAC TLV in the message");
  251. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  252. return -2;
  253. }
  254. tlv->pac = pos;
  255. tlv->pac_len = len;
  256. break;
  257. default:
  258. /* Unknown TLV */
  259. return -1;
  260. }
  261. return 0;
  262. }