rsa p7 验签通过

This commit is contained in:
cheney
2026-04-04 08:32:06 +08:00
parent 5a033318c0
commit 9d826fb985
3 changed files with 137 additions and 94 deletions
+30 -54
View File
@@ -46,6 +46,12 @@ module.exports.getInfoFromPKCS7 = function (p7sign) {
// 解析 PKCS7 数据
const p7 = forge.pkcs7.messageFromPem(pemData)
// 辅助函数:将属性数组转换为 DN 字符串
function attrsToDN(attrs) {
if (!attrs || attrs.length === 0) return ''
return attrs.map(attr => `${attr.name}=${attr.value}`).join(', ')
}
// 提取证书
let cert = null
if (p7.certificates && p7.certificates.length > 0) {
@@ -55,10 +61,12 @@ module.exports.getInfoFromPKCS7 = function (p7sign) {
name: attr.name,
value: attr.value
})),
subjectDN: attrsToDN(certObj.subject.attributes),
issuer: certObj.issuer.attributes.map(attr => ({
name: attr.name,
value: attr.value
})),
issuerDN: attrsToDN(certObj.issuer.attributes),
serialNumber: certObj.serialNumber,
notBefore: certObj.validity.notBefore,
notAfter: certObj.validity.notAfter,
@@ -66,13 +74,18 @@ module.exports.getInfoFromPKCS7 = function (p7sign) {
}
}
// 提取公钥 - 从证书中提取公钥对象并转换为 PEM
// 提取公钥 - 从证书中提取公钥对象并转换为 PEM 和 Hex
let publicKey = null
let publicKeyHex = null
if (p7.certificates && p7.certificates.length > 0) {
const certObj = p7.certificates[0]
// 提取公钥对象并转换为 PEM 格式
if (certObj.publicKey) {
publicKey = forge.pki.publicKeyToPem(certObj.publicKey)
// 将公钥转换为 hex 格式
const publicKeyAsn1 = forge.pki.publicKeyToAsn1(certObj.publicKey)
const publicKeyDer = forge.asn1.toDer(publicKeyAsn1)
publicKeyHex = forge.util.bytesToHex(publicKeyDer.getBytes())
}
}
@@ -89,6 +102,7 @@ module.exports.getInfoFromPKCS7 = function (p7sign) {
return {
cert,
publicKey,
publicKeyHex,
signature,
signatureHex
}
@@ -143,7 +157,21 @@ module.exports.verifyPKCS7 = function (p7sign, originalData) {
// 验证签名
try {
// 尝试使用 node-forge 验证(适用于 RSA)
// 对于 RSA 签名,使用公钥手动验证
if (p7.certificates && p7.certificates.length > 0) {
const publicKey = p7.certificates[0].publicKey
if (publicKey && p7.rawCapture && p7.rawCapture.signature) {
// 计算数据的摘要
const md = forge.md.sha256.create()
md.update(verifyData.toString(), 'utf8')
const digest = md.digest().getBytes()
// 使用公钥验证签名
return publicKey.verify(digest, p7.rawCapture.signature)
}
}
// 如果 RSA 验证失败或不适用,尝试使用 node-forge 的 verify(如果支持)
return p7.verify()
} catch (error) {
// 如果验证失败,尝试使用 SM2 验证
@@ -164,55 +192,3 @@ module.exports.verifyPKCS7 = function (p7sign, originalData) {
}
}
/**
* PKCS7 签名(自动判断 RSA 或 SM2 算法)
* @param {Buffer|string} data - 待签名数据
* @param {string} privateKey - 私钥
* @returns {Buffer} PKCS7 签名数据
*/
module.exports.pkcs7Sign = function (data, privateKey) {
// 处理输入数据
let inputData
if (Buffer.isBuffer(data)) {
inputData = data
} else if (typeof data === 'string') {
inputData = Buffer.from(data)
} else {
throw new Error('Invalid input type for data')
}
// 判断私钥类型
if (privateKey.includes('RSA PRIVATE KEY')) {
// RSA 算法签名
const pkey = forge.pki.privateKeyFromPem(privateKey)
const p7 = forge.pkcs7.createSignedData()
p7.content = forge.util.createBuffer(inputData.toString('binary'))
p7.addCertificate(forge.pki.certificateFromPem(privateKey)) // 假设私钥包含证书
p7.addSigner({
key: pkey,
certificate: forge.pki.certificateFromPem(privateKey),
digestAlgorithm: forge.pki.oids.sha256
})
p7.sign()
const pem = forge.pkcs7.messageToPem(p7)
return Buffer.from(pem, 'ascii')
} else if (privateKey.includes('SM2') || privateKey.length === 64) {
// SM2 算法签名
// 由于 node-forge 不直接支持 SM2 算法
// 这里需要使用 sm-crypto 库进行 SM2 签名,然后封装到 PKCS7 格式中
// 注意:这只是一个简化的实现,实际应用中可能需要更复杂的处理
const hash = sm3(inputData.toString())
const signature = sm2.doSignature(hash, privateKey)
// 创建一个简单的 PKCS7 结构来存储 SM2 签名
// 注意:这不是标准的 PKCS7 SM2 签名实现
const p7 = forge.pkcs7.createSignedData()
p7.content = forge.util.createBuffer(inputData.toString('binary'))
// 这里需要添加 SM2 证书和签名逻辑
// 由于复杂性,这里暂时抛出错误
throw new Error('SM2 PKCS7 signature not fully implemented yet')
} else {
throw new Error('Unsupported private key type')
}
}