| 1 |
# acme-tiny |
| 2 |
|
| 3 |
[](https://github.com/diafygi/acme-tiny/actions/workflows/full-tests-with-coverage.yml) |
| 4 |
[](https://coveralls.io/github/diafygi/acme-tiny?branch=main) |
| 5 |
|
| 6 |
This is a tiny, auditable script that you can throw on your server to issue |
| 7 |
and renew [Let's Encrypt](https://letsencrypt.org/) certificates. Since it has |
| 8 |
to be run on your server and have access to your private Let's Encrypt account |
| 9 |
key, I tried to make it as tiny as possible (currently less than 200 lines). |
| 10 |
The only prerequisites are python and openssl. |
| 11 |
|
| 12 |
**PLEASE READ THE SOURCE CODE! YOU MUST TRUST IT WITH YOUR PRIVATE ACCOUNT KEY!** |
| 13 |
|
| 14 |
## Donate |
| 15 |
|
| 16 |
If this script is useful to you, please donate to the EFF. I don't work there, |
| 17 |
but they do fantastic work. |
| 18 |
|
| 19 |
[https://eff.org/donate/](https://eff.org/donate/) |
| 20 |
|
| 21 |
## How to use this script |
| 22 |
|
| 23 |
If you already have a Let's Encrypt issued certificate and just want to renew, |
| 24 |
you should only have to do Steps 3 and 6. |
| 25 |
|
| 26 |
### Step 1: Create a Let's Encrypt account private key (if you haven't already) |
| 27 |
|
| 28 |
You must have a public key registered with Let's Encrypt and sign your requests |
| 29 |
with the corresponding private key. If you don't understand what I just said, |
| 30 |
this script likely isn't for you! Please use the official Let's Encrypt |
| 31 |
[client](https://github.com/letsencrypt/letsencrypt). |
| 32 |
To accomplish this you need to initially create a key, that can be used by |
| 33 |
acme-tiny, to register an account for you and sign all following requests. |
| 34 |
|
| 35 |
``` |
| 36 |
openssl genrsa 4096 > account.key |
| 37 |
``` |
| 38 |
|
| 39 |
#### Use existing Let's Encrypt key |
| 40 |
|
| 41 |
Alternatively you can convert your key, previously generated by the original |
| 42 |
Let's Encrypt client. |
| 43 |
|
| 44 |
The private account key from the Let's Encrypt client is saved in the |
| 45 |
[JWK](https://tools.ietf.org/html/rfc7517) format. `acme-tiny` is using the PEM |
| 46 |
key format. To convert the key, you can use the tool |
| 47 |
[conversion script](https://gist.github.com/JonLundy/f25c99ee0770e19dc595) by JonLundy: |
| 48 |
|
| 49 |
```sh |
| 50 |
# Download the script |
| 51 |
wget -O - "https://gist.githubusercontent.com/JonLundy/f25c99ee0770e19dc595/raw/6035c1c8938fae85810de6aad1ecf6e2db663e26/conv.py" > conv.py |
| 52 |
|
| 53 |
# Copy your private key to your working directory |
| 54 |
cp /etc/letsencrypt/accounts/acme-v01.api.letsencrypt.org/directory/<id>/private_key.json private_key.json |
| 55 |
|
| 56 |
# Create a DER encoded private key |
| 57 |
openssl asn1parse -noout -out private_key.der -genconf <(python2 conv.py private_key.json) |
| 58 |
|
| 59 |
# Convert to PEM |
| 60 |
openssl rsa -in private_key.der -inform der > account.key |
| 61 |
``` |
| 62 |
|
| 63 |
### Step 2: Create a certificate signing request (CSR) for your domains. |
| 64 |
|
| 65 |
The ACME protocol (what Let's Encrypt uses) requires a CSR file to be submitted |
| 66 |
to it, even for renewals. You can use the same CSR for multiple renewals. NOTE: |
| 67 |
you can't use your account private key as your domain private key! |
| 68 |
|
| 69 |
``` |
| 70 |
# Generate a domain private key (if you haven't already) |
| 71 |
openssl genrsa 4096 > domain.key |
| 72 |
``` |
| 73 |
|
| 74 |
``` |
| 75 |
# For a single domain |
| 76 |
openssl req -new -sha256 -key domain.key -subj "/CN=yoursite.com" > domain.csr |
| 77 |
|
| 78 |
# For multiple domains (use this one if you want both www.yoursite.com and yoursite.com) |
| 79 |
openssl req -new -sha256 -key domain.key -subj "/" -addext "subjectAltName = DNS:yoursite.com, DNS:www.yoursite.com" > domain.csr |
| 80 |
|
| 81 |
# For multiple domains (same as above but works with openssl < 1.1.1) |
| 82 |
openssl req -new -sha256 -key domain.key -subj "/" -reqexts SAN -config <(cat /etc/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:yoursite.com,DNS:www.yoursite.com")) > domain.csr |
| 83 |
``` |
| 84 |
|
| 85 |
### Step 3: Make your website host challenge files |
| 86 |
|
| 87 |
You must prove you own the domains you want a certificate for, so Let's Encrypt |
| 88 |
requires you host some files on them. This script will generate and write those |
| 89 |
files in the folder you specify, so all you need to do is make sure that this |
| 90 |
folder is served under the ".well-known/acme-challenge/" url path. NOTE: Let's |
| 91 |
Encrypt will perform a plain HTTP request to port 80 on your server, so you |
| 92 |
must serve the challenge files via HTTP (a redirect to HTTPS is fine too). |
| 93 |
|
| 94 |
``` |
| 95 |
# Make some challenge folder (modify to suit your needs) |
| 96 |
mkdir -p /var/www/challenges/ |
| 97 |
``` |
| 98 |
|
| 99 |
```nginx |
| 100 |
# Example for nginx |
| 101 |
server { |
| 102 |
listen 80; |
| 103 |
server_name yoursite.com www.yoursite.com; |
| 104 |
|
| 105 |
location /.well-known/acme-challenge/ { |
| 106 |
alias /var/www/challenges/; |
| 107 |
try_files $uri =404; |
| 108 |
} |
| 109 |
|
| 110 |
...the rest of your config |
| 111 |
} |
| 112 |
``` |
| 113 |
|
| 114 |
### Step 4: Get a signed certificate! |
| 115 |
|
| 116 |
Now that you have setup your server and generated all the needed files, run this |
| 117 |
script on your server with the permissions needed to write to the above folder |
| 118 |
and read your private account key and CSR. |
| 119 |
|
| 120 |
``` |
| 121 |
# Run the script on your server |
| 122 |
python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /var/www/challenges/ > ./signed_chain.crt |
| 123 |
``` |
| 124 |
|
| 125 |
### Step 5: Install the certificate |
| 126 |
|
| 127 |
The signed https certificate chain that is output by this script can be used along |
| 128 |
with your private key to run an https server. You need to include them in the |
| 129 |
https settings in your web server's configuration. Here's an example on how to |
| 130 |
configure an nginx server: |
| 131 |
|
| 132 |
```nginx |
| 133 |
server { |
| 134 |
listen 443 ssl; |
| 135 |
server_name yoursite.com www.yoursite.com; |
| 136 |
|
| 137 |
ssl_certificate /path/to/signed_chain.crt; |
| 138 |
ssl_certificate_key /path/to/domain.key; |
| 139 |
ssl_session_timeout 5m; |
| 140 |
ssl_protocols TLSv1.2; |
| 141 |
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; |
| 142 |
ssl_session_cache shared:SSL:50m; |
| 143 |
ssl_dhparam /path/to/server.dhparam; |
| 144 |
ssl_prefer_server_ciphers on; |
| 145 |
|
| 146 |
...the rest of your config |
| 147 |
} |
| 148 |
|
| 149 |
server { |
| 150 |
listen 80; |
| 151 |
server_name yoursite.com www.yoursite.com; |
| 152 |
|
| 153 |
location /.well-known/acme-challenge/ { |
| 154 |
alias /var/www/challenges/; |
| 155 |
try_files $uri =404; |
| 156 |
} |
| 157 |
|
| 158 |
...the rest of your config |
| 159 |
} |
| 160 |
``` |
| 161 |
|
| 162 |
### Step 6: Setup an auto-renew cronjob |
| 163 |
|
| 164 |
Congrats! Your website is now using https! Unfortunately, Let's Encrypt |
| 165 |
certificates only last for 90 days, so you need to renew them often. No worries! |
| 166 |
It's automated! Just make a bash script and add it to your crontab (see below |
| 167 |
for example script). |
| 168 |
|
| 169 |
Example of a `renew_cert.sh`: |
| 170 |
```sh |
| 171 |
#!/usr/bin/sh |
| 172 |
python /path/to/acme_tiny.py --account-key /path/to/account.key --csr /path/to/domain.csr --acme-dir /var/www/challenges/ > /path/to/signed_chain.crt.tmp || exit |
| 173 |
mv /path/to/signed_chain.crt.tmp /path/to/signed_chain.crt |
| 174 |
service nginx reload |
| 175 |
``` |
| 176 |
|
| 177 |
``` |
| 178 |
# Example line in your crontab (runs once per month) |
| 179 |
0 0 1 * * /path/to/renew_cert.sh 2>> /var/log/acme_tiny.log |
| 180 |
``` |
| 181 |
|
| 182 |
**NOTE:** Since Let's Encrypt's ACME v2 release (acme-tiny 4.0.0+), the intermediate |
| 183 |
certificate is included in the issued certificate download, so you no longer have |
| 184 |
to independently download the intermediate certificate and concatenate it to your |
| 185 |
signed certificate. If you have an shell script or Makefile using acme-tiny <4.0 (e.g. before |
| 186 |
2018-03-17) with acme-tiny 4.0.0+, then you may be adding the intermediate |
| 187 |
certificate to your signed_chain.crt twice (which |
| 188 |
[causes issues with at least GnuTLS 3.7.0](https://gitlab.com/gnutls/gnutls/-/issues/1131) |
| 189 |
besides making the certificate slightly larger than it needs to be). To fix, |
| 190 |
simply remove the bash code where you're downloading the intermediate and adding |
| 191 |
it to the acme-tiny certificate output. |
| 192 |
|
| 193 |
## Permissions |
| 194 |
|
| 195 |
The biggest problem you'll likely come across while setting up and running this |
| 196 |
script is permissions. You want to limit access to your account private key and |
| 197 |
challenge web folder as much as possible. I'd recommend creating a user |
| 198 |
specifically for handling this script, the account private key, and the |
| 199 |
challenge folder. Then add the ability for that user to write to your installed |
| 200 |
certificate file (e.g. `/path/to/signed_chain.crt`) and reload your webserver. That |
| 201 |
way, the cron script will do its thing, overwrite your old certificate, and |
| 202 |
reload your webserver without having permission to do anything else. |
| 203 |
|
| 204 |
**BE SURE TO:** |
| 205 |
* Backup your account private key (e.g. `account.key`) |
| 206 |
* Don't allow this script to be able to read your domain private key! |
| 207 |
* Don't allow this script to be run as root! |
| 208 |
|
| 209 |
## Staging Environment |
| 210 |
|
| 211 |
Let's Encrypt recommends testing new configurations against their staging servers, |
| 212 |
so when testing out your new setup, you can use |
| 213 |
`--directory-url https://acme-staging-v02.api.letsencrypt.org/directory` |
| 214 |
to issue fake test certificates instead of real ones from Let's Encrypt's production servers. |
| 215 |
See [https://letsencrypt.org/docs/staging-environment/](https://letsencrypt.org/docs/staging-environment/) |
| 216 |
for more details. |
| 217 |
|
| 218 |
## Feedback/Contributing |
| 219 |
|
| 220 |
This project has a very, very limited scope and codebase. I'm happy to receive |
| 221 |
bug reports and pull requests, but please don't add any new features. This |
| 222 |
script must stay under 200 lines of code to ensure it can be easily audited by |
| 223 |
anyone who wants to run it. |
| 224 |
|
| 225 |
If you want to add features for your own setup to make things easier for you, |
| 226 |
please do! It's open source, so feel free to fork it and modify as necessary. |