Added support for validation of incoming FINGERPRINT attribtues. (and this time it works)

This commit is contained in:
emcho 2010-04-25 14:43:43 +00:00
parent 06caa8ae0b
commit 4f61eabce2

View File

@ -49,17 +49,12 @@ public class FingerprintAttribute
*/ */
public static final String NAME = "FINGERPRINT"; public static final String NAME = "FINGERPRINT";
/**
* CRC value.
*/
private long crc = 0;
/** /**
* The value that we need to XOR the CRC with. The XOR helps in cases where * The value that we need to XOR the CRC with. The XOR helps in cases where
* an application packet is also using CRC-32 in it). * an application packet is also using CRC-32 in it).
*/ */
public static final int XOR_MASK = 0x5354554e; public static final byte[] XOR_MASK = { 0x53, 0x54, 0x55, 0x4e};
/** /**
* Creates a <tt>FingerPrintAttribute</tt> instance. * Creates a <tt>FingerPrintAttribute</tt> instance.
@ -107,8 +102,7 @@ public class FingerprintAttribute
FingerprintAttribute att = (FingerprintAttribute) obj; FingerprintAttribute att = (FingerprintAttribute) obj;
if (att.getAttributeType() != getAttributeType() if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength() || att.getDataLength() != getDataLength())
|| att.crc == crc)
{ {
return false; return false;
} }
@ -116,29 +110,6 @@ public class FingerprintAttribute
return true; return true;
} }
/**
* Returns a (cloned) byte array containing the data value of the CRC
* attribute.
*
* @return the value of the CRC.
*/
public long getCrc()
{
return crc;
}
/**
* Copies the specified binary array into the the CRC value of the CRC
* attribute.
*
* @param crc the CRC value.
*/
public void setCrc(long crc)
{
this.crc = crc;
}
/** /**
* Returns a binary representation of this attribute. * Returns a binary representation of this attribute.
* *
@ -180,16 +151,13 @@ public class FingerprintAttribute
binValue[3] = (byte)(getDataLength()&0x00FF); binValue[3] = (byte)(getDataLength()&0x00FF);
//calculate the check sum //calculate the check sum
CRC32 checksum = new CRC32(); byte[] xorCrc32 = calculateXorCRC32(content, offset, length);
checksum.update(content, offset, length);
this.crc = checksum.getValue(); //copy into the attribute;
binValue[4] = xorCrc32[0];
/* CRC */ binValue[5] = xorCrc32[1];
binValue[4] = (byte)((crc >> 24) & 0xff); binValue[6] = xorCrc32[2];
binValue[5] = (byte)((crc >> 16) & 0xff); binValue[7] = xorCrc32[3];
binValue[6] = (byte)((crc >> 8) & 0xff);
binValue[7] = (byte) (crc & 0xff);
return binValue; return binValue;
} }
@ -226,31 +194,72 @@ public class FingerprintAttribute
throw new StunException("length invalid"); throw new StunException("length invalid");
} }
crc = ((attributeValue[0] << 24) & 0xff000000) byte[] incomingCrcBytes = new byte[4];
+ ((attributeValue[1] << 16) & 0x00ff0000)
+ ((attributeValue[2] << 8) & 0x0000ff00) incomingCrcBytes[0] = attributeValue[offset];
+ (attributeValue[3] & 0x000000ff); incomingCrcBytes[1] = attributeValue[offset + 1];
incomingCrcBytes[2] = attributeValue[offset + 2];
incomingCrcBytes[3] = attributeValue[offset + 3];
//now check whether the CRC really is what it's supposed to be. //now check whether the CRC really is what it's supposed to be.
//re calculate the check sum //re calculate the check sum
CRC32 checksum = new CRC32(); byte[] realCrcBytes = calculateXorCRC32(messageHead, mhOffset, mhLen);
checksum.update(messageHead, mhOffset, mhLen);
//CRC failure. //CRC validation.
/*this currently doesn't work for some reason so we'll let everyone pass if ( ! Arrays.equals(incomingCrcBytes, realCrcBytes))
long realChecksum = checksum.getValue();
if (realChecksum != crc)
throw new StunException(StunException.ILLEGAL_ARGUMENT, throw new StunException(StunException.ILLEGAL_ARGUMENT,
"An incoming message arrived with a wrong FINGERPRINT " "An incoming message arrived with a wrong FINGERPRINT "
+"attribute value. " +"attribute value. "
+"CRC Was: 0x" + Long.toHexString(attributeValue[0]) +"CRC Was:" + Arrays.toString(incomingCrcBytes)
+ Long.toHexString(attributeValue[1]) + ". Should have been:" + Arrays.toString(realCrcBytes)
+ Long.toHexString(attributeValue[2])
+ Long.toHexString(attributeValue[3])
+ ". Should have been:" + Long.toHexString(realChecksum)
+". Will ignore."); +". Will ignore.");
*/ }
/**
* Calculates and returns the CRC32 checksum for <tt>message</tt> after
* applying the <tt>XOR_MASK</tt> specified by RFC 5389.
*
* @param message the message whose checksum we'd like to have
* @param offset the location in <tt>message</tt> where the actual message
* starts.
* @param len the number of message bytes in <tt>message</tt>
*
* @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)
{
//now check whether the CRC really is what it's supposed to be.
//re calculate the check sum
CRC32 checksum = new CRC32();
checksum.update(message, offset, len);
long crc = checksum.getValue();
byte[] xorCRC32 = new byte[4];
xorCRC32[0] = (byte)((byte)((crc >> 24) & 0xff) ^ XOR_MASK[0]);
xorCRC32[1] = (byte)((byte)((crc >> 16) & 0xff) ^ XOR_MASK[1]);
xorCRC32[2] = (byte)((byte)((crc >> 8) & 0xff) ^ XOR_MASK[2]);
xorCRC32[3] = (byte)((byte) (crc & 0xff) ^ XOR_MASK[3]);
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;
} }
/** /**