Improves validation of incoming FINGERPRINT and MESSAGE-INTEGRITY attributes

This commit is contained in:
emcho 2010-04-26 21:33:50 +00:00
parent 9bc3eebfee
commit 7485d05891
2 changed files with 78 additions and 13 deletions

View File

@ -55,6 +55,12 @@ public class FingerprintAttribute
*/
public static final byte[] XOR_MASK = { 0x53, 0x54, 0x55, 0x4e};
/**
* The CRC32 checksum that this attribute is carrying. Only used in incoming
* messages.
*/
private byte[] crc;
/**
* Creates a <tt>FingerPrintAttribute</tt> instance.
@ -64,6 +70,17 @@ public class FingerprintAttribute
super(FINGERPRINT);
}
/**
* Returns the CRC32 checksum that this attribute is carrying. Only makes
* sense for incoming messages and hence only set for them.
*
* @return the CRC32 checksum that this attribute is carrying or
* <tt>null</tt> if it has not been set.
*/
public byte[] getChecksum()
{
return crc;
}
/**
* Returns the length of this attribute's body.
*
@ -201,18 +218,7 @@ public class FingerprintAttribute
incomingCrcBytes[2] = attributeValue[offset + 2];
incomingCrcBytes[3] = attributeValue[offset + 3];
//now check whether the CRC really is what it's supposed to be.
//re calculate the check sum
byte[] realCrcBytes = calculateXorCRC32(messageHead, mhOffset, mhLen);
//CRC validation.
if ( ! Arrays.equals(incomingCrcBytes, realCrcBytes))
throw new StunException(StunException.ILLEGAL_ARGUMENT,
"An incoming message arrived with a wrong FINGERPRINT "
+"attribute value. "
+"CRC Was:" + Arrays.toString(incomingCrcBytes)
+ ". Should have been:" + Arrays.toString(realCrcBytes)
+". Will ignore.");
this.crc = incomingCrcBytes;
}
/**
@ -227,7 +233,7 @@ public class FingerprintAttribute
* @return the CRC value that should be sent in a <tt>FINGERPRINT</tt>
* attribute traveling in the <tt>message</tt> message.
*/
private static byte[] calculateXorCRC32(byte[] message, int offset, int len)
public static byte[] calculateXorCRC32(byte[] message, int offset, int len)
{
//now check whether the CRC really is what it's supposed to be.
//re calculate the check sum

View File

@ -678,6 +678,7 @@ public abstract class Message
binMessage, (char)initialOffset,
(char)(offset - initialOffset)); // message head
//assert Message Integrity
if (att instanceof MessageIntegrityAttribute)
{
if (!message.validateMessageIntegrity(
@ -690,12 +691,70 @@ public abstract class Message
}
//check finger print CRC
if (att instanceof FingerprintAttribute)
{
if (!validateFingerprint((FingerprintAttribute)att,
binMessage, originalOffset, offset))
{
throw new StunException(StunException.ILLEGAL_ARGUMENT,
"Wrong value in FINGERPRINT");
}
}
message.addAttribute(att);
offset += att.getDataLength() + Attribute.HEADER_LENGTH;
}
return message;
}
/**
* Recalculates the fingerprint CRC32 checksum of the <tt>message</tt>
* array so that we could compare it with the value brought by the
* {@link FingerprintAttribute}.
*
* @param fingerprint the attribute that we need to validate.
* @param message the message whose CRC32 checksum we'd need to recalculate.
* @param offset the index in <tt>message</tt> where data starts.
* @param length the number of bytes in <tt>message</tt> that the CRC32
* would need to be calculated over.
*
* @return <tt>true</tt> if <tt>fingerprint</tt> contains a valid CRC32
* value and <tt>false</tt> otherwise.
*/
public static boolean validateFingerprint(FingerprintAttribute fingerprint,
byte[] message,
int offset,
int length)
{
byte[] incomingCrcBytes = fingerprint.getChecksum();
//now check whether the CRC really is what it's supposed to be.
//re calculate the check sum
byte[] realCrcBytes = FingerprintAttribute.calculateXorCRC32(
message, offset, length);
//CRC validation.
if ( ! Arrays.equals(incomingCrcBytes, realCrcBytes))
{
if (logger.isLoggable(Level.FINE))
{
logger.fine(
"An incoming message arrived with a wrong FINGERPRINT "
+"attribute value. "
+"CRC Was:" + Arrays.toString(incomingCrcBytes)
+ ". Should have been:" + Arrays.toString(realCrcBytes)
+". Will ignore.");
}
return false;
}
return true;
}
/**
* Recalculates the HMAC-SHA1 signature of the <tt>message</tt> array so
* that we could compare it with the value brought by the