wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Using LetsEncrypt certificates in your JKS


Dealing with certificates in Java is always fun. The keystore Java uses is different from the certificate files you are used to in your web server or node.js.

Salesforce is build on Java, so we have to make peace with the Keystore. This article outlines the steps to use a LetsEncrypt certificate in a keystore. You will need:

  • Internet connection
  • OpenSSL installed
  • Able to run the LetsEncrypt challenge
  • Access to your DNS to add a record (for the challenge)
  • Java runtime installed
  • Public IP address

For this sample I will use the Domain “demo.example.com”

Obtaining a PEM certficate from LetsEncrypt

Easiest is to use the certbot utility on a Linux machine (e.g. spin up an instance on Heroku). DigitalOcean has detailed instructions.
There used to be a tls-sni challenge which was marked insecure, so you want to the DNS challenge.

sudo certbot certonly --manual --preferred-challenges dns -d demo.example.com

Convert PEM to PKCS12 format

First concatenate all PEM files into one. Presuming you used the Letsencrypt mechanism:

sudo cat /etc/letsencrypt/life/demo.example.com/*.pem > fullcert.pem

Then use OpenSSL to convert that into PKCS12 format. Note: if you do that on a Windows command prompt you must run the command prompt as administrator otherwise you just get an error

openssl pkcs12 -export -out fullchain.pkcs12 -in fullchain.pem

Prepare a Java JSK keystore

You can't just create an empty keystore, so create a new temp key and specify a new keystore, then delete that key. That gives you the empty keystore:

keytool -genkey -keyalg RSA -alias sfdcsec -keystore sfdcsec.ks
keytool -delete -alias sfdcsec -keystore sfdcsec.ks

Import pkcs12 into JKS

Almost final steps. Don't forget your passwords

keytool -v -importkeystore -srckeystore fullchain.pkcs12 -destkeystore sfdcsec.ks -deststoretype JKS

Adjust alias for Salesforce import

The Salesforce import utility is picky about Alias names. The previous import created the entry
Alias name: 1 which needs to be updated:

keytool -keystore sfdcsec.ks -changealias -alias 1 -destalias demo_example_com

And voilah, you have a properly signed certificate for your Salesforce instance. Downside: to be repeated every 90 days.

As usual YMMV!


Posted by on 22 March 2018 | Comments (4) | categories: Java Salesforce

Comments

  1. posted by BionicMessiah on Sunday 26 May 2019 AD:

    There's a typo in the 2nd code snippet. Should be /live/ instead of /life/


  2. posted by Justin on Tuesday 30 July 2019 AD:

    openssl pkcs12 -export -out fullchain.pkcs12 -in fullchain.pem

    should be

    openssl pkcs12 -export -out fullchain.pkcs12 -in fullcert.pem


  3. posted by MarkDenford on Tuesday 30 March 2021 AD:

    Saved me once again Stephan - thanks for this! Note, one thing I wrestled with was that these instructions produce an invalid JKS when using Java 11, but work for Java 1.8. For anyone else who finds this...


  4. posted by Thalapathy K on Friday 26 November 2021 AD:

    Thanks for the simple yet working explanation.