Added support for outgoing MESSAGE-INTEGRITY and FINGERPRINT attribtues

This commit is contained in:
emcho 2010-04-25 14:42:38 +00:00
parent 7e0b166d93
commit d383618b80
7 changed files with 249 additions and 120 deletions

View File

@ -124,6 +124,7 @@ public abstract class Attribute
/**
* Returns the length of this attribute's body.
*
* @return the length of this attribute's value.
*/
public abstract char getDataLength();
@ -132,12 +133,14 @@ public abstract class Attribute
* Returns the human readable name of this attribute. Attribute names do
* not really matter from the protocol point of view. They are only used
* for debugging and readability.
*
* @return this attribute's name.
*/
public abstract String getName();
/**
* Returns the attribute's type.
*
* @return the type of this attribute.
*/
public char getAttributeType()
@ -147,6 +150,7 @@ public abstract class Attribute
/**
* Sets the attribute's type.
*
* @param type the new type of this attribute
*/
protected void setAttributeType(char type)
@ -159,6 +163,7 @@ public abstract class Attribute
* have the same type length and value.
*
* @param obj the object to compare this attribute with.
*
* @return true if the attributes are equal and false otherwise.
*/
@ -166,6 +171,7 @@ public abstract class Attribute
/**
* Returns a binary representation of this attribute.
*
* @return a binary representation of this attribute.
*/
public abstract byte[] encode();
@ -174,14 +180,14 @@ public abstract class Attribute
* Sets this attribute's fields according to attributeValue array.
*
* @param attributeValue a binary array containing this attribute's field
* values and NOT containing the attribute header.
* 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)
* offset is equal to the index of the first byte after length)
* @param length the length of the binary array.
*
* @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;
}

View File

@ -34,7 +34,7 @@ public class AttributeDecoder
{
if(bytes == null || bytes.length < Attribute.HEADER_LENGTH)
throw new StunException(StunException.ILLEGAL_ARGUMENT,
"Could not decode the specified binary array.");
"Could not decode the specified binary array.");
//Discover attribute type
char attributeType = (char)((bytes[offset]<<8)|bytes[offset + 1]);
@ -47,8 +47,8 @@ public class AttributeDecoder
if(attributeLength > bytes.length - offset )
throw new StunException(StunException.ILLEGAL_ARGUMENT,
"The indicated attribute length ("+attributeLength+") "
+"does not match the length of the passed binary array");
"The indicated attribute length ("+attributeLength+") "
+"does not match the length of the passed binary array");
Attribute decodedAttribute = null;
@ -64,8 +64,7 @@ public class AttributeDecoder
case Attribute.ERROR_CODE:
decodedAttribute = new ErrorCodeAttribute(); break;
case Attribute.MESSAGE_INTEGRITY:
throw new UnsupportedOperationException(
"The MESSAGE-INTEGRITY Attribute is not yet implemented.");
decodedAttribute = new MessageIntegrityAttribute(); break;
case Attribute.PASSWORD:
throw new UnsupportedOperationException(
"The PASSWORD Attribute is not yet implemented.");
@ -113,13 +112,16 @@ public class AttributeDecoder
decodedAttribute = new ReservationTokenAttribute(); break;
//According to rfc3489 we should silently ignore unknown attributes.
default: decodedAttribute = new OptionalAttribute(Attribute.UNKNOWN_OPTIONAL_ATTRIBUTE);
default: decodedAttribute
= new OptionalAttribute(Attribute.UNKNOWN_OPTIONAL_ATTRIBUTE);
break;
}
decodedAttribute.setAttributeType(attributeType);
decodedAttribute.decodeAttributeBody(bytes, (char)(Attribute.HEADER_LENGTH + offset), attributeLength);
decodedAttribute.decodeAttributeBody(bytes,
(char)(Attribute.HEADER_LENGTH + offset),
attributeLength);
return decodedAttribute;
}

