blob: 297fee7cc7122c5a8bcce90b678ae3a31a118702 [file] [log] [blame]
Ari Saha79d7c252015-06-26 10:31:48 -07001/*
2 *
3 * * Copyright 2015 AT&T Foundry
4 * *
5 * * Licensed under the Apache License, Version 2.0 (the "License");
6 * * you may not use this file except in compliance with the License.
7 * * You may obtain a copy of the License at
8 * *
9 * * http://www.apache.org/licenses/LICENSE-2.0
10 * *
11 * * Unless required by applicable law or agreed to in writing, software
12 * * distributed under the License is distributed on an "AS IS" BASIS,
13 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * * See the License for the specific language governing permissions and
15 * * limitations under the License.
16 *
17 */
18
Jonathan Hart4a60bb32015-06-30 15:31:20 -070019package org.onlab.packet;
Ari Saha79d7c252015-06-26 10:31:48 -070020
Ari Saha79d7c252015-06-26 10:31:48 -070021import org.slf4j.Logger;
22
23import javax.crypto.Mac;
24import javax.crypto.spec.SecretKeySpec;
25import java.io.ByteArrayOutputStream;
26import java.io.IOException;
27import java.nio.ByteBuffer;
28import java.security.SecureRandom;
29import java.util.ArrayList;
30import java.util.Arrays;
Jonathan Hart4a60bb32015-06-30 15:31:20 -070031import java.util.List;
Ari Saha79d7c252015-06-26 10:31:48 -070032
Jonathan Hart4a60bb32015-06-30 15:31:20 -070033import static org.onlab.packet.PacketUtils.checkHeaderLength;
34import static org.onlab.packet.PacketUtils.checkInput;
Ari Saha79d7c252015-06-26 10:31:48 -070035import static org.slf4j.LoggerFactory.getLogger;
36
37/**
Jonathan Hart4a60bb32015-06-30 15:31:20 -070038 * RADIUS packet.
Ari Saha79d7c252015-06-26 10:31:48 -070039 */
40public class RADIUS extends BasePacket {
41 protected byte code;
42 protected byte identifier;
43 protected short length = RADIUS_MIN_LENGTH;
44 protected byte[] authenticator = new byte[16];
Jonathan Hart4a60bb32015-06-30 15:31:20 -070045 protected List<RADIUSAttribute> attributes = new ArrayList<>();
Ari Saha79d7c252015-06-26 10:31:48 -070046
Jonathan Hart4a60bb32015-06-30 15:31:20 -070047 // RADIUS parameters
Ari Saha79d7c252015-06-26 10:31:48 -070048 public static final short RADIUS_MIN_LENGTH = 20;
49 public static final short MAX_ATTR_VALUE_LENGTH = 253;
50 public static final short RADIUS_MAX_LENGTH = 4096;
51
Jonathan Hart4a60bb32015-06-30 15:31:20 -070052 // RADIUS packet types
Ari Saha79d7c252015-06-26 10:31:48 -070053 public static final byte RADIUS_CODE_ACCESS_REQUEST = 0x01;
54 public static final byte RADIUS_CODE_ACCESS_ACCEPT = 0x02;
55 public static final byte RADIUS_CODE_ACCESS_REJECT = 0x03;
56 public static final byte RADIUS_CODE_ACCOUNTING_REQUEST = 0x04;
57 public static final byte RADIUS_CODE_ACCOUNTING_RESPONSE = 0x05;
58 public static final byte RADIUS_CODE_ACCESS_CHALLENGE = 0x0b;
59
60 private final Logger log = getLogger(getClass());
61
Jonathan Hart4a60bb32015-06-30 15:31:20 -070062 /**
63 * Default constructor.
64 */
Ari Saha79d7c252015-06-26 10:31:48 -070065 public RADIUS() {
66 }
67
Jonathan Hart4a60bb32015-06-30 15:31:20 -070068 /**
69 * Constructs a RADIUS packet with the given code and identifier.
70 *
71 * @param code code
72 * @param identifier identifier
73 */
Ari Saha79d7c252015-06-26 10:31:48 -070074 public RADIUS(byte code, byte identifier) {
75 this.code = code;
76 this.identifier = identifier;
77 }
78
Jonathan Hart4a60bb32015-06-30 15:31:20 -070079 /**
80 * Gets the code.
81 *
82 * @return code
83 */
Ari Saha79d7c252015-06-26 10:31:48 -070084 public byte getCode() {
85 return this.code;
86 }
87
Jonathan Hart4a60bb32015-06-30 15:31:20 -070088 /**
89 * Sets the code.
90 *
91 * @param code code
92 */
Ari Saha79d7c252015-06-26 10:31:48 -070093 public void setCode(byte code) {
94 this.code = code;
95 }
96
Jonathan Hart4a60bb32015-06-30 15:31:20 -070097 /**
98 * Gets the identifier.
99 *
100 * @return identifier
101 */
Ari Saha79d7c252015-06-26 10:31:48 -0700102 public byte getIdentifier() {
103 return this.identifier;
104 }
105
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700106 /**
107 * Sets the identifier.
108 *
109 * @param identifier identifier
110 */
Ari Saha79d7c252015-06-26 10:31:48 -0700111 public void setIdentifier(byte identifier) {
112 this.identifier = identifier;
113 }
114
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700115 /**
116 * Gets the authenticator.
117 *
118 * @return authenticator
119 */
Ari Saha79d7c252015-06-26 10:31:48 -0700120 public byte[] getAuthenticator() {
121 return this.authenticator;
122 }
123
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700124 /**
125 * Sets the authenticator.
126 *
127 * @param authenticator authenticator
128 */
129 public void setAuthenticator(byte[] authenticator) {
130 this.authenticator = authenticator;
Ari Saha79d7c252015-06-26 10:31:48 -0700131 }
132
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700133 /**
134 * Generates an authenticator code.
135 *
136 * @return the authenticator
137 */
Ari Saha79d7c252015-06-26 10:31:48 -0700138 public byte[] generateAuthCode() {
139 new SecureRandom().nextBytes(this.authenticator);
140 return this.authenticator;
141 }
142
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700143 /**
144 * Checks if the packet's code field is valid.
145 *
146 * @return whether the code is valid
147 */
Ari Saha79d7c252015-06-26 10:31:48 -0700148 public boolean isValidCode() {
149 return this.code == RADIUS_CODE_ACCESS_REQUEST ||
150 this.code == RADIUS_CODE_ACCESS_ACCEPT ||
151 this.code == RADIUS_CODE_ACCESS_REJECT ||
152 this.code == RADIUS_CODE_ACCOUNTING_REQUEST ||
153 this.code == RADIUS_CODE_ACCOUNTING_RESPONSE ||
154 this.code == RADIUS_CODE_ACCESS_CHALLENGE;
155 }
156
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700157 /**
158 * Adds a message authenticator to the packet based on the given key.
159 *
160 * @param key key to generate message authenticator
161 * @return the messgae authenticator RADIUS attribute
162 */
Ari Saha79d7c252015-06-26 10:31:48 -0700163 public RADIUSAttribute addMessageAuthenticator(String key) {
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700164 // Message-Authenticator = HMAC-MD5 (Type, Identifier, Length,
165 // Request Authenticator, Attributes)
166 // When the message integrity check is calculated the signature string
167 // should be considered to be sixteen octets of zero.
Ari Saha79d7c252015-06-26 10:31:48 -0700168 byte[] hashOutput = new byte[16];
169 Arrays.fill(hashOutput, (byte) 0);
170
171 RADIUSAttribute authAttribute = this.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH);
172 if (authAttribute != null) {
173 // If Message-Authenticator was already present, override it
174 this.log.warn("Attempted to add duplicate Message-Authenticator");
175 authAttribute = this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, hashOutput);
176 } else {
177 // Else generate a new attribute padded with zeroes
178 authAttribute = this.setAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, hashOutput);
179 }
180 // Calculate the MD5 HMAC based on the message
181 try {
182 SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
183 Mac mac = Mac.getInstance("HmacMD5");
184 mac.init(keySpec);
185 hashOutput = mac.doFinal(this.serialize());
186 // Update HMAC in Message-Authenticator
187 authAttribute = this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, hashOutput);
188 } catch (Exception e) {
189 this.log.error("Failed to generate message authenticator: {}", e.getMessage());
190 }
191
192 return authAttribute;
193 }
194
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700195 /**
196 * Checks the message authenticator in the packet with one generated from
197 * the given key.
198 *
199 * @param key key to generate message authenticator
200 * @return whether the message authenticators match or not
201 */
Ari Saha79d7c252015-06-26 10:31:48 -0700202 public boolean checkMessageAuthenticator(String key) {
203 byte[] newHash = new byte[16];
204 Arrays.fill(newHash, (byte) 0);
205 byte[] messageAuthenticator = this.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue();
206 this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash);
207 // Calculate the MD5 HMAC based on the message
208 try {
209 SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
210 Mac mac = Mac.getInstance("HmacMD5");
211 mac.init(keySpec);
212 newHash = mac.doFinal(this.serialize());
213 } catch (Exception e) {
214 log.error("Failed to generate message authenticator: {}", e.getMessage());
215 }
216 this.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator);
217 // Compare the calculated Message-Authenticator with the one in the message
218 return Arrays.equals(newHash, messageAuthenticator);
219 }
220
221 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700222 * Encapsulates an EAP packet in this RADIUS packet.
223 *
224 * @param message EAP message object to be embedded in the RADIUS
225 * EAP-Message attributed
Ari Saha79d7c252015-06-26 10:31:48 -0700226 */
227 public void encapsulateMessage(EAP message) {
228 if (message.length <= MAX_ATTR_VALUE_LENGTH) {
229 // Use the regular serialization method as it fits into one EAP-Message attribute
230 this.setAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE,
231 message.serialize());
232 } else {
233 // Segment the message into chucks and embed them in several EAP-Message attributes
234 short remainingLength = message.length;
235 byte[] messageBuffer = message.serialize();
236 final ByteBuffer bb = ByteBuffer.wrap(messageBuffer);
237 while (bb.hasRemaining()) {
238 byte[] messageAttributeData;
239 if (remainingLength > MAX_ATTR_VALUE_LENGTH) {
240 // The remaining data is still too long to fit into one attribute, keep going
241 messageAttributeData = new byte[MAX_ATTR_VALUE_LENGTH];
242 bb.get(messageAttributeData, 0, MAX_ATTR_VALUE_LENGTH);
243 remainingLength -= MAX_ATTR_VALUE_LENGTH;
244 } else {
245 // The remaining data fits, this will be the last chunk
246 messageAttributeData = new byte[remainingLength];
247 bb.get(messageAttributeData, 0, remainingLength);
248 }
249 this.attributes.add(new RADIUSAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE,
250 (byte) (messageAttributeData.length + 2), messageAttributeData));
251
252 // Adding the size of the data to the total RADIUS length
253 this.length += (short) (messageAttributeData.length & 0xFF);
254 // Adding the size of the overhead attribute type and length
255 this.length += 2;
256 }
257 }
258 }
259
260 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700261 * Decapsulates an EAP packet from the RADIUS packet.
262 *
Ari Saha79d7c252015-06-26 10:31:48 -0700263 * @return An EAP object containing the reassembled EAP message
264 */
265 public EAP decapsulateMessage() {
266 EAP message = new EAP();
267 ByteArrayOutputStream messageStream = new ByteArrayOutputStream();
268 // Iterating through EAP-Message attributes to concatenate their value
269 for (RADIUSAttribute ra : this.getAttributeList(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE)) {
270 try {
271 messageStream.write(ra.getValue());
272 } catch (IOException e) {
273 log.error("Error while reassembling EAP message: {}", e.getMessage());
274 }
275 }
276 // Assembling EAP object from the concatenated stream
277 message.deserialize(messageStream.toByteArray(), 0, messageStream.size());
278 return message;
279 }
280
281 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700282 * Gets a list of attributes from the RADIUS packet.
283 *
284 * @param attrType the type field of the required attributes
Ari Saha79d7c252015-06-26 10:31:48 -0700285 * @return List of the attributes that matches the type or an empty list if there is none
286 */
287 public ArrayList<RADIUSAttribute> getAttributeList(byte attrType) {
288 ArrayList<RADIUSAttribute> attrList = new ArrayList<>();
289 for (int i = 0; i < this.attributes.size(); i++) {
290 if (this.attributes.get(i).getType() == attrType) {
291 attrList.add(this.attributes.get(i));
292 }
293 }
294 return attrList;
295 }
296
297 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700298 * Gets an attribute from the RADIUS packet.
299 *
300 * @param attrType the type field of the required attribute
Ari Saha79d7c252015-06-26 10:31:48 -0700301 * @return the first attribute that matches the type or null if does not exist
302 */
303 public RADIUSAttribute getAttribute(byte attrType) {
304 for (int i = 0; i < this.attributes.size(); i++) {
305 if (this.attributes.get(i).getType() == attrType) {
306 return this.attributes.get(i);
307 }
308 }
309 return null;
310 }
311
312 /**
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700313 * Sets an attribute in the RADIUS packet.
314 *
315 * @param attrType the type field of the attribute to set
316 * @param value value to be set
Ari Saha79d7c252015-06-26 10:31:48 -0700317 * @return reference to the attribute object
318 */
319 public RADIUSAttribute setAttribute(byte attrType, byte[] value) {
320 byte attrLength = (byte) (value.length + 2);
321 RADIUSAttribute newAttribute = new RADIUSAttribute(attrType, attrLength, value);
322 this.attributes.add(newAttribute);
323 this.length += (short) (attrLength & 0xFF);
324 return newAttribute;
325 }
326
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700327 /**
328 * Updates an attribute in the RADIUS packet.
329 *
330 * @param attrType the type field of the attribute to update
331 * @param value the value to update to
332 * @return reference to the attribute object
333 */
Ari Saha79d7c252015-06-26 10:31:48 -0700334 public RADIUSAttribute updateAttribute(byte attrType, byte[] value) {
335 for (int i = 0; i < this.attributes.size(); i++) {
336 if (this.attributes.get(i).getType() == attrType) {
337 this.length -= (short) (this.attributes.get(i).getLength() & 0xFF);
338 RADIUSAttribute newAttr = new RADIUSAttribute(attrType, (byte) (value.length + 2), value);
339 this.attributes.set(i, newAttr);
340 this.length += (short) (newAttr.getLength() & 0xFF);
341 return newAttr;
342 }
343 }
344 return null;
345 }
346
Jonathan Hart4a60bb32015-06-30 15:31:20 -0700347 /**
348 * Deserializer for RADIUS packets.
349 *
350 * @return deserializer
351 */
352 public static Deserializer<RADIUS> deserializer() {
353 return (data, offset, length) -> {
354 checkInput(data, offset, length, RADIUS_MIN_LENGTH);
355
356 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
357 RADIUS radius = new RADIUS();
358 radius.code = bb.get();
359 radius.identifier = bb.get();
360 radius.length = bb.getShort();
361 bb.get(radius.authenticator, 0, 16);
362
363 checkHeaderLength(length, radius.length);
364
365 int remainingLength = radius.length - RADIUS_MIN_LENGTH;
366 while (remainingLength > 0 && bb.hasRemaining()) {
367
368 RADIUSAttribute attr = new RADIUSAttribute();
369 attr.setType(bb.get());
370 attr.setLength(bb.get());
371 short attrLength = (short) (attr.length & 0xff);
372 attr.value = new byte[attrLength - 2];
373 bb.get(attr.value, 0, attrLength - 2);
374 radius.attributes.add(attr);
375 remainingLength -= attr.length;
376 }
377 return radius;
378 };
379 }
380
Ari Saha79d7c252015-06-26 10:31:48 -0700381 @Override
382 public byte[] serialize() {
383 final byte[] data = new byte[this.length];
384 final ByteBuffer bb = ByteBuffer.wrap(data);
385
386 bb.put(this.code);
387 bb.put(this.identifier);
388 bb.putShort(this.length);
389 bb.put(this.authenticator);
390 for (int i = 0; i < this.attributes.size(); i++) {
391 RADIUSAttribute attr = this.attributes.get(i);
392 bb.put(attr.getType());
393 bb.put(attr.getLength());
394 bb.put(attr.getValue());
395 }
396
397 return data;
398 }
399
400 @Override
401 public IPacket deserialize(final byte[] data, final int offset,
402 final int length) {
403 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
404 this.code = bb.get();
405 this.identifier = bb.get();
406 this.length = bb.getShort();
407 bb.get(this.authenticator, 0, 16);
408
409 int remainingLength = this.length - RADIUS_MIN_LENGTH;
410 while (remainingLength > 0 && bb.hasRemaining()) {
411 RADIUSAttribute attr = new RADIUSAttribute();
412 attr.setType(bb.get());
413 attr.setLength(bb.get());
414 short attrLength = (short) (attr.length & 0xff);
415 attr.value = new byte[attrLength - 2];
416 bb.get(attr.value, 0, attrLength - 2);
417 this.attributes.add(attr);
418 remainingLength -= attr.length;
419 }
420 return this;
421 }
422
423}