简体   繁体   English

使用证书RSA Javascript验证签名

[英]Verify a signature using a certificate RSA Javascript

I am attempting to verify a signature with a certificate. 我正在尝试使用证书验证签名。 We have to download the required certificate from the CA, verify the certificate, then verify the signature. 我们必须从CA下载所需的证书,验证证书,然后验证签名。 I have no idea, and I'm hoping someone can shed some light. 我不知道,我希望有人能有所启发。 Here's what I have / know so far. 到目前为止,这是我所知道的。

To sign a message, I used the following code: 要签名消息,我使用了以下代码:

function sign(sk, m, certname) {
var key = new RSAKey();
key.setPrivate(sk.n, sk.e, sk.d);
var h = CryptoJS.SHA256(JSON.stringify(m)).toString(CryptoJS.enc.Hex);
h = new BigInteger(h, 16);
var sig = key.doPrivate(h).toString(16);
var obj = { "type": "SIGNED", "msg": m, "certname": certname, "sig": sig };
return JSON.stringify(obj);
}

To verify a signature, I used the following code: 为了验证签名,我使用了以下代码:

function verify(pk, signed) {
var key = new RSAKey();
var s = JSON.stringify(signed.sig).toString(CryptoJS.enc.Hex);
s = new BigInteger(s, 16);
key.setPublic(pk.n, pk.e);
var v = key.doPublic(s).toString(16);
var h = CryptoJS.SHA256(JSON.stringify(signed.msg)).toString(CryptoJS.enc.Hex);
return (v == h);
}

To verify a certificate, I used the following code: (EDIT: this is the new certificate verification function). 为了验证证书,我使用了以下代码:(编辑:这是新的证书验证功能)。

function verifyCertificate(signedCert, certname) {
var key = new RSAKey();
var s = JSON.stringify(signedCert.sig).toString(CryptoJS.enc.Hex);
s = new BigInteger(s, 16);
key.setPublic(CApk.n, CApk.e);
var v = key.doPublic(s).toString(16);
var h = CryptoJS.SHA256(JSON.stringify(signedCert.msg)).toString(CryptoJS.enc.Hex);
return (v == h);
}

And that's that. 就是这样。 Can anyone please help. 谁能帮忙。 I don't know how to go about this. 我不知道该怎么办。

EDIT: Okay, I think I have solved my own question (with assistance from the responses). 编辑:好的,我想我已经解决了自己的问题(在答复的帮助下)。 This is the code that returns all positive results: 这是返回所有积极结果的代码:

function verifyWithCert(sig) {
// 1. Download the required certificate from the CA
// 2. Verify the certificate
// 3. Verify the message
var certKey = new RSAKey();
var loadedCert = loadCert(sig.certname);
var certS = JSON.stringify(loadedCert.sig).toString(CryptoJS.enc.Hex);
certS = new BigInteger(certS, 16);
certKey.setPublic(CApk.n, CApk.e);
var certV = certKey.doPublic(certS).toString(16);
var certH = CryptoJS.SHA256(JSON.stringify(loadedCert.msg)).toString(CryptoJS.enc.Hex);
var verifyResult;
if (certV == certH) {
    verifyResult = true;
}
var Sigkey = new RSAKey();
var s = JSON.stringify(sig.sig).toString(CryptoJS.enc.Hex);
s = new BigInteger(s, 16);
Sigkey.setPublic(loadedCert.msg.subject.pk.n, loadedCert.msg.subject.pk.e);
var v = Sigkey.doPublic(s).toString(16);
var h = CryptoJS.SHA256(JSON.stringify(sig.msg)).toString(CryptoJS.enc.Hex);
var verifySignature;
if (v == h) {
    verifySignature = true;
}
var result = { "certificateFound": loadedCert ,"certificateVerified": verifyResult ,"signatureVerified": verifySignature };
return result;
}

(A note to other members of StackOverflow, I am also in this class so there's a bit of stuff that I mention that comes out of nowhere in regards to variables and other references.) (请注意StackOverflow的其他成员,我也在该类中,所以我提到的一些关于变量和其他引用的内容无所不在。)

In the verifyCertificate function: 在verifyCertificate函数中:

function verifyCertificate(signedCert, certname) {
    var loadedCert = loadCert(certname);

    // signedCert is the same as loadedCert above, the button runs the
    // loadCert function and outputs the contents into the textarea,
    // so the following will always be true.

    var originalSig = JSON.stringify(signedCert.sig);
    var loadedSig = JSON.stringify(loadedCert.sig);
    log(loadedSig);
    return (originalSig == loadedSig);
}

How am I supposed to verify the certificate then? 那我该如何验证证书? What am I comparing the loaded CA certificate to? 我将加载的CA证书与什么进行比较? I thought maybe compare the public key in the certificate to the public key used to sign the message but... I don't know. 我以为可以将证书中的公钥与用于签名消息的公钥进行比较,但是...我不知道。 I'm very confused. 我很困惑

You're on the right track with that though, think about the verify() function, and the details contained in the CApk variable at the top of the file. 不过,您的工作方向正确,请考虑一下verify()函数以及文件顶部CApk变量中包含的详细信息。 Can you hash the message from the loadCert() JSON response and match it against the output from: 您是否可以对来自loadCert()JSON响应的消息进行哈希处理,然后将其与以下输出的内容进行匹配:

function verify() {
    //[...]
    key.setPublic(pk.n, pk.e);
    //[...]
}

Assuming you change a few variables? 假设您更改了一些变量?

It's similar to the method I used at least, so I'm hoping it's right. 它与我至少使用的方法类似,所以我希望它是正确的。 I figure if you can hash the message using the details in CApk, and compare it to a hash of the message contained in the JSON response, that verifies the certificate. 我想知道是否可以使用CApk中的详细信息对消息进行哈希处理,然后将其与JSON响应中包含的消息哈希进行比较,以验证证书。 Hopefully. 希望。

There is an error in 'verify certificate' approach. “验证证书”方法中存在错误。 you need to test the signature of certificate with public key of CA given in 355a3_main to verify, the code given here will only verify your certificate and will give s false positive for rest 您需要使用355a3_main中提供的CA的公钥来测试证书的签名以进行验证,此处给出的代码仅会验证您的证书,并且会给假假肯定

i think this should work 我认为这应该工作

var loadedCert = loadCert(certname);
var originalSig = JSON.stringify(signedCert.sig);
var loadedSig = JSON.stringify(loadedCert.sig);
log(loadedSig,originalSig);

var key = new RSAKey();
var s = JSON.stringify(signedCert.sig).toString(CryptoJS.enc.Hex);
s = new BigInteger(s, 16);
key.setPublic(CApk.n, CApk.e);
var v = key.doPublic(s).toString(16);
var h = CryptoJS.SHA256(JSON.stringify(signedCert.msg)).toString(CryptoJS.enc.Hex);

if (originalSig == loadedSig && v==h)
return true;
else
return false;

话虽这么说,那么长短的消息呢?

 Except... you know how he says his solutions for the core tasks are between 5 and 10 lines? well this is about 20 lines of code, so i don't know if I should be suspicious of my code 

I used the function verify and verifycertificate again in the RSA signature verification with certificate function. 我在带有证书的RSA签名验证功能中再次使用了功能verify and verifycertificate。 That will make your code fairly short. 这将使您的代码相当简短。 and I really appreciate this post, you're all my life savers. 我真的很感谢这篇文章,您是我所有的救生员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM