bitcoin-php icon indicating copy to clipboard operation
bitcoin-php copied to clipboard

How make private key import to HSM

Open drsaluml opened this issue 5 years ago • 5 comments

Supported PKCS#11 Key Types

https://docs.aws.amazon.com/cloudhsm/latest/userguide/pkcs11-key-types.html

Is it possible or not to be able to import HSM ?

I need advice from all of you.

Thanks.

drsaluml avatar Dec 16 '19 14:12 drsaluml

Yea it should work, but you might have to embed the key in an ASN1 structure so the HSM has the curve information. I'm not familiar with AWS's cloudhsm, have you looked into an import command?

afk11 avatar Dec 17 '19 02:12 afk11

https://docs.aws.amazon.com/cloudhsm/latest/userguide/key_mgmt_util-importPrivateKey.html

drsaluml avatar Dec 17 '19 08:12 drsaluml

I want to export private key to a file of ECDSA ( secp256k1 ) format. How can I do this? Is there a sample command or code? Thanks!

drsaluml avatar Dec 17 '19 09:12 drsaluml

Hmm, there isn't code for it in this project. How about generating one on AWS and posting it's contents here and we'll try?

You can dump any ASN1 base64 or hex into this website and decode it, so you see what elements are embedded. https://lapo.it/asn1js/

Anyway, my guess is this DerPrivateKeySerializer from phpecc will be near enough.. https://github.com/phpecc/phpecc/blob/master/src/Serializer/PrivateKey/DerPrivateKeySerializer.php You can see how the ASN1 private key format is specified here: https://github.com/phpecc/phpecc/blob/master/src/Serializer/PrivateKey/DerPrivateKeySerializer.php#L54

If you can get a key from AWS, and then compare it against a key serialized by DerPrivateKeySerializer, I think you'll be 90% of the way there!

Convert bitcoin-php privkey to phpecc privkey, and serialize to DER:

$g = Bitcoin::getGenerator();
$privKey = // generate or parse your private key
$eccPrivKey = $g->getPrivateKeyFrom($privKey->getSecret());
$derPrivSerializer = new DerPrivateKeySerializer();
$der = $derPrivSerializer->serialize($eccPrivKey);
echo base64_encode($der)."\n";

afk11 avatar Dec 19 '19 19:12 afk11

@drsaluml how did this go? any luck?

afk11 avatar Feb 14 '20 14:02 afk11