Improves validation of incoming FINGERPRINT and MESSAGE-INTEGRITY attributes

This commit is contained in:
emcho 2010-04-26 21:34:03 +00:00
parent 7485d05891
commit d6ba672a01
4 changed files with 5 additions and 129 deletions

View File

@ -132,19 +132,9 @@ public class AttributeDecoder
decodedAttribute.setAttributeType(attributeType);
if (decodedAttribute instanceof ContentDependentAttribute)
{
((ContentDependentAttribute)decodedAttribute).decodeAttributeBody(
bytes, (char)(Attribute.HEADER_LENGTH + offset),
attributeLength,
messageHeadBytes, messageHeadOffset, messageHeadLen );
}
else
{
decodedAttribute.decodeAttributeBody(bytes,
decodedAttribute.decodeAttributeBody(bytes,
(char)(Attribute.HEADER_LENGTH + offset),
attributeLength);
}
return decodedAttribute;
}

View File

@ -33,31 +33,4 @@ public interface ContentDependentAttribute
* with the specified <tt>content</tt>.
*/
public byte[] encode(byte[] content, int offset, int length);
/**
* 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.
*/
public void decodeAttributeBody( byte[] attributeValue,
char offset,
char length,
byte[] messageHead,
char mhOffset,
char mhLen)
throws StunException;
}

View File

@ -6,7 +6,6 @@
*/
package org.ice4j.attribute;
import java.util.*;
import java.util.zip.*;
import org.ice4j.*;
@ -181,29 +180,20 @@ public class FingerprintAttribute
/**
* 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.
* arrays.
*
* @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.
*/
public void decodeAttributeBody( byte[] attributeValue,
char offset,
char length,
byte[] messageHead,
char mhOffset,
char mhLen)
char length)
throws StunException
{
if(length != 4)
@ -250,43 +240,5 @@ public class FingerprintAttribute
return xorCRC32;
}
/**
* Creates a <tt>long</tt> CRC value out of the specified <tt>bytes</tt>.
*
* @param bytes the byte array to convert into a <tt>long</tt>.
*
* @return the CRC value contained by the <tt>bytes</tt> array.
*/
private long byteArrayToCRC(byte[] bytes)
{
long crc = ((bytes[0] << 24) & 0xff000000)
+ ((bytes[1] << 16) & 0x00ff0000)
+ ((bytes[2] << 8) & 0x0000ff00)
+ (bytes[3] & 0x000000ff);
return crc;
}
/**
* 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");
}
}

View File

@ -191,62 +191,23 @@ public class MessageIntegrityAttribute
/**
* 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.
* arrays.
*
* @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.
*/
public void decodeAttributeBody( byte[] attributeValue,
char offset,
char length,
byte[] messageHead,
char mhOffset,
char mhLen)
throws StunException
char length)
{
hmacSha1Content = new byte[length];
System.arraycopy(attributeValue, offset, hmacSha1Content, 0, length);
//we can't check the validity of the attribute here since we don't have
//the password
//todo: we should think of a way to access passwords from here.
}
/**
* 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");
}
/**
* Returns a binary representation of this attribute.
*