Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Jobs might require variables for parameterization that hold secrets. We find a number of requirements for management of such variables, see JS7 - How to encrypt and decrypt Variables

The preferred solution with JS7 is to use asymmetric keys:, for details see JS7 - Encryption and Decryption.

  • Encryption and decryption can be performed directly by related jobs or outside of JS7 products.
  • This includes that JS7 products have no knowledge of secrets involved that potentially could be compromised by logging, database persistence etc.

For creation of Encryption Keys see JS7 - How to create X.509 Encryption Keys.

Display feature availability
StartingFromRelease2.5.9

Display feature availability
StartingFromRelease2.6.6

Display feature availability
StartingFromRelease2.7.1

Download

The solution ships with JS7 Agents that can use encyption/decryption from with shell jobs out-of-the-box.

In addition, the solution is provided for download to perform encryption and decryption outside of JS7 products.

  • Download: JS7 - Download (Section: Utililties, Unix: js7.encryption.tar.gz, Windows: js7.encryption.zip),
  • The The solution is available for Linux, MacOS®, AIX® using bash, zsh, dash shell, see JS7 - How to encrypt and decrypt using Unix Shell. Encryption and decryption with Unix and Windows can be used interchangeably.
  • The solution is available for Windows® Shell.

Managing the Private Key and Certificate

Assymetric Asymetric encryption makes use of a Private Key and Certificate/Public Key that can be created in a number of ways:

  • Users can create a Certificate Signing Request (CSR) and ask their Certificate Authority (CA) to sign the CSR and to receive an X.509 certificate. The X.509 Private Key or Certificate X.509 Certificate allow to derive the Public Key.
  • User can create a selfCA-signed X.509 Certificate, see JS7 - How to create self-signed X.509 SSL TLS Certificates.
  • Users can create a Private Key and Certificate as explained in the next chapter.

...

Note:

...

Private Keys can be protected using a passphrase that acts as a second factor when a human user will access the key: while the Private Key is in the file system, the passphrase is in the user's brains. However, this does not improve security for unattended processing: it's pointless to store a passphrase side-by-side with the Private Key in scripts or configuration files on the same media.

Step 1: Creating the Private Key and Certificate

The following step is performed on the server hosting the Agent that should decrypt secrets. The example makes use of the openssl command line utility that can be installed for Windows. There are alternative ways how to create Private Keys and Certificates. Find more examples and explanations from JS7 - How to create X.509 Encryption Keys.

Code Block
languagebash
titleExample how to create Private Key and Certificate using ECDSA encryption
linenumberstrue
collapsetrue
@rem
Code Block
languagebash
titleExample how to create ECDSA private key and certificate
linenumberstrue
# navigate to the Agent's <agent-data>/\config/\private directory
cd /var/%Programdata%\sos-berlin.com/\js7/\agent/\config/\private

#@rem create thePrivate privateKey
@rem key  infor pkcs#1use formatwith 
#passphrase add:  without passphrase
# -passout pass:"secret"
openssl ecparam -name secp256k1secp384r1 -genkey -noout -out agent.key
#
@rem create Certificate withSigning passphraseRequest
openssl ecparamreq -genkeynew -namesha512 secp256k1-nodes | openssl ec -aes256 -passout pass:"jobscheduler" -out agent.key

# create certificate
openssl req -new -x509 -key agent.key-key agent.key -out agent.csr -subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=Agent"

@rem create Certificate
set user_crt_tmp_file=user-crt-%RANDOM%.tmp
echo keyUsage=critical,keyAgreement,keyEncipherment > %user_crt_tmp_file%

@rem   for passphrase add: -passin pass:"jobschedulersecret"
openssl x509 -out agent.crtreq -sha512 -days 1825

# extract public key from private key (not required)
# openssl ec-signkey agent.key -in agent.keycsr -passin pass:"jobscheduler"out agent.crt -pubout > agent.pubextfile %user_crt_tmp_file%
del /q %user_crt_tmp_file%
Code Block
languagebash
titleExample how to create RSA private key and certificatePrivate Key and Certificate using RSA encryption
linenumberstrue
collapselinenumberstrue
#@rem navigate to the Agent's <agent-data>/\config/\private directory
cd /var/%Programdata%\sos-berlin.com/\js7/\agent/\config/\private

#@rem create thePrivate privateKey keyand inCertificate pkcs#1Signing formatRequest
#@rem   for passphrase without passphrase
# add: -passout pass:"secret"
openssl req -x509 -sha256new -newkey rsa:20484096 -sha256 -nodes -keyout agent.key -out agent.crt
#   with passphrase
openssl req -x509 -sha256 -newkey rsa:2048 -passout pass:"jobscheduler" -keyout agent.key -out agent.crt

# extract public key from certificate (not required)
# openssl x509 -pubkey -noout -in agent.crt > agent.pub

Step 2: Making the Certificate available

csr -subj "/C=DE/ST=Berlin/L=Berlin/O=SOS/OU=IT/CN=Agent"

@rem create Certificate
set user_crt_tmp_file=user-crt-%RANDOM%.tmp
echo keyUsage=critical,keyAgreement,keyEncipherment > %user_crt_tmp_file%

@rem   for passphrase add: -passin pass:"secret"
openssl x509 -req -sha512 -days 1825 -signkey agent.key -in agent.csr -out agent.crt -extfile %user_crt_tmp_file%
del /q %user_crt_tmp_file%

Step 2: Making the Certificate available

Copy the Certificate file to the server(Copy the Certificate file to the server(s) hosting the Agent(s) or 3rd-party components that should encrypt variables:

...

Encryption

...

languagebash
titleExample where to copy the certificate
linenumberstrue

...

# navigate to the Agent's <agent-data>/config directory
Set-Location $env:ProgramData/sos-berlin.com/js7/agent/config
# cd %ProgramData%/sos-berlin.com/js7/agent/config

# copy the agent.crt certificate file from to the current location using a file transfer tool or your clipboard

Usage

Invoking the script without arguments displays the usage clause:


Code Block
titleUsage
Usage: js7_encrypt.cmd [Options] [Switches]

  Options:
     --cert=<path-to-certificate>  | path to X.509 certificate or public key file used to encrypt the secret.
     --in=<secret>                 | secret that should be encrypted.
     --infile=<path-to-file>       | path to input file

Encryption

Usage

Invoking the script without arguments displays the usage clause:

Code Block
titleUsage
Usage: js7_encrypt.cmd [Options] [Switches]

  Options:
     --cert=<path-to-certificate>  | path to X.509 certificate or public key file used to encrypt the secret.
     --in=<secret>outfile=<path-to-file>      | path to         | secret output file that should be encrypted.

  Switches:
   --infile=<path-to-file>      -h | path--help to input file.
     --outfile=<path-to-file>      | path to output file that| should be encrypted.

  Switches:
     -h | --help                   | displays usagedisplays usage

Options

  • --cert
    • Specifies the path to a file that holds the CA signed or self-signed X.509 Certificate. Alternatively the path to a file holding the Public Key can be specified.
  • --in
    • Specifies the input value that should be encrypted, typically a secret.
    • One of the options --in or --infile has to be specified.
  • --infile
    • Specifies the path to the input file that should be encrypted.
    • One of the options --in or --infile has to be specified.
    • This option requires use of the --outfile option.
  • --outfile
    • Specifies the path to the output file that will be created holding the encrypted content of the input file.
    • The option is required if the --infile option is specified.

...

Code Block
titleExample for Encryption using Windows Shell
linenumberstrue
call .\bin\js7_encrypt.cmd "--cert=agent.crt" "--in=secret"
@echo %JS7_ENCRYPT_VALUE%
 
@rem encrypts the given secret using an Agent's X.509 certificate
@rem consider that for Windows Shell all arguments have to be quoted
@rem output is provided from an environment variable that includes the encrypted symmetric key, initialization vector and encrypted stringsecret separated by space

Encrypting and forwarding Secret using Windows Shell in Jobs

...

Code Block
titleExample for Encryption using Windows Shell
linenumberstrue
echo secret file > %TEMP%\secret.txt
call .\bin\js7_encrypt.cmd "--cert=agent.crt" "--infile=%TEMP%\secret.txt" "--outfile=%TEMP%\secret.txt.encrypted"
@echo %JS7_ENCRYPT_VALUE%

@rem encrypts the given file using an Agent's X.509 certificate
@rem consider that for Windows Shell all arguments have to be quoted
@rem output is available from the JS7_ENCRYPT_VALUE environment variable
@rem output includes the encrypted symmetric key, initialization vector and path to encrypted file separated by spacespaces

Decryption

Usage

Invoking the script without arguments displays the usage clause:

...

Code Block
titleUsage
Usage: js7_decrypt.cmd [Options] [Switches]

  Options:
     --key=<path>                  | path to private key file for decryption.
     --key-password=<password>     | password for the private key.
     --ivin=<initialization-vector><encrypted-result>       | base64 encodedencrypted symmetric key, initialization vector ( and path to encrypted file as returned by encryption).
     --encrypted-key=<key>infile=<path-to-file>         | base64path encodedto encrypted symmetricinput keyfile.
 (returned by encryption).
     --inoutfile=<encrypted-secret> <path-to-file>      | encryptedpath secretto todecrypted decryptoutput (returned by encryption).file. 

  Switches:
     --infile=<path-to-file>h | --help        | path to encrypted input file.
     --outfile=<path-to-file>      | path to decrypted output file. 

  Switches:
     -h | --help                   | displays usage

Options

 | displays usage

Options

  • --key
    • Specifies the path to a the Private Key file that matches the X.509 Certificate or Public Key used for previous encryption.
    • The argument is required.
  • --key-password
    • Specifies the passphrase if the Private Key file indicated with the --key option is protected by a passphrase.
  • --in
    • Specifies the encryption result that should be decrypted. The result includes the encrypted symmetric key, initialization vector and path to encrypted file separated by spaces as returned from the encryption step.
    • The argument is required. If the option --infile is specified, then its value takes precedence to the path specified with the --in option.
  • --infile
    • Specifies the path to an encrypted file that should be decrypted.
    • If this option is specified then its value takes precedence to the path specified with the --in option
  • --key
    • Specifies the path to a the Private Key file that matches the X.509 Certificate or Public Key used for previous encryption.
    • The argument is required.
  • --key-password
    • Specifies the password if the Private Key file indicated with the --key option is protected by a password.
  • --iv
    • Specifies the base64 encoded initialization vector as returned during encryption.
    • The argument is required.
  • --encrypted-key
    • Specifies the base64 encoded, encrypted symmetric key as returned during encryption.
    • The argument is required.
  • --in
    • Specifies the encrypted value that should be decrypted.
    • One of the options --in or --infile has to be specified.
  • --infile
    • Specifies the path to an encrypted file that should be decrypted.
    • One of the options --in or --infile has to be specified.
    • This option requires use of the --outfile option.
  • --outfile
    • Specifies the path to the output file that will be created holding the decrypted content of the input file.
    • The option is required if the --infile option is specified.

...

  • 1: argument errors
  • 2: processing errors

Return Values

The script creates the following environment variables:

  • JS7_DECRYPT_VALUE holds the decrypted secret if the --in option is used,
  • JS7_DECRYPT_FILE holds the path to the decrypted output file if the --infile and --outfile options are used.

Examples

The following examples illustrate typical use cases.

Decrypting Secret using Windows Shell

Code Block
titleExample for Decryption using Windows Shell
linenumberstrue
@rem call .\bin\js7_encrypt.cmd "--cert=agent.crt" "--in=secret"

for /f "tokens=1-3" %%i in ("%JS7_ENCRYPT_VALUE%") do (
    set encrypted_symmetric_key=%%i
    set encrypted_base64_iv=%%j
    set encrypted_string=%%k
)
 
call .\bin\js7_decrypt.cmd ^
    "--key=agent.key" ^
    "--key-password=jobscheduler" ^
    "--encrypted-key=%encrypted_symmetric_key%" ^
    "--iv=%encrypted_base64_iv%" ^
    "--in=%encrypted_string%"
@echo %JS7_DECRYPT_VALUE%

@rem decrypts the encrypted secret using an Agent's private key and passphrase
@rem consider that for Windows Shell all arguments have to be quoted
@rem the JS7_DECRYPT_VALUE environment variable is automatically created and holds the decrypted secret

...

The script creates the following environment variables:

  • JS7_DECRYPT_VALUE holds the decrypted secret if the --in option is used,
  • JS7_DECRYPT_FILE holds the path to the decrypted output file if the --infile and --outfile options are used.

Examples

The following examples illustrate typical use cases.

Decrypting Secret using Windows Shell

Code Block
titleExample for Decryption using Windows Shell
linenumberstrue
@rem call .\bin\js7_encrypt.cmd "--cert=agent.crt" "--in=secret"

@rem call .\bin\js7_encryptdecrypt.cmd ^
    "--certkey=agent.crt"key" ^
    "--in=%JS7_ENCRYPT_VALUE%"
@echo %JS7_DECRYPT_VALUE%

@rem decrypts the encrypted result using an Agent's private key
@rem consider that for Windows Shell all arguments have to be quoted
@rem the JS7_DECRYPT_VALUE environment variable is automatically created and holds the decrypted secret

Decrypting File using Windows Shell

Code Block
titleExample for Decryption using Windows Shell
linenumberstrue
@rem infile=%TEMP%\secret.txt" "--outfile=%TEMP%\secret.txt.encrypted"

for /f "tokens=1-3" %%i in ("%JS7_ENCRYPT_VALUE%") do (
    set encrypted_symmetric_key=%%i
    set encrypted_base64_iv=%%j
    set encrypted_file=%%k
)

call .\bin\js7_decryptencrypt.cmd ^
    "--keycert=agent.keycrt" ^
    "--key-password=jobscheduler" "--infile=%TEMP%\secret.txt" "--outfile=%TEMP%\secret.txt.encrypted"

call .\bin\js7_decrypt.cmd ^
    "--encrypted-key=%encrypted_symmetric_key%agent.key" ^
    "--ivin=%encrypted%JS7_base64ENCRYPT_iv%VALUE%" ^
    "--infile=%encrypted_file%%TEMP%\secret.txt.encrypted" ^
    "--outfile=%TEMP%\secret.txt.decrypted"
type %TEMP%\secret.txt.decrypted

@rem decrypts the given encrypted file using an Agent's private key and passphrase
@rem the JS7_ENCRYPT_VALUE environment variable is returned in the encryption step and holds the encrypted symmetric key, initialization vector and path to the encrypted file
@rem consider that for Windows Shell all arguments have to be quoted
@rem output includes the path to the decrypted file that is provided from the JS7_DECRYPT_FILE environment variable


Further Resources