eap_fast_common.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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, size_t len)
  78. {
  79. u8 *out;
  80. out = os_malloc(len);
  81. if (out == NULL)
  82. return NULL;
  83. if (tls_connection_get_eap_fast_key(ssl_ctx, conn, out, len)) {
  84. os_free(out);
  85. return NULL;
  86. }
  87. return out;
  88. }
  89. int eap_fast_derive_eap_msk(const u8 *simck, u8 *msk)
  90. {
  91. /*
  92. * RFC 4851, Section 5.4: EAP Master Session Key Generation
  93. * MSK = T-PRF(S-IMCK[j], "Session Key Generating Function", 64)
  94. */
  95. if (sha1_t_prf(simck, EAP_FAST_SIMCK_LEN,
  96. "Session Key Generating Function", (u8 *) "", 0,
  97. msk, EAP_FAST_KEY_LEN) < 0)
  98. return -1;
  99. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (MSK)",
  100. msk, EAP_FAST_KEY_LEN);
  101. return 0;
  102. }
  103. int eap_fast_derive_eap_emsk(const u8 *simck, u8 *emsk)
  104. {
  105. /*
  106. * RFC 4851, Section 5.4: EAP Master Session Key Genreration
  107. * EMSK = T-PRF(S-IMCK[j],
  108. * "Extended Session Key Generating Function", 64)
  109. */
  110. if (sha1_t_prf(simck, EAP_FAST_SIMCK_LEN,
  111. "Extended Session Key Generating Function", (u8 *) "", 0,
  112. emsk, EAP_EMSK_LEN) < 0)
  113. return -1;
  114. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (EMSK)",
  115. emsk, EAP_EMSK_LEN);
  116. return 0;
  117. }
  118. int eap_fast_parse_tlv(struct eap_fast_tlv_parse *tlv,
  119. int tlv_type, u8 *pos, size_t len)
  120. {
  121. switch (tlv_type) {
  122. case EAP_TLV_EAP_PAYLOAD_TLV:
  123. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: EAP-Payload TLV",
  124. pos, len);
  125. if (tlv->eap_payload_tlv) {
  126. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  127. "EAP-Payload TLV in the message");
  128. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  129. return -2;
  130. }
  131. tlv->eap_payload_tlv = pos;
  132. tlv->eap_payload_tlv_len = len;
  133. break;
  134. case EAP_TLV_RESULT_TLV:
  135. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Result TLV", pos, len);
  136. if (tlv->result) {
  137. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  138. "Result TLV in the message");
  139. tlv->result = EAP_TLV_RESULT_FAILURE;
  140. return -2;
  141. }
  142. if (len < 2) {
  143. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  144. "Result TLV");
  145. tlv->result = EAP_TLV_RESULT_FAILURE;
  146. break;
  147. }
  148. tlv->result = WPA_GET_BE16(pos);
  149. if (tlv->result != EAP_TLV_RESULT_SUCCESS &&
  150. tlv->result != EAP_TLV_RESULT_FAILURE) {
  151. wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Result %d",
  152. tlv->result);
  153. tlv->result = EAP_TLV_RESULT_FAILURE;
  154. }
  155. wpa_printf(MSG_DEBUG, "EAP-FAST: Result: %s",
  156. tlv->result == EAP_TLV_RESULT_SUCCESS ?
  157. "Success" : "Failure");
  158. break;
  159. case EAP_TLV_INTERMEDIATE_RESULT_TLV:
  160. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Intermediate Result TLV",
  161. pos, len);
  162. if (len < 2) {
  163. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  164. "Intermediate-Result TLV");
  165. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  166. break;
  167. }
  168. if (tlv->iresult) {
  169. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  170. "Intermediate-Result TLV in the message");
  171. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  172. return -2;
  173. }
  174. tlv->iresult = WPA_GET_BE16(pos);
  175. if (tlv->iresult != EAP_TLV_RESULT_SUCCESS &&
  176. tlv->iresult != EAP_TLV_RESULT_FAILURE) {
  177. wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Intermediate "
  178. "Result %d", tlv->iresult);
  179. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  180. }
  181. wpa_printf(MSG_DEBUG, "EAP-FAST: Intermediate Result: %s",
  182. tlv->iresult == EAP_TLV_RESULT_SUCCESS ?
  183. "Success" : "Failure");
  184. break;
  185. case EAP_TLV_CRYPTO_BINDING_TLV:
  186. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV",
  187. pos, len);
  188. if (tlv->crypto_binding) {
  189. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  190. "Crypto-Binding TLV in the message");
  191. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  192. return -2;
  193. }
  194. tlv->crypto_binding_len = sizeof(struct eap_tlv_hdr) + len;
  195. if (tlv->crypto_binding_len < sizeof(*tlv->crypto_binding)) {
  196. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  197. "Crypto-Binding TLV");
  198. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  199. return -2;
  200. }
  201. tlv->crypto_binding = (struct eap_tlv_crypto_binding_tlv *)
  202. (pos - sizeof(struct eap_tlv_hdr));
  203. break;
  204. case EAP_TLV_REQUEST_ACTION_TLV:
  205. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Request-Action TLV",
  206. pos, len);
  207. if (tlv->request_action) {
  208. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  209. "Request-Action TLV in the message");
  210. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  211. return -2;
  212. }
  213. if (len < 2) {
  214. wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
  215. "Request-Action TLV");
  216. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  217. break;
  218. }
  219. tlv->request_action = WPA_GET_BE16(pos);
  220. wpa_printf(MSG_DEBUG, "EAP-FAST: Request-Action: %d",
  221. tlv->request_action);
  222. break;
  223. case EAP_TLV_PAC_TLV:
  224. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: PAC TLV", pos, len);
  225. if (tlv->pac) {
  226. wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
  227. "PAC TLV in the message");
  228. tlv->iresult = EAP_TLV_RESULT_FAILURE;
  229. return -2;
  230. }
  231. tlv->pac = pos;
  232. tlv->pac_len = len;
  233. break;
  234. default:
  235. /* Unknown TLV */
  236. return -1;
  237. }
  238. return 0;
  239. }