Working with OpenSSL
07 Feb 2015OpenSSL is the open source project that provides the world with SSL and TLS. In today’s post, I’ll walk through some simple tasks to encrypt and decrypt your data.
Features
OpenSSL is a very feature-rich library. It contains many pieces of functionality that you should study in more detail. The man page for it goes into all of these details in great depth.
Encoding information
Perhaps a slightly edge-case piece of functionality, OpenSSL has the ability to Base64 encode your information. It’s no where near actually securing your information, but the facility is there.
You can Base64 encode a string with the following command:
You can bring it back to plain text with the following:
Encrypt with a password
OpenSSL gives you the ability to encrypt a piece of information using a password. This is a simple way of securing your information without certificates, but isn’t a very strong strategy for information security.
Take a look under the Encoding and Cipher Commands for a full range of strategies here. Where we used the base64
options above, no password was asked for. This is because it’s just an encoding. If we were to use the bf
option (which will use the Blowfish Cipher), we’re prompted for a password.
password_enc.dat
contains what would appear to be garbage, but it is our string; just encrypted. To get our plain text back:
You need to enter the correct password in order to get your plain text back. Pretty simple. This is the process for any of the ciphers mentioned above.
Encrypt with a key pair
Stepping up the complexity, you can get OpenSSL to encrypt and decrypt your data using public-key cryptographyy.
First of all, we need to generate a public/private key pair. The following command will generate a private key. This will be an RSA keypair with a 4096 bit private key.
Now that the private key has been generated, we extract the public key from it:
You can view all of the details of your keypair details with the following command. It’s a pretty verbose information dump, so brace yourself.
We encrypt the source information with the public key and perform the decryption using the private key.
To encrypt the information:
To decrypt the information:
Working with certificates
You can use OpenSSL to generate a self-signed certificate.
Generating a self-signed certificate is a fairly simple process. The following will generate a certificate and private key (in the one file) that’s valid for 1 year. This certificate’s key won’t be protected by a passphrase.
You can shorted the key generation process (make it ask less questions) by specifying all of the subject details in the generation command:
Other functions
You can use OpenSSL to generate some random data for you as well. This is useful in scenarios where your application requires nonce data. The rand
switch does this easily:
Piping the contents of /dev/urandom
through OpenSSL’s base64 encoder will also perform the same task (with better entropy).
Prime testing is an important cryptographic step and can be achieved with the prime
switch:
A really practical utility bundled inside of OpenSSL is the testing server that you can instantiate to test out your certificates that you generate.
This starts a HTTPS server on your machine. You can point your web browser to https://server:4433/ to see how a browser responds to your certificate.
You can also use OpenSSL as a client to pull down remote certificates: