eap_sake_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * EAP server/peer: EAP-SAKE shared routines
  3. * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "wpabuf.h"
  17. #include "crypto/sha1.h"
  18. #include "eap_defs.h"
  19. #include "eap_sake_common.h"
  20. static int eap_sake_parse_add_attr(struct eap_sake_parse_attr *attr,
  21. const u8 *pos)
  22. {
  23. size_t i;
  24. switch (pos[0]) {
  25. case EAP_SAKE_AT_RAND_S:
  26. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_RAND_S");
  27. if (pos[1] != 2 + EAP_SAKE_RAND_LEN) {
  28. wpa_printf(MSG_DEBUG, "EAP-SAKE: AT_RAND_S with "
  29. "invalid length %d", pos[1]);
  30. return -1;
  31. }
  32. attr->rand_s = pos + 2;
  33. break;
  34. case EAP_SAKE_AT_RAND_P:
  35. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_RAND_P");
  36. if (pos[1] != 2 + EAP_SAKE_RAND_LEN) {
  37. wpa_printf(MSG_DEBUG, "EAP-SAKE: AT_RAND_P with "
  38. "invalid length %d", pos[1]);
  39. return -1;
  40. }
  41. attr->rand_p = pos + 2;
  42. break;
  43. case EAP_SAKE_AT_MIC_S:
  44. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_MIC_S");
  45. if (pos[1] != 2 + EAP_SAKE_MIC_LEN) {
  46. wpa_printf(MSG_DEBUG, "EAP-SAKE: AT_MIC_S with "
  47. "invalid length %d", pos[1]);
  48. return -1;
  49. }
  50. attr->mic_s = pos + 2;
  51. break;
  52. case EAP_SAKE_AT_MIC_P:
  53. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_MIC_P");
  54. if (pos[1] != 2 + EAP_SAKE_MIC_LEN) {
  55. wpa_printf(MSG_DEBUG, "EAP-SAKE: AT_MIC_P with "
  56. "invalid length %d", pos[1]);
  57. return -1;
  58. }
  59. attr->mic_p = pos + 2;
  60. break;
  61. case EAP_SAKE_AT_SERVERID:
  62. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_SERVERID");
  63. attr->serverid = pos + 2;
  64. attr->serverid_len = pos[1] - 2;
  65. break;
  66. case EAP_SAKE_AT_PEERID:
  67. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_PEERID");
  68. attr->peerid = pos + 2;
  69. attr->peerid_len = pos[1] - 2;
  70. break;
  71. case EAP_SAKE_AT_SPI_S:
  72. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_SPI_S");
  73. attr->spi_s = pos + 2;
  74. attr->spi_s_len = pos[1] - 2;
  75. break;
  76. case EAP_SAKE_AT_SPI_P:
  77. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_SPI_P");
  78. attr->spi_p = pos + 2;
  79. attr->spi_p_len = pos[1] - 2;
  80. break;
  81. case EAP_SAKE_AT_ANY_ID_REQ:
  82. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_ANY_ID_REQ");
  83. if (pos[1] != 4) {
  84. wpa_printf(MSG_DEBUG, "EAP-SAKE: Invalid AT_ANY_ID_REQ"
  85. " length %d", pos[1]);
  86. return -1;
  87. }
  88. attr->any_id_req = pos + 2;
  89. break;
  90. case EAP_SAKE_AT_PERM_ID_REQ:
  91. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_PERM_ID_REQ");
  92. if (pos[1] != 4) {
  93. wpa_printf(MSG_DEBUG, "EAP-SAKE: Invalid "
  94. "AT_PERM_ID_REQ length %d", pos[1]);
  95. return -1;
  96. }
  97. attr->perm_id_req = pos + 2;
  98. break;
  99. case EAP_SAKE_AT_ENCR_DATA:
  100. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_ENCR_DATA");
  101. attr->encr_data = pos + 2;
  102. attr->encr_data_len = pos[1] - 2;
  103. break;
  104. case EAP_SAKE_AT_IV:
  105. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_IV");
  106. attr->iv = pos + 2;
  107. attr->iv_len = pos[1] - 2;
  108. break;
  109. case EAP_SAKE_AT_PADDING:
  110. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_PADDING");
  111. for (i = 2; i < pos[1]; i++) {
  112. if (pos[i]) {
  113. wpa_printf(MSG_DEBUG, "EAP-SAKE: AT_PADDING "
  114. "with non-zero pad byte");
  115. return -1;
  116. }
  117. }
  118. break;
  119. case EAP_SAKE_AT_NEXT_TMPID:
  120. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_NEXT_TMPID");
  121. attr->next_tmpid = pos + 2;
  122. attr->next_tmpid_len = pos[1] - 2;
  123. break;
  124. case EAP_SAKE_AT_MSK_LIFE:
  125. wpa_printf(MSG_DEBUG, "EAP-SAKE: Parse: AT_IV");
  126. if (pos[1] != 6) {
  127. wpa_printf(MSG_DEBUG, "EAP-SAKE: Invalid "
  128. "AT_MSK_LIFE length %d", pos[1]);
  129. return -1;
  130. }
  131. attr->msk_life = pos + 2;
  132. break;
  133. default:
  134. if (pos[0] < 128) {
  135. wpa_printf(MSG_DEBUG, "EAP-SAKE: Unknown non-skippable"
  136. " attribute %d", pos[0]);
  137. return -1;
  138. }
  139. wpa_printf(MSG_DEBUG, "EAP-SAKE: Ignoring unknown skippable "
  140. "attribute %d", pos[0]);
  141. break;
  142. }
  143. if (attr->iv && !attr->encr_data) {
  144. wpa_printf(MSG_DEBUG, "EAP-SAKE: AT_IV included without "
  145. "AT_ENCR_DATA");
  146. return -1;
  147. }
  148. return 0;
  149. }
  150. /**
  151. * eap_sake_parse_attributes - Parse EAP-SAKE attributes
  152. * @buf: Packet payload (starting with the first attribute)
  153. * @len: Payload length
  154. * @attr: Structure to be filled with found attributes
  155. * Returns: 0 on success or -1 on failure
  156. */
  157. int eap_sake_parse_attributes(const u8 *buf, size_t len,
  158. struct eap_sake_parse_attr *attr)
  159. {
  160. const u8 *pos = buf, *end = buf + len;
  161. os_memset(attr, 0, sizeof(*attr));
  162. while (pos < end) {
  163. if (end - pos < 2) {
  164. wpa_printf(MSG_DEBUG, "EAP-SAKE: Too short attribute");
  165. return -1;
  166. }
  167. if (pos[1] < 2) {
  168. wpa_printf(MSG_DEBUG, "EAP-SAKE: Invalid attribute "
  169. "length (%d)", pos[1]);
  170. return -1;
  171. }
  172. if (pos + pos[1] > end) {
  173. wpa_printf(MSG_DEBUG, "EAP-SAKE: Attribute underflow");
  174. return -1;
  175. }
  176. if (eap_sake_parse_add_attr(attr, pos))
  177. return -1;
  178. pos += pos[1];
  179. }
  180. return 0;
  181. }
  182. /**
  183. * eap_sake_kdf - EAP-SAKE Key Derivation Function (KDF)
  184. * @key: Key for KDF
  185. * @key_len: Length of the key in bytes
  186. * @label: A unique label for each purpose of the KDF
  187. * @data: Extra data (start) to bind into the key
  188. * @data_len: Length of the data
  189. * @data2: Extra data (end) to bind into the key
  190. * @data2_len: Length of the data2
  191. * @buf: Buffer for the generated pseudo-random key
  192. * @buf_len: Number of bytes of key to generate
  193. *
  194. * This function is used to derive new, cryptographically separate keys from a
  195. * given key (e.g., SMS). This is identical to the PRF used in IEEE 802.11i.
  196. */
  197. static void eap_sake_kdf(const u8 *key, size_t key_len, const char *label,
  198. const u8 *data, size_t data_len,
  199. const u8 *data2, size_t data2_len,
  200. u8 *buf, size_t buf_len)
  201. {
  202. u8 counter = 0;
  203. size_t pos, plen;
  204. u8 hash[SHA1_MAC_LEN];
  205. size_t label_len = os_strlen(label) + 1;
  206. const unsigned char *addr[4];
  207. size_t len[4];
  208. addr[0] = (u8 *) label; /* Label | Y */
  209. len[0] = label_len;
  210. addr[1] = data; /* Msg[start] */
  211. len[1] = data_len;
  212. addr[2] = data2; /* Msg[end] */
  213. len[2] = data2_len;
  214. addr[3] = &counter; /* Length */
  215. len[3] = 1;
  216. pos = 0;
  217. while (pos < buf_len) {
  218. plen = buf_len - pos;
  219. if (plen >= SHA1_MAC_LEN) {
  220. hmac_sha1_vector(key, key_len, 4, addr, len,
  221. &buf[pos]);
  222. pos += SHA1_MAC_LEN;
  223. } else {
  224. hmac_sha1_vector(key, key_len, 4, addr, len,
  225. hash);
  226. os_memcpy(&buf[pos], hash, plen);
  227. break;
  228. }
  229. counter++;
  230. }
  231. }
  232. /**
  233. * eap_sake_derive_keys - Derive EAP-SAKE keys
  234. * @root_secret_a: 16-byte Root-Secret-A
  235. * @root_secret_b: 16-byte Root-Secret-B
  236. * @rand_s: 16-byte RAND_S
  237. * @rand_p: 16-byte RAND_P
  238. * @tek: Buffer for Temporary EAK Keys (TEK-Auth[16] | TEK-Cipher[16])
  239. * @msk: Buffer for 64-byte MSK
  240. * @emsk: Buffer for 64-byte EMSK
  241. *
  242. * This function derives EAP-SAKE keys as defined in RFC 4763, section 3.2.6.
  243. */
  244. void eap_sake_derive_keys(const u8 *root_secret_a, const u8 *root_secret_b,
  245. const u8 *rand_s, const u8 *rand_p, u8 *tek, u8 *msk,
  246. u8 *emsk)
  247. {
  248. u8 sms_a[EAP_SAKE_SMS_LEN];
  249. u8 sms_b[EAP_SAKE_SMS_LEN];
  250. u8 key_buf[EAP_MSK_LEN + EAP_EMSK_LEN];
  251. wpa_printf(MSG_DEBUG, "EAP-SAKE: Deriving keys");
  252. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: Root-Secret-A",
  253. root_secret_a, EAP_SAKE_ROOT_SECRET_LEN);
  254. eap_sake_kdf(root_secret_a, EAP_SAKE_ROOT_SECRET_LEN,
  255. "SAKE Master Secret A",
  256. rand_p, EAP_SAKE_RAND_LEN, rand_s, EAP_SAKE_RAND_LEN,
  257. sms_a, EAP_SAKE_SMS_LEN);
  258. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: SMS-A", sms_a, EAP_SAKE_SMS_LEN);
  259. eap_sake_kdf(sms_a, EAP_SAKE_SMS_LEN, "Transient EAP Key",
  260. rand_s, EAP_SAKE_RAND_LEN, rand_p, EAP_SAKE_RAND_LEN,
  261. tek, EAP_SAKE_TEK_LEN);
  262. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: TEK-Auth",
  263. tek, EAP_SAKE_TEK_AUTH_LEN);
  264. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: TEK-Cipher",
  265. tek + EAP_SAKE_TEK_AUTH_LEN, EAP_SAKE_TEK_CIPHER_LEN);
  266. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: Root-Secret-B",
  267. root_secret_b, EAP_SAKE_ROOT_SECRET_LEN);
  268. eap_sake_kdf(root_secret_b, EAP_SAKE_ROOT_SECRET_LEN,
  269. "SAKE Master Secret B",
  270. rand_p, EAP_SAKE_RAND_LEN, rand_s, EAP_SAKE_RAND_LEN,
  271. sms_b, EAP_SAKE_SMS_LEN);
  272. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: SMS-B", sms_b, EAP_SAKE_SMS_LEN);
  273. eap_sake_kdf(sms_b, EAP_SAKE_SMS_LEN, "Master Session Key",
  274. rand_s, EAP_SAKE_RAND_LEN, rand_p, EAP_SAKE_RAND_LEN,
  275. key_buf, sizeof(key_buf));
  276. os_memcpy(msk, key_buf, EAP_MSK_LEN);
  277. os_memcpy(emsk, key_buf + EAP_MSK_LEN, EAP_EMSK_LEN);
  278. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: MSK", msk, EAP_MSK_LEN);
  279. wpa_hexdump_key(MSG_DEBUG, "EAP-SAKE: EMSK", emsk, EAP_EMSK_LEN);
  280. }
  281. /**
  282. * eap_sake_compute_mic - Compute EAP-SAKE MIC for an EAP packet
  283. * @tek_auth: 16-byte TEK-Auth
  284. * @rand_s: 16-byte RAND_S
  285. * @rand_p: 16-byte RAND_P
  286. * @serverid: SERVERID
  287. * @serverid_len: SERVERID length
  288. * @peerid: PEERID
  289. * @peerid_len: PEERID length
  290. * @peer: MIC calculation for 0 = Server, 1 = Peer message
  291. * @eap: EAP packet
  292. * @eap_len: EAP packet length
  293. * @mic_pos: MIC position in the EAP packet (must be [eap .. eap + eap_len])
  294. * @mic: Buffer for the computed 16-byte MIC
  295. */
  296. int eap_sake_compute_mic(const u8 *tek_auth,
  297. const u8 *rand_s, const u8 *rand_p,
  298. const u8 *serverid, size_t serverid_len,
  299. const u8 *peerid, size_t peerid_len,
  300. int peer, const u8 *eap, size_t eap_len,
  301. const u8 *mic_pos, u8 *mic)
  302. {
  303. u8 _rand[2 * EAP_SAKE_RAND_LEN];
  304. u8 *tmp, *pos;
  305. size_t tmplen;
  306. tmplen = serverid_len + 1 + peerid_len + 1 + eap_len;
  307. tmp = os_malloc(tmplen);
  308. if (tmp == NULL)
  309. return -1;
  310. pos = tmp;
  311. if (peer) {
  312. if (peerid) {
  313. os_memcpy(pos, peerid, peerid_len);
  314. pos += peerid_len;
  315. }
  316. *pos++ = 0x00;
  317. if (serverid) {
  318. os_memcpy(pos, serverid, serverid_len);
  319. pos += serverid_len;
  320. }
  321. *pos++ = 0x00;
  322. os_memcpy(_rand, rand_s, EAP_SAKE_RAND_LEN);
  323. os_memcpy(_rand + EAP_SAKE_RAND_LEN, rand_p,
  324. EAP_SAKE_RAND_LEN);
  325. } else {
  326. if (serverid) {
  327. os_memcpy(pos, serverid, serverid_len);
  328. pos += serverid_len;
  329. }
  330. *pos++ = 0x00;
  331. if (peerid) {
  332. os_memcpy(pos, peerid, peerid_len);
  333. pos += peerid_len;
  334. }
  335. *pos++ = 0x00;
  336. os_memcpy(_rand, rand_p, EAP_SAKE_RAND_LEN);
  337. os_memcpy(_rand + EAP_SAKE_RAND_LEN, rand_s,
  338. EAP_SAKE_RAND_LEN);
  339. }
  340. os_memcpy(pos, eap, eap_len);
  341. os_memset(pos + (mic_pos - eap), 0, EAP_SAKE_MIC_LEN);
  342. eap_sake_kdf(tek_auth, EAP_SAKE_TEK_AUTH_LEN,
  343. peer ? "Peer MIC" : "Server MIC",
  344. _rand, 2 * EAP_SAKE_RAND_LEN, tmp, tmplen,
  345. mic, EAP_SAKE_MIC_LEN);
  346. os_free(tmp);
  347. return 0;
  348. }
  349. void eap_sake_add_attr(struct wpabuf *buf, u8 type, const u8 *data,
  350. size_t len)
  351. {
  352. wpabuf_put_u8(buf, type);
  353. wpabuf_put_u8(buf, 2 + len); /* Length; including attr header */
  354. if (data)
  355. wpabuf_put_data(buf, data, len);
  356. else
  357. os_memset(wpabuf_put(buf, len), 0, len);
  358. }