public class MessageIntegrityAttribute extends StunAttribute implements ContextDependentAttribute
key = MD5(username ":" realm ":" SASLprep(password))
That is, the 16-byte key is formed by taking the MD5 hash of the result of
concatenating the following five fields: (1) the username, with any quotes
and trailing nulls removed, as taken from the USERNAME attribute (in which
case SASLprep has already been applied); (2) a single colon; (3) the realm,
with any quotes and trailing nulls removed; (4) a single colon; and (5) the
password, with any trailing nulls removed and after processing using
SASLprep. For example, if the username was 'user', the realm was 'realm', and
the password was 'pass', then the 16-byte HMAC key would be the result of
performing an MD5 hash on the string 'user:realm:pass', the resulting hash
being 0x8493fbc53ba582fb4c044c456bdc40eb.
For short-term credentials:
key = SASLprep(password)where MD5 is defined in RFC 1321 [RFC1321] and SASLprep() is defined in RFC 4013 [RFC4013].
The structure of the key when used with long-term credentials facilitates deployment in systems that also utilize SIP. Typically, SIP systems utilizing SIP's digest authentication mechanism do not actually store the password in the database. Rather, they store a value called H(A1), which is equal to the key defined above.
Based on the rules above, the hash used to construct MESSAGE- INTEGRITY includes the length field from the STUN message header. Prior to performing the hash, the MESSAGE-INTEGRITY attribute MUST be inserted into the message (with dummy content). The length MUST then be set to point to the length of the message up to, and including, the MESSAGE-INTEGRITY attribute itself, but excluding any attributes after it. Once the computation is performed, the value of the MESSAGE-INTEGRITY attribute can be filled in, and the value of the length in the STUN header can be set to its correct value -- the length of the entire message. Similarly, when validating the MESSAGE-INTEGRITY, the length field should be adjusted to point to the end of the MESSAGE-INTEGRITY attribute prior to calculating the HMAC. Such adjustment is necessary when attributes, such as FINGERPRINT, appear after MESSAGE-INTEGRITY.
| Modifier and Type | Field and Description |
|---|---|
static char |
DATA_LENGTH |
static String |
HMAC_SHA1_ALGORITHM |
static String |
NAME |
ALTERNATE_SERVER, attributeType, CHANGE_REQUEST, CHANGED_ADDRESS, CHANNEL_NUMBER, DATA, DESTINATION_ADDRESS, DONT_FRAGMENT, ERROR_CODE, EVEN_PORT, FINGERPRINT, HEADER_LENGTH, ICE_CONTROLLED, ICE_CONTROLLING, LIFETIME, MAGIC_COOKIE, MAPPED_ADDRESS, MESSAGE_INTEGRITY, NONCE, PASSWORD, PRIORITY, REALM, REFLECTED_FROM, REMOTE_ADDRESS, REQUESTED_TRANSPORT, RESERVATION_TOKEN, RESPONSE_ADDRESS, SOFTWARE, SOURCE_ADDRESS, UNKNOWN_ATTRIBUTES, UNKNOWN_OPTIONAL_ATTRIBUTE, USE_CANDIDATE, USERNAME, XOR_MAPPED_ADDRESS, XOR_ONLY, XOR_PEER_ADDRESS, XOR_RELAYED_ADDRESS| Constructor and Description |
|---|
MessageIntegrityAttribute() |
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
calculateHmacSha1(byte[] message,
int offset,
int length,
byte[] key)
Encodes message using key and the HMAC-SHA1 algorithm
as per RFC 2104 and returns the resulting byte array.
|
protected void |
decodeAttributeBody(byte[] data,
char offset,
char length)
Sets this attribute's fields according to attributeValue array.
|
byte[] |
encode()
Returns a binary representation of this attribute.
|
byte[] |
encode(byte[] data,
int offset,
int length)
Returns a binary representation of this attribute.
|
boolean |
equals(Object other)
Compares two STUN Attributes.
|
char |
getDataLength()
Returns the length of this attribute's body.
|
byte[] |
getHmacSha1Content() |
byte[] |
getKey() |
String |
getMedia() |
String |
getName()
Returns the human readable name of this attribute.
|
String |
getUsername() |
void |
setHmacSha1Content(byte[] hmacSha1Content) |
void |
setKey(byte[] key) |
void |
setMedia(String media)
Sets the media name that we should use to get the corresponding remote
key (short-term authentication only).
|
void |
setUsername(String username)
Sets the username that we should use to obtain an encryption key
(password) that the
encode() method should use when creating the
content of this message. |
String |
toString() |
getAttributeType, getLocationInMessage, setAttributeType, setLocationInMessagepublic static final String NAME
public static final String HMAC_SHA1_ALGORITHM
public static final char DATA_LENGTH
public byte[] getKey()
public void setKey(byte[] key)
public byte[] getHmacSha1Content()
public void setHmacSha1Content(byte[] hmacSha1Content)
public String getUsername()
public void setUsername(String username)
encode() method should use when creating the
content of this message.username - the username that we should use to obtain an encryption key
(password) that the encode() method should use when
creating the content of this message.public String getMedia()
public void setMedia(String media)
media - namepublic static byte[] calculateHmacSha1(byte[] message,
int offset,
int length,
byte[] key)
throws IllegalArgumentException
MessageIntegrityAttribute
regardless of the credentials being used (short or long term).message - the STUN message that the resulting content will need to
travel in.offset - the index where data starts in message.length - the length of the data in message that the method
should consider.key - the key that we should be using for the encoding (which
depends on whether we are using short or long term
credentials).IllegalArgumentException - if the encoding fails for some reason.public byte[] encode(byte[] data,
int offset,
int length)
ContextDependentAttributeencode in interface ContextDependentAttributedata - the content of the message that this attribute will be
transported inoffset - the content-related offset where the actual content
starts.length - the length of the content in the content array.public char getDataLength()
StunAttributegetDataLength in class StunAttributepublic String getName()
StunAttributegetName in class StunAttributepublic boolean equals(Object other)
StunAttributeequals in class StunAttributeother - the object to compare this attribute with.public byte[] encode()
throws UnsupportedOperationException
StunAttributeencode in class StunAttributeUnsupportedOperationExceptionprotected void decodeAttributeBody(byte[] data,
char offset,
char length)
throws StunException
StunAttributedecodeAttributeBody in class StunAttributedata - a binary array containing this attribute's field values and
NOT containing the attribute header.offset - the position where attribute values begin (most often offset
is equal to the index of the first byte after length)length - the length of the binary array.StunException - if attrubteValue contains invalid data.Copyright © 2017 TeleStax, Inc.. All Rights Reserved.