radius.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /*
  2. * RADIUS message processing
  3. * Copyright (c) 2002-2009, 2011-2012, 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 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_FRAMED_MTU, "Framed-MTU", RADIUS_ATTR_INT32 },
  143. { RADIUS_ATTR_REPLY_MESSAGE, "Reply-Message", RADIUS_ATTR_TEXT },
  144. { RADIUS_ATTR_STATE, "State", RADIUS_ATTR_UNDIST },
  145. { RADIUS_ATTR_CLASS, "Class", RADIUS_ATTR_UNDIST },
  146. { RADIUS_ATTR_VENDOR_SPECIFIC, "Vendor-Specific", RADIUS_ATTR_UNDIST },
  147. { RADIUS_ATTR_SESSION_TIMEOUT, "Session-Timeout", RADIUS_ATTR_INT32 },
  148. { RADIUS_ATTR_IDLE_TIMEOUT, "Idle-Timeout", RADIUS_ATTR_INT32 },
  149. { RADIUS_ATTR_TERMINATION_ACTION, "Termination-Action",
  150. RADIUS_ATTR_INT32 },
  151. { RADIUS_ATTR_CALLED_STATION_ID, "Called-Station-Id",
  152. RADIUS_ATTR_TEXT },
  153. { RADIUS_ATTR_CALLING_STATION_ID, "Calling-Station-Id",
  154. RADIUS_ATTR_TEXT },
  155. { RADIUS_ATTR_NAS_IDENTIFIER, "NAS-Identifier", RADIUS_ATTR_TEXT },
  156. { RADIUS_ATTR_PROXY_STATE, "Proxy-State", RADIUS_ATTR_UNDIST },
  157. { RADIUS_ATTR_ACCT_STATUS_TYPE, "Acct-Status-Type",
  158. RADIUS_ATTR_INT32 },
  159. { RADIUS_ATTR_ACCT_DELAY_TIME, "Acct-Delay-Time", RADIUS_ATTR_INT32 },
  160. { RADIUS_ATTR_ACCT_INPUT_OCTETS, "Acct-Input-Octets",
  161. RADIUS_ATTR_INT32 },
  162. { RADIUS_ATTR_ACCT_OUTPUT_OCTETS, "Acct-Output-Octets",
  163. RADIUS_ATTR_INT32 },
  164. { RADIUS_ATTR_ACCT_SESSION_ID, "Acct-Session-Id", RADIUS_ATTR_TEXT },
  165. { RADIUS_ATTR_ACCT_AUTHENTIC, "Acct-Authentic", RADIUS_ATTR_INT32 },
  166. { RADIUS_ATTR_ACCT_SESSION_TIME, "Acct-Session-Time",
  167. RADIUS_ATTR_INT32 },
  168. { RADIUS_ATTR_ACCT_INPUT_PACKETS, "Acct-Input-Packets",
  169. RADIUS_ATTR_INT32 },
  170. { RADIUS_ATTR_ACCT_OUTPUT_PACKETS, "Acct-Output-Packets",
  171. RADIUS_ATTR_INT32 },
  172. { RADIUS_ATTR_ACCT_TERMINATE_CAUSE, "Acct-Terminate-Cause",
  173. RADIUS_ATTR_INT32 },
  174. { RADIUS_ATTR_ACCT_MULTI_SESSION_ID, "Acct-Multi-Session-Id",
  175. RADIUS_ATTR_TEXT },
  176. { RADIUS_ATTR_ACCT_LINK_COUNT, "Acct-Link-Count", RADIUS_ATTR_INT32 },
  177. { RADIUS_ATTR_ACCT_INPUT_GIGAWORDS, "Acct-Input-Gigawords",
  178. RADIUS_ATTR_INT32 },
  179. { RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS, "Acct-Output-Gigawords",
  180. RADIUS_ATTR_INT32 },
  181. { RADIUS_ATTR_EVENT_TIMESTAMP, "Event-Timestamp",
  182. RADIUS_ATTR_INT32 },
  183. { RADIUS_ATTR_NAS_PORT_TYPE, "NAS-Port-Type", RADIUS_ATTR_INT32 },
  184. { RADIUS_ATTR_TUNNEL_TYPE, "Tunnel-Type", RADIUS_ATTR_HEXDUMP },
  185. { RADIUS_ATTR_TUNNEL_MEDIUM_TYPE, "Tunnel-Medium-Type",
  186. RADIUS_ATTR_HEXDUMP },
  187. { RADIUS_ATTR_TUNNEL_PASSWORD, "Tunnel-Password",
  188. RADIUS_ATTR_UNDIST },
  189. { RADIUS_ATTR_CONNECT_INFO, "Connect-Info", RADIUS_ATTR_TEXT },
  190. { RADIUS_ATTR_EAP_MESSAGE, "EAP-Message", RADIUS_ATTR_UNDIST },
  191. { RADIUS_ATTR_MESSAGE_AUTHENTICATOR, "Message-Authenticator",
  192. RADIUS_ATTR_UNDIST },
  193. { RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID, "Tunnel-Private-Group-Id",
  194. RADIUS_ATTR_HEXDUMP },
  195. { RADIUS_ATTR_ACCT_INTERIM_INTERVAL, "Acct-Interim-Interval",
  196. RADIUS_ATTR_INT32 },
  197. { RADIUS_ATTR_CHARGEABLE_USER_IDENTITY, "Chargeable-User-Identity",
  198. RADIUS_ATTR_TEXT },
  199. { RADIUS_ATTR_NAS_IPV6_ADDRESS, "NAS-IPv6-Address", RADIUS_ATTR_IPV6 },
  200. { RADIUS_ATTR_ERROR_CAUSE, "Error-Cause", RADIUS_ATTR_INT32 }
  201. };
  202. #define RADIUS_ATTRS (sizeof(radius_attrs) / sizeof(radius_attrs[0]))
  203. static struct radius_attr_type *radius_get_attr_type(u8 type)
  204. {
  205. size_t i;
  206. for (i = 0; i < RADIUS_ATTRS; i++) {
  207. if (type == radius_attrs[i].type)
  208. return &radius_attrs[i];
  209. }
  210. return NULL;
  211. }
  212. static void print_char(char c)
  213. {
  214. if (c >= 32 && c < 127)
  215. printf("%c", c);
  216. else
  217. printf("<%02x>", c);
  218. }
  219. static void radius_msg_dump_attr(struct radius_attr_hdr *hdr)
  220. {
  221. struct radius_attr_type *attr;
  222. int i, len;
  223. unsigned char *pos;
  224. attr = radius_get_attr_type(hdr->type);
  225. printf(" Attribute %d (%s) length=%d\n",
  226. hdr->type, attr ? attr->name : "?Unknown?", hdr->length);
  227. if (attr == NULL || hdr->length < sizeof(struct radius_attr_hdr))
  228. return;
  229. len = hdr->length - sizeof(struct radius_attr_hdr);
  230. pos = (unsigned char *) (hdr + 1);
  231. switch (attr->data_type) {
  232. case RADIUS_ATTR_TEXT:
  233. printf(" Value: '");
  234. for (i = 0; i < len; i++)
  235. print_char(pos[i]);
  236. printf("'\n");
  237. break;
  238. case RADIUS_ATTR_IP:
  239. if (len == 4) {
  240. struct in_addr addr;
  241. os_memcpy(&addr, pos, 4);
  242. printf(" Value: %s\n", inet_ntoa(addr));
  243. } else
  244. printf(" Invalid IP address length %d\n", len);
  245. break;
  246. #ifdef CONFIG_IPV6
  247. case RADIUS_ATTR_IPV6:
  248. if (len == 16) {
  249. char buf[128];
  250. const char *atxt;
  251. struct in6_addr *addr = (struct in6_addr *) pos;
  252. atxt = inet_ntop(AF_INET6, addr, buf, sizeof(buf));
  253. printf(" Value: %s\n", atxt ? atxt : "?");
  254. } else
  255. printf(" Invalid IPv6 address length %d\n", len);
  256. break;
  257. #endif /* CONFIG_IPV6 */
  258. case RADIUS_ATTR_HEXDUMP:
  259. case RADIUS_ATTR_UNDIST:
  260. printf(" Value:");
  261. for (i = 0; i < len; i++)
  262. printf(" %02x", pos[i]);
  263. printf("\n");
  264. break;
  265. case RADIUS_ATTR_INT32:
  266. if (len == 4)
  267. printf(" Value: %u\n", WPA_GET_BE32(pos));
  268. else
  269. printf(" Invalid INT32 length %d\n", len);
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. void radius_msg_dump(struct radius_msg *msg)
  276. {
  277. size_t i;
  278. printf("RADIUS message: code=%d (%s) identifier=%d length=%d\n",
  279. msg->hdr->code, radius_code_string(msg->hdr->code),
  280. msg->hdr->identifier, be_to_host16(msg->hdr->length));
  281. for (i = 0; i < msg->attr_used; i++) {
  282. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  283. radius_msg_dump_attr(attr);
  284. }
  285. }
  286. int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
  287. size_t secret_len)
  288. {
  289. if (secret) {
  290. u8 auth[MD5_MAC_LEN];
  291. struct radius_attr_hdr *attr;
  292. os_memset(auth, 0, MD5_MAC_LEN);
  293. attr = radius_msg_add_attr(msg,
  294. RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  295. auth, MD5_MAC_LEN);
  296. if (attr == NULL) {
  297. wpa_printf(MSG_WARNING, "RADIUS: Could not add "
  298. "Message-Authenticator");
  299. return -1;
  300. }
  301. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  302. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  303. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  304. } else
  305. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  306. if (wpabuf_len(msg->buf) > 0xffff) {
  307. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  308. (unsigned long) wpabuf_len(msg->buf));
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
  314. size_t secret_len, const u8 *req_authenticator)
  315. {
  316. u8 auth[MD5_MAC_LEN];
  317. struct radius_attr_hdr *attr;
  318. const u8 *addr[4];
  319. size_t len[4];
  320. os_memset(auth, 0, MD5_MAC_LEN);
  321. attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  322. auth, MD5_MAC_LEN);
  323. if (attr == NULL) {
  324. printf("WARNING: Could not add Message-Authenticator\n");
  325. return -1;
  326. }
  327. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  328. os_memcpy(msg->hdr->authenticator, req_authenticator,
  329. sizeof(msg->hdr->authenticator));
  330. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  331. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  332. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  333. addr[0] = (u8 *) msg->hdr;
  334. len[0] = 1 + 1 + 2;
  335. addr[1] = req_authenticator;
  336. len[1] = MD5_MAC_LEN;
  337. addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
  338. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  339. addr[3] = secret;
  340. len[3] = secret_len;
  341. md5_vector(4, addr, len, msg->hdr->authenticator);
  342. if (wpabuf_len(msg->buf) > 0xffff) {
  343. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  344. (unsigned long) wpabuf_len(msg->buf));
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
  350. size_t secret_len,
  351. const struct radius_hdr *req_hdr)
  352. {
  353. const u8 *addr[2];
  354. size_t len[2];
  355. u8 auth[MD5_MAC_LEN];
  356. struct radius_attr_hdr *attr;
  357. os_memset(auth, 0, MD5_MAC_LEN);
  358. attr = radius_msg_add_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR,
  359. auth, MD5_MAC_LEN);
  360. if (attr == NULL) {
  361. wpa_printf(MSG_WARNING, "Could not add Message-Authenticator");
  362. return -1;
  363. }
  364. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  365. os_memcpy(msg->hdr->authenticator, req_hdr->authenticator, 16);
  366. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  367. wpabuf_len(msg->buf), (u8 *) (attr + 1));
  368. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  369. addr[0] = wpabuf_head_u8(msg->buf);
  370. len[0] = wpabuf_len(msg->buf);
  371. addr[1] = secret;
  372. len[1] = secret_len;
  373. if (md5_vector(2, addr, len, msg->hdr->authenticator) < 0)
  374. return -1;
  375. if (wpabuf_len(msg->buf) > 0xffff) {
  376. wpa_printf(MSG_WARNING, "RADIUS: Too long message (%lu)",
  377. (unsigned long) wpabuf_len(msg->buf));
  378. return -1;
  379. }
  380. return 0;
  381. }
  382. void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
  383. size_t secret_len)
  384. {
  385. const u8 *addr[2];
  386. size_t len[2];
  387. msg->hdr->length = host_to_be16(wpabuf_len(msg->buf));
  388. os_memset(msg->hdr->authenticator, 0, MD5_MAC_LEN);
  389. addr[0] = wpabuf_head(msg->buf);
  390. len[0] = wpabuf_len(msg->buf);
  391. addr[1] = secret;
  392. len[1] = secret_len;
  393. md5_vector(2, addr, len, msg->hdr->authenticator);
  394. if (wpabuf_len(msg->buf) > 0xffff) {
  395. wpa_printf(MSG_WARNING, "RADIUS: Too long messages (%lu)",
  396. (unsigned long) wpabuf_len(msg->buf));
  397. }
  398. }
  399. int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
  400. size_t secret_len)
  401. {
  402. const u8 *addr[4];
  403. size_t len[4];
  404. u8 zero[MD5_MAC_LEN];
  405. u8 hash[MD5_MAC_LEN];
  406. os_memset(zero, 0, sizeof(zero));
  407. addr[0] = (u8 *) msg->hdr;
  408. len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
  409. addr[1] = zero;
  410. len[1] = MD5_MAC_LEN;
  411. addr[2] = (u8 *) (msg->hdr + 1);
  412. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  413. addr[3] = secret;
  414. len[3] = secret_len;
  415. md5_vector(4, addr, len, hash);
  416. return os_memcmp(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0;
  417. }
  418. int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
  419. size_t secret_len)
  420. {
  421. const u8 *addr[4];
  422. size_t len[4];
  423. u8 zero[MD5_MAC_LEN];
  424. u8 hash[MD5_MAC_LEN];
  425. u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
  426. u8 orig_authenticator[16];
  427. struct radius_attr_hdr *attr = NULL, *tmp;
  428. size_t i;
  429. os_memset(zero, 0, sizeof(zero));
  430. addr[0] = (u8 *) msg->hdr;
  431. len[0] = sizeof(struct radius_hdr) - MD5_MAC_LEN;
  432. addr[1] = zero;
  433. len[1] = MD5_MAC_LEN;
  434. addr[2] = (u8 *) (msg->hdr + 1);
  435. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  436. addr[3] = secret;
  437. len[3] = secret_len;
  438. md5_vector(4, addr, len, hash);
  439. if (os_memcmp(msg->hdr->authenticator, hash, MD5_MAC_LEN) != 0)
  440. return 1;
  441. for (i = 0; i < msg->attr_used; i++) {
  442. tmp = radius_get_attr_hdr(msg, i);
  443. if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
  444. if (attr != NULL) {
  445. wpa_printf(MSG_WARNING, "Multiple "
  446. "Message-Authenticator attributes "
  447. "in RADIUS message");
  448. return 1;
  449. }
  450. attr = tmp;
  451. }
  452. }
  453. if (attr == NULL) {
  454. /* Message-Authenticator is MAY; not required */
  455. return 0;
  456. }
  457. os_memcpy(orig, attr + 1, MD5_MAC_LEN);
  458. os_memset(attr + 1, 0, MD5_MAC_LEN);
  459. os_memcpy(orig_authenticator, msg->hdr->authenticator,
  460. sizeof(orig_authenticator));
  461. os_memset(msg->hdr->authenticator, 0,
  462. sizeof(msg->hdr->authenticator));
  463. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  464. wpabuf_len(msg->buf), auth);
  465. os_memcpy(attr + 1, orig, MD5_MAC_LEN);
  466. os_memcpy(msg->hdr->authenticator, orig_authenticator,
  467. sizeof(orig_authenticator));
  468. return os_memcmp(orig, auth, MD5_MAC_LEN) != 0;
  469. }
  470. static int radius_msg_add_attr_to_array(struct radius_msg *msg,
  471. struct radius_attr_hdr *attr)
  472. {
  473. if (msg->attr_used >= msg->attr_size) {
  474. size_t *nattr_pos;
  475. int nlen = msg->attr_size * 2;
  476. nattr_pos = os_realloc_array(msg->attr_pos, nlen,
  477. sizeof(*msg->attr_pos));
  478. if (nattr_pos == NULL)
  479. return -1;
  480. msg->attr_pos = nattr_pos;
  481. msg->attr_size = nlen;
  482. }
  483. msg->attr_pos[msg->attr_used++] =
  484. (unsigned char *) attr - wpabuf_head_u8(msg->buf);
  485. return 0;
  486. }
  487. struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type,
  488. const u8 *data, size_t data_len)
  489. {
  490. size_t buf_needed;
  491. struct radius_attr_hdr *attr;
  492. if (data_len > RADIUS_MAX_ATTR_LEN) {
  493. printf("radius_msg_add_attr: too long attribute (%lu bytes)\n",
  494. (unsigned long) data_len);
  495. return NULL;
  496. }
  497. buf_needed = sizeof(*attr) + data_len;
  498. if (wpabuf_tailroom(msg->buf) < buf_needed) {
  499. /* allocate more space for message buffer */
  500. if (wpabuf_resize(&msg->buf, buf_needed) < 0)
  501. return NULL;
  502. msg->hdr = wpabuf_mhead(msg->buf);
  503. }
  504. attr = wpabuf_put(msg->buf, sizeof(struct radius_attr_hdr));
  505. attr->type = type;
  506. attr->length = sizeof(*attr) + data_len;
  507. wpabuf_put_data(msg->buf, data, data_len);
  508. if (radius_msg_add_attr_to_array(msg, attr))
  509. return NULL;
  510. return attr;
  511. }
  512. /**
  513. * radius_msg_parse - Parse a RADIUS message
  514. * @data: RADIUS message to be parsed
  515. * @len: Length of data buffer in octets
  516. * Returns: Parsed RADIUS message or %NULL on failure
  517. *
  518. * This parses a RADIUS message and makes a copy of its data. The caller is
  519. * responsible for freeing the returned data with radius_msg_free().
  520. */
  521. struct radius_msg * radius_msg_parse(const u8 *data, size_t len)
  522. {
  523. struct radius_msg *msg;
  524. struct radius_hdr *hdr;
  525. struct radius_attr_hdr *attr;
  526. size_t msg_len;
  527. unsigned char *pos, *end;
  528. if (data == NULL || len < sizeof(*hdr))
  529. return NULL;
  530. hdr = (struct radius_hdr *) data;
  531. msg_len = be_to_host16(hdr->length);
  532. if (msg_len < sizeof(*hdr) || msg_len > len) {
  533. wpa_printf(MSG_INFO, "RADIUS: Invalid message length");
  534. return NULL;
  535. }
  536. if (msg_len < len) {
  537. wpa_printf(MSG_DEBUG, "RADIUS: Ignored %lu extra bytes after "
  538. "RADIUS message", (unsigned long) len - msg_len);
  539. }
  540. msg = os_zalloc(sizeof(*msg));
  541. if (msg == NULL)
  542. return NULL;
  543. msg->buf = wpabuf_alloc_copy(data, msg_len);
  544. if (msg->buf == NULL || radius_msg_initialize(msg)) {
  545. radius_msg_free(msg);
  546. return NULL;
  547. }
  548. msg->hdr = wpabuf_mhead(msg->buf);
  549. /* parse attributes */
  550. pos = wpabuf_mhead_u8(msg->buf) + sizeof(struct radius_hdr);
  551. end = wpabuf_mhead_u8(msg->buf) + wpabuf_len(msg->buf);
  552. while (pos < end) {
  553. if ((size_t) (end - pos) < sizeof(*attr))
  554. goto fail;
  555. attr = (struct radius_attr_hdr *) pos;
  556. if (pos + attr->length > end || attr->length < sizeof(*attr))
  557. goto fail;
  558. /* TODO: check that attr->length is suitable for attr->type */
  559. if (radius_msg_add_attr_to_array(msg, attr))
  560. goto fail;
  561. pos += attr->length;
  562. }
  563. return msg;
  564. fail:
  565. radius_msg_free(msg);
  566. return NULL;
  567. }
  568. int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, size_t data_len)
  569. {
  570. const u8 *pos = data;
  571. size_t left = data_len;
  572. while (left > 0) {
  573. int len;
  574. if (left > RADIUS_MAX_ATTR_LEN)
  575. len = RADIUS_MAX_ATTR_LEN;
  576. else
  577. len = left;
  578. if (!radius_msg_add_attr(msg, RADIUS_ATTR_EAP_MESSAGE,
  579. pos, len))
  580. return 0;
  581. pos += len;
  582. left -= len;
  583. }
  584. return 1;
  585. }
  586. struct wpabuf * radius_msg_get_eap(struct radius_msg *msg)
  587. {
  588. struct wpabuf *eap;
  589. size_t len, i;
  590. struct radius_attr_hdr *attr;
  591. if (msg == NULL)
  592. return NULL;
  593. len = 0;
  594. for (i = 0; i < msg->attr_used; i++) {
  595. attr = radius_get_attr_hdr(msg, i);
  596. if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
  597. attr->length > sizeof(struct radius_attr_hdr))
  598. len += attr->length - sizeof(struct radius_attr_hdr);
  599. }
  600. if (len == 0)
  601. return NULL;
  602. eap = wpabuf_alloc(len);
  603. if (eap == NULL)
  604. return NULL;
  605. for (i = 0; i < msg->attr_used; i++) {
  606. attr = radius_get_attr_hdr(msg, i);
  607. if (attr->type == RADIUS_ATTR_EAP_MESSAGE &&
  608. attr->length > sizeof(struct radius_attr_hdr)) {
  609. int flen = attr->length - sizeof(*attr);
  610. wpabuf_put_data(eap, attr + 1, flen);
  611. }
  612. }
  613. return eap;
  614. }
  615. int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
  616. size_t secret_len, const u8 *req_auth)
  617. {
  618. u8 auth[MD5_MAC_LEN], orig[MD5_MAC_LEN];
  619. u8 orig_authenticator[16];
  620. struct radius_attr_hdr *attr = NULL, *tmp;
  621. size_t i;
  622. for (i = 0; i < msg->attr_used; i++) {
  623. tmp = radius_get_attr_hdr(msg, i);
  624. if (tmp->type == RADIUS_ATTR_MESSAGE_AUTHENTICATOR) {
  625. if (attr != NULL) {
  626. printf("Multiple Message-Authenticator "
  627. "attributes in RADIUS message\n");
  628. return 1;
  629. }
  630. attr = tmp;
  631. }
  632. }
  633. if (attr == NULL) {
  634. printf("No Message-Authenticator attribute found\n");
  635. return 1;
  636. }
  637. os_memcpy(orig, attr + 1, MD5_MAC_LEN);
  638. os_memset(attr + 1, 0, MD5_MAC_LEN);
  639. if (req_auth) {
  640. os_memcpy(orig_authenticator, msg->hdr->authenticator,
  641. sizeof(orig_authenticator));
  642. os_memcpy(msg->hdr->authenticator, req_auth,
  643. sizeof(msg->hdr->authenticator));
  644. }
  645. hmac_md5(secret, secret_len, wpabuf_head(msg->buf),
  646. wpabuf_len(msg->buf), auth);
  647. os_memcpy(attr + 1, orig, MD5_MAC_LEN);
  648. if (req_auth) {
  649. os_memcpy(msg->hdr->authenticator, orig_authenticator,
  650. sizeof(orig_authenticator));
  651. }
  652. if (os_memcmp(orig, auth, MD5_MAC_LEN) != 0) {
  653. printf("Invalid Message-Authenticator!\n");
  654. return 1;
  655. }
  656. return 0;
  657. }
  658. int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
  659. size_t secret_len, struct radius_msg *sent_msg, int auth)
  660. {
  661. const u8 *addr[4];
  662. size_t len[4];
  663. u8 hash[MD5_MAC_LEN];
  664. if (sent_msg == NULL) {
  665. printf("No matching Access-Request message found\n");
  666. return 1;
  667. }
  668. if (auth &&
  669. radius_msg_verify_msg_auth(msg, secret, secret_len,
  670. sent_msg->hdr->authenticator)) {
  671. return 1;
  672. }
  673. /* ResponseAuth = MD5(Code+ID+Length+RequestAuth+Attributes+Secret) */
  674. addr[0] = (u8 *) msg->hdr;
  675. len[0] = 1 + 1 + 2;
  676. addr[1] = sent_msg->hdr->authenticator;
  677. len[1] = MD5_MAC_LEN;
  678. addr[2] = wpabuf_head_u8(msg->buf) + sizeof(struct radius_hdr);
  679. len[2] = wpabuf_len(msg->buf) - sizeof(struct radius_hdr);
  680. addr[3] = secret;
  681. len[3] = secret_len;
  682. md5_vector(4, addr, len, hash);
  683. if (os_memcmp(hash, msg->hdr->authenticator, MD5_MAC_LEN) != 0) {
  684. printf("Response Authenticator invalid!\n");
  685. return 1;
  686. }
  687. return 0;
  688. }
  689. int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
  690. u8 type)
  691. {
  692. struct radius_attr_hdr *attr;
  693. size_t i;
  694. int count = 0;
  695. for (i = 0; i < src->attr_used; i++) {
  696. attr = radius_get_attr_hdr(src, i);
  697. if (attr->type == type && attr->length >= sizeof(*attr)) {
  698. if (!radius_msg_add_attr(dst, type, (u8 *) (attr + 1),
  699. attr->length - sizeof(*attr)))
  700. return -1;
  701. count++;
  702. }
  703. }
  704. return count;
  705. }
  706. /* Create Request Authenticator. The value should be unique over the lifetime
  707. * of the shared secret between authenticator and authentication server.
  708. * Use one-way MD5 hash calculated from current timestamp and some data given
  709. * by the caller. */
  710. void radius_msg_make_authenticator(struct radius_msg *msg,
  711. const u8 *data, size_t len)
  712. {
  713. struct os_time tv;
  714. long int l;
  715. const u8 *addr[3];
  716. size_t elen[3];
  717. os_get_time(&tv);
  718. l = os_random();
  719. addr[0] = (u8 *) &tv;
  720. elen[0] = sizeof(tv);
  721. addr[1] = data;
  722. elen[1] = len;
  723. addr[2] = (u8 *) &l;
  724. elen[2] = sizeof(l);
  725. md5_vector(3, addr, elen, msg->hdr->authenticator);
  726. }
  727. /* Get Vendor-specific RADIUS Attribute from a parsed RADIUS message.
  728. * Returns the Attribute payload and sets alen to indicate the length of the
  729. * payload if a vendor attribute with subtype is found, otherwise returns NULL.
  730. * The returned payload is allocated with os_malloc() and caller must free it
  731. * by calling os_free().
  732. */
  733. static u8 *radius_msg_get_vendor_attr(struct radius_msg *msg, u32 vendor,
  734. u8 subtype, size_t *alen)
  735. {
  736. u8 *data, *pos;
  737. size_t i, len;
  738. if (msg == NULL)
  739. return NULL;
  740. for (i = 0; i < msg->attr_used; i++) {
  741. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  742. size_t left;
  743. u32 vendor_id;
  744. struct radius_attr_vendor *vhdr;
  745. if (attr->type != RADIUS_ATTR_VENDOR_SPECIFIC ||
  746. attr->length < sizeof(*attr))
  747. continue;
  748. left = attr->length - sizeof(*attr);
  749. if (left < 4)
  750. continue;
  751. pos = (u8 *) (attr + 1);
  752. os_memcpy(&vendor_id, pos, 4);
  753. pos += 4;
  754. left -= 4;
  755. if (ntohl(vendor_id) != vendor)
  756. continue;
  757. while (left >= sizeof(*vhdr)) {
  758. vhdr = (struct radius_attr_vendor *) pos;
  759. if (vhdr->vendor_length > left ||
  760. vhdr->vendor_length < sizeof(*vhdr)) {
  761. left = 0;
  762. break;
  763. }
  764. if (vhdr->vendor_type != subtype) {
  765. pos += vhdr->vendor_length;
  766. left -= vhdr->vendor_length;
  767. continue;
  768. }
  769. len = vhdr->vendor_length - sizeof(*vhdr);
  770. data = os_malloc(len);
  771. if (data == NULL)
  772. return NULL;
  773. os_memcpy(data, pos + sizeof(*vhdr), len);
  774. if (alen)
  775. *alen = len;
  776. return data;
  777. }
  778. }
  779. return NULL;
  780. }
  781. static u8 * decrypt_ms_key(const u8 *key, size_t len,
  782. const u8 *req_authenticator,
  783. const u8 *secret, size_t secret_len, size_t *reslen)
  784. {
  785. u8 *plain, *ppos, *res;
  786. const u8 *pos;
  787. size_t left, plen;
  788. u8 hash[MD5_MAC_LEN];
  789. int i, first = 1;
  790. const u8 *addr[3];
  791. size_t elen[3];
  792. /* key: 16-bit salt followed by encrypted key info */
  793. if (len < 2 + 16)
  794. return NULL;
  795. pos = key + 2;
  796. left = len - 2;
  797. if (left % 16) {
  798. printf("Invalid ms key len %lu\n", (unsigned long) left);
  799. return NULL;
  800. }
  801. plen = left;
  802. ppos = plain = os_malloc(plen);
  803. if (plain == NULL)
  804. return NULL;
  805. plain[0] = 0;
  806. while (left > 0) {
  807. /* b(1) = MD5(Secret + Request-Authenticator + Salt)
  808. * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
  809. addr[0] = secret;
  810. elen[0] = secret_len;
  811. if (first) {
  812. addr[1] = req_authenticator;
  813. elen[1] = MD5_MAC_LEN;
  814. addr[2] = key;
  815. elen[2] = 2; /* Salt */
  816. } else {
  817. addr[1] = pos - MD5_MAC_LEN;
  818. elen[1] = MD5_MAC_LEN;
  819. }
  820. md5_vector(first ? 3 : 2, addr, elen, hash);
  821. first = 0;
  822. for (i = 0; i < MD5_MAC_LEN; i++)
  823. *ppos++ = *pos++ ^ hash[i];
  824. left -= MD5_MAC_LEN;
  825. }
  826. if (plain[0] == 0 || plain[0] > plen - 1) {
  827. printf("Failed to decrypt MPPE key\n");
  828. os_free(plain);
  829. return NULL;
  830. }
  831. res = os_malloc(plain[0]);
  832. if (res == NULL) {
  833. os_free(plain);
  834. return NULL;
  835. }
  836. os_memcpy(res, plain + 1, plain[0]);
  837. if (reslen)
  838. *reslen = plain[0];
  839. os_free(plain);
  840. return res;
  841. }
  842. static void encrypt_ms_key(const u8 *key, size_t key_len, u16 salt,
  843. const u8 *req_authenticator,
  844. const u8 *secret, size_t secret_len,
  845. u8 *ebuf, size_t *elen)
  846. {
  847. int i, len, first = 1;
  848. u8 hash[MD5_MAC_LEN], saltbuf[2], *pos;
  849. const u8 *addr[3];
  850. size_t _len[3];
  851. WPA_PUT_BE16(saltbuf, salt);
  852. len = 1 + key_len;
  853. if (len & 0x0f) {
  854. len = (len & 0xf0) + 16;
  855. }
  856. os_memset(ebuf, 0, len);
  857. ebuf[0] = key_len;
  858. os_memcpy(ebuf + 1, key, key_len);
  859. *elen = len;
  860. pos = ebuf;
  861. while (len > 0) {
  862. /* b(1) = MD5(Secret + Request-Authenticator + Salt)
  863. * b(i) = MD5(Secret + c(i - 1)) for i > 1 */
  864. addr[0] = secret;
  865. _len[0] = secret_len;
  866. if (first) {
  867. addr[1] = req_authenticator;
  868. _len[1] = MD5_MAC_LEN;
  869. addr[2] = saltbuf;
  870. _len[2] = sizeof(saltbuf);
  871. } else {
  872. addr[1] = pos - MD5_MAC_LEN;
  873. _len[1] = MD5_MAC_LEN;
  874. }
  875. md5_vector(first ? 3 : 2, addr, _len, hash);
  876. first = 0;
  877. for (i = 0; i < MD5_MAC_LEN; i++)
  878. *pos++ ^= hash[i];
  879. len -= MD5_MAC_LEN;
  880. }
  881. }
  882. struct radius_ms_mppe_keys *
  883. radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
  884. const u8 *secret, size_t secret_len)
  885. {
  886. u8 *key;
  887. size_t keylen;
  888. struct radius_ms_mppe_keys *keys;
  889. if (msg == NULL || sent_msg == NULL)
  890. return NULL;
  891. keys = os_zalloc(sizeof(*keys));
  892. if (keys == NULL)
  893. return NULL;
  894. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
  895. RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY,
  896. &keylen);
  897. if (key) {
  898. keys->send = decrypt_ms_key(key, keylen,
  899. sent_msg->hdr->authenticator,
  900. secret, secret_len,
  901. &keys->send_len);
  902. os_free(key);
  903. }
  904. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_MICROSOFT,
  905. RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY,
  906. &keylen);
  907. if (key) {
  908. keys->recv = decrypt_ms_key(key, keylen,
  909. sent_msg->hdr->authenticator,
  910. secret, secret_len,
  911. &keys->recv_len);
  912. os_free(key);
  913. }
  914. return keys;
  915. }
  916. struct radius_ms_mppe_keys *
  917. radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
  918. const u8 *secret, size_t secret_len)
  919. {
  920. u8 *key;
  921. size_t keylen;
  922. struct radius_ms_mppe_keys *keys;
  923. if (msg == NULL || sent_msg == NULL)
  924. return NULL;
  925. keys = os_zalloc(sizeof(*keys));
  926. if (keys == NULL)
  927. return NULL;
  928. key = radius_msg_get_vendor_attr(msg, RADIUS_VENDOR_ID_CISCO,
  929. RADIUS_CISCO_AV_PAIR, &keylen);
  930. if (key && keylen == 51 &&
  931. os_memcmp(key, "leap:session-key=", 17) == 0) {
  932. keys->recv = decrypt_ms_key(key + 17, keylen - 17,
  933. sent_msg->hdr->authenticator,
  934. secret, secret_len,
  935. &keys->recv_len);
  936. }
  937. os_free(key);
  938. return keys;
  939. }
  940. int radius_msg_add_mppe_keys(struct radius_msg *msg,
  941. const u8 *req_authenticator,
  942. const u8 *secret, size_t secret_len,
  943. const u8 *send_key, size_t send_key_len,
  944. const u8 *recv_key, size_t recv_key_len)
  945. {
  946. struct radius_attr_hdr *attr;
  947. u32 vendor_id = htonl(RADIUS_VENDOR_ID_MICROSOFT);
  948. u8 *buf;
  949. struct radius_attr_vendor *vhdr;
  950. u8 *pos;
  951. size_t elen;
  952. int hlen;
  953. u16 salt;
  954. hlen = sizeof(vendor_id) + sizeof(*vhdr) + 2;
  955. /* MS-MPPE-Send-Key */
  956. buf = os_malloc(hlen + send_key_len + 16);
  957. if (buf == NULL) {
  958. return 0;
  959. }
  960. pos = buf;
  961. os_memcpy(pos, &vendor_id, sizeof(vendor_id));
  962. pos += sizeof(vendor_id);
  963. vhdr = (struct radius_attr_vendor *) pos;
  964. vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY;
  965. pos = (u8 *) (vhdr + 1);
  966. salt = os_random() | 0x8000;
  967. WPA_PUT_BE16(pos, salt);
  968. pos += 2;
  969. encrypt_ms_key(send_key, send_key_len, salt, req_authenticator, secret,
  970. secret_len, pos, &elen);
  971. vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
  972. attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
  973. buf, hlen + elen);
  974. os_free(buf);
  975. if (attr == NULL) {
  976. return 0;
  977. }
  978. /* MS-MPPE-Recv-Key */
  979. buf = os_malloc(hlen + send_key_len + 16);
  980. if (buf == NULL) {
  981. return 0;
  982. }
  983. pos = buf;
  984. os_memcpy(pos, &vendor_id, sizeof(vendor_id));
  985. pos += sizeof(vendor_id);
  986. vhdr = (struct radius_attr_vendor *) pos;
  987. vhdr->vendor_type = RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY;
  988. pos = (u8 *) (vhdr + 1);
  989. salt ^= 1;
  990. WPA_PUT_BE16(pos, salt);
  991. pos += 2;
  992. encrypt_ms_key(recv_key, recv_key_len, salt, req_authenticator, secret,
  993. secret_len, pos, &elen);
  994. vhdr->vendor_length = hlen + elen - sizeof(vendor_id);
  995. attr = radius_msg_add_attr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
  996. buf, hlen + elen);
  997. os_free(buf);
  998. if (attr == NULL) {
  999. return 0;
  1000. }
  1001. return 1;
  1002. }
  1003. /* Add User-Password attribute to a RADIUS message and encrypt it as specified
  1004. * in RFC 2865, Chap. 5.2 */
  1005. struct radius_attr_hdr *
  1006. radius_msg_add_attr_user_password(struct radius_msg *msg,
  1007. const u8 *data, size_t data_len,
  1008. const u8 *secret, size_t secret_len)
  1009. {
  1010. u8 buf[128];
  1011. size_t padlen, i, buf_len, pos;
  1012. const u8 *addr[2];
  1013. size_t len[2];
  1014. u8 hash[16];
  1015. if (data_len > 128)
  1016. return NULL;
  1017. os_memcpy(buf, data, data_len);
  1018. buf_len = data_len;
  1019. padlen = data_len % 16;
  1020. if (padlen && data_len < sizeof(buf)) {
  1021. padlen = 16 - padlen;
  1022. os_memset(buf + data_len, 0, padlen);
  1023. buf_len += padlen;
  1024. }
  1025. addr[0] = secret;
  1026. len[0] = secret_len;
  1027. addr[1] = msg->hdr->authenticator;
  1028. len[1] = 16;
  1029. md5_vector(2, addr, len, hash);
  1030. for (i = 0; i < 16; i++)
  1031. buf[i] ^= hash[i];
  1032. pos = 16;
  1033. while (pos < buf_len) {
  1034. addr[0] = secret;
  1035. len[0] = secret_len;
  1036. addr[1] = &buf[pos - 16];
  1037. len[1] = 16;
  1038. md5_vector(2, addr, len, hash);
  1039. for (i = 0; i < 16; i++)
  1040. buf[pos + i] ^= hash[i];
  1041. pos += 16;
  1042. }
  1043. return radius_msg_add_attr(msg, RADIUS_ATTR_USER_PASSWORD,
  1044. buf, buf_len);
  1045. }
  1046. int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len)
  1047. {
  1048. struct radius_attr_hdr *attr = NULL, *tmp;
  1049. size_t i, dlen;
  1050. for (i = 0; i < msg->attr_used; i++) {
  1051. tmp = radius_get_attr_hdr(msg, i);
  1052. if (tmp->type == type) {
  1053. attr = tmp;
  1054. break;
  1055. }
  1056. }
  1057. if (!attr || attr->length < sizeof(*attr))
  1058. return -1;
  1059. dlen = attr->length - sizeof(*attr);
  1060. if (buf)
  1061. os_memcpy(buf, (attr + 1), dlen > len ? len : dlen);
  1062. return dlen;
  1063. }
  1064. int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
  1065. size_t *len, const u8 *start)
  1066. {
  1067. size_t i;
  1068. struct radius_attr_hdr *attr = NULL, *tmp;
  1069. for (i = 0; i < msg->attr_used; i++) {
  1070. tmp = radius_get_attr_hdr(msg, i);
  1071. if (tmp->type == type &&
  1072. (start == NULL || (u8 *) tmp > start)) {
  1073. attr = tmp;
  1074. break;
  1075. }
  1076. }
  1077. if (!attr || attr->length < sizeof(*attr))
  1078. return -1;
  1079. *buf = (u8 *) (attr + 1);
  1080. *len = attr->length - sizeof(*attr);
  1081. return 0;
  1082. }
  1083. int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len)
  1084. {
  1085. size_t i;
  1086. int count;
  1087. for (count = 0, i = 0; i < msg->attr_used; i++) {
  1088. struct radius_attr_hdr *attr = radius_get_attr_hdr(msg, i);
  1089. if (attr->type == type &&
  1090. attr->length >= sizeof(struct radius_attr_hdr) + min_len)
  1091. count++;
  1092. }
  1093. return count;
  1094. }
  1095. struct radius_tunnel_attrs {
  1096. int tag_used;
  1097. int type; /* Tunnel-Type */
  1098. int medium_type; /* Tunnel-Medium-Type */
  1099. int vlanid;
  1100. };
  1101. /**
  1102. * radius_msg_get_vlanid - Parse RADIUS attributes for VLAN tunnel information
  1103. * @msg: RADIUS message
  1104. * Returns: VLAN ID for the first tunnel configuration of -1 if none is found
  1105. */
  1106. int radius_msg_get_vlanid(struct radius_msg *msg)
  1107. {
  1108. struct radius_tunnel_attrs tunnel[RADIUS_TUNNEL_TAGS], *tun;
  1109. size_t i;
  1110. struct radius_attr_hdr *attr = NULL;
  1111. const u8 *data;
  1112. char buf[10];
  1113. size_t dlen;
  1114. os_memset(&tunnel, 0, sizeof(tunnel));
  1115. for (i = 0; i < msg->attr_used; i++) {
  1116. attr = radius_get_attr_hdr(msg, i);
  1117. if (attr->length < sizeof(*attr))
  1118. return -1;
  1119. data = (const u8 *) (attr + 1);
  1120. dlen = attr->length - sizeof(*attr);
  1121. if (attr->length < 3)
  1122. continue;
  1123. if (data[0] >= RADIUS_TUNNEL_TAGS)
  1124. tun = &tunnel[0];
  1125. else
  1126. tun = &tunnel[data[0]];
  1127. switch (attr->type) {
  1128. case RADIUS_ATTR_TUNNEL_TYPE:
  1129. if (attr->length != 6)
  1130. break;
  1131. tun->tag_used++;
  1132. tun->type = WPA_GET_BE24(data + 1);
  1133. break;
  1134. case RADIUS_ATTR_TUNNEL_MEDIUM_TYPE:
  1135. if (attr->length != 6)
  1136. break;
  1137. tun->tag_used++;
  1138. tun->medium_type = WPA_GET_BE24(data + 1);
  1139. break;
  1140. case RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID:
  1141. if (data[0] < RADIUS_TUNNEL_TAGS) {
  1142. data++;
  1143. dlen--;
  1144. }
  1145. if (dlen >= sizeof(buf))
  1146. break;
  1147. os_memcpy(buf, data, dlen);
  1148. buf[dlen] = '\0';
  1149. tun->tag_used++;
  1150. tun->vlanid = atoi(buf);
  1151. break;
  1152. }
  1153. }
  1154. for (i = 0; i < RADIUS_TUNNEL_TAGS; i++) {
  1155. tun = &tunnel[i];
  1156. if (tun->tag_used &&
  1157. tun->type == RADIUS_TUNNEL_TYPE_VLAN &&
  1158. tun->medium_type == RADIUS_TUNNEL_MEDIUM_TYPE_802 &&
  1159. tun->vlanid > 0)
  1160. return tun->vlanid;
  1161. }
  1162. return -1;
  1163. }
  1164. /**
  1165. * radius_msg_get_tunnel_password - Parse RADIUS attribute Tunnel-Password
  1166. * @msg: Received RADIUS message
  1167. * @keylen: Length of returned password
  1168. * @secret: RADIUS shared secret
  1169. * @secret_len: Length of secret
  1170. * @sent_msg: Sent RADIUS message
  1171. * Returns: pointer to password (free with os_free) or %NULL
  1172. */
  1173. char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
  1174. const u8 *secret, size_t secret_len,
  1175. struct radius_msg *sent_msg)
  1176. {
  1177. u8 *buf = NULL;
  1178. size_t buflen;
  1179. const u8 *salt;
  1180. u8 *str;
  1181. const u8 *addr[3];
  1182. size_t len[3];
  1183. u8 hash[16];
  1184. u8 *pos;
  1185. size_t i;
  1186. struct radius_attr_hdr *attr;
  1187. const u8 *data;
  1188. size_t dlen;
  1189. const u8 *fdata = NULL; /* points to found item */
  1190. size_t fdlen = -1;
  1191. char *ret = NULL;
  1192. /* find attribute with lowest tag and check it */
  1193. for (i = 0; i < msg->attr_used; i++) {
  1194. attr = radius_get_attr_hdr(msg, i);
  1195. if (attr == NULL ||
  1196. attr->type != RADIUS_ATTR_TUNNEL_PASSWORD) {
  1197. continue;
  1198. }
  1199. if (attr->length <= 5)
  1200. continue;
  1201. data = (const u8 *) (attr + 1);
  1202. dlen = attr->length - sizeof(*attr);
  1203. if (dlen <= 3 || dlen % 16 != 3)
  1204. continue;
  1205. if (fdata != NULL && fdata[0] <= data[0])
  1206. continue;
  1207. fdata = data;
  1208. fdlen = dlen;
  1209. }
  1210. if (fdata == NULL)
  1211. goto out;
  1212. /* alloc writable memory for decryption */
  1213. buf = os_malloc(fdlen);
  1214. if (buf == NULL)
  1215. goto out;
  1216. os_memcpy(buf, fdata, fdlen);
  1217. buflen = fdlen;
  1218. /* init pointers */
  1219. salt = buf + 1;
  1220. str = buf + 3;
  1221. /* decrypt blocks */
  1222. pos = buf + buflen - 16; /* last block */
  1223. while (pos >= str + 16) { /* all but the first block */
  1224. addr[0] = secret;
  1225. len[0] = secret_len;
  1226. addr[1] = pos - 16;
  1227. len[1] = 16;
  1228. md5_vector(2, addr, len, hash);
  1229. for (i = 0; i < 16; i++)
  1230. pos[i] ^= hash[i];
  1231. pos -= 16;
  1232. }
  1233. /* decrypt first block */
  1234. if (str != pos)
  1235. goto out;
  1236. addr[0] = secret;
  1237. len[0] = secret_len;
  1238. addr[1] = sent_msg->hdr->authenticator;
  1239. len[1] = 16;
  1240. addr[2] = salt;
  1241. len[2] = 2;
  1242. md5_vector(3, addr, len, hash);
  1243. for (i = 0; i < 16; i++)
  1244. pos[i] ^= hash[i];
  1245. /* derive plaintext length from first subfield */
  1246. *keylen = (unsigned char) str[0];
  1247. if ((u8 *) (str + *keylen) >= (u8 *) (buf + buflen)) {
  1248. /* decryption error - invalid key length */
  1249. goto out;
  1250. }
  1251. if (*keylen == 0) {
  1252. /* empty password */
  1253. goto out;
  1254. }
  1255. /* copy passphrase into new buffer */
  1256. ret = os_malloc(*keylen);
  1257. if (ret)
  1258. os_memcpy(ret, str + 1, *keylen);
  1259. out:
  1260. /* return new buffer */
  1261. os_free(buf);
  1262. return ret;
  1263. }
  1264. void radius_free_class(struct radius_class_data *c)
  1265. {
  1266. size_t i;
  1267. if (c == NULL)
  1268. return;
  1269. for (i = 0; i < c->count; i++)
  1270. os_free(c->attr[i].data);
  1271. os_free(c->attr);
  1272. c->attr = NULL;
  1273. c->count = 0;
  1274. }
  1275. int radius_copy_class(struct radius_class_data *dst,
  1276. const struct radius_class_data *src)
  1277. {
  1278. size_t i;
  1279. if (src->attr == NULL)
  1280. return 0;
  1281. dst->attr = os_calloc(src->count, sizeof(struct radius_attr_data));
  1282. if (dst->attr == NULL)
  1283. return -1;
  1284. dst->count = 0;
  1285. for (i = 0; i < src->count; i++) {
  1286. dst->attr[i].data = os_malloc(src->attr[i].len);
  1287. if (dst->attr[i].data == NULL)
  1288. break;
  1289. dst->count++;
  1290. os_memcpy(dst->attr[i].data, src->attr[i].data,
  1291. src->attr[i].len);
  1292. dst->attr[i].len = src->attr[i].len;
  1293. }
  1294. return 0;
  1295. }
  1296. u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs)
  1297. {
  1298. size_t i, j;
  1299. struct radius_attr_hdr *attr;
  1300. for (i = 0; i < msg->attr_used; i++) {
  1301. attr = radius_get_attr_hdr(msg, i);
  1302. for (j = 0; attrs[j]; j++) {
  1303. if (attr->type == attrs[j])
  1304. break;
  1305. }
  1306. if (attrs[j] == 0)
  1307. return attr->type; /* unlisted attr */
  1308. }
  1309. return 0;
  1310. }