From 15b6780ccffd85184df11631c1c56285219cbf49 Mon Sep 17 00:00:00 2001 From: seb Date: Mon, 6 Dec 2010 16:41:58 +0000 Subject: [PATCH] * 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. --- .../attribute/MessageIntegrityAttribute.java | 19 ++++- src/org/ice4j/ice/Agent.java | 80 +++++-------------- src/org/ice4j/ice/Component.java | 75 ++++++++++++++++- .../ice4j/ice/ConnectivityCheckClient.java | 10 ++- .../ice4j/ice/ConnectivityCheckServer.java | 24 ++++-- src/org/ice4j/ice/IceMediaStream.java | 61 +++++++++++++- .../ice4j/security/CredentialsAuthority.java | 3 +- .../ice4j/security/CredentialsManager.java | 5 +- .../security/LongTermCredentialSession.java | 3 +- .../RelayedCandidateDatagramSocket.java | 18 +++-- test/test/Ice.java | 14 +++- test/test/SdpUtils.java | 7 +- 12 files changed, 230 insertions(+), 89 deletions(-) diff --git a/src/org/ice4j/attribute/MessageIntegrityAttribute.java b/src/org/ice4j/attribute/MessageIntegrityAttribute.java index 40da05c..9c8a631 100644 --- a/src/org/ice4j/attribute/MessageIntegrityAttribute.java +++ b/src/org/ice4j/attribute/MessageIntegrityAttribute.java @@ -105,6 +105,11 @@ public class MessageIntegrityAttribute */ private String username; + /** + * The media name if we use short-term authentication. + */ + private String media; + /** * Creates a MessageIntegrityAttribute. */ @@ -127,6 +132,17 @@ public class MessageIntegrityAttribute 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. * @@ -261,7 +277,8 @@ public class MessageIntegrityAttribute if(Message.isRequestType(msgType)) { /* 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) || Message.isErrorResponseType(msgType)) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 7fe7fa6..c96c452 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -141,16 +141,6 @@ public class Agent */ 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 * repair the case where both agents believe to have the controlling or the @@ -435,7 +425,8 @@ public class Agent */ public boolean isStarted() { - return state != IceProcessingState.WAITING; + return state != IceProcessingState.WAITING && + state != IceProcessingState.TERMINATED; } /** @@ -600,40 +591,6 @@ public class Agent return password; } - /** - * Returns the user name that we received from the remote peer or - * null if we haven't received a user name from them yet. - * - * @return the user name that we received from the remote peer or - * null 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 - * null if we haven't received a password from them yet. - * - * @return the password that we received from the remote peer or - * null 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 Agent should use in connectivity * 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) * 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 Agent can use in connectivity * 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) * 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 Agent would use in connectivity * check for outgoing Binding Requests. */ - public String generateRemoteUserName() + public String generateRemoteUserName(String media) { - return getLocalUfrag() + ":" + getRemoteUfrag(); - } + IceMediaStream stream = getStream(media); - /** - * 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; + if(stream == null) + { + return null; + } + + return getLocalUfrag() + ":" + stream.getRemoteUfrag(); } /** diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index 1b2401f..40168c5 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -22,6 +22,7 @@ import org.ice4j.*; * * @author Emil Ivov * @author Namal Senarathne + * @author Sebastien Vincent */ public class Component { @@ -75,6 +76,13 @@ public class Component */ private List remoteCandidates = new LinkedList(); + /** + * The list of candidates that the peer agent sent for this stream after + * connectivity establishment. + */ + private List remoteUpdateCandidates = + new LinkedList(); + /** * A Comparator that we use for sorting Candidates by * their priority. @@ -204,6 +212,71 @@ public class Component } } + /** + * Update the media-stream Component with the specified + * Candidate. + * + * @param candidate new Candidate to add. + */ + public void addUpdateRemoteCandidate(Candidate candidate) + { + synchronized(remoteUpdateCandidates) + { + remoteUpdateCandidates.add(candidate); + } + } + + /** + * Update ICE processing with the new Candidates. + */ + public void updateRemoteCandidate() + { + List checkList = null; + + synchronized(remoteUpdateCandidates) + { + if(remoteUpdateCandidates.size() == 0) + return; + + List localCnds = getLocalCandidates(); + checkList = new Vector(); + + 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 * registered in this component. @@ -747,7 +820,7 @@ public class Component * with this component. * * @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() { diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java index 2ce8c82..9e2d741 100644 --- a/src/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/ConnectivityCheckClient.java @@ -147,7 +147,9 @@ class ConnectivityCheckClient } //credentials - String localUserName = parentAgent.generateLocalUserName(); + String localUserName = parentAgent.generateLocalUserName( + candidatePair.getParentComponent().getParentStream().getName()); + UsernameAttribute unameAttr = AttributeFactory.createUsernameAttribute( localUserName); @@ -157,6 +159,12 @@ class ConnectivityCheckClient MessageIntegrityAttribute msgIntegrity = AttributeFactory .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); TransactionID tran = TransactionID.createNewTransactionID(); diff --git a/src/org/ice4j/ice/ConnectivityCheckServer.java b/src/org/ice4j/ice/ConnectivityCheckServer.java index 70af6ef..a5c3580 100644 --- a/src/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/ConnectivityCheckServer.java @@ -125,12 +125,13 @@ class ConnectivityCheckServer request, evt.getRemoteAddress()); /* 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 * requests */ + Attribute usernameAttribute = + AttributeFactory.createUsernameAttribute(uname.getUsername()); + Attribute messageIntegrityAttribute = AttributeFactory.createMessageIntegrityAttribute( new String(uname.getUsername())); @@ -362,29 +363,36 @@ class ConnectivityCheckServer * are expected to use when querying the remote peer. * * @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 username * equals the remote ufrag and null 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 //entire user name. int colon = username.indexOf(":"); + IceMediaStream stream = parentAgent.getStream(media); + if(stream == null) + { + return null; + } + if (colon < 0) { //caller gave us a ufrag - if (username.equals(parentAgent.getRemoteUfrag())) - return parentAgent.getRemotePassword().getBytes(); + if (username.equals(stream.getRemoteUfrag())) + return stream.getRemotePassword().getBytes(); } else { //caller gave us the entire username. - if (username.equals(parentAgent.generateLocalUserName())) + if (username.equals(parentAgent.generateLocalUserName(media))) { - if(parentAgent.getRemotePassword() != null) - return parentAgent.getRemotePassword().getBytes(); + if(stream.getRemotePassword() != null) + return stream.getRemotePassword().getBytes(); } } return null; diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index e1b784a..b8445dd 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -106,6 +106,16 @@ public class IceMediaStream */ 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 IceMediaStream object. * @@ -309,7 +319,7 @@ public class IceMediaStream createCheckList(checkList); orderCheckList(); - pruneCheckList(); + pruneCheckList(checkList); 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 * 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). + * + * @param checkList the checklist to prune */ - private void pruneCheckList() + protected void pruneCheckList(List checkList) { //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. @@ -803,4 +815,49 @@ public class IceMediaStream 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 + * null if we haven't received a user name from them yet. + * + * @return the user name that we received from the remote peer or + * null 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 + * null if we haven't received a password from them yet. + * + * @return the password that we received from the remote peer or + * null if we haven't received a password from them yet. + */ + public String getRemotePassword() + { + return remotePassword; + } } diff --git a/src/org/ice4j/security/CredentialsAuthority.java b/src/org/ice4j/security/CredentialsAuthority.java index 6bada7f..232406c 100644 --- a/src/org/ice4j/security/CredentialsAuthority.java +++ b/src/org/ice4j/security/CredentialsAuthority.java @@ -39,13 +39,14 @@ public interface CredentialsAuthority * * @param username the remote user name or user frag whose credentials we'd * 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 * username or user frag, an empty array if there was no password for that * username or null if the username is not a remote user name * recognized by this CredentialsAuthority. */ - public byte[] getRemoteKey(String username); + public byte[] getRemoteKey(String username, String media); /** * Verifies whether username is currently known to this authority diff --git a/src/org/ice4j/security/CredentialsManager.java b/src/org/ice4j/security/CredentialsManager.java index 4966721..394d13c 100644 --- a/src/org/ice4j/security/CredentialsManager.java +++ b/src/org/ice4j/security/CredentialsManager.java @@ -67,17 +67,18 @@ public class CredentialsManager * * @param username a remote user name or user frag whose credentials we'd * like to obtain. + * @param media the media name that we want to get remote key. * * @return null if username was not a recognized remote user name * for none of the currently registered CredentialsAuthoritys or * a byte array containing the first non-null password * that one of them returned. */ - public byte[] getRemoteKey(String username) + public byte[] getRemoteKey(String username, String media) { for (CredentialsAuthority auth : authorities) { - byte[] passwd = auth.getRemoteKey(username); + byte[] passwd = auth.getRemoteKey(username, media); if (passwd != null) { diff --git a/src/org/ice4j/security/LongTermCredentialSession.java b/src/org/ice4j/security/LongTermCredentialSession.java index 0856edc..ee38184 100644 --- a/src/org/ice4j/security/LongTermCredentialSession.java +++ b/src/org/ice4j/security/LongTermCredentialSession.java @@ -195,13 +195,14 @@ public class LongTermCredentialSession * * @param username the remote user name or user frag whose credentials * we'd like to obtain + * @param media not used * @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 null if the username is not a remote user * name recognized by this CredentialsAuthority * @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. return getLocalKey(username); diff --git a/src/org/ice4j/socket/RelayedCandidateDatagramSocket.java b/src/org/ice4j/socket/RelayedCandidateDatagramSocket.java index 0ff7111..905799d 100644 --- a/src/org/ice4j/socket/RelayedCandidateDatagramSocket.java +++ b/src/org/ice4j/socket/RelayedCandidateDatagramSocket.java @@ -67,7 +67,7 @@ public class RelayedCandidateDatagramSocket /** * The minimum channel number which is valid for TURN ChannelBind - * Requests. + * Requests. */ private static final char MIN_CHANNEL_NUMBER = 0x4000; @@ -79,7 +79,7 @@ public class RelayedCandidateDatagramSocket /** * The time in milliseconds before a TURN permission expires that a - * RelayedCandidateDatagramSocket is to try to reinstall it. + * RelayedCandidateDatagramSocket is to try to reinstall it. */ private static final long PERMISSION_LIFETIME_LEEWAY = 60 /* seconds */ * 1000L; @@ -242,7 +242,8 @@ public class RelayedCandidateDatagramSocket private boolean channelDataSocketAccept(DatagramPacket p) { // Is it from our TURN server? - if (turnCandidateHarvest.harvester.stunServer.equals(p.getSocketAddress())) + if (turnCandidateHarvest.harvester.stunServer.equals( + p.getSocketAddress())) { int pLength = p.getLength(); @@ -278,13 +279,16 @@ public class RelayedCandidateDatagramSocket ((pData[pOffset++] << 8) | (pData[pOffset++] & 0xFF)); + int padding = ((length % 4) > 0) ? 4 - (length % 4) : 0; + /* * The Length field specifies the length in bytes of the * Application Data field. The Length field does not include * the padding that is sometimes present in the data of the * DatagramPacket. */ - return ((CHANNELDATA_LENGTH_LENGTH + length) >= pLength); + return (length == + (pLength - padding - CHANNELDATA_LENGTH_LENGTH)); } } } @@ -547,7 +551,7 @@ public class RelayedCandidateDatagramSocket /* * RFC 5766: When the client receives a Data indication, it checks that * the Data indication contains both an XOR-PEER-ADDRESS and a DATA - * attribute, and discards the indication if it does not. + * attribute, and discards the indication if it does not. */ XorPeerAddressAttribute peerAddressAttribute = (XorPeerAddressAttribute) @@ -1312,7 +1316,7 @@ public class RelayedCandidateDatagramSocket /** * Gets the indication which determines whether this instance is * currently considered bound/installed. - * + * * @return true if this instance is currently considered * bound/installed; otherwise, false */ @@ -1502,7 +1506,7 @@ public class RelayedCandidateDatagramSocket * * @param channelNumberIsConfirmed true if allocation of the * channel number has been confirmed by a success Response to - * our ChannelBind Request + * our ChannelBind Request * @param channelNumberIsConfirmedTransactionID an array of * bytes which represents the ID of the transaction with which * the confirmation about the allocation of the channel number has diff --git a/test/test/Ice.java b/test/test/Ice.java index b0767f3..ac4be80 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -63,14 +63,20 @@ public class Ice long endTime = System.currentTimeMillis(); transferRemoteCandidates(localAgent, remotePeer); - localAgent.setRemoteUfrag(remotePeer.getLocalUfrag()); - localAgent.setRemotePassword(remotePeer.getLocalPassword()); + for(IceMediaStream stream : localAgent.getStreams()) + { + stream.setRemoteUfrag(remotePeer.getLocalUfrag()); + stream.setRemotePassword(remotePeer.getLocalPassword()); + } if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER) transferRemoteCandidates(remotePeer, localAgent); - remotePeer.setRemoteUfrag(localAgent.getLocalUfrag()); - remotePeer.setRemotePassword(localAgent.getLocalPassword()); + for(IceMediaStream stream : remotePeer.getStreams()) + { + stream.setRemoteUfrag(localAgent.getLocalUfrag()); + stream.setRemotePassword(localAgent.getLocalPassword()); + } System.out.println("Total candidate gathering time: " + (endTime - startTime) + "ms"); diff --git a/test/test/SdpUtils.java b/test/test/SdpUtils.java index 17d654c..939f188 100644 --- a/test/test/SdpUtils.java +++ b/test/test/SdpUtils.java @@ -119,8 +119,11 @@ public class SdpUtils SdpFactory factory = SdpFactory.getInstance(); SessionDescription sdess = factory.createSessionDescription(sdp); - localAgent.setRemotePassword(sdess.getAttribute("ice-pwd")); - localAgent.setRemoteUfrag(sdess.getAttribute("ice-ufrag")); + for(IceMediaStream stream : localAgent.getStreams()) + { + stream.setRemotePassword(sdess.getAttribute("ice-pwd")); + stream.setRemoteUfrag(sdess.getAttribute("ice-ufrag")); + } Connection globalConn = sdess.getConnection(); String globalConnAddr = null;