« Security/sevices/SSLCertificate/Apache » : différence entre les versions
| Ligne 119 : | Ligne 119 : | ||
-out certs/${user}.crt -outdir certs -infiles certs/${user}.csr | -out certs/${user}.crt -outdir certs -infiles certs/${user}.csr | ||
cat certs/${user}.crt certs/${user}.key > ${user}.pem | cat certs/${user}.crt certs/${user}.key > pem/${user}.pem | ||
mv ssl2.cnf confs/${user}-ssl.cnf | mv ssl2.cnf confs/${user}-ssl.cnf | ||
Version du 23 décembre 2012 à 10:37
Préparation
Création du répertoire des certificats
mkdir /etc/pki/httpd && cd /etc/pki/httpd
Création du fichier de configuration de nos futurs certificats
On créer le fichier <path>/etc/pki/httpd/ssl.cnf</path>
HOME = . RANDFILE = .rand [ca] default_ca = ca_default [ca_default] dir = . certs = $dir/certs crl_dir = $dir/crl database = $dir/index.txt new_certs_dir = $dir/newcerts certificate = $dir/%s_ca_cert.pem private_key = $dir/private/%s_ca_key.pem serial = $dir/serial crl = $dir/crl.pem x509_extensions = usr_cert name_opt = ca_default cert_opt = ca_default default_days = 3650 default_crl_days = 30 default_md = md5 preserve = no policy = policy_match [policy_match] countryName = match stateOrProvinceName = match organizationName = match organizationalUnitName = optional commonName = supplied emailAddress = optional [req] default_bits = 1024 default_keyfile = privkey.pem distinguished_name = req_distinguished_name attributes = req_attributes x509_extensions = v3_ca # The extentions to add to the self signed cert string_mask = MASK:0x2002 [req_distinguished_name] countryName = Country Name (2 letter code) countryName_default = FR countryName_min = 2 countryName_max = 2 stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Paca localityName = Locality Name (eg, city) localityName_default = Cavaillon 0.organizationName = Organization Name (eg, company) 0.organizationName_default = B2PWeb organizationalUnitName = Organizational Unit Name (eg, section) commonName = Common Name (eg, your name or your server\'s hostname) commonName_max = 64 emailAddress = Email Address emailAddress_max = 64 [req_attributes] challengePassword = A challenge password challengePassword_min = 4 challengePassword_max = 20 unstructuredName = An optional company name [usr_cert] basicConstraints = CA:FALSE nsComment = "OpenSSL Generated Certificate" subjectKeyIdentifier = hash authorityKeyIdentifier = keyid,issuer:always [v3_ca] subjectKeyIdentifier = hash authorityKeyIdentifier = keyid:always,issuer:always basicConstraints = CA:true
Création des scripts de gestion de certificats
Par simplicité, on va se servir de ces scripts pour générer les certificats des utilisateurs.
Script de génération des certificats : <path>/etc/pki/httpd/generate-certificate.sh</path>
#!/bin/bash
#if you change your certificate authority name to something else you will need to change the caname value to reflect the change.
caname=httpd
# user is equal to parameter one or the first argument when you actually run the script
user=$1
if [[ "$user" == "" ]]
then
echo "Usage: $(basename $0) USER"
exit
fi
echo "Generate certificates for \"${user}\""
openssl genrsa -out certs/${user}.key 2048
cat ssl.cnf | sed 's/insert_hostname/'${user}'/'> ssl2.cnf
openssl req -config ssl2.cnf -new -nodes -out certs/${user}.csr -key certs/${user}.key
openssl ca -config ssl2.cnf -keyfile private/${caname}_ca_cert.key -cert ${caname}_ca_cert.crt \
-out certs/${user}.crt -outdir certs -infiles certs/${user}.csr
cat certs/${user}.crt certs/${user}.key > pem/${user}.pem
mv ssl2.cnf confs/${user}-ssl.cnf
Script de génération des certificats pour les navigateurs Web : <path>/etc/pki/httpd/generate-web-certificate.sh</path>
#!/bin/bash
#if you change your certificate authority name to something else you will need to change the caname value to reflect the change.
caname=httpd
# user is equal to parameter one or the first argument when you actually run the script
user=$1
if [[ "$user" == "" ]]
then
echo "Usage: $(basename $0) USER"
exit
fi
echo "Generate web certificates for \"${user}\""
openssl pkcs12 -export -inkey certs/${user}.key -in certs/${user}.crt -CAfile ${caname}_ca_cert.crt \
-out certs/${user}_browser_cert.p12
Script de revocation de certificats : <path>/etc/pki/httpd/revoke-certificate.sh</path>
#!/bin/bash
#if you change your certificate authority name to something else you will need to change the caname value to reflect the change.
caname=httpd
# user is equal to parameter one or the first argument when you actually run the script
user=$1
if [[ "$user" == "" ]]
then
echo "Usage: $(basename $0) USER"
exit
fi
echo "Revoke certificates for \"${user}\""
openssl -revoke certs/${user}.crt
openssl ca -gencrl -config ssl.cnf -out crl/sopac-ca.crl
Script de renouvellement de certificats : <path>/etc/pki/httpd/renew-certificate.sh</path>
L'utilisateur transmet son ancienne demande de certificat ou en régénère une nouvelle basée sur sa clef privée.
Il faut alors révoquer l'ancien certificat et signer la nouvelle demande de certificat.
L'ancien certificat se retrouve en cherchant dans le fichier index.txt le nom qualifié (DN) qui correspond à la requête. La procédure de révocation s'effectue ensuite avec le numéro de série <xx> et le fichier de certificat cert/<xx>.pem.
La signature de la nouvelle requête peut être manuelle pour s'assurer que les dates de validité du certificat seront correctes.
#!/bin/bash
#if you change your certificate authority name to something else you will need to change the caname value to reflect the change.
caname=httpd
# user is equal to parameter one or the first argument when you actually run the script
user=$1
if [[ "$user" == "" ]]
then
echo "Usage: $(basename $0) USER"
exit
fi
echo "Renew certificates for \"${user}\""
./revoke-certificate.sh ${user}
openssl ca -config confs/${user}-ssl.cnf -keyfile private/${caname}_ca_cert.key -cert ${caname}_ca_cert.crt \
-out certs/${user}.crt -outdir certs -infiles certs/${user}.csr
cat certs/${user}.crt certs/${user}.key > ${user}.pem
Génération de l'autorité de certification
Toujours depuis le répertoire /etc/pki/httpd, on initialise notre générateur et on génère l’autorité de certification. Celle-ci se compose d'une paire clé/certificat qui servira à signer tous les autres certificats.
Ici, le CN sera obligatoirement le nom d'hôte du serveur (FQDN), les autres renseignements ne sont pas bloquants pour la suite.
cd /etc/pki/httpd/
mkdir {certs,private,confs,crl}
touch index.txt
echo 01 > serial
openssl genrsa -out private/httpd_ca_cert.key 2048
openssl req -config ssl.cnf -new -x509 -days 3650 -key private/httpd_ca_cert.key \
-out httpd_ca_cert.crt -extensions v3_ca
Gérer les certificats des utilisateurs
Cette partie est la seule qui est exécuter plusieurs fois. Elle est très amplement facilitée par les scripts de gestion édités auparavant.
Générer un certificat
cd /etc/pki/httpd/
./generate-certificate.sh <USER>
./generate-web-certificate.sh <USER>
Révoquer un certificat utilisateur
cd /etc/pki/httpd/
./revoke-certificate.sh <USER>
Configuration d'Apache
Fichier <path>/etc/httpd/conf.d/ssl.conf</path>
LoadModule ssl_module modules/mod_ssl.so
Listen 443
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
SSLMutex default
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
<VirtualHost _default_:443>
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warn
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite RC4-SHA:AES128-SHA:ALL:!ADH:!EXP:!LOW:!MD5:!SSLV2:!NULL
SSLCertificateFile /etc/pki/httpd/certs/httpd.crt
SSLCertificateKeyFile /etc/pki/httpd/certs/httpd.key
SSLCertificateChainFile /etc/pki/httpd/httpd_ca_cert.crt
SSLCACertificateFile /etc/pki/httpd/httpd_ca_cert.crt
SSLVerifyClient require
SSLVerifyDepth 10
<Files ~ "\.(cgi|shtml|phtml|php3?)$">
SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>