Monday, November 8, 2010

Adding a New Trusted Certificate on Ubuntu

How to add a new trusted certificate to a Ubuntu system.


Problem

I was developing an application that communicated with a remote server over SSL. In production, the SSL certificate used by the remote server would be chained to a valid root certificate authority. However, for development I had to test against a server with a self-signed certificate which was causing a problem since I wanted SSL verification to be fully enabled -- It would have been no good for me to just disable certificate validation because when the code went to production I would lose a main purpose for using SSL, verifying the identity of the communicating server
This situation is very common. When deploying SSL to an internal server, many system administrators tend to use what is called a self-signed certificate. This is a certificate that is generated and signed by itself. This cost no money to generate, but  it won't  be trusted by clients because there's no chain of trust to follow. So, when I made a connection with this server, the underlying SSL implementation complained that I was not communicating with a verifiable source.

Solution

My test and production systems are running on the Ubuntu linux distribution. So I'm going to describe how to add a new trusted certificate to a Ubuntu system.  Once the target SSL certificate is trusted, connections from the system will no longer complain of being unverifiable and the code can be exactly as it will be production.

Getting the Public Certificate

First, you'll need the public portion of the self-signed certificate in PEM format. If you generated it yourself then this should be easy to find as its part of what you installed in your web server (NOTE: remember you want the PUBLIC part). If you didn't or don't know which is the public part, we can use the openssl library to retrieve it directly from the operating target server, the following is the necessary openssl command executed against google. In a self signed example, there'd be only 1 certificate in the certificat chain section:
$ openssl s_client -connect google.com:443 -showcerts
CONNECTED(00000003)depth=1 /C=ZA/O=Thawte Consulting (Pty) Ltd./CN=Thawte SGC CA
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com
   i:/C=ZA/O=Thawte Consulting (Pty) Ltd./CN=Thawte SGC CA
-----BEGIN CERTIFICATE-----
MIIDITCCAoqgAwIBAgIQL9+89q6RUm0PmqPfQDQ+mjANBgkqhkiG9w0BAQUFADBM
MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wOTEyMTgwMDAwMDBaFw0x
MTEyMTgyMzU5NTlaMGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
MRYwFAYDVQQHFA1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKFApHb29nbGUgSW5jMRcw
FQYDVQQDFA53d3cuZ29vZ2xlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC
gYEA6PmGD5D6htffvXImttdEAoN4c9kCKO+IRTn7EOh8rqk41XXGOOsKFQebg+jN
gtXj9xVoRaELGYW84u+E593y17iYwqG7tcFR39SDAqc9BkJb4SLD3muFXxzW2k6L
05vuuWciKh0R73mkszeK9P4Y/bz5RiNQl/Os/CRGK1w7t0UCAwEAAaOB5zCB5DAM
BgNVHRMBAf8EAjAAMDYGA1UdHwQvMC0wK6ApoCeGJWh0dHA6Ly9jcmwudGhhd3Rl
LmNvbS9UaGF3dGVTR0NDQS5jcmwwKAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUF
BwMCBglghkgBhvhCBAEwcgYIKwYBBQUHAQEEZjBkMCIGCCsGAQUFBzABhhZodHRw
Oi8vb2NzcC50aGF3dGUuY29tMD4GCCsGAQUFBzAChjJodHRwOi8vd3d3LnRoYXd0
ZS5jb20vcmVwb3NpdG9yeS9UaGF3dGVfU0dDX0NBLmNydDANBgkqhkiG9w0BAQUF
AAOBgQCfQ89bxFApsb/isJr/aiEdLRLDLE5a+RLizrmCUi3nHX4adpaQedEkUjh5
u2ONgJd8IyAPkU0Wueru9G2Jysa9zCRo1kNbzipYvzwY4OA8Ys+WAi0oR1A04Se6
z5nRUP8pJcA2NhUzUnC+MY+f6H/nEQyNv4SgQhqAibAxWEEHXw==
-----END CERTIFICATE-----
 1 s:/C=ZA/O=Thawte Consulting (Pty) Ltd./CN=Thawte SGC CA
   i:/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification Authority
-----BEGIN CERTIFICATE-----
MIIDIzCCAoygAwIBAgIEMAAAAjANBgkqhkiG9w0BAQUFADBfMQswCQYDVQQGE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/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com
issuer=/C=ZA/O=Thawte Consulting (Pty) Ltd./CN=Thawte SGC CA
---
No client certificate CA names sent
---
SSL handshake has read 1765 bytes and written 313 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-SHA
Server public key is 1024 bit
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1
    Cipher    : RC4-SHA
    Session-ID: C7EF786B1A1A4AE79B7A88EC0A1E1406A9119F024924F5239CCFB12313A51752
    Session-ID-ctx:
    Master-Key: 21DA278CEBBFCC7C916C97957F7F19A5D8C7E06F68315A4C032D9EEB4751BCE3CB411A7C414BFED95F13888DE175EE85
    Key-Arg   : None
    Start Time: 1289266030
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---
The bolded section included the "----BEGIN CERTIFICATE----" and "----END CERTIFICATE----" lines represents a PEM encoded public certificate. This is the content that you'll want to add to your trusted certificates on your system.

Adding the Certificate

On Ubuntu, you'll want to create a new file in /usr/local/share/ca-certificates named appropriately for the host certificate you're adding. The name can be anything, but it must end with a .crt extension for the next command step to work. For my example, google.com.crt would be appropriate. Now that you've created the file, you need to tell the system about it. This is done with the command:
$ sudo update-ca-certificates
Updating certificates in /etc/ssl/certs... 1 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....
updating keystore /etc/ssl/certs/java/cacerts...
done.
done.
The command will both, link the certificate appropriately into /etc/ssl/certs as well as updating the JRE cacerts files (if you have a JRE installed).

No comments:

Post a Comment