Jonathan Hart
Committed by Gerrit Code Review

Cleaned up AAA app now it's in the ONOS core.

Moved packets into the packet library, minor app cleanups and javadoc.

Change-Id: I7ee04d09f82051fdb2a9bcfe577cb163661d5055
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.aaa.packet;
import org.onlab.packet.Deserializer;
import org.onlab.packet.EthType;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPacket;
import java.util.HashMap;
import java.util.Map;
/**
* Created by jono on 5/19/15.
*/
public final class EAPEthernet extends Ethernet {
public static final short TYPE_PAE = (short) 0x888e;
private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP =
new HashMap<>();
private EAPEthernet() {
}
static {
for (EthType.EtherType ethType : EthType.EtherType.values()) {
if (ethType.deserializer() != null) {
ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(),
ethType.deserializer());
}
}
ETHERTYPE_DESERIALIZER_MAP.put((short) 0x888e, EAPOL.deserializer());
}
}
......@@ -16,29 +16,30 @@
*
*/
package org.onosproject.aaa.packet;
import org.onlab.packet.BasePacket;
import org.onlab.packet.IPacket;
package org.onlab.packet;
import java.nio.ByteBuffer;
import static org.onlab.packet.PacketUtils.checkHeaderLength;
import static org.onlab.packet.PacketUtils.checkInput;
/**
*
* EAP (Extensible Authentication Protocol) packet.
*/
public class EAP extends BasePacket {
private static final int HEADER_LENGTH = 4;
public static final short MIN_LEN = 0x4;
public static final short EAP_HDR_LEN_REQ_RESP = 5;
public static final short EAP_HDR_LEN_SUC_FAIL = 4;
/* EAP Code */
// EAP Code
public static final byte REQUEST = 0x1;
public static final byte RESPONSE = 0x2;
public static final byte SUCCESS = 0x3;
public static final byte FAILURE = 0x4;
/* EAP Attribute Type */
// EAP Attribute Type
public static final byte ATTR_IDENTITY = 0x1;
public static final byte ATTR_NOTIFICATION = 0x2;
public static final byte ATTR_NAK = 0x3;
......@@ -55,7 +56,8 @@ public class EAP extends BasePacket {
/**
* Get the EAP code.
* Gets the EAP code.
*
* @return EAP code
*/
public byte getCode() {
......@@ -64,7 +66,8 @@ public class EAP extends BasePacket {
/**
* Set the EAP code.
* Sets the EAP code.
*
* @param code EAP code
* @return this
*/
......@@ -74,7 +77,8 @@ public class EAP extends BasePacket {
}
/**
* Get the EAP identifier.
* Gets the EAP identifier.
*
* @return EAP identifier
*/
public byte getIdentifier() {
......@@ -82,7 +86,8 @@ public class EAP extends BasePacket {
}
/**
* Set the EAP identifier.
* Sets the EAP identifier.
*
* @param identifier
* @return this
*/
......@@ -92,7 +97,8 @@ public class EAP extends BasePacket {
}
/**
* Get the get packet length.
* Gets the get packet length.
*
* @return packet length
*/
public short getLength() {
......@@ -100,7 +106,8 @@ public class EAP extends BasePacket {
}
/**
* Set the packet length.
* Sets the packet length.
*
* @param length packet length
* @return this
*/
......@@ -110,7 +117,8 @@ public class EAP extends BasePacket {
}
/**
* Get the data type.
* Gets the data type.
*
* @return data type
*/
public byte getDataType() {
......@@ -118,7 +126,8 @@ public class EAP extends BasePacket {
}
/**
* Set the data type.
* Sets the data type.
*
* @param type data type
* @return this
*/
......@@ -128,7 +137,8 @@ public class EAP extends BasePacket {
}
/**
* Get the EAP data.
* Gets the EAP data.
*
* @return EAP data
*/
public byte[] getData() {
......@@ -136,7 +146,8 @@ public class EAP extends BasePacket {
}
/**
* Set the EAP data.
* Sets the EAP data.
*
* @param data EAP data to be set
* @return this
*/
......@@ -146,7 +157,7 @@ public class EAP extends BasePacket {
}
/**
* Default EAP constructor that set the EAP code to 0.
* Default EAP constructor that sets the EAP code to 0.
*/
public EAP() {
this.code = 0;
......@@ -154,6 +165,7 @@ public class EAP extends BasePacket {
/**
* EAP constructor that initially sets all fields.
*
* @param code EAP code
* @param identifier EAP identifier
* @param type packet type
......@@ -172,10 +184,36 @@ public class EAP extends BasePacket {
}
/**
* Serializes the packet, based on the code/type using the payload
* to compute its length.
* @return the serialized payload
* Deserializer for EAP packets.
*
* @return deserializer
*/
public static Deserializer<EAP> deserializer() {
return (data, offset, length) -> {
checkInput(data, offset, length, HEADER_LENGTH);
EAP eap = new EAP();
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
eap.code = bb.get();
eap.identifier = bb.get();
eap.length = bb.getShort();
checkHeaderLength(length, HEADER_LENGTH + eap.length);
int dataLength;
if (eap.code == REQUEST || eap.code == RESPONSE) {
eap.type = bb.get();
dataLength = eap.length - 5;
} else {
dataLength = eap.length - 4;
}
eap.data = new byte[dataLength];
bb.get(eap.data);
return eap;
};
}
@Override
public byte[] serialize() {
final byte[] data = new byte[this.length];
......
......@@ -16,20 +16,15 @@
*
*/
package org.onosproject.aaa.packet;
import org.onlab.packet.BasePacket;
import org.onlab.packet.Deserializer;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPacket;
import org.onlab.packet.MacAddress;
package org.onlab.packet;
import java.nio.ByteBuffer;
import static org.onlab.packet.PacketUtils.checkHeaderLength;
import static org.onlab.packet.PacketUtils.checkInput;
/**
*
* EAPOL (Extensible Authentication Protocol over LAN) header.
*/
public class EAPOL extends BasePacket {
......@@ -37,7 +32,9 @@ public class EAPOL extends BasePacket {
private byte eapolType;
private short packetLength;
/* EAPOL Packet Type */
private static final int HEADER_LENGTH = 4;
// EAPOL Packet Type
public static final byte EAPOL_PACKET = 0x0;
public static final byte EAPOL_START = 0x1;
public static final byte EAPOL_LOGOFF = 0x2;
......@@ -48,9 +45,9 @@ public class EAPOL extends BasePacket {
(byte) 0x01, (byte) 0x80, (byte) 0xc2, (byte) 0x00, (byte) 0x00, (byte) 0x03
});
/**
* Get version.
* Gets the version.
*
* @return version
*/
public byte getVersion() {
......@@ -58,7 +55,8 @@ public class EAPOL extends BasePacket {
}
/**
* Set version.
* Sets the version.
*
* @param version EAPOL version
* @return this
*/
......@@ -68,7 +66,8 @@ public class EAPOL extends BasePacket {
}
/**
* Get type.
* Gets the type.
*
* @return EAPOL type
*/
public byte getEapolType() {
......@@ -76,7 +75,8 @@ public class EAPOL extends BasePacket {
}
/**
* Set EAPOL type.
* Sets the EAPOL type.
*
* @param eapolType EAPOL type
* @return this
*/
......@@ -86,7 +86,8 @@ public class EAPOL extends BasePacket {
}
/**
* Get packet length.
* Gets the packet length.
*
* @return packet length
*/
public short getPacketLength() {
......@@ -94,7 +95,8 @@ public class EAPOL extends BasePacket {
}
/**
* Set packet length.
* Sets the packet length.
*
* @param packetLen packet length
* @return this
*/
......@@ -103,16 +105,14 @@ public class EAPOL extends BasePacket {
return this;
}
/**
* Serializes the packet, based on the code/type using the payload
* to compute its length.
*
* @return this
*/
@Override
public byte[] serialize() {
byte[] payloadData = null;
if (this.payload != null) {
......@@ -120,15 +120,16 @@ public class EAPOL extends BasePacket {
payloadData = this.payload.serialize();
}
//prepare the buffer to hold the version (1), packet type (1), packet length (2) and the eap payload.
//if there is no payload, packet length is 0
// prepare the buffer to hold the version (1), packet type (1),
// packet length (2) and the eap payload.
// if there is no payload, packet length is 0
byte[] data = new byte[4 + this.packetLength];
final ByteBuffer bb = ByteBuffer.wrap(data);
bb.put(this.version);
bb.put(this.eapolType);
bb.putShort(this.packetLength);
//put the EAP payload
// put the EAP payload
if (payloadData != null) {
bb.put(payloadData);
}
......@@ -136,8 +137,6 @@ public class EAPOL extends BasePacket {
return data;
}
@Override
public int hashCode() {
final int prime = 3889;
......@@ -149,39 +148,13 @@ public class EAPOL extends BasePacket {
}
/**
* Deserializer for EAPOL packets.
*
* @param dstMac
* @param srcMac
* @param eapolType
* @param eap
* @return Ethernet frame
* @return deserializer
*/
public static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
short vlan, byte eapolType, EAP eap) {
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(dstMac.toBytes());
eth.setSourceMACAddress(srcMac.toBytes());
eth.setEtherType(EAPEthernet.TYPE_PAE);
if (vlan != Ethernet.VLAN_UNTAGGED) {
eth.setVlanID(vlan);
}
//eapol header
EAPOL eapol = new EAPOL();
eapol.setEapolType(eapolType);
eapol.setPacketLength(eap.getLength());
//eap part
eapol.setPayload(eap);
eth.setPayload(eapol);
eth.setPad(true);
return eth;
}
public static Deserializer<EAPOL> deserializer() {
return (data, offset, length) -> {
checkInput(data, offset, length, 0);
checkInput(data, offset, length, HEADER_LENGTH);
EAPOL eapol = new EAPOL();
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
......@@ -190,12 +163,14 @@ public class EAPOL extends BasePacket {
eapol.setPacketLength(bb.getShort());
if (eapol.packetLength > 0) {
//deserialize the EAP Payload
eapol.payload = new EAP();
checkHeaderLength(length, HEADER_LENGTH + eapol.packetLength);
// deserialize the EAP Payload
eapol.payload = EAP.deserializer().deserialize(data,
bb.position(), bb.limit() - bb.position());
eapol.payload = eapol.payload.deserialize(data, bb.position(), length - 4);
eapol.payload.setParent(eapol);
}
return eapol;
};
}
......@@ -205,24 +180,20 @@ public class EAPOL extends BasePacket {
final int length) {
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
//deserialize the EAPOL header
// deserialize the EAPOL header
this.version = bb.get();
this.eapolType = bb.get();
this.packetLength = bb.getShort();
if (this.packetLength > 0) {
//deserialize the EAP Payload
// deserialize the EAP Payload
this.payload = new EAP();
this.payload = this.payload.deserialize(data, bb.position(), length - 4);
this.payload.setParent(this);
}
return this;
}
}
......
......@@ -35,8 +35,9 @@ public class EthType {
VLAN(0x8100, "vlan", null),
BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()),
MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer());
MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()),
UNKNOWN(0, "unknown", null);
private final EthType etherType;
......@@ -69,6 +70,15 @@ public class EthType {
return deserializer;
}
public static EtherType lookup(short etherType) {
for (EtherType ethType : EtherType.values()) {
if (ethType.ethType().toShort() == etherType) {
return ethType;
}
}
return UNKNOWN;
}
}
......
......@@ -16,10 +16,8 @@
*
*/
package org.onosproject.aaa.packet;
package org.onlab.packet;
import org.onlab.packet.BasePacket;
import org.onlab.packet.IPacket;
import org.slf4j.Logger;
import javax.crypto.Mac;
......@@ -30,25 +28,28 @@ import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.onlab.packet.PacketUtils.checkHeaderLength;
import static org.onlab.packet.PacketUtils.checkInput;
import static org.slf4j.LoggerFactory.getLogger;
/**
*
* RADIUS packet.
*/
public class RADIUS extends BasePacket {
protected byte code;
protected byte identifier;
protected short length = RADIUS_MIN_LENGTH;
protected byte[] authenticator = new byte[16];
protected ArrayList<RADIUSAttribute> attributes = new ArrayList<>();
protected List<RADIUSAttribute> attributes = new ArrayList<>();
/* RADIUS parameters */
// RADIUS parameters
public static final short RADIUS_MIN_LENGTH = 20;
public static final short MAX_ATTR_VALUE_LENGTH = 253;
public static final short RADIUS_MAX_LENGTH = 4096;
/* RADIUS packet types */
// RADIUS packet types
public static final byte RADIUS_CODE_ACCESS_REQUEST = 0x01;
public static final byte RADIUS_CODE_ACCESS_ACCEPT = 0x02;
public static final byte RADIUS_CODE_ACCESS_REJECT = 0x03;
......@@ -58,43 +59,92 @@ public class RADIUS extends BasePacket {
private final Logger log = getLogger(getClass());
/**
* Default constructor.
*/
public RADIUS() {
}
/**
* Constructs a RADIUS packet with the given code and identifier.
*
* @param code code
* @param identifier identifier
*/
public RADIUS(byte code, byte identifier) {
this.code = code;
this.identifier = identifier;
}
/**
* Gets the code.
*
* @return code
*/
public byte getCode() {
return this.code;
}
/**
* Sets the code.
*
* @param code code
*/
public void setCode(byte code) {
this.code = code;
}
/**
* Gets the identifier.
*
* @return identifier
*/
public byte getIdentifier() {
return this.identifier;
}
/**
* Sets the identifier.
*
* @param identifier identifier
*/
public void setIdentifier(byte identifier) {
this.identifier = identifier;
}
/**
* Gets the authenticator.
*
* @return authenticator
*/
public byte[] getAuthenticator() {
return this.authenticator;
}
public void setAuthenticator(byte[] a) {
this.authenticator = a;
/**
* Sets the authenticator.
*
* @param authenticator authenticator
*/
public void setAuthenticator(byte[] authenticator) {
this.authenticator = authenticator;
}
/**
* Generates an authenticator code.
*
* @return the authenticator
*/
public byte[] generateAuthCode() {
new SecureRandom().nextBytes(this.authenticator);
return this.authenticator;
}
/**
* Checks if the packet's code field is valid.
*
* @return whether the code is valid
*/
public boolean isValidCode() {
return this.code == RADIUS_CODE_ACCESS_REQUEST ||
this.code == RADIUS_CODE_ACCESS_ACCEPT ||
......@@ -104,11 +154,17 @@ public class RADIUS extends BasePacket {
this.code == RADIUS_CODE_ACCESS_CHALLENGE;
}
/**
* Adds a message authenticator to the packet based on the given key.
*
* @param key key to generate message authenticator
* @return the messgae authenticator RADIUS attribute
*/
public RADIUSAttribute addMessageAuthenticator(String key) {
/* Message-Authenticator = HMAC-MD5 (Type, Identifier, Length, Request Authenticator, Attributes)
When the message integrity check is calculated the signature string should be considered to be
sixteen octets of zero.
*/
// Message-Authenticator = HMAC-MD5 (Type, Identifier, Length,
// Request Authenticator, Attributes)
// When the message integrity check is calculated the signature string
// should be considered to be sixteen octets of zero.
byte[] hashOutput = new byte[16];
Arrays.fill(hashOutput, (byte) 0);
......@@ -136,6 +192,13 @@ public class RADIUS extends BasePacket {
return authAttribute;
}
/**
* Checks the message authenticator in the packet with one generated from
* the given key.
*
* @param key key to generate message authenticator
* @return whether the message authenticators match or not
*/
public boolean checkMessageAuthenticator(String key) {
byte[] newHash = new byte[16];
Arrays.fill(newHash, (byte) 0);
......@@ -156,8 +219,10 @@ public class RADIUS extends BasePacket {
}
/**
* @param message
* EAP message object to be embedded in the RADIUS EAP-Message attributed
* Encapsulates an EAP packet in this RADIUS packet.
*
* @param message EAP message object to be embedded in the RADIUS
* EAP-Message attributed
*/
public void encapsulateMessage(EAP message) {
if (message.length <= MAX_ATTR_VALUE_LENGTH) {
......@@ -193,6 +258,8 @@ public class RADIUS extends BasePacket {
}
/**
* Decapsulates an EAP packet from the RADIUS packet.
*
* @return An EAP object containing the reassembled EAP message
*/
public EAP decapsulateMessage() {
......@@ -212,8 +279,9 @@ public class RADIUS extends BasePacket {
}
/**
* @param attrType
* the type field of the required attributes
* Gets a list of attributes from the RADIUS packet.
*
* @param attrType the type field of the required attributes
* @return List of the attributes that matches the type or an empty list if there is none
*/
public ArrayList<RADIUSAttribute> getAttributeList(byte attrType) {
......@@ -227,8 +295,9 @@ public class RADIUS extends BasePacket {
}
/**
* @param attrType
* the type field of the required attribute
* Gets an attribute from the RADIUS packet.
*
* @param attrType the type field of the required attribute
* @return the first attribute that matches the type or null if does not exist
*/
public RADIUSAttribute getAttribute(byte attrType) {
......@@ -241,10 +310,10 @@ public class RADIUS extends BasePacket {
}
/**
* @param attrType
* the type field of the attribute to set
* @param value
* value to be set
* Sets an attribute in the RADIUS packet.
*
* @param attrType the type field of the attribute to set
* @param value value to be set
* @return reference to the attribute object
*/
public RADIUSAttribute setAttribute(byte attrType, byte[] value) {
......@@ -255,6 +324,13 @@ public class RADIUS extends BasePacket {
return newAttribute;
}
/**
* Updates an attribute in the RADIUS packet.
*
* @param attrType the type field of the attribute to update
* @param value the value to update to
* @return reference to the attribute object
*/
public RADIUSAttribute updateAttribute(byte attrType, byte[] value) {
for (int i = 0; i < this.attributes.size(); i++) {
if (this.attributes.get(i).getType() == attrType) {
......@@ -268,6 +344,40 @@ public class RADIUS extends BasePacket {
return null;
}
/**
* Deserializer for RADIUS packets.
*
* @return deserializer
*/
public static Deserializer<RADIUS> deserializer() {
return (data, offset, length) -> {
checkInput(data, offset, length, RADIUS_MIN_LENGTH);
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
RADIUS radius = new RADIUS();
radius.code = bb.get();
radius.identifier = bb.get();
radius.length = bb.getShort();
bb.get(radius.authenticator, 0, 16);
checkHeaderLength(length, radius.length);
int remainingLength = radius.length - RADIUS_MIN_LENGTH;
while (remainingLength > 0 && bb.hasRemaining()) {
RADIUSAttribute attr = new RADIUSAttribute();
attr.setType(bb.get());
attr.setLength(bb.get());
short attrLength = (short) (attr.length & 0xff);
attr.value = new byte[attrLength - 2];
bb.get(attr.value, 0, attrLength - 2);
radius.attributes.add(attr);
remainingLength -= attr.length;
}
return radius;
};
}
@Override
public byte[] serialize() {
final byte[] data = new byte[this.length];
......
......@@ -16,16 +16,17 @@
*
*/
package org.onosproject.aaa.packet;
import java.nio.ByteBuffer;
package org.onlab.packet;
/**
* An attribute in a RADIUS packet.
*/
public class RADIUSAttribute {
protected byte type;
protected byte length;
protected byte[] value;
/* RADIUS attribute types */
// RADIUS attribute types
public static final byte RADIUS_ATTR_USERNAME = 1;
public static final byte RADIUS_ATTR_NAS_IP = 4;
public static final byte RADIUS_ATTR_NAS_PORT = 5;
......@@ -40,15 +41,30 @@ public class RADIUSAttribute {
public static final byte RADIUS_ATTR_MESSAGE_AUTH = 80;
public static final byte RADIUS_ATTR_NAS_PORT_ID = 87;
/**
* Default constructor.
*/
public RADIUSAttribute() {
}
/**
* Constructs a RADIUS attribute with the give type, length and value.
*
* @param type type
* @param length length
* @param value value
*/
public RADIUSAttribute(final byte type, final byte length, final byte[] value) {
this.type = type;
this.length = length;
this.value = value;
}
/**
* Checks if the attribute type is valid.
*
* @return whether the type is valid or not
*/
public boolean isValidType() {
return this.type == RADIUS_ATTR_USERNAME ||
this.type == RADIUS_ATTR_NAS_IP ||
......@@ -64,6 +80,8 @@ public class RADIUSAttribute {
}
/**
* Gets the attribute type.
*
* @return the type
*/
public byte getType() {
......@@ -71,8 +89,9 @@ public class RADIUSAttribute {
}
/**
* @param type
* the code to set
* Sets the attribute type.
*
* @param type the code to set
* @return this
*/
public RADIUSAttribute setType(final byte type) {
......@@ -81,6 +100,8 @@ public class RADIUSAttribute {
}
/**
* Gets the attribute length.
*
* @return the length
*/
public byte getLength() {
......@@ -88,8 +109,9 @@ public class RADIUSAttribute {
}
/**
* @param length
* the length to set
* Sets the attribute length.
*
* @param length the length to set
* @return this
*/
public RADIUSAttribute setLength(final byte length) {
......@@ -98,6 +120,8 @@ public class RADIUSAttribute {
}
/**
* Gets the attribute value.
*
* @return the value
*/
public byte[] getValue() {
......@@ -105,8 +129,9 @@ public class RADIUSAttribute {
}
/**
* @param value
* the data to set
* Sets the attribute value.
*
* @param value the data to set
* @return this
*/
public RADIUSAttribute setValue(final byte[] value) {
......@@ -114,12 +139,4 @@ public class RADIUSAttribute {
return this;
}
public byte[] serialize() {
final byte[] data = new byte[this.length];
final ByteBuffer bb = ByteBuffer.wrap(data);
bb.put(this.type);
bb.put(this.length);
bb.put(this.value);
return data;
}
}
......