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

generateKeyPairSync doesn't return KeyObject #25322

Closed
panva opened this issue Jan 3, 2019 · 4 comments
Closed

generateKeyPairSync doesn't return KeyObject #25322

panva opened this issue Jan 3, 2019 · 4 comments
Assignees
Labels
confirmed-bug Issues with confirmed bugs. crypto Issues and PRs related to the crypto subsystem.

Comments

@panva
Copy link
Member

panva commented Jan 3, 2019

  • Version: v11.6.0
  • Platform: macOS Mojave (10.14.2) Darwin C02TT3JQHTD6 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64

From crypto.generateKeyPair documentation

If a publicKeyEncoding or privateKeyEncoding was specified, this function behaves as if keyObject.export() had been called on its result. Otherwise, the respective part of the key is returned as a KeyObject.

This documentation part is missing from crypto.generateKeyPairSync but you can still omit the encoding objects, then a simple empty object {} is returned instead of an expected KeyObject.

/cc @tniessen

Examples:

const { generateKeyPairSync } = require('crypto')

console.log('generateKeyPairSync result', generateKeyPairSync('rsa', { modulusLength: 2048 }))
// => generateKeyPairSync result { publicKey: {}, privateKey: {} }
@panva panva changed the title generateKeyPair (+sync) doesn't return KeyObject as documented generateKeyPair (+sync) doesn't return KeyObject as documented when promisified Jan 3, 2019
@panva panva changed the title generateKeyPair (+sync) doesn't return KeyObject as documented when promisified generateKeyPairSync doesn't return KeyObject Jan 3, 2019
@tniessen tniessen self-assigned this Jan 3, 2019
@tniessen tniessen added confirmed-bug Issues with confirmed bugs. crypto Issues and PRs related to the crypto subsystem. labels Jan 3, 2019
@tniessen
Copy link
Member

tniessen commented Jan 3, 2019

Thanks for reporting this! The problem is that the native (C++) key object is being returned, not the JavaScript key object, that's why the object appears to be empty.

> const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 })
undefined
> publicKey.getAsymmetricKeyType()
'rsa'
> publicKey.getSymmetricKeySize()
C:\WINDOWS\system32\cmd.exe - node[6416]: src\node_crypto.cc:3350: Assertion `(key_type_) == (kKeyTypeSecret)' failed.
 1: 00007FF7F0EC3FDA v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+4714
 2: 00007FF7F0E68AD6 uv_loop_fork+80918
 3: 00007FF7F0E68B8F uv_loop_fork+81103
 4: 00007FF7F0D8682F std::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >+102815
 5: 00007FF7F12D20BE v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+59870
 6: 00007FF7F12D3620 v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+65344
 7: 00007FF7F12D25B9 v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+61145
 8: 00007FF7F12D249B v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+60859
 9: 00000038CF650361

tniessen added a commit to tniessen/node that referenced this issue Jan 3, 2019
@danbev danbev closed this as completed in 7afdfae Jan 7, 2019
addaleax pushed a commit that referenced this issue Jan 9, 2019
PR-URL: #25326
Fixes: #25322
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
refack pushed a commit to refack/node that referenced this issue Jan 14, 2019
PR-URL: nodejs#25326
Fixes: nodejs#25322
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BridgeAR pushed a commit to BridgeAR/node that referenced this issue Jan 16, 2019
PR-URL: nodejs#25326
Fixes: nodejs#25322
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@tniessen
Copy link
Member

@cqupt-yifanwu Your code creates a key pair and returns them as a pair of Buffers, because you specified the publicKeyEncoding and privateKeyEncoding options. Buffer does not have properties such as getAsymmetricKeyType and getSymmetricKeySize. You can either remove the encoding options and use KeyObjects, or create KeyObjects from the returned Buffers.

@cqupt-yifanwu
Copy link

@cqupt-yifanwu Your code creates a key pair and returns them as a pair of Buffers, because you specified the publicKeyEncoding and privateKeyEncoding options. Buffer does not have properties such as getAsymmetricKeyType and getSymmetricKeySize. You can either remove the encoding options and use KeyObjects, or create KeyObjects from the returned Buffers.

i want to Communication with java Server in ECDH encryption way ,but the generateKeyPairSync retruns privateKey has different length with java server

@tniessen
Copy link
Member

@cqupt-yifanwu Please open a new issue for that (preferably in nodejs/help), and please include code to reproduce your problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. crypto Issues and PRs related to the crypto subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants