diff --git a/src/org/ice4j/attribute/FingerprintAttribute.java b/src/org/ice4j/attribute/FingerprintAttribute.java index ac65100..5abdb2a 100644 --- a/src/org/ice4j/attribute/FingerprintAttribute.java +++ b/src/org/ice4j/attribute/FingerprintAttribute.java @@ -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 FingerPrintAttribute 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 + * null 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 FINGERPRINT * attribute traveling in the message 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 diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index bb3d5c8..e147822 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -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 message + * 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 message where data starts. + * @param length the number of bytes in message that the CRC32 + * would need to be calculated over. + * + * @return true if fingerprint contains a valid CRC32 + * value and false 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 message array so * that we could compare it with the value brought by the