无法在Node.js中验证RSA-PSS签名



我在JavaScript中有一个客户端,在Node.JS有一个服务器。我试图在客户端中签署一个简单的文本,并将签名和公钥一起发送到服务器,然后服务器可以验证公钥。

客户端中的任何内容都可以!但我无法在服务器端验证签名。我认为您没有必要阅读客户端代码,但为了保证我也会提供它。

客户代码:

let privateKey = 0;
let publicKey = 0;
let encoded = '';
let signatureAsBase64 = '';
let pemExported = ''
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function str2ab(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
let keygen = crypto.subtle.generateKey({
name: 'RSA-PSS',
modulusLength: 4096,
publicExponent: new Uint8Array([1,0,1]),
hash: 'SHA-256'
}, true, ['sign', 'verify']);
keygen.then((value)=>{
publicKey = value.publicKey;
privateKey = value.privateKey;
let exported = crypto.subtle.exportKey('spki', publicKey);
return  exported
}).then((value)=>{
console.log('successful');
const exportedAsString = ab2str(value);
const exportedAsBase64 = btoa(exportedAsString);
pemExported = `-----BEGIN PUBLIC KEY-----n${exportedAsBase64}n-----END PUBLIC KEY-----`;
//signing:
encoded = new TextEncoder().encode('test');
let signing = crypto.subtle.sign({
name: "RSA-PSS",
saltLength: 32
},
privateKey,
encoded);
return signing;
}).then((signature)=>{
const signatureAsString = ab2str(signature);
signatureAsBase64 = btoa(signatureAsString);
//verifying just to be sure everything is OK:
return crypto.subtle.verify({
name: 'RSA-PSS',
saltLength: 32
},
publicKey,
signature,
encoded)
}).then((result)=>{
console.log(result);

//send information to server:
let toSend = new XMLHttpRequest();
toSend.onreadystatechange = ()=>{
console.log(this.status);
};
toSend.open("POST", "http://127.0.0.1:3000/authentication", true);
let data = {
signature: signatureAsBase64,
publicKey: pemExported
};
toSend.setRequestHeader('Content-Type', 'application/json');
toSend.send(JSON.stringify(data));

//to let you see the values, I'll print them to console in result:
console.log("signature is:n", signatureAsBase64);
console.log("publicKey is:n", pemExported);
}).catch((error)=>{
console.log("error",error.message);
})

服务器代码(我使用express(:

const express = require('express');
const crypto = require('crypto');
const router = express.Router(); 
function str2ab(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
router.post('/authentication',  async (req, res)=>{
try{
const publicKey = crypto.createPublicKey({
key: req.body.publicKey,
format: 'pem',
type: 'spki'
});
console.log(publicKey.asymmetricKeyType, publicKey.asymmetricKeySize, publicKey.type);
let signature = Buffer.from(req.body.signature, 'base64').toString();
signature = str2ab(signature);
const result = crypto.verify('rsa-sha256', new TextEncoder().encode('test'),
publicKey, new Uint8Array(signature));
console.log(result);
}catch(error){
console.log('Error when autheticating user: ', error.message);
}
})

服务器控制台日志:

rsa undefined public
false

注意:

  1. 我认为公钥在服务器中导入正确,因为当我导出在服务器中再次使用公钥,双方(客户端和服务器(的pem格式完全相同的所以我认为问题与服务器中的"验证"或"转换签名"有关
  2. 如果可能的话,我更喜欢使用内置的加密模块,所以其他库(如微妙加密(是我的第二选择,我来这里是想看看是否可以使用加密来完成
  3. 我想学习如何验证由JavaScript SubtleCrypto签名的签名,因此,请不要问一些问题,例如:

为什么要验证服务器中的公钥?

为什么不在客户端中使用'X'库?

  1. 请随意更改导出格式(pem(、公钥格式('spki'(、算法格式(RSA-PSS(等
验证失败有两个原因:
  • 必须显式地指定PSS填充,因为PKCS#1 v1.5填充是默认的。

  • 签名的转换破坏了数据:行:

    let signature = Buffer.from(req.body.signature, 'base64').toString();
    

    执行UTF8解码,此处为s,其不可逆地改变数据,此处为s。签名由二进制数据组成,这些数据通常UTF8不兼容。只有使用合适的二进制到文本编码(如Base64、十六进制等(才能转换为字符串
    但除此之外,转换实际上根本没有必要,因为签名可以直接作为缓冲区传递。

以下NodeJS代码执行成功验证(针对使用客户端代码生成的签名和公钥(:

const publicKey = crypto.createPublicKey(
{
key: req.body.publicKey,
format: 'pem',
type: 'spki'
});
const result = crypto.verify(
'rsa-sha256', 
new TextEncoder().encode('test'), 
{
key: publicKey, 
padding: crypto.constants.RSA_PKCS1_PSS_PADDING
}, 
Buffer.from(req.body.signature, 'base64'));
console.log(result); // true

最新更新