In my previous article, we looked at how email started and how it works. In this article we continue with email, but from the security point of view.
I know of an owner of an IT school who was preparing to sell to a large international educational conglomerate. In this small community, many of the IT companies had relationships that were both professional and personal: in this case, their local ISP had direct ties to one of the competing IT schools. There was a bit of a panic when management became aware of this fact, and were concerned about any leaked information that would affect the impending sale. The "official" solution was to send all important communications in a password-encrypted Microsoft Word document — not quite the solution expected of an IT organization.
Using a cookbook approach, I will demonstrate how to set up various email clients to use authentication and encryption.
There are several freely available technologies in a Unix/Linux environment;
OpenSSL, OpenPGP, and PGP to name just a few. For the sake of clarity, I'm only going
to deal with OpenSSL here. Otherwise, for all intents and purposes, all of the
above mentioned technologies are practical and useful. The applications and
services that I'll use are
pine, Mozilla, Apache, and Sendmail.
OpenSSL is a cryptography toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS v1) network protocols and related required cryptography standards. OpenSSL can be used to make certificates that follow the Public Key Infrastructure (PKI).
Only the most basic definitions regarding public key encryption and digital signing are explained in this article. Please refer to the suggested articles and sites for further readings on this topic.
A Certification Authority (CA) is an organization, either commercial or nonprofit, that is capable of validating a certificate. The best-known CAs are VeriSign and Thawte. Open your security preferences in your browser and you should find a list of certificates from multiple CAs already defined there.
A digital certificate is mathematically generated data that is almost impossible to duplicate and therefore can be considered unique and non-reproducible. It is useful as a mechanism that demonstrates that an email does indeed come from a particular organization or individual and that it has neither been tampered nor forged.
Encryption is the translation of data into a secret code. Encryption is the most effective way to achieve data security. To read an encrypted file, you must have access to a secret key or password that enables you to decrypt it. Unencrypted data is called plain text; encrypted data is referred to as cipher text.
The use of combined public and private keys is known as asymmetric cryptography. A public key is a value provided by some designated authority, such as a commercial Certification Authority, as an encryption key. A private key is secretly derived from the public key and is controlled by the person who wishes to sign email or receive encrypted messages by others who encrypt their messages using their public keys.
A Public Key Infrastructure (PKI) consists of three parts:
Reference sites: OpenSSL, Webopedia, techtarget.com,, Public Key Infrastructure, OpenPGP, PGP
Creating a self-signed certificate is simple. Just navigate on a text
console to a location in your directory where the files and directories will be
created and type the correct path to the Perl script
$ /usr/share/ssl/misc/CA.pl -newca
Figure 1. Running
You will now be prompted to answer a number of questions:
Figure 2. CA questions
After successfully answering all the questions, you should have a directory called demoCA with a number of files and directories in it. Keep in mind that if you forget the PEM passphrase, you will have rendered the certificate useless and will have to start all over again.
The Root Certification Authority Certificate authenticates all certificates that the browser accepts through normal email correspondence that is signed with this certificate. It is with this certificate that all subsequent certificates that you create and sign against can be verified as legitimate. Installing this certificate on a company web site allows the employees to incorporate this into their browsers and further secure their company communications as they sign and encrypt their messages. Take demoCA/cacert.pem and copy it as cacert.crt to your web server. You can then pick up the Root Certification Authority Certificate from the web server via the browser.
To create your public and private keys, generate a certificate request by typing the following in the same directory as you did with the previous command:
$ /usr/share/ssl/misc/CA.pl -newreq
As with the Root CA script, this script will guide you through series of questions and requests that you must answer before the private key in newreq.pem and the certificate newcert.pem are created and signed with the file cacert.pem, which is your Root Authority certificate.
Beware of your newreq.pem file, as it contains not only a
certificate request, but also your private key. The
section is not required when you sign it. So if you request that someone else
sign your certificate request, ensure that you have removed the
KEY- section from the file. If you sign a certificate request for someone
else, ask him for his
-CERTIFICATE REQUEST- section and not the
private key section.
Proceed by typing the following command:
$ /usr/share/ssl/misc/CA.pl -sign
The certificate that you have created will now be signed with the Root Certificate's private key at demoCA/private/cakey.pem. A new file called newcert.pem has now been created. This file is your certificate signed with the Root CA certificate.
We're almost done! A PKCS#12 file contains the user's certificate, his
private key, and the CA certificate. The
CA.pl script uses the user
certificate and private key in the file newcert.pem and the
CA certificate in the file demoCA/cacert.pem. The PKCS#12
file can be imported directly into a browser. In addition, a user-specific name
can be added as an additional argument on the command line for the
$ /usr/share/ssl/misc/CA.pl -pkcs12
You will be asked for the PEM password and then asked to a create an export password. You now have a file called newcert.p12 that will be compatible with Mozilla, Netscape, Outlook, and Outlook Express for signing and encrypting email.
Until now, we've been depending on the
CA.pl script to create
and manage our certificates. Keep in mind that it is only a front end to the
OpenSSL certificate programs. You can achieve better control by manipulating
the configuration file openssl.cnf, and even the
CA.pl script itself.
Some of the possibilities are:
Making and converting certificates between DSA, RSA, and, to some extent, PGP.
Changing the certificate lifespan from the default configuration of one year. For example, look at the two ways of creating the certificate request:
$ CA.pl -newreq $ openssl req -config /etc/openssl.cnf -new -keyout newreq.pem \ -out newreq.pem -days 365
These both perform the same action, but by using
directly, you can change the certificate name, where it will be saved, the
configuration file to be used, and how long the certificate will remain
Creating new default certificate variables such as company name, section names, and departments.
$ openssl x509 -in newcert.pem -noout -text
$ openssl -revoke newcert.pem
$ openssl ca -config /etc/openssl.cnf -policy policy_anything \ -out newcert.pem -infiles newreq.pem \ -startdate [now] -enddate [previous enddate+365days]
Once you've created a certificate, using it is a piece of cake. All
examples below are command-line-based. Why should we concern ourselves with the
command line? Because GUIs break and can be resource-intensive, you might be
controlling your machine remotely, or you may need to carry out a set of
actions using a script. All of the examples below assume that you are in the
directory in which you originally invoked
You will be asked to enter your PEM pass phrase.
$ openssl smime \ -in myMessage.txt \ -sign \ -signer newcert.pem \ -inkey newreq.pem \ -out mySignedMessage.txt
It is important to add the
text switch, as this option adds
plain text (
text/plain) MIME headers to the supplied message when
encrypting or signing.
$ openssl smime \ -in myMessage.txt \ -text \ -sign \ -signer newcert.pem \ -inkey newreq.pem \ -to firstname.lastname@example.org \ -from email@example.com \ -subject "visit next week" \ | /usr/sbin/sendmail firstname.lastname@example.org
$ openssl smime \ -encrypt \ -text \ -in myMessage.txt \ -out myMessage.ecrypted.txt \ newcert.pem
Your private key, newcert.pem, is used to encrypt the email.
$ openssl smime \ -encrypt \ -text \ -in myMessage.txt \ -from email@example.com \ -to firstname.lastname@example.org \ -subject "Encrypted message" \ newcert.pem \ | /usr/sbin/sendmail email@example.com
Running this command with the
-out switch will save it to the
named file, and the
-signer switch saves the certificate to a named
file upon successful verification.
$ openssl smime \ -verify \ -in mySignedMessage.txt \ -signer friend.cert.pem \ -CAfile myRoot.crt \ -out message.verified.txt
The message is checked against two certificates: newcert.pem, the certificate from the sender, and newcert.pem, the CA's certificate. The code below will send the decrypted message to standard output.
openssl smime \ -decrypt \ -in myMessage.ecrypted.txt \ -recip newcert.pem \ -inkey newreq.pem
The easiest way to get signed and/or encrypted messages is to use the old
pine, the message will end up as garbage.
On the other hand, unless you're a
mutt. You can then use OpenSSL in a manner
similar to the above.
There are several ongoing projects for console-based email clients that
incorporate the OpenSSL libraries directly.
mutt is one of them.
Mozilla is a GUI option — what can I say, it's just a click away!
The pros of OpenSSL are many:
There are cons, however:
If you want to save yourself a bit of grief in Mozilla as you experiment, use the profile manager to create two separate accounts. Generate one root and two client certificates. Be advised that you will have problems if you attach a certificate with an email address that is different from your email configuration.
Pretty Good Privacy (PGP) is a pretty good solution. Among other clients, there
is support for
Eudora, and there is a plug-in that will allow it to work with Mozilla, too.
Robert Bernier is the PostgreSQL business intelligence analyst for SRA America, a subsidiary of Software Research America (SRA).
Return to the Linux DevCenter.
Copyright © 2009 O'Reilly Media, Inc.