예제 #1
0
  private void switchOverToZLib(Tag currentTag)
      throws XmlPullParserException, IOException, NoSuchAlgorithmException {
    tagReader.readTag(); // read tag close

    tagWriter.setOutputStream(new ZLibOutputStream(tagWriter.getOutputStream()));
    tagReader.setInputStream(new ZLibInputStream(tagReader.getInputStream()));

    sendStartStream();
    Log.d(LOGTAG, account.getJid() + ": compression enabled");
    processStream(tagReader.readTag());
  }
예제 #2
0
  private void switchOverToTls(Tag currentTag) throws XmlPullParserException, IOException {
    Tag nextTag = tagReader.readTag(); // should be proceed end tag
    try {
      SSLContext sc = SSLContext.getInstance("TLS");
      TrustManagerFactory tmf =
          TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
      // Initialise the TMF as you normally would, for example:
      // tmf.in
      try {
        tmf.init((KeyStore) null);
      } catch (KeyStoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }

      TrustManager[] trustManagers = tmf.getTrustManagers();
      final X509TrustManager origTrustmanager = (X509TrustManager) trustManagers[0];

      TrustManager[] wrappedTrustManagers =
          new TrustManager[] {
            new X509TrustManager() {

              @Override
              public void checkClientTrusted(X509Certificate[] chain, String authType)
                  throws CertificateException {
                origTrustmanager.checkClientTrusted(chain, authType);
              }

              @Override
              public void checkServerTrusted(X509Certificate[] chain, String authType)
                  throws CertificateException {
                try {
                  origTrustmanager.checkServerTrusted(chain, authType);
                } catch (CertificateException e) {
                  if (e.getCause() instanceof CertPathValidatorException) {
                    String sha;
                    try {
                      MessageDigest sha1 = MessageDigest.getInstance("SHA1");
                      sha1.update(chain[0].getEncoded());
                      sha = CryptoHelper.bytesToHex(sha1.digest());
                      if (!sha.equals(account.getSSLFingerprint())) {
                        changeStatus(Account.STATUS_TLS_ERROR);
                        if (tlsListener != null) {
                          tlsListener.onTLSExceptionReceived(sha, account);
                        }
                        throw new CertificateException();
                      }
                    } catch (NoSuchAlgorithmException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                    }
                  } else {
                    throw new CertificateException();
                  }
                }
              }

              @Override
              public X509Certificate[] getAcceptedIssuers() {
                return origTrustmanager.getAcceptedIssuers();
              }
            }
          };
      sc.init(null, wrappedTrustManagers, null);
      SSLSocketFactory factory = sc.getSocketFactory();
      SSLSocket sslSocket =
          (SSLSocket)
              factory.createSocket(
                  socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
      tagReader.setInputStream(sslSocket.getInputStream());
      tagWriter.setOutputStream(sslSocket.getOutputStream());
      sendStartStream();
      Log.d(LOGTAG, account.getJid() + ": TLS connection established");
      processStream(tagReader.readTag());
      sslSocket.close();
    } catch (NoSuchAlgorithmException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (KeyManagementException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
예제 #3
0
 protected void connect() {
   Log.d(LOGTAG, account.getJid() + ": connecting");
   lastConnect = SystemClock.elapsedRealtime();
   try {
     shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER);
     tagReader = new XmlReader(wakeLock);
     tagWriter = new TagWriter();
     packetCallbacks.clear();
     this.changeStatus(Account.STATUS_CONNECTING);
     Bundle namePort = DNSHelper.getSRVRecord(account.getServer());
     if ("timeout".equals(namePort.getString("error"))) {
       Log.d(LOGTAG, account.getJid() + ": dns timeout");
       this.changeStatus(Account.STATUS_OFFLINE);
       return;
     }
     String srvRecordServer = namePort.getString("name");
     String srvIpServer = namePort.getString("ipv4");
     int srvRecordPort = namePort.getInt("port");
     if (srvRecordServer != null) {
       if (srvIpServer != null) {
         Log.d(
             LOGTAG,
             account.getJid()
                 + ": using values from dns "
                 + srvRecordServer
                 + "["
                 + srvIpServer
                 + "]:"
                 + srvRecordPort);
         socket = new Socket(srvIpServer, srvRecordPort);
       } else {
         Log.d(
             LOGTAG,
             account.getJid()
                 + ": using values from dns "
                 + srvRecordServer
                 + ":"
                 + srvRecordPort);
         socket = new Socket(srvRecordServer, srvRecordPort);
       }
     } else {
       socket = new Socket(account.getServer(), 5222);
     }
     OutputStream out = socket.getOutputStream();
     tagWriter.setOutputStream(out);
     InputStream in = socket.getInputStream();
     tagReader.setInputStream(in);
     tagWriter.beginDocument();
     sendStartStream();
     Tag nextTag;
     while ((nextTag = tagReader.readTag()) != null) {
       if (nextTag.isStart("stream")) {
         processStream(nextTag);
         break;
       } else {
         Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName());
         return;
       }
     }
     if (socket.isConnected()) {
       socket.close();
     }
   } catch (UnknownHostException e) {
     this.changeStatus(Account.STATUS_SERVER_NOT_FOUND);
     if (wakeLock.isHeld()) {
       wakeLock.release();
     }
     return;
   } catch (IOException e) {
     if (account.getStatus() != Account.STATUS_TLS_ERROR) {
       this.changeStatus(Account.STATUS_OFFLINE);
     }
     if (wakeLock.isHeld()) {
       wakeLock.release();
     }
     return;
   } catch (NoSuchAlgorithmException e) {
     this.changeStatus(Account.STATUS_OFFLINE);
     Log.d(LOGTAG, "compression exception " + e.getMessage());
     if (wakeLock.isHeld()) {
       wakeLock.release();
     }
     return;
   } catch (XmlPullParserException e) {
     this.changeStatus(Account.STATUS_OFFLINE);
     Log.d(LOGTAG, "xml exception " + e.getMessage());
     if (wakeLock.isHeld()) {
       wakeLock.release();
     }
     return;
   }
 }