kaikramer / keystore-explorer

KeyStore Explorer is a free GUI replacement for the Java command-line utilities keytool and jarsigner.
https://keystore-explorer.org/
GNU General Public License v3.0
1.7k stars 275 forks source link

Create Keystore P12 with Certificate and a Private key #185

Closed matteshe closed 5 years ago

matteshe commented 5 years ago

Describe the solution you'd like I have a certificate and I have private key. I'd like to sign the certificate with the priv key and put this in a p12 keystore. At the moment I can't create a cert/key pair with this given files.

With openssl I can use the options pkcs12 -export -in cert -inkey privkey -out p12

kaikramer commented 5 years ago

The KSE equivalent of this OpenSSL command would be:

  1. Create a new keystore, select type PKCS#12: grafik
  2. Click "Import Key Pair" in Tools menu: grafik
  3. Choose either "PKCS#8" (more likely) or "OpenSSL" (that's an outdated OpenSSL format): grafik
  4. It seems the private key file is unencrypted, so unselect this option, then browse to the two files: grafik

The rest should be self-explaining.

matteshe commented 5 years ago

Hi,

I alreday tried this ways, but I wasn't able to do it. I received the following errors.

Import with openssl: priv key and cert does not contain a valid key pair import_openssl

Import with pkcs8: private key could not be read import_pkcs8

It look like, that the key need to be converted first: http://openssl.cs.utah.edu/docs/apps/ec.html

The ec command processes EC keys. They can be converted between various forms and their components printed out. Note OpenSSL uses the private key format specified in 'SEC 1: Elliptic Curve Cryptography' (http://www.secg.org/). To convert a OpenSSL EC private key into the PKCS#8 private key format use the pkcs8 command.

kaikramer commented 5 years ago

Ok, it seems like the key actually is in the old OpenSSL format. Could you please use the "Detect File Type" function on the key file:

grafik

It will probably say something like "unencrypted OpenSSL private key".

And also the output of this command (delete the key data as in the following example):

$ openssl ec -in ec.key -noout -text
read EC key
Private-Key: (256 bit)
priv:
     <deleted>
pub:
     <deleted>
ASN1 OID: prime256v1
NIST CURVE: P-256
kaikramer commented 5 years ago

Oh, and please try "Examine File" on the key file as well and post the error message.

matteshe commented 5 years ago

Hi,

as you said, the tools as determined a unencrypted private key.

Detect File type of private key 2019-07-30 12_17_07-Kryptographischer Dateityp

Detect File type of certificate 2019-07-30 12_17_25-Kryptographischer Dateityp

openssl ec -in privkey-PK.pem -noout -text

read EC key Private-Key: (256 bit) priv:

pub: ASN1 OID: prime256v1 NIST CURVE: P-256 No Error Messages on Examine Files: Examine Private Key: ![2019-07-30 12_22_48-Unbenannt 1 _ - KeyStore Explorer 5 4 1](https://user-images.githubusercontent.com/7125692/62121900-d6e24d00-b2c4-11e9-91cf-397bd1d7ac79.png) Examile certificate: ![2019-07-30 12_24_36-KeyStore Explorer 5 4 1](https://user-images.githubusercontent.com/7125692/62122035-1c067f00-b2c5-11e9-88d7-38e1a6ca48e1.png)
matteshe commented 5 years ago

I just tried the following:

Import pkcs8 with my priv key and click detail ends in error 2019-07-30 12_31_03-Untitled-1 _ - KeyStore Explorer 5 4 1

Examine my priv key and use PEM Button to store new file and import that works. 2019-07-30 12_29_39-Private Key Details for File 'privkey-PK pem'

So why the examine file works but the detail button doesn't.

look inside both pem files:

my priv key -----BEGIN EC PRIVATE KEY----- deleted data -----END EC PRIVATE KEY-----

new stored priv key based on PEM button -----BEGIN PRIVATE KEY----- deleted data; differ from my -----END PRIVATE KEY-----

kaikramer commented 5 years ago

as you said, the tools as determined a unencrypted private key.

The important part is that it is in OpenSSL format (not PKCS#8), so you have to choose OpenSSL for import.

"Examine File" always shows "PKCS#8" format because the key is always internally converted to PKCS#8 regardless of the actual file format. The PEM button in "Examine File" therefore always outputs PKCS#8 format.

This PEM header means OpenSSL (RFC 5915) format: -----BEGIN EC PRIVATE KEY-----

And this PEM header means PKCS#8 (RFC 5208) format: -----BEGIN PRIVATE KEY-----

So far everything seems fine, the question now is why did the import as OpenSSL key not work. Does the import work, if you use the convrted PKCS#8 key file?

matteshe commented 5 years ago

Import as OpenSSL 2019-07-30 13_55_37-Untitled-1 _ - KeyStore Explorer 5 4 1_openssl

Detail on priv key and cert can be opened 2019-07-30 13_56_17-Untitled-1 _ - KeyStore Explorer 5 4 1detailworked

Import fails 2019-07-30 13_57_33-Untitled-1 _ - KeyStore Explorer 5 4 1

Import with converted file fail as well 2019-07-30 13_59_53-Untitled-1 _ - KeyStore Explorer 5 4 1

kaikramer commented 5 years ago

The converted file is PKCS#8, so you have to select this for the import instead of OpenSSL.

Are you sure the certificate matches the key?

matteshe commented 5 years ago

As I wrote above, import as pkcs8 with converted key and cert works. 2019-07-30 14_11_21-Untitled-1 _ - KeyStore Explorer 5 4 1

kaikramer commented 5 years ago

Ok, I've overlooked that. I think I have now all the information I need. Thanks for reporting this!