Skip to content

支持微信支付 V3 SM2/SM3 签名与验签 - #4151

Open
binarywang wants to merge 3 commits into
developfrom
copilot/v3
Open

binarywang wants to merge 3 commits into
developfrom
copilot/v3

Conversation

@binarywang

@binarywang binarywang commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

微信支付 API v3 增加国密签名支持,按官方规则使用 SM2-with-SM3、指定用户 ID,并通过 Wechatpay-Signature-Type 区分响应签名算法。

  • 请求签名:SM2 密钥使用 WECHATPAY2-SM2-WITH-SM3;RSA 签名保持原有行为。
  • 响应验签:根据响应签名类型选择 SM2 或 RSA 验签;未知类型拒绝。
  • 密钥解析:PEM 工具兼容解析 RSA 与 SM2/EC 密钥。
WxPayCredentials credentials =
  new WxPayCredentials(mchId, new PrivateKeySigner(serialNo, sm2PrivateKey));
// credentials.getSchema() 返回 WECHATPAY2-SM2-WITH-SM3

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@binarywang binarywang changed the title [WIP] Add support for national encryption algorithm in v3 payment interface 支持微信支付 V3 SM2/SM3 签名与验签 Nov 25, 2026
@binarywang
binarywang marked this pull request as ready for review November 26, 2026 02:31
Copilot AI balanced review requested due to automatic review settings November 26, 2026 02:31
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T02:32:57.559346Z bae282b Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bae282bbce

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +17 to +18
default boolean verify(String serialNumber, byte[] message, String signature, String signatureType) {
return verify(serialNumber, message, signature);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 在自动更新证书验签器中透传签名类型

当使用 VerifierBuilder 创建的默认平台证书验签器处理 SM2 响应时,WxPayValidator 虽然调用了四参数 verify,但 AutoUpdateCertificatesVerifier 没有覆盖该重载,因此会执行这里的默认实现,丢弃 signatureType。其三参数方法随后调用 CertificatesVerifier.verify 的三参数版本,最终强制使用 RSA,导致有效的 SM2 响应验签失败或抛出无效证书异常。需要在 AutoUpdateCertificatesVerifier 中覆盖四参数重载,在完成证书更新检查后向内部验签器透传签名类型,并补充覆盖这条默认构建链路的测试。

Useful? React with 👍 / 👎.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Signature types are not propagated through the certificate wrapper and notification verification paths, causing valid SM2 signatures to be incorrectly verified as RSA and fail.

2 open issues
What changed in this PR

本 PR 为 WxJava 微信支付 V3 的请求签名和响应验签增加 SM2/SM3 支持,对应 #4141,并保留现有 RSA 行为。

变更:

  • 新增统一签名算法工具,支持 SM2/SM3、指定用户 ID 和动态认证类型。
  • 根据响应签名类型选择验签算法,并扩展 PEM 工具以解析 EC 密钥。
  • 新增 SM2 签名、验签、密钥解析及 RSA 兼容性测试,并注册到 TestNG 套件。

验证范围:已静态核查相关差异和调用链,未执行构建或测试。

File Description
weixin-java-pay/​src/​test/​resources/​testng.xml 注册新增 SM2 测试类。
weixin-java-pay/​src/​test/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​Sm2SignatureTest.java 覆盖 SM2 功能及 RSA 兼容性。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​util/​PemUtils.java 增加 EC 私钥和公钥解析。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​WxPayValidator.java 读取并校验响应签名类型,传入验证器。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​WxPayCredentials.java 从签名器获取认证类型。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​WechatPaySignature.java 集中创建 RSA、SM2 签名对象并设置用户 ID。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​Verifier.java 增加带签名类型的验签重载。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​Signer.java 增加默认 RSA 认证类型接口。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​PublicCertificateVerifier.java 支持按指定算法进行公钥验签和证书委托。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​PrivateKeySigner.java 根据私钥类型选择签名算法和认证类型。
weixin-java-pay/​src/​main/​java/​com/​github/​binarywang/​wxpay/​v3/​auth/​CertificatesVerifier.java 增加按指定算法进行证书验签的路径。

Review effort: Balanced


@Override
public boolean verify(String serialNumber, byte[] message, String signature) {
return verify(serialNumber, message, signature, WechatPaySignature.RSA_TYPE);
Comment on lines +17 to +18
default boolean verify(String serialNumber, byte[] message, String signature, String signatureType) {
return verify(serialNumber, message, signature);
Keep RSA defaults and add regression coverage for certificate verification.
Document the preloaded SM2 platform certificate path tested offline.

Refs #4151.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3支付接口签名支持国密算法

3 participants