radius.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. /*
  2. * RADIUS message processing
  3. * Copyright (c) 2002-2009, 2011-2015, 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 "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/wpabuf.h"
  11. #include "crypto/md5.h"
  12. #include "crypto/crypto.h"
  13. #include "radius.h"
  14. /**
  15. * struct radius_msg - RADIUS message structure for new and parsed messages
  16. */
  17. struct radius_msg {
  18. /**
  19. * buf - Allocated buffer for RADIUS message
  20. */
  21. struct wpabuf *buf;
  22. /**
  23. * hdr - Pointer to the RADIUS header in buf
  24. */
  25. struct radius_hdr *hdr;
  26. /**
  27. * attr_pos - Array of indexes to attributes
  28. *
  29. * The values are number of bytes from buf to the beginning of
  30. * struct radius_attr_hdr.
  31. */
  32. size_t *attr_pos;
  33. /**
  34. * attr_size - Total size of the attribute pointer array
  35. */
  36. size_t attr_size;
  37. /**
  38. * attr_used - Total number of attributes in the array
  39. */
  40. size_t attr_used;
  41. };
  42. struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg)
  43. {
  44. return msg->hdr;
  45. }
  46. struct wpabuf * radius_msg_get_buf(struct radius_msg *msg)
  47. {
  48. return msg->buf;
  49. }
  50. static struct radius_attr_hdr *
  51. radius_get_attr_hdr(struct radius_msg *msg, int idx)
  52. {
  53. return (struct radius_attr_hdr *)
  54. (wpabuf_mhead_u8(msg->buf) + msg->attr_pos[idx]);
  55. }
  56. static void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier)
  57. {
  58. msg->hdr->code = code;
  59. msg->hdr->identifier = identifier;
  60. }
  61. static int radius_msg_initialize(struct radius_msg *msg)
  62. {
  63. msg->attr_pos = os_calloc(RADIUS_DEFAULT_ATTR_COUNT,
  64. sizeof(*msg->attr_pos));
  65. if (msg->attr_pos == NULL)
  66. return -1;
  67. msg->attr_size = RADIUS_DEFAULT_ATTR_COUNT;
  68. msg->attr_used = 0;
  69. return 0;
  70. }
  71. /**
  72. * radius_msg_new - Create a new RADIUS message
  73. * @code: Code for RADIUS header
  74. * @identifier: Identifier for RADIUS header
  75. * Returns: Context for RADIUS message or %NULL on failure
  76. *
  77. * The caller is responsible for freeing the returned data with
  78. * radius_msg_free().
  79. */
  80. struct radius_msg * radius_msg_new(u8 code, u8 identifier)
  81. {
  82. struct radius_msg *msg;
  83. msg = os_zalloc(sizeof(*msg));
  84. if (msg == NULL)
  85. return NULL;
  86. msg->buf = wpabuf_alloc(RADIUS_DEFAULT_MSG_SIZE);
  87. if (msg->buf == NULL || radius_msg_initialize(msg)) {
  88. radius_msg_free(msg);
  89. return NULL;
  90. }
  91. msg->hdr = wpabuf_put(msg->buf, sizeof(struct radius_hdr));
  92. radius_msg_set_hdr(msg, code, identifier);
  93. return msg;
  94. }
  95. /**
  96. * radius_msg_free - Free a RADIUS message
  97. * @msg: RADIUS message from radius_msg_new() or radius_msg_parse()
  98. */
  99. void radius_msg_free(struct radius_msg *msg)
  100. {
  101. if (msg == NULL)
  102. return;
  103. wpabuf_free(msg->buf);
  104. os_free(msg->attr_pos);
  105. os_free(msg);
  106. }
  107. static const char *radius_code_string(u8 code)
  108. {
  109. switch (code) {
  110. case RADIUS_CODE_ACCESS_REQUEST: return "Access-Request";
  111. case RADIUS_CODE_ACCESS_ACCEPT: return "Access-Accept";
  112. case RADIUS_CODE_ACCESS_REJECT: return "Access-Reject";
  113. case RADIUS_CODE_ACCOUNTING_REQUEST: return "Accounting-Request";
  114. case RADIUS_CODE_ACCOUNTING_RESPONSE: return "Accounting-Response";
  115. case RADIUS_CODE_ACCESS_CHALLENGE: return "Access-Challenge";
  116. case RADIUS_CODE_STATUS_SERVER: return "Status-Server";
  117. case RADIUS_CODE_STATUS_CLIENT: return "Status-Client";
  118. case RADIUS_CODE_RESERVED: return "Reserved";
  119. case RADIUS_CODE_DISCONNECT_REQUEST: return "Disconnect-Request";
  120. case RADIUS_CODE_DISCONNECT_ACK: return "Disconnect-ACK";
  121. case RADIUS_CODE_DISCONNECT_NAK: return "Disconnect-NAK";
  122. case RADIUS_CODE_COA_REQUEST: return "CoA-Request";
  123. case RADIUS_CODE_COA_ACK: return "CoA-ACK";
  124. case RADIUS_CODE_COA_NAK: return "CoA-NAK";
  125. default: return "?Unknown?";
  126. }
  127. }
  128. struct radius_attr_type {
  129. u8 type;
  130. char *name;
  131. enum {
  132. RADIUS_ATTR_UNDIST, RADIUS_ATTR_TEXT, RADIUS_ATTR_IP,
  133. RADIUS_ATTR_HEXDUMP, RADIUS_ATTR_INT32, RADIUS_ATTR_IPV6
  134. } data_type;
  135. };
  136. static const struct radius_attr_type radius_attrs[] =
  137. {
  138. { RADIUS_ATTR_USER_NAME, "User-Name", RADIUS_ATTR_TEXT },
  139. { RADIUS_ATTR_USER_PASSWORD, "User-Password", RADIUS_ATTR_UNDIST },
  140. { RADIUS_ATTR_NAS_IP_ADDRESS, "NAS-IP-Address", RADIUS_ATTR_IP },
  141. { RADIUS_ATTR_NAS_PORT, "NAS-Port", RADIUS_ATTR_INT32 },
  142. { RADIUS_ATTR_SERVICE_TYPE, "Service-Type", RADIUS_ATTR_INT32 },
  143. { RADIUS_ATTR_FRAMED_IP_ADDRESS, "Framed-IP-Address", RADIUS_ATTR_IP },
  144. { RADIUS_ATTR_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
  145. { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
  146. { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
  147. { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
  148. { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
  149. { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
  150. { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
  151. { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
  152. RADIUS_ATTR_INT32 },
  153. { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
  154. RADIUS_ATTR_TEXT },
  155. { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
  156. RADIUS_ATTR_TEXT },
  157. { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
  158. { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
  159. { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
  160. RADIUS_ATTR_INT32 },
  161. { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
  162. { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
  163. RADIUS_ATTR_INT32 },
  164. { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
  165. RADIUS_ATTR_INT32 },
  166. { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
  167. { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
  168. { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
  169. RADIUS_ATTR_INT32 },
  170. { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
  171. RADIUS_ATTR_INT32 },
  172. { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
  173. RADIUS_ATTR_INT32 },
  174. { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
  175. RADIUS_ATTR_INT32 },
  176. { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
  177. RADIUS_ATTR_TEXT },
  178. { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
  179. { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
  180. RADIUS_ATTR_INT32 },
  181. { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
  182. RADIUS_ATTR_INT32 },
  183. { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
  184. RADIUS_ATTR_INT32 },
  185. { RADIUS_ATTR_EGRESS_VLANID, "EGRESS-VLANID", RADIUS_ATTR_HEXDUMP },
  186. { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
  187. { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
  188. { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
  189. RADIUS_ATTR_HEXDUMP },
  190. { RADIUS_ATTR_TUNNEL_PASSWORD, "Tunnel-Password",
  191. RADIUS_ATTR_UNDIST },
  192. { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
  193. { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
  194. { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
  195. RADIUS_ATTR_UNDIST },
  196. { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
  197. RADIUS_ATTR_HEXDUMP },
  198. { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
  199. RADIUS_ATTR_INT32 },
  200. { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
  201. RADIUS_ATTR_TEXT },
  202. { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
  203. { RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 },
  204. { RADIUS_ATTR_EAP_KEY_NAME, "EAP-Key-Name", RADIUS_ATTR_HEXDUMP },
  205. { RADIUS_ATTR_OPERATOR_NAME, "Operator-Name", RADIUS_ATTR_TEXT },
  206. { RADIUS_ATTR_LOCATION_INFO, "Location-Information",
  207. RADIUS_ATTR_HEXDUMP },
  208. { RADIUS_ATTR_LOCATION_DATA, "Location-Data", RADIUS_ATTR_HEXDUMP },
  209. { RADIUS_ATTR_BASIC_LOCATION_POLICY_RULES,
  210. "Basic-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
  211. { RADIUS_ATTR_EXTENDED_LOCATION_POLICY_RULES,
  212. "Extended-Location-Policy-Rules", RADIUS_ATTR_HEXDUMP },
  213. { RADIUS_ATTR_LOCATION_CAPABLE, "Location-Capable", RADIUS_ATTR_INT32 },
  214. { RADIUS_ATTR_REQUESTED_LOCATION_INFO, "Requested-Location-Info",
  215. RADIUS_ATTR_INT32 },
  216. { RADIUS_ATTR_MOBILITY_DOMAIN_ID, "Mobility-Domain-Id",
  217. RADIUS_ATTR_INT32 },
  218. { RADIUS_ATTR_WLAN_HESSID, "WLAN-HESSID", RADIUS_ATTR_TEXT },
  219. { RADIUS_ATTR_WLAN_PAIRWISE_CIPHER, "WLAN-Pairwise-Cipher",
  220. RADIUS_ATTR_HEXDUMP },
  221. { RADIUS_ATTR_WLAN_GROUP_CIPHER, "WLAN-Group-Cipher",
  222. RADIUS_ATTR_HEXDUMP },
  223. { RADIUS_ATTR_WLAN_AKM_SUITE, "WLAN-AKM-Suite",
  224. RADIUS_ATTR_HEXDUMP },
  225. { RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, "WLAN-Group-Mgmt-Pairwise-Cipher",
  226. RADIUS_ATTR_HEXDUMP },
  227. };
  228. #define RADIUS_ATTRS ARRAY_SIZE(radius_attrs)
  229. static const struct radius_attr_type *radius_get_attr_type(u8 type)
  230. {
  231. size_t i;
  232. for (i = 0; i < RADIUS_ATTRS; i++) {
  233. if (type == radius_attrs[i].type)
  234. return &radius_attrs[i];
  235. }
  236. return NULL;
  237. }
  238. static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
  239. {
  240. const struct radius_attr_type *attr;
  241. int len;
  242. unsigned char *pos;
  243. char buf[1000];
  244. attr = radius_get_attr_type(hdr->type);
  245. wpa_printf(MSG_INFO, " Attribute %d (%s) length=%d",
  246. hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
  247. if (attr == NULL || hdr->length < sizeof(struct radius_attr_hdr))
  248. return;
  249. len = hdr->length - sizeof(struct radius_attr_hdr);
  250. pos = (unsigned char *) (hdr + 1);
  251. switch (attr->data_type) {
  252. case RADIUS_ATTR_TEXT:
  253. printf_encode(buf, sizeof(buf), pos, len);
  254. wpa_printf(MSG_INFO, " Value: '%s'", buf);
  255. break;
  256. case RADIUS_ATTR_IP:
  257. if (len == 4) {
  258. struct in_addr addr;
  259. os_memcpy(&addr, pos, 4);
  260. wpa_printf(MSG_INFO, " Value: %s",
  261. inet_ntoa(addr));
  262. } else {
  263. wpa_printf(MSG_INFO, " Invalid IP address length %d",
  264. len);
  265. }
  266. break;
  267. #ifdef CONFIG_IPV6
  268. case RADIUS_ATTR_IPV6:
  269. if (len == 16) {
  270. const char *atxt;
  271. struct in6_addr *addr = (struct in6_addr *) pos;
  272. atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
  273. wpa_printf(MSG_INFO, " Value: %s",
  274. atxt ? atxt : "?");
  275. } else {
  276. wpa_printf(MSG_INFO, " Invalid IPv6 address length %d",
  277. len);
  278. }
  279. break;
  280. #endif /* CONFIG_IPV6 */
  281. case RADIUS_ATTR_HEXDUMP:
  282. case RADIUS_ATTR_UNDIST:
  283. wpa_snprintf_hex(buf, sizeof(buf), pos, len);
  284. wpa_printf(MSG_INFO, " Value: %s", buf);
  285. break;
  286. case RADIUS_ATTR_INT32:
  287. if (len == 4)
  288. wpa_printf(MSG_INFO, " Value: %u",
  289. WPA_GET_BE32(pos));
  290. else
  291. wpa_printf(MSG_INFO, " Invalid INT32 length %d",
  292. len);
  293. break;
  294. default:
  295. break;
  296. }
  297. }
  298. void radius_msg_dump(struct radius_msg *msg)
  299. {
  300. size_t i;
  301. wpa_printf(MSG_INFO, "RADIUS message: code=%d (%s) identifier=%d length=%d",
  302. msg->hdr->code, radius_code_string(msg->hdr->code),
  303. msg->hdr->identifier, be_to_host16(msg->hdr->length));
  304. for (i = 0; i < msg->attr_used; i++) {
  305. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  306. radius_msg_dump_attr(attr);
  307. }
  308. }
  309. int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
  310. size_t secret_len)
  311. {
  312. if (secret) {
  313. u8 auth[MD5_MAC_LEN];
  314. struct radius_attr_hdr *attr;
  315. os_memset(auth, 0, MD5_MAC_LEN);
  316. attr = radius_msg_add_attr(msg,
  317. RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  318. auth, MD5_MAC_LEN);
  319. if (attr == NULL) {
  320. wpa_printf(MSG_WARNING, "RADIUS: Could not add "
  321. "Message-Authenticator");
  322. return -1;
  323. }
  324. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  325. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  326. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  327. } else
  328. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  329. if (wpabuf_len(msg->buf) > 0xffff) {
  330. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  331. (unsigned long) wpabuf_len(msg->buf));
  332. return -1;
  333. }
  334. return 0;
  335. }
  336. int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
  337. size_t secret_len, const u8 *req_authenticator)
  338. {
  339. u8 auth[MD5_MAC_LEN];
  340. struct radius_attr_hdr *attr;
  341. const u8 *addr[4];
  342. size_t len[4];
  343. os_memset(auth, 0, MD5_MAC_LEN);
  344. attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  345. auth, MD5_MAC_LEN);
  346. if (attr == NULL) {
  347. wpa_printf(MSG_ERROR, "WARNING: Could not add Message-Authenticator");
  348. return -1;
  349. }
  350. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  351. os_memcpy(msg->hdr->authenticator, req_authenticator,
  352. sizeof(msg->hdr->authenticator));
  353. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  354. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  355. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  356. addr[0] = (u8 *) msg->hdr;
  357. len[0] = 1 + 1 + 2;
  358. addr[1] = req_authenticator;
  359. len[1] = MD5_MAC_LEN;
  360. addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
  361. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  362. addr[3] = secret;
  363. len[3] = secret_len;
  364. md5_vector(4, addr, len, msg->hdr->authenticator);
  365. if (wpabuf_len(msg->buf) > 0xffff) {
  366. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  367. (unsigned long) wpabuf_len(msg->buf));
  368. return -1;
  369. }
  370. return 0;
  371. }
  372. int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
  373. size_t secret_len,
  374. const struct radius_hdr *req_hdr)
  375. {
  376. const u8 *addr[2];
  377. size_t len[2];
  378. u8 auth[MD5_MAC_LEN];
  379. struct radius_attr_hdr *attr;
  380. os_memset(auth, 0, MD5_MAC_LEN);
  381. attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  382. auth, MD5_MAC_LEN);
  383. if (attr == NULL) {
  384. wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
  385. return -1;
  386. }
  387. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  388. os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
  389. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  390. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  391. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  392. addr[0] = wpabuf_head_u8(msg->buf);
  393. len[0] = wpabuf_len(msg->buf);
  394. addr[1] = secret;
  395. len[1] = secret_len;
  396. if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
  397. return -1;
  398. if (wpabuf_len(msg->buf) > 0xffff) {
  399. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  400. (unsigned long) wpabuf_len(msg->buf));
  401. return -1;
  402. }
  403. return 0;
  404. }
  405. void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
  406. size_t secret_len)
  407. {
  408. const u8 *addr[2];
  409. size_t len[2];
  410. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  411. os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
  412. addr[0] = wpabuf_head(msg->buf);
  413. len[0] = wpabuf_len(msg->buf);
  414. addr[1] = secret;
  415. len[1] = secret_len;
  416. md5_vector(2, addr, len, msg->hdr->authenticator);
  417. if (wpabuf_len(msg->buf) > 0xffff) {
  418. wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
  419. (unsigned long) wpabuf_len(msg->buf));
  420. }
  421. }
  422. void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
  423. size_t secret_len, const u8 *req_authenticator)
  424. {
  425. const u8 *addr[2];
  426. size_t len[2];
  427. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  428. os_memcpy(msg->hdr->authenticator, req_authenticator, MD5_MAC_LEN);
  429. addr[0] = wpabuf_head(msg->buf);
  430. len[0] = wpabuf_len(msg->buf);
  431. addr[1] = secret;
  432. len[1] = secret_len;
  433. md5_vector(2, addr, len, msg->hdr->authenticator);
  434. if (wpabuf_len(msg->buf) > 0xffff) {
  435. wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
  436. (unsigned long) wpabuf_len(msg->buf));
  437. }
  438. }
  439. int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
  440. size_t secret_len)
  441. {
  442. const u8 *addr[4];
  443. size_t len[4];
  444. u8 zero[MD5_MAC_LEN];
  445. u8 hash[MD5_MAC_LEN];
  446. os_memset(zero, 0, sizeof(zero));
  447. addr[0] = (u8 *) msg->hdr;
  448. len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
  449. addr[1] = zero;
  450. len[1] = MD5_MAC_LEN;
  451. addr[2] = (u8 *) (msg->hdr + 1);
  452. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  453. addr[3] = secret;
  454. len[3] = secret_len;
  455. md5_vector(4, addr, len, hash);
  456. return os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
  457. }
  458. int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
  459. size_t secret_len,
  460. int require_message_authenticator)
  461. {
  462. const u8 *addr[4];
  463. size_t len[4];
  464. u8 zero[MD5_MAC_LEN];
  465. u8 hash[MD5_MAC_LEN];
  466. u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
  467. u8 orig_authenticator[16];
  468. struct radius_attr_hdr *attr = NULL, *tmp;
  469. size_t i;
  470. os_memset(zero, 0, sizeof(zero));
  471. addr[0] = (u8 *) msg->hdr;
  472. len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
  473. addr[1] = zero;
  474. len[1] = MD5_MAC_LEN;
  475. addr[2] = (u8 *) (msg->hdr + 1);
  476. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  477. addr[3] = secret;
  478. len[3] = secret_len;
  479. md5_vector(4, addr, len, hash);
  480. if (os_memcmp_const(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
  481. return 1;
  482. for (i = 0; i < msg->attr_used; i++) {
  483. tmp = radius_get_attr_hdr(msg, i);
  484. if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
  485. if (attr != NULL) {
  486. wpa_printf(MSG_WARNING, "Multiple "
  487. "Message-Authenticator attributes "
  488. "in RADIUS message");
  489. return 1;
  490. }
  491. attr = tmp;
  492. }
  493. }
  494. if (attr == NULL) {
  495. if (require_message_authenticator) {
  496. wpa_printf(MSG_WARNING,
  497. "Missing Message-Authenticator attribute in RADIUS message");
  498. return 1;
  499. }
  500. return 0;
  501. }
  502. os_memcpy(orig, attr + 1, MD5_MAC_LEN);
  503. os_memset(attr + 1, 0, MD5_MAC_LEN);
  504. os_memcpy(orig_authenticator, msg->hdr->authenticator,
  505. sizeof(orig_authenticator));
  506. os_memset(msg->hdr->authenticator, 0,
  507. sizeof(msg->hdr->authenticator));
  508. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  509. wpabuf_len(msg->buf), auth);
  510. os_memcpy(attr + 1, orig, MD5_MAC_LEN);
  511. os_memcpy(msg->hdr->authenticator, orig_authenticator,
  512. sizeof(orig_authenticator));
  513. return os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0;
  514. }
  515. static int radius_msg_add_attr_to_array(struct radius_msg *msg,
  516. struct radius_attr_hdr *attr)
  517. {
  518. if (msg->attr_used >= msg->attr_size) {
  519. size_t *nattr_pos;
  520. int nlen = msg->attr_size * 2;
  521. nattr_pos = os_realloc_array(msg->attr_pos, nlen,
  522. sizeof(*msg->attr_pos));
  523. if (nattr_pos == NULL)
  524. return -1;
  525. msg->attr_pos = nattr_pos;
  526. msg->attr_size = nlen;
  527. }
  528. msg->attr_pos[msg->attr_used++] =
  529. (unsigned char *) attr - wpabuf_head_u8(msg->buf);
  530. return 0;
  531. }
  532. struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
  533. const u8 *data, size_t data_len)
  534. {
  535. size_t buf_needed;
  536. struct radius_attr_hdr *attr;
  537. if (data_len > RADIUS_MAX_ATTR_LEN) {
  538. wpa_printf(MSG_ERROR, "radius_msg_add_attr: too long attribute (%lu bytes)",
  539. (unsigned long) data_len);
  540. return NULL;
  541. }
  542. buf_needed = sizeof(*attr) + data_len;
  543. if (wpabuf_tailroom(msg->buf) < buf_needed) {
  544. /* allocate more space for message buffer */
  545. if (wpabuf_resize(&msg->buf, buf_needed) < 0)
  546. return NULL;
  547. msg->hdr = wpabuf_mhead(msg->buf);
  548. }
  549. attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
  550. attr->type = type;
  551. attr->length = sizeof(*attr) + data_len;
  552. wpabuf_put_data(msg->buf, data, data_len);
  553. if (radius_msg_add_attr_to_array(msg, attr))
  554. return NULL;
  555. return attr;
  556. }
  557. /**
  558. * radius_msg_parse - Parse a RADIUS message
  559. * @data: RADIUS message to be parsed
  560. * @len: Length of data buffer in octets
  561. * Returns: Parsed RADIUS message or %NULL on failure
  562. *
  563. * This parses a RADIUS message and makes a copy of its data. The caller is
  564. * responsible for freeing the returned data with radius_msg_free().
  565. */
  566. struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
  567. {
  568. struct radius_msg *msg;
  569. struct radius_hdr *hdr;
  570. struct radius_attr_hdr *attr;
  571. size_t msg_len;
  572. unsigned char *pos, *end;
  573. if (data == NULL || len < sizeof(*hdr))
  574. return NULL;
  575. hdr = (struct radius_hdr *) data;
  576. msg_len = be_to_host16(hdr->length);
  577. if (msg_len < sizeof(*hdr) || msg_len > len) {
  578. wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
  579. return NULL;
  580. }
  581. if (msg_len < len) {
  582. wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
  583. "RADIUS message", (unsigned long) len - msg_len);
  584. }
  585. msg = os_zalloc(sizeof(*msg));
  586. if (msg == NULL)
  587. return NULL;
  588. msg->buf = wpabuf_alloc_copy(data, msg_len);
  589. if (msg->buf == NULL || radius_msg_initialize(msg)) {
  590. radius_msg_free(msg);
  591. return NULL;
  592. }
  593. msg->hdr = wpabuf_mhead(msg->buf);
  594. /* parse attributes */
  595. pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
  596. end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
  597. while (pos < end) {
  598. if ((size_t) (end - pos) < sizeof(*attr))
  599. goto fail;
  600. attr = (struct radius_attr_hdr *) pos;
  601. if (attr->length > end - pos || attr->length < sizeof(*attr))
  602. goto fail;
  603. /* TODO: check that attr->length is suitable for attr->type */
  604. if (radius_msg_add_attr_to_array(msg, attr))
  605. goto fail;
  606. pos += attr->length;
  607. }
  608. return msg;
  609. fail:
  610. radius_msg_free(msg);
  611. return NULL;
  612. }
  613. int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
  614. {
  615. const u8 *pos = data;
  616. size_t left = data_len;
  617. while (left > 0) {
  618. int len;
  619. if (left > RADIUS_MAX_ATTR_LEN)
  620. len = RADIUS_MAX_ATTR_LEN;
  621. else
  622. len = left;
  623. if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
  624. pos, len))
  625. return 0;
  626. pos += len;
  627. left -= len;
  628. }
  629. return 1;
  630. }
  631. struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
  632. {
  633. struct wpabuf *eap;
  634. size_t len, i;
  635. struct radius_attr_hdr *attr;
  636. if (msg == NULL)
  637. return NULL;
  638. len = 0;
  639. for (i = 0; i < msg->attr_used; i++) {
  640. attr = radius_get_attr_hdr(msg, i);
  641. if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
  642. attr->length > sizeof(struct radius_attr_hdr))
  643. len += attr->length - sizeof(struct radius_attr_hdr);
  644. }
  645. if (len == 0)
  646. return NULL;
  647. eap = wpabuf_alloc(len);
  648. if (eap == NULL)
  649. return NULL;
  650. for (i = 0; i < msg->attr_used; i++) {
  651. attr = radius_get_attr_hdr(msg, i);
  652. if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
  653. attr->length > sizeof(struct radius_attr_hdr)) {
  654. int flen = attr->length - sizeof(*attr);
  655. wpabuf_put_data(eap, attr + 1, flen);
  656. }
  657. }
  658. return eap;
  659. }
  660. int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
  661. size_t secret_len, const u8 *req_auth)
  662. {
  663. u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
  664. u8 orig_authenticator[16];
  665. struct radius_attr_hdr *attr = NULL, *tmp;
  666. size_t i;
  667. for (i = 0; i < msg->attr_used; i++) {
  668. tmp = radius_get_attr_hdr(msg, i);
  669. if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
  670. if (attr != NULL) {
  671. wpa_printf(MSG_INFO, "Multiple Message-Authenticator attributes in RADIUS message");
  672. return 1;
  673. }
  674. attr = tmp;
  675. }
  676. }
  677. if (attr == NULL) {
  678. wpa_printf(MSG_INFO, "No Message-Authenticator attribute found");
  679. return 1;
  680. }
  681. os_memcpy(orig, attr + 1, MD5_MAC_LEN);
  682. os_memset(attr + 1, 0, MD5_MAC_LEN);
  683. if (req_auth) {
  684. os_memcpy(orig_authenticator, msg->hdr->authenticator,
  685. sizeof(orig_authenticator));
  686. os_memcpy(msg->hdr->authenticator, req_auth,
  687. sizeof(msg->hdr->authenticator));
  688. }
  689. if (hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  690. wpabuf_len(msg->buf), auth) < 0)
  691. return 1;
  692. os_memcpy(attr + 1, orig, MD5_MAC_LEN);
  693. if (req_auth) {
  694. os_memcpy(msg->hdr->authenticator, orig_authenticator,
  695. sizeof(orig_authenticator));
  696. }
  697. if (os_memcmp_const(orig, auth, MD5_MAC_LEN) != 0) {
  698. wpa_printf(MSG_INFO, "Invalid Message-Authenticator!");
  699. return 1;
  700. }
  701. return 0;
  702. }
  703. int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
  704. size_t secret_len, struct radius_msg *sent_msg, int auth)
  705. {
  706. const u8 *addr[4];
  707. size_t len[4];
  708. u8 hash[MD5_MAC_LEN];
  709. if (sent_msg == NULL) {
  710. wpa_printf(MSG_INFO, "No matching Access-Request message found");
  711. return 1;
  712. }
  713. if (auth &&
  714. radius_msg_verify_msg_auth(msg, secret, secret_len,
  715. sent_msg->hdr->authenticator)) {
  716. return 1;
  717. }
  718. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  719. addr[0] = (u8 *) msg->hdr;
  720. len[0] = 1 + 1 + 2;
  721. addr[1] = sent_msg->hdr->authenticator;
  722. len[1] = MD5_MAC_LEN;
  723. addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
  724. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  725. addr[3] = secret;
  726. len[3] = secret_len;
  727. if (md5_vector(4, addr, len, hash) < 0 ||
  728. os_memcmp_const(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
  729. wpa_printf(MSG_INFO, "Response Authenticator invalid!");
  730. return 1;
  731. }
  732. return 0;
  733. }
  734. int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
  735. u8 type)
  736. {
  737. struct radius_attr_hdr *attr;
  738. size_t i;
  739. int count = 0;
  740. for (i = 0; i < src->attr_used; i++) {
  741. attr = radius_get_attr_hdr(src, i);
  742. if (attr->type == type && attr->length >= sizeof(*attr)) {
  743. if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
  744. attr->length - sizeof(*attr)))
  745. return -1;
  746. count++;
  747. }
  748. }
  749. return count;
  750. }
  751. /* Create Request Authenticator. The value should be unique over the lifetime
  752. * of the shared secret between authenticator and authentication server.
  753. */
  754. int radius_msg_make_authenticator(struct radius_msg *msg)
  755. {
  756. return os_get_random((u8 *) &msg->hdr->authenticator,
  757. sizeof(msg->hdr->authenticator));
  758. }
  759. /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
  760. * Returns the Attribute payload and sets alen to indicate the length of the
  761. * payload if a vendor attribute with subtype is found, otherwise returns NULL.
  762. * The returned payload is allocated with os_malloc() and caller must free it
  763. * by calling os_free().
  764. */
  765. static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
  766. u8 subtype, size_t *alen)
  767. {
  768. u8 *data, *pos;
  769. size_t i, len;
  770. if (msg == NULL)
  771. return NULL;
  772. for (i = 0; i < msg->attr_used; i++) {
  773. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  774. size_t left;
  775. u32 vendor_id;
  776. struct radius_attr_vendor *vhdr;
  777. if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
  778. attr->length < sizeof(*attr))
  779. continue;
  780. left = attr->length - sizeof(*attr);
  781. if (left < 4)
  782. continue;
  783. pos = (u8 *) (attr + 1);
  784. os_memcpy(&vendor_id, pos, 4);
  785. pos += 4;
  786. left -= 4;
  787. if (ntohl(vendor_id) != vendor)
  788. continue;
  789. while (left >= sizeof(*vhdr)) {
  790. vhdr = (struct radius_attr_vendor *) pos;
  791. if (vhdr->vendor_length > left ||
  792. vhdr->vendor_length < sizeof(*vhdr)) {
  793. break;
  794. }
  795. if (vhdr->vendor_type != subtype) {
  796. pos += vhdr->vendor_length;
  797. left -= vhdr->vendor_length;
  798. continue;
  799. }
  800. len = vhdr->vendor_length - sizeof(*vhdr);
  801. data = os_malloc(len);
  802. if (data == NULL)
  803. return NULL;
  804. os_memcpy(data, pos + sizeof(*vhdr), len);
  805. if (alen)
  806. *alen = len;
  807. return data;
  808. }
  809. }
  810. return NULL;
  811. }
  812. static u8 * decrypt_ms_key(const u8 *key, size_t len,
  813. const u8 *req_authenticator,
  814. const u8 *secret, size_t secret_len, size_t *reslen)
  815. {
  816. u8 *plain, *ppos, *res;
  817. const u8 *pos;
  818. size_t left, plen;
  819. u8 hash[MD5_MAC_LEN];
  820. int i, first = 1;
  821. const u8 *addr[3];
  822. size_t elen[3];
  823. /* key: 16-bit salt followed by encrypted key info */
  824. if (len < 2 + 16) {
  825. wpa_printf(MSG_DEBUG, "RADIUS: %s: Len is too small: %d",
  826. __func__, (int) len);
  827. return NULL;
  828. }
  829. pos = key + 2;
  830. left = len - 2;
  831. if (left % 16) {
  832. wpa_printf(MSG_INFO, "RADIUS: Invalid ms key len %lu",
  833. (unsigned long) left);
  834. return NULL;
  835. }
  836. plen = left;
  837. ppos = plain = os_malloc(plen);
  838. if (plain == NULL)
  839. return NULL;
  840. plain[0] = 0;
  841. while (left > 0) {
  842. /* b(1) = MD5(Secret + Request-Authenticator + Salt)
  843. * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
  844. addr[0] = secret;
  845. elen[0] = secret_len;
  846. if (first) {
  847. addr[1] = req_authenticator;
  848. elen[1] = MD5_MAC_LEN;
  849. addr[2] = key;
  850. elen[2] = 2; /* Salt */
  851. } else {
  852. addr[1] = pos - MD5_MAC_LEN;
  853. elen[1] = MD5_MAC_LEN;
  854. }
  855. if (md5_vector(first ? 3 : 2, addr, elen, hash) < 0) {
  856. os_free(plain);
  857. return NULL;
  858. }
  859. first = 0;
  860. for (i = 0; i < MD5_MAC_LEN; i++)
  861. *ppos++ = *pos++ ^ hash[i];
  862. left -= MD5_MAC_LEN;
  863. }
  864. if (plain[0] == 0 || plain[0] > plen - 1) {
  865. wpa_printf(MSG_INFO, "RADIUS: Failed to decrypt MPPE key");
  866. os_free(plain);
  867. return NULL;
  868. }
  869. res = os_malloc(plain[0]);
  870. if (res == NULL) {
  871. os_free(plain);
  872. return NULL;
  873. }
  874. os_memcpy(res, plain + 1, plain[0]);
  875. if (reslen)
  876. *reslen = plain[0];
  877. os_free(plain);
  878. return res;
  879. }
  880. static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
  881. const u8 *req_authenticator,
  882. const u8 *secret, size_t secret_len,
  883. u8 *ebuf, size_t *elen)
  884. {
  885. int i, len, first = 1;
  886. u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
  887. const u8 *addr[3];
  888. size_t _len[3];
  889. WPA_PUT_BE16(saltbuf, salt);
  890. len = 1 + key_len;
  891. if (len & 0x0f) {
  892. len = (len & 0xf0) + 16;
  893. }
  894. os_memset(ebuf, 0, len);
  895. ebuf[0] = key_len;
  896. os_memcpy(ebuf + 1, key, key_len);
  897. *elen = len;
  898. pos = ebuf;
  899. while (len > 0) {
  900. /* b(1) = MD5(Secret + Request-Authenticator + Salt)
  901. * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
  902. addr[0] = secret;
  903. _len[0] = secret_len;
  904. if (first) {
  905. addr[1] = req_authenticator;
  906. _len[1] = MD5_MAC_LEN;
  907. addr[2] = saltbuf;
  908. _len[2] = sizeof(saltbuf);
  909. } else {
  910. addr[1] = pos - MD5_MAC_LEN;
  911. _len[1] = MD5_MAC_LEN;
  912. }
  913. md5_vector(first ? 3 : 2, addr, _len, hash);
  914. first = 0;
  915. for (i = 0; i < MD5_MAC_LEN; i++)
  916. *pos++ ^= hash[i];
  917. len -= MD5_MAC_LEN;
  918. }
  919. }
  920. struct radius_ms_mppe_keys *
  921. radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
  922. const u8 *secret, size_t secret_len)
  923. {
  924. u8 *key;
  925. size_t keylen;
  926. struct radius_ms_mppe_keys *keys;
  927. if (msg == NULL || sent_msg == NULL)
  928. return NULL;
  929. keys = os_zalloc(sizeof(*keys));
  930. if (keys == NULL)
  931. return NULL;
  932. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
  933. RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
  934. &keylen);
  935. if (key) {
  936. keys->send = decrypt_ms_key(key, keylen,
  937. sent_msg->hdr->authenticator,
  938. secret, secret_len,
  939. &keys->send_len);
  940. if (!keys->send) {
  941. wpa_printf(MSG_DEBUG,
  942. "RADIUS: Failed to decrypt send key");
  943. }
  944. os_free(key);
  945. }
  946. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
  947. RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
  948. &keylen);
  949. if (key) {
  950. keys->recv = decrypt_ms_key(key, keylen,
  951. sent_msg->hdr->authenticator,
  952. secret, secret_len,
  953. &keys->recv_len);
  954. if (!keys->recv) {
  955. wpa_printf(MSG_DEBUG,
  956. "RADIUS: Failed to decrypt recv key");
  957. }
  958. os_free(key);
  959. }
  960. return keys;
  961. }
  962. struct radius_ms_mppe_keys *
  963. radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
  964. const u8 *secret, size_t secret_len)
  965. {
  966. u8 *key;
  967. size_t keylen;
  968. struct radius_ms_mppe_keys *keys;
  969. if (msg == NULL || sent_msg == NULL)
  970. return NULL;
  971. keys = os_zalloc(sizeof(*keys));
  972. if (keys == NULL)
  973. return NULL;
  974. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
  975. RADIUS_CISCO_AV_PAIR, &keylen);
  976. if (key && keylen == 51 &&
  977. os_memcmp(key, "leap:session-key=", 17) == 0) {
  978. keys->recv = decrypt_ms_key(key + 17, keylen - 17,
  979. sent_msg->hdr->authenticator,
  980. secret, secret_len,
  981. &keys->recv_len);
  982. }
  983. os_free(key);
  984. return keys;
  985. }
  986. int radius_msg_add_mppe_keys(struct radius_msg *msg,
  987. const u8 *req_authenticator,
  988. const u8 *secret, size_t secret_len,
  989. const u8 *send_key, size_t send_key_len,
  990. const u8 *recv_key, size_t recv_key_len)
  991. {
  992. struct radius_attr_hdr *attr;
  993. u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
  994. u8 *buf;
  995. struct radius_attr_vendor *vhdr;
  996. u8 *pos;
  997. size_t elen;
  998. int hlen;
  999. u16 salt;
  1000. hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
  1001. /* MS-MPPE-Send-Key */
  1002. buf = os_malloc(hlen + send_key_len + 16);
  1003. if (buf == NULL) {
  1004. return 0;
  1005. }
  1006. pos = buf;
  1007. os_memcpy(pos, &vendor_id, sizeof(vendor_id));
  1008. pos += sizeof(vendor_id);
  1009. vhdr = (struct radius_attr_vendor *) pos;
  1010. vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
  1011. pos = (u8 *) (vhdr + 1);
  1012. if (os_get_random((u8 *) &salt, sizeof(salt)) < 0) {
  1013. os_free(buf);
  1014. return 0;
  1015. }
  1016. salt |= 0x8000;
  1017. WPA_PUT_BE16(pos, salt);
  1018. pos += 2;
  1019. encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
  1020. secret_len, pos, &elen);
  1021. vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
  1022. attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
  1023. buf, hlen + elen);
  1024. os_free(buf);
  1025. if (attr == NULL) {
  1026. return 0;
  1027. }
  1028. /* MS-MPPE-Recv-Key */
  1029. buf = os_malloc(hlen + recv_key_len + 16);
  1030. if (buf == NULL) {
  1031. return 0;
  1032. }
  1033. pos = buf;
  1034. os_memcpy(pos, &vendor_id, sizeof(vendor_id));
  1035. pos += sizeof(vendor_id);
  1036. vhdr = (struct radius_attr_vendor *) pos;
  1037. vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
  1038. pos = (u8 *) (vhdr + 1);
  1039. salt ^= 1;
  1040. WPA_PUT_BE16(pos, salt);
  1041. pos += 2;
  1042. encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
  1043. secret_len, pos, &elen);
  1044. vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
  1045. attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
  1046. buf, hlen + elen);
  1047. os_free(buf);
  1048. if (attr == NULL) {
  1049. return 0;
  1050. }
  1051. return 1;
  1052. }
  1053. int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
  1054. size_t len)
  1055. {
  1056. struct radius_attr_hdr *attr;
  1057. u8 *buf, *pos;
  1058. size_t alen;
  1059. alen = 4 + 2 + len;
  1060. buf = os_malloc(alen);
  1061. if (buf == NULL)
  1062. return 0;
  1063. pos = buf;
  1064. WPA_PUT_BE32(pos, RADIUS_VENDOR_ID_WFA);
  1065. pos += 4;
  1066. *pos++ = subtype;
  1067. *pos++ = 2 + len;
  1068. os_memcpy(pos, data, len);
  1069. attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
  1070. buf, alen);
  1071. os_free(buf);
  1072. if (attr == NULL)
  1073. return 0;
  1074. return 1;
  1075. }
  1076. int radius_user_password_hide(struct radius_msg *msg,
  1077. const u8 *data, size_t data_len,
  1078. const u8 *secret, size_t secret_len,
  1079. u8 *buf, size_t buf_len)
  1080. {
  1081. size_t padlen, i, pos;
  1082. const u8 *addr[2];
  1083. size_t len[2];
  1084. u8 hash[16];
  1085. if (data_len + 16 > buf_len)
  1086. return -1;
  1087. os_memcpy(buf, data, data_len);
  1088. padlen = data_len % 16;
  1089. if (padlen && data_len < buf_len) {
  1090. padlen = 16 - padlen;
  1091. os_memset(buf + data_len, 0, padlen);
  1092. buf_len = data_len + padlen;
  1093. } else {
  1094. buf_len = data_len;
  1095. }
  1096. addr[0] = secret;
  1097. len[0] = secret_len;
  1098. addr[1] = msg->hdr->authenticator;
  1099. len[1] = 16;
  1100. md5_vector(2, addr, len, hash);
  1101. for (i = 0; i < 16; i++)
  1102. buf[i] ^= hash[i];
  1103. pos = 16;
  1104. while (pos < buf_len) {
  1105. addr[0] = secret;
  1106. len[0] = secret_len;
  1107. addr[1] = &buf[pos - 16];
  1108. len[1] = 16;
  1109. md5_vector(2, addr, len, hash);
  1110. for (i = 0; i < 16; i++)
  1111. buf[pos + i] ^= hash[i];
  1112. pos += 16;
  1113. }
  1114. return buf_len;
  1115. }
  1116. /* Add User-Password attribute to a RADIUS message and encrypt it as specified
  1117. * in RFC 2865, Chap. 5.2 */
  1118. struct radius_attr_hdr *
  1119. radius_msg_add_attr_user_password(struct radius_msg *msg,
  1120. const u8 *data, size_t data_len,
  1121. const u8 *secret, size_t secret_len)
  1122. {
  1123. u8 buf[128];
  1124. int res;
  1125. res = radius_user_password_hide(msg, data, data_len,
  1126. secret, secret_len, buf, sizeof(buf));
  1127. if (res < 0)
  1128. return NULL;
  1129. return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
  1130. buf, res);
  1131. }
  1132. int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
  1133. {
  1134. struct radius_attr_hdr *attr = NULL, *tmp;
  1135. size_t i, dlen;
  1136. for (i = 0; i < msg->attr_used; i++) {
  1137. tmp = radius_get_attr_hdr(msg, i);
  1138. if (tmp->type == type) {
  1139. attr = tmp;
  1140. break;
  1141. }
  1142. }
  1143. if (!attr || attr->length < sizeof(*attr))
  1144. return -1;
  1145. dlen = attr->length - sizeof(*attr);
  1146. if (buf)
  1147. os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
  1148. return dlen;
  1149. }
  1150. int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
  1151. size_t *len, const u8 *start)
  1152. {
  1153. size_t i;
  1154. struct radius_attr_hdr *attr = NULL, *tmp;
  1155. for (i = 0; i < msg->attr_used; i++) {
  1156. tmp = radius_get_attr_hdr(msg, i);
  1157. if (tmp->type == type &&
  1158. (start == NULL || (u8 *) tmp > start)) {
  1159. attr = tmp;
  1160. break;
  1161. }
  1162. }
  1163. if (!attr || attr->length < sizeof(*attr))
  1164. return -1;
  1165. *buf = (u8 *) (attr + 1);
  1166. *len = attr->length - sizeof(*attr);
  1167. return 0;
  1168. }
  1169. int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
  1170. {
  1171. size_t i;
  1172. int count;
  1173. for (count = 0, i = 0; i < msg->attr_used; i++) {
  1174. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  1175. if (attr->type == type &&
  1176. attr->length >= sizeof(struct radius_attr_hdr) + min_len)
  1177. count++;
  1178. }
  1179. return count;
  1180. }
  1181. struct radius_tunnel_attrs {
  1182. int tag_used;
  1183. int type; /* Tunnel-Type */
  1184. int medium_type; /* Tunnel-Medium-Type */
  1185. int vlanid;
  1186. };
  1187. static int cmp_int(const void *a, const void *b)
  1188. {
  1189. int x, y;
  1190. x = *((int *) a);
  1191. y = *((int *) b);
  1192. return (x - y);
  1193. }
  1194. /**
  1195. * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
  1196. * The k tagged vlans found are sorted by vlan_id and stored in the first k
  1197. * items of tagged.
  1198. *
  1199. * @msg: RADIUS message
  1200. * @untagged: Pointer to store untagged vid
  1201. * @numtagged: Size of tagged
  1202. * @tagged: Pointer to store tagged list
  1203. *
  1204. * Returns: 0 if neither tagged nor untagged configuration is found, 1 otherwise
  1205. */
  1206. int radius_msg_get_vlanid(struct radius_msg *msg, int *untagged, int numtagged,
  1207. int *tagged)
  1208. {
  1209. struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
  1210. size_t i;
  1211. struct radius_attr_hdr *attr = NULL;
  1212. const u8 *data;
  1213. char buf[10];
  1214. size_t dlen;
  1215. int j, taggedidx = 0, vlan_id;
  1216. os_memset(&tunnel, 0, sizeof(tunnel));
  1217. for (j = 0; j < numtagged; j++)
  1218. tagged[j] = 0;
  1219. *untagged = 0;
  1220. for (i = 0; i < msg->attr_used; i++) {
  1221. attr = radius_get_attr_hdr(msg, i);
  1222. if (attr->length < sizeof(*attr))
  1223. return -1;
  1224. data = (const u8 *) (attr + 1);
  1225. dlen = attr->length - sizeof(*attr);
  1226. if (attr->length < 3)
  1227. continue;
  1228. if (data[0] >= RADIUS_TUNNEL_TAGS)
  1229. tun = &tunnel[0];
  1230. else
  1231. tun = &tunnel[data[0]];
  1232. switch (attr->type) {
  1233. case RADIUS_ATTR_TUNNEL_TYPE:
  1234. if (attr->length != 6)
  1235. break;
  1236. tun->tag_used++;
  1237. tun->type = WPA_GET_BE24(data + 1);
  1238. break;
  1239. case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
  1240. if (attr->length != 6)
  1241. break;
  1242. tun->tag_used++;
  1243. tun->medium_type = WPA_GET_BE24(data + 1);
  1244. break;
  1245. case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
  1246. if (data[0] < RADIUS_TUNNEL_TAGS) {
  1247. data++;
  1248. dlen--;
  1249. }
  1250. if (dlen >= sizeof(buf))
  1251. break;
  1252. os_memcpy(buf, data, dlen);
  1253. buf[dlen] = '\0';
  1254. vlan_id = atoi(buf);
  1255. if (vlan_id <= 0)
  1256. break;
  1257. tun->tag_used++;
  1258. tun->vlanid = vlan_id;
  1259. break;
  1260. case RADIUS_ATTR_EGRESS_VLANID: /* RFC 4675 */
  1261. if (attr->length != 6)
  1262. break;
  1263. vlan_id = WPA_GET_BE24(data + 1);
  1264. if (vlan_id <= 0)
  1265. break;
  1266. if (data[0] == 0x32)
  1267. *untagged = vlan_id;
  1268. else if (data[0] == 0x31 && tagged &&
  1269. taggedidx < numtagged)
  1270. tagged[taggedidx++] = vlan_id;
  1271. break;
  1272. }
  1273. }
  1274. /* Use tunnel with the lowest tag for untagged VLAN id */
  1275. for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
  1276. tun = &tunnel[i];
  1277. if (tun->tag_used &&
  1278. tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
  1279. tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
  1280. tun->vlanid > 0) {
  1281. *untagged = tun->vlanid;
  1282. break;
  1283. }
  1284. }
  1285. if (taggedidx)
  1286. qsort(tagged, taggedidx, sizeof(int), cmp_int);
  1287. if (*untagged > 0 || taggedidx)
  1288. return 1;
  1289. return 0;
  1290. }
  1291. /**
  1292. * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
  1293. * @msg: Received RADIUS message
  1294. * @keylen: Length of returned password
  1295. * @secret: RADIUS shared secret
  1296. * @secret_len: Length of secret
  1297. * @sent_msg: Sent RADIUS message
  1298. * @n: Number of password attribute to return (starting with 0)
  1299. * Returns: Pointer to n-th password (free with os_free) or %NULL
  1300. */
  1301. char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
  1302. const u8 *secret, size_t secret_len,
  1303. struct radius_msg *sent_msg, size_t n)
  1304. {
  1305. u8 *buf = NULL;
  1306. size_t buflen;
  1307. const u8 *salt;
  1308. u8 *str;
  1309. const u8 *addr[3];
  1310. size_t len[3];
  1311. u8 hash[16];
  1312. u8 *pos;
  1313. size_t i, j = 0;
  1314. struct radius_attr_hdr *attr;
  1315. const u8 *data;
  1316. size_t dlen;
  1317. const u8 *fdata = NULL; /* points to found item */
  1318. size_t fdlen = -1;
  1319. char *ret = NULL;
  1320. /* find n-th valid Tunnel-Password attribute */
  1321. for (i = 0; i < msg->attr_used; i++) {
  1322. attr = radius_get_attr_hdr(msg, i);
  1323. if (attr == NULL ||
  1324. attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
  1325. continue;
  1326. }
  1327. if (attr->length <= 5)
  1328. continue;
  1329. data = (const u8 *) (attr + 1);
  1330. dlen = attr->length - sizeof(*attr);
  1331. if (dlen <= 3 || dlen % 16 != 3)
  1332. continue;
  1333. j++;
  1334. if (j <= n)
  1335. continue;
  1336. fdata = data;
  1337. fdlen = dlen;
  1338. break;
  1339. }
  1340. if (fdata == NULL)
  1341. goto out;
  1342. /* alloc writable memory for decryption */
  1343. buf = os_malloc(fdlen);
  1344. if (buf == NULL)
  1345. goto out;
  1346. os_memcpy(buf, fdata, fdlen);
  1347. buflen = fdlen;
  1348. /* init pointers */
  1349. salt = buf + 1;
  1350. str = buf + 3;
  1351. /* decrypt blocks */
  1352. pos = buf + buflen - 16; /* last block */
  1353. while (pos >= str + 16) { /* all but the first block */
  1354. addr[0] = secret;
  1355. len[0] = secret_len;
  1356. addr[1] = pos - 16;
  1357. len[1] = 16;
  1358. md5_vector(2, addr, len, hash);
  1359. for (i = 0; i < 16; i++)
  1360. pos[i] ^= hash[i];
  1361. pos -= 16;
  1362. }
  1363. /* decrypt first block */
  1364. if (str != pos)
  1365. goto out;
  1366. addr[0] = secret;
  1367. len[0] = secret_len;
  1368. addr[1] = sent_msg->hdr->authenticator;
  1369. len[1] = 16;
  1370. addr[2] = salt;
  1371. len[2] = 2;
  1372. md5_vector(3, addr, len, hash);
  1373. for (i = 0; i < 16; i++)
  1374. pos[i] ^= hash[i];
  1375. /* derive plaintext length from first subfield */
  1376. *keylen = (unsigned char) str[0];
  1377. if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
  1378. /* decryption error - invalid key length */
  1379. goto out;
  1380. }
  1381. if (*keylen == 0) {
  1382. /* empty password */
  1383. goto out;
  1384. }
  1385. /* copy passphrase into new buffer */
  1386. ret = os_malloc(*keylen);
  1387. if (ret)
  1388. os_memcpy(ret, str + 1, *keylen);
  1389. out:
  1390. /* return new buffer */
  1391. os_free(buf);
  1392. return ret;
  1393. }
  1394. void radius_free_class(struct radius_class_data *c)
  1395. {
  1396. size_t i;
  1397. if (c == NULL)
  1398. return;
  1399. for (i = 0; i < c->count; i++)
  1400. os_free(c->attr[i].data);
  1401. os_free(c->attr);
  1402. c->attr = NULL;
  1403. c->count = 0;
  1404. }
  1405. int radius_copy_class(struct radius_class_data *dst,
  1406. const struct radius_class_data *src)
  1407. {
  1408. size_t i;
  1409. if (src->attr == NULL)
  1410. return 0;
  1411. dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
  1412. if (dst->attr == NULL)
  1413. return -1;
  1414. dst->count = 0;
  1415. for (i = 0; i < src->count; i++) {
  1416. dst->attr[i].data = os_malloc(src->attr[i].len);
  1417. if (dst->attr[i].data == NULL)
  1418. break;
  1419. dst->count++;
  1420. os_memcpy(dst->attr[i].data, src->attr[i].data,
  1421. src->attr[i].len);
  1422. dst->attr[i].len = src->attr[i].len;
  1423. }
  1424. return 0;
  1425. }
  1426. u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
  1427. {
  1428. size_t i, j;
  1429. struct radius_attr_hdr *attr;
  1430. for (i = 0; i < msg->attr_used; i++) {
  1431. attr = radius_get_attr_hdr(msg, i);
  1432. for (j = 0; attrs[j]; j++) {
  1433. if (attr->type == attrs[j])
  1434. break;
  1435. }
  1436. if (attrs[j] == 0)
  1437. return attr->type; /* unlisted attr */
  1438. }
  1439. return 0;
  1440. }
  1441. int radius_gen_session_id(u8 *id, size_t len)
  1442. {
  1443. /*
  1444. * Acct-Session-Id and Acct-Multi-Session-Id should be globally and
  1445. * temporarily unique. A high quality random number is required
  1446. * therefore. This could be be improved by switching to a GUID.
  1447. */
  1448. return os_get_random(id, len);
  1449. }