Skip to content

pair.encodePkcs8() not interoperable #1858

@rvalle

Description

@rvalle
  • I'm submitting a ...
  • Bug report
  • Feature request
  • Support request
  • Other
  • What is the current behavior and expected behavior?

Polkadot jS api provides a function encodePkcs8() to encode an private key of type ed25519 to the PKCS8 standard.

We cannot achieve interoperability with other software using the same standard, in particular NodeJS crypto package or OpenSSL.

  • What is the motivation for changing the behavior?

The data type returned by encodePkcs8() should conform with the standard and ideally be easy to use with other software using the same standard.

  • Please tell us about your environment:
  • Version: Node 18 / Openssl

  • Environment: Ubutnu

    • Node.js
    • Browser
    • Other (limited support for other environments)
  • Language:

    • JavaScript
    • [X ] TypeScript (include tsc --version)
    • [X ] Other
  • How to reproduce

We first generate a keypair and then encode it as PKCS8:

        const mnemonic = mnemonicGenerate();
        const k8 = pair.encodePkcs8();

        const node_key = crypto.createPrivateKey({
            key: Buffer.from(k8),
            format: 'der',
            type: 'pkcs8',
        });

NodeJS returns:

Error: error:0680009B:asn1 encoding routines::too long

We can also export the PKCS #8 for processing with OpenSSL as:

        fs.writeFileSync('key.pkcs8', k8);

And then try to parse it with OpenSSL by Doing:

(venv) rafael@tuxraf:~/Development/valletech/dinfra/packages/keytool$ openssl pkey -inform DER -outform PEM -in key.pkcs8 -out key.pem
unable to load key
140232103302464:error:0D07209B:asn1 encoding routines:ASN1_get_object:too long:../crypto/asn1/asn1_lib.c:91:
140232103302464:error:0D068066:asn1 encoding routines:asn1_check_tlen:bad object header:../crypto/asn1/tasn_dec.c:1137:
140232103302464:error:0D06C03A:asn1 encoding routines:asn1_d2i_ex_primitive:nested asn1 error:../crypto/asn1/tasn_dec.c:698:
140232103302464:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:614:
140232103302464:error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag:../crypto/asn1/tasn_dec.c:1149:
140232103302464:error:0D06C03A:asn1 encoding routines:asn1_d2i_ex_primitive:nested asn1 error:../crypto/asn1/tasn_dec.c:713:
140232103302464:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:646:Field=n, Type=RSAPrivateKey
140232103302464:error:04093004:rsa routines:old_rsa_priv_decode:RSA lib:../crypto/rsa/rsa_ameth.c:133:
140232103302464:error:0D07209B:asn1 encoding routines:ASN1_get_object:too long:../crypto/asn1/asn1_lib.c:91:
140232103302464:error:0D068066:asn1 encoding routines:asn1_check_tlen:bad object header:../crypto/asn1/tasn_dec.c:1137:
140232103302464:error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error:../crypto/asn1/tasn_dec.c:572:Field=attributes, Type=PKCS8_PRIV_KEY_INFO

Accoring to the RFC5208, which defines PKCS8 the main data structure that holds the key includes information about the PrivateKeyAlgorithmIdentifier while the output from the OpenSSL command seems to identify the key as RSAPrivateKey which makes me believe that there is something fundamentally wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationUpdates or additions to guides, API references, and comments to aid SDK understanding.SupportTracks issues or requests related to troubleshooting, answering questions, and user assistance.

    Type

    No type

    Projects

    Status

    Support

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions