Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWS, ES512: Invalid signed JWTs due to invalid signature size #627

Open
user163 opened this issue Aug 5, 2024 · 0 comments
Open

JWS, ES512: Invalid signed JWTs due to invalid signature size #627

user163 opened this issue Aug 5, 2024 · 0 comments

Comments

@user163
Copy link

user163 commented Aug 5, 2024

Some of the signed JWTs for ES512 generated with jsrsasign v11.1.0 in the context of JWS have signatures that are shorter than 132 bytes (e.g. 130 bytes). Such signatures and thus the associated signed JWTs are invalid and are verified as invalid by other JWS-compliant libraries.
With the 130 bytes signatures (at least the ones I have analyzed), r and s are only 65 bytes in size and the padding with a leading 0x00 on 66 bytes is missing for both values. So presumably the issue is a flawed implementation of the P1363 format.

In addition, there are also sporadic exceptions (unknown ECDSA sig s length error) during signing (with a frequency of approximately 1:1000).

Here is a script that demonstrates both issues:

// code takes a few minutes to execute!

var privateKey = `-----BEGIN PRIVATE KEY-----
MIH3AgEAMBAGByqGSM49AgEGBSuBBAAjBIHfMIHcAgEBBEIBt9JkMzOnDTkWGeWr
hq5a73ByFKDazPsiKSAyS7QrD9p7LY2fxpuJ33eccF4BlKcdUpH3JdBfQWLAhifA
t5vTRW6gBwYFK4EEACOhgYkDgYYABAGHGt/TynDUfNy8TII8lJOaRHezUbRooLM7
lCtkIejai/dZLbq9GUAeSG3dXujrx7lrElqbnFytJQgZ71OMOabmjAHEkJejYdC6
sGxsFCROu3oLZdNk8ZSY5pGIQj4CqLGthpgglVlfDQlQw2P3Ib0MP9r3TYaB6g8i
cx/Qwp4dqrtYug==
-----END PRIVATE KEY-----`;

// code for demonstrating different signature lengths
for (var i = 0; i < 16; i++) {
    var header = JSON.stringify({"alg": "ES512"});
    var payload = JSON.stringify({"sub": "1234567890", "name": "John Doe"});
    var jwt = KJUR.jws.JWS.sign("ES512", header, payload, privateKey);
    var signatureHex = b64utohex(jwt.split('.')[2]);
    var signatureLen = signatureHex.length/2;
    if (signatureLen != 132) console.log(signatureLen);
}
console.log("demo 1: done\n");

// code for demonstrating the exception "unknown ECDSA sig s length error"
for (var i = 0; i < 1024; i++) {
    var header = JSON.stringify({"alg": "ES512"});
    var payload = JSON.stringify({"sub": "1234567890", "name": "John Doe"});
    try {
        var jwt = KJUR.jws.JWS.sign("ES512", header, payload, privateKey);
    } catch(ex) {
        console.log(ex.message);
    }
}
console.log("demo 2: done");

Sample output:

130
130
130
130
demo 1: done

unknown ECDSA sig s length error
demo 2: done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant