Introduction
JS7 - Encryption and Decryption offers a number of scenarios when to use encrypted values such as arguments for Jobs and Job Resources.
Use of certificates for encryption is not related to use of certificates to secure HTTPS connections, see JS7 - How to create X.509 SSL TLS Certificates.
Asymmetric encryption is based on a Private Key and Certificate/Public Key. The article suggests use of Certificates as they provide metadata about their origin and can be limited in lifetime.
Users can choose one of the approachs specified with RFC5280:
- Self-issued Certificates are created individually and are deployed from certificate files to parties that perform encryption.
- There is no security gap in use of self-issued Certificates. When users accept certificate files then this proves that they trust the certificates.
- Private CA-signed Certificates are issued by users who operate their own Private Certificate Authority (CA). The proceeding is not in scope of the article.
- Public CA-signed Certificates are issued by a trusted Certificate Authority (CA) that validates the domain owner. Such certificates are not created by users but are purchased from the trusted CA and are not in scope of this article.
Examples in the article make use of JS7 Release 2.7.2, OpenSSL 1.1.1k FIPS 25 Mar 2021 for Unix and OpenSSL 3.1.4 24 Oct 2023 for Windows. OpenSSL ships with Linux & other Unix OS and is available for Windows.
Creating self-issued Certificates
Creating the Private Key and Certificate Signing Request
The steps to create a Private Key and Certificate Signing Request are similar for self-issued Certificates and Private CA-signed Certificates.
Users have the option to use ECDSA or RSA algorithms for encryption performed using the Private Key.
Users can run the following commands from the shell and replace the value of the key_name
environment variable with a name of their choice that is used when creating related files.
Using ECDSA Encryption
# Specify key name used for file names
key_name=encryption
# Create Private Key
openssl ecparam -genkey -name secp384r1 -noout -out ${key_name}.key
# Create Certificate Signing Request
openssl req -new -sha512 -nodes \
-key ${key_name}.key \
-out ${key_name}.csr \
-subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=${key_name}"
Windows version...
@rem Specify key name used for file names
set key_name=encryption
@rem Create Private Key
openssl ecparam -genkey -name secp384r1 -noout -out %key_name%.key
@rem Create Certificate Signing Request
openssl req -new -sha512 -nodes ^
-key %key_name%.key ^
-out %key_name%.csr ^
-subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=%key_name%"
Explanations...
- Private Key
- Choice of algorithm such as
secp256k1
, secp384r1
depends on support by the Java version used with JS7.
- Certificare Signing Request
- The hash algorithm such as
-sha256, -sha512
can be freely chosen. - The
-subj
option specifies the Distinguished Name used for the subject of the CSR and resulting Certificate.- The Distinguished Name is a unique identifier frequently using the hierarchy of Country
C
, State ST
, Location L
, Organization O
, Organizational Unit OU
and Common Name CN
. - For self-issued Certificates the subject and issuer properties of the CSR/Certificate are the same. The minimum requirement is to specify the Common Name
CN=<name>
where <name>
can freely be chosen. - For Private CA-signed Certificates the subject property holds the certificate's Distinguished Name and the issuer property holds the Private CA Certificate's Distinguished Name that must be different.
- The following files will be created with this step:
- The
encryption.key
file will hold the Private Key. - The
encryption.csr
file will hold the Certificate Signing Request.
Using RSA Encryption
Click to expand/collapse...
# Specify key name used for file names
key_name=encryption
# Create Private Key and Certificate Signing Request
openssl req -new -newkey rsa:4096 -sha256 -nodes \
-keyout ${key_name}.key \
-out ${key_name}.csr \
-subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=${key_name}"
Windows version...
@rem Specify key name used for file names
set key_name=encryption
@rem Create Private Key and Certificate Signing Request
openssl req -new -newkey rsa:4096 -sha256 -nodes ^
-keyout %key_name%.key ^
-out %key_name%.csr ^
-subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=%key_name%"
Explanations...
- In the example the Private Key is created using the specified key size
4096
. - For use of the SHA hash algorithm
-sha256
and the -subj
option see Using ECDSA Encryption. - The following files will be created with this step:
- The
encryption.key
file will hold the Private Key. - The
encryption.csr
file will hold the Certificate Signing Request.
Creating the Encryption Certificate
Users can run the following commands from the shell and replace the value of the key_name
environment variable with a name of their choice that is used when creating related files.
# Specify key name used for file names
key_name=encryption
# Create Certificate
openssl x509 -req -sha512 -days 3652 \
-signkey ${key_name}.key \
-in ${key_name}.csr \
-out ${key_name}.crt \
-extfile <(printf "keyUsage=critical,keyEncipherment,keyAgreement\n")
Windows version...
@rem Specify key name used for file names
set key_name=encryption
@rem Create Certificate
set user_crt_tmp_file=user-crt-%RANDOM%.tmp
echo keyUsage=critical,keyAgreement,keyEncipherment > %user_crt_tmp_file%
openssl x509 -req -sha512 -days 3652 ^
-signkey %key_name%.key ^
-in %key_name%.csr ^
-out %key_name%.crt ^
-extfile %user_crt_tmp_file%
del /q %user_crt_tmp_file%
Explanations...
- The SHA option such as
-sha256, -sha384, -sha512
can be freely chosen. - The
-days
argument optionally specifies the validity period of the resulting certificate. - The
-signkey
option specifies the location of the Private Key file created from the previous step. - The
-in
option specifies the location of the Certificate Signing Request file created from the previous step. - The
-out
option specifies the location of the resulting Certificate file. - The
-extfile
option specifies a number of extensions recommended for use with Encryption Certificates. - The following files will be created with this step:
- The
encryption.crt
file will hold the self-issued Certificate.
Resources
Shell Script
As an alternative to running OpenSSL commands in an interactive shell, a script is provided that performs this task.
Download: example-create-keys.sh
#!/bin/bash
set -e
key_name=agent
key_passphrase=jobscheduler
key_type=ECDSA
if [ "${key_type}" == "ECDSA" ];then
# create Private Key
# for passphrase add: -passout pass:"${key_passphrase}"
openssl ecparam -name secp384r1 -genkey -noout -out ${key_name}.key
# create Certificate Signing Request
openssl req -new -sha512 -nodes \
-key ${key_name}.key \
-out ${key_name}.csr \
-subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=${key_name}"
else
# create Private Key and Certificate Signing Request
# for passphrase add: -passout pass:"${key_passphrase}"
openssl req -new -newkey rsa:4096 -sha256 -nodes \
-keyout ${key_name}.key \
-out ${key_name}.csr \
-subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=${key_name}"
fi
# create Certificate
# for passphrase add: -passin pass:"${key_passphrase}"
openssl x509 -req -sha512 -days 1825 \
-signkey ${key_name}.key \
-in ${key_name}.csr \
-out ${key_name}.crt \
-extfile <(printf "keyUsage=critical,keyEncipherment,keyAgreement\n")
# get Certificate information
openssl x509 -in ${key_name}.crt -noout -text
# extract Public Key from Certificate
# openssl x509 -pubkey -noout -in ${key_name}.crt > ${key_name}.pub
Links