martes, 12 de octubre de 2010

Uso del módulo SSL en Apache HTTP Server

Actualmente es considerado normal el realizar operaciones a través de internet en las que se intercambian datos de todo tipo, sin embargo, el protocolo http por si mismo no ofrece ningún tipo de blindaje ya que es posible obtener la información simplemente monitoreando las comunicaciones, por lo que es indispensable proteger los sitios en los que se ingresan datos mediante el uso del protocolo https, el cual, utiliza TLS/SSL para encriptar la comunicación, además de que se recurre a un certificado emitido por un CA para evitar el robo de identidad.

Para esta práctica requerimos un servidor Apache 2 con el módulo SSL activo, previamente vimos cómo instalarlo.

Lo primero que vamos a hacer es crear un certificado para pruebas empleando el OpenSSL, en un directorio vacío creamos los subdirectorios demoCA, demoCA/newcerts y demoCA/private:

mkdir demoCA
mkdir demoCA/newcerts
mkdir demoCA/private

También creamos el archivo vacío demoCA/index.txt y el archivo demoCA/serial con la cadena “01”:

touch demoCA/index.txt
echo 01 > demoCA/serial

Ahora creamos un certificado x509 autofirmado:

openssl genrsa -out demoCA/private/cakey.pem 1024
Generating RSA private key, 1024 bit long modulus
.........................++++++
...................++++++
e is 65537 (0x10001)
openssl req -new -key demoCA/private/cakey.pem -out demoCA/cacert.pem -x509
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:MX
State or Province Name (full name) [Some-State]:Queretaro
Locality Name (eg, city) []:Queretaro
Organization Name (eg, company) [Internet Widgits Pty Ltd]:unixymas
Organizational Unit Name (eg, section) []:unixymas
Common Name (eg, YOUR name) []:unixymas
Email Address []:webmaster@unixymas.com.mx

Ahora estamos listos para firmar una solicitud, vamos a crear una:

openssl genrsa -out key.pem 1024
Generating RSA private key, 1024 bit long modulus
.........++++++
..........................................++++++
e is 65537 (0x10001)
openssl req -new -key key.pem -out req.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:MX
State or Province Name (full name) [Some-State]:Queretaro
Locality Name (eg, city) []:Queretaro
Organization Name (eg, company) [Internet Widgits Pty Ltd]:unixymas
Organizational Unit Name (eg, section) []:unixymas
Common Name (eg, YOUR name) []:ubuntu.unixymas.com.mx
Email Address []:webmaster@unixymas.com.mx

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

Observen que en “common name” escribí el nombre del sitio. Tal cual, esta solicitud podemos enviarla a un CA para adquirir un certificado, pero en este ejemplo vamos a firmarlo nosotros mismos:

openssl ca -in req.pem -key key.pem -out cert.pem
Using configuration from /usr/lib/ssl/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Oct 13 02:39:21 2010 GMT
            Not After : Oct 13 02:39:21 2011 GMT
        Subject:
            countryName               = MX
            stateOrProvinceName       = Queretaro
            organizationName          = unixymas
            organizationalUnitName    = unixymas
            commonName                = ubuntu.unixymas.com.mx
            emailAddress              = webmaster@unixymas.com.mx
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                8F:E6:90:EE:92:EA:C4:B7:FA:5D:EF:AC:ED:26:5D:6E:C9:BD:81:FE
            X509v3 Authority Key Identifier:
                keyid:DD:63:E9:44:86:7B:7B:E3:85:43:31:C2:6C:4D:A7:B0:7B:5C:4D:C9

Certificate is to be certified until Oct 13 02:39:21 2011 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

Listo, ahora vamos a instalar este certificado en nuestro servidor, nos pasamos al directorio conf dentro del Apache, en mi caso, /usr/local/apache2/conf, y editamos el archivo httpd.conf, localizamos la línea #Include conf/extra/httpd-ssl.conf y le retiramos el signo de número, lo salvamos y copiamos el archivo certificado a server.crt y la llave a server.key:

cp cert.pem /usr/local/apache2/conf/server.crt
cp key.pem /usr/local/apache2/conf/server.key

Es muy importante que el archivo server.key tenga solo los atributos 600:

chmod 600 /usr/local/apache2/conf/server.key

Y listo, podemos probar desde un browser con la url: https://sitio

image

Es lógico que al principio presente errores por la falta de un certificado firmado por un CA reconocido, simplemente agregamos la excepción y listo.

image

No hay comentarios.: