Sub topic 10
Creating a key pair and certificate for JWT signing
This chapter assumes you can run curl and openssl from a terminal. It also assumes you know what a public/private key pair is, and that you have a Salesforce org where you can edit a connected app. The chapter covers how to create the key material the JWT bearer flow depends on. It does not cover the flow itself. That comes in a later chapter.
Why the JWT bearer flow needs a key pair
In the JWT bearer flow, the integration does not send a secret to Salesforce. Instead it builds a small signed document called an assertion. It signs the assertion with a private key. Salesforce checks the signature with the matching public key, which you uploaded to the connected app as a certificate.
This splits the trust between two parties:
- The private key stays on the integration server. Only the integration can produce valid signatures.
- The certificate holds the public key. It is safe to share, and Salesforce stores it in the connected app.
If someone steals the certificate, they can verify signatures but cannot create them. If someone steals the private key, they can impersonate the integration. Everything in this chapter follows from that difference.
Generating the key and certificate
One openssl command creates both files. It produces a private key and a self-signed certificate in a single step.
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 \
-keyout server.key -out server.crt -subj "/CN=sf-jwt-integration"Each option has a purpose:
| Option | Meaning |
|---|---|
| `req -x509` | Create a self-signed certificate instead of a certificate signing request. |
| `-sha256` | Sign the certificate with SHA-256. |
| `-nodes` | Do not encrypt the private key with a passphrase. |
| `-days 365` | The certificate is valid for 365 days. |
| `-newkey rsa:2048` | Generate a new 2048-bit RSA key pair. |
| `-keyout server.key` | Write the private key to this file. |
| `-out server.crt` | Write the certificate to this file. |
| `-subj "/CN=sf-jwt-integration"` | Set the certificate subject, so openssl does not prompt for it. |
You now have two files. server.key is the private key and must never leave the integration server. server.crt is the certificate you upload to Salesforce.
Inspecting what you created
Check the files before you use them. A wrong file in the wrong place is a common cause of failed signatures later.
# Show the subject and validity dates of the certificate
openssl x509 -in server.crt -noout -subject -dates
# Confirm the private key is well formed
openssl rsa -in server.key -check -nooutThe first command prints the subject and the notBefore and notAfter dates. Note the notAfter date. After it, the certificate expires, and the integration will fail until you replace it.
To confirm that a key and a certificate belong together, compare their public key moduli. The two digests must match.
openssl x509 -in server.crt -noout -modulus | openssl md5
openssl rsa -in server.key -noout -modulus | openssl md5If the digests differ, you have a mismatched pair. Signatures made with the key will not verify against the certificate.
Uploading the certificate to the connected app
In Salesforce, open the connected app you set up earlier and edit its OAuth settings:
- Select Use digital signatures.
- Choose
server.crtand upload it. - Save the connected app.
Salesforce can take a few minutes to apply connected app changes. If the first token request fails right after the upload, wait and retry before you suspect the key.
Uploading the certificate does not grant any user access. The connected app must also pre-authorize the user the assertion names. The next chapter covers that step.
Choices that affect security and operations
Key size and algorithm
The JWT bearer flow signs with RS256, which is RSA with SHA-256. A 2048-bit RSA key meets this need. A larger key, such as 4096 bits, works too, but signing is slower and the added protection is rarely needed here. Do not use a different algorithm family, because the flow expects RS256 signatures.
Passphrase or no passphrase
The example uses -nodes, so the private key file is not encrypted. This is convenient because the integration can read it without a human typing a passphrase. The trade-off is that anyone who can read the file can use the key. If you encrypt the key, the integration needs the passphrase at startup, and you then have to store that passphrase somewhere safe. Pick one of these deliberately:
| Approach | Benefit | Cost |
|---|---|---|
| Unencrypted key with strict file permissions | Simple, works unattended | Anyone with file access has the key |
| Encrypted key with passphrase from a secret store | Stolen file is not enough | More setup, and the passphrase is another secret |
| Key held in a secrets manager or hardware module | Key never sits on disk | Needs platform support |
Whichever you choose, restrict the file so only the integration's account can read it:
chmod 600 server.keySelf-signed versus CA-signed certificates
Salesforce does not need a certificate signed by a certificate authority for this flow. It only needs the public key inside the certificate. Salesforce does not check a chain of trust. A self-signed certificate is enough, and a CA-signed one adds no security here.
Expiry and rotation
A certificate has an end date, and the JWT flow breaks when it passes. Plan for replacement:
- Generate a new key and certificate before the old one expires.
- Upload the new certificate to the connected app.
- Deploy the new private key to the integration.
- Delete the old private key from every place it was stored.
The connected app holds one certificate at a time, so the swap in steps 2 and 3 causes a short window where signatures fail unless you coordinate. Do the swap in a maintenance window, or deploy the new key to the integration first and upload the certificate right after. Put the expiry date in a calendar or monitoring alert. A 365-day certificate that nobody tracks is a common cause of outages.
Common mistakes
- Uploading the private key. Only the certificate is uploaded. If you uploaded
server.key, treat it as exposed, create a new pair, and remove the old one. - Using separate runs of openssl for the key and certificate. Each run with
-newkeymakes a new key. If you run the command twice, theserver.crtandserver.keyfrom different runs do not match. Use the modulus check above. - Using one key across environments. Production and sandbox should each have their own key pair. A leaked sandbox key then does not affect production.
- Reading the key file in text mode or altering it. The Python example in the next chapters opens the key with
"rb". Stripping line breaks or adding extra characters breaks the PEM format and causes signing errors. - Forgetting the expiry. The certificate expires, and the integration fails with an authentication error.
Exercises
Exercise
You ran the openssl command once and have server.key and server.crt. Which file do you upload to the connected app, and which file stays on the integration server?
Show answer
Upload server.crt, the certificate containing the public key. Keep server.key, the private key, on the integration server. The private key must never be uploaded or shared.
Exercise
A teammate regenerated only server.key on the integration server and left the uploaded certificate unchanged. What happens, and how can you detect the mismatch before testing against Salesforce?
Show answer
Signatures made with the new private key will not verify against the old certificate, so Salesforce rejects the assertion. Compare the moduli of both files with openssl x509 -in server.crt -noout -modulus | openssl md5 and openssl rsa -in server.key -noout -modulus | openssl md5. The digests will differ. The fix is to upload a certificate that matches the new key, or restore the old key.
Exercise
Your certificate was created with -days 365. Name two actions you should take to avoid an outage when it expires.
Show answer
Track the expiry date (for example with a calendar entry or monitoring alert), and replace the key pair and uploaded certificate before that date. Coordinate the swap so the new private key is deployed to the integration and the new certificate is uploaded to the connected app at nearly the same time.