* Fix a bug when the TURN ChannelData data is padded, it results of the ChannelData to be discarded;

* Allow remote candidates to be injected while ICE processing is running;
 * Support different remote ufrag/password for streams.
This commit is contained in:
seb 2010-12-06 16:41:58 +00:00
parent fc3e411657
commit 15b6780ccf
12 changed files with 230 additions and 89 deletions

View File

@ -105,6 +105,11 @@ public class MessageIntegrityAttribute
*/ */
private String username; private String username;
/**
* The media name if we use short-term authentication.
*/
private String media;
/** /**
* Creates a <tt>MessageIntegrityAttribute</tt>. * Creates a <tt>MessageIntegrityAttribute</tt>.
*/ */
@ -127,6 +132,17 @@ public class MessageIntegrityAttribute
this.username = username; this.username = username;
} }
/**
* Sets the media name that we should use to get the corresponding remote
* key (short-term authentication only).
*
* @param media name
*/
public void setMedia(String media)
{
this.media = media;
}
/** /**
* Returns the HMAC-SHA1 value stored in this attribute. * Returns the HMAC-SHA1 value stored in this attribute.
* *
@ -261,7 +277,8 @@ public class MessageIntegrityAttribute
if(Message.isRequestType(msgType)) if(Message.isRequestType(msgType))
{ {
/* attribute part of a request, use the remote key */ /* attribute part of a request, use the remote key */
key = stunStack.getCredentialsManager().getRemoteKey(username); key = stunStack.getCredentialsManager().getRemoteKey(username,
media);
} }
else if(Message.isSuccessResponseType(msgType) || else if(Message.isSuccessResponseType(msgType) ||
Message.isErrorResponseType(msgType)) Message.isErrorResponseType(msgType))

View File

@ -141,16 +141,6 @@ public class Agent
*/ */
private final String password; private final String password;
/**
* The user fragment that we received from the remote party.
*/
private String remoteUfrag;
/**
* The password that we received from the remote party.
*/
private String remotePassword;
/** /**
* The tie-breaker number is used in connectivity checks to detect and * The tie-breaker number is used in connectivity checks to detect and
* repair the case where both agents believe to have the controlling or the * repair the case where both agents believe to have the controlling or the
@ -435,7 +425,8 @@ public class Agent
*/ */
public boolean isStarted() public boolean isStarted()
{ {
return state != IceProcessingState.WAITING; return state != IceProcessingState.WAITING &&
state != IceProcessingState.TERMINATED;
} }
/** /**
@ -600,40 +591,6 @@ public class Agent
return password; return password;
} }
/**
* Returns 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.
*
* @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 getRemoteUfrag()
{
return remoteUfrag;
}
/**
* Returns the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*
* @return the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*/
public String getRemotePassword()
{
return remotePassword;
}
/**
* Specifies the user name that we received from the remote peer.
*
* @param remoteUfrag the user name that we received from the remote peer.
*/
public void setRemoteUfrag(String remoteUfrag)
{
this.remoteUfrag = remoteUfrag;
}
/** /**
* Returns the user name that this <tt>Agent</tt> should use in connectivity * Returns the user name that this <tt>Agent</tt> should use in connectivity
* checks for outgoing Binding Requests. According to RFC 5245, a Binding * checks for outgoing Binding Requests. According to RFC 5245, a Binding
@ -650,12 +607,20 @@ public class Agent
* password of RPASS. A connectivity check from R to L (and its response) * password of RPASS. A connectivity check from R to L (and its response)
* utilize the username LFRAG:RFRAG and a password of LPASS. * utilize the username LFRAG:RFRAG and a password of LPASS.
* *
* @param media media name that we want to generate local username for.
* @return a user name that this <tt>Agent</tt> can use in connectivity * @return a user name that this <tt>Agent</tt> can use in connectivity
* check for outgoing Binding Requests. * check for outgoing Binding Requests.
*/ */
public String generateLocalUserName() public String generateLocalUserName(String media)
{ {
return getRemoteUfrag() + ":" + getLocalUfrag(); IceMediaStream stream = getStream(media);
if(stream == null || stream.getRemoteUfrag() == null)
{
return null;
}
return stream.getRemoteUfrag() + ":" + getLocalUfrag();
} }
/** /**
@ -675,23 +640,20 @@ public class Agent
* password of RPASS. A connectivity check from R to L (and its response) * password of RPASS. A connectivity check from R to L (and its response)
* utilize the username LFRAG:RFRAG and a password of LPASS. * utilize the username LFRAG:RFRAG and a password of LPASS.
* *
* @param media media name that we want to generate local username for.
* @return a user name that a peer <tt>Agent</tt> would use in connectivity * @return a user name that a peer <tt>Agent</tt> would use in connectivity
* check for outgoing Binding Requests. * check for outgoing Binding Requests.
*/ */
public String generateRemoteUserName() public String generateRemoteUserName(String media)
{ {
return getLocalUfrag() + ":" + getRemoteUfrag(); IceMediaStream stream = getStream(media);
}
/** if(stream == null)
* Specifies the password that we received from the remote peer. {
* return null;
* @param remotePassword the user name that we received from the remote }
* peer.
*/ return getLocalUfrag() + ":" + stream.getRemoteUfrag();
public void setRemotePassword(String remotePassword)
{
this.remotePassword = remotePassword;
} }
/** /**

View File

@ -22,6 +22,7 @@ import org.ice4j.*;
* *
* @author Emil Ivov * @author Emil Ivov
* @author Namal Senarathne * @author Namal Senarathne
* @author Sebastien Vincent
*/ */
public class Component public class Component
{ {
@ -75,6 +76,13 @@ public class Component
*/ */
private List<Candidate> remoteCandidates = new LinkedList<Candidate>(); private List<Candidate> remoteCandidates = new LinkedList<Candidate>();
/**
* The list of candidates that the peer agent sent for this stream after
* connectivity establishment.
*/
private List<Candidate> remoteUpdateCandidates =
new LinkedList<Candidate>();
/** /**
* A <tt>Comparator</tt> that we use for sorting <tt>Candidate</tt>s by * A <tt>Comparator</tt> that we use for sorting <tt>Candidate</tt>s by
* their priority. * their priority.
@ -204,6 +212,71 @@ public class Component
} }
} }
/**
* Update the media-stream <tt>Component</tt> with the specified
* <tt>Candidate</tt>.
*
* @param candidate new <tt>Candidate</tt> to add.
*/
public void addUpdateRemoteCandidate(Candidate candidate)
{
synchronized(remoteUpdateCandidates)
{
remoteUpdateCandidates.add(candidate);
}
}
/**
* Update ICE processing with the new <tt>Candidate</tt>s.
*/
public void updateRemoteCandidate()
{
List<CandidatePair> checkList = null;
synchronized(remoteUpdateCandidates)
{
if(remoteUpdateCandidates.size() == 0)
return;
List<LocalCandidate> localCnds = getLocalCandidates();
checkList = new Vector<CandidatePair>();
for(LocalCandidate localCnd : localCnds)
{
for(Candidate remoteCnd : remoteUpdateCandidates)
{
if(localCnd.canReach(remoteCnd))
{
CandidatePair pair = new CandidatePair(localCnd, remoteCnd);
checkList.add(pair);
}
}
}
remoteUpdateCandidates.clear();
}
/* sort and prune update checklist */
Collections.sort(checkList, CandidatePair.comparator);
parentStream.pruneCheckList(checkList);
if(parentStream.getCheckList().getState().equals(
CheckListState.RUNNING))
{
/* add the update CandidatePair list to the currently running
* checklist
*/
CheckList streamCheckList = parentStream.getCheckList();
synchronized(streamCheckList)
{
for(CandidatePair pair : checkList)
{
streamCheckList.add(pair);
}
}
}
}
/** /**
* Returns a copy of the list containing all remote candidates currently * Returns a copy of the list containing all remote candidates currently
* registered in this component. * registered in this component.
@ -747,7 +820,7 @@ public class Component
* with this component. * with this component.
* *
* @return "RTP" if the component ID is 1, "RTCP" if the component id is 2 * @return "RTP" if the component ID is 1, "RTCP" if the component id is 2
* and the compoent id itself otherwise. * and the component id itself otherwise.
*/ */
public String getName() public String getName()
{ {

View File

@ -147,7 +147,9 @@ class ConnectivityCheckClient
} }
//credentials //credentials
String localUserName = parentAgent.generateLocalUserName(); String localUserName = parentAgent.generateLocalUserName(
candidatePair.getParentComponent().getParentStream().getName());
UsernameAttribute unameAttr = AttributeFactory.createUsernameAttribute( UsernameAttribute unameAttr = AttributeFactory.createUsernameAttribute(
localUserName); localUserName);
@ -157,6 +159,12 @@ class ConnectivityCheckClient
MessageIntegrityAttribute msgIntegrity = AttributeFactory MessageIntegrityAttribute msgIntegrity = AttributeFactory
.createMessageIntegrityAttribute(localUserName); .createMessageIntegrityAttribute(localUserName);
// when we will encode the MESSAGE-INTEGRITY attribute (thus generate
// the HMAC-SHA1 authentication), we need to know the remote key of the
// current stream, that why we pass the media name.
msgIntegrity.setMedia(
candidatePair.getParentComponent().getParentStream().getName());
request.addAttribute(msgIntegrity); request.addAttribute(msgIntegrity);
TransactionID tran = TransactionID.createNewTransactionID(); TransactionID tran = TransactionID.createNewTransactionID();

View File

@ -125,12 +125,13 @@ class ConnectivityCheckServer
request, evt.getRemoteAddress()); request, evt.getRemoteAddress());
/* add USERNAME and MESSAGE-INTEGRITY attribute in the response */ /* add USERNAME and MESSAGE-INTEGRITY attribute in the response */
Attribute usernameAttribute = AttributeFactory.createUsernameAttribute(
parentAgent.generateLocalUserName());
/* The responses utilize the same usernames and passwords as the /* The responses utilize the same usernames and passwords as the
* requests * requests
*/ */
Attribute usernameAttribute =
AttributeFactory.createUsernameAttribute(uname.getUsername());
Attribute messageIntegrityAttribute = Attribute messageIntegrityAttribute =
AttributeFactory.createMessageIntegrityAttribute( AttributeFactory.createMessageIntegrityAttribute(
new String(uname.getUsername())); new String(uname.getUsername()));
@ -362,29 +363,36 @@ class ConnectivityCheckServer
* are expected to use when querying the remote peer. * are expected to use when querying the remote peer.
* *
* @param username the remote ufrag that we should return a password for. * @param username the remote ufrag that we should return a password for.
* @param media the media name that we want to get remote key.
* *
* @return this handler's parent agent remote password if <tt>username</tt> * @return this handler's parent agent remote password if <tt>username</tt>
* equals the remote ufrag and <tt>null</tt> otherwise. * equals the remote ufrag and <tt>null</tt> otherwise.
*/ */
public byte[] getRemoteKey(String username) public byte[] getRemoteKey(String username, String media)
{ {
//support both the case where username is the local fragment or the //support both the case where username is the local fragment or the
//entire user name. //entire user name.
int colon = username.indexOf(":"); int colon = username.indexOf(":");
IceMediaStream stream = parentAgent.getStream(media);
if(stream == null)
{
return null;
}
if (colon < 0) if (colon < 0)
{ {
//caller gave us a ufrag //caller gave us a ufrag
if (username.equals(parentAgent.getRemoteUfrag())) if (username.equals(stream.getRemoteUfrag()))
return parentAgent.getRemotePassword().getBytes(); return stream.getRemotePassword().getBytes();
} }
else else
{ {
//caller gave us the entire username. //caller gave us the entire username.
if (username.equals(parentAgent.generateLocalUserName())) if (username.equals(parentAgent.generateLocalUserName(media)))
{ {
if(parentAgent.getRemotePassword() != null) if(stream.getRemotePassword() != null)
return parentAgent.getRemotePassword().getBytes(); return stream.getRemotePassword().getBytes();
} }
} }
return null; return null;

View File

@ -106,6 +106,16 @@ public class IceMediaStream
*/ */
private int maxCheckListSize = Agent.DEFAULT_MAX_CHECK_LIST_SIZE; private int maxCheckListSize = Agent.DEFAULT_MAX_CHECK_LIST_SIZE;
/**
* The user fragment that we received from the remote party.
*/
private String remoteUfrag = null;
/**
* The password that we received from the remote party.
*/
private String remotePassword = null;
/** /**
* Initializes a new <tt>IceMediaStream</tt> object. * Initializes a new <tt>IceMediaStream</tt> object.
* *
@ -309,7 +319,7 @@ public class IceMediaStream
createCheckList(checkList); createCheckList(checkList);
orderCheckList(); orderCheckList();
pruneCheckList(); pruneCheckList(checkList);
logger.finest("Checklist initialized."); logger.finest("Checklist initialized.");
} }
} }
@ -377,8 +387,10 @@ public class IceMediaStream
* In addition, in order to limit the attacks described in Section 18.5.2 * In addition, in order to limit the attacks described in Section 18.5.2
* of the ICE spec, we limit the total number of pairs and hence * of the ICE spec, we limit the total number of pairs and hence
* (connectivity checks) to a specific value, (a total of 100 by default). * (connectivity checks) to a specific value, (a total of 100 by default).
*
* @param checkList the checklist to prune
*/ */
private void pruneCheckList() protected void pruneCheckList(List<CandidatePair> checkList)
{ {
//a list that we only use for storing pairs that we've already gone //a list that we only use for storing pairs that we've already gone
//through. The list is destroyed at the end of this method. //through. The list is destroyed at the end of this method.
@ -803,4 +815,49 @@ public class IceMediaStream
return null; return null;
} }
/**
* Specifies the user name that we received from the remote peer.
*
* @param remoteUfrag the user name that we received from the remote peer.
*/
public void setRemoteUfrag(String remoteUfrag)
{
this.remoteUfrag = remoteUfrag;
}
/**
* Returns 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.
*
* @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 getRemoteUfrag()
{
return remoteUfrag;
}
/**
* Specifies the password that we received from the remote peer.
*
* @param remotePassword the user name that we received from the remote
* peer.
*/
public void setRemotePassword(String remotePassword)
{
this.remotePassword = remotePassword;
}
/**
* Returns the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*
* @return the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*/
public String getRemotePassword()
{
return remotePassword;
}
} }

View File

@ -39,13 +39,14 @@ public interface CredentialsAuthority
* *
* @param username the remote user name or user frag whose credentials we'd * @param username the remote user name or user frag whose credentials we'd
* like to obtain. * like to obtain.
* @param media the media name that we want to get remote key.
* *
* @return the key (password) that corresponds to the specified remote * @return the key (password) that corresponds to the specified remote
* username or user frag, an empty array if there was no password for that * username or user frag, an empty array if there was no password for that
* username or <tt>null</tt> if the username is not a remote user name * username or <tt>null</tt> if the username is not a remote user name
* recognized by this <tt>CredentialsAuthority</tt>. * recognized by this <tt>CredentialsAuthority</tt>.
*/ */
public byte[] getRemoteKey(String username); public byte[] getRemoteKey(String username, String media);
/** /**
* Verifies whether <tt>username</tt> is currently known to this authority * Verifies whether <tt>username</tt> is currently known to this authority

View File

@ -67,17 +67,18 @@ public class CredentialsManager
* *
* @param username a remote user name or user frag whose credentials we'd * @param username a remote user name or user frag whose credentials we'd
* like to obtain. * like to obtain.
* @param media the media name that we want to get remote key.
* *
* @return <tt>null</tt> if username was not a recognized remote user name * @return <tt>null</tt> if username was not a recognized remote user name
* for none of the currently registered <tt>CredentialsAuthority</tt>s or * for none of the currently registered <tt>CredentialsAuthority</tt>s or
* a <tt>byte</tt> array containing the first non-<tt>null</tt> password * a <tt>byte</tt> array containing the first non-<tt>null</tt> password
* that one of them returned. * that one of them returned.
*/ */
public byte[] getRemoteKey(String username) public byte[] getRemoteKey(String username, String media)
{ {
for (CredentialsAuthority auth : authorities) for (CredentialsAuthority auth : authorities)
{ {
byte[] passwd = auth.getRemoteKey(username); byte[] passwd = auth.getRemoteKey(username, media);
if (passwd != null) if (passwd != null)
{ {

View File

@ -195,13 +195,14 @@ public class LongTermCredentialSession
* *
* @param username the remote user name or user frag whose credentials * @param username the remote user name or user frag whose credentials
* we'd like to obtain * we'd like to obtain
* @param media not used
* @return the key (password) that corresponds to the specified remote * @return the key (password) that corresponds to the specified remote
* username or user frag, an empty array if there was no password for * username or user frag, an empty array if there was no password for
* that username or <tt>null</tt> if the username is not a remote user * that username or <tt>null</tt> if the username is not a remote user
* name recognized by this <tt>CredentialsAuthority</tt> * name recognized by this <tt>CredentialsAuthority</tt>
* @see CredentialsAuthority#getRemoteKey(String) * @see CredentialsAuthority#getRemoteKey(String)
*/ */
public byte[] getRemoteKey(String username) public byte[] getRemoteKey(String username, String media)
{ {
// The password is the same on the local and the remote sides. // The password is the same on the local and the remote sides.
return getLocalKey(username); return getLocalKey(username);

View File

@ -242,7 +242,8 @@ public class RelayedCandidateDatagramSocket
private boolean channelDataSocketAccept(DatagramPacket p) private boolean channelDataSocketAccept(DatagramPacket p)
{ {
// Is it from our TURN server? // Is it from our TURN server?
if (turnCandidateHarvest.harvester.stunServer.equals(p.getSocketAddress())) if (turnCandidateHarvest.harvester.stunServer.equals(
p.getSocketAddress()))
{ {
int pLength = p.getLength(); int pLength = p.getLength();
@ -278,13 +279,16 @@ public class RelayedCandidateDatagramSocket
((pData[pOffset++] << 8) ((pData[pOffset++] << 8)
| (pData[pOffset++] & 0xFF)); | (pData[pOffset++] & 0xFF));
int padding = ((length % 4) > 0) ? 4 - (length % 4) : 0;
/* /*
* The Length field specifies the length in bytes of the * The Length field specifies the length in bytes of the
* Application Data field. The Length field does not include * Application Data field. The Length field does not include
* the padding that is sometimes present in the data of the * the padding that is sometimes present in the data of the
* DatagramPacket. * DatagramPacket.
*/ */
return ((CHANNELDATA_LENGTH_LENGTH + length) >= pLength); return (length ==
(pLength - padding - CHANNELDATA_LENGTH_LENGTH));
} }
} }
} }

View File

@ -63,14 +63,20 @@ public class Ice
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
transferRemoteCandidates(localAgent, remotePeer); transferRemoteCandidates(localAgent, remotePeer);
localAgent.setRemoteUfrag(remotePeer.getLocalUfrag()); for(IceMediaStream stream : localAgent.getStreams())
localAgent.setRemotePassword(remotePeer.getLocalPassword()); {
stream.setRemoteUfrag(remotePeer.getLocalUfrag());
stream.setRemotePassword(remotePeer.getLocalPassword());
}
if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER) if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER)
transferRemoteCandidates(remotePeer, localAgent); transferRemoteCandidates(remotePeer, localAgent);
remotePeer.setRemoteUfrag(localAgent.getLocalUfrag()); for(IceMediaStream stream : remotePeer.getStreams())
remotePeer.setRemotePassword(localAgent.getLocalPassword()); {
stream.setRemoteUfrag(localAgent.getLocalUfrag());
stream.setRemotePassword(localAgent.getLocalPassword());
}
System.out.println("Total candidate gathering time: " System.out.println("Total candidate gathering time: "
+ (endTime - startTime) + "ms"); + (endTime - startTime) + "ms");

View File

@ -119,8 +119,11 @@ public class SdpUtils
SdpFactory factory = SdpFactory.getInstance(); SdpFactory factory = SdpFactory.getInstance();
SessionDescription sdess = factory.createSessionDescription(sdp); SessionDescription sdess = factory.createSessionDescription(sdp);
localAgent.setRemotePassword(sdess.getAttribute("ice-pwd")); for(IceMediaStream stream : localAgent.getStreams())
localAgent.setRemoteUfrag(sdess.getAttribute("ice-ufrag")); {
stream.setRemotePassword(sdess.getAttribute("ice-pwd"));
stream.setRemoteUfrag(sdess.getAttribute("ice-ufrag"));
}
Connection globalConn = sdess.getConnection(); Connection globalConn = sdess.getConnection();
String globalConnAddr = null; String globalConnAddr = null;