Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SSLPeerUnverifiedException:未使用本地主机上netbeans 8.2中的自签名证书对对等方进行身份验证_Java_Ssl_Netbeans_Self Signed - Fatal编程技术网

Java SSLPeerUnverifiedException:未使用本地主机上netbeans 8.2中的自签名证书对对等方进行身份验证

Java SSLPeerUnverifiedException:未使用本地主机上netbeans 8.2中的自签名证书对对等方进行身份验证,java,ssl,netbeans,self-signed,Java,Ssl,Netbeans,Self Signed,我已经用谷歌搜索了几个小时这个错误,到目前为止没有任何结果,我不知道为什么…我生成的证书是这样的: 客户: keytool-genkey-alias babicaprivate-keystore babica.private-storetype JKS-keyalg rsa-dname“CN=babica”-storepass babicapwd-keypass babicapwd-validity 365 keytool-export-alias babicaprivate-keystore

我已经用谷歌搜索了几个小时这个错误,到目前为止没有任何结果,我不知道为什么…我生成的证书是这样的:

客户:

keytool-genkey-alias babicaprivate-keystore babica.private-storetype JKS-keyalg rsa-dname“CN=babica”-storepass babicapwd-keypass babicapwd-validity 365
keytool-export-alias babicaprivate-keystore babica.private-file temp.key-storepass babicapwd
keytool-import-noprompt-alias babicapublic-keystore client.public-file temp.key-storepass public
删除温度键

服务器:

keytool-genkey-alias serverprivate-keystore server.private-storetype JKS-keyalg rsa-dname“CN=localhost”-storepass serverpwd-keypass serverpwd-validity 365
keytool-export-alias serverprivate-keystore server.private-file temp.key-storepass serverpwd
keytool-import-noprompt-alias serverpublic-keystore server.public-file temp.key-storepass public
删除温度键

这为我提供了服务器和客户端的公共和私有证书

然后,我启动服务器和客户机,当我尝试登录服务器并尝试使用从服务器上的客户机读取“公共名称”时

Socket newClientSocket=ss.accept();
((SSLSocket)newClientSocket).startAndShake();
字符串用户名=((SSLSocket)newClientSocket).getSession().getPeerPrincipal().getName()

这是用户名获取给我的错误,我不知道为什么。我如何制作证书有问题吗

以下是我的服务器完整代码:

import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.*;
import java.security.*;

public class ChatServer {

protected List<Socket> clients = new ArrayList<Socket>(); // list of clients

private String[][] portUserPair = new String[10][2];
int i = 0;

private String passphrase = "serverpwd";
private int port = 1234;

public static void main(String[] args) throws Exception {
    new ChatServer();
}

public ChatServer() throws Exception {

    KeyStore clientKeyStore = KeyStore.getInstance("JKS");
    clientKeyStore.load(new FileInputStream("client.public"), "public".toCharArray());

    KeyStore serverKeyStore = KeyStore.getInstance("JKS");
    serverKeyStore.load(new FileInputStream("server.private"), passphrase.toCharArray());

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
    tmf.init(clientKeyStore);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(serverKeyStore, passphrase.toCharArray());
    SSLContext sslContext = SSLContext.getInstance("TLS");
    SSLContext.setDefault(sslContext);
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), (new SecureRandom()));

    SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) factory.createServerSocket(port);
    ss.setNeedClientAuth(false);
    ss.setEnabledCipherSuites(new String[] {"TLS_RSA_WITH_AES_128_CBC_SHA"});


    // start listening for new connections
    System.out.println("[system] listening ...");
    try {
        while (true) {
            Socket newClientSocket = ss.accept(); // wait for a new client connection
            ((SSLSocket)newClientSocket).startHandshake();
            String username = ((SSLSocket) newClientSocket).getSession().getPeerPrincipal().getName();
            System.out.println("Established SSL connection with: " + username);
            synchronized(this) {
                portUserPair[i][0] = Integer.toString(newClientSocket.getPort());
                clients.add(newClientSocket); // add client to the list of clients
            }
            ChatServerConnector conn = new ChatServerConnector(this, newClientSocket); // create a new thread for communication with the new client
            conn.start(); // run the new thread
        }
    } catch (Exception e) {
        System.err.println("[error] Accept failed.");
        e.printStackTrace(System.err);
        System.exit(1);
    }

    // close socket
    System.out.println("[system] closing server socket ...");
    try {
        ss.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

// send a message to all clients connected to the server
public void sendToAllClients(String message) throws Exception {
    Iterator<Socket> i = clients.iterator();
    while (i.hasNext()) { // iterate through the client list
        Socket socket = (Socket) i.next(); // get the socket for communicating with this client
        try {
            DataOutputStream out = new DataOutputStream(socket.getOutputStream()); // create output stream for sending messages to the client
            out.writeUTF(message); // send message to the client
        } catch (Exception e) {
            System.err.println("[system] could not send message to a client");
            e.printStackTrace(System.err);
        }
    }
}

public void sendToAClient(String message, String userInfo) throws Exception {
    Socket userSocket = null;
    String userPort = "";
    Iterator<Socket> i = clients.iterator();
    for (int k = 0; k < portUserPair.length; k++) {
        if(portUserPair[k][1].equals(userInfo)) {
            userPort = portUserPair[k][0];
            break;
        }
    }

    System.out.println(userPort);

    while (i.hasNext()) { // iterate through the client list
        Socket socket = (Socket) i.next(); // get the socket for communicating with this client
        if(Integer.toString(socket.getPort()).equals(userPort)) {
            userSocket = socket;
            break;
        }
    }

    message = message.substring(userInfo.length()+2);

    try {
        DataOutputStream out = new DataOutputStream(userSocket.getOutputStream()); // create output stream for sending messages to the client
        out.writeUTF(message); // send message to the client
    } catch (Exception e) {
        System.err.println("[system] could not send message to the client");
        e.printStackTrace(System.err);
    }
}

public void removeClient(Socket socket) {
    synchronized(this) {
        clients.remove(socket);
    }
}

public void saveUserInfo(String userInfo) {
    portUserPair[i][1] = userInfo;
    System.out.println(portUserPair[i][0] + " : " + portUserPair[i][1]);
    i++;
}
}

class ChatServerConnector extends Thread {
private ChatServer server;
private Socket socket;

public ChatServerConnector(ChatServer server, Socket socket) {
    this.server = server;
    this.socket = socket;
}

public void run() {
    System.out.println("[system] connected with " + this.socket.getInetAddress().getHostName() + ":" + this.socket.getPort());

    DataInputStream in;
    try {
        in = new DataInputStream(this.socket.getInputStream()); // create input stream for listening for incoming messages
    } catch (IOException e) {
        System.err.println("[system] could not open input stream!");
        e.printStackTrace(System.err);
        this.server.removeClient(socket);
        return;
    }

    while (true) { // infinite loop in which this thread waits for incoming messages and processes them
        String msg_received;
        try {
            msg_received = in.readUTF(); // read the message from the client
        } catch (Exception e) {
            System.err.println("[system] there was a problem while reading message client on port " + this.socket.getPort());
            e.printStackTrace(System.err);
            this.server.removeClient(this.socket);
            return;
        }

        if (msg_received.length() == 0) // invalid message
            continue;

        String[] msg = msg_received.split(" ");
        System.out.println(msg_received);

        if (msg[1].equals("system")) {
//            try {
//                server.saveUserInfo(((SSLSocket) socket).getSession().getPeerPrincipal().getName());
//            } catch (SSLPeerUnverifiedException ex) {
//                Logger.getLogger(ChatServerConnector.class.getName()).log(Level.SEVERE, null, ex);
//            }
            continue;
        }

        System.out.println(msg_received); // print the incoming message in the console

        String msg_send = msg_received.toUpperCase();

        if (msg[3].charAt(0) == '/') {
            String[] privateMsg = msg[3].split(" ");
            try {
                this.server.sendToAClient(msg_send, privateMsg[0].replace("/", "")); // send message to the client
            } catch (Exception e) {
                System.err.println("[system] there was a problem while sending the message to the client");
                e.printStackTrace(System.err);
                continue;
            }
            continue;
        }

        try {
            this.server.sendToAllClients(msg_send); // send message to all clients
        } catch (Exception e) {
            System.err.println("[system] there was a problem while sending the message to all clients");
            e.printStackTrace(System.err);
            continue;
        }
    }
}
}
import java.io.*;
导入java.net。*;
导入java.util.*;
导入javax.net.ssl.*;
导入java.security.*;
公共类聊天服务器{
受保护的列表客户端=新ArrayList();//客户端列表
私有字符串[][]portUserPair=新字符串[10][2];
int i=0;
私有字符串密码短语=“serverpwd”;
专用int端口=1234;
公共静态void main(字符串[]args)引发异常{
新建聊天服务器();
}
public ChatServer()引发异常{
KeyStore clientKeyStore=KeyStore.getInstance(“JKS”);
load(新文件输入流(“client.public”),“public.tocharray());
KeyStore-serverKeyStore=KeyStore.getInstance(“JKS”);
load(新文件输入流(“server.private”),passphrase.toCharArray();
TrustManagerFactory tmf=TrustManagerFactory.getInstance(“SunX509”);
tmf.init(clientKeyStore);
KeyManagerFactory kmf=KeyManagerFactory.getInstance(“SunX509”);
init(serverKeyStore,passphrase.toCharArray());
SSLContext SSLContext=SSLContext.getInstance(“TLS”);
setDefault(SSLContext);
sslContext.init(kmf.getKeyManager(),tmf.getTrustManager(),(new SecureRandom());
SSLServerSocketFactory=(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket ss=(SSLServerSocket)工厂。createServerSocket(端口);
ss.setNeedClientAuth(假);
ss.setEnabledChipersuites(新字符串[]{“TLS_RSA_WITH_AES_128_CBC_SHA”});
//开始监听新的连接
System.out.println(“[System]侦听…”);
试一试{
while(true){
Socket newClientSocket=ss.accept();//等待新的客户端连接
((SSLSocket)newClientSocket).startAndShake();
字符串用户名=((SSLSocket)newClientSocket).getSession().getPeerPrincipal().getName();
System.out.println(“与:“+用户名”建立SSL连接);
已同步(此){
portUserPair[i][0]=Integer.toString(newClientSocket.getPort());
clients.add(newClientSocket);//将客户机添加到客户机列表中
}
ChatServerConnector conn=newchatserverconnector(这个,newClientSocket);//创建一个新线程,用于与新客户端通信
conn.start();//运行新线程
}
}捕获(例外e){
System.err.println(“[error]接受失败”);
e、 printStackTrace(System.err);
系统出口(1);
}
//闭合插座
System.out.println(“[System]正在关闭服务器套接字…”);
试一试{
ss.close();
}捕获(IOE异常){
e、 printStackTrace(System.err);
系统出口(1);
}
}
//向连接到服务器的所有客户端发送消息
public void sendToAllClients(字符串消息)引发异常{
迭代器i=clients.Iterator();
而(i.hasNext()){//遍历客户机列表
Socket Socket=(Socket)i.next();//获取用于与此客户端通信的套接字
试一试{
DataOutputStream out=新建DataOutputStream(socket.getOutputStream());//创建用于向客户端发送消息的输出流
out.writeUTF(message);//向客户端发送消息
}捕获(例外e){
System.err.println(“[System]无法向客户端发送消息”);
e、 printStackTrace(System.err);
}
}
}
public void sendToAClient(字符串消息、字符串userInfo)引发异常{
套接字userSocket=null;
字符串userPort=“”;
迭代器i=clients.Iterator();
for(int k=0;kimport java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.net.ssl.*;
import java.security.*;

public class ChatClient extends Thread {

    private String userInfo = "";

    private String passphrase = "pwd";
    private int port = 1234;

    public static void main(String[] args) throws Exception {
        new ChatClient();
    }

    public ChatClient() throws Exception {

        KeyStore serverKeyStore = KeyStore.getInstance("JKS");
        serverKeyStore.load(new FileInputStream("server.public"), "public".toCharArray());

        KeyStore clientKeyStore = KeyStore.getInstance("JKS");
        Scanner s = new Scanner(System.in);
        String username = s.nextLine();
        clientKeyStore.load(new FileInputStream(username + ".private"), (username + passphrase).toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(serverKeyStore);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(clientKeyStore, (username + passphrase).toCharArray());
        SSLContext sslContext = SSLContext.getInstance("TLS");
        SSLContext.setDefault(sslContext);
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());

        SSLSocketFactory sf = sslContext.getSocketFactory();
        SSLSocket socket = (SSLSocket) sf.createSocket("localhost", port);
        socket.setEnabledCipherSuites(new String[] {"TLS_RSA_WITH_AES_128_CBC_SHA"});
        socket.startHandshake();

        DataInputStream in = null;
        DataOutputStream out = null;

        // connect to the chat server
        try {
            System.out.println("[system] connecting to chat server ...");
            System.out.println("[system] you are signed in as: " + username);

            in = new DataInputStream(socket.getInputStream()); // create input stream for listening for incoming messages
            out = new DataOutputStream(socket.getOutputStream()); // create output stream for sending messages

            this.sendMessage(username, out, "system");

            System.out.println("[system] connected");
            System.out.println("[system] to send a private message type \"/<NameOfUser>\" and then type the message");

            ChatClientMessageReceiver message_receiver = new ChatClientMessageReceiver(in); // create a separate thread for listening to messages from the chat server
            message_receiver.start(); // run the new thread
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }

        // read from STDIN and send messages to the chat server
        BufferedReader std_in = new BufferedReader(new InputStreamReader(System.in));
        String userInput;
        while ((userInput = std_in.readLine()) != null) { // read a line from the console
            this.sendMessage(userInput, out, userInfo); // send the message to the chat server
        }

        // cleanup
        out.close();
        in.close();
        std_in.close();
        socket.close();
    }

    private void sendMessage(String message, DataOutputStream out, String userInfo) {
        message = "[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + userInfo + " : " + message;
        try {
            out.writeUTF(message); // send the message to the chat server
            out.flush(); // ensure the message has been sent
        } catch (IOException e) {
            System.err.println("[system] could not send message");
            e.printStackTrace(System.err);
        }
    }
}

// wait for messages from the chat server and print the out
class ChatClientMessageReceiver extends Thread {
    private DataInputStream in;

    public ChatClientMessageReceiver(DataInputStream in) {
        this.in = in;
    }

    public void run() {
        try {
            String message;
            while ((message = this.in.readUTF()) != null) { // read new message
                System.out.println(message); // print the message to the console
            }
        } catch (Exception e) {
            System.err.println("[system] could not read message");
            e.printStackTrace(System.err);
        }
    }
}
ss.setNeedClientAuth(false);