2016-09-28 115 views
0

我为我的域名购买了证书(来自comodo),它包含3个文件:私钥,证书和ca-bundle文件。如何将证书链添加到netty服务器?

我将它添加到我的apache服务器/ mod_ssl中,它完美地工作。我可以轻松配置,因为它有3个配置参数:SSLCertificateFile,SSLCertificateKeyFile,SSLCertificateChainFile。

我想添加到我的其他Java应用程序,使用Netty作为网络库。 以下是我的服务器代码:

SslContext sslContext = null; 
File cert = new File(Config.CERT_FILE_PATH); 
File key = new File(Config.KEY_FILE_PATH); 
sslContext = SslContextBuilder.forServer(cert, key).build(); 
ServerBootstrap b = new ServerBootstrap(); 
b.group(bossGroup, workerGroup) 
     .channel(NioServerSocketChannel.class) 
     .localAddress(port) 
     .childOption(ChannelOption.TCP_NODELAY, true) 
     .childOption(ChannelOption.SO_KEEPALIVE, true) 
     .childHandler(new WebServerInitializer(sslCtx)); 
Channel ch = b.bind().sync().channel(); 

随着Config.CERT_FILE_PATH是我的证书文件路径,Config.KEY_FILE_PATH是我的私有密钥文件的路径。

但问题是一些浏览器说证书错误(更多细节是在Mac OS浏览器,浏览器不信任我的证书)。

我做了一些研究,发现我的netty http服务器没有发送完整的证书链给客户端(包含我的证书的中间证书),它只发送我的证书(我通过KeyStore Explorer工具检查)。

我尝试将Config.CERT_FILE_PATH值替换为ca-bundle文件的路径或将证书文件的值附加到ca-bundle文件并使用新文件,但仍然不成功。抛出异常:

io.netty.handler.codec.DecoderException: javax.net.ssl.SSLException: Received fatal alert: decrypt_error 
     at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:358) 
     at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:230) 
     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) 
     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) 
     at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) 
     at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) 
     at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) 
     at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) 
     at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) 
     at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) 
     at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112) 
     at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) 
     at java.lang.Thread.run(Thread.java:745) 
Caused by: javax.net.ssl.SSLException: Received fatal alert: decrypt_error 
     at sun.security.ssl.Alerts.getSSLException(Alerts.java:208) 
     at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1646) 
     at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1614) 
     at sun.security.ssl.SSLEngineImpl.recvAlert(SSLEngineImpl.java:1780) 
     at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:1075) 
     at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:901) 
     at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:775) 
     at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624) 
     at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1135) 
     at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1025) 
     at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:965) 
     at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:327) 
     ... 12 more 

如何将我的证书与ca-bundle正确添加到netty服务器?

为什么其他浏览器(除了基于铬的浏览器在Mac上的Windows和Mac上的所有浏览器)仍然与我的错误服务器一起工作?

回答

1

您究竟是如何连接文件的?如果你的网站是公开的,我建议访问What's My Chain Cert?并下载正确的链(保持“包括根证书”未选中)。使用此文件作为您的cert

至于为什么该网站可以在大多数浏览器中使用,而不是在某些情况下,see this answer

您可以使用SSL Labs来诊断任何证书和SSL相关问题。

+3

我以错误的顺序连接这些证书,正确的顺序是我的证书服务器在证书链之前 – Viet