如何检测 HTTPS 仅启用 SM2/SM3 套件?

解读

在国内金融、政务、运营商等强合规场景下,国密算法(SM2/SM3/SM4) 已写入等保 2.0、GMT 0005-2012、GMT 0024-2014 等标准,要求对外服务仅开启国密 TLS 套件,禁用 RSA/ECDHE 等国际算法。面试官想验证两点:

  1. 你是否理解“仅启用 SM2/SM3”在 TLS 层面的真正含义——不是简单看证书 OID,而是看握手阶段协商出的 Cipher Suite;
  2. 你是否能在 Cypress 测试流水线中低成本、可落地、可回归地自动化验证这一点,而非手动 openssl s_client 一把梭。

知识点

  1. 国密 TLS 套件编号:

    • ECC_SM4_GCM_SM3 = 0xe013
    • ECC_SM4_CBC_SM3 = 0xe011
    • ECDHE_SM4_GCM_SM3 = 0xe053
    • ECDHE_SM4_CBC_SM3 = 0xe051
      以上套件在ServerHello中返回,若握手成功则证明服务端同意使用国密算法。
  2. 浏览器暴露通道:
    Chrome/Edge 国密版(国密双证书)在握手结束后,通过 window.performance.getEntriesByType('navigation')[0].nextHopProtocol 可拿到 h2 或 http/1.1,但不会直接暴露 Cipher Suite。想拿到套件,必须借助:

    • 国密版 Chrome 的私有扩展:chrome.loadTimes().connectionInfo 已废弃;
    • 抓包层面:利用 Cypress 的 cy.intercept() + 代理层 把 TLS 事件导出;
    • Node 侧脚本:在 Cypress 的 plugins/index.js 里拉起 BoringSSL-GM 或 TongSuo(原 BabaSSL) 的 s_client,以 GMSSL 指令 发起握手,解析 ServerHello 中的 Cipher Suite。
  3. Cypress 介入点:

    • 把检测脚本封装成 cy.task('assertGMTLS', { host, port });
    • 在 before() 钩子中调用,断言返回的套件必须在白名单 [0xe013, 0xe011, 0xe053, 0xe051] 内,且不出现任何国际套件;
    • 将结果写入 mochawesome 报告,供审计留痕。
  4. 合规细节:

    • 若服务端同时开启国密网关与国际网关,需确保 DNS 指向国密 VIP,防止“算法降级”;
    • 部分负载均衡会剥离 TLS 再加密,此时要在真实边缘节点上测,而不是 SLB 公网 IP。

答案

步骤一:在 cypress/plugins/index.js 中注册 Node 任务

const { exec } = require('child_process');
const path = require('path');

module.exports = (on, config) => {
  on('task', {
    assertGMTLS({ host, port = 443 }) {
      return new Promise((resolve) => {
        // 使用 TongSuo 的 s_client 工具,编译时开启 GM 支持
        const cmd = `apps/s_client -connect ${host}:${port} -cipher SM2-WITH-SM4-SM3 -gmtls`;
        exec(cmd, { cwd: '/opt/Tongsuo' }, (err, stdout, stderr) => {
          if (err) return resolve({ ok: false, reason: err.message });
          const reg = /Protocol\s*:\s*GMTLS\s+.*\s+Cipher\s*:\s*(0x[0-9a-f]+)/i;
          const m = stdout.match(reg);
          if (!m) return resolve({ ok: false, reason: '未协商出国密套件' });
          const suite = parseInt(m[1], 16);
          const whiteList = [0xe013, 0xe011, 0xe053, 0xe051];
          if (!whiteList.includes(suite))
            return resolve({ ok: false, reason: `协商出非预期套件 ${m[1]}` });
          // 再反向验证:禁用国密,强制 RSA,应握手失败
          const cmd2 = `apps/s_client -connect ${host}:${port} -cipher ECDHE-RSA-AES128-GCM-SHA256`;
          exec(cmd2, { cwd: '/opt/Tongsuo' }, (err2) => {
            if (!err2) return resolve({ ok: false, reason: '国际算法仍可用,未仅启用国密' });
            resolve({ ok: true, suite });
          });
        });
      });
    }
  });
};

步骤二:在测试用例中调用

describe('国密合规检测', () => {
  it('应仅启用 SM2/SM3 套件', () => {
    cy.task('assertGMTLS', { host: 'gm.example.cn' }).then((res) => {
      expect(res.ok).to.eq(true);
      cy.log(`协商套件: 0x${res.suite.toString(16)}`);
    });
  });
});

步骤三:流水线集成
把 TongSuo 编译产物缓存到 CI 镜像,在 GitLab-CI 的 cypress-e2e job 中执行,失败即阻断版本上车,满足央行《金融数据安全 数据安全分级指南》中“自动化验证”要求。

拓展思考

  1. 双向国密:若服务端同时要求客户端证书,需在 s_client 加 -cert user.sm2.sig.pem -key user.sm2.sig.pem -cert2 user.sm2.enc.pem -key2 user.sm2.enc.pem,并在 Cypress 中通过 cy.readFile() 把证书注入 Node 任务,实现双证书双向国密的完整自动化。

  2. 性能基线:国密算法在软算场景下 QPS 下降 30% 左右,可在 Cypress 同一套代码里用 cy.request() 循环 100 次,记录 response.duration,作为性能回归基线,防止后续升级 TongSuo 版本带来意外损耗。

  3. 国密 FIDO:未来国密 FIDO2 会进入浏览器,Cypress 需预留 WebAuthn 虚拟 authenticator 的扩展点,提前在 support/index.js 中注册 window.navigator.credentials.sm2.create() 的 polyfill,保证端到端登录链路也能在国密环境下跑通。