openssl:accueil

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
openssl:accueil [2022/01/11 18:44] – [OpenSSL] add link phswopenssl:accueil [2025/12/07 20:09] (Version actuelle) – [Gérer sa propre PKI] add how to use certbot with smallstep phsw
Ligne 3: Ligne 3:
   * [[https://net-security.fr/security/openssl-formats-cheat-sheet/]]   * [[https://net-security.fr/security/openssl-formats-cheat-sheet/]]
   * [[https://connect.ed-diamond.com/Linux-Pratique/lp-123/les-certificats-de-l-emission-a-la-revocation]]   * [[https://connect.ed-diamond.com/Linux-Pratique/lp-123/les-certificats-de-l-emission-a-la-revocation]]
 +  * [[https://blog.idriss-code.fr/gestion-pratique-des-certificats-avec-openssl/|Gestion pratique des certificats avec OpenSSL]]
 +
 +
 +==== Ajouter un certificat au système ====
 +
 +<code bash>
 +sudo apt install libnss3-tools
 +
 +sudo cp homelab.crt /usr/local/share/ca-certificates/
 +sudo update-ca-certificates
 +sudo chmod a+r /usr/local/share/ca-certificates/
 +</code>
 +
 +Pour que le certificat soit aussi reconnu dans Firefox et Thunderbird, il faut exécuter le script suivant ([[https://askubuntu.com/a/369858|source]]) :
 +
 +<code bash>
 +sudo apt install libnss3-tools  # pour certutil
 +
 +readonly certificateFile="/usr/local/share/ca-certificates/homelab.crt"
 +readonly certificateName="Homelab"
 +
 +find ~/.mozilla* ~/.thunderbird -name "cert9.db" | while read -r certDB
 +do
 +  certDir=$(dirname "${certDB}");
 +  echo "install '${certificateName}' in ${certDir}"
 +  certutil -A -n "${certificateName}" -t "TCu,Cuw,Tuw" -i ${certificateFile} -d ${certDir}
 +done
 +</code>
 +
 +
 ==== Générer un certificat auto-signé ==== ==== Générer un certificat auto-signé ====
 +
 +
 +  * [[https://ache.one/notes/cr%C3%A9er-un-certificat-auto-sign%C3%A9-depuis-un-CA-auto-sign%C3%A9|Obtenir un certificat auto-signé à partir de sa propre autorité de certification]]
  
 === Générer une clé privée RSA === === Générer une clé privée RSA ===
Ligne 33: Ligne 66:
  
  
------+
  
 ==== Lancer un serveur HTTPS ==== ==== Lancer un serveur HTTPS ====
 +
 <code bash> <code bash>
 sudo openssl s_server -accept 443 -cert server.pem sudo openssl s_server -accept 443 -cert server.pem
Ligne 44: Ligne 78:
  
  
-------+
  
 ==== Se connecter à un serveur HTTPS ==== ==== Se connecter à un serveur HTTPS ====
Ligne 55: Ligne 89:
  
  
------+
  
 ==== Connaître les dates d'un certificat ==== ==== Connaître les dates d'un certificat ====
Ligne 63: Ligne 97:
 <code bash> <code bash>
 openssl x509 -noout -dates -in cert.pem openssl x509 -noout -dates -in cert.pem
 +</code>
 +
 +
 +
 +==== Gérer sa propre PKI ====
 +
 +   * [[https://www.linuxtricks.fr/wiki/openssl-creation-d-une-autorite-de-certification-interne-et-de-certificats-clients|Openssl : Création d'une Autorité de Certification interne et de Certificats Clients]]
 +   * [[https://www.reddit.com/r/homelab/comments/1foflc8/pki_in_homelabs_how_do_you_manage_internal/|PKI in Homelabs - How Do You Manage Internal Certificates?]]
 +   * https://www.certwarden.com/, https://smallstep.com/docs/step-ca/getting-started/
 +   * [[https://github.com/FiloSottile/mkcert|mkcert]]
 +   * [[https://support.tools/create-certificate-authority-homelab/|Create Your Own Certificate Authority (CA) for Homelab Environment]]
 +
 +
 +=== Avec Smallstep ===
 +
 +  * [[https://gyptazy.com/building-your-own-pki-with-step-ca-from-root-ca-to-proxmox-integration-with-acme/|Building Your Own PKI with Step-CA – From Root CA to Proxmox Integration with ACME]]
 +  * [[https://smallstep.com/blog/build-a-tiny-ca-with-raspberry-pi-yubikey/|Build a Tiny Certificate Authority For Your Homelab]]
 +  * [[https://www.reddit.com/r/homelab/comments/azxwql/setup_an_internal_homelab_pki_and_get_valid_https/|Setup an internal homelab PKI and get valid HTTPS certificates using step]]
 +  * [[https://smallstep.com/docs/step-ca/certificate-authority-server-production/#running-step-ca-as-a-daemon]]
 +  * [[https://www.networktechguy.com/smallsteps-step-ca-as-ca-with-acme-support/|Smallstep's step-ca as CA with ACME support]]
 +
 +<code bash>
 +sudo curl -fsSL https://packages.smallstep.com/keys/apt/repo-signing-key.gpg -o /etc/apt/trusted.gpg.d/smallstep.asc && \
 +    echo 'deb [signed-by=/etc/apt/trusted.gpg.d/smallstep.asc] https://packages.smallstep.com/stable/debian debs main' \
 +    | sudo tee /etc/apt/sources.list.d/smallstep.list
 +sudo apt update
 +sudo apt install step-ca step-cli
 +
 +step ca init
 +
 +sudo step-ca $(step path)/config/ca.json
 +
 +step ca provisioner add acme --type ACME --x509-default-dur=$((30*24))h
 +
 +sudo useradd --user-group --system --home /etc/step-ca --shell /bin/false step
 +sudo setcap CAP_NET_BIND_SERVICE=+eip $(which step-ca)
 +sudo mv $(step path) /etc/step-ca
 +# put password in /etc/step-ca/password.txt
 +sudo chmod 400 /etc/step-ca/password.txt
 +# edit and adapt paths in /etc/step-ca/config/defaults.json
 +# edit and adapt paths in /etc/step-ca/config/ca.json
 +sudo chown -R step:step /etc/step-ca
 +# create the service in /etc/systemd/system/step-ca.service
 +sudo systemctl daemon-reload
 +sudo systemctl status step-ca
 +sudo systemctl enable --now step-ca
 +sudo journalctl --follow --unit=step-ca
 +</code>
 +
 +
 +=== Utiliser Certbot pour récupérer un certificat ===
 +
 +  * [[https://smallstep.com/docs/tutorials/acme-protocol-acme-clients/|Configure popular ACME clients to use a private CA with the ACME protocol]]
 +  * [[https://wizardsoftheweb.pro/certbot-hook-api/|The certbot Hook API]]
 +  * [[https://university.itametis.com/fr/articles/informatique/security/ssl/015_adding_hooks_before_after_certificate_renewal|Ajouter des hooks avant/après le renouvellement d'un certificat Let's Encrypt]]
 +  * [[https://gist.github.com/kaczmar2/e1b5eb635c1a1e792faf36508c5698ee|Pi-hole v6: Creating Your Own Self-Signed SSL Certificates]]
 +
 +Ici, on utilise Certbot pour automatiser la récupération d'un certificat pour l'interface web de PiHole :
 +<code bash>
 +sudo apt install certbot
 +
 +sudo certbot certonly -n --standalone -d pihole.homelab --server https://ca.homelab/acme/acme/directory
 +
 +sudo cat /etc/letsencrypt/live/pihole.homelab/privkey.pem /etc/letsencrypt/live/pihole.homelab/fullchain.pem | sudo tee /etc/pihole/tls.pem
 +sudo service pihole-FTL restart
 +</code>
 +
 +Après la commande, Certbot va automatiquement créer un timer Systemd pour deux fois par jour renouveler les certificats. Pour que le certificat renouvelé soit pris en compte par PiHole, créer le script ''/etc/letsencrypt/renewal-hooks/deploy/pihole'' (lui donner l'autorisation en exécution) :
 +<code bash>
 +#!/bin/sh -eu
 +
 +found=0
 +for domain in $RENEWED_DOMAINS
 +do
 +    if [ "$domain" = "pihole.homelab" ]
 +    then
 +        found=1
 +    fi
 +done
 +if [ "$found" = "0" ]
 +then
 +    exit 0
 +fi
 +
 +cp /etc/pihole/tls.pem /etc/pihole/tls.pem.old
 +cat /etc/letsencrypt/live/pihole.homelab/privkey.pem /etc/letsencrypt/live/pihole.homelab/fullchain.pem > /etc/pihole/tls.pem
 +
 +service pihole-FTL restart
 +</code>
 +
 +Un bug dans la version 4.0.0 de Certbot considère que ''renew_before_expiry'' vaut toujours 30 jours. Si les certificats ont une durée de validité de 30 jours, cela signifie qu'ils sont renouvelés à chaque fois. Pour contourner le bug, il faut préciser la durée de renouvellement dans ''/etc/letsencrypt/renewal/domain.conf'' :
 +<code>
 +renew_before_expiry = 10 days
 </code> </code>
  • openssl/accueil.1641923086.txt.gz
  • Dernière modification : 2022/01/11 18:44
  • de phsw