Last active
July 3, 2017 07:25
-
-
Save diyfr/05455de8dd7177ad93c6f078c6a7c689 to your computer and use it in GitHub Desktop.
Sécurisation d'un serveur apache avec des certificat utilisateurs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ============================================================================= | |
# Application : Génération de certificat utilisateur | |
# Fichier : domain.sh | |
# Description : Script de génération de certificat utilisateur | |
# Version : 1.0 | |
# ============================================================================= | |
# Historique : | |
# 29/06/2017 : 1.0 diyfr - creation | |
# ============================================================================= | |
# Utilisation | |
# ----------------------------------------------------------------------------- | |
# Personnalisez les variables dans properties.sh,et exécutez le script doamin.sh | |
# Syntaxe : ./client.sh [id Client] [Password] | |
# Param : {1} identifiant du client {2} mot de passe du certificat | |
# Log : aucun | |
# ============================================================================= | |
if [ ! -f ./properties.sh ] | |
then | |
echo "ERREUR sur la localisation du fichier properties.sh " | |
echo "Action : revoir le parametrage" | |
exit 1; | |
fi | |
. ./properties.sh | |
if [ -z "$1" ]; then | |
echo "ID Utilisateur Manquant" | |
exit 1; | |
fi | |
if [ -z "$2" ]; then | |
echo "Mot de passe Manquant" | |
exit 1; | |
fi | |
FILE_NAME="${ORGANISATION_UNIT}-${1}" | |
FILENAME_KEY="${FILE_NAME}.key" | |
FILENAME_CSR="${FILE_NAME}.csr" | |
FILENAME_CRT="${FILE_NAME}.crt" | |
FILENAME_P12="${FILE_NAME}.p12" | |
SUBJECT="/C=${COUNTRY}/O=${ORGANISATION}/OU=${ORGANISATION_UNIT}/CN=${FILE_NAME}" | |
if [ ! -f $CA_KEY ]; then | |
echo "ERREUR le CA n'a pas été correctement générer. relancer domain.sh " | |
exit 1; | |
fi | |
if [ ! -f $CA_CRT ]; then | |
echo "ERREUR le CA n'a pas été correctement générer. relancer domain.sh " | |
exit 1; | |
fi | |
echo KEY | |
openssl genrsa -des3 -out $FILENAME_KEY -passout pass:$2 4096 | |
echo CSR | |
openssl req -new -key $FILENAME_KEY -config $OPEN_SSL_CONF -extensions client -out $FILENAME_CSR -passin pass:$2 -subj "${SUBJECT}" | |
echo CRT | |
openssl x509 -req -days 730 -in $FILENAME_CSR -CA $CA_CRT -CAkey $CA_KEY -out $FILENAME_CRT -passin pass:$CA_PASSWORD -CAserial $CA_SERIAL | |
echo p12 | |
# Convert Client Key to PKCS | |
openssl pkcs12 -export -clcerts -in $FILENAME_CRT -inkey $FILENAME_KEY -out $FILENAME_P12 -name $ALIAS -passin pass:$2 -passout pass:$2 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ============================================================================= | |
# Application : Génération de chaine de confiance et certificat/conf serveur | |
# Fichier : domain.sh | |
# Description : Script de génération de la chaine de confiance, certificat et | |
# configuration du serveur apache | |
# Version : 1.0 | |
# ============================================================================= | |
# Historique : | |
# 29/06/2017 : 1.0 diyfr - creation | |
# ============================================================================= | |
# SEE https://www.vanimpe.eu/2015/07/03/client-side-certificate-authentication/ | |
# ============================================================================= | |
# Utilisation | |
# ----------------------------------------------------------------------------- | |
# Personnalisez les variables dans properties.sh | |
# Syntaxe : ./domain.sh | |
# Param : aucun | |
# Log : aucun | |
# ============================================================================= | |
if [ ! -f ./properties.sh ] | |
then | |
echo "ERREUR sur la localisation du fichier properties.sh " | |
echo "Action : revoir le parametrage" | |
exit 1; | |
fi | |
. ./properties.sh | |
COMMON_NAME="${ORGANISATION} ${ORGANISATION_UNIT}" | |
SUBJECT="/C=${COUNTRY}/O=${ORGANISATION}/OU=${ORGANISATION_UNIT}/CN=${COMMON_NAME}" | |
#MAKE CA CERT PATH | |
mkdir -p $CA_PATH | |
if [ ! -f $CA_KEY ]; then | |
openssl genrsa -des3 -out $CA_KEY -passout pass:$CA_PASSWORD 4096 | |
fi | |
if [ ! -f $CA_CRT ]; then | |
openssl req -new -x509 -days 3650 -config $OPEN_SSL_CONF -extensions certauth -key $CA_KEY -out $CA_CRT -passin pass:$CA_PASSWORD -subj "${SUBJECT}" | |
fi | |
if [ ! -f $CA_SRL ]; then | |
echo $SRL_START > $CA_SERIAL | |
fi | |
# check crt | |
#openssl x509 -in $CA_CRT -text -noout | |
#MAKE CA REVOCATION PATH | |
mkdir -p $CA_REVOCATION_PATH | |
#MAKE SERVER CERT PATH | |
mkdir -p $SERVER_PATH | |
if [ ! -f $SERVER_KEY ]; then | |
openssl genrsa -out $SERVER_KEY 4096 | |
fi | |
SUBJECT="/C=${COUNTRY}/O=${ORGANISATION}/OU=${ORGANISATION_UNIT}/CN=${SERVER_NAME}" | |
if [ ! -f $SERVER_CRT ]; then | |
openssl req -new -key $SERVER_KEY -out $SERVER_CSR -config $OPEN_SSL_CONF -extensions server -subj "${SUBJECT}" | |
openssl x509 -req -days 730 -in $SERVER_CSR -CA $CA_CRT -CAkey $CA_KEY -out $SERVER_CRT -CAserial $CA_SERIAL -passin pass:$CA_PASSWORD | |
fi | |
rm $APACHE_FILE | |
#create apache configuration | |
echo "ServerAdmin webmaster@localhost" >> $APACHE_FILE | |
echo "#https://mozilla.github.io/server-side-tls/ssl-config-generator/" >> $APACHE_FILE | |
echo "DocumentRoot /var/www/html ">> $APACHE_FILE | |
echo "ErrorLog \${APACHE_LOG_DIR}/uc_error.log ">> $APACHE_FILE | |
echo "CustomLog \${APACHE_LOG_DIR}/uc_access.log combined ">> $APACHE_FILE | |
echo "ServerName ${SERVER_NAME}">> $APACHE_FILE | |
echo "SSLEngine on" >> $APACHE_FILE | |
echo "SSLCertificateFile ${PWD}/${SERVER_CRT}" >> $APACHE_FILE | |
echo "SSLCertificateKeyFile ${PWD}/${SERVER_KEY}">> $APACHE_FILE | |
echo "SSLCACertificateFile ${PWD}/${CA_CRT}">> $APACHE_FILE | |
echo "<Location /usercert>">> $APACHE_FILE | |
echo " SSLRequireSSL" >> $APACHE_FILE | |
echo " SSLVerifyClient require" >> $APACHE_FILE | |
echo " SSLVerifyDepth 10" >> $APACHE_FILE | |
echo " SSLRequire %{SSL_CLIENT_S_DN_O} eq \"$ORGANISATION\" and %{SSL_CLIENT_S_DN_OU} eq \"$ORGANISATION_UNIT\" ">> $APACHE_FILE | |
echo "</Location> ">> $APACHE_FILE | |
echo "============================================================================================" | |
echo " CONFIGURATION APACHE" | |
echo "============================================================================================" | |
echo "Dans un VirtualHost ajoutez l'inclusion du fichier de configuration ${APACHE_FILE} exemple:" | |
echo "<IfModule mod_ssl.c>" | |
echo " <VirtualHost _default_:443>" | |
echo " DocumentRoot /var/www/html" | |
echo " Include ${PWD}/${APACHE_FILE}" | |
echo " ErrorLog ${APACHE_LOG_DIR}/uc_error.log" | |
echo " CustomLog ${APACHE_LOG_DIR}/uc_access.log combined" | |
echo " </VirtualHost>" | |
echo "</IfModule>" | |
echo "============================================================================================" | |
echo "Pensez à personnaliser la valeur Location (usercert par défaut) dans le fichier ${APACHE_FILE}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ req ] | |
default_bits = 4096 | |
distinguished_name = req_distinguished_name | |
[ req_distinguished_name ] | |
[ certauth ] | |
subjectKeyIdentifier = hash | |
authorityKeyIdentifier = keyid:always,issuer:always | |
basicConstraints = CA:true | |
[ server ] | |
basicConstraints = critical,CA:FALSE | |
[ client ] | |
basicConstraints = critical,CA:FALSE | |
keyUsage = digitalSignature, keyEncipherment, dataEncipherment | |
extendedKeyUsage = clientAuth |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ============================================================================= | |
# Application : Génération de chaine de certificat utilisateur | |
# Fichier : properties.sh | |
# Description : Ensemble de variables utilisées dans les scripts | |
# Version : 1.0 | |
# ============================================================================= | |
# Historique : | |
# 29/06/2017 : 1.0 diyfr - creation | |
# ============================================================================= | |
# ============================================================================= | |
# Utilisation | |
# ----------------------------------------------------------------------------- | |
# Personnalisez les variables ci-dessous en fonction de votre usage | |
# Syntaxe : ./domain.sh pour générer le CA autosignée, le certificat | |
# serveur ainsi que l'exemple de conf pour apache | |
# Syntaxe : ./client.sh pour générer le Certificat client | |
# Param Sortie : aucun | |
# Log : aucun | |
# ============================================================================= | |
# ============================================================================= | |
# Ressources | |
# ----------------------------------------------------------------------------- | |
# https://www.vanimpe.eu/2015/07/03/client-side-certificate-authentication/ | |
# https://www.tbs-certificats.com/FAQ/fr/214.html | |
# ============================================================================= | |
#PATH | |
CA_PATH="ca" | |
CA_BASENAME="${CA_PATH}/ca" | |
CA_KEY="${CA_BASENAME}.key" | |
CA_CRT="${CA_BASENAME}.crt" | |
CA_SERIAL="${CA_BASENAME}_serial.srl" | |
CA_REVOCATION_PATH="ssl.crl" | |
SERVER_PATH="server" | |
SERVER_BASENAME="${SERVER_PATH}/server" | |
SERVER_KEY="${SERVER_BASENAME}.key" | |
SERVER_CSR="${SERVER_BASENAME}.csr" | |
SERVER_CRT="${SERVER_BASENAME}.crt" | |
# SERIAL 16 o | |
SRL_START=8A0000000000000A | |
OPEN_SSL_CONF="open_ssl.conf" | |
#DETAIL ORGANISATION A PERSONNALISER | |
CA_PASSWORD="TopSecret" | |
COUNTRY="FR" | |
ORGANISATION="MonOrga" | |
ORGANISATION_UNIT="Test" | |
ALIAS="apache" | |
#SERVER INFORMATIONS | |
SERVER_NAME="mon.domaine.net" | |
APACHE_FILE="apache.include.conf" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment