Difference between revisions of "Command Line Utilities"

From OpenSSLWiki
Jump to navigationJump to search
(→‎Commands: remove pretty useless reference to Standard commands and non-existing ones to other types of commands)
 
(27 intermediate revisions by 9 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.
  
When installed on your system openssl binary is entry point for many functions.
+
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.
  
* launching openssl without any parameter will enter an interactive mode with an OpenSSL> prompt
+
=Getting Started=
** to quit: quit
 
** enter a command it will set a command context in which parameters depends on command.
 
* to known what commands and parameters you can issue for openssl do :
 
** openssl help
 
<pre>
 
openssl:Error: 'help' is an invalid command.
 
  
Standard commands
+
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:
asn1parse        ca                ciphers          cms             
 
crl              crl2pkcs7        dgst              dh               
 
dhparam          dsa              dsaparam          ec               
 
ecparam          enc              engine            errstr           
 
gendh            gendsa            genpkey          genrsa           
 
nseq              ocsp              passwd            pkcs12           
 
pkcs7            pkcs8            pkey              pkeyparam       
 
pkeyutl          prime            rand              req             
 
rsa              rsautl            s_client          s_server         
 
s_time            sess_id          smime            speed           
 
spkac            srp              ts                verify           
 
version          x509             
 
  
Message Digest commands (see the `dgst' command for more details)
+
$ openssl command [ command_options ] [ command_arguments ]
md4              md5              rmd160            sha             
 
sha1             
 
  
Cipher commands (see the `enc' command for more details)
+
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.
aes-128-cbc      aes-128-ecb      aes-192-cbc      aes-192-ecb     
 
aes-256-cbc      aes-256-ecb      base64            bf               
 
bf-cbc            bf-cfb            bf-ecb            bf-ofb           
 
camellia-128-cbc  camellia-128-ecb  camellia-192-cbc  camellia-192-ecb 
 
camellia-256-cbc  camellia-256-ecb  cast              cast-cbc         
 
cast5-cbc        cast5-cfb        cast5-ecb        cast5-ofb       
 
des              des-cbc          des-cfb          des-ecb         
 
des-ede          des-ede-cbc      des-ede-cfb      des-ede-ofb     
 
des-ede3          des-ede3-cbc      des-ede3-cfb      des-ede3-ofb     
 
des-ofb          des3              desx              rc2             
 
rc2-40-cbc        rc2-64-cbc        rc2-cbc          rc2-cfb         
 
rc2-ecb          rc2-ofb          rc4              rc4-40           
 
seed              seed-cbc          seed-cfb          seed-ecb         
 
seed-ofb          zlib             
 
</pre>
 
  
== Commands by purpose ==
+
OpenSSL> prime -generate -bits 24
 +
13467269
 +
OpenSSL> prime -generate -bits 24
 +
16651079
 +
OpenSSL> quit
  
Following commands are grouped by purpose using a different grouping than of default openssl documentation / help.
+
=Basic Tasks=
  
=== Get information about your openssl toolkit ===
+
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]].
  
==== 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:
  
<pre>
+
$ 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
 
</pre>
 
  
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.
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.
  
==== Basic file ====
+
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.
  
to cipher a file or data to protect and share it protected by a shared key.
+
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.
  
symetric cipher :
+
$ man openssl
[[AES]] [[Blowfish]] [[RC4]] [[3DES]] [[RC2]] [[DES]] [[CAST5]] [[SEED]]
+
$ man openssl-genpkey
 +
$ man genpkey
  
block to stream conversion :
+
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.
[[ECB]] [[CBC]] [[OFB]] [[CFB]] [[CTR]] [[XTS]] [[GCM]]
 
  
compression :
+
==Getting Library Version Information==
[[ZLIB]]
 
  
<pre>
+
$ openssl version
Cipher commands (see the `enc' command for more details)
+
OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022)
aes-128-cbc      aes-128-ecb      aes-192-cbc      aes-192-ecb     
 
aes-256-cbc      aes-256-ecb      base64            bf               
 
bf-cbc            bf-cfb            bf-ecb            bf-ofb           
 
camellia-128-cbc  camellia-128-ecb  camellia-192-cbc  camellia-192-ecb 
 
camellia-256-cbc  camellia-256-ecb  cast              cast-cbc         
 
cast5-cbc        cast5-cfb        cast5-ecb        cast5-ofb       
 
des              des-cbc          des-cfb          des-ecb         
 
des-ede          des-ede-cbc      des-ede-cfb      des-ede-ofb     
 
des-ede3          des-ede3-cbc      des-ede3-cfb      des-ede3-ofb     
 
des-ofb          des3              desx              rc2             
 
rc2-40-cbc        rc2-64-cbc        rc2-cbc          rc2-cfb         
 
rc2-ecb          rc2-ofb          rc4              rc4-40           
 
seed              seed-cbc          seed-cfb          seed-ecb         
 
seed-ofb          zlib
 
</pre>
 
  
<pre>
+
As mentioned above, the <tt>version</tt> command's help menu may be queried for additional options like so:
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 -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
  
===== smime v2 pkcs7 1.5 =====
+
Using the <tt>-a</tt> option to show all version information yields the following output on my current machine:
  
<pre>
+
$ openssl version -a
openssl smime --help
+
OpenSSL 3.0.4 21 Jun 2022 (Library: OpenSSL 3.0.4 21 Jun 2022)
Usage smime [options] cert.pem ...
+
built on: Fri Jun 24 08:58:53 2022 UTC
where options are
+
platform: linux-x86_64
-encrypt      encrypt message
+
options:  bn(64,64)
-decrypt      decrypt encrypted message
+
compiler: gcc -fPIC -pthread -m64 -Wa,--noexecstack -Wall -O3 -DOPENSSL_USE_NODELETE -DL_ENDIAN -DOPENSSL_PIC -DOPENSSL_BUILDING_OPENSSL -DNDEBUG
-sign          sign message
+
OPENSSLDIR: "/usr/local/ssl"
-verify        verify signed message
+
ENGINESDIR: "/usr/local/lib64/engines-3"
-pk7out        output PKCS#7 structure
+
MODULESDIR: "/usr/local/lib64/ossl-modules"
-des3          encrypt with triple DES
+
  Seeding source: os-specific
-des          encrypt with DES
+
  CPUINFO: OPENSSL_ia32cap=0x7ffaf3ffffebffff:0x29c67af
-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 =====
 
  
<pre>
+
=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>
 
  
=== Create / Handle Public Key Certificates ===
+
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.
  
This requires you have a knowledge of what PKI is ( Certificate Authorities, Certificate Request, Certificate, Public Key, Private Key )
+
==Standard Commands==
  
Classical use case is to obtain a valid Certificate for a Secured Web site ( https protocol ).
+
{| class="wikitable" style="margin:auto; text-align: center; width: 65%;"
First you create a Private Key ( will be created together with Public key ).
+
|+ Overview of OpenSSL's command line utilities
Then create a Certificate Request for that private key with some informations for purpose of future Certificate.
+
! style="width: 25%; padding: 4px;" | Command
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.  
+
! style="width: 75%; padding: 4px;" | Description
Up to you to install your Private key together with the received Certificate on your system.  
+
|-
 +
|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.
 +
|}
  
It exists graphical front-end to operate openssl wihtin a GUI : [http://xca.sourceforge.net/ XCA]
+
==Generating an RSA Private Key==
  
==== Key Generation ====
+
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.
  
===== rsa / genrsa =====
+
$ openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem
  
RSA is the most common type of Public/Private Key.
+
To generate a password protected private key, the previous command may be slightly amended as follows:
Private Key part should never de disclosed while public key part is ... public.
 
  
<pre>
+
$ openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem
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>
+
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:
rsa help
 
unknown option help
 
rsa [options] <infile >outfile
 
where options are
 
-inform arg    input format - one of DER NET PEM
 
-outform arg    output format - one of DER NET PEM
 
-in arg        input file
 
-sgckey        Use IIS SGC key format
 
-passin arg    input file pass phrase source
 
-out arg        output file
 
-passout arg    output file pass phrase source
 
-des            encrypt PEM output with cbc des
 
-des3          encrypt PEM output with ede cbc des using 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
 
-text          print the key in text
 
-noout          don't print key out
 
-modulus        print the RSA key modulus
 
-check          verify key consistency
 
-pubin          expect a public key in input file
 
-pubout        output a public key
 
-engine e      use engine e, possibly a hardware device.
 
error in rsa
 
</pre>
 
  
===== dsa / gendsa=====
+
$ openssl list -cipher-algorithms
  
dsa is a less common Public/Private key scheme, but can be seen anyway, so ...
+
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.
  
<pre>
+
$ openssl pkey -in private-key.pem -text
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>
+
The above command yields the following output in my specific case. Your output will differ but should be structurally similar.
  
<pre>
+
-----BEGIN PRIVATE KEY-----
OpenSSL> dsa help
+
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDZD6IMLRFk4CaF
unknown option help
+
w0rhRienwuE5EZ6xFE8e3C5TVi1+d9Enhi38RgkwD7UlWxPE6AWhp5T3kfrFWdak
dsa [options] <infile >outfile
+
1lZFVPp7/btOKLjKUru15nLoA4AKYtz9W9PhsM0dyzLc6FQ6K4ReQam5pHCqI2zF
where options are
+
82MwE+eIAduvuqyoQLKiI608EArWZqDtMUpBJzv0UVEYvRdnMWpCwfzpI+hPJywV
  -inform arg    input format - DER or PEM
+
CcTlNCT/ctGgBKyIx+dDuZ7bR9MNmSW7GreJEbTH+R13xT3dd/JCka1+LYCl4h0q
  -outform arg    output format - DER or PEM
+
oWhFPhOkvQzmmSzUmZlAlTDQLv2eAdJIrQcsnKZ3SsIOCC/3IpqwSzpid38Ill4O
  -in arg        input file
+
xH6XIrVFAgMBAAECggEBAJ2MC0JrM8TULSHJrf/0u7O4b2DMuTIuW386sSUr17mD
  -passin arg    input file pass phrase source
+
nfviGF6TNvf7bq++e4rgHbZHvIg1HJ9Bpdne+J86HtUARYNlazru8fAFZEGiyLzB
  -out arg        output file
+
JUV/8TpO6ZJGepR8zSWrkFgZsOddw6i6LalADy5GRDcjoiDajZdR3lZxLrv5qOQU
  -passout arg    output file pass phrase source
+
I1vKTf4Zs2Tl3gnaJ/Il1gBHIQ9W9xUH8jPBIwj51iXwCh8H0BiDPvFkU7cHIFCP
  -engine e      use engine e, possibly a hardware device.
+
  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
  -aes128, -aes192, -aes256
+
PfxATHDLL8NMa+gGgZY5oTKUsrXEpS132HhCJ9T9LoesQjRb4kOZH8POVqm6O4Xf
                encrypt PEM output with cbc aes
+
lCt0y1+M1eQHI1NPO9CmPBgouEUCgYEA5+F4SS8RMyYRkU/kx195fwh0hhaOElzr
  -camellia128, -camellia192, -camellia256
+
E8mZou3NFL/XT6/9t+2+7sMTuiQCP9zIa6s+/rrXdjWtrTcDp4WlDITas0UUgZhv
                encrypt PEM output with cbc camellia
+
YVBQBF4vhHxIVwJxnT9Gwi4XM1JlFmVHofWD71P6DRe7jSWRS3CujP3AE9vmpWMx
  -seed          encrypt PEM output with cbc seed
+
tE1D9qLiWQECgYB445LzFYBvrKjWz4iI4CJKFNJwvGz+iXfzkXehg7KzkVtMAYSB
  -text          print the key in text
+
  0rjXYzm3J2ktgq778nn8Qxc0agy2GEil6GvzY+9MgAQ8Z0do9gTKif6zjLjP7vkH
  -noout          don't print key out
+
  bdtJxsuWPoEqwMkdgqZrfNbJp0O4pVddovJ/agtdF3R2YJ+W+DH0HOfl1QKBgFnM
  -modulus        print the DSA public value
+
  c2zEEYEhaQRBUHP1gXO0rouPCI4L9e2/0QPL2/QBJzzxBuzH4X1NhsI7V7OrqOIp
error in dsa
+
  e0fiy7Y3q369I2ko1HY4rQln4z0c72VcWOCYKQbBqrInfCBNdPWWK93wNr2pk0gh
</pre>
+
  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
  
===== Elliptic Curves / ec ecparam =====
+
Keep in mind the above key was generated solely for pedagogical purposes; never give anyone access to your private keys.
  
[[Elliptic_Curve_Cryptography]]
+
==Generating a Public Key==
  
<pre>
+
Having previously generated your private key, you may generate the corresponding public key using the following command.
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>
+
  $ openssl pkey -in private-key.pem -out public-key.pem -pubout
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 ====
+
You may once again view the key details, using a slightly different command this time.
  
When you want to act as a Certificate Authority.
+
$ openssl pkey -in public-key.pem -pubin -text
  
OpenSSL> ca
+
The output for the public key will be shorter, as it carries much less information, and it will look something like this.
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...
+
-----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)
  
==== Certificate Request / pkcs10 / req ====
+
For more information on generating keys, see the source code documentation, located in the <tt>doc/HOWTO/keys.txt</tt> file.
  
OpenSSL> req ?
+
==Generating Keys Based on Elliptic Curves==
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
+
There are essentially two steps to generating a key:
  
==== Certificates AKA x509  ====
+
# Generate the parameters for the specific curve you are using
 +
# Use those parameters to generate the key
  
x509 command allows you to display content of a x509 certificate and to convert it from/to [[PEM]], [[NET]] or [[DER]] formats.
+
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.
  
OpenSSL> x509 help
+
  $ openssl ecparam -list_curves
unknown option help
+
   secp112r1 : SECG/WTLS curve over a 112 bit prime field
usage: x509 args
+
   secp112r2 : SECG curve over a 112 bit prime field
  -inform arg    - input format - default PEM (one of DER, NET or PEM)
+
   secp128r1 : SECG curve over a 128 bit prime field
-outform arg   - output format - default PEM (one of DER, NET or PEM)
+
   secp128r2 : SECG curve over a 128 bit prime field
-keyform arg   - private key format - default PEM
+
   secp160k1 : SECG curve over a 160 bit prime field
-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 ====
+
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.
  
Client Certificate is a language abuse, but anyway it is knid of file you need to install on your system when SSL/TLS server require Client Authentication.
+
===Generating the Curve Parameters===
Those kind of certificates credentials are known as pkcs12 or pfx files.
 
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>
+
Having selected our curve, we now call <tt>ecparam</tt> to generate our parameters file.
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 -name prime256v1 -out prime256v1.pem
  
==== s_server ====
+
====Printing Parameters to Standard Out====
  
==== s_client ====
+
You can print the generated curve parameters to the terminal output with the following command:
  
==== ocsp ====
+
$ openssl ecparam -in prime256v1.pem -noout -text
 +
ASN1 OID: prime256v1
 +
NIST CURVE: P-256
  
 +
====Printing Parameters as C Code====
  
=== Signing / Digest and Timestamping ===
+
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:
  
==== Signing / Digest ====
+
$ openssl ecparam -in prime256v1.pem -noout -C
  
<pre>
+
And here are the first few lines of the corresponding output:
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
+
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===
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 ===
+
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:
  
==== ASN.1 ====
+
$ openssl genpkey -aes256 -paramfile prime256v1.pem -out private-key.pem
 +
Enter PEM pass phrase:
 +
Verifying - Enter PEM pass phrase:
  
[[DER]] decoding
+
===Putting it All Together===
  
openssl asn1parse
+
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:
  
==== Base64 ====
+
$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256
  
base64 encoding / decoding
+
This command will result in the generated key being printed to the terminal's output.
  
===== a String =====
+
$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256
 +
 +
-----BEGIN PRIVATE KEY-----
 +
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgqqYoJGowXJ5/GTkB
 +
SRLnBMNWLoQ2RM/QxrY+bfDDGRahRANCAASPY4eTANkwIIAWhh32eoFl2YFLJSWy
 +
bdITdZ82O5JDpDijmGmJ2hepe5afek9WVqxMPYjmbTwMPO3xMGbqUiJD
 +
-----END PRIVATE KEY-----
  
<pre>
+
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 base64 -e <<< 'Welcome to openssl wiki'
 
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK
 
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'
 
Welcome to openssl wiki
 
</pre>
 
  
warning '''base64 line length is limited to 64 characters by default in openssl''' :
+
$ openssl genpkey -aes256 -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private-key.pem
 +
Enter PEM pass phrase:
 +
Verifying - Enter PEM pass phrase:
  
<pre>
+
Just as with the previous example, you can use the <tt>pkey</tt> command to inspect your newly-generated key.
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 64 characters use -A option :
+
$ 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 base64 -d -A <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
 
Welcome to openssl wiki with a very long line that splits...
 
</pre>
 
  
This is anyway better to actualy split base64 result in 64 characters lines since -A option is BUGGY ( limit with long files ).
+
==Base64 Encoding Strings==
  
==== DER <-> PEM conversion ====
+
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.
  
on each command handling PEM and DER format an '''inform''' and '''outform''' options are provided to specify it.
+
$ openssl base64 -e <<< 'Welcome to openssl wiki'
Then it is easy to read it in format and write it in another
+
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK
  
==== pkcs8 / pkcs5 ====
+
Similarly, the base64 command's <tt>-d</tt> flag may be used to indicate decoding mode.
  
pkcs8 is a format to store private keys.
+
$ openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'
pkcs8 uses various pkcs5 version as subformat.
+
Welcome to openssl wiki
  
<pre>
+
 
openssl pkcs8 --help
+
'''''Note:''''' base64 line length is limited to 76 characters by default in openssl (and generated with 64 characters per line).
Usage pkcs8 [options]
+
 
where options are
+
openssl base64 -e <<< 'Welcome to openssl wiki with a very long line that splits...'
-in file       input file
+
V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRo
-inform X      input format (DER or PEM)
+
YXQgc3BsaXRzLi4uCg==
-passin arg    input file pass phrase source
+
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
-outform X      output format (DER or PEM)
+
 
-out file      output file
+
=> NOTHING!
-passout arg    output file pass phrase source
+
 
-topk8         output PKCS8 file
+
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.
-nooct         use (nonstandard) no octet format
+
 
-embed          use (nonstandard) embedded DSA parameters format
+
openssl base64 -d -A <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
-nsdb          use (nonstandard) DSA Netscape DB format
+
Welcome to openssl wiki with a very long line that splits...
-noiter        use 1 as iteration count
+
 
-nocrypt        use or expect unencrypted private key
+
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.
-v2 alg        use PKCS#5 v2.0 and cipher "alg"
+
 
-v1 obj        use PKCS#5 v1.5 and cipher "alg"
+
==Generating a File Hash==
  -engine e       use engine e, possibly a hardware device.
+
 
</pre>
+
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|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==
 +
 
 +
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 <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]]

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.