死磕GMSSL通信-java/Netty系列(三)
接著上次的博客繼續(xù)完善,上次其實(shí)只是客戶端的改造,這次把服務(wù)端的也補(bǔ)上,netty集成GMSSL實(shí)現(xiàn)GMServer
1、netty_tcnative c代碼改造,這個(gè)是客戶端和服務(wù)端都需要都該的地方
sslcontext.c文件
TCN_IMPLEMENT_CALL(jlong, SSLContext, make)(TCN_STDARGS, jint protocol, jint mode)方法
#elif OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
// TODO this is very hacky as io.netty.handler.ssl.OpenSsl#doesSupportProtocol also uses this method to test for supported protocols. Furthermore
// in OpenSSL 1.1.0 the way protocols are enable/disabled changes
// (SSL_OP_NO_SSLv3,... are deprecated and you should use: https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_set_max_proto_version.html)
if (mode == SSL_MODE_CLIENT) {
ctx = SSL_CTX_new(GMTLS_client_method());//修改
} else if (mode == SSL_MODE_SERVER) {
ctx = SSL_CTX_new(GMTLS_server_method());//修改
} else {
ctx = SSL_CTX_new(TLS_method());
}
客戶端必須注釋掉這行代碼SL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
和 SSL_OP_LEGACY_SERVER_CONNECT
這兩個(gè)選項(xiàng)。這樣做是為了增強(qiáng) SSL/TLS 通信的安全性,避免因兼容性設(shè)置而引入潛在的安全風(fēng)險(xiǎn)。 這個(gè)地方注意,必須要注釋掉,不然會(huì)提示加密套件有漏洞之類的。
//SSL_CTX_clear_options(c->ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION | SSL_OP_LEGACY_SERVER_CONNECT);
GmSSL 中對(duì)應(yīng)的國(guó)密算法 SM2 使用的是名為 SM2 P-256V1 的橢圓曲線,其參數(shù)與國(guó)際標(biāo)準(zhǔn)中的曲線不同,專門為國(guó)密算法設(shè)計(jì),所以需要再netty里邊把這個(gè)加上,否則會(huì)提示加密套件不支持之類的錯(cuò)誤提示,再OpenSsl.java這個(gè)文件
private static final String[] DEFAULT_NAMED_GROUPS = { "x25519", "secp256r1", "secp384r1", "secp521r1","sm2p256v1" };
使用也很簡(jiǎn)單,其他就和netty的流程一樣了
final SslContext sslCtx = SslContextGMBuilder.forServer(encCertPath, encKeyPath,
signCertPath, signKeyPath,
caCertPath).protocols()
.ciphers(Arrays.asList(
"TLCP_SM2-WITH-SMS4-SM3"
))
.clientAuth(ClientAuth.NONE)
.build();
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
final EchoServerHandler serverHandler = new EchoServerHandler();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(sslCtx.newHandler(ch.alloc()));
p.addLast(serverHandler);
}
});
// Start the server.
ChannelFuture f = b.bind(8999).sync();
基本代碼和流程已經(jīng)介紹完了,稍后我會(huì)把源碼上傳的github上,方便大家編譯和下載
后續(xù)工作:
1、繼續(xù)實(shí)現(xiàn)其他語(yǔ)言的GMSSL通信
2、代碼上傳到github
參考博客
新手入坑GMSSL(一)Windows下編譯GMSSL并生成CA證書_gmssl證書制作windows-CSDN博客文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-858343.html
GmSSL編程實(shí)現(xiàn)gmtls協(xié)議C/S通信(BIO版本)_tassl_demo/mk_tls_cert 下的 sm2certgen.sh-CSDN博客文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-858343.html
到了這里,關(guān)于死磕GMSSL通信-java/Netty系列(三)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!