| 1 |
== link:index.html[Index] -> link:cookbook.html[Cookbook] |
|---|
| 2 |
|
|---|
| 3 |
Cookbook: SSL, TLS and certificates |
|---|
| 4 |
----------------------------------- |
|---|
| 5 |
|
|---|
| 6 |
This section answers some general questions and details the procedure |
|---|
| 7 |
to generate SSL keys. |
|---|
| 8 |
|
|---|
| 9 |
[[general]] |
|---|
| 10 |
General questions |
|---|
| 11 |
~~~~~~~~~~~~~~~~~ |
|---|
| 12 |
|
|---|
| 13 |
.Can the same server provide HTTP and HTTPS? |
|---|
| 14 |
|
|---|
| 15 |
Yes. HTTP and HTTPS use different server ports. The former binds to |
|---|
| 16 |
port 80 and the latter to port 443, so there is no conflict between |
|---|
| 17 |
them. To provide HTTPS you will need matching certificates. |
|---|
| 18 |
|
|---|
| 19 |
Keep in mind that to have one of your virtual servers with HTTPS |
|---|
| 20 |
enabled you will need to configure HTTPS settings for all of them. |
|---|
| 21 |
|
|---|
| 22 |
.Which port does HTTPS use? |
|---|
| 23 |
|
|---|
| 24 |
HTTPS can run on any port, but the standards specify port 443. That's |
|---|
| 25 |
where any HTTPS compliant browser will look by default. You can change |
|---|
| 26 |
that by specifying another port in the URL. For example, |
|---|
| 27 |
https://example.com:8080/ would look for an HTTPS server on port 8080. |
|---|
| 28 |
|
|---|
| 29 |
.How to manually test HTTP/HTTPS? |
|---|
| 30 |
|
|---|
| 31 |
HTTP can easily be tested like this: |
|---|
| 32 |
---- |
|---|
| 33 |
$ telnet localhost 80 |
|---|
| 34 |
GET / HTTP/1.0 |
|---|
| 35 |
---- |
|---|
| 36 |
|
|---|
| 37 |
For HTTPS it is not so easy because of the SSL protocol between TCP |
|---|
| 38 |
and HTTP. However you can do a similar check with the help of |
|---|
| 39 |
OpenSSL's s_client command. |
|---|
| 40 |
|
|---|
| 41 |
---- |
|---|
| 42 |
$ openssl s_client -connect localhost:443 -state -debug |
|---|
| 43 |
GET / HTTP/1.0 |
|---|
| 44 |
---- |
|---|
| 45 |
|
|---|
| 46 |
You will receive detailed information about the SSL handshake before |
|---|
| 47 |
the actual HTTP response. |
|---|
| 48 |
|
|---|
| 49 |
A more general command line client is probably a better choice. |
|---|
| 50 |
link:http://curl.haxx.se/[cURL] deals with both HTTP and HTTPS, and |
|---|
| 51 |
performs a bunch of other interesting stuff. |
|---|
| 52 |
|
|---|
| 53 |
---- |
|---|
| 54 |
$ curl http://localhost/ |
|---|
| 55 |
$ curl https://localhost/ |
|---|
| 56 |
---- |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
[[overview]] |
|---|
| 60 |
Private keys and Certificates overview |
|---|
| 61 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 62 |
First some terminology: |
|---|
| 63 |
|
|---|
| 64 |
. RSA private key file: a digital file that can be used to decrypt |
|---|
| 65 |
messages sent to you. It has a public component that must be |
|---|
| 66 |
distributed (via your Certificate file) to allow people to encrypt |
|---|
| 67 |
those messages. |
|---|
| 68 |
|
|---|
| 69 |
. CSR, or Certificate Signing Request: a digital file containing your |
|---|
| 70 |
public key and your name. It is sent to a Certifying Authority (CA) |
|---|
| 71 |
that will convert sign it to conert it into a real Certificate. |
|---|
| 72 |
|
|---|
| 73 |
. Certificate: contains your RSA public key and name, the name of the |
|---|
| 74 |
CA, and is digitally signed by the CA. A browser that knows the CA |
|---|
| 75 |
can verify the signature and obtain your RSA public key, which can |
|---|
| 76 |
be used to send messages which only you can decrypt. |
|---|
| 77 |
|
|---|
| 78 |
.Does the startup between a non-SSL and an SSL-aware web server differ? |
|---|
| 79 |
|
|---|
| 80 |
Yes. Although in essence it is exactly the same, if you have a |
|---|
| 81 |
passphrase on your SSL private key file, a startup dialog will asks |
|---|
| 82 |
you to enter it. This can be problematic if the web server needs to be |
|---|
| 83 |
started automatically. In this case, the link:#passphrase[passphrase] |
|---|
| 84 |
can be removed from your private key at the cost of erasing a security |
|---|
| 85 |
layer, wich brings additional security risks. |
|---|
| 86 |
|
|---|
| 87 |
.Does Cherokee offer any help for Certificate Generation? |
|---|
| 88 |
|
|---|
| 89 |
Yes. A script is provided to assit you with Certificate Generation. |
|---|
| 90 |
|
|---|
| 91 |
Just locate the `contrib` subdirectory and type: |
|---|
| 92 |
|
|---|
| 93 |
---- |
|---|
| 94 |
make-cert.sh |
|---|
| 95 |
---- |
|---|
| 96 |
|
|---|
| 97 |
And follow the instructions. It will generate the required files, but |
|---|
| 98 |
you will have to install them manually. |
|---|
| 99 |
|
|---|
| 100 |
It has been tested and has worked fine every time, but if you don't find |
|---|
| 101 |
the script or it doesn't work for you can always follow the rest of |
|---|
| 102 |
the procedure described in this recipe to manually generate the |
|---|
| 103 |
certificates. |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
[[locate]] |
|---|
| 107 |
Locate CA.pl or CA.sh |
|---|
| 108 |
~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 109 |
|
|---|
| 110 |
* On Debian or Ubuntu those are usually located under /usr/lib/ssl/misc/ |
|---|
| 111 |
* On MacOS X, you will find them in /System/Library/OpenSSL/misc/ |
|---|
| 112 |
|
|---|
| 113 |
In any other case ``find / -iname CA.pl -print`` will help you to locate it. |
|---|
| 114 |
|
|---|
| 115 |
[[create]] |
|---|
| 116 |
Create a new CA |
|---|
| 117 |
~~~~~~~~~~~~~~~ |
|---|
| 118 |
---- |
|---|
| 119 |
$ /usr/lib/ssl/misc/CA.pl -newca |
|---|
| 120 |
CA certificate filename (or enter to create) <press enter> |
|---|
| 121 |
Making CA certificate ... |
|---|
| 122 |
Generating a 1024 bit RSA private key |
|---|
| 123 |
.............++++++ |
|---|
| 124 |
.......................................++++++ |
|---|
| 125 |
writing new private key to './demoCA/private/cakey.pem' |
|---|
| 126 |
Enter PEM pass phrase: <type the secret phrase again> |
|---|
| 127 |
Verifying - Enter PEM pass phrase: <type the secret phrase again> |
|---|
| 128 |
----- |
|---|
| 129 |
You are about to be asked to enter information that will be incorporated |
|---|
| 130 |
into your certificate request. |
|---|
| 131 |
What you are about to enter is what is called a Distinguished Name or a DN. |
|---|
| 132 |
There are quite a few fields but you can leave some blank |
|---|
| 133 |
For some fields there will be a default value, |
|---|
| 134 |
If you enter '.', the field will be left blank. |
|---|
| 135 |
----- |
|---|
| 136 |
Country Name (2 letter code) [AU]:ES |
|---|
| 137 |
State or Province Name (full name) [Some-State]:. |
|---|
| 138 |
Locality Name (eg, city) []:. |
|---|
| 139 |
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Cherokee Team |
|---|
| 140 |
Organizational Unit Name (eg, section) []:<Enter> |
|---|
| 141 |
Common Name (eg, YOUR name) []:Cherokee Certificate Master |
|---|
| 142 |
Email Address []:alvaro@alobbs.com |
|---|
| 143 |
---- |
|---|
| 144 |
|
|---|
| 145 |
[[generate]] |
|---|
| 146 |
Generate a certificate request |
|---|
| 147 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 148 |
---- |
|---|
| 149 |
$ /usr/lib/ssl/misc/CA.pl -newreq |
|---|
| 150 |
Generating a 1024 bit RSA private key |
|---|
| 151 |
.....................................++++++ |
|---|
| 152 |
...++++++ |
|---|
| 153 |
writing new private key to 'newreq.pem' |
|---|
| 154 |
Enter PEM pass phrase: <anothe phrase> |
|---|
| 155 |
Verifying - Enter PEM pass phrase: <repeat it> |
|---|
| 156 |
----- |
|---|
| 157 |
You are about to be asked to enter information that will be incorporated |
|---|
| 158 |
into your certificate request. |
|---|
| 159 |
What you are about to enter is what is called a Distinguished Name or a DN. |
|---|
| 160 |
There are quite a few fields but you can leave some blank |
|---|
| 161 |
For some fields there will be a default value, |
|---|
| 162 |
If you enter '.', the field will be left blank. |
|---|
| 163 |
----- |
|---|
| 164 |
Country Name (2 letter code) [AU]:ES |
|---|
| 165 |
State or Province Name (full name) [Some-State]:. |
|---|
| 166 |
Locality Name (eg, city) []:. |
|---|
| 167 |
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Cherokee web server |
|---|
| 168 |
Organizational Unit Name (eg, section) []:. |
|---|
| 169 |
Common Name (eg, YOUR name) []:www.cherokee-project.com |
|---|
| 170 |
Email Address []:sysop@cherokee-project.com |
|---|
| 171 |
|
|---|
| 172 |
Please enter the following 'extra' attributes |
|---|
| 173 |
to be sent with your certificate request |
|---|
| 174 |
A challenge password []: <Enter> |
|---|
| 175 |
An optional company name []: <Enter> |
|---|
| 176 |
Request (and private key) is in newreq.pem |
|---|
| 177 |
---- |
|---|
| 178 |
|
|---|
| 179 |
[[sign]] |
|---|
| 180 |
Sign the certificate request |
|---|
| 181 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 182 |
---- |
|---|
| 183 |
$ /usr/lib/ssl/misc/CA.pl -sign: |
|---|
| 184 |
Using configuration from /usr/lib/ssl/openssl.cnf |
|---|
| 185 |
Enter pass phrase for ./demoCA/private/cakey.pem: |
|---|
| 186 |
Check that the request matches the signature |
|---|
| 187 |
Signature ok |
|---|
| 188 |
Certificate Details: |
|---|
| 189 |
Serial Number: 1 (0x1) |
|---|
| 190 |
Validity: |
|---|
| 191 |
Not Before: Aug 17 13:12:44 2003 GMT |
|---|
| 192 |
Not After : Aug 16 13:12:44 2004 GMT |
|---|
| 193 |
Subject: |
|---|
| 194 |
countryName = ES |
|---|
| 195 |
organizationName = Cherokee web server |
|---|
| 196 |
commonName = www.cherokee-project.com |
|---|
| 197 |
emailAddress = sysop@cherokee-project.com |
|---|
| 198 |
X509v3 extensions: |
|---|
| 199 |
X509v3 Basic Constraints: |
|---|
| 200 |
CA:FALSE |
|---|
| 201 |
Netscape Comment: |
|---|
| 202 |
OpenSSL Generated Certificate |
|---|
| 203 |
X509v3 Subject Key Identifier: |
|---|
| 204 |
14:6A:45:66:A2:EB:73:74:5A:C5:68:80:50:D5:48:94:DD:ED:25:F7 |
|---|
| 205 |
X509v3 Authority Key Identifier: |
|---|
| 206 |
keyid:9E:E0:E2:6E:1B:02:17:F2:72:C9:0D:E3:DA:C9:E1:8F:CE:BC:6E:A2 |
|---|
| 207 |
DirName:/C=ES/ST=Madrid/L=Madrid/O=Cherokee Team/CN=Cherokee Certificate Master/emailAddress=alvaro@alobbs.com |
|---|
| 208 |
serial:00 |
|---|
| 209 |
|
|---|
| 210 |
Certificate is to be certified until Aug 16 13:12:44 2004 GMT (365 days) |
|---|
| 211 |
Sign the certificate? [y/n]:y |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
1 out of 1 certificate requests certified, commit? [y/n]y |
|---|
| 215 |
Write out database with 1 new entries |
|---|
| 216 |
Data Base Updated |
|---|
| 217 |
Signed certificate is in newcert.pem |
|---|
| 218 |
---- |
|---|
| 219 |
|
|---|
| 220 |
[[self_sign]] |
|---|
| 221 |
Self signed certificates |
|---|
| 222 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 223 |
|
|---|
| 224 |
It is another way to generate certificate files. Ramon Pons sent this |
|---|
| 225 |
little script to create self signed certificates:: |
|---|
| 226 |
|
|---|
| 227 |
---- |
|---|
| 228 |
#!/bin/sh |
|---|
| 229 |
CERTNAME=cherokee.pem |
|---|
| 230 |
openssl req -days 1000 -new -x509 -nodes -out $CERTNAME -keyout $CERTNAME |
|---|
| 231 |
chmod 600 $CERTNAME |
|---|
| 232 |
openssl verify $CERTNAME |
|---|
| 233 |
if [ $? != 0 ]; then |
|---|
| 234 |
\mv $CERTNAME $CERTNAME.not_valid |
|---|
| 235 |
fi |
|---|
| 236 |
---- |
|---|
| 237 |
|
|---|
| 238 |
You can see that, in essence, it issues the folowing command: |
|---|
| 239 |
---- |
|---|
| 240 |
$ openssl req -new -x509 -nodes -out server.crt -keyout server.key |
|---|
| 241 |
---- |
|---|
| 242 |
|
|---|
| 243 |
Which would produce a couple of files: the SSL Certificate File |
|---|
| 244 |
(server.crt) and the SSL Certificate key file (server.key). |
|---|
| 245 |
|
|---|
| 246 |
This server.key does not have any passphrase. To add a passphrase to |
|---|
| 247 |
the key, you should run the following command, and enter & verify the |
|---|
| 248 |
passphrase as requested. |
|---|
| 249 |
|
|---|
| 250 |
---- |
|---|
| 251 |
$ openssl rsa -des3 -in server.key -out server.key.new |
|---|
| 252 |
$ mv server.key.new server.key |
|---|
| 253 |
---- |
|---|
| 254 |
|
|---|
| 255 |
You should probably backup the key file and the entered passphrase in |
|---|
| 256 |
a secure location. |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
[[passphrase]] |
|---|
| 260 |
Pass-phrase issues |
|---|
| 261 |
~~~~~~~~~~~~~~~~~~ |
|---|
| 262 |
|
|---|
| 263 |
As noted above, if you have a pass-phrase on your SSL private key file, |
|---|
| 264 |
the web-server start up will remain on hold until you enter it. Here |
|---|
| 265 |
is the information needed to changeit or even removing it, but bare in |
|---|
| 266 |
minda the security implications. |
|---|
| 267 |
|
|---|
| 268 |
.How to change the pass-phrase on a private key file? |
|---|
| 269 |
|
|---|
| 270 |
Simply read it with the old pass-phrase and write it again, specifying |
|---|
| 271 |
a new pass-phrase. This can be done withe these commands: |
|---|
| 272 |
|
|---|
| 273 |
---- |
|---|
| 274 |
$ openssl rsa -des3 -in server.key -out server.key.new |
|---|
| 275 |
$ mv server.key.new server.key |
|---|
| 276 |
---- |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
.How to get rid of the pass-phrase? |
|---|
| 280 |
|
|---|
| 281 |
The RSA private key inside the server.key file is stored in encrypted |
|---|
| 282 |
format for security reasons. The pass-phrase is needed to decrypt this |
|---|
| 283 |
file, so it can be read and parsed. Thus, removing it removes a layer |
|---|
| 284 |
of security from the web server. It is advised to keep a backup copy |
|---|
| 285 |
of the original file before proceeding. |
|---|
| 286 |
|
|---|
| 287 |
---- |
|---|
| 288 |
$ cp server.key server.key.org |
|---|
| 289 |
$ openssl rsa -in server.key.org -out server.key |
|---|
| 290 |
$ chmod 400 server.key |
|---|
| 291 |
---- |
|---|
| 292 |
|
|---|
| 293 |
Since the server.key now contains an unencrypted copy of the key, if |
|---|
| 294 |
anyone gets it they will be able to impersonate you on the net. |
|---|
| 295 |
|
|---|
| 296 |
[[verification]] |
|---|
| 297 |
Verifying that a private key matches its Certificate |
|---|
| 298 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 299 |
|
|---|
| 300 |
To view the Certificate and the key run the commands: |
|---|
| 301 |
---- |
|---|
| 302 |
$ openssl x509 -noout -text -in server.crt |
|---|
| 303 |
$ openssl rsa -noout -text -in server.key |
|---|
| 304 |
---- |
|---|
| 305 |
|
|---|
| 306 |
The `modulus` and the `public exponent` portions in the key and the |
|---|
| 307 |
Certificate must match. It is difficult to visually check that the |
|---|
| 308 |
long modulus numbers are the same, so this approach can be used |
|---|
| 309 |
instead to obtain the numbers to compare (though it is mathematically |
|---|
| 310 |
less rigorous). |
|---|
| 311 |
|
|---|
| 312 |
---- |
|---|
| 313 |
$ openssl x509 -noout -modulus -in server.crt | openssl md5 |
|---|
| 314 |
$ openssl rsa -noout -modulus -in server.key | openssl md5 |
|---|
| 315 |
---- |
|---|
| 316 |
|
|---|
| 317 |
To check to which key or certificate a particular CSR belongs you can |
|---|
| 318 |
perform the same calculation on the CSR as follows: |
|---|
| 319 |
|
|---|
| 320 |
---- |
|---|
| 321 |
$ openssl req -noout -modulus -in server.csr | openssl md5 |
|---|
| 322 |
---- |
|---|