diff --git a/src/org/ice4j/attribute/Attribute.java b/src/org/ice4j/attribute/Attribute.java index e656972..6a63cfb 100644 --- a/src/org/ice4j/attribute/Attribute.java +++ b/src/org/ice4j/attribute/Attribute.java @@ -159,8 +159,8 @@ public abstract class Attribute } /** - * Compares two STUN Attributes. Two attributes are considered equal when they - * have the same type length and value. + * Compares two STUN Attributes. Two attributes are considered equal when + * they have the same type length and value. * * @param obj the object to compare this attribute with. * @@ -187,7 +187,8 @@ public abstract class Attribute * * @throws StunException if attrubteValue contains invalid data. */ - abstract void decodeAttributeBody( - byte[] attributeValue, char offset, char length) + abstract void decodeAttributeBody( byte[] attributeValue, + char offset, + char length) throws StunException; } diff --git a/src/org/ice4j/attribute/AttributeDecoder.java b/src/org/ice4j/attribute/AttributeDecoder.java index 89ecd0b..96b341b 100644 --- a/src/org/ice4j/attribute/AttributeDecoder.java +++ b/src/org/ice4j/attribute/AttributeDecoder.java @@ -20,16 +20,29 @@ public class AttributeDecoder /** * Decodes the specified binary array and returns the corresponding * attribute object. + * * @param bytes the binary array that should be decoded. * @param offset the index where the message starts. * @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 + * messageHeadBytes array. + * @param messageHeadLen the length of the message head in the + * messageHeadBytes array, up until the beginning of the attribute. + * * @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. */ - public static Attribute decode(byte bytes[], + public static Attribute decode(byte[] bytes, char offset, - char length) + char length, + byte[] messageHeadBytes, + char messageHeadOffset, + char messageHeadLen) throws StunException { if(bytes == null || bytes.length < Attribute.HEADER_LENGTH) @@ -119,9 +132,19 @@ public class AttributeDecoder 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), attributeLength); + } return decodedAttribute; } diff --git a/src/org/ice4j/attribute/FingerprintAttribute.java b/src/org/ice4j/attribute/FingerprintAttribute.java index 84895f7..94ebb51 100644 --- a/src/org/ice4j/attribute/FingerprintAttribute.java +++ b/src/org/ice4j/attribute/FingerprintAttribute.java @@ -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 * 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. + * @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. */ - 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 { if(length != 4) @@ -216,6 +229,40 @@ public class FingerprintAttribute + ((attributeValue[1] << 16) & 0x00ff0000) + ((attributeValue[2] << 8) & 0x0000ff00) + (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"); } } diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index 8edbebd..d55e08b 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -567,6 +567,8 @@ public abstract class Message throws StunException { arrayLen = (char)Math.min(binMessage.length, arrayLen); + char initialOffset = offset; + if(binMessage == null || arrayLen - offset < Message.HEADER_LENGTH) throw new StunException(StunException.ILLEGAL_ARGUMENT, @@ -608,9 +610,10 @@ public abstract class Message while(offset - Message.HEADER_LENGTH< length) { - Attribute att = AttributeDecoder.decode(binMessage, - offset, - (char)(length - offset)); + Attribute att = AttributeDecoder.decode( + binMessage, offset, (char)(length - offset), //attribute + binMessage, (char)initialOffset, + (char)(offset - initialOffset)); // message head message.addAttribute(att); offset += att.getDataLength() + Attribute.HEADER_LENGTH; }