Networking

Learn to use the openssl command-line program

OpenSSL can be used to create, request, sign, and revoke certificates and can also be used to perform other cryptographic operations such as creating hashes for files, testing SSL connections, and more.

OpenSSL is more than just an open source SSL library implementation. It can be used to create, request, sign, and revoke certificates and can also be used to perform other cryptographic operations such as creating hashes for files, testing SSL connections, and more. This week, we'll take a look at some of the interesting things that can be done with the openssl command-line program.

To test SSL connections to a mail server, use the openssl command with the s_client parameter:

$ openssl s_client -connect smtp.myhost.com:25 -starttls smtp

This essentially opens a telnet-like connection to smtp.myhost.com on port 25 using the STARTTLS extension. This is an interactive session, so you can send commands to the remote SMTP server as well as view the certificate used, view the details of the SSL session, and more. To test SMTP over SSL, don't use the -starttls option:

$ openssl s_client -connect smtp.myhost.com:465

The above can also be used to connect to any service that uses SSL, such as HTTPS (port 443), POP3 over SSL (port 995), and so forth.

The openssl command can also be used to create digests of a file, which can be used to verify that a file has not been tampered with:

$ echo "test file"> foo.txt
$ openssl dgst -md5 foo.txt
MD5(foo.txt)= b05403212c66bdc8ccc597fedf6cd5fe
$ openssl dgst -sha1 foo.txt
SHA1(foo.txt)= 0181d93fee60b818e3f92e470ea97a2aff4ca56a

To view the other message digests that can be used, look at the output of openssl list-message-digest-commands.

You can also use openssl to encrypt files. To view the list of available ciphers, use openssl list-cipher-commands. Once you've chosen a cipher to use, you can encrypt the file using the following commands:

$ openssl enc -aes-256-cbc -salt -in foo.txt -out foo.enc
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
$ file foo.enc
foo.enc: data
$ cat foo.enc
Salted__yvi{!e????i"Yt?;(Ѱ e%                                                                                                                         $ openssl enc -d -aes-256-cbc -in foo.enc
enter aes-256-cbc decryption password:
test file

In the above example, the file foo.txt was encrypted using 256-bit AES in CBC mode, the encrypted copy being saved as the file foo.enc. Looking at the contents of the file provide gibberish. Decrypting the file is done using the -d option; however, keep in mind that not only do you need to remember the password, you also need to know the cipher used.

As you can see, OpenSSL provides more than just a library for other applications to use, and the openssl command-line binary is a powerful program in its own right, allowing for many uses.

Get the PDF version of this tip.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks