eap_sake_common.c 10 KB

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