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

Node incompatibility for "sha256" algorithm #84

Open
zenshixd opened this issue Mar 5, 2024 · 5 comments
Open

Node incompatibility for "sha256" algorithm #84

zenshixd opened this issue Mar 5, 2024 · 5 comments

Comments

@zenshixd
Copy link

zenshixd commented Mar 5, 2024

Hello,

there seems to be some differences between this library and Node.js, consider following code:

const bCrypto = require('./browser');
const nCrypto = require('crypto');

const key = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAvvjcmEFSc9CYHZ8gN5phm9xBDxEXbM/8K62Rw5TpAENI3RZr
7Ua570EL3GEJBFM5IfWjn4zlEoXdLIM4Rj4mC9TQdxsmRYr9zGL8r9/jDTwwPTTk
ApGTZWJg5og2IrVaKByXnJHQGtHEkgNS0ay3JnL8TEOUr75WujU8YRPoBCnSnFtN
vhGRgrp+4QOROtP5pW85FmQKTkYK1s0bdlJM4YCDyicFPuqo8d2+pn8uXHcmHq7C
2QJwVEOSESpSEKwwVpZ2jQws7QRb5+ELaRdH2QQQ239FZRhVrEMsNf3AOSXcNULl
W+Mp6rbwY9+xM84g5pKgGJ80i3LDqsPvdvMAcwIDAQABAoIBAB8JvHSkfT2kg+yg
jbBrz3xw9kP4H6oIPbBto9i5TYtE5EVGnSDx00tu83oMbVY+HnWGAmNul1aE2jcf
VpiYBj/7BSyuhXBdEDXVSsNc7H6cryxreblvJpkePaiKL95BQldtmjiQvFV3jW67
yoyYeSXvH1FX1IUhtt/Jwjf278kpKIhfTmshlRaSUavjZWYv4kj7qOURUTkXuZrF
yfy3jyid1lRmLzOOGsNs9EMjNMdbThLYOmhrkrMr4dM7jWby850MMTXMJxIcTlAJ
SlhPONvvJfS2D49dKVPBxMMh3DoBGsOcd6lZiy3VSRBrUHXFyI98pH7Y+2NHCjjw
SApfDI0CgYEA+raJ+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dmdsZTJrbVZWaF9DTXcyeExZSWc4VkMzTzdTRSIsImp3ayI6eyJlIjoiQVFBQiIsImt0eSI6IlJTQSIsIm4iOiJ1VjlEUzB4bnNfOWtYbXZlQUp2VmwxV0M3emduTFlvdnNqcDY5UldQYTJ2UFVCUm1iS1lRWlZOZXQwOTdSaWxjUXNtaEhNMTVpdDA2QUdrQ2ZiNHNZcU1aWVd0QXh0a0N3U2pKUEN1OWN4WmdoU0dYWFFEM3dncm8wYl9vbmN2d1V1cjhaeG10U09pRmJQVElOZWUwUVlMWEg3ak12cVhKN3VzMTcxSU5jNDBhOXU1WDJoa3hGMVFBeWdOYmx2RVJxWHo2V2otNjR3TEhIMnBZNk9QaVFST2Rub0VlQ29DNEhXMjNqNlNDckZXTlNOei1DcUxTZWtYM0Z3TGZUMk55Z3l5N1pEeWFPZHhobFRPa0NxdUlJVGFHMF81Qjl1a1ZHRW9yQ0E5RmhYb20yRk50b1J3c0I3eUFtdkpDQ3B4Q1lUTmFuZ3VWYmVmU3luYS14VWR1ZHcifX0.eyJ0ZXJtc09mU2VydmljZUFncmVlZCI6dHJ1ZSwiY29udGFjdCI6WyJtYWlsdG86ZW1haWxAZXhhbXBsZS5jb20iXX0";

let nData = nCrypto.createSign('sha256WithRSAEncryption').update(txt).sign(p);
console.log('node', nData);
let bData = bCrypto.createSign('sha256WithRSAEncryption').update(txt).sign(p);
console.log('browserify-sign', bData);
console.log('isEqual', nData.toString('hex') === bData.toString('hex'));

nData = nCrypto.createSign('sha256').update(txt).sign(p);
console.log('node', nData);
bData = bCrypto.createSign('sha256').update(txt).sign(p);
console.log('browserify-sign', bData);
console.log('isEqual', nData.toString('hex') === bData.toString('hex'));

For sha256WithRSAEncryption both Node.js and browserify-sign are correctly signing the text using provided private key, but for sha256 only Node.js is able to sign a text, while browserify-sign` throws an error:

Error: wrong private key type
    at sign (/home/ubuntu/Projects/browserify-sign/browser/sign.js:24:63)
    at Sign.signMethod [as sign] (/home/ubuntu/Projects/browserify-sign/browser/index.js:43:13)
    at Object.<anonymous> (/home/ubuntu/Projects/browserify-sign/test-bsign.js:45:50)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

From my initial investigation, its because sha256 algorithm expects only ECDSA key, but we are providing RSA key instead.

Is it expected? Since browserify-sign wants to duplicate node behaviour, I think its a bug.

@ljharb
Copy link
Member

ljharb commented Mar 5, 2024

Is there an older version of node where they were in agreement?

If no, it's definitely a bug, and if so, it's a bit trickier to bring it into compliance.

@zenshixd
Copy link
Author

zenshixd commented Mar 5, 2024

I tested LTS versions from Node 4 up to Node 18 - and discrepancy seems to be present in all of those versions.

@ljharb
Copy link
Member

ljharb commented Mar 6, 2024

This package goes back farther than that :-) it’d be good to compare in node 0.10 as well.

@zenshixd
Copy link
Author

zenshixd commented Mar 6, 2024

Node 0.10.48 - still same issue:

D:\Projects\browserify-sign git:[main]> nvm use 0.10.48
Now using node v0.10.48 (64-bit)

D:\Projects\browserify-sign git:[main]> node test.js
node <SlowBuffer 7f c4 32 92 34 e0 9f 9f 84 b7 d2 fe 38 63 6e 95 ba ee ae 6a 8e be d1 9d 35 69 f4 2e fc 3c 84 a4 c7 81 92 7f 23 51 fa 59 40 a0 a4 8c f4 f5 ed 02 f4 ef b0 ...>
browserify-sign <Buffer 7f c4 32 92 34 e0 9f 9f 84 b7 d2 fe 38 63 6e 95 ba ee ae 6a 8e be d1 9d 35 69 f4 2e fc 3c 84 a4 c7 81 92 7f 23 51 fa 59 40 a0 a4 8c f4 f5 ed 02 f4 ef b0 ...>
isEqual true
node <SlowBuffer 7f c4 32 92 34 e0 9f 9f 84 b7 d2 fe 38 63 6e 95 ba ee ae 6a 8e be d1 9d 35 69 f4 2e fc 3c 84 a4 c7 81 92 7f 23 51 fa 59 40 a0 a4 8c f4 f5 ed 02 f4 ef b0 ...>

D:\Projects\browserify-sign\browser\sign.js:24
  if (signType !== 'rsa' && signType !== 'ecdsa/rsa') { throw new Error('wrong
                                                              ^
Error: wrong private key type
    at sign (D:\Projects\browserify-sign\browser\sign.js:24:63)
    at Sign.signMethod [as sign] (D:\Projects\browserify-sign\browser\index.js:43:13)
    at Object.<anonymous> (D:\Projects\browserify-sign\test.js:44:50)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:945:3

@ljharb
Copy link
Member

ljharb commented Mar 6, 2024

Awesome, thanks for confirming :-) let's fix it then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants