Repository navigation
crypto: Assertation failed when createPrivateKey(PUBLIC) #29904
Copy link
Copy link
Closed
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.
Description
Activity
- addedcryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.
on Oct 9, 2019 @nodejs/crypto @tniessen
@tespent can you provide a runnable reproduction, I can't repro:
% node --version; node -p "require('crypto').generateKeyPairSync('ec',{namedCurve:'P-256'}).publicKey.export({format:'der',type:'spki'}).toString('base64')" v12.11.1 MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBEGMUwSkAYZNhIctHCL6VV/ufsVjGax0gWxBhCP9rZckKlcI1HQhIMv1qPzIS5zbFpud0YgrvZnYFkgWlJs5Ig==@tespent can you provide a runnable reproduction, I can't repro:
% node --version; node -p "require('crypto').generateKeyPairSync('ec',{namedCurve:'P-256'}).publicKey.export({format:'der',type:'spki'}).toString('base64')" v12.11.1 MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBEGMUwSkAYZNhIctHCL6VV/ufsVjGax0gWxBhCP9rZckKlcI1HQhIMv1qPzIS5zbFpud0YgrvZnYFkgWlJs5Ig==node -p "require('crypto').createPrivateKey({key:Buffer.from('MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBEGMUwSkAYZNhIctHCL6VV/ufsVjGax0gWxBhCP9rZckKlcI1HQhIMv1qPzIS5zbFpud0YgrvZnYFkgWlJs5Ig==','base64'),format:'der',type:'spki'})"- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Oct 9, 2019 Thanks, I can repro
Its because spki is a public key format, and it isn't handled in the private key parsing code. I'm rebuildign to see if this fixes it:
diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 3405fbb5b4..c2a22d0d20 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3345,6 +3345,8 @@ static ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey, if (p8inf) pkey->reset(EVP_PKCS82PKEY(p8inf.get())); } + } else if (config.type_.ToChecked() == kKeyEncodingSPKI) { + return ParseKeyResult::kParseKeyFailed; } else { CHECK_EQ(config.type_.ToChecked(), kKeyEncodingSEC1); const unsigned char* p = reinterpret_cast<const unsigned char*>(key);- added a commit that references this issue
on Oct 10, 2019 Right, but I believe execution shouldn't even get to
ParsePrivateKeyin this case. Possible fix in #29913.- added a commit that references this issue
on Oct 12, 2019 - added 2 commits that reference this issue
on Nov 8, 2019
Metadata
Metadata
Assignees
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.
crypto.createPrivateKeyraise an assertation failure when trying to import a public key.Output of v12.11.1:
I have tried
keyPair=require('crypto').generateKeyPairSync('rsa',{modulusLength:4096})andkeyPair=require('crypto').generateKeyPairSync('ec',{namedCurve:'P-256'}), the result are the same.