Search in sources :

Example 1 with SipException

use of javax.sip.SipException in project Openfire by igniterealtime.

the class SipSecurityManager method handleChallenge.

/**
     * Uses securityAuthority to determinie a set of valid user credentials for
     * the specified Response (Challenge) and appends it to the challenged
     * request so that it could be retransmitted.
     * <p/>
     * Fredrik Wickstrom reported that dialog cseq counters are not incremented
     * when resending requests. He later uncovered additional problems and
     * proposed a way to fix them (his proposition was taken into account).
     *
     * @param challenge             the 401/407 challenge response
     * @param challengedTransaction the transaction established by the challenged request
     * @return a transaction containing a reoriginated request with the
     *         necessary authorization header.
     * @throws SipSecurityException
     */
public ClientTransaction handleChallenge(Response challenge, ClientTransaction challengedTransaction) throws SipSecurityException, SipException, InvalidArgumentException, ParseException {
    try {
        String branchID = challengedTransaction.getBranchId();
        Request challengedRequest = challengedTransaction.getRequest();
        Request reoriginatedRequest = (Request) challengedRequest.clone();
        ListIterator authHeaders = null;
        if (challenge == null || reoriginatedRequest == null)
            throw new NullPointerException("A null argument was passed to handle challenge.");
        if (challenge.getStatusCode() == Response.UNAUTHORIZED)
            authHeaders = challenge.getHeaders(WWWAuthenticateHeader.NAME);
        else if (challenge.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED)
            authHeaders = challenge.getHeaders(ProxyAuthenticateHeader.NAME);
        if (authHeaders == null)
            throw new SecurityException("Could not find WWWAuthenticate or ProxyAuthenticate headers");
        // Remove all authorization headers from the request (we'll re-add
        // them
        // from cache)
        reoriginatedRequest.removeHeader(AuthorizationHeader.NAME);
        reoriginatedRequest.removeHeader(ProxyAuthorizationHeader.NAME);
        // rfc 3261 says that the cseq header should be augmented for the
        // new
        // request. do it here so that the new dialog (created together with
        // the new client transaction) takes it into account.
        // Bug report - Fredrik Wickstrom
        CSeqHeader cSeq = (CSeqHeader) reoriginatedRequest.getHeader((CSeqHeader.NAME));
        cSeq.setSequenceNumber(cSeq.getSequenceNumber() + 1);
        ClientTransaction retryTran = transactionCreator.getNewClientTransaction(reoriginatedRequest);
        WWWAuthenticateHeader authHeader = null;
        CredentialsCacheEntry ccEntry = null;
        while (authHeaders.hasNext()) {
            authHeader = (WWWAuthenticateHeader) authHeaders.next();
            String realm = authHeader.getRealm();
            // Check whether we have cached credentials for authHeader's
            // realm
            // make sure that if such credentials exist they get removed.
            // The
            // challenge means that there's something wrong with them.
            ccEntry = cachedCredentials.remove(realm);
            // Try to guess user name and facilitate user
            UserCredentials defaultCredentials = new UserCredentials();
            FromHeader from = (FromHeader) reoriginatedRequest.getHeader(FromHeader.NAME);
            URI uri = from.getAddress().getURI();
            if (uri.isSipURI()) {
                Log.debug("handleChallenge", SIPConfig.getAuthUserName());
                String user = SIPConfig.getAuthUserName() != null ? SIPConfig.getAuthUserName() : ((SipURI) uri).getUser();
                defaultCredentials.setAuthUserName(user == null ? SIPConfig.getUserName() : user);
            }
            boolean ccEntryHasSeenTran = false;
            if (ccEntry != null)
                ccEntryHasSeenTran = ccEntry.processResponse(branchID);
            // get a new pass
            if (// we don't have credentials for the
            ccEntry == null || // specified realm
            ((!authHeader.isStale() && ccEntryHasSeenTran))) {
                if (ccEntry == null) {
                    ccEntry = new CredentialsCacheEntry();
                    ccEntry.userCredentials = defaultCredentials;
                }
                // put the returned user name in the properties file
                // so that it appears as a default one next time user is
                // prompted for pass
                SIPConfig.setUserName(ccEntry.userCredentials.getUserName());
            } else // encode and send what we have
            if (ccEntry != null && (!ccEntryHasSeenTran || authHeader.isStale())) {
            }
            // if user canceled or sth else went wrong
            if (ccEntry.userCredentials == null)
                throw new SecurityException("Unable to authenticate with realm " + realm);
            AuthorizationHeader authorization = this.getAuthorization(reoriginatedRequest.getMethod(), reoriginatedRequest.getRequestURI().toString(), reoriginatedRequest.getContent() == null ? "" : reoriginatedRequest.getContent().toString(), authHeader, ccEntry.userCredentials);
            ccEntry.processRequest(retryTran.getBranchId());
            cachedCredentials.cacheEntry(realm, ccEntry);
            reoriginatedRequest.addHeader(authorization);
            // if there was trouble with the user - make sure we fix it
            if (uri.isSipURI()) {
                ((SipURI) uri).setUser(ccEntry.userCredentials.getUserName());
                Address add = from.getAddress();
                add.setURI(uri);
                from.setAddress(add);
                reoriginatedRequest.setHeader(from);
                if (challengedRequest.getMethod().equals(Request.REGISTER)) {
                    ToHeader to = (ToHeader) reoriginatedRequest.getHeader(ToHeader.NAME);
                    add.setURI(uri);
                    to.setAddress(add);
                    reoriginatedRequest.setHeader(to);
                }
                // very ugly but very necessary
                sipManCallback.setCurrentlyUsedURI(uri.toString());
                Log.debug("URI: " + uri.toString());
            }
        // if this is a register - fix to as well
        }
        return retryTran;
    } catch (Exception e) {
        Log.debug("ERRO REG: " + e.toString());
        return null;
    }
}
Also used : Address(javax.sip.address.Address) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) ListIterator(java.util.ListIterator) SipURI(javax.sip.address.SipURI) URI(javax.sip.address.URI) SipURI(javax.sip.address.SipURI) SipException(javax.sip.SipException) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException)

Example 2 with SipException

use of javax.sip.SipException in project XobotOS by xamarin.

the class SipSessionGroup method reset.

synchronized void reset(String localIp) throws SipException, IOException {
    mLocalIp = localIp;
    if (localIp == null)
        return;
    SipProfile myself = mLocalProfile;
    SipFactory sipFactory = SipFactory.getInstance();
    Properties properties = new Properties();
    properties.setProperty("javax.sip.STACK_NAME", getStackName());
    properties.setProperty("gov.nist.javax.sip.THREAD_POOL_SIZE", THREAD_POOL_SIZE);
    String outboundProxy = myself.getProxyAddress();
    if (!TextUtils.isEmpty(outboundProxy)) {
        Log.v(TAG, "outboundProxy is " + outboundProxy);
        properties.setProperty("javax.sip.OUTBOUND_PROXY", outboundProxy + ":" + myself.getPort() + "/" + myself.getProtocol());
    }
    SipStack stack = mSipStack = sipFactory.createSipStack(properties);
    try {
        SipProvider provider = stack.createSipProvider(stack.createListeningPoint(localIp, allocateLocalPort(), myself.getProtocol()));
        provider.addSipListener(this);
        mSipHelper = new SipHelper(stack, provider);
    } catch (InvalidArgumentException e) {
        throw new IOException(e.getMessage());
    } catch (TooManyListenersException e) {
        // must never happen
        throw new SipException("SipSessionGroup constructor", e);
    }
    Log.d(TAG, " start stack for " + myself.getUriString());
    stack.start();
    mCallReceiverSession = null;
    mSessionMap.clear();
    resetExternalAddress();
}
Also used : TooManyListenersException(java.util.TooManyListenersException) InvalidArgumentException(javax.sip.InvalidArgumentException) SipProfile(android.net.sip.SipProfile) SipFactory(javax.sip.SipFactory) SipStack(javax.sip.SipStack) IOException(java.io.IOException) Properties(java.util.Properties) SipException(javax.sip.SipException) SipProvider(javax.sip.SipProvider)

Example 3 with SipException

use of javax.sip.SipException in project XobotOS by xamarin.

the class SipSessionGroup method createPeerProfile.

private static SipProfile createPeerProfile(HeaderAddress header) throws SipException {
    try {
        Address address = header.getAddress();
        SipURI uri = (SipURI) address.getURI();
        String username = uri.getUser();
        if (username == null)
            username = ANONYMOUS;
        int port = uri.getPort();
        SipProfile.Builder builder = new SipProfile.Builder(username, uri.getHost()).setDisplayName(address.getDisplayName());
        if (port > 0)
            builder.setPort(port);
        return builder.build();
    } catch (IllegalArgumentException e) {
        throw new SipException("createPeerProfile()", e);
    } catch (ParseException e) {
        throw new SipException("createPeerProfile()", e);
    }
}
Also used : HeaderAddress(javax.sip.header.HeaderAddress) Address(javax.sip.address.Address) SipProfile(android.net.sip.SipProfile) ParseException(java.text.ParseException) SipURI(javax.sip.address.SipURI) SipException(javax.sip.SipException) ListeningPoint(javax.sip.ListeningPoint)

Example 4 with SipException

use of javax.sip.SipException in project XobotOS by xamarin.

the class SipHelper method sendRinging.

/**
     * @param event the INVITE request event
     */
public ServerTransaction sendRinging(RequestEvent event, String tag) throws SipException {
    try {
        Request request = event.getRequest();
        ServerTransaction transaction = getServerTransaction(event);
        Response response = mMessageFactory.createResponse(Response.RINGING, request);
        ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
        toHeader.setTag(tag);
        response.addHeader(toHeader);
        if (DEBUG)
            Log.d(TAG, "send RINGING: " + response);
        transaction.sendResponse(response);
        return transaction;
    } catch (ParseException e) {
        throw new SipException("sendRinging()", e);
    }
}
Also used : Response(javax.sip.message.Response) Request(javax.sip.message.Request) ToHeader(javax.sip.header.ToHeader) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Example 5 with SipException

use of javax.sip.SipException in project XobotOS by xamarin.

the class SipHelper method sendInviteRequestTerminated.

public void sendInviteRequestTerminated(Request inviteRequest, ServerTransaction inviteTransaction) throws SipException {
    try {
        Response response = mMessageFactory.createResponse(Response.REQUEST_TERMINATED, inviteRequest);
        if (DEBUG)
            Log.d(TAG, "send response: " + response);
        inviteTransaction.sendResponse(response);
    } catch (ParseException e) {
        throw new SipException("sendInviteRequestTerminated()", e);
    }
}
Also used : Response(javax.sip.message.Response) ParseException(java.text.ParseException) SipException(javax.sip.SipException)

Aggregations

SipException (javax.sip.SipException)37 ParseException (java.text.ParseException)25 IOException (java.io.IOException)16 SIPRequest (gov.nist.javax.sip.message.SIPRequest)15 Request (javax.sip.message.Request)15 InvalidArgumentException (javax.sip.InvalidArgumentException)14 ObjectInUseException (javax.sip.ObjectInUseException)10 SIPResponse (gov.nist.javax.sip.message.SIPResponse)9 ClientTransaction (javax.sip.ClientTransaction)8 Response (javax.sip.message.Response)8 Via (gov.nist.javax.sip.header.Via)6 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)6 DialogDoesNotExistException (javax.sip.DialogDoesNotExistException)6 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)6 Hop (javax.sip.address.Hop)6 SipURI (javax.sip.address.SipURI)6 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)5 ListeningPointImpl (gov.nist.javax.sip.ListeningPointImpl)3 From (gov.nist.javax.sip.header.From)3 RSeq (gov.nist.javax.sip.header.RSeq)3