Repository navigation
Signing with a ecdh type private key #18147
Description
Activity
- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.cryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.
on Jan 15, 2018 .getPrivateKey()returns just the private key,openssl ecparam -genkeywrites the private key wrapped in a PEM-encoded ASN.1 container:$ openssl ecparam -genkey -name secp521r1 -noout | openssl asn1parse 0:d=0 hl=3 l= 219 cons: SEQUENCE 3:d=1 hl=2 l= 1 prim: INTEGER :01 6:d=1 hl=2 l= 65 prim: OCTET STRING [HEX DUMP]:E713D72B75731D8ED64E2EF6DB54E0EB0CD4AC3FE5BF34270E348DD7CDC3D1428A91C0ABF45B89790D9EC5D87DC4768F13DB5164A48DD2B2BF92B545B9936C85B5 73:d=1 hl=2 l= 7 cons: cont [ 0 ] 75:d=2 hl=2 l= 5 prim: OBJECT :secp521r1 82:d=1 hl=3 l= 137 cons: cont [ 1 ] 85:d=2 hl=3 l= 134 prim: BIT STRINGThat 65 byte octet string is the private key.
Node.js currently has no direct support for constructing that container but there's probably a library for it on npm (asn1.js?) and worst case, you can construct it yourself, the format is not hugely complex.
I'll close this out, see #15116 for the feature request. Please post follow-up questions to the help repo.
Why doesn't the
Sign#signmethod allow the use of aBufferto pass along the private key to use? It seems like quite a waste to first add a wrapperSign#signneeds to unwrap anyway.@skerit Just raw bytes doesn't tell you what kind of key it is or what params it has. Look up how many openssl apis start with 'EVP_PKEY_' and you'll get a feel for the phase space. :-)
Reacted by Jelle De Loecker
I didn't know where to ask this. But I needed to ask.
The problem is after generating public private keys with ecdh, I am trying to use
sign.signand it doesn't seem to work.At first I just wrapped the
alice.getPrivateKey('base64')inside a-----BEGIN EC PRIVATE KEY-----and-----END EC PRIVATE KEY-----but that resulted inasn1 too longSo I split the private key at 64 characters, and that resulted in
Error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tagI also tried spliting the `alice.generateKeys().toString('base64') and wrapping it in begin end block. But that too results in the same error.
Here are the codes:
try1.jstry2.jsThe error being:
But if I generate the pem file like:
which is from where I copied the begin end blocks. And I read the file in ascii and pass that to the
signmethod. It works like butter.What am I doing wrong