我正在尝试在我正在编写的服务器和我不太了解的设备之间建立SSL连接,除了它可能嵌入了公钥(因为你会看到)下面我有私钥)。
我所知道的是,我有一个服务器的python代码,该设备能够连接,发送和接收消息。
除了python代码之外,我还收到了证书(PEM格式)和我在套接字初始化期间提供的私钥。 python代码如下:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
sock.bind((HOST, 34567))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
sslSocket = ssl.wrap_socket(sock , server_side=True, certfile="device.crt", keyfile="device.key", ssl_version=ssl.PROTOCOL_TLSv1_2)
#Start listening on socket
sslSocket.listen(10)
print 'Socket now listening'
#wait to accept a connection - blocking call
print 'wait to accept a connection'
conn, addr = sslSocket.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is
the tuple of arguments to the function.
start_new_thread(receiveThread ,(conn,))
time.sleep(1)
start_new_thread(clientThread ,(conn,))
while 1:
time.sleep(1)
if exitNow:
print 'Exit'
break
sock.close()
print 'exiting'
sys.exit()
我对套接字没有经验,但根据我在Java中读到的套接字,我必须初始化并设置密钥库和信任库来创建SSL套接字。
我所做的是(根据this SO answer)
将我的证书和密钥转换为PKCS12证书 -
openssl pkcs12 -export -in device.crt -inkey device.key -certfile device.crt -name "someName" -out device.p12
创建密钥库并将新生成的证书导入其中 -
keytool -importkeystore -deststorepass 123456 -destkeypass 123456 -destkeystore keystore.jks -srckeystore device.p12 -srcstoretype PKCS12 -srcstorepass 1234 -alias someName
再次复制该密钥库并将其称为信任库(不确定这是正确的做法)。 然后我使用生成的密钥库来使用以下代码初始化套接字:(请注意我使用Spring Integration,因为我的最终目标是尝试启动应用程序,它将在接收REST API调用后在套接字上发送消息):
@EnableIntegration
@IntegrationComponentScan
@Configuration
public class SocketConfiguration implements
ApplicationListener<TcpConnectionEvent> {
private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
@Bean
public AbstractServerConnectionFactory AbstractServerConnectionFactory() {
TcpNetServerConnectionFactory tcpNetServerConnectionFactory = new TcpNetServerConnectionFactory(34567);
DefaultTcpNetSSLSocketFactorySupport tcpNetSSLSocketFactory = tcpSocketFactorySupport();
tcpNetServerConnectionFactory.setTcpSocketFactorySupport(tcpNetSSLSocketFactory);
return tcpNetServerConnectionFactory;
}
@Bean
public DefaultTcpNetSSLSocketFactorySupport tcpSocketFactorySupport() {
TcpSSLContextSupport sslContextSupport = new DefaultTcpSSLContextSupport("keystore.jks",
"trustStore.jks", "123456", "123456");
DefaultTcpNetSSLSocketFactorySupport tcpSocketFactorySupport =
new DefaultTcpNetSSLSocketFactorySupport(sslContextSupport);
return tcpSocketFactorySupport;
}
@Bean
public TcpInboundGateway TcpInboundGateway(AbstractServerConnectionFactory connectionFactory) {
TcpInboundGateway inGate = new TcpInboundGateway();
inGate.setConnectionFactory(connectionFactory);
inGate.setRequestChannel(getMessageChannel());
return inGate;
}
@Bean
public MessageChannel getMessageChannel() {
return new DirectChannel();
}
@MessageEndpoint
public class Echo {
@Transformer(inputChannel = "getMessageChannel")
public String convert(byte[] bytes) throws Exception {
return new String(bytes);
}
}
private static ConcurrentHashMap<String, TcpConnection> tcpConnections = new ConcurrentHashMap<>();
@Override
public void onApplicationEvent(TcpConnectionEvent tcpEvent) {
TcpConnection source = (TcpConnection) tcpEvent.getSource();
if (tcpEvent instanceof TcpConnectionOpenEvent) {
log.info("Socket Opened " + source.getConnectionId());
tcpConnections.put(tcpEvent.getConnectionId(), source);
if (!authorizeIncomingConnection(source.getSocketInfo())) {
log.warn("Socket Rejected " + source.getConnectionId());
source.close();
}
} else if (tcpEvent instanceof TcpConnectionCloseEvent) {
log.info("Socket Closed " + source.getConnectionId());
tcpConnections.remove(source.getConnectionId());
}
}
private boolean authorizeIncomingConnection(SocketInfo socketInfo) {
//Authorization Logic , Like Ip,Mac Address WhiteList or anyThing else !
return (System.currentTimeMillis() / 1000) % 2 == 0;
}
我认为套接字是成功创建的,因为在我的Windows机器上运行netstat显示端口40003是由java进程占用的(当我终止进程时不是这样。)
现在唯一的问题是设备仍然无法与我的套接字连接。 不幸的是,这是一个封闭的设备,我没有信息,无法调试它,了解发生了什么以及为什么它无法连接(也没有从中获取任何日志)。 我唯一的参考是 - 当我在运行java代码的同一台机器上运行附加的Python代码时,相同的设备能够连接到套接字(显然不在一起)。
请指点我的java代码与python代码不同的地方?或者我可以使用的任何其他方向。
谢谢!
答案 0 :(得分:0)
原来问题出在我的盒子防火墙上。 python在防火墙中有例外(我的假设是这些异常是在安装过程中插入但不太确定),而java没有这些异常,所以java进程无法&#34;暴露&#34 ;这个对外界的插座