SSL连接被拒绝了

时间:2015-03-31 08:31:54

标签: java android ssl

我很擅长使用安全连接,而我当前的项目要求我能够安全地将数据从Android手机传递到远程服务器。我的服务器运行ubuntu。我一直在使用this walkthrough

中的代码

我做了一些小改动 在服务器代码中,所有信息都是硬编码的 所以

int socket=8080 (not my port number just using as example)
string keystore="path\to\my\keystore\file"

我使用portecle来创建我的ks和bks文件

在客户端我不得不采取主线程的网络,就像这样

InputStream keyin = view.getResources().openRawResource(R.raw.clientkey);
new signinthread(keyin).start();

并创建了一个包含线程和相应构造函数的类。同样,所有信息都是硬编码而不是由用户输入

int port = 8080;
String ip_address = "192.my.server.ip";

因此,当我最终获得代码并运行时没有崩溃错误,只是拒绝服务器连接

connect failed: ECONNREFUSED (Connection refused)failed to connect to
/192.my.server.ip (port 8080): connect failed: ECONNREFUSED (Connection refused)
03-31 01:13:21.272: W/System.err(7028): java.net.ConnectException: failed to connect to /192.my.server.ip (port 8080): connect failed: ECONNREFUSED (Connection refused)
03-31 01:13:21.282: W/System.err(7028):     at libcore.io.IoBridge.connect(IoBridge.java:114)
03-31 01:13:21.282: W/System.err(7028):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-31 01:13:21.282: W/System.err(7028):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
03-31 01:13:21.282: W/System.err(7028):     at java.net.Socket.startupSocket(Socket.java:566)
03-31 01:13:21.282: W/System.err(7028):     at java.net.Socket.tryAllAddresses(Socket.java:128)
03-31 01:13:21.282: W/System.err(7028):     at java.net.Socket.<init>(Socket.java:178)
03-31 01:13:21.282: W/System.err(7028):     at java.net.Socket.<init>(Socket.java:150)
03-31 01:13:21.282: W/System.err(7028):     at com.my.project.signinthread.run(signinthread.java:57)
03-31 01:13:21.282: W/System.err(7028): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
03-31 01:13:21.282: W/System.err(7028):     at libcore.io.Posix.connect(Native Method)
03-31 01:13:21.282: W/System.err(7028):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
03-31 01:13:21.282: W/System.err(7028):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
03-31 01:13:21.282: W/System.err(7028):     at libcore.io.IoBridge.connect(IoBridge.java:112)

第57行

socket = (SSLSocket)socketFactory.createSocket(new Socket(ip_address,port), ip_address, port, false);

其他一些信息。

当我使用java knock knock示例时,它在该端口上工作得很好,端口检查器会说端口是打开的。使用此代码,ubuntu表示端口正在侦听,但使用外部端口检查程序,它表示已关闭。

我已经把头撞到了墙上太长时间了,所以现在我问大量的公众堆栈溢出我是什么或出了什么问题。

因为要求

package com.my.project;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

import org.apache.http.conn.ssl.SSLSocketFactory;

import android.content.Context;
import android.util.Log;
import android.widget.TextView;

public class signinthread extends Thread
{
    Context context;
    InputStream keyin;
    public signinthread(InputStream frommain)
    {
        keyin=frommain;
        System.out.println("keyin loaded");
    }
    private TextView mSignInStatus;

    private static final String TAG  = "SignInActivity";
    private BufferedWriter out = null;
    private BufferedReader in = null;
    int port = 8080;
    String ip_address = "192.my.public.ip";
    private SSLSocket socket = null;
    private char keystorepass[] = "My Password".toCharArray();
    private char keypassword[] = "My Password"".toCharArray();
    public void run()
    {
        try
        {
            KeyStore ks = KeyStore.getInstance("BKS");
            //keyin = this.getResources().openRawResource(R.raw.bkskey);
            ks.load(keyin,keystorepass);
            SSLSocketFactory socketFactory = new SSLSocketFactory(ks);
            socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            socket = (SSLSocket)socketFactory.createSocket(new Socket(ip_address,port), ip_address, port, false);
            socket.startHandshake();
            printServerCertificate(socket);
            printSocketInfo(socket);
            out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String msgToServer="login";
            chat(msgToServer);
        } catch (UnknownHostException e) {
            Log.i(TAG,"Unknown host");
        } catch  (IOException e) {
            Log.i(TAG,"No I/O");
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (KeyStoreException e) {
            Log.i(TAG,"Keystore ks error");
        } catch (NoSuchAlgorithmException e) {
            Log.i(TAG,"No such algorithm for ks.load");
            e.printStackTrace();
        } catch (CertificateException e) {
            Log.i(TAG,"certificate missing");
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            Log.i(TAG,"unrecoverableKeyException");
            e.printStackTrace();
        } catch (KeyManagementException e) {
            Log.i(TAG,"key management exception");
            e.printStackTrace();
        }
    }
    private void printServerCertificate(SSLSocket socket) {
        try
        {
            java.security.cert.Certificate[] serverCerts = socket.getSession().getPeerCertificates();
            for (int i = 0; i < serverCerts.length; i++) {
                java.security.cert.Certificate myCert = serverCerts[i];
                Log.i(TAG,"====Certificate:" + (i+1) + "====");
                Log.i(TAG,"-Public Key-\n" + myCert.getPublicKey());
                Log.i(TAG,"-Certificate Type-\n " + myCert.getType());
                System.out.println();
            }
        } catch (SSLPeerUnverifiedException e) {
            Log.i(TAG,"Could not verify peer");
            e.printStackTrace();
            System.exit(-1);
        }
    }
    private void printSocketInfo(SSLSocket s) {
        Log.i(TAG,"Socket class: "+s.getClass());
        Log.i(TAG,"   Remote address = "+s.getInetAddress().toString());
        Log.i(TAG,"   Remote port = "+s.getPort());
        Log.i(TAG,"   Local socket address = "+s.getLocalSocketAddress().toString());
        Log.i(TAG,"   Local address = "+s.getLocalAddress().toString());
        Log.i(TAG,"   Local port = "+s.getLocalPort());
        Log.i(TAG,"   Need client authentication = "+s.getNeedClientAuth());
        SSLSession ss = s.getSession();
        Log.i(TAG,"   Cipher suite = "+ss.getCipherSuite());
        Log.i(TAG,"   Protocol = "+ss.getProtocol());
    }
    public void chat(String temp)
    {
        String message = temp;
        String line = "";
        // send id of the device to match with the image
        try {
            out.write(message+"\n");
            out.flush();
        } catch (IOException e2) {
            Log.i(TAG,"Read failed");
            System.exit(1);
        }
        // receive a ready command from the server
        try {
            line = in.readLine();
            mSignInStatus.setText("SERVER SAID: "+line);
            System.out.println("from Server:"+line);
            //Log.i(TAG,line);
        } catch (IOException e1) {
            Log.i(TAG,"Read failed");
            System.exit(1);
        }
    }
}

0 个答案:

没有答案