Command Line Utilities
Command Line Utilities
1Getting Started
2Basic Tasks
o 2.1Getting Help
o 2.2Getting Library Version Information
3Commands
o 3.1Standard Commands
o 3.2Generating an RSA Private Key
o 3.3Generating a Public Key
o 3.4Generating Keys Based on Elliptic Curves
3.4.1Generating the Curve Parameters
3.4.1.1Printing Parameters to Standard Out
3.4.1.2Printing Parameters as C Code
3.4.2Generating the Key
3.4.3Putting it All Together
o 3.5Base64 Encoding Strings
o 3.6Generating a File Hash
o 3.7File Encryption and Decryption
4Further reading
Getting Started
The entry point for the OpenSSL library is the openssl binary, usually /usr/bin/openssl on Linux. The general syntax
for calling openssl is as follows:
Before OpenSSL 3.0, you could call openssl without arguments to enter the interactive mode prompt and then
enter commands directly, exiting with either a quit command or by issuing a termination signal with
either Ctrl+C or Ctrl+D. The following is a sample interactive session in which the user invokes the prime command
twice before using the quit command to terminate the session.
Basic Tasks
This section is a brief tutorial on performing the most basic tasks using OpenSSL. For a detailed explanation of the
rationale behind the syntax and semantics of the commands shown here, see the section on Commands.
Getting Help
As mentioned previously, the general syntax of a command is openssl command [ command_options ] [ command_arguments ] .
The help command is no different, but it does have its idiosyncrasies. To view the top-level help menu, you can
call openssl as follows.
$ openssl help
$ openssl --help
$ openssl -h
This query will print all of the available commands, like so:
Standard commands
asn1parse ca ciphers cmp
cms crl crl2pkcs7 dgst
dhparam dsa dsaparam ec
...
Note the above output was truncated, so only the first four lines of output are shown.
The same output is obtained also with
A help menu for each command may be requested in two different ways. First, the same command used above may
be repeated, followed by the name of the command to print help for.
$ openssl help genpkey
The program will then display the valid options for the given command.
The second way of requesting the help menu for a particular command is by using the first option in the output
shown above, namely openssl command -help . Both commands will yield the same output; the help menu displayed
will be exactly the same.
For additional information on the usage of a particular command, the project manpages are the definite source of
information. The manpages may be views in a shell as usual, e.g.
$ man openssl
$ man openssl-genpkey
$ man genpkey
Another way of accessing the manpages is via the project perldocs. perldoc is a utility included with most if not
all Perl distributions, and it's capable of displaying documentation information in a variety of formats, one of which
is as manpages. Not surprisingly, the project documentation is generated from the pod files located in
the doc directory of the source code.
Getting Library Version Information
$ openssl version
OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022)
As mentioned above, the version command's help menu may be queried for additional options like so:
Using the -a option to show all version information yields the following output on my current machine:
$ openssl version -a
OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022)
built on: Fri Jun 24 08:58:53 2022 UTC
platform: linux-x86_64
options: bn(64,64)
compiler: gcc -fPIC -pthread -m64 -Wa,--noexecstack -Wall -O3 -DOPENSSL_USE_NODELETE -DL_ENDIAN -DOPENSSL_PIC -
DOPENSSL_BUILDING_OPENSSL -DNDEBUG
OPENSSLDIR: "/usr/local/ssl"
ENGINESDIR: "/usr/local/lib64/engines-3"
MODULESDIR: "/usr/local/lib64/ossl-modules"
Seeding source: os-specific
CPUINFO: OPENSSL_ia32cap=0x7ffaf3ffffebffff:0x29c67af
Commands
There are three different kinds of commands. These are standard commands, cipher commands, and message digest
commands. Calling the OpenSSL top-level help command with no arguments will result in openssl printing all
available commands by group, sorted alphabetically.
Standard Commands
Overview of OpenSSL's command line utilities
Command Description
asn1parse Parse an ASN.1 sequence.
ca Certificate Authority (CA) Management.
ciphers Cipher Suite Description Determination.
cmp Certificate Management Protocol (CMP, RFC 4210) application.
cms CMS (Cryptographic Message Syntax) utility.
crl Certificate Revocation List (CRL) Management.
crl2pkcs7 CRL to PKCS#7 Conversion.
dgst Message Digest calculation. MAC calculations are superseded by mac(1).
Generation and Management of Diffie-Hellman Parameters. Superseded by
dhparam
genpkey(1) and pkeyparam(1).
dsa DSA Data Management.
DSA Parameter Generation and Management. Superseded by genpkey(1) and
dsaparam
pkeyparam(1).
ec EC (Elliptic curve) key processing.
ecparam EC parameter manipulation and generation.
enc Symmetric cipher routines.
engine Engine (loadable module) information and manipulation.
errstr Error Number to Error String Conversion.
fipsinstall IPS configuration installation.
Generation of DSA Private Key from Parameters. Superseded by genpkey(1) and
gendsa
pkey(1).
genpkey Generation of Private Key or Parameters.
genrsa Generation of RSA Private Key. Superseded by genpkey(1).
help Display information about a command's options.
info Display diverse information built into the OpenSSL libraries.
kdf Key Derivation Functions.
list List algorithms and features.
mac Message Authentication Code Calculation.
nseq Create or examine a Netscape certificate sequence.
ocsp Online Certificate Status Protocol utility.
passwd Generation of hashed passwords.
pkcs12 PKCS#12 Data Management.
pkcs7 PKCS#7 Data Management.
pkcs8 PKCS#8 format private key conversion tool.
pkey Public and private key management.
pkeyparam Public key algorithm parameter management.
pkeyutl Public key algorithm cryptographic operation utility.
prime Compute prime numbers.
rand Generate pseudo-random bytes -- see Random Numbers
rehash Create symbolic links to certificate and CRL files named by the hash values.
req PKCS#10 X.509 Certificate Signing Request (CSR) Management.
rsa RSA key management.
RSA utility for signing, verification, encryption, and decryption. Superseded by
rsautl
pkeyutl(1).
This implements a generic SSL/TLS client which can establish a transparent
s_client
connection to a remote server speaking SSL/TLS.
This implements a generic SSL/TLS server which accepts connections from remote
s_server
clients speaking SSL/TLS.
s_time SSL Connection Timer.
sess_id SSL Session Data Management.
smime S/MIME mail processing.
speed Algorithm Speed Measurement.
spkac SPKAC printing and generating utility.
srp Maintain SRP password file.
storeutl Utility to list and display certificates, keys, CRLs, etc.
ts Time Stamping Authority tool (client/server).
verify X.509 Certificate Verification.
version OpenSSL Version Information.
x509 X.509 Certificate Data Management.
Generating an RSA Private Key
Generating a private key can be done in a variety of different ways depending on the type of key, algorithm, bits,
and other options your specific use case may require. In this example, we are generating a private key using RSA
and a key size of 2048 bits.
$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem
To generate a password protected private key, the previous command may be slightly amended as follows:
The addition of the -aes256 option specifies the cipher to use to encrypt the private key file. For a list of available
ciphers in the library, you can run the following command:
With your private key in hand, you can use the following command to see the key's details, such as its modulus and
its constituent primes. Remember to change the name of the input file to the file name of your private key.
The above command yields the following output in my specific case. Your output will differ but should be
structurally similar.
Keep in mind the above key was generated solely for pedagogical purposes; never give anyone access to your
private keys.
Generating a Public Key
Having previously generated your private key, you may generate the corresponding public key using the following
command.
You may once again view the key details, using a slightly different command this time.
The output for the public key will be shorter, as it carries much less information, and it will look something like
this.
For more information on generating keys, see the source code documentation, located in the doc/HOWTO/keys.txt file.
Generating Keys Based on Elliptic Curves
There are essentially two steps to generating a key:
1. Generate the parameters for the specific curve you are using
2. Use those parameters to generate the key
To see the list of curves instrinsically supported by openssl, you can use the -list_curves</t> option when calling
the ecparam command.
For this example I will use the prime256v1 curve, which is an X9.62/SECG curve over a 256 bit prime field.
And here are the first few lines of the corresponding output:
EC_GROUP *get_ec_group_256(void)
{
static unsigned char ec_p_256[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
...
This command will result in the generated key being printed to the terminal's output.
Just as with the previous example, you can use the pkey command to inspect your newly-generated key.
For more details on elliptic curve cryptography or key generation, check out the manpages.
Similarly, the base64 command's -d flag may be used to indicate decoding mode.
Note: base64 line length is limited to 76 characters by default in openssl (and generated with 64 characters per line).
openssl base64 -e <<< 'Welcome to openssl wiki with a very long line that splits...'
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRo
YXQgc3BsaXRzLi4uCg==
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
=> NOTHING!
To be able to decode a base64 line without line feeds that exceeds the default 76 character length restriction use the -A option.
It is recommended to actually split base64 strings into multiple lines of 64 characters, however, since the -A option is buggy, particularly with its
handling of long files.
For a list of the available digest algorithms, you can use the following command.
You can also use a similar command to see the available digest commands:
Below are three sample invocations of the md5, sha1, and sha384 digest commands using the same file as the dgst command invocation above.
$ openssl md5 primes.dat
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664
Having selected an encryption algorithm, you must then specify whether the action you are taking is either encryption or decryption via the -e or -
d flags, respectively. The -iter flag specifies the number of iterations on the password used for deriving the encryption key. A higher iteration
count increases the time required to brute-force the resulting file. Using this option implies enabling use of the Password-Based Key Derivation
Function 2, usually set using the -pbkdf2 flag. We then use the -salt flag to enable the use of a randomly generated salt in the key-derivation
function.
Putting it all together, you can see the command to encrypt a file and the corresponding output below. Note that the passwords entered by the user
are blank, just as they would usually be in a terminal session.
$ openssl enc -aes-256-cbc -e -iter 1000 -salt -in primes.dat -out primes.enc
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password: