Hack 45 Create Your Own Certificate AuthoritySign your own certificates to use in securing your network. SSL certificates are usually thought of as being used for secure communications over the HTTP protocol. However, they are also useful in providing both a means for authentication and a means for initiating key exchange for a myriad of other services where encryption is desired, such as POP and IMAP [Hack #47], SMTP [Hack #48], IPSec (see Chapter 6), and, of course, SSL tunnels [Hack #76] . To make the best use of SSL, you will need to properly manage your own certificates. If an SSL client needs to verify the authenticity of an SSL server, the cert used by the server needs to be signed by a Certificate Authority (CA) that is already trusted by the client. Well-known Certificate Authorities (such as Thawte and VeriSign) exist to serve as an authoritative, trusted third party for authentication. They are in the business of signing SSL certificates that are used on sites dealing with sensitive information (such as account numbers or passwords). If a site's SSL certificate is signed by a trusted authority, then presumably it is possible to verify the identity of a server supplying that cert's credentials. However, for anything other than e-commerce applications, a self-signed certificate is usually sufficient for gaining all of the security advantages that SSL provides. But even a self-signed cert must be signed by an authority that the client recognizes. OpenSSL, a free SSL implementation, is perfectly capable of generating everything you need to run your own Certificate Authority. The CA.pl utility makes the process very simple. In these examples, you'll need to type anything in boldface, and enter passwords wherever appropriate (they don't echo to the screen). To establish your new Certificate Authority, first change to the misc/ directory under wherever OpenSSL is installed (/System/Library/OpenSSL/ on OpenBSD; /usr/ssl/ or /usr/local/ssl/ on most Linux systems). Then use these commands: $./CA.pl -newca Note that you don't necessarily need root permissions, but you will need write permissions on the current directory. Congratulations! You're the proud owner of your very own Certificate Authority. Take a look around: $ ls -l demoCA/ The public key for your new Certificate Authority is contained in cacert.pem, and the private key is in private/cakey.pem. You can now use this private key to sign other SSL certs. By default, CA.pl will create keys that are good for only one year. To change this behavior, edit CA.pl and change the line that reads: $DAYS="-days 365"; Alternatively, you can forego CA.pl altogether and generate the public and private keys manually with a command like this: $ openssl req -new -x509 -keyout cakey.pem -out \ This will create a key pair that is good for the next 10 years, which can of course be changed by using a different argument to the -days switch. Additionally, you should change the private key's permissions to 600, to ensure that it is protected from being read by anyone. So far, we have only created the Certificate Authority. To actually create keys that you can use with your services, you need to create a certificate-signing request and a key. Again, this can be done easily with CA.pl. First, a certificate-signing request is created: $ ./CA.pl -newreq-nodes If you wish to encrypt the private key, you can use the -newreq switch in place of -newreq-nodes. However, if you encrypt the private key, you will have to enter the password for it each time the service that uses it is started. If you decide not to use an encrypted private key, be extremely cautious with your private key, as anyone who can obtain a copy of it can impersonate your server. Now, to actually sign the request and generate the signed certificate: $ ./CA.pl -sign Now you can set up keys in this manner for each server that needs to provide an SSL-encrypted service. It is easier to do this if you designate a single workstation to maintain the certificate authority and all the files associated with it. Don't forget to distribute your CA cert to programs that need to trust it [Hack #46] . |