简体   繁体   English

SSL服务器套接字和具有已知证书的握手

[英]SSL server socket and handshake with known certificate

I am new to SSl server sockets. 我是SSl服务器套接字的新手。 All I am tying to do is to read data over SSL. 我想做的就是通过SSL读取数据。 My application listens on port 8000. Please give me few steps on how I can do this. 我的应用程序在端口8000上侦听。请给我几个步骤,以了解如何执行此操作。 When I have a certificate (on my disc), how can I establish the SSL server socket and read from client ? 拥有证书(在光盘上)后,如何建立SSL服务器套接字并从客户端读取?

Here are my steps 这是我的步骤

1) reading server.crt from file and making X509Certificate (has public certificate and private key) 2) Getting instance of JKS keystore 1)从文件中读取server.crt并制作X509Certificate(具有公共证书和私钥)2)获取JKS密钥库的实例

3) Get instance of context 3)获取上下文实例

4) create server socket over the port (8000) 4)通过端口创建服务器套接字(8000)

InputStream in = new DataInputStream(new FileInputStream(new File("server.crt")));
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(null, null);

CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(in);
in.close();

ks.setCertificateEntry("dts", cert);

char[] newpass = "password".toCharArray();
String name = "mykeystore.ks";
FileOutputStream output = new FileOutputStream(name);
ks.store(output, newpass);

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, "password".toCharArray());




try{

    System.setProperty("javax.net.ssl.keyStore","mykeystore.ks");
    System.setProperty("javax.net.ssl.keyStorePassword","password");
    System.setProperty("javax.net.debug","all");



    SSLContext context = SSLContext.getInstance("TLSv1.2");
    context.init(kmf.getKeyManagers(), null, null);
    SSLServerSocketFactory sslServerSocketfactory = context.getServerSocketFactory();
    SSLServerSocket sslServerSocket = (SSLServerSocket)sslServerSocketfactory.createServerSocket(8000);
    SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();  


    InputStream dataIN = sslSocket.getInputStream();

    byte[] hello = new byte[20];

    dataIN.read(hello);
    System.out.println(new String(hello));

    dataIN.close();

} catch (IOException e){
    e.printStackTrace();
}

I got the answer for my question, I did research on how to setup my own keystore with self signed certificate. 我得到了我问题的答案,我研究了如何使用自签名证书设置自己的密钥库。 This way helped me. 这样对我有帮助。

ping me for a detailed solutions. 为我提供详细的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM