Category Archives: Linux

Use .htaccess to redirect to https

If you need to redirect any request in a site to https you can use mod rewrite in a htaccess file. In this specific case we want to redirect whatever insecure url is used to a secure url. The advantage is that it doesn’t do a thing in the request was already secure. Also if you have multiple headers pointing to that directory, the htaccess file will redirect them preserving the original host header use the code below.

RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

SHA2 certificate OpenSSL Linux

To generate a certificate signing request with requesting a SHA256 signed certificate you can run the following script on a linux box with OpenSSL installed on it. Run this Shell script. chmod +X of course

 

domein=YourFQDN
pass=YourPassword
organisatie=YourOrganisation
provincie=YourProvince
stad=YourCity

string=/CN=$domein/O=$organisatie/C=NL/ST=$provincie/L=$stad
openssl genrsa -des3 -passout pass:$pass 2048 > $domein.key
openssl req -sha256 -new -key $domein.key -passin pass:$pass -subj $string -out $domein.csr
openssl rsa -in $domein.key -passin pass:$pass -out $domein-decrypted.key

~

This script will output a private key, a decrypted private key and a CSR.

If you received the CA’s response and it is in a wrong format you can run all of the lines below. If you received a PEM formatted response you can just the line with PKCS12 OpenSSL command.

 

domein=YourFQDN
pass=YourPassword

openssl x509 -in $domein.cer  -out $domein.der -outform DER
openssl x509 -in $domein.der -inform DER -out $domein.pem -outform PEM
openssl pkcs12 -export -in $domein.pem -inkey $domein.key -passin pass:$pass -out $domein.pfx -passout pass:$pass -name $domein

Certificaat aanvragen en converteren met OpenSSL

Certificaten kunnen in openssl worden geconverteerd. Het aanpassen van de indeling is een van de opties die openssl ondersteund. Zolang het certificaat een x509 standaard is kunnen onderstaande commando’s gebruiker worden voor een dergelijke actie:

Het aanvragen van een certificaat gaat als volgt in OpenSSL.

Genereren van een RSA private key:

openssl genrsa -des3 2048 > private.key

Genereren van een PKCS#10 CSR:

openssl -req -new -key private.key -out certificaat.csr

Hierna kan bij de CA een certificaat aangevraagd worden. De CA zal over het algemeen een certificaat aanleveren in de CER indeling. Om een PFX bestand te maken dient het certificaat van de CER indeling naar de PEM indeling worden geconverteerd. Dit gaat als volgd:

openssl x509 -in certificaat.cer -out certificaat.der -outform DER

openssl x509 -in certificaat.der -out certificaat.pem -outform pem

Nadat het certificaat is omgezet naar de PEM indeling kan er een PFX of PKCS#12 aangemaakt te worden:

openssl pkcs12 -export -in certificaat.pem -inkey private.key -out certificaat.pfx -name Friendly name