Skip to content
Advertisement

Unable to verify RSA-PSS signature in Node.js

I have a client in JavaScript and a server in Node.JS. I’m trying to sign a simple text in client and send the signature along with publicKey to the server then server can verify the publicKey.

Anything in client-side is OK! but I’m unable to verify the signature in server-side. I think there is no need for you to read the client code but just for assurance I’ll provide it too.

Client code:

JavaScript

Server Code(I use express for this purpose):

JavaScript

Server Console Log:

JavaScript

NOTE:

  1. I think the public key is imported correctly in server because when I export the public key again in server, the pem formats of both sides(client & server) are completely equal. so I think the problem is associated with ‘verification’ or ‘converting signature’ in server.
  2. I prefer to use the built-in crypto module if it’s possible, so other libraries such as subtle-crypto are my second options and I’m here to see if this can be done with crypto or not.
  3. I want to learn how to verify a signature that is signed by JavaScript SubtleCrypto, due to this, Please don’t ask some questions such as:

Why do you want to verify the public key in server?

Why don’t you use ‘X’ library in client?

  1. Feel free to change Exported format(pem), Public key format(‘spki’), Algorithm format(RSA-PSS) and so on.

Advertisement

Answer

The failed verification has two reasons:

  • The PSS padding must be specified explicitly, since PKCS#1 v1.5 padding is the default, s. here.

  • The conversion of the signature corrupts the data: The line:

    JavaScript

    performs a UTF8 decoding, s. here, which irreversibly changes the data, s. here. The signature consists of binary data that is generally UTF8 incompatible. A conversion to a string is only possible with suitable binary-to-text encodings (like Base64, hex etc.), s. here.
    But apart from that a conversion is actually not necessary at all, because the signature can be passed directly as a buffer, s. here.

The following NodeJS code performs a successful verification (for a signature and public key produced with the client code):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement