死磕GMSSL通信-java/Netty系列(三)

死磕GMSSL通信-java/Netty系列(三)

接着上次的博客继续完善,上次其实只是客户端的改造,这次把服务端的也补上,netty集成GMSSL实现GMServer

1、netty_tcnative c代码改造,这个是客户端和服务端都需要都该的地方

sslcontext.c文件

TCN_IMPLEMENT_CALL(jlong, SSLContext, make)(TCN_STDARGS, jint protocol, jint mode)方法

c 复制代码
#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_RENEGOTIATIONSSL_OP_LEGACY_SERVER_CONNECT 这两个选项。这样做是为了增强 SSL/TLS 通信的安全性,避免因兼容性设置而引入潜在的安全风险。 这个地方注意,必须要注释掉,不然会提示加密套件有漏洞之类的。

c 复制代码
    //SSL_CTX_clear_options(c->ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION | SSL_OP_LEGACY_SERVER_CONNECT);

GmSSL 中对应的国密算法 SM2 使用的是名为 SM2 P-256V1 的椭圆曲线,其参数与国际标准中的曲线不同,专门为国密算法设计,所以需要再netty里边把这个加上,否则会提示加密套件不支持之类的错误提示,再OpenSsl.java这个文件

复制代码
private static final String[] DEFAULT_NAMED_GROUPS = { "x25519", "secp256r1", "secp384r1", "secp521r1","sm2p256v1" };

使用也很简单,其他就和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();

基本代码和流程已经介绍完了,稍后我会把源码上传的github上,方便大家编译和下载

后续工作:

1、继续实现其他语言的GMSSL通信

2、代码上传到github

参考博客

新手入坑GMSSL(一)Windows下编译GMSSL并生成CA证书_gmssl证书制作windows-CSDN博客

GmSSL编程实现gmtls协议C/S通信(BIO版本)_tassl_demo/mk_tls_cert 下的 sm2certgen.sh-CSDN博客

相关推荐
桦说编程9 分钟前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅2 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者3 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺3 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart4 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP5 小时前
MyBatis-mybatis入门与增删改查
java
孟陬8 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌8 小时前
一站式了解四种限流算法
java·后端·go
华仔啊9 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java