View File

@ -18,13 +18,6 @@ import org.ice4j.*;
*/
public class AttributeFactory
{
private AttributeFactory()
{
}
// ------------------------------------ CHANGE REQUEST
// --------------------------
/**
* Creates a ChangeRequestAttribute with "false" values for the changeIP and
* changePort flags.
@ -54,9 +47,6 @@ public class AttributeFactory
return attribute;
};
// ------------------------------------ CHANGED ADDRESS
// -------------------------
/**
* Creates a changedAddressAttribute of the specified type and with the
* specified address and port
@ -202,8 +192,6 @@ public class AttributeFactory
};
// ------------------------------------ RESPONSE ADRESS
// -------------------------
/**
* Creates a ResponseFromAddressAttribute of the specified type and with the
* specified address and port
@ -222,8 +210,6 @@ public class AttributeFactory
};
// ------------------------------------ SOURCE ADDRESS
// --------------------------
/**
* Creates a SourceFromAddressAttribute of the specified type and with the
* specified address and port
@ -242,8 +228,6 @@ public class AttributeFactory
};
// ------------------------------------ UNKNOWN ATTRIBUTES
// ----------------------
/**
* Creates an empty UnknownAttributesAttribute.
*
@ -256,10 +240,6 @@ public class AttributeFactory
return attribute;
};
/*
* ------------------------------------ XOR-RELAYED-ADRESS attribute
* ------------------------
*/
/**
* Creates a XorRelayedAddressAttribute of the specified type and with the
* specified address and port.
@ -280,10 +260,6 @@ public class AttributeFactory
return attribute;
}
/*
* ------------------------------------ XOR-PEER-ADRESS attribute
* ------------------------
*/
/**
* Creates a XorPeerAddressAttribute of the specified type and with the
* specified address and port
@ -303,8 +279,6 @@ public class AttributeFactory
return attribute;
}
// ------------------------------------ XOR MAPPED ADDRESS
// --------------------------
/**
* Creates a XorMappedAddressAttribute for the specified <tt>address</tt>.
*
@ -312,7 +286,7 @@ public class AttributeFactory
* @param tranID the ID of the transaction that we will be using for the XOR
* mask.
*
* @return the newly created xor address attribute.
* @return the newly created XOR address attribute.
*/
public static XorMappedAddressAttribute createXorMappedAddressAttribute(
TransportAddress address, byte[] tranID)
@ -325,14 +299,11 @@ public class AttributeFactory
};
/*
* --------------------------------- USERNAME attribute
* -----------------------------
*/
/**
* Create a UsernameAttribute.
*
* @param username username value
*
* @return newly created UsernameAttribute
*/
public static UsernameAttribute createUsernameAttribute(byte username[])
@ -343,10 +314,58 @@ public class AttributeFactory
return attribute;
}
/*
* --------------------------------- CHANNEL-NUMBER attribute
* -----------------------------
/**
* Create a UsernameAttribute.
*
* @param username the String value of the username
*
* @return newly created UsernameAttribute
*/
public static UsernameAttribute createUsernameAttribute(String username)
{
UsernameAttribute attribute = new UsernameAttribute();
attribute.setUsername(username.getBytes());
return attribute;
}
/**
* Creates an empty <tt>MessageIntegrityAttribute</tt>. When included in a
* message the stack would set the body of this attribute so that the
* the HMAC-SHA1 (RFC 2104) would correspond to the actual message that's
* transporting the attribute.
*
* @param key the key that we should use when creating the HMAC-SHA1.
*
* @return the newly created address attribute.
*/
public static MessageIntegrityAttribute createMessageIntegrityAttribute(
byte[] key)
{
MessageIntegrityAttribute attribute = new MessageIntegrityAttribute();
attribute.setKey(key);
//todo: implement saslprep one day even though I don't think it's
//necessary for the ICE use case.
return attribute;
};
/**
* Creates an empty <tt>FingerprintAttribute</tt> with a 0 check sum. Once
* included in a message, the value of this attribute will be calculated
* by the stack just before sending it.
*
* @return the newly created <tt>FingerprintAttribute</tt>.
*/
public static FingerprintAttribute createFingerprintAttribute()
{
FingerprintAttribute attribute = new FingerprintAttribute();
return attribute;
};
/**
* Create a ChannelNumberAttribute.
*
@ -362,10 +381,6 @@ public class AttributeFactory
return attribute;
}
/*
* --------------------------------- REALM attribute
* -----------------------------
*/
/**
* Create a RealmAttribute.
*
@ -380,10 +395,6 @@ public class AttributeFactory
return attribute;
}
/*
* --------------------------------- NONCE attribute
* -----------------------------
*/
/**
* Create a NonceAttribute.
*
@ -398,10 +409,6 @@ public class AttributeFactory
return attribute;
}
/*
* --------------------------------- SOFTWARE attribute
* -----------------------------
*/
/**
* Create a SoftwareAttribute.
*
@ -416,7 +423,6 @@ public class AttributeFactory
return attribute;
}
/* --------------------------------- EVEN-PORT attribute ---------------- */
/**
* Create a EventAttribute.
*
@ -431,10 +437,6 @@ public class AttributeFactory
return attribute;
}
/*
* --------------------------------- LIFETIME attribute
* ---------------------------
*/
/**
* Create a LifetimeAttribute.
*
@ -449,11 +451,6 @@ public class AttributeFactory
return attribute;
}
/*
* --------------------------------- REQUESTED-TRANSPORT attribute
* ----------------
*/
/**
* Create a RequestedTransportAttribute.
*
@ -469,10 +466,6 @@ public class AttributeFactory
return attribute;
}
/*
* -------------------------------- RESERVATION-TOKEN attribute
* --------------------
*/
/**
* Create a ReservationTokenAttribute.
*
@ -488,10 +481,6 @@ public class AttributeFactory
return attribute;
}
/*
* -------------------------------- DATA attribute
* ---------------------------------
*/
/**
* Create a DataAtttribute.
*
@ -506,8 +495,6 @@ public class AttributeFactory
return attribute;
}
// -------------------------------- ICE-CONTROLLED ATTRIBUTE
// -----------------------
/**
* Creates an IceControlledAttribute object with the specified tie-breaker
* value
@ -538,9 +525,6 @@ public class AttributeFactory
return attribute;
}
// ---------------------------------- PRIORITY ATTRIBUTE
// -----------------------
/**
* Creates a Priority attribute with the specified priority value
*

View File

@ -6,15 +6,42 @@
*/
package org.ice4j.attribute;
import java.util.zip.*;
import org.ice4j.*;
/**
* The FINGERPRINT attribute is used to distinguish STUN packets
* from packets of other protocols.
* The FINGERPRINT attribute is used to distinguish STUN packets from packets
* of other protocols. It MAY be present in all STUN messages. The
* value of the attribute is computed as the CRC-32 of the STUN message
* up to (but excluding) the FINGERPRINT attribute itself, XOR'ed with
* the 32-bit value 0x5354554e (the XOR helps in cases where an
* application packet is also using CRC-32 in it). The 32-bit CRC is
* the one defined in ITU V.42 [ITU.V42.2002], which has a generator
* polynomial of x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1.
* When present, the FINGERPRINT attribute MUST be the last attribute in
* the message, and thus will appear after MESSAGE-INTEGRITY.
* <p>
* The FINGERPRINT attribute can aid in distinguishing STUN packets from
* packets of other protocols. See Section 8.
* <p>
* As with MESSAGE-INTEGRITY, the CRC used in the FINGERPRINT attribute
* covers the length field from the STUN message header. Therefore,
* this value must be correct and include the CRC attribute as part of
* the message length, prior to computation of the CRC. When using the
* FINGERPRINT attribute in a message, the attribute is first placed
* into the message with a dummy value, then the CRC is computed, and
* then the value of the attribute is updated. If the MESSAGE-INTEGRITY
* attribute is also present, then it must be present with the correct
* message-integrity value before the CRC is computed, since the CRC is
* done over the value of the MESSAGE-INTEGRITY attribute as well.
*
* @author Sebastien Vincent
* @author Emil Ivov
*/
public class FingerprintAttribute extends Attribute
public class FingerprintAttribute
extends Attribute
implements ContentDependentAttribute
{
/**
* Attribute name.
@ -24,10 +51,17 @@ public class FingerprintAttribute extends Attribute
/**
* CRC value.
*/
private int crc = 0;
private long crc = 0;
/**
* Constructor.
* 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).
*/
public static final int XOR_MASK = 0x5354554e;
/**
* Creates a <tt>FingerPrintAttribute</tt> instance.
*/
FingerprintAttribute()
{
@ -36,6 +70,7 @@ public class FingerprintAttribute extends Attribute
/**
* Returns the length of this attribute's body.
*
* @return the length of this attribute's value.
*/
public char getDataLength()
@ -45,6 +80,7 @@ public class FingerprintAttribute extends Attribute
/**
* Returns the human readable name of this attribute.
*
* @return this attribute's name.
*/
public String getName()
@ -53,9 +89,11 @@ public class FingerprintAttribute extends 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.
*
* @return true if the attributes are equal and false otherwise.
*/
public boolean equals(Object obj)
@ -70,17 +108,20 @@ public class FingerprintAttribute extends Attribute
if (att.getAttributeType() != getAttributeType()
|| att.getDataLength() != getDataLength()
|| att.crc == crc)
{
return false;
}
return true;
}
/**
* Returns a (cloned) byte array containg the data value of the CRC
* Returns a (cloned) byte array containing the data value of the CRC
* attribute.
* @return the binary array containing the CRC.
*
* @return the value of the CRC.
*/
public int getCrc()
public long getCrc()
{
return crc;
}
@ -89,18 +130,43 @@ public class FingerprintAttribute extends Attribute
/**
* Copies the specified binary array into the the CRC value of the CRC
* attribute.
* @param crc the binary array containing the CRC.
*
* @param crc the CRC value.
*/
public void setCrc(int crc)
public void setCrc(long crc)
{
this.crc = crc;
}
/**
* Returns a binary representation of this attribute.
* @return a binary representation of this attribute.
*
* @return nothing
* @throws UnsupportedOperationException since {@link
* ContentDependentAttribute}s should be encoded through the content
* dependent encode method.
*/
public byte[] encode()
throws UnsupportedOperationException
{
throw new UnsupportedOperationException(
"ContentDependentAttributes should be encoded "
+"through the contend-dependent encode method");
}
/**
* Returns a binary representation of this attribute.
*
* @param content the content of the message that this attribute will be
* transported in
* @param offset the <tt>content</tt>-related offset where the actual
* content starts.
* @param length the length of the content in the <tt>content</tt> array.
*
* @return a binary representation of this attribute valid for the message
* with the specified <tt>content</tt>.
*/
public byte[] encode(byte[] content, int offset, int length)
{
char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()];
@ -112,34 +178,44 @@ public class FingerprintAttribute extends Attribute
binValue[2] = (byte)(getDataLength()>>8);
binValue[3] = (byte)(getDataLength()&0x00FF);
//calculate the check sum
CRC32 checksum = new CRC32();
checksum.update(content, offset, length);
this.crc = checksum.getValue();
/* CRC */
binValue[4] = (byte)((crc >> 24) & 0xff);
binValue[5] = (byte)((crc >> 16) & 0xff);
binValue[6] = (byte)((crc >> 8) & 0xff);
binValue[7] = (byte)(crc & 0xff);
binValue[6] = (byte)((crc >> 8) & 0xff);
binValue[7] = (byte) (crc & 0xff);
return binValue;
}
/**
* Sets this attribute's fields according to attributeValue array.
*
* @param attributeValue a binary array containing this attribute's field
* values and NOT containing the attribute header.
* 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)
* offset is equal to the index of the first byte after length)
* @param length the length of the binary array.
*
* @throws StunException if attrubteValue contains invalid data.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
throws StunException
{
if(length != 4)
{
throw new StunException("length invalid");
}
crc = ((attributeValue[0] << 24) & 0xff000000) + ((attributeValue[1] << 16) & 0x00ff0000)
+ ((attributeValue[2] << 8) & 0x0000ff00) + (attributeValue[3] & 0x000000ff);
crc = ((attributeValue[0] << 24) & 0xff000000)
+ ((attributeValue[1] << 16) & 0x00ff0000)
+ ((attributeValue[2] << 8) & 0x0000ff00)
+ (attributeValue[3] & 0x000000ff);
}
}

View File

@ -15,6 +15,7 @@ import org.ice4j.*;
* The value of USERNAME is a variable length value.
*
* @author Sebastien Vincent
* @author Emil Ivov
*/
public class UsernameAttribute extends Attribute
{
@ -39,14 +40,14 @@ public class UsernameAttribute extends Attribute
/**
* Copies the value of the username attribute from the specified
* attributeValue.
*
* @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 StunException if attributeValue contains invalid data.
*/
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
{
username = new byte[length];
System.arraycopy(attributeValue, offset, username, 0, length);
@ -54,12 +55,15 @@ public class UsernameAttribute extends Attribute
/**
* Returns a binary representation of this attribute.
*
* @return a binary representation of this attribute.
*/
public byte[] encode()
{
char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + (getDataLength() % 4)];
byte binValue[] = new byte[HEADER_LENGTH + getDataLength()
//add padding
+ (4 - getDataLength()%4)%4];
//Type
binValue[0] = (byte)(type>>8);
@ -69,7 +73,7 @@ public class UsernameAttribute extends Attribute
binValue[2] = (byte)(getDataLength()>>8);
binValue[3] = (byte)(getDataLength()&0x00FF);
/* username */
//username
System.arraycopy(username, 0, binValue, 4, getDataLength());
return binValue;
@ -77,6 +81,7 @@ public class UsernameAttribute extends Attribute
/**
* Returns the length of this attribute's body.
*
* @return the length of this attribute's value.
*/
public char getDataLength()
@ -94,7 +99,7 @@ public class UsernameAttribute extends Attribute
}
/**
* Returns a (cloned) byte array containg the data value of the username
* Returns a (cloned) byte array containing the data value of the username
* attribute.
* @return the binary array containing the username.
*/
@ -111,6 +116,7 @@ public class UsernameAttribute extends Attribute
/**
* Copies the specified binary array into the the data value of the username
* attribute.
*
* @param username the binary array containing the username.
*/
public void setUsername(byte[] username)
@ -126,8 +132,9 @@ public class UsernameAttribute extends 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.
* @return true if the attributes are equal and false otherwise.
*/

View File

@ -318,7 +318,7 @@ public class Agent
* @return that user name that should be advertised in session descriptions
* containing ICE data from this agent.
*/
public String getUserName()
public String getLocalUfrag()
{
return ufrag;
}
@ -342,7 +342,7 @@ public class Agent
* @return the user name that we received from the remote peer or
* <tt>null</tt> if we haven't received a user name from them yet.
*/
public String getRemoteUserName()
public String getRemoteUfrag()
{
return remoteUfrag;
}
@ -364,17 +364,57 @@ public class Agent
*
* @param remoteUfrag the user name that we received from the remote peer.
*/
public void setRemoteUserName(String remoteUfrag)
public void setRemoteUfrag(String remoteUfrag)
{
this.remoteUfrag = remoteUfrag;
}
/**
* Returns the
* Returns the user name that this <tt>Agent</tt> should use in connectivity
* checks for outgoing Binding Requests. According to RFC 5245, a Binding
* Request serving as a connectivity check MUST utilize the STUN short term
* credential mechanism. The username for the credential is formed by
* concatenating the username fragment provided by the peer with the
* username fragment of the agent sending the request, separated by a
* colon (":"). The password is equal to the password provided by the peer.
* For example, consider the case where agent L is the offerer, and agent R
* is the answerer. Agent L included a username fragment of LFRAG for its
* candidates, and a password of LPASS. Agent R provided a username
* fragment of RFRAG and a password of RPASS. A connectivity check from L
* to R (and its response of course) utilize the username RFRAG:LFRAG and a
* password of RPASS. A connectivity check from R to L (and its response)
* utilize the username LFRAG:RFRAG and a password of LPASS.
*
* @return a user name that this <tt>Agent</tt> can use in connectivity
* check for outgoing Binding Requests.
*/
public String generateUserName()
public String generateLocalUserName()
{
return getUserName();
return getRemoteUfrag() + ":" + getLocalUfrag();
}
/**
* Returns the user name that we should respect a peer <tt>Agent</tt> to use
* in connectivity checks for outgoing Binding Requests. According to RFC
* 5245, a Binding Request serving as a connectivity check MUST utilize the
* STUN short term credential mechanism. The username for the credential is
* formed by concatenating the username fragment provided by the peer with
* the username fragment of the agent sending the request, separated by a
* colon (":"). The password is equal to the password provided by the peer.
* For example, consider the case where agent L is the offerer, and agent R
* is the answerer. Agent L included a username fragment of LFRAG for its
* candidates, and a password of LPASS. Agent R provided a username
* fragment of RFRAG and a password of RPASS. A connectivity check from L
* to R (and its response of course) utilize the username RFRAG:LFRAG and a
* password of RPASS. A connectivity check from R to L (and its response)
* utilize the username LFRAG:RFRAG and a password of LPASS.
*
* @return a user name that a peer <tt>Agent</tt> would use in connectivity
* check for outgoing Binding Requests.
*/
public String generateRemoteUserName()
{
return getLocalUfrag() + ":" + getRemoteUfrag();
}
/**
@ -466,7 +506,7 @@ public class Agent
buff.append(getStreamCount()).append(" ice-pwd:").append(getPassword());
buff.append(getStreamCount()).append(" ice-ufrag:")
.append(getUserName());
.append(getLocalUfrag());
buff.append(getStreamCount()).append(" tie-breaker:" + getTieBreaker());
buff.append("):\n");

View File

@ -238,7 +238,7 @@ public abstract class Message
};
/**
* Creates an empty STUN Mesage.
* Creates an empty STUN Message.
*/
protected Message()
{
@ -391,6 +391,9 @@ public abstract class Message
*/
protected byte getAttributePresentity(char attributeType)
{
if(!rfc3489CompatibilityMode)
return O;
byte msgIndex = -1;
byte attributeIndex = -1;
@ -529,11 +532,22 @@ public abstract class Message
= attributes.entrySet().iterator();
while (iter.hasNext())
{
Attribute attribute = iter.next().getValue();
byte[] attBinValue = attribute.encode();
System.arraycopy(attBinValue, 0, binMsg, offset, attBinValue.length);
//special handling for message integrity and fingerprint values
byte[] attBinValue;
if (attribute instanceof ContentDependentAttribute)
{
attBinValue = ((ContentDependentAttribute)attribute)
.encode(binMsg, 0, offset);
}
else
{
attBinValue = attribute.encode();
}
System.arraycopy(
attBinValue, 0, binMsg, offset, attBinValue.length);
offset += attBinValue.length;
}