Let's Encrypt: Difference between revisions

From Leo's Notes
This page was last edited on 30 December 2021, at 21:22.
apache
m Removed Linux category, added webapp category
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
Let's Encrypt is a service that provides free SSL certificates. It verifies a website's server by a series of challenges before signing any certificate requests.
[[File:Lets Encrypt Logo.png|thumb|right|Let's Encrypt]] Let's Encrypt is a service that provides free SSL certificates. It verifies a website's server by a series of challenges before signing any certificate requests.


== Getting Certificates ==
== Obtaining Certificates ==
Use the {{code|letsencrypt-auto}} script to interface with Let's Encrypt.


To generate a certificate, turn off any servers on port 80 and then run:
=== Certbot ===
{{highlight|lang=bash|code=
To get signed certificates from Let's Encrypt manually, use {{code|certbot}} available from https://certbot.eff.org.
./letsencrypt-auto certonly
 
Alternatively, some shared hosting providers (eg. cPanel hosts) may already have an automated process running that automatically fetches and installs signed certificates for you.
 
Install certbot using a package manager or download it directly from their site.
{{highlight|lang=terminal|code=
# dnf install certbot
## or
# wget https://dl.eff.org/certbot-auto
}}
 
To obtain only certificates using the built-in server:
{{highlight|lang=terminal|code=
## stop any services on port 443
# certbot certonly --standalone -d example.com -d www.example.com ...
}}
 
Alternatively, pass in the {{code|--webroot}} and {{code|-w /path}} for each of the domains to verify. Eg:
{{highlight|lang=terminal|code=
# certbot certonly --webroot -w /example.com -d example.com -w /testing.example.com -d testing.example.com ...
}}
}}


Once generated, you may use the new certificates.
Certificates will be placed in {{code|/etc/letsencrypt/live/$certname/}} for each certificate obtained.
 
To renew certificates:
{{highlight|lang=terminal|code=
## Renews all certificates on the system
# certbot renew
## or for only one specific certificate
# certbot renew --cert-name example.com
}}
 
To add additional domains, run certbot as if you were creating a new certificate. Certbot will then ask if you wish to expand your existing certificate.
 


=== Apache ===
==== Rate Limit ====
To manually configure apache for a virtual host, set:
You may get the following error when trying to obtain a domain:


{{highlight|lang=text|code=
{{highlight|lang=text|code=
SSLCertificateFile /etc/letsencrypt/live/domain/cert.pem
{
SSLCertificateKeyFile /etc/letsencrypt/live/domain/privkey.pem
"type": "urn:acme:error:rateLimited",
SSLCACertificateFile /etc/letsencrypt/live/domain/chain.pem
"detail": "Error creating new cert :: too many certificates already issued for: ucalgary.ca",
"status": 429
}
}}
 
This happens when more than 20 certificates per week is registered. More information at https://letsencrypt.org/docs/rate-limits/
 
==== See Also ====
* https://certbot.eff.org/docs/using.html
* https://certbot.eff.org/all-instructions/
 
=== letsencrypt-nosudo ===
{{Warning|1=Use Certbot Instead|2=This was written when Certbot wasn't available. You can use Certbot to just obtain the certificates as well without this hassle.
}}
}}


== Running Without Root ==
This neat project contains only the essential pieces of code in order to obtain signed certificate. Unlike the official client, this does not require sudo which is great for shared hosting accounts of paranoid sysadmins.
You may try to use the letsencrypt-nosudo project at https://github.com/diafygi/letsencrypt-nosudo.  


I was '''not able to get it working''' since the API kept on returning a 400 error:
Get the code from their GitHub repository at https://github.com/diafygi/letsencrypt-nosudo.
 
{{highlight|lang=terminal|code=
$ git clone https://github.com/diafygi/letsencrypt-nosudo
$ cd letsencrypt-nosudo
}}
 
I will do all the work inside a {{code|keys}} directory.
 
If this is the first time you are using Let's Encrypt, generate your user keys.
 
{{highlight|lang=terminal|code=
$ mkdir keys
$ cd keys
$ openssl genrsa 4096 > user.key
$ openssl rsa -in user.key -pubout > user.pub
}}
 
Create a private key for your domain if you don't have one already and then generate a certificate signing request with the subject containing the domain name to be signed.
 
{{highlight|lang=terminal|code=
$ openssl genrsa 4096 > domain.key
$ openssl req -new -sha256 -key domain.key -subj "/CN=leo.leung.xyz" > leo.leung.xyz.csr
}}
 
In a separate window (or screen, or Ctrl+Z out), run the {{code|sign_csr.py}} script. Since we do not want to run anything as root, we will have to use the file based challenge (option {{code|-f}}) instead as the other (default) option requires running a server on port 80.
 
{{highlight|lang=terminal|code=
$ python ../sign_csr.py -f --public-key user.pub  leo.leung.xyz.csr > signed.crt
}}
 
Follow the instructions that the script provides you with in order to satisfy Let's Encrypt's challenges.
 
{{highlight|lang=terminal|code=
## Step 2?
$ openssl dgst -sha256 -sign user.key -out register_kZt6ne.sig register_XHetUA.json
$ openssl dgst -sha256 -sign user.key -out domain_7w4NCI.sig domain_zrVWwn.json
$ openssl dgst -sha256 -sign user.key -out cert_LTLknJ.sig cert_cFr76L.json
## Step 3?
$ openssl dgst -sha256 -sign user.key -out challenge_2Hk1Dk.sig challenge_rVYWdK.json
## Step 4
## You will need to place the challenge file and response to a file served by your domain.
$ mkdir -p ~/public_html/.well-known/acme-challenge/
$ echo "" > ~/public_html/.well-known/acme-challenge/b9dITAlwo94hzwIiSHvR7wPclNpWHIJG5YnNHP4UvoM
}}
 
Once all that is done, you should now have the following files:
# {{code|domain.key}} - your domain's private key
# {{code|signed.crt}} - your domain's signed certificate
 
You may want to create chained certificate in case some browsers are unable to verify the deep chain of trust like so:
{{highlight|lang=terminal|code=
$ wget https://letsencrypt.org/certs/lets-encrypt-x1-cross-signed.pem
$ cat signed.crt lets-encrypt-x1-cross-signed.pem > chained.pem
}}
 
== Installing the Certificates ==
 
=== Apache ===
To manually configure apache for a virtual host, set:


{{highlight|lang=text|code=
{{highlight|lang=text|code=
{"type":"urn:acme:error:malformed","detail":"Unable to read/verify body :: JWS verification error","status":400}
SSLCertificateFile /etc/letsencrypt/live/domain/signed.crt
SSLCertificateKeyFile /etc/letsencrypt/live/domain/domain.key
SSLCACertificateFile /etc/letsencrypt/live/domain/chain.pem
}}
}}


This lead me to cheat. (see below)
=== cPanel ===


== Getting Certificates for cPanel ==
{{Info|1=Let's Encrypt + cPanel Integration|2=If you are on a cPanel web host, cPanel might already be obtaining SSL certificates for you automatically.
Because I do not want to run the {{code|letsencrypt-auto}} script as root on my cPanel account (and this applies to all shared hosting accounts too), I decided to run it inside a VM at home instead and then copy the certificate to the cPanel account. For this to work, I temporarily updated my DNS record to point to my home IP address before running {{code|letsencrypt-auto}}. Once the challenges passes and the certificate is signed, I reverted my DNS to point back to my cPanel server.


While this still exposes the private keys to their script, I was able to get around needing root access on the cPanel server.
If not, ask your web hosting provider to enable AutoSSL with Let's Encrypt as the provider, or run the {{code|/scripts/install_lets_encrypt_autossl_provider}} script as root.}}


Let's encrypt will then place the certificates at {{code|/etc/letsencrypt/archive/$DOMAIN$/}}, but should be referenced from the 'live' directory at {{code|/etc/letsencrypt/live/$DOMAIN$/}}. To get your certificate working with cPanel, log in to cPanel, click on 'Install an SSL Website', then fill the following fields with their respective file contents:
To install the certificates manually using cPanel, log in to cPanel, click on 'Install an SSL Website', then fill the following fields with their respective file contents:


# Certificate: (CRT) -> {{code|privkey.pem}}
# Certificate: (CRT) -> {{code|privkey.pem}}
Line 42: Line 141:
# Certificate Authority Bundle: (CABUNDLE) -> {{code|chain.pem}}
# Certificate Authority Bundle: (CABUNDLE) -> {{code|chain.pem}}


cPanel should take care of the rest once the SSL certificates are installed.
cPanel will automatically install the certificate on the web server once the SSL certificates are uploaded.


== Renewing ==
== Renewing ==
Let's Encrypt's certificates are only valid for 90 days and requires constant renewal. Possible solutions to automate this are:
Let's Encrypt's certificates are only valid for 90 days and requires constant renewal.
# Custom DNS zone for let's encrypt so they always see my domains from my home.
# ???
 


[[Category:Linux]]
{{Navbox Certificates}}[[Category:Certificates]]
{{Navbox Web}}
[[Category:WebApp]]

Latest revision as of 21:22, 30 December 2021

Let's Encrypt

Let's Encrypt is a service that provides free SSL certificates. It verifies a website's server by a series of challenges before signing any certificate requests.

Obtaining Certificates

Certbot

To get signed certificates from Let's Encrypt manually, use certbot available from https://certbot.eff.org.

Alternatively, some shared hosting providers (eg. cPanel hosts) may already have an automated process running that automatically fetches and installs signed certificates for you.

Install certbot using a package manager or download it directly from their site.

# dnf install certbot
## or
# wget https://dl.eff.org/certbot-auto

To obtain only certificates using the built-in server:

## stop any services on port 443
# certbot certonly --standalone -d example.com -d www.example.com ...

Alternatively, pass in the --webroot and -w /path for each of the domains to verify. Eg:

# certbot certonly --webroot -w /example.com -d example.com -w /testing.example.com -d testing.example.com ...

Certificates will be placed in /etc/letsencrypt/live/$certname/ for each certificate obtained.

To renew certificates:

## Renews all certificates on the system
# certbot renew
## or for only one specific certificate
# certbot renew --cert-name example.com

To add additional domains, run certbot as if you were creating a new certificate. Certbot will then ask if you wish to expand your existing certificate.


Rate Limit

You may get the following error when trying to obtain a domain:

{
"type": "urn:acme:error:rateLimited",
"detail": "Error creating new cert :: too many certificates already issued for: ucalgary.ca",
"status": 429
}

This happens when more than 20 certificates per week is registered. More information at https://letsencrypt.org/docs/rate-limits/

See Also

letsencrypt-nosudo

Use Certbot Instead
This was written when Certbot wasn't available. You can use Certbot to just obtain the certificates as well without this hassle.


This neat project contains only the essential pieces of code in order to obtain signed certificate. Unlike the official client, this does not require sudo which is great for shared hosting accounts of paranoid sysadmins.

Get the code from their GitHub repository at https://github.com/diafygi/letsencrypt-nosudo.

$ git clone https://github.com/diafygi/letsencrypt-nosudo
$ cd letsencrypt-nosudo

I will do all the work inside a keys directory.

If this is the first time you are using Let's Encrypt, generate your user keys.

$ mkdir keys
$ cd keys
$ openssl genrsa 4096 > user.key
$ openssl rsa -in user.key -pubout > user.pub

Create a private key for your domain if you don't have one already and then generate a certificate signing request with the subject containing the domain name to be signed.

$ openssl genrsa 4096 > domain.key
$ openssl req -new -sha256 -key domain.key -subj "/CN=leo.leung.xyz" > leo.leung.xyz.csr

In a separate window (or screen, or Ctrl+Z out), run the sign_csr.py script. Since we do not want to run anything as root, we will have to use the file based challenge (option -f) instead as the other (default) option requires running a server on port 80.

$ python ../sign_csr.py -f --public-key user.pub  leo.leung.xyz.csr > signed.crt

Follow the instructions that the script provides you with in order to satisfy Let's Encrypt's challenges.

## Step 2?
$ openssl dgst -sha256 -sign user.key -out register_kZt6ne.sig register_XHetUA.json
$ openssl dgst -sha256 -sign user.key -out domain_7w4NCI.sig domain_zrVWwn.json
$ openssl dgst -sha256 -sign user.key -out cert_LTLknJ.sig cert_cFr76L.json
## Step 3?
$ openssl dgst -sha256 -sign user.key -out challenge_2Hk1Dk.sig challenge_rVYWdK.json
## Step 4
## You will need to place the challenge file and response to a file served by your domain.
$ mkdir -p ~/public_html/.well-known/acme-challenge/
$ echo "" > ~/public_html/.well-known/acme-challenge/b9dITAlwo94hzwIiSHvR7wPclNpWHIJG5YnNHP4UvoM

Once all that is done, you should now have the following files:

  1. domain.key - your domain's private key
  2. signed.crt - your domain's signed certificate

You may want to create chained certificate in case some browsers are unable to verify the deep chain of trust like so:

$ wget https://letsencrypt.org/certs/lets-encrypt-x1-cross-signed.pem
$ cat signed.crt lets-encrypt-x1-cross-signed.pem > chained.pem

Installing the Certificates

Apache

To manually configure apache for a virtual host, set:

SSLCertificateFile /etc/letsencrypt/live/domain/signed.crt
SSLCertificateKeyFile /etc/letsencrypt/live/domain/domain.key
SSLCACertificateFile /etc/letsencrypt/live/domain/chain.pem

cPanel

Let's Encrypt + cPanel Integration
If you are on a cPanel web host, cPanel might already be obtaining SSL certificates for you automatically. If not, ask your web hosting provider to enable AutoSSL with Let's Encrypt as the provider, or run the /scripts/install_lets_encrypt_autossl_provider script as root.


To install the certificates manually using cPanel, log in to cPanel, click on 'Install an SSL Website', then fill the following fields with their respective file contents:

  1. Certificate: (CRT) -> privkey.pem
  2. Private Key (KEY) -> cert.pem
  3. Certificate Authority Bundle: (CABUNDLE) -> chain.pem

cPanel will automatically install the certificate on the web server once the SSL certificates are uploaded.

Renewing

Let's Encrypt's certificates are only valid for 90 days and requires constant renewal.