Add SWIG post.
[blog.git] / posts / X.509_certificates.mdwn
1 If you're running your own server, your probably not shelling out $400
2 to get an "official" Certificate Authority to sign your key.  Here's a
3 quick not to myself about how to create and sign your own key.
4 Depending on your application, you can use either the [[GnuTLS]] or
5 [OpenSSL][] toolchain.
6
7 GnuTLS
8 ------
9
10 Following the [GnuTLS manual][gnutls-manual], create a certificate
11 authority with [certtool][], adjusting the `cn` as you see fit:
12
13     $ certtool --generate-privkey > x509-ca-key.pem
14     $ echo 'cn = GnuTLS test CA' > ca.tmpl
15     $ echo 'ca' >> ca.tmpl
16     $ echo 'cert_signing_key' >> ca.tmpl
17     $ certtool --generate-self-signed --load-privkey x509-ca-key.pem \
18         --template ca.tmpl --outfile x509-ca.pem
19
20 Now generate the *unencrypted* server key.
21
22     $ certtool --generate-privkey > x509-server-key.pem
23
24 And sign the key with your CA, adjusting the `cn` as you see fit, and
25 changing `dns_name` to match your fully qualified host name.
26
27     $ echo 'organization = GnuTLS test server' > server.tmpl
28     $ echo 'cn = test.gnutls.org' >> server.tmpl
29     $ echo 'tls_www_server' >> server.tmpl
30     $ echo 'encryption_key' >> server.tmpl
31     $ echo 'signing_key' >> server.tmpl
32     $ echo 'dns_name = test.gnutls.org' >> server.tmpl
33     $ certtool --generate-certificate --load-privkey x509-server-key.pem \
34         --load-ca-certificate x509-ca.pem --load-ca-privkey x509-ca-key.pem \
35         --template server.tmpl --outfile x509-server.pem
36
37 You can also print certificates with [certtool][].
38
39     $ certtool --infile x509-server.pem --certificate-info
40
41 OpenSSL
42 -------
43
44 Use [openssl][]'s [genpkey][] to generate an *unencrypted* key.
45
46     $ openssl genpkey -algorithm RSA -out key.pem
47
48 An unencrypted key is less secure, but it allows the web server to be
49 restarted (e.g. after rebooting) without you being there to enter the
50 decryption key.  Make sure `key.pem` is only readable by `root`.
51
52 Use [req][] to generate certificate signing request.
53
54     $ openssl req -new -key key.pem -out req.pem
55
56 `-new` prompts you for new relevant field values.  You can also
57 specify the values on the command line or in an configuration file
58 (override the default with `-config filename`).
59
60 Use [x509][] to sign the certificate.
61
62     $ openssl x509 -req -days 365 -in req.pem -signkey key.pem -out cert.pem
63
64 You should keep your certificate signing request around so you can
65 re-sign your key later on (since your initial signature will
66 eventually expire).
67
68 You can also print certificates with [x509][].
69
70     $ openssl x509 -in cert.pem -noout -text
71
72 [gnutls-manual]: http://www.gnu.org/software/gnutls/manual/html_node/Invoking-gnutls_002dserv.html
73 [certtool]: http://www.gnu.org/software/gnutls/manual/html_node/Invoking-certtool.html#Invoking-certtool
74 [OpenSSL]: http://www.openssl.org/docs/apps/openssl.html
75 [genpkey]: http://www.openssl.org/docs/apps/genpkey.html
76 [req]: http://www.openssl.org/docs/apps/req.html
77 [x509]: http://www.openssl.org/docs/apps/x509.html
78
79 [[!tag tags/linux]]
80 [[!tag tags/tools]]