Difference between revisions of "Command Line Utilities"

From OpenSSLWiki
Jump to navigationJump to search
(link to cookbook-style recipes, etc.)
(→‎Commands: remove pretty useless reference to Standard commands and non-existing ones to other types of commands)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[http://www.openssl.org/docs/apps/openssl.html OpenSSL site command line tools]
+
The '''openssl''' program provides a rich variety of commands, each of which often has a wealth of options and arguments. Many commands use an external configuration file for some or all of their arguments and have a <code>-config</code> option to specify that file. The environment variable [[OPENSSL_CONF]] can be used to specify the location of the configuration file. If the environment variable is not specified, a default file is created in the default certificate storage area called '''openssl.cnf'''. The settings in this default configuration file depend on the flags set when the version of OpenSSL being used was built.
  
=== Getting started with your openssl toolkit ===
+
This article is an overview of the available tools provided by OpenSSL. For all of the details on usage and implementation, you can find the [https://www.openssl.org/docs/manmaster/man1/ manpages], which are automatically generated from the source code at the [https://www.openssl.org/ official OpenSSL project home]. Likewise, the source code itself may be found on the [https://www.openssl.org/source/ OpenSSL project home page], as well as on the [https://github.com/openssl/openssl OpenSSL Github]. The main OpenSSL site also includes an [https://www.openssl.org/docs/manmaster/man1/openssl.html overview of the command-line utilities], as well as links to all of their respective documentation.
  
The openssl binary (usually /usr/bin/openssl on linux) is an entry point for many functions. You call it following the pattern
+
=Getting Started=
<pre>
 
$ openssl command [ command_opts ] [ command_args ]
 
</pre>
 
  
Alternatively you can call it without arguments to enter the interactive mode with an 'OpenSSL>' prompt. Then you can directly type your commands. You can leave the interactive mode with Ctrl+C or Ctrl+D or by typing 'quit':
+
The entry point for the OpenSSL library is the '''openssl''' binary, usually <tt>/usr/bin/openssl</tt> on Linux. The general syntax for calling '''openssl''' is as follows:
<pre>
 
OpenSSL> quit
 
</pre>
 
  
There are three different kinds of commands. These are [[standard commands]], [[cipher commands]], and [[message-digest commands]]. In the following an overview over some commands is given. These are grouped by purpose and not necessarily by the classification just mentioned.
+
$ openssl command [ command_options ] [ command_arguments ]
  
=== Learn about your installation ===
+
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 <code>quit</code> command or by issuing a termination signal with either <tt>Ctrl+C</tt> or <tt>Ctrl+D</tt>. The following is a sample interactive session in which the user invokes the [[prime]] command twice before using the <tt>quit</tt> command to terminate the session.
  
==== List commands by type ====
+
OpenSSL> prime -generate -bits 24
 +
13467269
 +
OpenSSL> prime -generate -bits 24
 +
16651079
 +
OpenSSL> quit
  
You can get a list of available commands by calling
+
=Basic Tasks=
  
<pre>
+
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|Commands]].
$ openssl list-standard-commands
 
$ openssl list-cipher-commands
 
$ openssl list-message-digest-commands
 
</pre>
 
  
==== version ====
+
==Getting Help==
  
OpenSSL> version
+
As mentioned previously, the general syntax of a command is <code>openssl command [ command_options ] [ command_arguments ]</code>. 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 1.0.1e 11 Feb 2013
 
  
==== ciphers ====
+
$ openssl help
  
returns SSL/TLS ciphers supported.
+
Since OpenSSL 3.0, there are equivalent invocations such as:
  
<code>
+
$ openssl --help
OpenSSL> ciphers
+
$ openssl -h
ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:SRP-DSS-AES-256-CBC-SHA:SRP-RSA-AES-256-CBC-SHA:DHE-DSS-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA256:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:DHE-RSA-CAMELLIA256-SHA:DHE-DSS-CAMELLIA256-SHA:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES256-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:CAMELLIA256-SHA:PSK-AES256-CBC-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:SRP-DSS-3DES-EDE-CBC-SHA:SRP-RSA-3DES-EDE-CBC-SHA:EDH-RSA-DES-CBC3-SHA:EDH-DSS-DES-CBC3-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-DES-CBC3-SHA:DES-CBC3-SHA:PSK-3DES-EDE-CBC-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:SRP-DSS-AES-128-CBC-SHA:SRP-RSA-AES-128-CBC-SHA:DHE-DSS-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-DSS-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA:DHE-RSA-SEED-SHA:DHE-DSS-SEED-SHA:DHE-RSA-CAMELLIA128-SHA:DHE-DSS-CAMELLIA128-SHA:ECDH-RSA-AES128-GCM-SHA256:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDH-RSA-AES128-SHA:ECDH-ECDSA-AES128-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:SEED-SHA:CAMELLIA128-SHA:PSK-AES128-CBC-SHA:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:ECDH-RSA-RC4-SHA:ECDH-ECDSA-RC4-SHA:RC4-SHA:RC4-MD5:PSK-RC4-SHA:EDH-RSA-DES-CBC-SHA:EDH-DSS-DES-CBC-SHA:DES-CBC-SHA:EXP-EDH-RSA-DES-CBC-SHA:EXP-EDH-DSS-DES-CBC-SHA:EXP-DES-CBC-SHA:EXP-RC2-CBC-MD5:EXP-RC4-MD5
 
</code>
 
  
openssl list-cipher-algorithms
+
This query will print all of the available commands, like so:
  
openssl list-public-key-algorithms
+
Standard commands
<pre>
+
asn1parse        ca                ciphers          cmp             
Name: OpenSSL RSA method
+
cms              crl              crl2pkcs7        dgst             
Type: Builtin Algorithm
+
dhparam          dsa              dsaparam          ec
OID: rsaEncryption
+
...
PEM string: RSA
 
Name: rsa
 
Type: Alias to rsaEncryption
 
Name: OpenSSL PKCS#3 DH method
 
Type: Builtin Algorithm
 
OID: dhKeyAgreement
 
PEM string: DH
 
Name: dsaWithSHA
 
Type: Alias to dsaEncryption
 
Name: dsaEncryption-old
 
Type: Alias to dsaEncryption
 
Name: dsaWithSHA1-old
 
Type: Alias to dsaEncryption
 
Name: dsaWithSHA1
 
Type: Alias to dsaEncryption
 
Name: OpenSSL DSA method
 
Type: Builtin Algorithm
 
OID: dsaEncryption
 
PEM string: DSA
 
Name: OpenSSL EC algorithm
 
Type: Builtin Algorithm
 
OID: id-ecPublicKey
 
PEM string: EC
 
Name: OpenSSL HMAC method
 
Type: Builtin Algorithm
 
OID: hmac
 
PEM string: HMAC
 
Name: OpenSSL CMAC method
 
Type: Builtin Algorithm
 
OID: cmac
 
PEM string: CMAC
 
</pre>
 
  
==== engine ====
+
Note the above output was truncated, so only the first four lines of output are shown.
  
OpenSSL> engine
+
The same output is obtained also with
(rsax) RSAX engine support
 
(dynamic) Dynamic engine loading support
 
  
==== speed ====
+
$ openssl list -standard-commands
  
returns informations of toolkit performance on cryptographic functions computations.
+
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.
  
( Ex: on Linux 3.1.0-1-amd64 #1 SMP x86_64 GNU/Linux, HP dv7 i7 4Gb )
+
$ openssl help genpkey
  
<pre>
+
The program will then display the valid options for the given command.
$ openssl speed
 
Doing md4 for 3s on 16 size blocks: 12430613 md4's in 3.00s
 
...
 
Doing md5 for 3s on 16 size blocks: 8943943 md5's in 2.99s
 
Doing md5 for 3s on 64 size blocks: 6560162 md5's in 3.00s
 
Doing md5 for 3s on 256 size blocks: 3674563 md5's in 3.00s
 
Doing md5 for 3s on 1024 size blocks: 1325803 md5's in 3.00s
 
Doing md5 for 3s on 8192 size blocks: 190271 md5's in 3.00s
 
Doing hmac(md5) for 3s on 16 size blocks: 7289025 hmac(md5)'s in 3.00s
 
Doing hmac(md5) for 3s on 64 size blocks: 5519732 hmac(md5)'s in 3.00s
 
Doing hmac(md5) for 3s on 256 size blocks: 3319123 hmac(md5)'s in 3.00s
 
Doing hmac(md5) for 3s on 1024 size blocks: 1275475 hmac(md5)'s in 3.00s
 
Doing hmac(md5) for 3s on 8192 size blocks: 187134 hmac(md5)'s in 3.00s
 
Doing sha1 for 3s on 16 size blocks: 10089842 sha1's in 2.99s
 
Doing sha1 for 3s on 64 size blocks: 7033355 sha1's in 3.00s
 
Doing sha1 for 3s on 256 size blocks: 3919372 sha1's in 3.00s
 
Doing sha1 for 3s on 1024 size blocks: 1374314 sha1's in 3.00s
 
Doing sha1 for 3s on 8192 size blocks: 198808 sha1's in 3.00s
 
Doing sha256 for 3s on 16 size blocks: 6462822 sha256's in 3.00s
 
Doing sha256 for 3s on 64 size blocks: 3504641 sha256's in 3.00s
 
Doing sha256 for 3s on 256 size blocks: 1486771 sha256's in 3.00s
 
Doing sha256 for 3s on 1024 size blocks: 440613 sha256's in 3.00s
 
Doing sha256 for 3s on 8192 size blocks: 58418 sha256's in 3.00s
 
Doing sha512 for 3s on 16 size blocks: 5040453 sha512's in 2.99s
 
Doing sha512 for 3s on 64 size blocks: 5089425 sha512's in 3.00s
 
Doing sha512 for 3s on 256 size blocks: 1865240 sha512's in 3.00s
 
Doing sha512 for 3s on 1024 size blocks: 643708 sha512's in 3.00s
 
Doing sha512 for 3s on 8192 size blocks: 90615 sha512's in 3.00s
 
...
 
Doing whirlpool for 3s on 8192 size blocks: 33204 whirlpool's in 3.00s
 
...
 
Doing rmd160 for 3s on 8192 size blocks: 66719 rmd160's in 3.00s
 
...
 
Doing rc4 for 3s on 8192 size blocks: 238972 rc4's in 3.00s
 
...
 
Doing des cbc for 3s on 8192 size blocks: 19837 des cbc's in 3.00s
 
...
 
Doing des ede3 for 3s on 8192 size blocks: 7706 des ede3's in 3.00s
 
...
 
Doing aes-128 cbc for 3s on 8192 size blocks: 35217 aes-128 cbc's in 3.00s
 
...
 
Doing aes-192 cbc for 3s on 8192 size blocks: 29225 aes-192 cbc's in 3.01s
 
...
 
Doing aes-256 cbc for 3s on 8192 size blocks: 24414 aes-256 cbc's in 3.00s
 
...
 
Doing aes-256 ige for 3s on 8192 size blocks: 23331 aes-256 ige's in 2.99s
 
...
 
</pre>
 
  
=== Basic encryption ===
+
$ openssl help genpkey
 +
General options:
 +
  -help              Display this summary
 +
  -engine val        Use engine, possibly a hardware device
 +
  -paramfile infile  Parameters file
 +
  -algorithm val      The public key algorithm
 +
  -quiet              Do not output status while generating keys
 +
  -pkeyopt val        Set the public key algorithm option as opt:value
 +
  -config infile      Load a configuration file (this may load modules)
 +
Output options:
 +
  -out outfile        Output file
 +
  -outform PEM|DER    output format (DER or PEM)
 +
  -pass val          Output file pass phrase source
 +
  -genparam          Generate parameters, not key
 +
  -text              Print the in text
 +
  -*                  Cipher to use to encrypt the key
 +
Provider options:
 +
  -provider-path val  Provider load path (must be before 'provider' argument if required)
 +
  -provider val      Provider to load (can be specified multiple times)
 +
  -propquery val      Property query used when fetching algorithms
 +
Order of options may be important!  See the documentation.
  
The command for symmetric encryption/decryption operations is [[enc|openssl enc]].
+
The second way of requesting the help menu for a particular command is by using the first option in the output shown above, namely <code>openssl command -help</code>. Both commands will yield the same output; the help menu displayed will be exactly the same.
  
For public key asymmetric encryption/decryption/sign/verify operations, use [[pkeyutl]] or the older RSA-specific [[rsautl]].
+
For additional information on the usage of a particular command, the project [https://www.openssl.org/docs/manmaster/man1/ manpages] are the definite source of information.
 +
The manpages may be views in a shell as usual, e.g.
  
==== Basic file ====
+
$ man openssl
 +
$ man openssl-genpkey
 +
$ man genpkey
  
to cipher a file or data to protect and share it protected by a shared key.
+
Another way of accessing the manpages is via the project perldocs. [https://perldoc.perl.org/perldoc perldoc] is a utility included with most if not all [https://www.perl.org/ 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 <tt>doc</tt> directory of the source code.
  
symmetric cipher :
+
==Getting Library Version Information==
[[AES]] [[Blowfish]] [[RC4]] [[3DES]] [[RC2]] [[DES]] [[CAST5]] [[SEED]]
 
  
block to stream conversion :
+
$ openssl version
[[ECB]] [[CBC]] [[OFB]] [[CFB]] [[CTR]] [[XTS]] [[GCM]]
+
OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022)
  
compression :
+
As mentioned above, the <tt>version</tt> command's help menu may be queried for additional options like so:
[[ZLIB]]
 
  
<pre>
+
$ openssl version -help
Cipher commands (see the `enc' command for more details)
+
Usage: version [options]
aes-128-cbc      aes-128-ecb      aes-192-cbc      aes-192-ecb     
+
General options:
aes-256-cbc      aes-256-ecb      base64            bf               
+
  -help Display this summary
bf-cbc            bf-cfb            bf-ecb            bf-ofb           
+
  Output options:
camellia-128-cbc camellia-128-ecb camellia-192-cbc  camellia-192-ecb 
+
  -a    Show all data
camellia-256-cbc  camellia-256-ecb  cast              cast-cbc         
+
  -b    Show build date
cast5-cbc        cast5-cfb        cast5-ecb        cast5-ofb       
+
  -d    Show configuration directory
des              des-cbc          des-cfb          des-ecb         
+
  -e    Show engines directory
des-ede          des-ede-cbc      des-ede-cfb      des-ede-ofb     
+
  -m    Show modules directory
des-ede3          des-ede3-cbc      des-ede3-cfb      des-ede3-ofb     
+
  -f    Show compiler flags used
des-ofb          des3              desx              rc2             
+
  -o    Show some internal datatype options
rc2-40-cbc        rc2-64-cbc        rc2-cbc          rc2-cfb         
+
  -p    Show target build platform
rc2-ecb          rc2-ofb          rc4              rc4-40           
+
  -r    Show random seeding options
seed              seed-cbc          seed-cfb          seed-ecb         
+
  -v    Show library version
seed-ofb          zlib
+
  -c    Show CPU settings info
</pre>
 
  
<pre>
+
Using the <tt>-a</tt> option to show all version information yields the following output on my current machine:
openssl enc --help
 
unknown option '--help'
 
options are
 
-in <file>    input file
 
-out <file>    output file
 
-pass <arg>    pass phrase source
 
-e            encrypt
 
-d            decrypt
 
-a/-base64    base64 encode/decode, depending on encryption flag
 
-k            passphrase is the next argument
 
-kfile        passphrase is the first line of the file argument
 
-md            the next argument is the md to use to create a key
 
                from a passphrase.  One of md2, md5, sha or sha1
 
-S            salt in hex is the next argument
 
-K/-iv        key/iv in hex is the next argument
 
-[pP]          print the iv/key (then exit if -P)
 
-bufsize <n>   buffer size
 
-nopad        disable standard block padding
 
-engine e      use engine e, possibly a hardware device.
 
Cipher Types
 
-aes-128-cbc              -aes-128-cfb              -aes-128-cfb1           
 
-aes-128-cfb8              -aes-128-ctr              -aes-128-ecb             
 
-aes-128-gcm              -aes-128-ofb              -aes-128-xts             
 
-aes-192-cbc              -aes-192-cfb              -aes-192-cfb1           
 
-aes-192-cfb8              -aes-192-ctr              -aes-192-ecb             
 
-aes-192-gcm              -aes-192-ofb              -aes-256-cbc             
 
-aes-256-cfb              -aes-256-cfb1              -aes-256-cfb8           
 
-aes-256-ctr              -aes-256-ecb              -aes-256-gcm             
 
-aes-256-ofb              -aes-256-xts              -aes128                 
 
-aes192                    -aes256                    -bf                     
 
-bf-cbc                    -bf-cfb                    -bf-ecb                 
 
-bf-ofb                    -blowfish                  -camellia-128-cbc       
 
-camellia-128-cfb          -camellia-128-cfb1        -camellia-128-cfb8       
 
-camellia-128-ecb          -camellia-128-ofb          -camellia-192-cbc       
 
-camellia-192-cfb          -camellia-192-cfb1        -camellia-192-cfb8       
 
-camellia-192-ecb          -camellia-192-ofb          -camellia-256-cbc       
 
-camellia-256-cfb          -camellia-256-cfb1        -camellia-256-cfb8       
 
-camellia-256-ecb          -camellia-256-ofb          -camellia128             
 
-camellia192              -camellia256              -cast                   
 
-cast-cbc                  -cast5-cbc                -cast5-cfb               
 
-cast5-ecb                -cast5-ofb                -des                     
 
-des-cbc                  -des-cfb                  -des-cfb1               
 
-des-cfb8                  -des-ecb                  -des-ede                 
 
-des-ede-cbc              -des-ede-cfb              -des-ede-ofb             
 
-des-ede3                  -des-ede3-cbc              -des-ede3-cfb           
 
-des-ede3-cfb1            -des-ede3-cfb8            -des-ede3-ofb           
 
-des-ofb                  -des3                      -desx                   
 
-desx-cbc                  -id-aes128-GCM            -id-aes192-GCM           
 
-id-aes256-GCM            -rc2                      -rc2-40-cbc             
 
-rc2-64-cbc                -rc2-cbc                  -rc2-cfb                 
 
-rc2-ecb                  -rc2-ofb                  -rc4                     
 
-rc4-40                    -rc4-hmac-md5              -seed                   
 
-seed-cbc                  -seed-cfb                  -seed-ecb               
 
-seed-ofb 
 
</pre>
 
  
==== Mail / SMIME ====
+
$ 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
  
===== smime v2 pkcs7 1.5 =====
 
  
<pre>
+
=Commands=
openssl smime --help
 
Usage smime [options] cert.pem ...
 
where options are
 
-encrypt      encrypt message
 
-decrypt      decrypt encrypted message
 
-sign          sign message
 
-verify        verify signed message
 
-pk7out        output PKCS#7 structure
 
-des3          encrypt with triple DES
 
-des          encrypt with DES
 
-seed          encrypt with SEED
 
-rc2-40        encrypt with RC2-40 (default)
 
-rc2-64        encrypt with RC2-64
 
-rc2-128      encrypt with RC2-128
 
-aes128, -aes192, -aes256
 
              encrypt PEM output with cbc aes
 
-camellia128, -camellia192, -camellia256
 
              encrypt PEM output with cbc camellia
 
-nointern      don't search certificates in message for signer
 
-nosigs        don't verify message signature
 
-noverify      don't verify signers certificate
 
-nocerts      don't include signers certificate when signing
 
-nodetach      use opaque signing
 
-noattr        don't include any signed attributes
 
-binary        don't translate message to text
 
-certfile file other certificates file
 
-signer file  signer certificate file
 
-recip  file  recipient certificate file for decryption
 
-in file      input file
 
-inform arg    input format SMIME (default), PEM or DER
 
-inkey file    input private key (if not signer or recipient)
 
-keyform arg  input private key format (PEM or ENGINE)
 
-out file      output file
 
-outform arg  output format SMIME (default), PEM or DER
 
-content file  supply or override content for detached signature
 
-to addr      to address
 
-from ad      from address
 
-subject s    subject
 
-text          include or delete text MIME headers
 
-CApath dir    trusted certificates directory
 
-CAfile file  trusted certificates file
 
-crl_check    check revocation status of signer's certificate using CRLs
 
-crl_check_all check revocation status of signer's certificate chain using CRLs
 
-engine e      use engine e, possibly a hardware device.
 
-passin arg    input file pass phrase source
 
-rand file:file:...
 
              load the file (or the files in the directory) into
 
              the random number generator
 
cert.pem      recipient certificate(s) for encryption
 
</pre>
 
  
===== smime v3 cms =====
+
There are three different kinds of commands. These are standard commands, cipher commands, and message digest commands. Calling the OpenSSL top-level <tt>help</tt> command with no arguments will result in openssl printing all available commands by group, sorted alphabetically.
  
<pre>
+
==Standard Commands==
openssl cms --help
 
Usage cms [options] cert.pem ...
 
where options are
 
-encrypt      encrypt message
 
-decrypt      decrypt encrypted message
 
-sign          sign message
 
-verify        verify signed message
 
-cmsout        output CMS structure
 
-des3          encrypt with triple DES
 
-des          encrypt with DES
 
-seed          encrypt with SEED
 
-rc2-40        encrypt with RC2-40 (default)
 
-rc2-64        encrypt with RC2-64
 
-rc2-128      encrypt with RC2-128
 
-aes128, -aes192, -aes256
 
              encrypt PEM output with cbc aes
 
-camellia128, -camellia192, -camellia256
 
              encrypt PEM output with cbc camellia
 
-nointern      don't search certificates in message for signer
 
-nosigs        don't verify message signature
 
-noverify      don't verify signers certificate
 
-nocerts      don't include signers certificate when signing
 
-nodetach      use opaque signing
 
-noattr        don't include any signed attributes
 
-binary        don't translate message to text
 
-certfile file other certificates file
 
-certsout file certificate output file
 
-signer file  signer certificate file
 
-recip  file  recipient certificate file for decryption
 
-keyid        use subject key identifier
 
-in file      input file
 
-inform arg    input format SMIME (default), PEM or DER
 
-inkey file    input private key (if not signer or recipient)
 
-keyform arg  input private key format (PEM or ENGINE)
 
-out file      output file
 
-outform arg  output format SMIME (default), PEM or DER
 
-content file  supply or override content for detached signature
 
-to addr      to address
 
-from ad      from address
 
-subject s    subject
 
-text          include or delete text MIME headers
 
-CApath dir    trusted certificates directory
 
-CAfile file  trusted certificates file
 
-crl_check    check revocation status of signer's certificate using CRLs
 
-crl_check_all check revocation status of signer's certificate chain using CRLs
 
-engine e      use engine e, possibly a hardware device.
 
-passin arg    input file pass phrase source
 
-rand file:file:...
 
              load the file (or the files in the directory) into
 
              the random number generator
 
cert.pem      recipient certificate(s) for encryption
 
</pre>
 
  
==== Public Key Cryptographic Operations ====
+
{| class="wikitable" style="margin:auto; text-align: center; width: 65%;"
 +
|+ Overview of OpenSSL's command line utilities
 +
! style="width: 25%; padding: 4px;" | Command
 +
! style="width: 75%; padding: 4px;" | Description
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-asn1parse.html asn1parse]
 +
|style="padding: 4px;" |  Parse an ASN.1 sequence.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-ca.html ca]
 +
|style="padding: 4px;" |  Certificate Authority (CA) Management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-ciphers.html ciphers]
 +
|style="padding: 4px;" |  Cipher Suite Description Determination.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-cmp.html cmp]
 +
|style="padding: 4px;" |  Certificate Management Protocol (CMP, RFC 4210) application.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-cms.html cms]
 +
|style="padding: 4px;" |  CMS (Cryptographic Message Syntax) utility.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-crl.html crl]
 +
|style="padding: 4px;" |  Certificate Revocation List (CRL) Management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-crl2pkcs7.html crl2pkcs7]
 +
|style="padding: 4px;" |  CRL to PKCS#7 Conversion.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-dgst.html dgst]
 +
|style="padding: 4px;" |  Message Digest calculation. MAC calculations are superseded by mac(1).
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-dhparam.html dhparam]
 +
|style="padding: 4px;" |  Generation and Management of Diffie-Hellman Parameters. Superseded by genpkey(1) and pkeyparam(1).
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-dsa.html dsa]
 +
|style="padding: 4px;" |  DSA Data Management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-dsaparam.html dsaparam]
 +
|style="padding: 4px;" |  DSA Parameter Generation and Management. Superseded by genpkey(1) and pkeyparam(1).
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-ec.html ec]
 +
|style="padding: 4px;" |  EC (Elliptic curve) key processing.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-ecparam.html ecparam]
 +
|style="padding: 4px;" |  EC parameter manipulation and generation.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-enc.html enc]
 +
|style="padding: 4px;" |  Symmetric cipher routines.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-engine.html engine]
 +
|style="padding: 4px;" |  Engine (loadable module) information and manipulation.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-errstr.html errstr]
 +
|style="padding: 4px;" |  Error Number to Error String Conversion.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-fipsinstall.html fipsinstall]
 +
|style="padding: 4px;" |  IPS configuration installation.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-gendsa.html gendsa]
 +
|style="padding: 4px;" |  Generation of DSA Private Key from Parameters. Superseded by genpkey(1) and pkey(1).
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-genpkey.html genpkey]
 +
|style="padding: 4px;" |  Generation of Private Key or Parameters.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-genrsa.html genrsa]
 +
|style="padding: 4px;" |  Generation of RSA Private Key. Superseded by genpkey(1).
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-help.html help]
 +
|style="padding: 4px;" |  Display information about a command's options.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-info.html info]
 +
|style="padding: 4px;" |  Display diverse information built into the OpenSSL libraries.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-kdf.html kdf]
 +
|style="padding: 4px;" |  Key Derivation Functions.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-list.html list]
 +
|style="padding: 4px;" |  List algorithms and features.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-mac.html mac]
 +
|style="padding: 4px;" |  Message Authentication Code Calculation.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-nseq.html nseq]
 +
|style="padding: 4px;" |  Create or examine a Netscape certificate sequence.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-ocsp.html ocsp]
 +
|style="padding: 4px;" |  Online Certificate Status Protocol utility.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-passwd.html passwd]
 +
|style="padding: 4px;" |  Generation of hashed passwords.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-pkcs12.html pkcs12]
 +
|style="padding: 4px;" |  PKCS#12 Data Management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-pkcs7.html pkcs7]
 +
|style="padding: 4px;" |  PKCS#7 Data Management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-pkcs8.html pkcs8]
 +
|style="padding: 4px;" |  PKCS#8 format private key conversion tool.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-pkey.html pkey]
 +
|style="padding: 4px;" |  Public and private key management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-pkeyparam.html pkeyparam]
 +
|style="padding: 4px;" |  Public key algorithm parameter management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-pkeyutl.html pkeyutl]
 +
|style="padding: 4px;" |  Public key algorithm cryptographic operation utility.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-prime.html prime]
 +
|style="padding: 4px;" |  Compute prime numbers.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-rand.html rand]
 +
|style="padding: 4px;" |  Generate pseudo-random bytes -- see [[Random Numbers]]
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-rehash.html rehash]
 +
|style="padding: 4px;" |  Create symbolic links to certificate and CRL files named by the hash values.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-req.html req]
 +
|style="padding: 4px;" |  PKCS#10 X.509 Certificate Signing Request (CSR) Management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-rsa.html rsa]
 +
|style="padding: 4px;" |  RSA key management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-rsautl.html rsautl]
 +
|style="padding: 4px;" |  RSA utility for signing, verification, encryption, and decryption. Superseded by pkeyutl(1).
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-s_client.html s_client]
 +
|style="padding: 4px;" |  This implements a generic SSL/TLS client which can establish a transparent connection to a remote server speaking SSL/TLS.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-s_server.html s_server]
 +
|style="padding: 4px;" |  This implements a generic SSL/TLS server which accepts connections from remote clients speaking SSL/TLS.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-s_time.html s_time]
 +
|style="padding: 4px;" |  SSL Connection Timer.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-sess_id.html sess_id]
 +
|style="padding: 4px;" |  SSL Session Data Management.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-smime.html smime]
 +
|style="padding: 4px;" |  S/MIME mail processing.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-openssl-speed.html speed]
 +
|style="padding: 4px;" |  Algorithm Speed Measurement.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-spkac.html spkac]
 +
|style="padding: 4px;" |  SPKAC printing and generating utility.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-srp.html srp]
 +
|style="padding: 4px;" |  Maintain SRP password file.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-storeutl.html storeutl]
 +
|style="padding: 4px;" |  Utility to list and display certificates, keys, CRLs, etc.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-ts.html ts]
 +
|style="padding: 4px;" |  Time Stamping Authority tool (client/server).
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-verify.html verify]
 +
|style="padding: 4px;" |  X.509 Certificate Verification.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-version.html version]
 +
|style="padding: 4px;" |  OpenSSL Version Information.
 +
|-
 +
|style="padding: 4px;" | [https://www.openssl.org/docs/manmaster/man1/openssl-x509.html x509]
 +
|style="padding: 4px;" |  X.509 Certificate Data Management.
 +
|}
  
<pre>
+
==Generating an RSA Private Key==
openssl pkeyutl --help
 
Usage: pkeyutl [options]
 
-in file        input file
 
-out file      output file
 
-sigfile file signature file (verify operation only)
 
-inkey file    input key
 
-keyform arg    private key format - default PEM
 
-pubin          input is a public key
 
-certin        input is a certificate carrying a public key
 
-pkeyopt X:Y    public key options
 
-sign          sign with private key
 
-verify        verify with public key
 
-verifyrecover  verify with public key, recover original data
 
-encrypt        encrypt with public key
 
-decrypt        decrypt with private key
 
-derive        derive shared secret
 
-hexdump        hex dump output
 
-engine e      use engine e, possibly a hardware device.
 
-passin arg    pass phrase source
 
</pre>
 
  
Legacy RSA command:
+
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.
<pre>
 
openssl rsautl --help
 
Usage: rsautl [options]
 
-in file        input file
 
-out file      output file
 
-inkey file    input key
 
-keyform arg    private key format - default PEM
 
-pubin          input is an RSA public
 
-certin        input is a certificate carrying an RSA public key
 
-ssl            use SSL v2 padding
 
-raw            use no padding
 
-pkcs          use PKCS#1 v1.5 padding (default)
 
-oaep          use PKCS#1 OAEP
 
-sign          sign with private key
 
-verify        verify with public key
 
-encrypt        encrypt with public key
 
-decrypt        decrypt with private key
 
-hexdump        hex dump output
 
-engine e      use engine e, possibly a hardware device.
 
-passin arg    pass phrase source
 
</pre>
 
  
=== Create / Handle Public Key Certificates ===
+
$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem
  
This requires you have a knowledge of what PKI is ( Certificate Authorities, Certificate Request, Certificate, Public Key, Private Key )
+
To generate a password protected private key, the previous command may be slightly amended as follows:
  
Classical use case is to obtain a valid Certificate for a Secured Web site ( https protocol ).
+
$ openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem
First you create a Private Key ( will be created together with Public key ).
 
Then create a Certificate Request for that private key with some informations for purpose of future Certificate.
 
Then send that Certificate Request to a Certificate Authority ( CA ) that will issue a Certificate that CA signed. For well known CA you need to pay.
 
Up to you to install your Private key together with the received Certificate on your system.  
 
  
It exists graphical front-end to operate openssl wihtin a GUI : [http://xca.sourceforge.net/ XCA]
+
The addition of the <tt>-aes256</tt> 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:
  
==== Key Generation ====
+
$ openssl list -cipher-algorithms
  
===== rsa / genrsa =====
+
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.
  
RSA is the most common type of Public/Private Key.
+
$ openssl pkey -in private-key.pem -text
Private Key part should never be disclosed while public key part is ... public.
 
  
<pre>
+
The above command yields the following output in my specific case. Your output will differ but should be structurally similar.
openssl genrsa --help
 
usage: genrsa [args] [numbits]
 
-des            encrypt the generated key with DES in cbc mode
 
-des3          encrypt the generated key with DES in ede cbc mode (168 bit key)
 
-seed
 
                encrypt PEM output with cbc seed
 
-aes128, -aes192, -aes256
 
                encrypt PEM output with cbc aes
 
-camellia128, -camellia192, -camellia256
 
                encrypt PEM output with cbc camellia
 
-out file      output the key to 'file
 
-passout arg    output file pass phrase source
 
-f4            use F4 (0x10001) for the E value
 
-3              use 3 for the E value
 
-engine e      use engine e, possibly a hardware device.
 
-rand file:file:...
 
                load the file (or the files in the directory) into
 
                the random number generator
 
</pre>
 
  
<pre>
+
-----BEGIN PRIVATE KEY-----
rsa help
+
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDZD6IMLRFk4CaF
unknown option help
+
w0rhRienwuE5EZ6xFE8e3C5TVi1+d9Enhi38RgkwD7UlWxPE6AWhp5T3kfrFWdak
rsa [options] <infile >outfile
+
1lZFVPp7/btOKLjKUru15nLoA4AKYtz9W9PhsM0dyzLc6FQ6K4ReQam5pHCqI2zF
where options are
+
82MwE+eIAduvuqyoQLKiI608EArWZqDtMUpBJzv0UVEYvRdnMWpCwfzpI+hPJywV
  -inform arg    input format - one of DER NET PEM
+
CcTlNCT/ctGgBKyIx+dDuZ7bR9MNmSW7GreJEbTH+R13xT3dd/JCka1+LYCl4h0q
  -outform arg    output format - one of DER NET PEM
+
oWhFPhOkvQzmmSzUmZlAlTDQLv2eAdJIrQcsnKZ3SsIOCC/3IpqwSzpid38Ill4O
  -in arg        input file
+
xH6XIrVFAgMBAAECggEBAJ2MC0JrM8TULSHJrf/0u7O4b2DMuTIuW386sSUr17mD
  -sgckey        Use IIS SGC key format
+
nfviGF6TNvf7bq++e4rgHbZHvIg1HJ9Bpdne+J86HtUARYNlazru8fAFZEGiyLzB
  -passin arg    input file pass phrase source
+
JUV/8TpO6ZJGepR8zSWrkFgZsOddw6i6LalADy5GRDcjoiDajZdR3lZxLrv5qOQU
  -out arg        output file
+
I1vKTf4Zs2Tl3gnaJ/Il1gBHIQ9W9xUH8jPBIwj51iXwCh8H0BiDPvFkU7cHIFCP
  -passout arg    output file pass phrase source
+
sJhGsGp6OS3uSwwQuSE+NqbuPfVilysCcwgZduknyio0QO1YfMBL6+XoKE/bFHsn
  -des            encrypt PEM output with cbc des
+
N+FzzczQg9sWyiwVR+3EeI9kp4JSElNh2nqG96i4QAECgYEA76OLUGrShHb4saoP
  -des3          encrypt PEM output with ede cbc des using 168 bit key
+
aYnBAKLEdWj5K483JdY6BSbdd5RkDbJG8ExmcbfTas/BGdKc4iVCkxV3ysxKnX18
  -seed          encrypt PEM output with cbc seed
+
PfxATHDLL8NMa+gGgZY5oTKUsrXEpS132HhCJ9T9LoesQjRb4kOZH8POVqm6O4Xf
  -aes128, -aes192, -aes256
+
lCt0y1+M1eQHI1NPO9CmPBgouEUCgYEA5+F4SS8RMyYRkU/kx195fwh0hhaOElzr
                encrypt PEM output with cbc aes
+
E8mZou3NFL/XT6/9t+2+7sMTuiQCP9zIa6s+/rrXdjWtrTcDp4WlDITas0UUgZhv
  -camellia128, -camellia192, -camellia256
+
  YVBQBF4vhHxIVwJxnT9Gwi4XM1JlFmVHofWD71P6DRe7jSWRS3CujP3AE9vmpWMx
                encrypt PEM output with cbc camellia
+
  tE1D9qLiWQECgYB445LzFYBvrKjWz4iI4CJKFNJwvGz+iXfzkXehg7KzkVtMAYSB
  -text          print the key in text
+
  0rjXYzm3J2ktgq778nn8Qxc0agy2GEil6GvzY+9MgAQ8Z0do9gTKif6zjLjP7vkH
  -noout          don't print key out
+
  bdtJxsuWPoEqwMkdgqZrfNbJp0O4pVddovJ/agtdF3R2YJ+W+DH0HOfl1QKBgFnM
  -modulus        print the RSA key modulus
+
  c2zEEYEhaQRBUHP1gXO0rouPCI4L9e2/0QPL2/QBJzzxBuzH4X1NhsI7V7OrqOIp
  -check          verify key consistency
+
  e0fiy7Y3q369I2ko1HY4rQln4z0c72VcWOCYKQbBqrInfCBNdPWWK93wNr2pk0gh
  -pubin          expect a public key in input file
+
  cGqqtteDLVrIBbCVfsOTMWN/cZ7y/zi4A23sPoQBAoGAEPzcIjOyoB97Pzd7iNim
  -pubout        output a public key
+
  Gin8RkwXIiFGSHo8vAh74CKBNokThM50OUNm5T2eJ4huzPpowQ+ID1mB5EjEai9n
  -engine e      use engine e, possibly a hardware device.
+
  JY9ll3cUpawiIIW/6uGTHyXfvZWNtqEYXrVJ6fcDaKcW4y3cplNj/SJaBW8HXsW7
error in rsa
+
  YGHW3zHsgy7EOAOzPwlm9oE=
</pre>
+
  -----END PRIVATE KEY-----
 +
  RSA Private-Key: (2048 bit, 2 primes)
 +
modulus:
 +
    00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46:
 +
    27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56:
 +
    2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b:
 +
    13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6:
 +
    56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6:
 +
    72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb:
 +
    32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23:
 +
    6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40:
 +
    b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27:
 +
    3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23:
 +
    e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04:
 +
    ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a:
 +
    b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91:
 +
    ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd:
 +
    0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01:
 +
    d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22:
 +
    9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22:
 +
    b5:45
 +
  publicExponent: 65537 (0x10001)
 +
  privateExponent:
 +
    00:9d:8c:0b:42:6b:33:c4:d4:2d:21:c9:ad:ff:f4:
 +
    bb:b3:b8:6f:60:cc:b9:32:2e:5b:7f:3a:b1:25:2b:
 +
    d7:b9:83:9d:fb:e2:18:5e:93:36:f7:fb:6e:af:be:
 +
    7b:8a:e0:1d:b6:47:bc:88:35:1c:9f:41:a5:d9:de:
 +
    f8:9f:3a:1e:d5:00:45:83:65:6b:3a:ee:f1:f0:05:
 +
    64:41:a2:c8:bc:c1:25:45:7f:f1:3a:4e:e9:92:46:
 +
    7a:94:7c:cd:25:ab:90:58:19:b0:e7:5d:c3:a8:ba:
 +
    2d:a9:40:0f:2e:46:44:37:23:a2:20:da:8d:97:51:
 +
    de:56:71:2e:bb:f9:a8:e4:14:23:5b:ca:4d:fe:19:
 +
    b3:64:e5:de:09:da:27:f2:25:d6:00:47:21:0f:56:
 +
    f7:15:07:f2:33:c1:23:08:f9:d6:25:f0:0a:1f:07:
 +
    d0:18:83:3e:f1:64:53:b7:07:20:50:8f:b0:98:46:
 +
    b0:6a:7a:39:2d:ee:4b:0c:10:b9:21:3e:36:a6:ee:
 +
    3d:f5:62:97:2b:02:73:08:19:76:e9:27:ca:2a:34:
 +
    40:ed:58:7c:c0:4b:eb:e5:e8:28:4f:db:14:7b:27:
 +
    37:e1:73:cd:cc:d0:83:db:16:ca:2c:15:47:ed:c4:
 +
    78:8f:64:a7:82:52:12:53:61:da:7a:86:f7:a8:b8:
 +
    40:01
 +
  prime1:
 +
    00:ef:a3:8b:50:6a:d2:84:76:f8:b1:aa:0f:69:89:
 +
    c1:00:a2:c4:75:68:f9:2b:8f:37:25:d6:3a:05:26:
 +
    dd:77:94:64:0d:b2:46:f0:4c:66:71:b7:d3:6a:cf:
 +
    c1:19:d2:9c:e2:25:42:93:15:77:ca:cc:4a:9d:7d:
 +
    7c:3d:fc:40:4c:70:cb:2f:c3:4c:6b:e8:06:81:96:
 +
    39:a1:32:94:b2:b5:c4:a5:2d:77:d8:78:42:27:d4:
 +
    fd:2e:87:ac:42:34:5b:e2:43:99:1f:c3:ce:56:a9:
 +
    ba:3b:85:df:94:2b:74:cb:5f:8c:d5:e4:07:23:53:
 +
    4f:3b:d0:a6:3c:18:28:b8:45
 +
  prime2:
 +
    00:e7:e1:78:49:2f:11:33:26:11:91:4f:e4:c7:5f:
 +
    79:7f:08:74:86:16:8e:12:5c:eb:13:c9:99:a2:ed:
 +
    cd:14:bf:d7:4f:af:fd:b7:ed:be:ee:c3:13:ba:24:
 +
    02:3f:dc:c8:6b:ab:3e:fe:ba:d7:76:35:ad:ad:37:
 +
    03:a7:85:a5:0c:84:da:b3:45:14:81:98:6f:61:50:
 +
    50:04:5e:2f:84:7c:48:57:02:71:9d:3f:46:c2:2e:
 +
    17:33:52:65:16:65:47:a1:f5:83:ef:53:fa:0d:17:
 +
    bb:8d:25:91:4b:70:ae:8c:fd:c0:13:db:e6:a5:63:
 +
    31:b4:4d:43:f6:a2:e2:59:01
 +
  exponent1:
 +
    78:e3:92:f3:15:80:6f:ac:a8:d6:cf:88:88:e0:22:
 +
    4a:14:d2:70:bc:6c:fe:89:77:f3:91:77:a1:83:b2:
 +
    b3:91:5b:4c:01:84:81:d2:b8:d7:63:39:b7:27:69:
 +
    2d:82:ae:fb:f2:79:fc:43:17:34:6a:0c:b6:18:48:
 +
    a5:e8:6b:f3:63:ef:4c:80:04:3c:67:47:68:f6:04:
 +
    ca:89:fe:b3:8c:b8:cf:ee:f9:07:6d:db:49:c6:cb:
 +
    96:3e:81:2a:c0:c9:1d:82:a6:6b:7c:d6:c9:a7:43:
 +
    b8:a5:57:5d:a2:f2:7f:6a:0b:5d:17:74:76:60:9f:
 +
    96:f8:31:f4:1c:e7:e5:d5
 +
  exponent2:
 +
    59:cc:73:6c:c4:11:81:21:69:04:41:50:73:f5:81:
 +
    73:b4:ae:8b:8f:08:8e:0b:f5:ed:bf:d1:03:cb:db:
 +
    f4:01:27:3c:f1:06:ec:c7:e1:7d:4d:86:c2:3b:57:
 +
    b3:ab:a8:e2:29:7b:47:e2:cb:b6:37:ab:7e:bd:23:
 +
    69:28:d4:76:38:ad:09:67:e3:3d:1c:ef:65:5c:58:
 +
    e0:98:29:06:c1:aa:b2:27:7c:20:4d:74:f5:96:2b:
 +
    dd:f0:36:bd:a9:93:48:21:70:6a:aa:b6:d7:83:2d:
 +
    5a:c8:05:b0:95:7e:c3:93:31:63:7f:71:9e:f2:ff:
 +
    38:b8:03:6d:ec:3e:84:01
 +
  coefficient:
 +
    10:fc:dc:22:33:b2:a0:1f:7b:3f:37:7b:88:d8:a6:
 +
    1a:29:fc:46:4c:17:22:21:46:48:7a:3c:bc:08:7b:
 +
    e0:22:81:36:89:13:84:ce:74:39:43:66:e5:3d:9e:
 +
    27:88:6e:cc:fa:68:c1:0f:88:0f:59:81:e4:48:c4:
 +
    6a:2f:67:25:8f:65:97:77:14:a5:ac:22:20:85:bf:
 +
    ea:e1:93:1f:25:df:bd:95:8d:b6:a1:18:5e:b5:49:
 +
    e9:f7:03:68:a7:16:e3:2d:dc:a6:53:63:fd:22:5a:
 +
    05:6f:07:5e:c5:bb:60:61:d6:df:31:ec:83:2e:c4:
 +
    38:03:b3:3f:09:66:f6:81
  
===== dsa / gendsa=====
+
Keep in mind the above key was generated solely for pedagogical purposes; never give anyone access to your private keys.
  
dsa is a less common Public/Private key scheme, but can be seen anyway, so ...
+
==Generating a Public Key==
  
<pre>
+
Having previously generated your private key, you may generate the corresponding public key using the following command.
openssl gendsa
 
usage: gendsa [args] dsaparam-file
 
-out file - output the key to 'file'
 
-des      - encrypt the generated key with DES in cbc mode
 
-des3    - encrypt the generated key with DES in ede cbc mode (168 bit key)
 
-seed
 
                encrypt PEM output with cbc seed
 
-aes128, -aes192, -aes256
 
                encrypt PEM output with cbc aes
 
-camellia128, -camellia192, -camellia256
 
                encrypt PEM output with cbc camellia
 
-engine e - use engine e, possibly a hardware device.
 
-rand file:file:...
 
          - load the file (or the files in the directory) into
 
            the random number generator
 
dsaparam-file
 
          - a DSA parameter file as generated by the dsaparam command
 
  
</pre>
+
$ openssl pkey -in private-key.pem -out public-key.pem -pubout
  
<pre>
+
You may once again view the key details, using a slightly different command this time.
OpenSSL> dsa help
 
unknown option help
 
dsa [options] <infile >outfile
 
where options are
 
-inform arg    input format - DER or PEM
 
-outform arg    output format - DER or PEM
 
-in arg        input file
 
-passin arg    input file pass phrase source
 
-out arg        output file
 
-passout arg    output file pass phrase source
 
-engine e      use engine e, possibly a hardware device.
 
-des            encrypt PEM output with cbc des
 
-des3          encrypt PEM output with ede cbc des using 168 bit key
 
-aes128, -aes192, -aes256
 
                encrypt PEM output with cbc aes
 
-camellia128, -camellia192, -camellia256
 
                encrypt PEM output with cbc camellia
 
-seed          encrypt PEM output with cbc seed
 
-text          print the key in text
 
-noout          don't print key out
 
-modulus        print the DSA public value
 
error in dsa
 
</pre>
 
  
===== Elliptic Curves / ec ecparam =====
+
$ openssl pkey -in public-key.pem -pubin -text
  
[[Elliptic_Curve_Cryptography]]
+
The output for the public key will be shorter, as it carries much less information, and it will look something like this.
  
See [[Command Line Elliptic Curve Operations]] for a description of these commands.
+
-----BEGIN PUBLIC KEY-----
 +
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2Q+iDC0RZOAmhcNK4UYn
 +
p8LhORGesRRPHtwuU1YtfnfRJ4Yt/EYJMA+1JVsTxOgFoaeU95H6xVnWpNZWRVT6
 +
e/27Tii4ylK7teZy6AOACmLc/VvT4bDNHcsy3OhUOiuEXkGpuaRwqiNsxfNjMBPn
 +
iAHbr7qsqECyoiOtPBAK1mag7TFKQSc79FFRGL0XZzFqQsH86SPoTycsFQnE5TQk
 +
/3LRoASsiMfnQ7me20fTDZkluxq3iRG0x/kdd8U93XfyQpGtfi2ApeIdKqFoRT4T
 +
pL0M5pks1JmZQJUw0C79ngHSSK0HLJymd0rCDggv9yKasEs6Ynd/CJZeDsR+lyK1
 +
RQIDAQAB
 +
-----END PUBLIC KEY-----
 +
RSA Public-Key: (2048 bit)
 +
Modulus:
 +
    00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46:
 +
    27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56:
 +
    2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b:
 +
    13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6:
 +
    56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6:
 +
    72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb:
 +
    32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23:
 +
    6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40:
 +
    b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27:
 +
    3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23:
 +
    e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04:
 +
    ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a:
 +
    b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91:
 +
    ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd:
 +
    0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01:
 +
    d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22:
 +
    9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22:
 +
    b5:45
 +
Exponent: 65537 (0x10001)
  
<pre>
+
For more information on generating keys, see the source code documentation, located in the <tt>doc/HOWTO/keys.txt</tt> file.
openssl ecparam --help
 
unknown option --help
 
ecparam [options] <infile >outfile
 
where options are
 
-inform arg      input format - default PEM (DER or PEM)
 
-outform arg      output format - default PEM
 
-in  arg          input file  - default stdin
 
-out arg          output file - default stdout
 
-noout            do not print the ec parameter
 
-text            print the ec parameters in text form
 
-check            validate the ec parameters
 
-C                print a 'C' function creating the parameters
 
-name arg        use the ec parameters with 'short name' name
 
-list_curves      prints a list of all currently available curve 'short names'
 
-conv_form arg    specifies the point conversion form
 
                  possible values: compressed
 
                                    uncompressed (default)
 
                                    hybrid
 
-param_enc arg    specifies the way the ec parameters are encoded
 
                  in the asn1 der encoding
 
                  possible values: named_curve (default)
 
                                    explicit
 
-no_seed          if 'explicit' parameters are chosen do not use the seed
 
-genkey          generate ec key
 
-rand file        files to use for random number input
 
-engine e        use engine e, possibly a hardware device
 
</pre>
 
  
<pre>
+
==Generating Keys Based on Elliptic Curves==
ec [options] <infile >outfile
 
where options are
 
-inform arg    input format - DER or PEM
 
-outform arg    output format - DER or PEM
 
-in arg        input file
 
-passin arg    input file pass phrase source
 
-out arg        output file
 
-passout arg    output file pass phrase source
 
-engine e      use engine e, possibly a hardware device.
 
-des            encrypt PEM output, instead of 'des' every other
 
                cipher supported by OpenSSL can be used
 
-text          print the key
 
-noout          don't print key out
 
-param_out      print the elliptic curve parameters
 
-conv_form arg  specifies the point conversion form
 
                possible values: compressed
 
                                  uncompressed (default)
 
                                  hybrid
 
-param_enc arg  specifies the way the ec parameters are encoded
 
                in the asn1 der encoding
 
                possible values: named_curve (default)
 
                                  explicit
 
</pre>
 
  
==== Certificate Authority / ca ====
+
There are essentially two steps to generating a key:
  
When you want to act as a Certificate Authority.
+
# Generate the parameters for the specific curve you are using
 +
# Use those parameters to generate the key
  
OpenSSL> ca
+
To see the list of curves instrinsically supported by openssl, you can use the <tt>-list_curves</t> option when calling the <tt>ecparam</tt> command.
Using configuration from /usr/lib/ssl/openssl.cnf
 
Error opening CA private key ./demoCA/private/cakey.pem
 
140492277311144:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('./demoCA/private/cakey.pem','r')
 
140492277311144:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
 
unable to load CA private key
 
error in ca
 
  
By default you don't have ca created...
+
$ openssl ecparam -list_curves
 +
  secp112r1 : SECG/WTLS curve over a 112 bit prime field
 +
  secp112r2 : SECG curve over a 112 bit prime field
 +
  secp128r1 : SECG curve over a 128 bit prime field
 +
  secp128r2 : SECG curve over a 128 bit prime field
 +
  secp160k1 : SECG curve over a 160 bit prime field
 +
  ...
  
==== Certificate Request / pkcs10 / req ====
+
For this example I will use the <tt>prime256v1</tt> curve, which is an <tt>X9.62/SECG</tt> curve over a 256 bit prime field.
  
OpenSSL> req ?
+
===Generating the Curve Parameters===
unknown option ?
 
req [options] <infile >outfile
 
where options  are
 
-inform arg    input format - DER or PEM
 
-outform arg  output format - DER or PEM
 
-in arg        input file
 
-out arg      output file
 
-text          text form of request
 
-pubkey        output public key
 
-noout        do not output REQ
 
-verify        verify signature on REQ
 
-modulus      RSA modulus
 
-nodes        don't encrypt the output key
 
-engine e      use engine e, possibly a hardware device
 
-subject      output the request's subject
 
-passin        private key password source
 
-key file      use the private key contained in file
 
-keyform arg  key file format
 
-keyout arg    file to send the key to
 
-rand file:file:...
 
                load the file (or the files in the directory) into
 
                the random number generator
 
-newkey rsa:bits generate a new RSA key of 'bits' in size
 
-newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'
 
-newkey ec:file generate a new EC key, parameters taken from CA in 'file'
 
-[digest]      Digest to sign with (md5, sha1, md2, mdc2, md4)
 
-config file  request template file.
 
-subj arg      set or modify request subject
 
-multivalue-rdn enable support for multivalued RDNs
 
-new          new request.
 
-batch        do not ask anything during request generation
 
-x509          output a x509 structure instead of a cert. req.
 
-days          number of days a certificate generated by -x509 is valid for.
 
-set_serial    serial number to use for a certificate generated by -x509.
 
-newhdr        output "NEW" in the header lines
 
-asn1-kludge  Output the 'request' in a format that is wrong but some CA's
 
                have been reported as requiring
 
-extensions .. specify certificate extension section (override value in config file)
 
-reqexts ..    specify request extension section (override value in config file)
 
-utf8          input characters are UTF8 (default ASCII)
 
-nameopt arg    - various certificate name options
 
-reqopt arg    - various request text options
 
  
error in req
+
Having selected our curve, we now call <tt>ecparam</tt> to generate our parameters file.
  
==== Certificates AKA x509 ====
+
  $ openssl ecparam -name prime256v1 -out prime256v1.pem
  
x509 command allows you to display content of a x509 certificate and to convert it from/to [[PEM]], [[NET]] or [[DER]] formats.
+
====Printing Parameters to Standard Out====
  
OpenSSL> x509 help
+
You can print the generated curve parameters to the terminal output with the following command:
unknown option help
 
usage: x509 args
 
-inform arg    - input format - default PEM (one of DER, NET or PEM)
 
-outform arg    - output format - default PEM (one of DER, NET or PEM)
 
-keyform arg    - private key format - default PEM
 
-CAform arg    - CA format - default PEM
 
-CAkeyform arg  - CA key format - default PEM
 
-in arg        - input file - default stdin
 
-out arg        - output file - default stdout
 
-passin arg    - private key password source
 
-serial        - print serial number value
 
-subject_hash  - print subject hash value
 
-subject_hash_old  - print old-style (MD5) subject hash value
 
-issuer_hash    - print issuer hash value
 
-issuer_hash_old    - print old-style (MD5) issuer hash value
 
-hash          - synonym for -subject_hash
 
-subject        - print subject DN
 
-issuer        - print issuer DN
 
-email          - print email address(es)
 
-startdate      - notBefore field
 
-enddate        - notAfter field
 
-purpose        - print out certificate purposes
 
-dates          - both Before and After dates
 
-modulus        - print the RSA key modulus
 
-pubkey        - output the public key
 
-fingerprint    - print the certificate fingerprint
 
-alias          - output certificate alias
 
-noout          - no certificate output
 
-ocspid        - print OCSP hash values for the subject name and public key
 
-ocsp_uri      - print OCSP Responder URL(s)
 
-trustout      - output a "trusted" certificate
 
-clrtrust      - clear all trusted purposes
 
-clrreject      - clear all rejected purposes
 
-addtrust arg  - trust certificate for a given purpose
 
-addreject arg  - reject certificate for a given purpose
 
-setalias arg  - set certificate alias
 
-days arg      - How long till expiry of a signed certificate - def 30 days
 
-checkend arg  - check whether the cert expires in the next arg seconds
 
                  exit 1 if so, 0 if not
 
-signkey arg    - self sign cert with arg
 
-x509toreq      - output a certification request object
 
-req            - input is a certificate request, sign and output.
 
-CA arg        - set the CA certificate, must be PEM format.
 
-CAkey arg      - set the CA key, must be PEM format
 
                  missing, it is assumed to be in the CA file.
 
-CAcreateserial - create serial number file if it does not exist
 
-CAserial arg  - serial file
 
-set_serial    - serial number to use
 
-text          - print the certificate in text form
 
-C              - print out C code forms
 
-md2/-md5/-sha1/-mdc2 - digest to use
 
-extfile        - configuration file with X509V3 extensions to add
 
-extensions    - section from config file with X509V3 extensions to add
 
-clrext        - delete extensions before signing and input certificate
 
-nameopt arg    - various certificate name options
 
-engine e      - use engine e, possibly a hardware device.
 
-certopt arg    - various certificate text options
 
  
==== Client Certificates AKA pkcs12 ====
+
$ openssl ecparam -in prime256v1.pem -noout -text
 +
ASN1 OID: prime256v1
 +
NIST CURVE: P-256
  
Client Certificate is a language abuse, but anyway it is kind of file you need to install on your system when SSL/TLS server require Client Authentication.
+
====Printing Parameters as C Code====
Those kind of certificates credentials are known with .'''pkcs12''' or .'''pfx''' file extension.
 
They contains a x509 Certificate and the public/private key of client. Those files are then very sensible to handle with same security as a private key.
 
  
<pre>
+
Analogously, you may also output the generated curve parameters as C code. The parameters can then be loaded by calling the <tt>get_ec_group_XXX()</tt> function. To print the C code to the current terminal's output, the following command may be used:
Usage: pkcs12 [options]
 
where options are
 
-export      output PKCS12 file
 
-chain        add certificate chain
 
-inkey file  private key if not infile
 
-certfile f  add all certs in f
 
-CApath arg  - PEM format directory of CA's
 
-CAfile arg  - PEM format file of CA's
 
-name "name"  use name as friendly name
 
-caname "nm"  use nm as CA friendly name (can be used more than once).
 
-in  infile  input filename
 
-out outfile  output filename
 
-noout        don't output anything, just verify.
 
-nomacver    don't verify MAC.
 
-nocerts      don't output certificates.
 
-clcerts      only output client certificates.
 
-cacerts      only output CA certificates.
 
-nokeys      don't output private keys.
 
-info        give info about PKCS#12 structure.
 
-des          encrypt private keys with DES
 
-des3        encrypt private keys with triple DES (default)
 
-seed        encrypt private keys with seed
 
-aes128, -aes192, -aes256
 
              encrypt PEM output with cbc aes
 
-camellia128, -camellia192, -camellia256
 
              encrypt PEM output with cbc camellia
 
-nodes        don't encrypt private keys
 
-noiter      don't use encryption iteration
 
-nomaciter    don't use MAC iteration
 
-maciter      use MAC iteration
 
-nomac        don't generate MAC
 
-twopass      separate MAC, encryption passwords
 
-descert      encrypt PKCS#12 certificates with triple DES (default RC2-40)
 
-certpbe alg  specify certificate PBE algorithm (default RC2-40)
 
-keypbe alg  specify private key PBE algorithm (default 3DES)
 
-macalg alg  digest algorithm used in MAC (default SHA1)
 
-keyex        set MS key exchange type
 
-keysig      set MS key signature type
 
-password p  set import/export password source
 
-passin p    input file pass phrase source
 
-passout p    output file pass phrase source
 
-engine e    use engine e, possibly a hardware device.
 
-rand file:file:...
 
              load the file (or the files in the directory) into
 
              the random number generator
 
-CSP name    Microsoft CSP name
 
-LMK          Add local machine keyset attribute to private key
 
</pre>
 
  
=== SSL/TLS and Certificates ONLINE services ===
+
$ openssl ecparam -in prime256v1.pem -noout -C
  
==== s_server ====
+
And here are the first few lines of the corresponding output:
  
This implements a generic SSL/TLS server.  
+
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,
 +
        ...
  
<pre>
+
===Generating the Key===
openssl s_server
 
Error opening server certificate private key file server.pem
 
139811478357672:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('server.pem','r')
 
139811478357672:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
 
unable to load server certificate private key file
 
</pre>
 
  
'''you need to provide certificate and private key to be able to run SSL/TLS server.'''
+
With the curve parameters in hand, we are now free to generate the key. Just as with the [#Generating an RSA Private Key|RSA] example above, we may optionally specify a cipher algorithm with which to encrypt the private key. The call to generate the key using the elliptic curve parameters generated in the example above looks like this:
  
<pre>
+
$ openssl genpkey -aes256 -paramfile prime256v1.pem -out private-key.pem
openssl s_server --help
+
Enter PEM pass phrase:
unknown option --help
+
Verifying - Enter PEM pass phrase:
usage: s_server [args ...]
 
  
-accept arg  - port to accept on (default is 4433)
+
===Putting it All Together===
-context arg  - set session ID context
 
-verify arg  - turn on peer certificate verification
 
-Verify arg  - turn on peer certificate verification, must have a cert.
 
-cert arg    - certificate file to use
 
                (default is server.pem)
 
-crl_check    - check the peer certificate has not been revoked by its CA.
 
                The CRL(s) are appended to the certificate file
 
-crl_check_all - check the peer certificate has not been revoked by its CA
 
                or any other CRL in the CA chain. CRL(s) are appened to the
 
                the certificate file.
 
-certform arg - certificate format (PEM or DER) PEM default
 
-key arg      - Private Key file to use, in cert file if
 
                not specified (default is server.pem)
 
-keyform arg  - key format (PEM, DER or ENGINE) PEM default
 
-pass arg    - private key file pass phrase source
 
-dcert arg    - second certificate file to use (usually for DSA)
 
-dcertform x  - second certificate format (PEM or DER) PEM default
 
-dkey arg    - second private key file to use (usually for DSA)
 
-dkeyform arg - second key format (PEM, DER or ENGINE) PEM default
 
-dpass arg    - second private key file pass phrase source
 
-dhparam arg  - DH parameter file to use, in cert file if not specified
 
                or a default set of parameters is used
 
-named_curve arg  - Elliptic curve name to use for ephemeral ECDH keys.
 
                Use "openssl ecparam -list_curves" for all names
 
                (default is nistp256).
 
-nbio        - Run with non-blocking IO
 
-nbio_test    - test with the non-blocking test bio
 
-crlf        - convert LF from terminal into CRLF
 
-debug        - Print more output
 
-msg          - Show protocol messages
 
-state        - Print the SSL states
 
-CApath arg  - PEM format directory of CA's
 
-CAfile arg  - PEM format file of CA's
 
-nocert      - Don't use any certificates (Anon-DH)
 
-cipher arg  - play with 'openssl ciphers' to see what goes here
 
-serverpref  - Use server's cipher preferences
 
-quiet        - No server output
 
-no_tmp_rsa  - Do not generate a tmp RSA key
 
-psk_hint arg - PSK identity hint to use
 
-psk arg      - PSK in hex (without 0x)
 
-srpvfile file      - The verifier file for SRP
 
-srpuserseed string - A seed string for a default user salt.
 
-ssl2        - Just talk SSLv2
 
-ssl3        - Just talk SSLv3
 
-tls1_2      - Just talk TLSv1.2
 
-tls1_1      - Just talk TLSv1.1
 
-tls1        - Just talk TLSv1
 
-dtls1        - Just talk DTLSv1
 
-timeout      - Enable timeouts
 
-mtu          - Set link layer MTU
 
-chain        - Read a certificate chain
 
-no_ssl2      - Just disable SSLv2
 
-no_ssl3      - Just disable SSLv3
 
-no_tls1      - Just disable TLSv1
 
-no_tls1_1    - Just disable TLSv1.1
 
-no_tls1_2    - Just disable TLSv1.2
 
-no_dhe      - Disable ephemeral DH
 
-no_ecdhe    - Disable ephemeral ECDH
 
-bugs        - Turn on SSL bug compatibility
 
-www          - Respond to a 'GET /' with a status page
 
-WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>
 
-HTTP        - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>
 
                with the assumption it contains a complete HTTP response.
 
-engine id    - Initialise and use the specified engine
 
-id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'
 
-rand file:file:...
 
-servername host - servername for HostName TLS extension
 
-servername_fatal - on mismatch send fatal alert (default warning alert)
 
-cert2 arg    - certificate file to use for servername
 
                (default is server2.pem)
 
-key2 arg    - Private Key file to use for servername, in cert file if
 
                not specified (default is server2.pem)
 
-tlsextdebug  - hex dump of all TLS extensions received
 
-no_ticket    - disable use of RFC4507bis session tickets
 
-legacy_renegotiation - enable use of legacy renegotiation (dangerous)
 
-nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)
 
-use_srtp profiles - Offer SRTP key management with a colon-separated profile list
 
-keymatexport label  - Export keying material using label
 
-keymatexportlen len  - Export len bytes of keying material (default 20)
 
</pre>
 
  
==== s_client ====
+
The process of generation a curve based on elliptic-curves can be streamlined by calling the <tt>genpkey</tt> command directly and specifying both the algorithm and the name of the curve to use for parameter generation. In it's simplest form, the command to generate a key based on the same curve as in the example above looks like this:
  
This implements a generic SSL/TLS client
+
$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256
  
<pre>
+
This command will result in the generated key being printed to the terminal's output.
unknown option --help
 
usage: s_client args
 
  
  -host host    - use -connect instead
+
  $ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256
-port port    - use -connect instead
+
   
-connect host:port - who to connect to (default is localhost:4433)
+
  -----BEGIN PRIVATE KEY-----
  -verify arg  - turn on peer certificate verification
+
  MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgqqYoJGowXJ5/GTkB
  -cert arg    - certificate file to use, PEM format assumed
+
  SRLnBMNWLoQ2RM/QxrY+bfDDGRahRANCAASPY4eTANkwIIAWhh32eoFl2YFLJSWy
-certform arg - certificate format (PEM or DER) PEM default
+
  bdITdZ82O5JDpDijmGmJ2hepe5afek9WVqxMPYjmbTwMPO3xMGbqUiJD
-key arg      - Private key file to use, in cert file if
+
  -----END PRIVATE KEY-----
                not specified but cert file is.
 
-keyform arg  - key format (PEM or DER) PEM default
 
-pass arg    - private key file pass phrase source
 
  -CApath arg  - PEM format directory of CA's
 
-CAfile arg  - PEM format file of CA's
 
-reconnect    - Drop and re-make the connection with the same Session-ID
 
-pause        - sleep(1) after each read(2) and write(2) system call
 
-showcerts    - show all certificates in the chain
 
-debug        - extra output
 
-msg          - Show protocol messages
 
-nbio_test    - more ssl protocol testing
 
-state        - print the 'ssl' states
 
-nbio        - Run with non-blocking IO
 
-crlf        - convert LF from terminal into CRLF
 
-quiet        - no s_client output
 
-ign_eof      - ignore input eof (default when -quiet)
 
-no_ign_eof  - don't ignore input eof
 
-psk_identity arg - PSK identity
 
-psk arg      - PSK in hex (without 0x)
 
-srpuser user    - SRP authentification for 'user'
 
-srppass arg      - password for 'user'
 
-srp_lateuser    - SRP username into second ClientHello message
 
-srp_moregroups  - Tolerate other than the known g N values.
 
-srp_strength int - minimal mength in bits for N (default 1024).
 
-ssl2        - just use SSLv2
 
-ssl3        - just use SSLv3
 
-tls1_2      - just use TLSv1.2
 
-tls1_1      - just use TLSv1.1
 
-tls1        - just use TLSv1
 
-dtls1        - just use DTLSv1
 
-mtu          - set the link layer MTU
 
  -no_tls1_2/-no_tls1_1/-no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol
 
-bugs        - Switch on all SSL implementation bug workarounds
 
-serverpref  - Use server's cipher preferences (only SSLv2)
 
-cipher      - preferred cipher to use, use the 'openssl ciphers'
 
                command to see what is available
 
-starttls prot - use the STARTTLS command before starting TLS
 
                for those protocols that support it, where
 
                'prot' defines which one to assume.  Currently,
 
                only "smtp", "pop3", "imap", "ftp" and "xmpp"
 
                are supported.
 
-engine id    - Initialise and use the specified engine
 
-rand file:file:...
 
-sess_out arg - file to write SSL session to
 
-sess_in arg  - file to read SSL session from
 
-servername host  - Set TLS extension servername in ClientHello
 
  -tlsextdebug      - hex dump of all TLS extensions received
 
  -status          - request certificate status from server
 
-no_ticket        - disable use of RFC4507bis session tickets
 
-nextprotoneg arg - enable NPN extension, considering named protocols supported (comma-separated list)
 
-legacy_renegotiation - enable use of legacy renegotiation (dangerous)
 
-use_srtp profiles - Offer SRTP key management with a colon-separated profile list
 
-keymatexport label  - Export keying material using label
 
-keymatexportlen len  - Export len bytes of keying material (default 20)
 
</pre>
 
  
==== ocsp ====
+
Remember that you can specify a cipher algorithm to encrypt the key with, which something you may or may not want to do, depending on your specific use case. Here is a slightly more complete example showing a key generated with a password and written to a specific output file.
  
 +
$ openssl genpkey -aes256 -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private-key.pem
 +
Enter PEM pass phrase:
 +
Verifying - Enter PEM pass phrase:
  
=== Signing / Digest and Timestamping ===
+
Just as with the previous example, you can use the <tt>pkey</tt> command to inspect your newly-generated key.
  
==== Signing / Digest ====
+
$ openssl pkey -in private-key.pem -text
 +
Enter pass phrase for private-key.pem:
 +
-----BEGIN PRIVATE KEY-----
 +
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEO7CxgTwi0hsjdbp
 +
sXWuU2x2flLthxqXabYDOqOZCvuhRANCAAQVTLkeCBJdvMnqwZKYJxrPvTTuanrD
 +
NkyAPQCARKsQ7bVrP6ky/5uAcAvjuZB0xKCcSp7roXLWRzD/y/ik8P5R
 +
-----END PRIVATE KEY-----
 +
Private-Key: (256 bit)
 +
priv:
 +
    10:ee:c2:c6:04:f0:8b:48:6c:8d:d6:e9:b1:75:ae:
 +
    53:6c:76:7e:52:ed:87:1a:97:69:b6:03:3a:a3:99:
 +
    0a:fb
 +
pub:
 +
    04:15:4c:b9:1e:08:12:5d:bc:c9:ea:c1:92:98:27:
 +
    1a:cf:bd:34:ee:6a:7a:c3:36:4c:80:3d:00:80:44:
 +
    ab:10:ed:b5:6b:3f:a9:32:ff:9b:80:70:0b:e3:b9:
 +
    90:74:c4:a0:9c:4a:9e:eb:a1:72:d6:47:30:ff:cb:
 +
    f8:a4:f0:fe:51
 +
ASN1 OID: prime256v1
 +
NIST CURVE: P-256
  
<pre>
+
For more details on elliptic curve cryptography or key generation, check out the [https://www.openssl.org/docs/manpages.html manpages].
openssl dgst --help
 
unknown option '--help'
 
options are
 
-c              to output the digest with separating colons
 
-r              to output the digest in coreutils format
 
-d              to output debug info
 
-hex            output as hex dump
 
-binary        output in binary form
 
-hmac arg      set the HMAC key to arg
 
-non-fips-allow allow use of non FIPS digest
 
-sign  file    sign digest using private key in file
 
-verify file    verify a signature using public key in file
 
-prverify file  verify a signature using private key in file
 
-keyform arg    key file format (PEM or ENGINE)
 
-out filename  output to filename rather than stdout
 
-signature file signature to verify
 
-sigopt nm:v    signature parameter
 
-hmac key      create hashed MAC with key
 
-mac algorithm  create MAC (not neccessarily HMAC)
 
-macopt nm:v    MAC algorithm parameters or key
 
-engine e      use engine e, possibly a hardware device.
 
-md4            to use the md4 message digest algorithm
 
-md5            to use the md5 message digest algorithm
 
-ripemd160      to use the ripemd160 message digest algorithm
 
-sha            to use the sha message digest algorithm
 
-sha1          to use the sha1 message digest algorithm
 
-sha224        to use the sha224 message digest algorithm
 
-sha256        to use the sha256 message digest algorithm
 
-sha384        to use the sha384 message digest algorithm
 
-sha512        to use the sha512 message digest algorithm
 
-whirlpool      to use the whirlpool message digest algorithm
 
</pre>
 
==== timestamping ====
 
  
openssl ts
+
==Base64 Encoding Strings==
  
<pre>
+
For simple string encoding, you can use "here string" syntax with the [[Base64 Encoding|base64]] command as below. Intuitively, the <tt>-e</tt> flag specifies the action to be encoding.
usage:
 
ts -query [-rand file:file:...] [-config configfile] [-data file_to_hash] [-digest digest_bytes][-md2|-md4|-md5|-sha|-sha1|-mdc2|-ripemd160] [-policy object_id] [-no_nonce] [-cert] [-in request.tsq] [-out request.tsq] [-text]
 
or
 
ts -reply [-config configfile] [-section tsa_section] [-queryfile request.tsq] [-passin password] [-signer tsa_cert.pem] [-inkey private_key.pem] [-chain certs_file.pem] [-policy object_id] [-in response.tsr] [-token_in] [-out response.tsr] [-token_out] [-text] [-engine id]
 
or
 
ts -verify [-data file_to_hash] [-digest digest_bytes] [-queryfile request.tsq] -in response.tsr [-token_in] -CApath ca_path -CAfile ca_file.pem -untrusted cert_file.pem
 
</pre>
 
  
=== Data handling ===
+
$ openssl base64 -e <<< 'Welcome to openssl wiki'
 +
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK
  
==== ASN.1 ====
+
Similarly, the base64 command's <tt>-d</tt> flag may be used to indicate decoding mode.
  
[[DER]] decoding
+
$ openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'
 +
Welcome to openssl wiki
  
<pre>
 
openssl asn1parse --help
 
unknown option --help
 
asn1parse [options] <infile
 
where options are
 
-inform arg  input format - one of DER PEM
 
-in arg      input file
 
-out arg      output file (output format is always DER
 
-noout arg    don't produce any output
 
-offset arg  offset into file
 
-length arg  length of section in file
 
-i            indent entries
 
-dump        dump unknown data in hex form
 
-dlimit arg  dump the first arg bytes of unknown data in hex form
 
-oid file    file of extra oid definitions
 
-strparse offset
 
              a series of these can be used to 'dig' into multiple
 
              ASN1 blob wrappings
 
-genstr str  string to generate ASN1 structure from
 
-genconf file file to generate ASN1 structure from
 
</pre>
 
  
==== Base64 ====
+
'''''Note:''''' base64 line length is limited to 76 characters by default in openssl (and generated with 64 characters per line).
  
base64 encoding / decoding
+
openssl base64 -e <<< 'Welcome to openssl wiki with a very long line that splits...'
 +
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRo
 +
YXQgc3BsaXRzLi4uCg==
 +
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
  
[[Base64]]
+
=> NOTHING!
  
===== a String =====
+
To be able to decode a base64 line without line feeds that exceeds the default 76 character length restriction use the <code>-A</code> option.
  
<pre>
+
openssl base64 -d -A <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
openssl base64 -e <<< 'Welcome to openssl wiki'
+
Welcome to openssl wiki with a very long line that splits...
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK
 
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'
 
Welcome to openssl wiki
 
</pre>
 
  
warning '''base64 line length is limited to 76 characters by default in openssl''' ( and generated with 64 characters / line ).
+
It is recommended to actually split base64 strings into multiple lines of 64 characters, however, since the <code>-A</code> option is buggy, particularly with its handling of long files.
  
<pre>
+
==Generating a File Hash==
openssl base64 -e <<< 'Welcome to openssl wiki with a very long line that splits...'
 
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRo
 
YXQgc3BsaXRzLi4uCg==
 
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
 
</pre>
 
=> NOTHING !
 
  
to be able to decode a base64 line without line feed that exceed 76 characters use -A option :
+
One of the most basic uses of the [[dgst]] command (short for digest) is viewing the hash of a given file. To do this, simply invoke the command with the specified digest algorithm to use. For this example, I will be hashing an arbitrary file on my system using the [[MD5]], [[SHA1]], and [[SHA384]] algorithms.
  
<pre>
+
$ openssl dgst -md5 primes.dat
openssl base64 -d -A <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
+
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664
Welcome to openssl wiki with a very long line that splits...
+
</pre>
+
$ openssl dgst -sha1 primes.dat
 +
SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32
 +
 +
$ openssl dgst -sha384 primes.dat
 +
SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271
  
This is anyway better to actualy split base64 result in 64 characters lines since -A option is BUGGY ( limit with long files ).
+
For a list of the available digest algorithms, you can use the following command.
  
==== DER <-> PEM conversion ====
+
$ openssl list -digest-algorithms
 +
RSA-MD4 => MD4
 +
RSA-MD5 => MD5
 +
RSA-MDC2 => MDC2
 +
RSA-RIPEMD160 => RIPEMD160
 +
RSA-SHA1 => SHA1
 +
RSA-SHA1-2 => RSA-SHA1
 +
...
  
on each command handling PEM and DER format an '''inform''' and '''outform''' options are provided to specify it.
+
You can also use a similar command to see the available [[Digest Commands|digest commands]]:
Then it is easy to read it in format and write it in another
 
  
==== pkcs8 / pkcs5 ====
+
$ openssl list -digest-commands
 +
blake2b512        blake2s256        md5              sha1             
 +
sha224            sha256            sha3-224          sha3-256         
 +
sha3-384          sha3-512          sha384            sha512           
 +
sha512-224        sha512-256        shake128          shake256         
 +
sm3 
  
pkcs8 is a format to store private keys.
+
Below are three sample invocations of the [[md5]], [[sha1]], and [[sha384]] digest commands using the same file as the [[dgst]] command invocation above.
pkcs8 uses various pkcs5 version as subformat.
 
  
<pre>
+
$ openssl md5 primes.dat
openssl pkcs8 --help
+
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664
Usage pkcs8 [options]
+
where options are
+
$ openssl sha1 primes.dat
-in file        input file
+
SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32
-inform X      input format (DER or PEM)
+
-passin arg    input file pass phrase source
+
$ openssl sha384 primes.dat
-outform X      output format (DER or PEM)
+
  SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271
-out file      output file
 
-passout arg    output file pass phrase source
 
-topk8          output PKCS8 file
 
-nooct          use (nonstandard) no octet format
 
-embed          use (nonstandard) embedded DSA parameters format
 
-nsdb          use (nonstandard) DSA Netscape DB format
 
-noiter        use 1 as iteration count
 
-nocrypt        use or expect unencrypted private key
 
-v2 alg        use PKCS#5 v2.0 and cipher "alg"
 
-v1 obj        use PKCS#5 v1.5 and cipher "alg"
 
  -engine e      use engine e, possibly a hardware device.
 
</pre>
 
  
=== Diagnostics ===
+
==File Encryption and Decryption==
  
==== SSL/TLS session information ====
+
The following example demonstrates a simple file encryption and decryption using the [[enc]] command. The first argument is the cipher algorithm to use for encrypting the file. For this example I carefully selected the [[AES-256]] algorithm in [[CBC Mode]] by looking up the available ciphers and picking out the first one I saw. To see the list of available ciphers, you can use the following command.
  
<pre>
+
$ openssl enc -ciphers
openssl sess_id --help
+
Supported ciphers:
unknown option --help
+
-aes-128-cbc              -aes-128-cfb              -aes-128-cfb1           
usage: sess_id args
+
-aes-128-cfb8              -aes-128-ctr              -aes-128-ecb             
 +
-aes-128-ofb              -aes-192-cbc              -aes-192-cfb             
 +
-aes-192-cfb1              -aes-192-cfb8              -aes-192-ctr
 +
...
  
-inform arg    - input format - default PEM (DER or PEM)
+
You can also use the following command:
-outform arg    - output format - default PEM
 
-in arg        - input file - default stdin
 
-out arg        - output file - default stdout
 
-text          - print ssl session id details
 
-cert          - output certificate
 
-noout          - no CRL output
 
-context arg    - set the session ID context
 
</pre>
 
  
== Further reading ==
+
$ openssl list -cipher-algorithms
 +
AES-128-CBC
 +
AES-128-CBC-HMAC-SHA1
 +
AES-128-CBC-HMAC-SHA256
 +
id-aes128-CCM
 +
AES-128-CFB
 +
AES-128-CFB1
 +
AES-128-CFB8
 +
AES-128-CTR
 +
...
  
* Paul Heinlein. [https://www.madboa.com/geek/openssl/ "OpenSSL Command-Line HOWTO"]. Has many quick cookbook-style recipes for doing common tasks using the "oppenssl" command-line application.
+
Having selected an encryption algorithm, you must then specify whether the action you are taking is either encryption or decryption via the <tt>-e</tt> or <tt>-d</tt> flags, respectively. The <tt>-iter</tt> 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 <tt>-pbkdf2</tt> flag. We then use the <tt>-salt</tt> 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:
 +
 +
The analogous decryption command is as follows:
 +
 +
$ openssl enc -aes-256-cbc -d -iter 1000 -in primes.enc -out primes.dec
 +
enter aes-256-cbc decryption password:
 +
 +
= Further reading =
 +
 +
* Paul Heinlein. [https://www.madboa.com/geek/openssl/ "OpenSSL Command-Line HOWTO"]. Has many quick cookbook-style recipes for doing common tasks using the "openssl" command-line application.
 +
 +
[[Category:Examples]]
 
[[Category:Shell level]]
 
[[Category:Shell level]]

Latest revision as of 11:56, 24 June 2022

The openssl program provides a rich variety of commands, each of which often has a wealth of options and arguments. Many commands use an external configuration file for some or all of their arguments and have a -config option to specify that file. The environment variable OPENSSL_CONF can be used to specify the location of the configuration file. If the environment variable is not specified, a default file is created in the default certificate storage area called openssl.cnf. The settings in this default configuration file depend on the flags set when the version of OpenSSL being used was built.

This article is an overview of the available tools provided by OpenSSL. For all of the details on usage and implementation, you can find the manpages, which are automatically generated from the source code at the official OpenSSL project home. Likewise, the source code itself may be found on the OpenSSL project home page, as well as on the OpenSSL Github. The main OpenSSL site also includes an overview of the command-line utilities, as well as links to all of their respective documentation.

Getting Started[edit]

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:

$ openssl command [ command_options ] [ command_arguments ]

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.

OpenSSL> prime -generate -bits 24
13467269
OpenSSL> prime -generate -bits 24
16651079
OpenSSL> quit

Basic Tasks[edit]

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[edit]

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

Since OpenSSL 3.0, there are equivalent invocations such as:

$ 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

$ openssl list -standard-commands

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.

$ openssl help genpkey
General options:
 -help               Display this summary
 -engine val         Use engine, possibly a hardware device
 -paramfile infile   Parameters file
 -algorithm val      The public key algorithm
 -quiet              Do not output status while generating keys
 -pkeyopt val        Set the public key algorithm option as opt:value
 -config infile      Load a configuration file (this may load modules)
Output options:
 -out outfile        Output file
 -outform PEM|DER    output format (DER or PEM)
 -pass val           Output file pass phrase source
 -genparam           Generate parameters, not key
 -text               Print the in text
 -*                  Cipher to use to encrypt the key
Provider options:
 -provider-path val  Provider load path (must be before 'provider' argument if required)
 -provider val       Provider to load (can be specified multiple times)
 -propquery val      Property query used when fetching algorithms
Order of options may be important!  See the documentation.

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[edit]

$ 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:

$ openssl version -help
Usage: version [options]
General options:
 -help  Display this summary
Output options:
 -a     Show all data
 -b     Show build date
 -d     Show configuration directory
 -e     Show engines directory
 -m     Show modules directory
 -f     Show compiler flags used
 -o     Show some internal datatype options
 -p     Show target build platform
 -r     Show random seeding options
 -v     Show library version
 -c     Show CPU settings info

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[edit]

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[edit]

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).
dhparam Generation and Management of Diffie-Hellman Parameters. Superseded by genpkey(1) and pkeyparam(1).
dsa DSA Data Management.
dsaparam DSA Parameter Generation and Management. Superseded by genpkey(1) and 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.
gendsa Generation of DSA Private Key from Parameters. Superseded by genpkey(1) and 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.
rsautl RSA utility for signing, verification, encryption, and decryption. Superseded by pkeyutl(1).
s_client This implements a generic SSL/TLS client which can establish a transparent connection to a remote server speaking SSL/TLS.
s_server This implements a generic SSL/TLS server which accepts connections from remote 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[edit]

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:

$ openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem

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:

$ openssl list -cipher-algorithms

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.

$ openssl pkey -in private-key.pem -text

The above command yields the following output in my specific case. Your output will differ but should be structurally similar.

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDZD6IMLRFk4CaF
w0rhRienwuE5EZ6xFE8e3C5TVi1+d9Enhi38RgkwD7UlWxPE6AWhp5T3kfrFWdak
1lZFVPp7/btOKLjKUru15nLoA4AKYtz9W9PhsM0dyzLc6FQ6K4ReQam5pHCqI2zF
82MwE+eIAduvuqyoQLKiI608EArWZqDtMUpBJzv0UVEYvRdnMWpCwfzpI+hPJywV
CcTlNCT/ctGgBKyIx+dDuZ7bR9MNmSW7GreJEbTH+R13xT3dd/JCka1+LYCl4h0q
oWhFPhOkvQzmmSzUmZlAlTDQLv2eAdJIrQcsnKZ3SsIOCC/3IpqwSzpid38Ill4O
xH6XIrVFAgMBAAECggEBAJ2MC0JrM8TULSHJrf/0u7O4b2DMuTIuW386sSUr17mD
nfviGF6TNvf7bq++e4rgHbZHvIg1HJ9Bpdne+J86HtUARYNlazru8fAFZEGiyLzB
JUV/8TpO6ZJGepR8zSWrkFgZsOddw6i6LalADy5GRDcjoiDajZdR3lZxLrv5qOQU
I1vKTf4Zs2Tl3gnaJ/Il1gBHIQ9W9xUH8jPBIwj51iXwCh8H0BiDPvFkU7cHIFCP
sJhGsGp6OS3uSwwQuSE+NqbuPfVilysCcwgZduknyio0QO1YfMBL6+XoKE/bFHsn
N+FzzczQg9sWyiwVR+3EeI9kp4JSElNh2nqG96i4QAECgYEA76OLUGrShHb4saoP
aYnBAKLEdWj5K483JdY6BSbdd5RkDbJG8ExmcbfTas/BGdKc4iVCkxV3ysxKnX18
PfxATHDLL8NMa+gGgZY5oTKUsrXEpS132HhCJ9T9LoesQjRb4kOZH8POVqm6O4Xf
lCt0y1+M1eQHI1NPO9CmPBgouEUCgYEA5+F4SS8RMyYRkU/kx195fwh0hhaOElzr
E8mZou3NFL/XT6/9t+2+7sMTuiQCP9zIa6s+/rrXdjWtrTcDp4WlDITas0UUgZhv
YVBQBF4vhHxIVwJxnT9Gwi4XM1JlFmVHofWD71P6DRe7jSWRS3CujP3AE9vmpWMx
tE1D9qLiWQECgYB445LzFYBvrKjWz4iI4CJKFNJwvGz+iXfzkXehg7KzkVtMAYSB
0rjXYzm3J2ktgq778nn8Qxc0agy2GEil6GvzY+9MgAQ8Z0do9gTKif6zjLjP7vkH
bdtJxsuWPoEqwMkdgqZrfNbJp0O4pVddovJ/agtdF3R2YJ+W+DH0HOfl1QKBgFnM
c2zEEYEhaQRBUHP1gXO0rouPCI4L9e2/0QPL2/QBJzzxBuzH4X1NhsI7V7OrqOIp
e0fiy7Y3q369I2ko1HY4rQln4z0c72VcWOCYKQbBqrInfCBNdPWWK93wNr2pk0gh
cGqqtteDLVrIBbCVfsOTMWN/cZ7y/zi4A23sPoQBAoGAEPzcIjOyoB97Pzd7iNim
Gin8RkwXIiFGSHo8vAh74CKBNokThM50OUNm5T2eJ4huzPpowQ+ID1mB5EjEai9n
JY9ll3cUpawiIIW/6uGTHyXfvZWNtqEYXrVJ6fcDaKcW4y3cplNj/SJaBW8HXsW7
YGHW3zHsgy7EOAOzPwlm9oE=
-----END PRIVATE KEY-----
RSA Private-Key: (2048 bit, 2 primes)
modulus:
    00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46:
    27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56:
    2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b:
    13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6:
    56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6:
    72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb:
    32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23:
    6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40:
    b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27:
    3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23:
    e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04:
    ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a:
    b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91:
    ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd:
    0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01:
    d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22:
    9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22:
    b5:45
publicExponent: 65537 (0x10001)
privateExponent:
    00:9d:8c:0b:42:6b:33:c4:d4:2d:21:c9:ad:ff:f4:
    bb:b3:b8:6f:60:cc:b9:32:2e:5b:7f:3a:b1:25:2b:
    d7:b9:83:9d:fb:e2:18:5e:93:36:f7:fb:6e:af:be:
    7b:8a:e0:1d:b6:47:bc:88:35:1c:9f:41:a5:d9:de:
    f8:9f:3a:1e:d5:00:45:83:65:6b:3a:ee:f1:f0:05:
    64:41:a2:c8:bc:c1:25:45:7f:f1:3a:4e:e9:92:46:
    7a:94:7c:cd:25:ab:90:58:19:b0:e7:5d:c3:a8:ba:
    2d:a9:40:0f:2e:46:44:37:23:a2:20:da:8d:97:51:
    de:56:71:2e:bb:f9:a8:e4:14:23:5b:ca:4d:fe:19:
    b3:64:e5:de:09:da:27:f2:25:d6:00:47:21:0f:56:
    f7:15:07:f2:33:c1:23:08:f9:d6:25:f0:0a:1f:07:
    d0:18:83:3e:f1:64:53:b7:07:20:50:8f:b0:98:46:
    b0:6a:7a:39:2d:ee:4b:0c:10:b9:21:3e:36:a6:ee:
    3d:f5:62:97:2b:02:73:08:19:76:e9:27:ca:2a:34:
    40:ed:58:7c:c0:4b:eb:e5:e8:28:4f:db:14:7b:27:
    37:e1:73:cd:cc:d0:83:db:16:ca:2c:15:47:ed:c4:
    78:8f:64:a7:82:52:12:53:61:da:7a:86:f7:a8:b8:
    40:01
prime1:
    00:ef:a3:8b:50:6a:d2:84:76:f8:b1:aa:0f:69:89:
    c1:00:a2:c4:75:68:f9:2b:8f:37:25:d6:3a:05:26:
    dd:77:94:64:0d:b2:46:f0:4c:66:71:b7:d3:6a:cf:
    c1:19:d2:9c:e2:25:42:93:15:77:ca:cc:4a:9d:7d:
    7c:3d:fc:40:4c:70:cb:2f:c3:4c:6b:e8:06:81:96:
    39:a1:32:94:b2:b5:c4:a5:2d:77:d8:78:42:27:d4:
    fd:2e:87:ac:42:34:5b:e2:43:99:1f:c3:ce:56:a9:
    ba:3b:85:df:94:2b:74:cb:5f:8c:d5:e4:07:23:53:
    4f:3b:d0:a6:3c:18:28:b8:45
prime2:
    00:e7:e1:78:49:2f:11:33:26:11:91:4f:e4:c7:5f:
    79:7f:08:74:86:16:8e:12:5c:eb:13:c9:99:a2:ed:
    cd:14:bf:d7:4f:af:fd:b7:ed:be:ee:c3:13:ba:24:
    02:3f:dc:c8:6b:ab:3e:fe:ba:d7:76:35:ad:ad:37:
    03:a7:85:a5:0c:84:da:b3:45:14:81:98:6f:61:50:
    50:04:5e:2f:84:7c:48:57:02:71:9d:3f:46:c2:2e:
    17:33:52:65:16:65:47:a1:f5:83:ef:53:fa:0d:17:
    bb:8d:25:91:4b:70:ae:8c:fd:c0:13:db:e6:a5:63:
    31:b4:4d:43:f6:a2:e2:59:01
exponent1:
    78:e3:92:f3:15:80:6f:ac:a8:d6:cf:88:88:e0:22:
    4a:14:d2:70:bc:6c:fe:89:77:f3:91:77:a1:83:b2:
    b3:91:5b:4c:01:84:81:d2:b8:d7:63:39:b7:27:69:
    2d:82:ae:fb:f2:79:fc:43:17:34:6a:0c:b6:18:48:
    a5:e8:6b:f3:63:ef:4c:80:04:3c:67:47:68:f6:04:
    ca:89:fe:b3:8c:b8:cf:ee:f9:07:6d:db:49:c6:cb:
    96:3e:81:2a:c0:c9:1d:82:a6:6b:7c:d6:c9:a7:43:
    b8:a5:57:5d:a2:f2:7f:6a:0b:5d:17:74:76:60:9f:
    96:f8:31:f4:1c:e7:e5:d5
exponent2:
    59:cc:73:6c:c4:11:81:21:69:04:41:50:73:f5:81:
    73:b4:ae:8b:8f:08:8e:0b:f5:ed:bf:d1:03:cb:db:
    f4:01:27:3c:f1:06:ec:c7:e1:7d:4d:86:c2:3b:57:
    b3:ab:a8:e2:29:7b:47:e2:cb:b6:37:ab:7e:bd:23:
    69:28:d4:76:38:ad:09:67:e3:3d:1c:ef:65:5c:58:
    e0:98:29:06:c1:aa:b2:27:7c:20:4d:74:f5:96:2b:
    dd:f0:36:bd:a9:93:48:21:70:6a:aa:b6:d7:83:2d:
    5a:c8:05:b0:95:7e:c3:93:31:63:7f:71:9e:f2:ff:
    38:b8:03:6d:ec:3e:84:01
coefficient:
    10:fc:dc:22:33:b2:a0:1f:7b:3f:37:7b:88:d8:a6:
    1a:29:fc:46:4c:17:22:21:46:48:7a:3c:bc:08:7b:
    e0:22:81:36:89:13:84:ce:74:39:43:66:e5:3d:9e:
    27:88:6e:cc:fa:68:c1:0f:88:0f:59:81:e4:48:c4:
    6a:2f:67:25:8f:65:97:77:14:a5:ac:22:20:85:bf:
    ea:e1:93:1f:25:df:bd:95:8d:b6:a1:18:5e:b5:49:
    e9:f7:03:68:a7:16:e3:2d:dc:a6:53:63:fd:22:5a:
    05:6f:07:5e:c5:bb:60:61:d6:df:31:ec:83:2e:c4:
    38:03:b3:3f:09:66:f6:81

Keep in mind the above key was generated solely for pedagogical purposes; never give anyone access to your private keys.

Generating a Public Key[edit]

Having previously generated your private key, you may generate the corresponding public key using the following command.

$ openssl pkey -in private-key.pem -out public-key.pem -pubout

You may once again view the key details, using a slightly different command this time.

$ openssl pkey -in public-key.pem -pubin -text

The output for the public key will be shorter, as it carries much less information, and it will look something like this.

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2Q+iDC0RZOAmhcNK4UYn
p8LhORGesRRPHtwuU1YtfnfRJ4Yt/EYJMA+1JVsTxOgFoaeU95H6xVnWpNZWRVT6
e/27Tii4ylK7teZy6AOACmLc/VvT4bDNHcsy3OhUOiuEXkGpuaRwqiNsxfNjMBPn
iAHbr7qsqECyoiOtPBAK1mag7TFKQSc79FFRGL0XZzFqQsH86SPoTycsFQnE5TQk
/3LRoASsiMfnQ7me20fTDZkluxq3iRG0x/kdd8U93XfyQpGtfi2ApeIdKqFoRT4T
pL0M5pks1JmZQJUw0C79ngHSSK0HLJymd0rCDggv9yKasEs6Ynd/CJZeDsR+lyK1
RQIDAQAB
-----END PUBLIC KEY-----
RSA Public-Key: (2048 bit)
Modulus:
    00:d9:0f:a2:0c:2d:11:64:e0:26:85:c3:4a:e1:46:
    27:a7:c2:e1:39:11:9e:b1:14:4f:1e:dc:2e:53:56:
    2d:7e:77:d1:27:86:2d:fc:46:09:30:0f:b5:25:5b:
    13:c4:e8:05:a1:a7:94:f7:91:fa:c5:59:d6:a4:d6:
    56:45:54:fa:7b:fd:bb:4e:28:b8:ca:52:bb:b5:e6:
    72:e8:03:80:0a:62:dc:fd:5b:d3:e1:b0:cd:1d:cb:
    32:dc:e8:54:3a:2b:84:5e:41:a9:b9:a4:70:aa:23:
    6c:c5:f3:63:30:13:e7:88:01:db:af:ba:ac:a8:40:
    b2:a2:23:ad:3c:10:0a:d6:66:a0:ed:31:4a:41:27:
    3b:f4:51:51:18:bd:17:67:31:6a:42:c1:fc:e9:23:
    e8:4f:27:2c:15:09:c4:e5:34:24:ff:72:d1:a0:04:
    ac:88:c7:e7:43:b9:9e:db:47:d3:0d:99:25:bb:1a:
    b7:89:11:b4:c7:f9:1d:77:c5:3d:dd:77:f2:42:91:
    ad:7e:2d:80:a5:e2:1d:2a:a1:68:45:3e:13:a4:bd:
    0c:e6:99:2c:d4:99:99:40:95:30:d0:2e:fd:9e:01:
    d2:48:ad:07:2c:9c:a6:77:4a:c2:0e:08:2f:f7:22:
    9a:b0:4b:3a:62:77:7f:08:96:5e:0e:c4:7e:97:22:
    b5:45
Exponent: 65537 (0x10001)

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[edit]

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.

$ openssl ecparam -list_curves
  secp112r1 : SECG/WTLS curve over a 112 bit prime field
  secp112r2 : SECG curve over a 112 bit prime field
  secp128r1 : SECG curve over a 128 bit prime field
  secp128r2 : SECG curve over a 128 bit prime field
  secp160k1 : SECG curve over a 160 bit prime field
  ...

For this example I will use the prime256v1 curve, which is an X9.62/SECG curve over a 256 bit prime field.

Generating the Curve Parameters[edit]

Having selected our curve, we now call ecparam to generate our parameters file.

$ openssl ecparam -name prime256v1 -out prime256v1.pem

Printing Parameters to Standard Out[edit]

You can print the generated curve parameters to the terminal output with the following command:

$ openssl ecparam -in prime256v1.pem -noout -text
ASN1 OID: prime256v1
NIST CURVE: P-256

Printing Parameters as C Code[edit]

Analogously, you may also output the generated curve parameters as C code. The parameters can then be loaded by calling the get_ec_group_XXX() function. To print the C code to the current terminal's output, the following command may be used:

$ openssl ecparam -in prime256v1.pem -noout -C

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,
        ...

Generating the Key[edit]

With the curve parameters in hand, we are now free to generate the key. Just as with the [#Generating an RSA Private Key|RSA] example above, we may optionally specify a cipher algorithm with which to encrypt the private key. The call to generate the key using the elliptic curve parameters generated in the example above looks like this:

$ openssl genpkey -aes256 -paramfile prime256v1.pem -out private-key.pem
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

Putting it All Together[edit]

The process of generation a curve based on elliptic-curves can be streamlined by calling the genpkey command directly and specifying both the algorithm and the name of the curve to use for parameter generation. In it's simplest form, the command to generate a key based on the same curve as in the example above looks like this:

$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256

This command will result in the generated key being printed to the terminal's output.

$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256

-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgqqYoJGowXJ5/GTkB
SRLnBMNWLoQ2RM/QxrY+bfDDGRahRANCAASPY4eTANkwIIAWhh32eoFl2YFLJSWy
bdITdZ82O5JDpDijmGmJ2hepe5afek9WVqxMPYjmbTwMPO3xMGbqUiJD
-----END PRIVATE KEY-----

Remember that you can specify a cipher algorithm to encrypt the key with, which something you may or may not want to do, depending on your specific use case. Here is a slightly more complete example showing a key generated with a password and written to a specific output file.

$ openssl genpkey -aes256 -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private-key.pem
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

Just as with the previous example, you can use the pkey command to inspect your newly-generated key.

$ openssl pkey -in private-key.pem -text
Enter pass phrase for private-key.pem:
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEO7CxgTwi0hsjdbp
sXWuU2x2flLthxqXabYDOqOZCvuhRANCAAQVTLkeCBJdvMnqwZKYJxrPvTTuanrD
NkyAPQCARKsQ7bVrP6ky/5uAcAvjuZB0xKCcSp7roXLWRzD/y/ik8P5R
-----END PRIVATE KEY-----
Private-Key: (256 bit)
priv:
    10:ee:c2:c6:04:f0:8b:48:6c:8d:d6:e9:b1:75:ae:
    53:6c:76:7e:52:ed:87:1a:97:69:b6:03:3a:a3:99:
    0a:fb
pub:
    04:15:4c:b9:1e:08:12:5d:bc:c9:ea:c1:92:98:27:
    1a:cf:bd:34:ee:6a:7a:c3:36:4c:80:3d:00:80:44:
    ab:10:ed:b5:6b:3f:a9:32:ff:9b:80:70:0b:e3:b9:
    90:74:c4:a0:9c:4a:9e:eb:a1:72:d6:47:30:ff:cb:
    f8:a4:f0:fe:51
ASN1 OID: prime256v1
NIST CURVE: P-256

For more details on elliptic curve cryptography or key generation, check out the manpages.

Base64 Encoding Strings[edit]

For simple string encoding, you can use "here string" syntax with the base64 command as below. Intuitively, the -e flag specifies the action to be encoding.

$ openssl base64 -e <<< 'Welcome to openssl wiki'
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK

Similarly, the base64 command's -d flag may be used to indicate decoding mode.

$ openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'
Welcome to openssl wiki


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.

openssl base64 -d -A <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
Welcome to openssl wiki with a very long line that splits...

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.

Generating a File Hash[edit]

One of the most basic uses of the dgst command (short for digest) is viewing the hash of a given file. To do this, simply invoke the command with the specified digest algorithm to use. For this example, I will be hashing an arbitrary file on my system using the MD5, SHA1, and SHA384 algorithms.

$ openssl dgst -md5 primes.dat
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664

$ openssl dgst -sha1 primes.dat
SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32

$ openssl dgst -sha384 primes.dat
SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271

For a list of the available digest algorithms, you can use the following command.

$ openssl list -digest-algorithms
RSA-MD4 => MD4
RSA-MD5 => MD5
RSA-MDC2 => MDC2
RSA-RIPEMD160 => RIPEMD160
RSA-SHA1 => SHA1
RSA-SHA1-2 => RSA-SHA1
...

You can also use a similar command to see the available digest commands:

$ openssl list -digest-commands
blake2b512        blake2s256        md5               sha1              
sha224            sha256            sha3-224          sha3-256          
sha3-384          sha3-512          sha384            sha512            
sha512-224        sha512-256        shake128          shake256          
sm3  

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

$ openssl sha1 primes.dat
SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32

$ openssl sha384 primes.dat
SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271

File Encryption and Decryption[edit]

The following example demonstrates a simple file encryption and decryption using the enc command. The first argument is the cipher algorithm to use for encrypting the file. For this example I carefully selected the AES-256 algorithm in CBC Mode by looking up the available ciphers and picking out the first one I saw. To see the list of available ciphers, you can use the following command.

$ openssl enc -ciphers
Supported ciphers:
-aes-128-cbc               -aes-128-cfb               -aes-128-cfb1             
-aes-128-cfb8              -aes-128-ctr               -aes-128-ecb              
-aes-128-ofb               -aes-192-cbc               -aes-192-cfb              
-aes-192-cfb1              -aes-192-cfb8              -aes-192-ctr
...

You can also use the following command:

$ openssl list -cipher-algorithms
AES-128-CBC
AES-128-CBC-HMAC-SHA1
AES-128-CBC-HMAC-SHA256
id-aes128-CCM
AES-128-CFB
AES-128-CFB1
AES-128-CFB8
AES-128-CTR
...

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:

The analogous decryption command is as follows:

$ openssl enc -aes-256-cbc -d -iter 1000 -in primes.enc -out primes.dec
enter aes-256-cbc decryption password:

Further reading[edit]

  • Paul Heinlein. "OpenSSL Command-Line HOWTO". Has many quick cookbook-style recipes for doing common tasks using the "openssl" command-line application.