Sample Script for Generating BYOK Secrets for Backup & Recover Next and Data 360
Shield Platform Encryption provides a script to help you upload customer-supplied key material for Backup & Recover Next and Data 360. It creates a root key that you can then upload to Salesforce. The script generates a random number as your secret and uses the public key from the certificate to encrypt the secret.
Required Editions
| Available in both Salesforce Classic (not available in all orgs) and Lightning Experience. |
| Available in: Enterprise, Performance, and Unlimited Editions with the Salesforce Shield or Shield Platform Encryption licenses. |
| Available for free in Developer Edition. |
Before using this script, first create a BYOK-compatible certificate and generate an import token. You only need a self-signed certificate. After you upload your root key, Salesforce automatically deletes the certificate.
This script creates a single file named EncryptedKeyMaterial.bin that’s ready to upload. It’s wrapped and encoded to meet the Shield Platform Encryption requirements.
- Create a file containing the script and save it in the same directory as the certificate and session token file you downloaded earlier.
-
Make sure that the script is executable with
chmod 775.By default, the temporary files aren’t deleted. Uncomment the final line of the script to have them deleted automatically. -
Run the script specifying the certificate name and session token file. For example, if you
named your script
2025_dc_rootkeygen.sh, and your certificate file is named2025_dc_byok.crt, run ./2025_dc_rootkeygen.sh 2025_dc_byok.crt. -
The script generates one file named EncryptedKeyMaterial.bin.
Upload EncryptedKeyMaterial.bin and the import token file for your self-signed certificate that you created earlier.
2025_dc_rootkeygen.sh file contents. You can run this script on Linux or MacOs
systems.
This script generates a secure root key that’s compatible with Backup & Recover Next and Data 360 data stores. You provide the import token and certificate that you generated on the BYOK page.
- Check that a certificate name is passed in and that it’s retrievable. Exit on failure.
- Extract the public key from the certificate. Exit on failure.
- Create the actual key material (in this script we use a 32-bit random number) as a file named PlainTextKeyMaterial.bin.
- Use openssl to wrap (encrypt) the PlainTextKeyMaterial.bin file with the certificate public key using the RSAES_OAEP_SHA_256 and RSA_4096 wrapping protocols. This process produces the EncryptedKeyMaterial.bin file.
- Optionally delete the interim plaintext files and the PublicKey.b64 input file.
You can then upload the EncryptedKeyMaterial.bin file and original import token to Salesforce.
#!/bin/bash
## BYOK for Backup & Recover Next Data 360 sample script to generate and prepare a root key for upload.
## Pass in as parameter: Certificate file (.crt) downloaded from the
## Generate Certificate page (referred in this script as certificate.crt or CERT_FILE).
## Validate input parameter
if [ -z "$1" ]; then
echo "Error: Please provide a certificate file as parameter."
echo "Usage: ./2025_dc_rootkeygen.sh certificate.crt."
exit 1
fi
if [ ! -f "$1" ]; then
echo "Error: File '$1' not found."
exit 1
fi
CERT_FILE="$1"
PUBLIC_KEY_FILE="public_key.pem"
## Step 1: Extract public key from certificate
echo "Extracting public key from certificate..."
openssl x509 -in "$CERT_FILE" -pubkey -noout > "$PUBLIC_KEY_FILE"
if [ $? -ne 0 ]; then
echo "Error: Failed to extract public key from certificate."
exit 1
fi
echo "Public key extracted to $PUBLIC_KEY_FILE."
## 3.0 Generate Key material as PlaintextKeyMaterial.bin
openssl rand -out PlaintextKeyMaterial.bin 32
## 3.1 wrap key material
# Note: we are using RSAES_OAEP_SHA_256 and RSA_4096 for wrapping
# Using PEM format public key directly
openssl pkeyutl \
-encrypt \
-in PlaintextKeyMaterial.bin \
-out EncryptedKeyMaterial.bin \
-inkey "$PUBLIC_KEY_FILE" \
-keyform PEM \
-pubin \
-pkeyopt rsa_padding_mode:oaep \
-pkeyopt rsa_oaep_md:sha256 \
-pkeyopt rsa_mgf1_md:sha256
echo "=== Output file:"
echo " EncryptedKeyMaterial.bin created."
## Optional cleanup. Remove the # from the next two lines if you want to delete the files automatically
# rm PlaintextKeyMaterial.bin
# rm "$PUBLIC_KEY_FILE"
