0

So I am trying to parse a der format file to extract the public key. I have three algos, RSA, EDDSA and ECDSA. The below code works for RSA but not for EDDSA and ECDSA. I am getting the below error

Error: Cannot read public key. Unknown OID. at push../node_modules/node-forge/lib/rsa.js.pki.publicKeyFromAsn1

Below is the function which I wrote which parses the der to extract the public key. I think this is generic but not sure why it is not working for ecdsa and eddsa.

  const uploadDer = (file: any): boolean => {
    const fileReader = new FileReader();

    fileReader.onload = () => {
      const arrayBuffer = fileReader.result as ArrayBuffer;

      try {
        const uint8Array = new Uint8Array(arrayBuffer);
        const byteBuffer = forge.util.createBuffer(uint8Array);
        const asnObject = forge.asn1.fromDer(byteBuffer);
        const publicKeyObject = forge.pki.publicKeyFromAsn1(asnObject);
        const fileContent = forge.pki.publicKeyToPem(publicKeyObject);
        console.log('fileContent--> ', fileContent);

        const pemSections = forge.pem.decode(fileContent);
        const lastPemSection = pemSections[pemSections.length - 1];
        const publicKey = forge.util.encode64(lastPemSection.body);
        props.change('publicKey', publicKey);
      } catch (error) {
        props.change('publicKey', '');

        return dispatch(
          showNotification({
            id: uuidv1(),
            title: 'statusTitle',
            message: 'uploadKeypairFailedMessage',
            type: NotificationTypes.error,
          })
        );
      }
    };

    fileReader.onerror = (error) => {
      console.error('onerror', error);
    };

    fileReader.readAsArrayBuffer(file);

    return false;
  };
5
  • 1
    ...Extract public key... from what? If you mean a private PEM key: There is information missing about the format. Private RSA keys can be in PKCS#8 or PKCS#1 format, private EC keys in PKCS#8 or SEC1. In addition, private EC keys do not necessarily have to contain the public key component. In this case, the public key must be calculated. Commented Jan 15 at 8:26
  • Perhaps it is best if you post non-productive sample keys (for ECDSA and EdDSA, since RSA seems to work). Commented Jan 15 at 8:30
  • the public key that i am talking about is part of the der file which is in binary. I need to decode it and extract it out. for encoding when the algo used was RSA, those der files can be parsed. for encoding done with eddsa and ecdsa, I am unable to parse those files. Commented Jan 15 at 9:15
  • 1
    ...the public key...is part of the der file which is in binary... unfortunately does not answer any of the ambiguities. Without knowing what the public ECDSA/EdDSA key is to be extracted from, this question can probably not be answered. Commented Jan 15 at 9:30
  • As a side note: If you are using NodeJS, then NodeJS' crypto module might be a better option as it supports import/export of PEM keys for various algorithms. Commented Jan 15 at 9:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.