• 死磕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)方法

    #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());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    客户端必须注释掉这行代码SL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATIONSSL_OP_LEGACY_SERVER_CONNECT 这两个选项。这样做是为了增强 SSL/TLS 通信的安全性,避免因兼容性设置而引入潜在的安全风险。 这个地方注意,必须要注释掉,不然会提示加密套件有漏洞之类的。

        //SSL_CTX_clear_options(c->ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION | SSL_OP_LEGACY_SERVER_CONNECT);
    
    • 1

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

    private static final String[] DEFAULT_NAMED_GROUPS = { "x25519", "secp256r1", "secp384r1", "secp521r1","sm2p256v1" };
    
    • 1

    使用也很简单,其他就和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() {
    
                            @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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    基本代码和流程已经介绍完了,稍后我会把源码上传的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博客

  • 相关阅读:
    苹果电脑快捷键
    Volcano:在离线作业混部管理平台,实现智能资源管理和作业调度
    手写哈希表
    OpenJudge NOI 2.1 6188:比饭量
    浅谈青岛啤酒厂事件—论智能视频监控的重要性和必要性
    PowerDesigner 16 导入表结构与生成 Html
    ARM 的 的 AMBA 总线
    如何解决golang开发中遇到的报错:checksum mismatch downloaded
    【小海实习日记】golang-iris框架学习笔记
    墙裂推荐:GitHub 上这个开源项目可以让你在短短几分钟之内了解一门技术
  • 原文地址:https://blog.csdn.net/qq_36577699/article/details/137915453