'signature verification using node.js crypto and aws s3 returns error wrong tag
Trying to validate a signature of a file using Node.js Crypto. Not getting success. I'm new to Node.js. this openSSL works for me:
openssl enc -d -A -base64 -in update.sig -out update.sha256 - that turns the sig into a binary
openssl dgst -sha256 -verify PublicKey.pem -signature update.sha256 update.zip
however when I attempt to implement that in Node.js it's not working.
I am storing the signature file (update.sig) and the file that is signed (update.zip) and the public key from the keypair used in generating the signature (publicKey.pem) in aws s3. This is the node.js code snippet which is not working
Key = path + "update." + uid + ".sig"
console.log("Looking for " + Key)
var signature = await s3.getObject({Bucket, Key }).promise();
var binSignature = crypto.createHash('sha256').update(signature.Body).digest()
console.log("signature: ", signature)
Key = path + "update." + uid + ".zip"
console.log("Looking for " + Key)
var data = await s3.getObject({Bucket, Key} ).promise();
console.log("data: ", data)
Key = "PublicKey.pem"
console.log("Looking for " + Key)
var pemKey = await s3.getObject({Bucket, Key}).promise();
console.log("pemKey: ", pemKey)
var verified = crypto.verify("sha256", data.Body, pemKey.Body, binSignature.Body)
console.log("verified: ", verified)
returns
2022-01-25T14:09:32.478Z d7054eef-8e81-4dfa-bb58-7b5165149202 INFO TypeError [ERR_INVALID_ARG_TYPE]: The "signature" argument must be an instance of Buffer, TypedArray, or DataView. Received undefined
at Object.verifyOneShot [as verify] (internal/crypto/sig.js:212:11)
at Runtime.exports.handler (/var/task/app.js:111:24)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'ERR_INVALID_ARG_TYPE'
}
If I use the signature directly from the file, rather than converting it to a binary with the crypto.createHash('sha256'), I get this failure
2022-01-25T13:57:46.055Z 19b11d30-c714-4f20-b7af-fa6d112c1486 INFO Error: error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag
at Object.verifyOneShot [as verify] (internal/crypto/sig.js:219:10)
at Runtime.exports.handler (/var/task/app.js:109:24)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
opensslErrorStack: [
'error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error'
],
library: 'asn1 encoding routines',
function: 'asn1_check_tlen',
reason: 'wrong tag',
code: 'ERR_OSSL_ASN1_WRONG_TAG'
}
The openSSL commands above verify these successfully. signature looks like this
MGYCMQCrK8FzAq2Fl+CVAWVQLOBNtNf0o+Ylwo/at4GZQtdu/UAL2yQijdNzPcEK/sIevngCMQDNOVQ0TWWyoBx2cTRgBxsaUd8jR8qIE5T9YrO81i4bARHALTQIMQNWNvyanyghAlE=
publicKey.pem looks like this
-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEQ0UK86Jdls1MT6iAVSrFuI+N2dU5wHXQ
fyHga2BDq+O9reM47KpfQR1pF/+MPsWf8IH8Il8HkIDhizAjMsSR3fwSe5xKns02
lJm71a3pY1+AFx3wbc1GP57X6vKFNJPm
-----END PUBLIC KEY-----
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
