2

When converting a password-protected PEM/PKCS1-encoded private key to DER format one is not able to encrypt the key, OpenSSL automagically asks for the password and writes the plain-text key in the output file.

However, I understand that PKCS8-encoding does support encryption when converted to DER format, my problem is that I can't make OpenSSL write an encrypted file. I have tried:

openssl pkey -inform PEM -in key_pkcs8_encrypted.pem -outform DER -out key_pkcs8_encrypted.der

...but OpenSSL asks for the password and writes a decrypted file (verified by diffing it with the DER version of the decrypted PEM key).

I have also tried:

openssl rsa -des3 -in key_pkcs8_unencrypted.pem -outform DER -out key_pkcs8_encrypted.der

...in the hope that OpenSSL would ask for a password and encrypt the output file, as it does in the -outform PEM case but it does not, it just writes an unencrypted file.

And finally I have tried:

openssl rsa -des3 -in key_pkcs8_unencrypted.pem -passout pass:mypassword -outform DER -out key_pkcs8_encrypted.der

...but that also writes an unencrypted file.

Am I correct that PEM/PKCS8 when converted to DER does support encryption and, if so, does anyone know how I might persuade OpenSSL to do it for me?

Edit: I am aware of this post but it doesn't address the part about how OpenSSL can be asked to generate a password-protected DER file.

14
  • Ah, I've just tried openssl pkcs8 -topk8 -inform pem -in key_pkcs1_encrypted.pem -outform DER -out key_pkcs8_encrypted.der and that asked for both decryption and encryption passwords, plus the DER file is now different, so I guess that's done it? – Rob Feb 11 at 15:24
  • Feel free to answer your own question below. – MechMK1 Feb 11 at 15:43
  • 1
    Does this answer your question? Is it possible to protect a DER private key with a password? – gowenfawr Feb 11 at 17:12
  • 1
    It's inconsistent. pkey [-outform pem] writes PKCS8 PEM and can be encrypted, but pkey -outform der writes traditional DER for most algorithms (PKCS1 for RSA, SEC1 for ECC, adhoc for DSA) which cannot, but since OpenSSL had no traditional key format for DH it writes that as PKCS8 (but still doesn't encrypt!). pkcs8 -topk8 -outform der is indeed the only way at commandline to create PKCS8-encrypted DER. (rsa and also genrsa writes traditional PEM or traditional=PKCS1 DER, never any PKCS8.) ... – dave_thompson_085 Feb 12 at 5:28
  • 1
    ... pkey -inform der uses an awful kludge called d2i_AutoPrivateKey and can actually read all traditional formats and PKCS8-clear but not PKCS8-encrypted, as you tried. Bleah. Do you have any doc, or examples, of what these devices want? – dave_thompson_085 Feb 12 at 5:36
1

As @dave_thompson_085 above points out, there is only one way to do this with OpenSSL which is:

openssl pkcs8 -topk8 -in key_pkcs1_encrypted.pem -outform DER -out key_pkcs8_encrypted.der

As he says, beware that OpenSSL gets a bit "kludgy" where support for encrypted PKCS8 is concerned (specifically pkey cannot read PKCS8-encrypted format input files).

1
  • You should be able to accept your own answer by now (there is a 2 days minimum). – Maarten Bodewes Mar 14 at 14:25

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.