【发布时间】:2017-08-27 21:26:03
【问题描述】:
我正在尝试在我正在编写的服务器和我不太了解的设备之间建立 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 代码的不同之处?或我可以使用的任何其他方向。
谢谢!
【问题讨论】: