0

Hope I got the terms correct.

I currently have an EC public key in the form

-----BEGIN PUBLIC KEY-----
xxxxx
-----END PUBLIC KEY-----

which I understand is the SubjectPublicKeyInfo form. I would like to convert this into the form

-----BEGIN EC PUBLIC KEY-----
xxxxx
-----END EC PUBLIC KEY-----

using commands like openssl or similar. I can't seem to find a RSAPublicKey_{in|out} for the openssl ec command. I would like to be able to convert the public key in both DER and PEM forms.

Is there any other way to do this?

  • Is this your answer? – ckujau Mar 7 '17 at 4:40
  • Unfortunately, no. That's for RSA Keys only. I cannot find the analogue for EC keys. – Lawliet Mar 7 '17 at 5:33
2

OpenSSL does not support any "BEGIN EC PUBLIC KEY" format and (AFAIK) no such format exists. There is a "traditional" public key format for RSA ("BEGIN RSA PUBLIC KEY"), which is essentially a PEM encoded PKCS#1 structure. Later standards defined how to encode public keys in an algorithm agnostic way (SubjectPublicKeyInfo) and so there isn't always a "traditional" format version.

You can check the known OpenSSL PEM types by looking in the openssl pem header file (include/openssl/pem.h). You will notice that there is no "EC PUBLIC KEY" entry. There is an "RSA PUBLIC KEY", "DSA PUBLIC KEY" and "ECDSA PUBLIC KEY", but the latter two are not used anywhere in the OpenSSL codebase (i.e. they only exist in the header file).

There are "traditional" formats for private keys. To convert a PKCS8 private key to traditional form use:

openssl ec -in p8file.pem -out tradfile.pem

The ec command also recognises the "-inform DER" and "-outform DER" options. So, for example, to convert a DER PKCS8 private key file into PEM traditional you could use:

openssl ec -inform DER -in p8file.der -out tradfile.pem

Or to convert a PEM PKCS8 private key file into DER traditional use:

openssl ec -in p8file.pem -outform DER -out tradfile.der

You can also go the other way, i.e. traditional form private key to PKCS8:

openssl pkcs8 -topk8 -nocrypt -in tradfile.pem -out p8file.pem

The pkcs8 command above also recognises the -inform and -outform options.

  • This does not work: $ openssl ec -in ecdsa_public_key.pem -out test.pem read EC key unable to load Key 140111551870616:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:701:Expecting: ANY PRIVATE KEY Even if you add -pubin and pubout, it doesn't change the key format. I've tried this before. – Lawliet Mar 8 '17 at 1:23
  • Ah. Sorry. Misread the original question. My answer was for private keys. I have edited the answer to provide additional info about public keys too. – Matt Caswell Mar 8 '17 at 10:10
  • Thanks for the clarification. I must have been mistaken when I saw BEGIN EC PUBLIC KEY somewhere. Do you know how I could get ECDSA PUBLIC KEY instead? EDIT: Oops. I just saw they're not used. Thanks – Lawliet Mar 8 '17 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.