我开发了一个与websockets兼容的套接字服务器,用于通信用java和web制作的应用程序。但最新的Chrome和Mozilla更新不再允许与websockets的不安全连接。然后,我被迫解密服务器收到的字节,然后再继续握手和协议的其余部分 https://www.rfc-editor.org/rfc/rfc6455
我取得了以下成就:
-
从 CA 签名的证书中获取公钥。还有我的服务器的私钥
-
使用Java的密码类,我设法使用这些密钥来加密和解密测试字符串
但我仍然无法做的是在继续握手之前解密我从 websocket 客户端收到的字节。
我希望你能帮助我。谢谢
我收到的错误:数据不得超过 256 字节
解决了!解密被委托给SSLSocket类。如果有人想在这里做,步骤。
将 CA 颁发的证书和私钥导出到 p12 文件
openssl pkcs12 -export -in certificate/path/certificate.crt -inkey /path/privatekey/private.key -out filep12.p12 -name your_domain -CAfile /path/ca.crt -caname your_ca
Java 密钥存储
keytool -genkey -alias your_alias -keyalg RSA -keystore name_store.jks -keysize 2048
输入密码(your_password)并确认后
keytool -importkeystore -srckeystore name_store.jks -destkeystore name_store.jks -deststoretype pkcs12 -srcstorepass your_password
keytool -delete -alias your_alias -keystore name_store.jks -storepass your_password
keytool -importkeystore -deststorepass your_password -destkeypass your_password -destkeystore name_store.jks -srckeystore filep12.p12 -srcstoretype PKCS12 -srcstorepass your_password -alias your_domain
your_alias不得与your_domain相同或相似,则要求在每个步骤中输入密码(your_password)始终相同,以便在解密时不会出现填充错误
java 中的类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
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 java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
public class SServidor {
public SServidor(){
try {
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
File keystrorefile = new File("/path/name_store.jks");
System.out.println(keystrorefile.getAbsolutePath());
InputStream keystoreStream = new FileInputStream(keystrorefile);
char[] passphrase="your_password".toCharArray();
keystore.load(keystoreStream, passphrase);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keystore, passphrase);
makeSSLSocketFactory(keystore, keyManagerFactory);
} catch (KeyStoreException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (CertificateException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnrecoverableKeyException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void log(Object msj){
System.out.println(msj.toString());
}
public void makeSSLSocketFactory(KeyStore loadedKeyStore, KeyManagerFactory key){
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(loadedKeyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(key.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
SSLServerSocketFactory sslssf = ctx.getServerSocketFactory();
ServerSocket conexion = sslssf.createServerSocket(your_port);
SSLSocket cliente=(SSLSocket) conexion.accept();
cliente.startHandshake();
InputStream in = cliente.getInputStream();
OutputStream out = cliente.getOutputStream();
int byte_recibido=-1;
while(cliente.isConnected() && (byte_recibido=in.read())>-1){
Integer n=byte_recibido & 0xFF;
String s=new String(String.valueOf(Character.toChars(n)));
log(s);
}
out.close();
bin.close();
in.close();
cliente.close();
conexion.close();
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (KeyStoreException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (KeyManagementException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SServidor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}`
其他东西,与 websocket 的连接应该是这样的 wss://your_domain:port 不得在 websocket url 中输入 IP 地址,必须使用在 CA 颁发的证书中注册的域来完成
使用解密的字节,我可以继续使用RFC6455协议。这只是我所做的测试,显然对于套接字应用程序,此外,有必要异步处理连接到服务器的客户端。我用ExecutorService类来做这件事,但这是另一个话题