Added support for validation of incoming FINGERPRINT attribtues
This commit is contained in:
parent
d383618b80
commit
96864b9489
@ -159,8 +159,8 @@ public abstract class Attribute
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares two STUN Attributes. Two attributes are considered equal when they
|
* Compares two STUN Attributes. Two attributes are considered equal when
|
||||||
* have the same type length and value.
|
* they have the same type length and value.
|
||||||
*
|
*
|
||||||
* @param obj the object to compare this attribute with.
|
* @param obj the object to compare this attribute with.
|
||||||
*
|
*
|
||||||
@ -187,7 +187,8 @@ public abstract class Attribute
|
|||||||
*
|
*
|
||||||
* @throws StunException if attrubteValue contains invalid data.
|
* @throws StunException if attrubteValue contains invalid data.
|
||||||
*/
|
*/
|
||||||
abstract void decodeAttributeBody(
|
abstract void decodeAttributeBody( byte[] attributeValue,
|
||||||
byte[] attributeValue, char offset, char length)
|
char offset,
|
||||||
|
char length)
|
||||||
throws StunException;
|
throws StunException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,16 +20,29 @@ public class AttributeDecoder
|
|||||||
/**
|
/**
|
||||||
* Decodes the specified binary array and returns the corresponding
|
* Decodes the specified binary array and returns the corresponding
|
||||||
* attribute object.
|
* attribute object.
|
||||||
|
*
|
||||||
* @param bytes the binary array that should be decoded.
|
* @param bytes the binary array that should be decoded.
|
||||||
* @param offset the index where the message starts.
|
* @param offset the index where the message starts.
|
||||||
* @param length the number of bytes that the message is long.
|
* @param length the number of bytes that the message is long.
|
||||||
|
* @param messageHeadBytes the binary array that contains the all the bytes
|
||||||
|
* of the message that brought this attribute up until the beginning of the
|
||||||
|
* attribute (at least).
|
||||||
|
* @param messageHeadOffset the start of the container message in the
|
||||||
|
* <tt>messageHeadBytes</tt> array.
|
||||||
|
* @param messageHeadLen the length of the message head in the
|
||||||
|
* <tt>messageHeadBytes</tt> array, up until the beginning of the attribute.
|
||||||
|
*
|
||||||
* @return An object representing the attribute encoded in bytes or null if
|
* @return An object representing the attribute encoded in bytes or null if
|
||||||
* the attribute was not recognized.
|
* the attribute was not recognized.
|
||||||
|
*
|
||||||
* @throws StunException if bytes does is not a valid STUN attribute.
|
* @throws StunException if bytes does is not a valid STUN attribute.
|
||||||
*/
|
*/
|
||||||
public static Attribute decode(byte bytes[],
|
public static Attribute decode(byte[] bytes,
|
||||||
char offset,
|
char offset,
|
||||||
char length)
|
char length,
|
||||||
|
byte[] messageHeadBytes,
|
||||||
|
char messageHeadOffset,
|
||||||
|
char messageHeadLen)
|
||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
if(bytes == null || bytes.length < Attribute.HEADER_LENGTH)
|
if(bytes == null || bytes.length < Attribute.HEADER_LENGTH)
|
||||||
@ -119,9 +132,19 @@ public class AttributeDecoder
|
|||||||
|
|
||||||
decodedAttribute.setAttributeType(attributeType);
|
decodedAttribute.setAttributeType(attributeType);
|
||||||
|
|
||||||
decodedAttribute.decodeAttributeBody(bytes,
|
if (decodedAttribute instanceof ContentDependentAttribute)
|
||||||
|
{
|
||||||
|
((ContentDependentAttribute)decodedAttribute).decodeAttributeBody(
|
||||||
|
bytes, (char)(Attribute.HEADER_LENGTH + offset),
|
||||||
|
attributeLength,
|
||||||
|
messageHeadBytes, messageHeadOffset, messageHeadLen );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
decodedAttribute.decodeAttributeBody(bytes,
|
||||||
(char)(Attribute.HEADER_LENGTH + offset),
|
(char)(Attribute.HEADER_LENGTH + offset),
|
||||||
attributeLength);
|
attributeLength);
|
||||||
|
}
|
||||||
|
|
||||||
return decodedAttribute;
|
return decodedAttribute;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -194,17 +194,30 @@ public class FingerprintAttribute
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets this attribute's fields according to attributeValue array.
|
* Sets this attribute's fields according to the message and attributeValue
|
||||||
|
* arrays. This method allows the stack to validate the content of content
|
||||||
|
* dependent attributes such as the {@link MessageIntegrityAttribute} or
|
||||||
|
* the {@link FingerprintAttribute} and hide invalid ones from the
|
||||||
|
* application.
|
||||||
*
|
*
|
||||||
* @param attributeValue a binary array containing this attribute's field
|
* @param attributeValue a binary array containing this attribute's field
|
||||||
* values and NOT containing the attribute header.
|
* values and NOT containing the attribute header.
|
||||||
* @param offset the position where attribute values begin (most often
|
* @param offset the position where attribute values begin (most often
|
||||||
* offset is equal to the index of the first byte after length)
|
* offset is equal to the index of the first byte after length)
|
||||||
* @param length the length of the binary array.
|
* @param length the length of the binary array.
|
||||||
|
* @param messageHead the bytes of the message that brought this attribute.
|
||||||
|
* @param mhOffset the start of the message that brought this attribute
|
||||||
|
* @param mhLen the length of the message in the messageHead param up until
|
||||||
|
* the start of this attribute.
|
||||||
*
|
*
|
||||||
* @throws StunException if attrubteValue contains invalid data.
|
* @throws StunException if attrubteValue contains invalid data.
|
||||||
*/
|
*/
|
||||||
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
|
public void decodeAttributeBody( byte[] attributeValue,
|
||||||
|
char offset,
|
||||||
|
char length,
|
||||||
|
byte[] messageHead,
|
||||||
|
char mhOffset,
|
||||||
|
char mhLen)
|
||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
if(length != 4)
|
if(length != 4)
|
||||||
@ -216,6 +229,40 @@ public class FingerprintAttribute
|
|||||||
+ ((attributeValue[1] << 16) & 0x00ff0000)
|
+ ((attributeValue[1] << 16) & 0x00ff0000)
|
||||||
+ ((attributeValue[2] << 8) & 0x0000ff00)
|
+ ((attributeValue[2] << 8) & 0x0000ff00)
|
||||||
+ (attributeValue[3] & 0x000000ff);
|
+ (attributeValue[3] & 0x000000ff);
|
||||||
|
|
||||||
|
//now check whether the CRC really is what it's supposed to be.
|
||||||
|
//re calculate the check sum
|
||||||
|
CRC32 checksum = new CRC32();
|
||||||
|
checksum.update(messageHead, mhOffset, mhLen);
|
||||||
|
|
||||||
|
long realChecksum = checksum.getValue();
|
||||||
|
|
||||||
|
//CRC failure.
|
||||||
|
if (realChecksum != crc)
|
||||||
|
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||||
|
"An incoming message arrived with a wrong FINGERPRINT "
|
||||||
|
+"attribute value. Will ignore.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Always throws an {@link UnsupportedOperationException} since this
|
||||||
|
* attribute should be decoded through the content specific decode method.
|
||||||
|
*
|
||||||
|
* @param attributeValue a binary array containing this attribute's
|
||||||
|
* field values and NOT containing the attribute header.
|
||||||
|
* @param offset the position where attribute values begin (most often
|
||||||
|
* offset is equal to the index of the first byte after length)
|
||||||
|
* @param length the length of the binary array.
|
||||||
|
*
|
||||||
|
* @throws UnsupportedOperationException since we are supposed to decode
|
||||||
|
* through the content specific decode method.
|
||||||
|
*/
|
||||||
|
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
|
||||||
|
throws UnsupportedOperationException
|
||||||
|
{
|
||||||
|
throw new UnsupportedOperationException(
|
||||||
|
"ContentDependentAttributes should be decoded "
|
||||||
|
+"through the contend-dependent decode method");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -567,6 +567,8 @@ public abstract class Message
|
|||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
arrayLen = (char)Math.min(binMessage.length, arrayLen);
|
arrayLen = (char)Math.min(binMessage.length, arrayLen);
|
||||||
|
char initialOffset = offset;
|
||||||
|
|
||||||
|
|
||||||
if(binMessage == null || arrayLen - offset < Message.HEADER_LENGTH)
|
if(binMessage == null || arrayLen - offset < Message.HEADER_LENGTH)
|
||||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||||
@ -608,9 +610,10 @@ public abstract class Message
|
|||||||
|
|
||||||
while(offset - Message.HEADER_LENGTH< length)
|
while(offset - Message.HEADER_LENGTH< length)
|
||||||
{
|
{
|
||||||
Attribute att = AttributeDecoder.decode(binMessage,
|
Attribute att = AttributeDecoder.decode(
|
||||||
offset,
|
binMessage, offset, (char)(length - offset), //attribute
|
||||||
(char)(length - offset));
|
binMessage, (char)initialOffset,
|
||||||
|
(char)(offset - initialOffset)); // message head
|
||||||
message.addAttribute(att);
|
message.addAttribute(att);
|
||||||
offset += att.getDataLength() + Attribute.HEADER_LENGTH;
|
offset += att.getDataLength() + Attribute.HEADER_LENGTH;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user