Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | /*********************************************************************************/ |
| 37 | /* freeDiameter author note: |
| 38 | * The content from this file comes mostly from the hostap project. |
| 39 | * It is redistributed under the terms of the BSD license, as allowed |
| 40 | * by the original copyright reproduced below. |
| 41 | * The changes to this file are placed under the copyright of the freeDiameter project. |
| 42 | */ |
| 43 | |
| 44 | /* |
| 45 | * hostapd / RADIUS message processing |
| 46 | * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi> |
| 47 | * |
| 48 | * This program is free software; you can redistribute it and/or modify |
| 49 | * it under the terms of the GNU General Public License version 2 as |
| 50 | * published by the Free Software Foundation. |
| 51 | * |
| 52 | * Alternatively, this software may be distributed under the terms of BSD |
| 53 | * license. |
| 54 | * |
| 55 | * See README and COPYING for more details. |
| 56 | */ |
| 57 | /*********************************************************************************/ |
| 58 | |
| 59 | |
| 60 | #ifndef RADIUS_H |
| 61 | #define RADIUS_H |
| 62 | |
| 63 | /* RFC 2865 - RADIUS */ |
| 64 | |
| 65 | #ifdef _MSC_VER |
| 66 | #pragma pack(push, 1) |
| 67 | #endif /* _MSC_VER */ |
| 68 | |
| 69 | struct radius_hdr { |
| 70 | u8 code; |
| 71 | u8 identifier; |
| 72 | u16 length; /* including this header */ |
| 73 | u8 authenticator[16]; |
| 74 | /* followed by length-20 octets of attributes */ |
| 75 | } STRUCT_PACKED; |
| 76 | |
| 77 | enum { RADIUS_CODE_ACCESS_REQUEST = 1, |
| 78 | RADIUS_CODE_ACCESS_ACCEPT = 2, |
| 79 | RADIUS_CODE_ACCESS_REJECT = 3, |
| 80 | RADIUS_CODE_ACCOUNTING_REQUEST = 4, |
| 81 | RADIUS_CODE_ACCOUNTING_RESPONSE = 5, |
| 82 | RADIUS_CODE_ACCESS_CHALLENGE = 11, |
| 83 | RADIUS_CODE_STATUS_SERVER = 12, |
| 84 | RADIUS_CODE_STATUS_CLIENT = 13, |
| 85 | RADIUS_CODE_RESERVED = 255 |
| 86 | }; |
| 87 | |
| 88 | struct radius_attr_hdr { |
| 89 | u8 type; |
| 90 | u8 length; /* including this header */ |
| 91 | /* followed by length-2 octets of attribute value */ |
| 92 | } STRUCT_PACKED; |
| 93 | |
| 94 | #define RADIUS_MAX_ATTR_LEN (255 - sizeof(struct radius_attr_hdr)) |
| 95 | |
| 96 | enum { RADIUS_ATTR_USER_NAME = 1, |
| 97 | RADIUS_ATTR_USER_PASSWORD = 2, |
| 98 | RADIUS_ATTR_NAS_IP_ADDRESS = 4, |
| 99 | RADIUS_ATTR_NAS_PORT = 5, |
| 100 | RADIUS_ATTR_FRAMED_MTU = 12, |
| 101 | RADIUS_ATTR_REPLY_MESSAGE = 18, |
| 102 | RADIUS_ATTR_STATE = 24, |
| 103 | RADIUS_ATTR_CLASS = 25, |
| 104 | RADIUS_ATTR_VENDOR_SPECIFIC = 26, |
| 105 | RADIUS_ATTR_SESSION_TIMEOUT = 27, |
| 106 | RADIUS_ATTR_IDLE_TIMEOUT = 28, |
| 107 | RADIUS_ATTR_TERMINATION_ACTION = 29, |
| 108 | RADIUS_ATTR_CALLED_STATION_ID = 30, |
| 109 | RADIUS_ATTR_CALLING_STATION_ID = 31, |
| 110 | RADIUS_ATTR_NAS_IDENTIFIER = 32, |
| 111 | RADIUS_ATTR_PROXY_STATE = 33, |
| 112 | RADIUS_ATTR_ACCT_STATUS_TYPE = 40, |
| 113 | RADIUS_ATTR_ACCT_DELAY_TIME = 41, |
| 114 | RADIUS_ATTR_ACCT_INPUT_OCTETS = 42, |
| 115 | RADIUS_ATTR_ACCT_OUTPUT_OCTETS = 43, |
| 116 | RADIUS_ATTR_ACCT_SESSION_ID = 44, |
| 117 | RADIUS_ATTR_ACCT_AUTHENTIC = 45, |
| 118 | RADIUS_ATTR_ACCT_SESSION_TIME = 46, |
| 119 | RADIUS_ATTR_ACCT_INPUT_PACKETS = 47, |
| 120 | RADIUS_ATTR_ACCT_OUTPUT_PACKETS = 48, |
| 121 | RADIUS_ATTR_ACCT_TERMINATE_CAUSE = 49, |
| 122 | RADIUS_ATTR_ACCT_MULTI_SESSION_ID = 50, |
| 123 | RADIUS_ATTR_ACCT_LINK_COUNT = 51, |
| 124 | RADIUS_ATTR_ACCT_INPUT_GIGAWORDS = 52, |
| 125 | RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS = 53, |
| 126 | RADIUS_ATTR_EVENT_TIMESTAMP = 55, |
| 127 | RADIUS_ATTR_NAS_PORT_TYPE = 61, |
| 128 | RADIUS_ATTR_TUNNEL_TYPE = 64, |
| 129 | RADIUS_ATTR_TUNNEL_MEDIUM_TYPE = 65, |
| 130 | RADIUS_ATTR_CONNECT_INFO = 77, |
| 131 | RADIUS_ATTR_EAP_MESSAGE = 79, |
| 132 | RADIUS_ATTR_MESSAGE_AUTHENTICATOR = 80, |
| 133 | RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID = 81, |
| 134 | RADIUS_ATTR_ACCT_INTERIM_INTERVAL = 85, |
| 135 | RADIUS_ATTR_CHARGEABLE_USER_IDENTITY = 89, |
| 136 | RADIUS_ATTR_NAS_IPV6_ADDRESS = 95 |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | /* Termination-Action */ |
| 141 | #define RADIUS_TERMINATION_ACTION_DEFAULT 0 |
| 142 | #define RADIUS_TERMINATION_ACTION_RADIUS_REQUEST 1 |
| 143 | |
| 144 | /* NAS-Port-Type */ |
| 145 | #define RADIUS_NAS_PORT_TYPE_IEEE_802_11 19 |
| 146 | |
| 147 | /* Acct-Status-Type */ |
| 148 | #define RADIUS_ACCT_STATUS_TYPE_START 1 |
| 149 | #define RADIUS_ACCT_STATUS_TYPE_STOP 2 |
| 150 | #define RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE 3 |
| 151 | #define RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON 7 |
| 152 | #define RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF 8 |
| 153 | |
| 154 | /* Acct-Authentic */ |
| 155 | #define RADIUS_ACCT_AUTHENTIC_RADIUS 1 |
| 156 | #define RADIUS_ACCT_AUTHENTIC_LOCAL 2 |
| 157 | #define RADIUS_ACCT_AUTHENTIC_REMOTE 3 |
| 158 | |
| 159 | /* Acct-Terminate-Cause */ |
| 160 | #define RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST 1 |
| 161 | #define RADIUS_ACCT_TERMINATE_CAUSE_LOST_CARRIER 2 |
| 162 | #define RADIUS_ACCT_TERMINATE_CAUSE_LOST_SERVICE 3 |
| 163 | #define RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT 4 |
| 164 | #define RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT 5 |
| 165 | #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_RESET 6 |
| 166 | #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT 7 |
| 167 | #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_ERROR 8 |
| 168 | #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_ERROR 9 |
| 169 | #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_REQUEST 10 |
| 170 | #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT 11 |
| 171 | #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_UNNEEDED 12 |
| 172 | #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_PREEMPTED 13 |
| 173 | #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_SUSPENDED 14 |
| 174 | #define RADIUS_ACCT_TERMINATE_CAUSE_SERVICE_UNAVAILABLE 15 |
| 175 | #define RADIUS_ACCT_TERMINATE_CAUSE_CALLBACK 16 |
| 176 | #define RADIUS_ACCT_TERMINATE_CAUSE_USER_ERROR 17 |
| 177 | #define RADIUS_ACCT_TERMINATE_CAUSE_HOST_REQUEST 18 |
| 178 | |
| 179 | #define RADIUS_TUNNEL_TAGS 32 |
| 180 | |
| 181 | /* Tunnel-Type */ |
| 182 | #define RADIUS_TUNNEL_TYPE_PPTP 1 |
| 183 | #define RADIUS_TUNNEL_TYPE_L2TP 3 |
| 184 | #define RADIUS_TUNNEL_TYPE_IPIP 7 |
| 185 | #define RADIUS_TUNNEL_TYPE_GRE 10 |
| 186 | #define RADIUS_TUNNEL_TYPE_VLAN 13 |
| 187 | |
| 188 | /* Tunnel-Medium-Type */ |
| 189 | #define RADIUS_TUNNEL_MEDIUM_TYPE_IPV4 1 |
| 190 | #define RADIUS_TUNNEL_MEDIUM_TYPE_IPV6 2 |
| 191 | #define RADIUS_TUNNEL_MEDIUM_TYPE_802 6 |
| 192 | |
| 193 | |
| 194 | struct radius_attr_vendor { |
| 195 | u8 vendor_type; |
| 196 | u8 vendor_length; |
| 197 | } STRUCT_PACKED; |
| 198 | |
| 199 | #define RADIUS_VENDOR_ID_CISCO 9 |
| 200 | #define RADIUS_CISCO_AV_PAIR 1 |
| 201 | |
| 202 | /* RFC 2548 - Microsoft Vendor-specific RADIUS Attributes */ |
| 203 | #define RADIUS_VENDOR_ID_MICROSOFT 311 |
| 204 | |
| 205 | enum { RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY = 16, |
| 206 | RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY = 17 |
| 207 | }; |
| 208 | |
| 209 | #ifdef _MSC_VER |
| 210 | #pragma pack(pop) |
| 211 | #endif /* _MSC_VER */ |
| 212 | |
| 213 | struct radius_ms_mppe_keys { |
| 214 | u8 *send; |
| 215 | size_t send_len; |
| 216 | u8 *recv; |
| 217 | size_t recv_len; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | /* RADIUS message structure for new and parsed messages */ |
| 222 | struct radius_msg { |
| 223 | unsigned char *buf; |
| 224 | size_t buf_size; /* total size allocated for buf */ |
| 225 | size_t buf_used; /* bytes used in buf */ |
| 226 | |
| 227 | struct radius_hdr *hdr; |
| 228 | |
| 229 | size_t *attr_pos; /* array of indexes to attributes (number of bytes |
| 230 | * from buf to the beginning of |
| 231 | * struct radius_attr_hdr). */ |
| 232 | size_t attr_size; /* total size of the attribute pointer array */ |
| 233 | size_t attr_used; /* total number of attributes in the array */ |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | /* Default size to be allocated for new RADIUS messages */ |
| 238 | #define RADIUS_DEFAULT_MSG_SIZE 1024 |
| 239 | |
| 240 | /* Default size to be allocated for attribute array */ |
| 241 | #define RADIUS_DEFAULT_ATTR_COUNT 16 |
| 242 | |
| 243 | |
| 244 | /* MAC address ASCII format for IEEE 802.1X use |
| 245 | * (draft-congdon-radius-8021x-20.txt) */ |
| 246 | #define RADIUS_802_1X_ADDR_FORMAT "%02X-%02X-%02X-%02X-%02X-%02X" |
| 247 | /* MAC address ASCII format for non-802.1X use */ |
| 248 | #define RADIUS_ADDR_FORMAT "%02x%02x%02x%02x%02x%02x" |
| 249 | |
| 250 | struct radius_msg *radius_msg_new(u8 code, u8 identifier); |
| 251 | int radius_msg_initialize(struct radius_msg *msg, size_t init_len); |
| 252 | void radius_msg_set_hdr(struct radius_msg *msg, u8 code, u8 identifier); |
| 253 | void radius_msg_free(struct radius_msg *msg); |
| 254 | int radius_msg_finish(struct radius_msg *msg, const u8 *secret, |
| 255 | size_t secret_len); |
| 256 | int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret, |
| 257 | size_t secret_len, const u8 *req_authenticator); |
| 258 | void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret, |
| 259 | size_t secret_len); |
| 260 | struct radius_attr_hdr *radius_msg_add_attr(struct radius_msg *msg, u8 type, |
| 261 | const u8 *data, size_t data_len); |
| 262 | int radius_msg_add_eap(struct radius_msg *msg, const u8 *data, |
| 263 | size_t data_len); |
| 264 | u8 *radius_msg_get_eap(struct radius_msg *msg, size_t *len); |
| 265 | int radius_msg_verify(struct radius_msg *msg, const u8 *secret, |
| 266 | size_t secret_len, struct radius_msg *sent_msg, |
| 267 | int auth); |
| 268 | int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret, |
| 269 | size_t secret_len, const u8 *req_auth); |
| 270 | int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src, |
| 271 | u8 type); |
| 272 | void radius_msg_make_authenticator(struct radius_msg *msg, |
| 273 | const u8 *data, size_t len); |
| 274 | struct radius_ms_mppe_keys * |
| 275 | radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg, |
| 276 | const u8 *secret, size_t secret_len); |
| 277 | struct radius_ms_mppe_keys * |
| 278 | radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg, |
| 279 | const u8 *secret, size_t secret_len); |
| 280 | int radius_msg_add_mppe_keys(struct radius_msg *msg, |
| 281 | const u8 *req_authenticator, |
| 282 | const u8 *secret, size_t secret_len, |
| 283 | const u8 *send_key, size_t send_key_len, |
| 284 | const u8 *recv_key, size_t recv_key_len); |
| 285 | struct radius_attr_hdr * |
| 286 | radius_msg_add_attr_user_password(struct radius_msg *msg, |
| 287 | const u8 *data, size_t data_len, |
| 288 | const u8 *secret, size_t secret_len); |
| 289 | int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len); |
| 290 | int radius_msg_get_vlanid(struct radius_msg *msg); |
| 291 | |
| 292 | static inline int radius_msg_add_attr_int32(struct radius_msg *msg, u8 type, |
| 293 | u32 value) |
| 294 | { |
| 295 | u32 val = htonl(value); |
| 296 | return radius_msg_add_attr(msg, type, (u8 *) &val, 4) != NULL; |
| 297 | } |
| 298 | |
| 299 | int radius_msg_add_attr_to_array(struct radius_msg *msg, struct radius_attr_hdr *attr); |
| 300 | static inline int radius_msg_get_attr_int32(struct radius_msg *msg, u8 type, |
| 301 | u32 *value) |
| 302 | { |
| 303 | u32 val; |
| 304 | int res; |
| 305 | res = radius_msg_get_attr(msg, type, (u8 *) &val, 4); |
| 306 | if (res != 4) |
| 307 | return -1; |
| 308 | |
| 309 | *value = ntohl(val); |
| 310 | return 0; |
| 311 | } |
| 312 | int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf, |
| 313 | size_t *len, const u8 *start); |
| 314 | int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len); |
| 315 | |
| 316 | |
| 317 | struct radius_attr_data { |
| 318 | u8 *data; |
| 319 | size_t len; |
| 320 | }; |
| 321 | |
| 322 | struct radius_class_data { |
| 323 | struct radius_attr_data *attr; |
| 324 | size_t count; |
| 325 | }; |
| 326 | |
| 327 | void radius_free_class(struct radius_class_data *c); |
| 328 | int radius_copy_class(struct radius_class_data *dst, |
| 329 | const struct radius_class_data *src); |
| 330 | |
| 331 | #endif /* RADIUS_H */ |