Search code examples
windowsopensslkeytool

Java keytool easy way to add server cert from url/port (for Windows)


I want to import CA certificate into a Java keystore. I found this answer with some Unix commands.

I understand that keytool works in Windows the same way as in Linux, doesn't it?

But what about openssl?

I installed openssl in Windows, then ran it and it works normally. But how can I replace a code below?

</dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${HOST}.cert

Or can I replace openssl with something else?


Solution

  • Probably the easiest way is to use KeyStore Explorer. Just use the "Examine SSL" feature and then click on "Import":

    enter image description here

    Alternatively you can replace </dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${HOST}.cert with manual steps. If you execute

    openssl s_client -connect -connect www.google.com:443
    

    it will output something like this:

    depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
    verify return:1
    depth=1 C = US, O = Google Trust Services, CN = Google Internet Authority G3
    verify return:1
    depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com
    verify return:1
    ---
    Certificate chain
     0 s:/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
       i:/C=US/O=Google Trust Services/CN=Google Internet Authority G3
     1 s:/C=US/O=Google Trust Services/CN=Google Internet Authority G3
       i:/OU=GlobalSign Root CA - R2/O=GlobalSign/CN=GlobalSign
    ---
    Server certificate
    -----BEGIN CERTIFICATE-----
    MIIEgjCCA2qgAwIBAgIIZ20sUt50wGgwDQYJKoZIhvcNAQELBQAwVDELMAkGA1UE
    BhMCVVMxHjAcBgNVBAoTFUdvb2dsZSBUcnVzdCBTZXJ2aWNlczElMCMGA1UEAxMc
    ...
    htkxJVFaLUXScpkNQagWzehkj9BGdV4oztKMgTU8hcZEsiDKF0aZmaMfAXAF6u0r
    0n9sKu3Ap0uSR0+G4PVDcJkJzw2UiUsu8IkkHy3HjKt4zCCrATjZ4FPgtFHSWv8d
    ZmUdWJhgJ30s/EvOKn1uQ6QlPWaCJKc9W6JaJQTX6PjPDMs0sK90ss9vCIJ0dDw5
    ud+EC4cT
    -----END CERTIFICATE-----
    subject=/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
    issuer=/C=US/O=Google Trust Services/CN=Google Internet Authority G3
    ---
    No client certificate CA names sent
    

    Mark the lines starting with -----BEGIN CERTIFICATE----- up to -----END CERTIFICATE----- and copy them into a text editor. Save it as [host].crt. Then execute the keytool command.

    If you want to automate these steps in a script, you'll have to install Cygwin or one of the suggestions in this answer here.