Browse Source
Use caddy's certmagic library for extensible/robust ACME handling (#14177)
Use caddy's certmagic library for extensible/robust ACME handling (#14177)
* use certmagic for more extensible/robust ACME cert handling * accept TOS based on config option Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv>mj-v1.14.3
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
437 changed files with 56286 additions and 4270 deletions
-
4.drone.yml
-
2Makefile
-
31cmd/web.go
-
69cmd/web_letsencrypt.go
-
2docs/config.yaml
-
1go.mod
-
24go.sum
-
1vendor/github.com/caddyserver/certmagic/.gitignore
-
201vendor/github.com/caddyserver/certmagic/LICENSE.txt
-
526vendor/github.com/caddyserver/certmagic/README.md
-
369vendor/github.com/caddyserver/certmagic/account.go
-
339vendor/github.com/caddyserver/certmagic/acmeclient.go
-
350vendor/github.com/caddyserver/certmagic/acmemanager.go
-
187vendor/github.com/caddyserver/certmagic/async.go
-
326vendor/github.com/caddyserver/certmagic/cache.go
-
404vendor/github.com/caddyserver/certmagic/certificates.go
-
480vendor/github.com/caddyserver/certmagic/certmagic.go
-
808vendor/github.com/caddyserver/certmagic/config.go
-
324vendor/github.com/caddyserver/certmagic/crypto.go
-
339vendor/github.com/caddyserver/certmagic/dnsutil.go
-
381vendor/github.com/caddyserver/certmagic/filestorage.go
-
12vendor/github.com/caddyserver/certmagic/go.mod
-
82vendor/github.com/caddyserver/certmagic/go.sum
-
573vendor/github.com/caddyserver/certmagic/handshake.go
-
133vendor/github.com/caddyserver/certmagic/httphandler.go
-
581vendor/github.com/caddyserver/certmagic/maintain.go
-
212vendor/github.com/caddyserver/certmagic/ocsp.go
-
243vendor/github.com/caddyserver/certmagic/ratelimiter.go
-
623vendor/github.com/caddyserver/certmagic/solvers.go
-
281vendor/github.com/caddyserver/certmagic/storage.go
-
1vendor/github.com/libdns/libdns/.gitignore
-
21vendor/github.com/libdns/libdns/LICENSE
-
48vendor/github.com/libdns/libdns/README.md
-
3vendor/github.com/libdns/libdns/go.mod
-
85vendor/github.com/libdns/libdns/libdns.go
-
1vendor/github.com/mholt/acmez/.gitignore
-
201vendor/github.com/mholt/acmez/LICENSE
-
59vendor/github.com/mholt/acmez/README.md
-
37vendor/github.com/mholt/acmez/THIRD-PARTY
-
249vendor/github.com/mholt/acmez/acme/account.go
-
283vendor/github.com/mholt/acmez/acme/authorization.go
-
165vendor/github.com/mholt/acmez/acme/certificate.go
-
133vendor/github.com/mholt/acmez/acme/challenge.go
-
240vendor/github.com/mholt/acmez/acme/client.go
-
394vendor/github.com/mholt/acmez/acme/http.go
-
223vendor/github.com/mholt/acmez/acme/jws.go
-
247vendor/github.com/mholt/acmez/acme/order.go
-
136vendor/github.com/mholt/acmez/acme/problem.go
-
656vendor/github.com/mholt/acmez/client.go
-
8vendor/github.com/mholt/acmez/go.mod
-
61vendor/github.com/mholt/acmez/go.sum
-
72vendor/github.com/mholt/acmez/solver.go
-
98vendor/github.com/mholt/acmez/tlsalpn01.go
-
8vendor/github.com/miekg/dns/.codecov.yml
-
4vendor/github.com/miekg/dns/.gitignore
-
17vendor/github.com/miekg/dns/.travis.yml
-
1vendor/github.com/miekg/dns/AUTHORS
-
1vendor/github.com/miekg/dns/CODEOWNERS
-
10vendor/github.com/miekg/dns/CONTRIBUTORS
-
9vendor/github.com/miekg/dns/COPYRIGHT
-
30vendor/github.com/miekg/dns/LICENSE
-
33vendor/github.com/miekg/dns/Makefile.fuzz
-
52vendor/github.com/miekg/dns/Makefile.release
-
176vendor/github.com/miekg/dns/README.md
-
61vendor/github.com/miekg/dns/acceptfunc.go
-
430vendor/github.com/miekg/dns/client.go
-
135vendor/github.com/miekg/dns/clientconfig.go
-
43vendor/github.com/miekg/dns/dane.go
-
384vendor/github.com/miekg/dns/defaults.go
-
134vendor/github.com/miekg/dns/dns.go
-
794vendor/github.com/miekg/dns/dnssec.go
-
140vendor/github.com/miekg/dns/dnssec_keygen.go
-
322vendor/github.com/miekg/dns/dnssec_keyscan.go
-
94vendor/github.com/miekg/dns/dnssec_privkey.go
-
268vendor/github.com/miekg/dns/doc.go
-
37vendor/github.com/miekg/dns/duplicate.go
-
675vendor/github.com/miekg/dns/edns.go
-
93vendor/github.com/miekg/dns/format.go
-
32vendor/github.com/miekg/dns/fuzz.go
-
247vendor/github.com/miekg/dns/generate.go
-
11vendor/github.com/miekg/dns/go.mod
-
39vendor/github.com/miekg/dns/go.sum
-
212vendor/github.com/miekg/dns/labels.go
-
44vendor/github.com/miekg/dns/listen_go111.go
-
23vendor/github.com/miekg/dns/listen_go_not111.go
-
1190vendor/github.com/miekg/dns/msg.go
-
766vendor/github.com/miekg/dns/msg_helpers.go
-
111vendor/github.com/miekg/dns/msg_truncate.go
-
95vendor/github.com/miekg/dns/nsecx.go
-
113vendor/github.com/miekg/dns/privaterr.go
-
52vendor/github.com/miekg/dns/reverse.go
-
86vendor/github.com/miekg/dns/sanitize.go
-
1331vendor/github.com/miekg/dns/scan.go
-
1743vendor/github.com/miekg/dns/scan_rr.go
-
122vendor/github.com/miekg/dns/serve_mux.go
-
764vendor/github.com/miekg/dns/server.go
-
209vendor/github.com/miekg/dns/sig0.go
-
61vendor/github.com/miekg/dns/singleinflight.go
-
44vendor/github.com/miekg/dns/smimea.go
-
44vendor/github.com/miekg/dns/tlsa.go
@ -0,0 +1,69 @@ |
|||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|||
// Use of this source code is governed by a MIT-style
|
|||
// license that can be found in the LICENSE file.
|
|||
|
|||
package cmd |
|||
|
|||
import ( |
|||
"net/http" |
|||
"strings" |
|||
|
|||
"code.gitea.io/gitea/modules/log" |
|||
"code.gitea.io/gitea/modules/setting" |
|||
|
|||
"github.com/caddyserver/certmagic" |
|||
context2 "github.com/gorilla/context" |
|||
) |
|||
|
|||
func runLetsEncrypt(listenAddr, domain, directory, email string, m http.Handler) error { |
|||
|
|||
// If HTTP Challenge enabled, needs to be serving on port 80. For TLSALPN needs 443.
|
|||
// Due to docker port mapping this can't be checked programatically
|
|||
// TODO: these are placeholders until we add options for each in settings with appropriate warning
|
|||
enableHTTPChallenge := true |
|||
enableTLSALPNChallenge := true |
|||
|
|||
magic := certmagic.NewDefault() |
|||
magic.Storage = &certmagic.FileStorage{Path: directory} |
|||
myACME := certmagic.NewACMEManager(magic, certmagic.ACMEManager{ |
|||
Email: email, |
|||
Agreed: setting.LetsEncryptTOS, |
|||
DisableHTTPChallenge: !enableHTTPChallenge, |
|||
DisableTLSALPNChallenge: !enableTLSALPNChallenge, |
|||
}) |
|||
|
|||
magic.Issuer = myACME |
|||
|
|||
// this obtains certificates or renews them if necessary
|
|||
err := magic.ManageSync([]string{domain}) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
tlsConfig := magic.TLSConfig() |
|||
|
|||
if enableHTTPChallenge { |
|||
go func() { |
|||
log.Info("Running Let's Encrypt handler on %s", setting.HTTPAddr+":"+setting.PortToRedirect) |
|||
// all traffic coming into HTTP will be redirect to HTTPS automatically (LE HTTP-01 validation happens here)
|
|||
var err = runHTTP("tcp", setting.HTTPAddr+":"+setting.PortToRedirect, myACME.HTTPChallengeHandler(http.HandlerFunc(runLetsEncryptFallbackHandler))) |
|||
if err != nil { |
|||
log.Fatal("Failed to start the Let's Encrypt handler on port %s: %v", setting.PortToRedirect, err) |
|||
} |
|||
}() |
|||
} |
|||
|
|||
return runHTTPSWithTLSConfig("tcp", listenAddr, tlsConfig, context2.ClearHandler(m)) |
|||
} |
|||
|
|||
func runLetsEncryptFallbackHandler(w http.ResponseWriter, r *http.Request) { |
|||
if r.Method != "GET" && r.Method != "HEAD" { |
|||
http.Error(w, "Use HTTPS", http.StatusBadRequest) |
|||
return |
|||
} |
|||
// Remove the trailing slash at the end of setting.AppURL, the request
|
|||
// URI always contains a leading slash, which would result in a double
|
|||
// slash
|
|||
target := strings.TrimSuffix(setting.AppURL, "/") + r.URL.RequestURI() |
|||
http.Redirect(w, r, target, http.StatusFound) |
|||
} |
@ -0,0 +1 @@ |
|||
_gitignore/ |
@ -0,0 +1,201 @@ |
|||
Apache License |
|||
Version 2.0, January 2004 |
|||
http://www.apache.org/licenses/ |
|||
|
|||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION |
|||
|
|||
1. Definitions. |
|||
|
|||
"License" shall mean the terms and conditions for use, reproduction, |
|||
and distribution as defined by Sections 1 through 9 of this document. |
|||
|
|||
"Licensor" shall mean the copyright owner or entity authorized by |
|||
the copyright owner that is granting the License. |
|||
|
|||
"Legal Entity" shall mean the union of the acting entity and all |
|||
other entities that control, are controlled by, or are under common |
|||
control with that entity. For the purposes of this definition, |
|||
"control" means (i) the power, direct or indirect, to cause the |
|||
direction or management of such entity, whether by contract or |
|||
otherwise, or (ii) ownership of fifty percent (50%) or more of the |
|||
outstanding shares, or (iii) beneficial ownership of such entity. |
|||
|
|||
"You" (or "Your") shall mean an individual or Legal Entity |
|||
exercising permissions granted by this License. |
|||
|
|||
"Source" form shall mean the preferred form for making modifications, |
|||
including but not limited to software source code, documentation |
|||
source, and configuration files. |
|||
|
|||
"Object" form shall mean any form resulting from mechanical |
|||
transformation or translation of a Source form, including but |
|||
not limited to compiled object code, generated documentation, |
|||
and conversions to other media types. |
|||
|
|||
"Work" shall mean the work of authorship, whether in Source or |
|||
Object form, made available under the License, as indicated by a |
|||
copyright notice that is included in or attached to the work |
|||
(an example is provided in the Appendix below). |
|||
|
|||
"Derivative Works" shall mean any work, whether in Source or Object |
|||
form, that is based on (or derived from) the Work and for which the |
|||
editorial revisions, annotations, elaborations, or other modifications |
|||
represent, as a whole, an original work of authorship. For the purposes |
|||
of this License, Derivative Works shall not include works that remain |
|||
separable from, or merely link (or bind by name) to the interfaces of, |
|||
the Work and Derivative Works thereof. |
|||
|
|||
"Contribution" shall mean any work of authorship, including |
|||
the original version of the Work and any modifications or additions |
|||
to that Work or Derivative Works thereof, that is intentionally |
|||
submitted to Licensor for inclusion in the Work by the copyright owner |
|||
or by an individual or Legal Entity authorized to submit on behalf of |
|||
the copyright owner. For the purposes of this definition, "submitted" |
|||
means any form of electronic, verbal, or written communication sent |
|||
to the Licensor or its representatives, including but not limited to |
|||
communication on electronic mailing lists, source code control systems, |
|||
and issue tracking systems that are managed by, or on behalf of, the |
|||
Licensor for the purpose of discussing and improving the Work, but |
|||
excluding communication that is conspicuously marked or otherwise |
|||
designated in writing by the copyright owner as "Not a Contribution." |
|||
|
|||
"Contributor" shall mean Licensor and any individual or Legal Entity |
|||
on behalf of whom a Contribution has been received by Licensor and |
|||
subsequently incorporated within the Work. |
|||
|
|||
2. Grant of Copyright License. Subject to the terms and conditions of |
|||
this License, each Contributor hereby grants to You a perpetual, |
|||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable |
|||
copyright license to reproduce, prepare Derivative Works of, |
|||
publicly display, publicly perform, sublicense, and distribute the |
|||
Work and such Derivative Works in Source or Object form. |
|||
|
|||
3. Grant of Patent License. Subject to the terms and conditions of |
|||
this License, each Contributor hereby grants to You a perpetual, |
|||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable |
|||
(except as stated in this section) patent license to make, have made, |
|||
use, offer to sell, sell, import, and otherwise transfer the Work, |
|||
where such license applies only to those patent claims licensable |
|||
by such Contributor that are necessarily infringed by their |
|||
Contribution(s) alone or by combination of their Contribution(s) |
|||
with the Work to which such Contribution(s) was submitted. If You |
|||
institute patent litigation against any entity (including a |
|||
cross-claim or counterclaim in a lawsuit) alleging that the Work |
|||
or a Contribution incorporated within the Work constitutes direct |
|||
or contributory patent infringement, then any patent licenses |
|||
granted to You under this License for that Work shall terminate |
|||
as of the date such litigation is filed. |
|||
|
|||
4. Redistribution. You may reproduce and distribute copies of the |
|||
Work or Derivative Works thereof in any medium, with or without |
|||
modifications, and in Source or Object form, provided that You |
|||
meet the following conditions: |
|||
|
|||
(a) You must give any other recipients of the Work or |
|||
Derivative Works a copy of this License; and |
|||
|
|||
(b) You must cause any modified files to carry prominent notices |
|||
stating that You changed the files; and |
|||
|
|||
(c) You must retain, in the Source form of any Derivative Works |
|||
that You distribute, all copyright, patent, trademark, and |
|||
attribution notices from the Source form of the Work, |
|||
excluding those notices that do not pertain to any part of |
|||
the Derivative Works; and |
|||
|
|||
(d) If the Work includes a "NOTICE" text file as part of its |
|||
distribution, then any Derivative Works that You distribute must |
|||
include a readable copy of the attribution notices contained |
|||
within such NOTICE file, excluding those notices that do not |
|||
pertain to any part of the Derivative Works, in at least one |
|||
of the following places: within a NOTICE text file distributed |
|||
as part of the Derivative Works; within the Source form or |
|||
documentation, if provided along with the Derivative Works; or, |
|||
within a display generated by the Derivative Works, if and |
|||
wherever such third-party notices normally appear. The contents |
|||
of the NOTICE file are for informational purposes only and |
|||
do not modify the License. You may add Your own attribution |
|||
notices within Derivative Works that You distribute, alongside |
|||
or as an addendum to the NOTICE text from the Work, provided |
|||
that such additional attribution notices cannot be construed |
|||
as modifying the License. |
|||
|
|||
You may add Your own copyright statement to Your modifications and |
|||
may provide additional or different license terms and conditions |
|||
for use, reproduction, or distribution of Your modifications, or |
|||
for any such Derivative Works as a whole, provided Your use, |
|||
reproduction, and distribution of the Work otherwise complies with |
|||
the conditions stated in this License. |
|||
|
|||
5. Submission of Contributions. Unless You explicitly state otherwise, |
|||
any Contribution intentionally submitted for inclusion in the Work |
|||
by You to the Licensor shall be under the terms and conditions of |
|||
this License, without any additional terms or conditions. |
|||
Notwithstanding the above, nothing herein shall supersede or modify |
|||
the terms of any separate license agreement you may have executed |
|||
with Licensor regarding such Contributions. |
|||
|
|||
6. Trademarks. This License does not grant permission to use the trade |
|||
names, trademarks, service marks, or product names of the Licensor, |
|||
except as required for reasonable and customary use in describing the |
|||
origin of the Work and reproducing the content of the NOTICE file. |
|||
|
|||
7. Disclaimer of Warranty. Unless required by applicable law or |
|||
agreed to in writing, Licensor provides the Work (and each |
|||
Contributor provides its Contributions) on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
|||
implied, including, without limitation, any warranties or conditions |
|||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A |
|||
PARTICULAR PURPOSE. You are solely responsible for determining the |
|||
appropriateness of using or redistributing the Work and assume any |
|||
risks associated with Your exercise of permissions under this License. |
|||
|
|||
8. Limitation of Liability. In no event and under no legal theory, |
|||
whether in tort (including negligence), contract, or otherwise, |
|||
unless required by applicable law (such as deliberate and grossly |
|||
negligent acts) or agreed to in writing, shall any Contributor be |
|||
liable to You for damages, including any direct, indirect, special, |
|||
incidental, or consequential damages of any character arising as a |
|||
result of this License or out of the use or inability to use the |
|||
Work (including but not limited to damages for loss of goodwill, |
|||
work stoppage, computer failure or malfunction, or any and all |
|||
other commercial damages or losses), even if such Contributor |
|||
has been advised of the possibility of such damages. |
|||
|
|||
9. Accepting Warranty or Additional Liability. While redistributing |
|||
the Work or Derivative Works thereof, You may choose to offer, |
|||
and charge a fee for, acceptance of support, warranty, indemnity, |
|||
or other liability obligations and/or rights consistent with this |
|||
License. However, in accepting such obligations, You may act only |
|||
on Your own behalf and on Your sole responsibility, not on behalf |
|||
of any other Contributor, and only if You agree to indemnify, |
|||
defend, and hold each Contributor harmless for any liability |
|||
incurred by, or claims asserted against, such Contributor by reason |
|||
of your accepting any such warranty or additional liability. |
|||
|
|||
END OF TERMS AND CONDITIONS |
|||
|
|||
APPENDIX: How to apply the Apache License to your work. |
|||
|
|||
To apply the Apache License to your work, attach the following |
|||
boilerplate notice, with the fields enclosed by brackets "{}" |
|||
replaced with your own identifying information. (Don't include |
|||
the brackets!) The text should be enclosed in the appropriate |
|||
comment syntax for the file format. We also recommend that a |
|||
file or class name and description of purpose be included on the |
|||
same "printed page" as the copyright notice for easier |
|||
identification within third-party archives. |
|||
|
|||
Copyright {yyyy} {name of copyright owner} |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
@ -0,0 +1,526 @@ |
|||
<p align="center"> |
|||
<a href="https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc"><img src="https://user-images.githubusercontent.com/1128849/49704830-49d37200-fbd5-11e8-8385-767e0cd033c3.png" alt="CertMagic" width="550"></a> |
|||
</p> |
|||
<h3 align="center">Easy and Powerful TLS Automation</h3> |
|||
<p align="center">The same library used by the <a href="https://caddyserver.com">Caddy Web Server</a></p> |
|||
<p align="center"> |
|||
<a href="https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc"><img src="https://img.shields.io/badge/godoc-reference-blue.svg"></a> |
|||
<a href="https://github.com/caddyserver/certmagic/actions?query=workflow%3ATests"><img src="https://github.com/caddyserver/certmagic/workflows/Tests/badge.svg"></a> |
|||
<a href="https://sourcegraph.com/github.com/caddyserver/certmagic?badge"><img src="https://sourcegraph.com/github.com/caddyserver/certmagic/-/badge.svg"></a> |
|||
</p> |
|||
|
|||
|
|||
Caddy's automagic TLS features—now for your own Go programs—in one powerful and easy-to-use library! |
|||
|
|||
CertMagic is the most mature, robust, and capable ACME client integration for Go... and perhaps ever. |
|||
|
|||
With CertMagic, you can add one line to your Go application to serve securely over TLS, without ever having to touch certificates. |
|||
|
|||
Instead of: |
|||
|
|||
```go |
|||
// plaintext HTTP, gross 🤢 |
|||
http.ListenAndServe(":80", mux) |
|||
``` |
|||
|
|||
Use CertMagic: |
|||
|
|||
```go |
|||
// encrypted HTTPS with HTTP->HTTPS redirects - yay! 🔒😍 |
|||
certmagic.HTTPS([]string{"example.com"}, mux) |
|||
``` |
|||
|
|||
That line of code will serve your HTTP router `mux` over HTTPS, complete with HTTP->HTTPS redirects. It obtains and renews the TLS certificates. It staples OCSP responses for greater privacy and security. As long as your domain name points to your server, CertMagic will keep its connections secure. |
|||
|
|||
Compared to other ACME client libraries for Go, only CertMagic supports the full suite of ACME features, and no other library matches CertMagic's maturity and reliability. |
|||
|
|||
|
|||
|
|||
|
|||
CertMagic - Automatic HTTPS using Let's Encrypt |
|||
=============================================== |
|||
|
|||
**Sponsored by Relica - Cross-platform local and cloud file backup:** |
|||
|
|||
<a href="https://relicabackup.com"><img src="https://caddyserver.com/resources/images/sponsors/relica.png" width="220" alt="Relica - Cross-platform file backup to the cloud, local disks, or other computers"></a> |
|||
|
|||
|
|||
## Menu |
|||
|
|||
- [Features](#features) |
|||
- [Requirements](#requirements) |
|||
- [Installation](#installation) |
|||
- [Usage](#usage) |
|||
- [Package Overview](#package-overview) |
|||
- [Certificate authority](#certificate-authority) |
|||
- [The `Config` type](#the-config-type) |
|||
- [Defaults](#defaults) |
|||
- [Providing an email address](#providing-an-email-address) |
|||
- [Rate limiting](#rate-limiting) |
|||
- [Development and testing](#development-and-testing) |
|||
- [Examples](#examples) |
|||
- [Serving HTTP handlers with HTTPS](#serving-http-handlers-with-https) |
|||
- [Starting a TLS listener](#starting-a-tls-listener) |
|||
- [Getting a tls.Config](#getting-a-tlsconfig) |
|||
- [Advanced use](#advanced-use) |
|||
- [Wildcard Certificates](#wildcard-certificates) |
|||
- [Behind a load balancer (or in a cluster)](#behind-a-load-balancer-or-in-a-cluster) |
|||
- [The ACME Challenges](#the-acme-challenges) |
|||
- [HTTP Challenge](#http-challenge) |
|||
- [TLS-ALPN Challenge](#tls-alpn-challenge) |
|||
- [DNS Challenge](#dns-challenge) |
|||
- [On-Demand TLS](#on-demand-tls) |
|||
- [Storage](#storage) |
|||
- [Cache](#cache) |
|||
- [Contributing](#contributing) |
|||
- [Project History](#project-history) |
|||
- [Credits and License](#credits-and-license) |
|||
|
|||
|
|||
## Features |
|||
|
|||
- Fully automated certificate management including issuance and renewal |
|||
- One-liner, fully managed HTTPS servers |
|||
- Full control over almost every aspect of the system |
|||
- HTTP->HTTPS redirects |
|||
- Solves all 3 ACME challenges: HTTP, TLS-ALPN, and DNS |
|||
- Most robust error handling of _any_ ACME client |
|||
- Challenges are randomized to avoid accidental dependence |
|||
- Challenges are rotated to overcome certain network blockages |
|||
- Robust retries for up to 30 days |
|||
- Exponential backoff with carefully-tuned intervals |
|||
- Retries with optional test/staging CA endpoint instead of production, to avoid rate limits |
|||
- Written in Go, a language with memory-safety guarantees |
|||
- Powered by [ACMEz](https://github.com/mholt/acmez), _the_ premier ACME client library for Go |
|||
- All [libdns](https://github.com/libdns) DNS providers work out-of-the-box |
|||
- Pluggable storage implementations (default: file system) |
|||
- Wildcard certificates |
|||
- Automatic OCSP stapling ([done right](https://gist.github.com/sleevi/5efe9ef98961ecfb4da8#gistcomment-2336055)) [keeps your sites online!](https://twitter.com/caddyserver/status/1234874273724084226) |
|||
- Will [automatically attempt](https://twitter.com/mholt6/status/1235577699541762048) to replace [revoked certificates](https://community.letsencrypt.org/t/2020-02-29-caa-rechecking-bug/114591/3?u=mholt)! |
|||
- Staples stored to disk in case of responder outages |
|||
- Distributed solving of all challenges (works behind load balancers) |
|||
- Highly efficient, coordinated management in a fleet |
|||
- Active locking |
|||
- Smart queueing |
|||
- Supports "on-demand" issuance of certificates (during TLS handshakes!) |
|||
- Caddy / CertMagic pioneered this technology |
|||
- Custom decision functions to regulate and throttle on-demand behavior |
|||
- Optional event hooks for observation |
|||
- Works with any certificate authority (CA) compliant with the ACME specification |
|||
- Certificate revocation (please, only if private key is compromised) |
|||
- Must-Staple (optional; not default) |
|||
- Cross-platform support! Mac, Windows, Linux, BSD, Android... |
|||
- Scales to hundreds of thousands of names/certificates per instance |
|||
- Use in conjunction with your own certificates |
|||
|
|||
|
|||
## Requirements |
|||
|
|||
1. Public DNS name(s) you control |
|||
2. Server reachable from public Internet |
|||
- Or use the DNS challenge to waive this requirement |
|||
3. Control over port 80 (HTTP) and/or 443 (HTTPS) |
|||
- Or they can be forwarded to other ports you control |
|||
- Or use the DNS challenge to waive this requirement |
|||
- (This is a requirement of the ACME protocol, not a library limitation) |
|||
4. Persistent storage |
|||
- Typically the local file system (default) |
|||
- Other integrations available/possible |
|||
|
|||
**_Before using this library, your domain names MUST be pointed (A/AAAA records) at your server (unless you use the DNS challenge)!_** |
|||
|
|||
|
|||
## Installation |
|||
|
|||
```bash |
|||
$ go get github.com/caddyserver/certmagic |
|||
``` |
|||
|
|||
|
|||
## Usage |
|||
|
|||
### Package Overview |
|||
|
|||
#### Certificate authority |
|||
|
|||
This library uses Let's Encrypt by default, but you can use any certificate authority that conforms to the ACME specification. Known/common CAs are provided as consts in the package, for example `LetsEncryptStagingCA` and `LetsEncryptProductionCA`. |
|||
|
|||
#### The `Config` type |
|||
|
|||
The `certmagic.Config` struct is how you can wield the power of this fully armed and operational battle station. However, an empty/uninitialized `Config` is _not_ a valid one! In time, you will learn to use the force of `certmagic.NewDefault()` as I have. |
|||
|
|||
#### Defaults |
|||
|
|||
The default `Config` value is called `certmagic.Default`. Change its fields to suit your needs, then call `certmagic.NewDefault()` when you need a valid `Config` value. In other words, `certmagic.Default` is a template and is not valid for use directly. |
|||
|
|||
You can set the default values easily, for example: `certmagic.Default.Issuer = ...`. |
|||
|
|||
Similarly, to configure ACME-specific defaults, use `certmagic.DefaultACME`. |
|||
|
|||
The high-level functions in this package (`HTTPS()`, `Listen()`, `ManageSync()`, and `ManageAsync()`) use the default config exclusively. This is how most of you will interact with the package. This is suitable when all your certificates are managed the same way. However, if you need to manage certificates differently depending on their name, you will need to make your own cache and configs (keep reading). |
|||
|
|||
|
|||
#### Providing an email address |
|||
|
|||
Although not strictly required, this is highly recommended best practice. It allows you to receive expiration emails if your certificates are expiring for some reason, and also allows the CA's engineers to potentially get in touch with you if something is wrong. I recommend setting `certmagic.DefaultACME.Email` or always setting the `Email` field of a new `Config` struct. |
|||
|
|||
|
|||
#### Rate limiting |
|||
|
|||
To avoid firehosing the CA's servers, CertMagic has built-in rate limiting. Currently, its default limit is up to 10 transactions (obtain or renew) every 1 minute (sliding window). This can be changed by setting the `RateLimitEvents` and `RateLimitEventsWindow` variables, if desired. |
|||
|
|||
The CA may still enforce their own rate limits, and there's nothing (well, nothing ethical) CertMagic can do to bypass them for you. |
|||
|
|||
Additionally, CertMagic will retry failed validations with exponential backoff for up to 30 days, with a reasonable maximum interval between attempts (an "attempt" means trying each enabled challenge type once). |
|||
|
|||
|
|||
### Development and Testing |
|||
|
|||
Note that Let's Encrypt imposes [strict rate limits](https://letsencrypt.org/docs/rate-limits/) at its production endpoint, so using it while developing your application may lock you out for a few days if you aren't careful! |
|||
|
|||
While developing your application and testing it, use [their staging endpoint](https://letsencrypt.org/docs/staging-environment/) which has much higher rate limits. Even then, don't hammer it: but it's much safer for when you're testing. When deploying, though, use their production CA because their staging CA doesn't issue trusted certificates. |
|||
|
|||
To use staging, set `certmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCA` or set `CA` of every `ACMEManager` struct. |
|||
|
|||
|
|||
|
|||
### Examples |
|||
|
|||
There are many ways to use this library. We'll start with the highest-level (simplest) and work down (more control). |
|||
|
|||
All these high-level examples use `certmagic.Default` and `certmagic.DefaultACME` for the config and the default cache and storage for serving up certificates. |
|||
|
|||
First, we'll follow best practices and do the following: |
|||
|
|||
```go |
|||
// read and agree to your CA's legal documents |
|||
certmagic.DefaultACME.Agreed = true |
|||
|
|||
// provide an email address |
|||
certmagic.DefaultACME.Email = "you@yours.com" |
|||
|
|||
// use the staging endpoint while we're developing |
|||
certmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCA |
|||
``` |
|||
|
|||
For fully-functional program examples, check out [this Twitter thread](https://twitter.com/mholt6/status/1073103805112147968) (or read it [unrolled into a single post](https://threadreaderapp.com/thread/1073103805112147968.html)). (Note that the package API has changed slightly since these posts.) |
|||
|
|||
|
|||
#### Serving HTTP handlers with HTTPS |
|||
|
|||
```go |
|||
err := certmagic.HTTPS([]string{"example.com", "www.example.com"}, mux) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
``` |
|||
|
|||
This starts HTTP and HTTPS listeners and redirects HTTP to HTTPS! |
|||
|
|||
#### Starting a TLS listener |
|||
|
|||
```go |
|||
ln, err := certmagic.Listen([]string{"example.com"}) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
``` |
|||
|
|||
|
|||
#### Getting a tls.Config |
|||
|
|||
```go |
|||
tlsConfig, err := certmagic.TLS([]string{"example.com"}) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
``` |
|||
|
|||
|
|||
#### Advanced use |
|||
|
|||
For more control (particularly, if you need a different way of managing each certificate), you'll make and use a `Cache` and a `Config` like so: |
|||
|
|||
```go |
|||
cache := certmagic.NewCache(certmagic.CacheOptions{ |
|||
GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) { |
|||
// do whatever you need to do to get the right |
|||
// configuration for this certificate; keep in |
|||
// mind that this config value is used as a |
|||
// template, and will be completed with any |
|||
// defaults that are set in the Default config |
|||
return &certmagic.Config{ |
|||
// ... |
|||
}, nil |
|||
}, |
|||
... |
|||
}) |
|||
|
|||
magic := certmagic.New(cache, certmagic.Config{ |
|||
// any customizations you need go here |
|||
}) |
|||
|
|||
myACME := certmagic.NewACMEManager(magic, ACMEManager{ |
|||
CA: certmagic.LetsEncryptStagingCA, |
|||
Email: "you@yours.com", |
|||
Agreed: true, |
|||
// plus any other customizations you need |
|||
}) |
|||
|
|||
magic.Issuer = myACME |
|||
|
|||
// this obtains certificates or renews them if necessary |
|||
err := magic.ManageSync([]string{"example.com", "sub.example.com"}) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
// to use its certificates and solve the TLS-ALPN challenge, |
|||
// you can get a TLS config to use in a TLS listener! |
|||
tlsConfig := magic.TLSConfig() |
|||
|
|||
//// OR //// |
|||
|
|||
// if you already have a TLS config you don't want to replace, |
|||
// we can simply set its GetCertificate field and append the |
|||
// TLS-ALPN challenge protocol to the NextProtos |
|||
myTLSConfig.GetCertificate = magic.GetCertificate |
|||
myTLSConfig.NextProtos = append(myTLSConfig.NextProtos, tlsalpn01.ACMETLS1Protocol} |
|||
|
|||
// the HTTP challenge has to be handled by your HTTP server; |
|||
// if you don't have one, you should have disabled it earlier |
|||
// when you made the certmagic.Config |
|||
httpMux = myACME.HTTPChallengeHandler(httpMux) |
|||
``` |
|||
|
|||
Great! This example grants you much more flexibility for advanced programs. However, _the vast majority of you will only use the high-level functions described earlier_, especially since you can still customize them by setting the package-level `Default` config. |
|||
|
|||
|
|||
### Wildcard certificates |
|||
|
|||
At time of writing (December 2018), Let's Encrypt only issues wildcard certificates with the DNS challenge. You can easily enable the DNS challenge with CertMagic for numerous providers (see the relevant section in the docs). |
|||
|
|||
|
|||
### Behind a load balancer (or in a cluster) |
|||
|
|||
CertMagic runs effectively behind load balancers and/or in cluster/fleet environments. In other words, you can have 10 or 1,000 servers all serving the same domain names, all sharing certificates and OCSP staples. |
|||
|
|||
To do so, simply ensure that each instance is using the same Storage. That is the sole criteria for determining whether an instance is part of a cluster. |
|||
|
|||
The default Storage is implemented using the file system, so mounting the same shared folder is sufficient (see [Storage](#storage) for more on that)! If you need an alternate Storage implementation, feel free to use one, provided that all the instances use the _same_ one. :) |
|||
|
|||
See [Storage](#storage) and the associated [pkg.go.dev](https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc#Storage) for more information! |
|||
|
|||
|
|||
## The ACME Challenges |
|||
|
|||
This section describes how to solve the ACME challenges. Challenges are how you demonstrate to the certificate authority some control over your domain name, thus authorizing them to grant you a certificate for that name. [The great innovation of ACME](https://www.dotconferences.com/2016/10/matthew-holt-go-with-acme) is that verification by CAs can now be automated, rather than having to click links in emails (who ever thought that was a good idea??). |
|||
|
|||
If you're using the high-level convenience functions like `HTTPS()`, `Listen()`, or `TLS()`, the HTTP and/or TLS-ALPN challenges are solved for you because they also start listeners. However, if you're making a `Config` and you start your own server manually, you'll need to be sure the ACME challenges can be solved so certificates can be renewed. |
|||
|
|||
The HTTP and TLS-ALPN challenges are the defaults because they don't require configuration from you, but they require that your server is accessible from external IPs on low ports. If that is not possible in your situation, you can enable the DNS challenge, which will disable the HTTP and TLS-ALPN challenges and use the DNS challenge exclusively. |
|||
|
|||
Technically, only one challenge needs to be enabled for things to work, but using multiple is good for reliability in case a challenge is discontinued by the CA. This happened to the TLS-SNI challenge in early 2018—many popular ACME clients such as Traefik and Autocert broke, resulting in downtime for some sites, until new releases were made and patches deployed, because they used only one challenge; Caddy, however—this library's forerunner—was unaffected because it also used the HTTP challenge. If multiple challenges are enabled, they are chosen randomly to help prevent false reliance on a single challenge type. And if one fails, any remaining enabled challenges are tried before giving up. |
|||
|
|||
|
|||
### HTTP Challenge |
|||
|
|||
Per the ACME spec, the HTTP challenge requires port 80, or at least packet forwarding from port 80. It works by serving a specific HTTP response that only the genuine server would have to a normal HTTP request at a special endpoint. |
|||
|
|||
If you are running an HTTP server, solving this challenge is very easy: just wrap your handler in `HTTPChallengeHandler` _or_ call `SolveHTTPChallenge()` inside your own `ServeHTTP()` method. |
|||
|
|||
For example, if you're using the standard library: |
|||
|
|||
```go |
|||
mux := http.NewServeMux() |
|||
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
|||
fmt.Fprintf(w, "Lookit my cool website over HTTPS!") |
|||
}) |
|||
|
|||
http.ListenAndServe(":80", myACME.HTTPChallengeHandler(mux)) |
|||
``` |
|||
|
|||
If wrapping your handler is not a good solution, try this inside your `ServeHTTP()` instead: |
|||
|
|||
```go |
|||
magic := certmagic.NewDefault() |
|||
myACME := certmagic.NewACMEManager(magic, certmagic.DefaultACME) |
|||
|
|||
func ServeHTTP(w http.ResponseWriter, req *http.Request) { |
|||
if myACME.HandleHTTPChallenge(w, r) { |
|||
return // challenge handled; nothing else to do |
|||
} |
|||
... |
|||
} |
|||
``` |
|||
|
|||
If you are not running an HTTP server, you should disable the HTTP challenge _or_ run an HTTP server whose sole job it is to solve the HTTP challenge. |
|||
|
|||
|
|||
### TLS-ALPN Challenge |
|||
|
|||
Per the ACME spec, the TLS-ALPN challenge requires port 443, or at least packet forwarding from port 443. It works by providing a special certificate using a standard TLS extension, Application Layer Protocol Negotiation (ALPN), having a special value. This is the most convenient challenge type because it usually requires no extra configuration and uses the standard TLS port which is where the certificates are used, also. |
|||
|
|||
This challenge is easy to solve: just use the provided `tls.Config` when you make your TLS listener: |
|||
|
|||
```go |
|||
// use this to configure a TLS listener |
|||
tlsConfig := magic.TLSConfig() |
|||
``` |
|||
|
|||
Or make two simple changes to an existing `tls.Config`: |
|||
|
|||
```go |
|||
myTLSConfig.GetCertificate = magic.GetCertificate |
|||
myTLSConfig.NextProtos = append(myTLSConfig.NextProtos, tlsalpn01.ACMETLS1Protocol} |
|||
``` |
|||
|
|||
Then just make sure your TLS listener is listening on port 443: |
|||
|
|||
```go |
|||
ln, err := tls.Listen("tcp", ":443", myTLSConfig) |
|||
``` |
|||
|
|||
|
|||
### DNS Challenge |
|||
|
|||
The DNS challenge is perhaps the most useful challenge because it allows you to obtain certificates without your server needing to be publicly accessible on the Internet, and it's the only challenge by which Let's Encrypt will issue wildcard certificates. |
|||
|
|||
This challenge works by setting a special record in the domain's zone. To do this automatically, your DNS provider needs to offer an API by which changes can be made to domain names, and the changes need to take effect immediately for best results. CertMagic supports [all DNS providers with `libdns` implementations](https://github.com/libdns)! It always cleans up the temporary record after the challenge completes. |
|||
|
|||
To enable it, just set the `DNS01Solver` field on a `certmagic.ACMEManager` struct, or set the default `certmagic.ACMEManager.DNS01Solver` variable. For example, if my domains' DNS was served by Cloudflare: |
|||
|
|||
```go |
|||
import "github.com/libdns/cloudflare" |
|||
|
|||
certmagic.DefaultACME.DNS01Solver = &certmagic.DNS01Solver{ |
|||
DNSProvider: cloudflare.Provider{ |
|||
APIToken: "topsecret", |
|||
}, |
|||
} |
|||
``` |
|||
|
|||
Now the DNS challenge will be used by default, and I can obtain certificates for wildcard domains, too. Enabling the DNS challenge disables the other challenges for that `certmagic.ACMEManager` instance. |
|||
|
|||
|
|||
## On-Demand TLS |
|||
|
|||
Normally, certificates are obtained and renewed before a listener starts serving, and then those certificates are maintained throughout the lifetime of the program. In other words, the certificate names are static. But sometimes you don't know all the names ahead of time, or you don't want to manage all the certificates up front. This is where On-Demand TLS shines. |
|||
|
|||
Originally invented for use in Caddy (which was the first program to use such technology), On-Demand TLS makes it possible and easy to serve certificates for arbitrary or specific names during the lifetime of the server. When a TLS handshake is received, CertMagic will read the Server Name Indication (SNI) value and either load and present that certificate in the ServerHello, or if one does not exist, it will obtain it from a CA right then-and-there. |
|||
|
|||
Of course, this has some obvious security implications. You don't want to DoS a CA or allow arbitrary clients to fill your storage with spammy TLS handshakes. That's why, when you enable On-Demand issuance, you should set limits or policy to allow getting certificates. CertMagic has an implicit whitelist built-in which is sufficient for nearly everyone, but also has a more advanced way to control on-demand issuance. |
|||
|
|||
The simplest way to enable on-demand issuance is to set the OnDemand field of a Config (or the default package-level value): |
|||
|
|||
```go |
|||
certmagic.Default.OnDemand = new(certmagic.OnDemandConfig) |
|||
``` |
|||
|
|||
By setting this to a non-nil value, on-demand TLS is enabled for that config. For convenient security, CertMagic's high-level abstraction functions such as `HTTPS()`, `TLS()`, `ManageSync()`, `ManageAsync()`, and `Listen()` (which all accept a list of domain names) will whitelist those names automatically so only certificates for those names can be obtained when using the Default config. Usually this is sufficient for most users. |
|||
|
|||
However, if you require advanced control over which domains can be issued certificates on-demand (for example, if you do not know which domain names you are managing, or just need to defer their operations until later), you should implement your own DecisionFunc: |
|||
|
|||
```go |
|||
// if the decision function returns an error, a certificate |
|||
// may not be obtained for that name at that time |
|||
certmagic.Default.OnDemand = &certmagic.OnDemandConfig{ |
|||
DecisionFunc: func(name string) error { |
|||
if name != "example.com" { |
|||
return fmt.Errorf("not allowed") |
|||
} |
|||
return nil |
|||
}, |
|||
} |
|||
``` |
|||
|
|||
The [pkg.go.dev](https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc#OnDemandConfig) describes how to use this in full detail, so please check it out! |
|||
|
|||
|
|||
## Storage |
|||
|
|||
CertMagic relies on storage to store certificates and other TLS assets (OCSP staple cache, coordinating locks, etc). Persistent storage is a requirement when using CertMagic: ephemeral storage will likely lead to rate limiting on the CA-side as CertMagic will always have to get new certificates. |
|||
|
|||
By default, CertMagic stores assets on the local file system in `$HOME/.local/share/certmagic` (and honors `$XDG_DATA_HOME` if set). CertMagic will create the directory if it does not exist. If writes are denied, things will not be happy, so make sure CertMagic can write to it! |
|||
|
|||
The notion of a "cluster" or "fleet" of instances that may be serving the same site and sharing certificates, etc, is tied to storage. Simply, any instances that use the same storage facilities are considered part of the cluster. So if you deploy 100 instances of CertMagic behind a load balancer, they are all part of the same cluster if they share the same storage configuration. Sharing storage could be mounting a shared folder, or implementing some other distributed storage system such as a database server or KV store. |
|||
|
|||
The easiest way to change the storage being used is to set `certmagic.DefaultStorage` to a value that satisfies the [Storage interface](https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc#Storage). Keep in mind that a valid `Storage` must be able to implement some operations atomically in order to provide locking and synchronization. |
|||
|
|||
If you write a Storage implementation, please add it to the [project wiki](https://github.com/caddyserver/certmagic/wiki/Storage-Implementations) so people can find it! |
|||
|
|||
|
|||
## Cache |
|||
|
|||
All of the certificates in use are de-duplicated and cached in memory for optimal performance at handshake-time. This cache must be backed by persistent storage as described above. |
|||
|
|||
Most applications will not need to interact with certificate caches directly. Usually, the closest you will come is to set the package-wide `certmagic.DefaultStorage` variable (before attempting to create any Configs). However, if your use case requires using different storage facilities for different Configs (that's highly unlikely and NOT recommended! Even Caddy doesn't get that crazy), you will need to call `certmagic.NewCache()` and pass in the storage you want to use, then get new `Config` structs with `certmagic.NewWithCache()` and pass in the cache. |
|||
|
|||
Again, if you're needing to do this, you've probably over-complicated your application design. |
|||
|
|||
|
|||
## FAQ |
|||
|
|||
### Can I use some of my own certificates while using CertMagic? |
|||
|
|||
Yes, just call the relevant method on the `Config` to add your own certificate to the cache: |
|||
|
|||
- [`CacheUnmanagedCertificatePEMBytes()`](https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc#Config.CacheUnmanagedCertificatePEMBytes) |
|||
- [`CacheUnmanagedCertificatePEMFile()`](https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc#Config.CacheUnmanagedCertificatePEMFile) |
|||
- [`CacheUnmanagedTLSCertificate()`](https://pkg.go.dev/github.com/caddyserver/certmagic?tab=doc#Config.CacheUnmanagedTLSCertificate) |
|||
|
|||
Keep in mind that unmanaged certificates are (obviously) not renewed for you, so you'll have to replace them when you do. However, OCSP stapling is performed even for unmanaged certificates that qualify. |
|||
|
|||
|
|||
### Does CertMagic obtain SAN certificates? |
|||
|
|||
Technically all certificates these days are SAN certificates because CommonName is deprecated. But if you're asking whether CertMagic issues and manages certificates with multiple SANs, the answer is no. But it does support serving them, if you provide your own. |
|||
|
|||
|
|||
### How can I listen on ports 80 and 443? Do I have to run as root? |
|||
|
|||
On Linux, you can use `setcap` to grant your binary the permission to bind low ports: |
|||
|
|||
```bash |
|||
$ sudo setcap cap_net_bind_service=+ep /path/to/your/binary |
|||
``` |
|||
|
|||
and then you will not need to run with root privileges. |
|||
|
|||
|
|||
## Contributing |
|||
|
|||
We welcome your contributions! Please see our **[contributing guidelines](https://github.com/caddyserver/certmagic/blob/master/.github/CONTRIBUTING.md)** for instructions. |
|||
|
|||
|
|||
## Project History |
|||
|
|||
CertMagic is the core of Caddy's advanced TLS automation code, extracted into a library. The underlying ACME client implementation is [ACMEz](https://github.com/mholt/acmez). CertMagic's code was originally a central part of Caddy even before Let's Encrypt entered public beta in 2015. |
|||
|
|||
In the years since then, Caddy's TLS automation techniques have been widely adopted, tried and tested in production, and served millions of sites and secured trillions of connections. |
|||
|
|||
Now, CertMagic is _the actual library used by Caddy_. It's incredibly powerful and feature-rich, but also easy to use for simple Go programs: one line of code can enable fully-automated HTTPS applications with HTTP->HTTPS redirects. |
|||
|
|||
Caddy is known for its robust HTTPS+ACME features. When ACME certificate authorities have had outages, in some cases Caddy was the only major client that didn't experience any downtime. Caddy can weather OCSP outages lasting days, or CA outages lasting weeks, without taking your sites offline. |
|||
|
|||
Caddy was also the first to sport "on-demand" issuance technology, which obtains certificates during the first TLS handshake for an allowed SNI name. |
|||
|
|||
Consequently, CertMagic brings all these (and more) features and capabilities right into your own Go programs. |
|||
|
|||
You can [watch a 2016 dotGo talk](https://www.dotconferences.com/2016/10/matthew-holt-go-with-acme) by the author of this library about using ACME to automate certificate management in Go programs: |
|||
|
|||
[](https://www.dotconferences.com/2016/10/matthew-holt-go-with-acme) |
|||
|
|||
|
|||
|
|||
## Credits and License |
|||
|
|||
CertMagic is a project by [Matthew Holt](https://twitter.com/mholt6), who is the author; and various contributors, who are credited in the commit history of either CertMagic or Caddy. |
|||
|
|||
CertMagic is licensed under Apache 2.0, an open source license. For convenience, its main points are summarized as follows (but this is no replacement for the actual license text): |
|||
|
|||
- The author owns the copyright to this code |
|||
- Use, distribute, and modify the software freely |
|||
- Private and internal use is allowed |
|||
- License text and copyright notices must stay intact and be included with distributions |
|||
- Any and all changes to the code must be documented |
@ -0,0 +1,369 @@ |
|||
// Copyright 2015 Matthew Holt
|
|||
//
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
// you may not use this file except in compliance with the License.
|
|||
// You may obtain a copy of the License at
|
|||
//
|
|||
// http://www.apache.org/licenses/LICENSE-2.0
|
|||
//
|
|||
// Unless required by applicable law or agreed to in writing, software
|
|||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
// See the License for the specific language governing permissions and
|
|||
// limitations under the License.
|
|||
|
|||
package certmagic |
|||
|
|||
import ( |
|||
"bufio" |
|||
"crypto/ecdsa" |
|||
"crypto/elliptic" |
|||
"crypto/rand" |
|||
"encoding/json" |
|||
"fmt" |
|||
"io" |
|||
"os" |
|||
"path" |
|||
"sort" |
|||
"strings" |
|||
|
|||
"github.com/mholt/acmez/acme" |
|||
) |
|||
|
|||
// getAccount either loads or creates a new account, depending on if
|
|||
// an account can be found in storage for the given CA + email combo.
|
|||
func (am *ACMEManager) getAccount(ca, email string) (acme.Account, error) { |
|||
regBytes, err := am.config.Storage.Load(am.storageKeyUserReg(ca, email)) |
|||
if err != nil { |
|||
if _, ok := err.(ErrNotExist); ok { |
|||
return am.newAccount(email) |
|||
} |
|||
return acme.Account{}, err |
|||
} |
|||
keyBytes, err := am.config.Storage.Load(am.storageKeyUserPrivateKey(ca, email)) |
|||
if err != nil { |
|||
if _, ok := err.(ErrNotExist); ok { |
|||
return am.newAccount(email) |
|||
} |
|||
return acme.Account{}, err |
|||
} |
|||
|
|||
var acct acme.Account |
|||
err = json.Unmarshal(regBytes, &acct) |
|||
if err != nil { |
|||
return acct, err |
|||
} |
|||
acct.PrivateKey, err = decodePrivateKey(keyBytes) |
|||
if err != nil { |
|||
return acct, fmt.Errorf("could not decode account's private key: %v", err) |
|||
} |
|||
|
|||
// TODO: July 2020 - transition to new ACME lib and account structure;
|
|||
// for a while, we will need to convert old accounts to new structure
|
|||
acct, err = am.transitionAccountToACMEzJuly2020Format(ca, acct, regBytes) |
|||
if err != nil { |
|||
return acct, fmt.Errorf("one-time account transition: %v", err) |
|||
} |
|||
|
|||
return acct, err |
|||
} |
|||
|
|||
// TODO: this is a temporary transition helper starting July 2020.
|
|||
// It can go away when we think enough time has passed that most active assets have transitioned.
|
|||
func (am *ACMEManager) transitionAccountToACMEzJuly2020Format(ca string, acct acme.Account, regBytes []byte) (acme.Account, error) { |
|||
if acct.Status != "" && acct.Location != "" { |
|||
return acct, nil |
|||
} |
|||
|
|||
var oldAcct struct { |
|||
Email string `json:"Email"` |
|||
Registration struct { |
|||
Body struct { |
|||
Status string `json:"status"` |
|||
TermsOfServiceAgreed bool `json:"termsOfServiceAgreed"` |
|||
Orders string `json:"orders"` |
|||
ExternalAccountBinding json.RawMessage `json:"externalAccountBinding"` |
|||
} `json:"body"` |
|||
URI string `json:"uri"` |
|||
} `json:"Registration"` |
|||
} |
|||
err := json.Unmarshal(regBytes, &oldAcct) |
|||
if err != nil { |
|||
return acct, fmt.Errorf("decoding into old account type: %v", err) |
|||
} |
|||
|
|||
acct.Status = oldAcct.Registration.Body.Status |
|||
acct.TermsOfServiceAgreed = oldAcct.Registration.Body.TermsOfServiceAgreed |
|||
acct.Location = oldAcct.Registration.URI |
|||
acct.ExternalAccountBinding = oldAcct.Registration.Body.ExternalAccountBinding |
|||
acct.Orders = oldAcct.Registration.Body.Orders |
|||
if oldAcct.Email != "" { |
|||
acct.Contact = []string{"mailto:" + oldAcct.Email} |
|||
} |
|||
|
|||
err = am.saveAccount(ca, acct) |
|||
if err != nil { |
|||
return acct, fmt.Errorf("saving converted account: %v", err) |
|||
} |
|||
|
|||
return acct, nil |
|||
} |
|||
|
|||
// newAccount generates a new private key for a new ACME account, but
|
|||
// it does not register or save the account.
|
|||
func (*ACMEManager) newAccount(email string) (acme.Account, error) { |
|||
var acct acme.Account |
|||
if email != "" { |
|||
acct.Contact = []string{"mailto:" + email} // TODO: should we abstract the contact scheme?
|
|||
} |
|||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
|||
if err != nil { |
|||
return acct, fmt.Errorf("generating private key: %v", err) |
|||
} |
|||
acct.PrivateKey = privateKey |
|||
return acct, nil |
|||
} |
|||
|
|||
// saveAccount persists an ACME account's info and private key to storage.
|
|||
// It does NOT register the account via ACME or prompt the user.
|
|||
func (am *ACMEManager) saveAccount(ca string, account acme.Account) error { |
|||
regBytes, err := json.MarshalIndent(account, "", "\t") |
|||
if err != nil { |
|||
return err |
|||
} |
|||
keyBytes, err := encodePrivateKey(account.PrivateKey) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
// extract primary contact (email), without scheme (e.g. "mailto:")
|
|||
primaryContact := getPrimaryContact(account) |
|||
all := []keyValue{ |
|||
{ |
|||
key: am.storageKeyUserReg(ca, primaryContact), |
|||
value: regBytes, |
|||
}, |
|||
{ |
|||
key: am.storageKeyUserPrivateKey(ca, primaryContact), |
|||
value: keyBytes, |
|||
}, |
|||
} |
|||
return storeTx(am.config.Storage, all) |
|||
} |
|||
|
|||
// getEmail does everything it can to obtain an email address
|
|||
// from the user within the scope of memory and storage to use
|
|||
// for ACME TLS. If it cannot get an email address, it does nothing
|
|||
// (If user is prompted, it will warn the user of
|
|||
// the consequences of an empty email.) This function MAY prompt
|
|||
// the user for input. If allowPrompts is false, the user
|
|||
// will NOT be prompted and an empty email may be returned.
|
|||
func (am *ACMEManager) getEmail(allowPrompts bool) error { |
|||
leEmail := am.Email |
|||
|
|||
// First try package default email
|
|||
if leEmail == "" { |
|||
leEmail = DefaultACME.Email // TODO: racey with line 122 (or whichever line assigns to DefaultACME.Email below)
|
|||
} |
|||
|
|||
// Then try to get most recent user email from storage
|
|||
var gotRecentEmail bool |
|||
if leEmail == "" { |
|||
leEmail, gotRecentEmail = am.mostRecentAccountEmail(am.CA) |
|||
} |
|||
if !gotRecentEmail && leEmail == "" && allowPrompts { |
|||
// Looks like there is no email address readily available,
|
|||
// so we will have to ask the user if we can.
|
|||
var err error |
|||
leEmail, err = am.promptUserForEmail() |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
// User might have just signified their agreement
|
|||
am.Agreed = DefaultACME.Agreed |
|||
} |
|||
|
|||
// save the email for later and ensure it is consistent
|
|||
// for repeated use; then update cfg with the email
|
|||
DefaultACME.Email = strings.TrimSpace(strings.ToLower(leEmail)) // TODO: this is racey with line 99
|
|||
am.Email = DefaultACME.Email |
|||
|
|||
return nil |
|||
} |
|||
|
|||
// promptUserForEmail prompts the user for an email address
|
|||
// and returns the email address they entered (which could
|
|||
// be the empty string). If no error is returned, then Agreed
|
|||
// will also be set to true, since continuing through the
|
|||
// prompt signifies agreement.
|
|||
func (am *ACMEManager) promptUserForEmail() (string, error) { |
|||
// prompt the user for an email address and terms agreement
|
|||
reader := bufio.NewReader(stdin) |
|||
am.promptUserAgreement("") |
|||
fmt.Println("Please enter your email address to signify agreement and to be notified") |
|||
fmt.Println("in case of issues. You can leave it blank, but we don't recommend it.") |
|||
fmt.Print(" Email address: ") |
|||
leEmail, err := reader.ReadString('\n') |
|||
if err != nil && err != io.EOF { |
|||
return "", fmt.Errorf("reading email address: %v", err) |
|||
} |
|||
leEmail = strings.TrimSpace(leEmail) |
|||
DefaultACME.Agreed = true |
|||
return leEmail, nil |
|||
} |
|||
|
|||
// promptUserAgreement simply outputs the standard user
|
|||
// agreement prompt with the given agreement URL.
|
|||
// It outputs a newline after the message.
|
|||
func (am *ACMEManager) promptUserAgreement(agreementURL string) { |
|||
userAgreementPrompt := `Your sites will be served over HTTPS automatically using an automated CA. |
|||
By continuing, you agree to the CA's terms of service` |
|||
if agreementURL == "" { |
|||
fmt.Printf("\n\n%s.\n", userAgreementPrompt) |
|||
return |
|||
} |
|||
fmt.Printf("\n\n%s at:\n %s\n", userAgreementPrompt, agreementURL) |
|||
} |
|||
|
|||
// askUserAgreement prompts the user to agree to the agreement
|
|||
// at the given agreement URL via stdin. It returns whether the
|
|||
// user agreed or not.
|
|||
func (am *ACMEManager) askUserAgreement(agreementURL string) bool { |
|||
am.promptUserAgreement(agreementURL) |
|||
fmt.Print("Do you agree to the terms? (y/n): ") |
|||
|
|||
reader := bufio.NewReader(stdin) |
|||
answer, err := reader.ReadString('\n') |
|||
if err != nil { |
|||
return false |
|||
} |
|||
answer = strings.ToLower(strings.TrimSpace(answer)) |
|||
|
|||
return answer == "y" || answer == "yes" |
|||
} |
|||
|
|||
func (am *ACMEManager) storageKeyCAPrefix(caURL string) string { |
|||
return path.Join(prefixACME, StorageKeys.Safe(am.issuerKey(caURL))) |
|||
} |
|||
|
|||
func (am *ACMEManager) storageKeyUsersPrefix(caURL string) string { |
|||
return path.Join(am.storageKeyCAPrefix(caURL), "users") |
|||
} |
|||
|
|||
func (am *ACMEManager) storageKeyUserPrefix(caURL, email string) string { |
|||
if email == "" { |
|||
email = emptyEmail |
|||
} |
|||
return path.Join(am.storageKeyUsersPrefix(caURL), StorageKeys.Safe(email)) |
|||
} |
|||
|
|||
func (am *ACMEManager) storageKeyUserReg(caURL, email string) string { |
|||
return am.storageSafeUserKey(caURL, email, "registration", ".json") |
|||
} |
|||
|
|||
func (am *ACMEManager) storageKeyUserPrivateKey(caURL, email string) string { |
|||
return am.storageSafeUserKey(caURL, email, "private", ".key") |
|||
} |
|||
|
|||
// storageSafeUserKey returns a key for the given email, with the default
|
|||
// filename, and the filename ending in the given extension.
|
|||
func (am *ACMEManager) storageSafeUserKey(ca, email, defaultFilename, extension string) string { |
|||
if email == "" { |
|||
email = emptyEmail |
|||
} |
|||
email = strings.ToLower(email) |
|||
filename := am.emailUsername(email) |
|||
if filename == "" { |
|||
filename = defaultFilename |
|||
} |
|||
filename = StorageKeys.Safe(filename) |
|||
return path.Join(am.storageKeyUserPrefix(ca, email), filename+extension) |
|||
} |
|||
|
|||
// emailUsername returns the username portion of an email address (part before
|
|||
// '@') or the original input if it can't find the "@" symbol.
|
|||
func (*ACMEManager) emailUsername(email string) string { |
|||
at := strings.Index(email, "@") |
|||
if at == -1 { |
|||
return email |
|||
} else if at == 0 { |
|||
return email[1:] |
|||
} |
|||
return email[:at] |
|||
} |
|||
|
|||
// mostRecentAccountEmail finds the most recently-written account file
|
|||
// in storage. Since this is part of a complex sequence to get a user
|
|||
// account, errors here are discarded to simplify code flow in
|
|||
// the caller, and errors are not important here anyway.
|
|||
func (am *ACMEManager) mostRecentAccountEmail(caURL string) (string, bool) { |
|||
accountList, err := am.config.Storage.List(am.storageKeyUsersPrefix(caURL), false) |
|||
if err != nil || len(accountList) == 0 { |
|||
return "", false |
|||
} |
|||
|
|||
// get all the key infos ahead of sorting, because
|
|||
// we might filter some out
|
|||
stats := make(map[string]KeyInfo) |
|||
for i, u := range accountList { |
|||
keyInfo, err := am.config.Storage.Stat(u) |
|||
if err != nil { |
|||
continue |
|||
} |
|||
if keyInfo.IsTerminal { |
|||
// I found a bug when macOS created a .DS_Store file in
|
|||
// the users folder, and CertMagic tried to use that as
|
|||
// the user email because it was newer than the other one
|
|||
// which existed... sure, this isn't a perfect fix but
|
|||
// frankly one's OS shouldn't mess with the data folder
|
|||
// in the first place.
|
|||
accountList = append(accountList[:i], accountList[i+1:]...) |
|||
continue |
|||
} |
|||
stats[u] = keyInfo |
|||
} |
|||
|
|||
sort.Slice(accountList, func(i, j int) bool { |
|||
iInfo := stats[accountList[i]] |
|||
jInfo := stats[accountList[j]] |
|||
return jInfo.Modified.Before(iInfo.Modified) |
|||
}) |
|||
|
|||
if len(accountList) == 0 { |
|||
return "", false |
|||
} |
|||
|
|||
account, err := am.getAccount(caURL, path.Base(accountList[0])) |
|||
if err != nil { |
|||
return "", false |
|||
} |
|||
|
|||
return getPrimaryContact(account), true |
|||
} |
|||
|
|||
// getPrimaryContact returns the first contact on the account (if any)
|
|||
// without the scheme. (I guess we assume an email address.)
|
|||
func getPrimaryContact(account acme.Account) string { |
|||
// TODO: should this be abstracted with some lower-level helper?
|
|||
var primaryContact string |
|||
if len(account.Contact) > 0 { |
|||
primaryContact = account.Contact[0] |
|||
if idx := strings.Index(primaryContact, ":"); idx >= 0 { |
|||
primaryContact = primaryContact[idx+1:] |
|||
} |
|||
} |
|||
return primaryContact |
|||
} |
|||
|
|||
// agreementTestURL is set during tests to skip requiring
|
|||
// setting up an entire ACME CA endpoint.
|
|||
var agreementTestURL string |
|||
|
|||
// stdin is used to read the user's input if prompted;
|
|||
// this is changed by tests during tests.
|
|||
var stdin = io.ReadWriter(os.Stdin) |
|||
|
|||
// The name of the folder for accounts where the email
|
|||
// address was not provided; default 'username' if you will,
|
|||
// but only for local/storage use, not with the CA.
|
|||
const emptyEmail = "default" |
@ -0,0 +1,339 @@ |
|||
// Copyright 2015 Matthew Holt
|
|||
//
|
|||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
// you may not use this file except in compliance with the License.
|
|||
// You may obtain a copy of the License at
|
|||
//
|
|||
// http://www.apache.org/licenses/LICENSE-2.0
|
|||
//
|
|||
// Unless required by applicable law or agreed to in writing, software
|
|||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
// See the License for the specific language governing permissions and
|
|||
// limitations under the License.
|
|||
|
|||
package certmagic |
|||
|
|||
import ( |
|||
"context" |
|||
"crypto/tls" |
|||
"crypto/x509" |
|||
"fmt" |
|||
weakrand "math/rand" |
|||
"net" |
|||
"net/http" |
|||
"net/url" |
|||
"strconv" |
|||
"strings" |
|||
"sync" |
|||
"time" |
|||
|
|||
"github.com/mholt/acmez" |
|||
"github.com/mholt/acmez/acme" |
|||
"go.uber.org/zap" |
|||
) |
|||
|
|||
func init() { |
|||
weakrand.Seed(time.Now().UnixNano()) |
|||
} |
|||
|
|||
// acmeClient holds state necessary for us to perform
|
|||
// ACME operations for certificate management. Call
|
|||
// ACMEManager.newACMEClient() to get a valid one to .
|
|||
type acmeClient struct { |
|||
mgr *ACMEManager |
|||
acmeClient *acmez.Client |
|||
account acme.Account |
|||
} |
|||
|
|||
// newACMEClient creates the underlying ACME library client type.
|
|||
// If useTestCA is true, am.TestCA will be used if it is set;
|
|||
// otherwise, the primary CA will still be used.
|
|||
func (am *ACMEManager) newACMEClient(ctx context.Context, useTestCA, interactive bool) (*acmeClient, error) { |
|||
// ensure defaults are filled in
|
|||
var caURL string |
|||
if useTestCA { |
|||
caURL = am.TestCA |
|||
} |
|||
if caURL == "" { |
|||
caURL = am.CA |
|||
} |
|||
if caURL == "" { |
|||
caURL = DefaultACME.CA |
|||
} |
|||
certObtainTimeout := am.CertObtainTimeout |
|||
if certObtainTimeout == 0 { |
|||
certObtainTimeout = DefaultACME.CertObtainTimeout |
|||
} |
|||
|
|||
// ensure endpoint is secure (assume HTTPS if scheme is missing)
|
|||
if !strings.Contains(caURL, "://") { |
|||
caURL = "https://" + caURL |
|||
} |
|||
u, err := url.Parse(caURL) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
if u.Scheme != "https" && !isLoopback(u.Host) && !isInternal(u.Host) { |
|||
return nil, fmt.Errorf("%s: insecure CA URL (HTTPS required)", caURL) |
|||
} |
|||
|
|||
// look up or create the ACME account
|
|||
account, err := am.getAccount(caURL, am.Email) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("getting ACME account: %v", err) |
|||
} |
|||
|
|||
// set up the dialers and resolver for the ACME client's HTTP client
|
|||
dialer := &net.Dialer{ |
|||
Timeout: 30 * time.Second, |
|||
KeepAlive: 2 * time.Minute, |
|||
} |
|||
if am.Resolver != "" { |
|||
dialer.Resolver = &net.Resolver{ |
|||
PreferGo: true, |
|||
Dial: func(ctx context.Context, network, _ string) (net.Conn, error) { |
|||
return (&net.Dialer{ |
|||
Timeout: 15 * time.Second, |
|||
}).DialContext(ctx, network, am.Resolver) |
|||
}, |
|||
} |
|||
} |
|||
|
|||
// TODO: we could potentially reuse the HTTP transport and client
|
|||
hc := am.httpClient // TODO: is this racey?
|
|||
if am.httpClient == nil { |
|||
transport := &http.Transport{ |
|||
Proxy: http.ProxyFromEnvironment, |
|||
DialContext: dialer.DialContext, |
|||
TLSHandshakeTimeout: 15 * time.Second, |
|||
ResponseHeaderTimeout: 15 * time.Second, |
|||
ExpectContinueTimeout: 2 * time.Second, |
|||
ForceAttemptHTTP2: true, |
|||
} |
|||
if am.TrustedRoots != nil { |
|||
transport.TLSClientConfig = &tls.Config{ |
|||
RootCAs: am.TrustedRoots, |
|||
} |
|||
} |
|||
|
|||
hc = &http.Client{ |
|||
Transport: transport, |
|||
Timeout: HTTPTimeout, |
|||
} |
|||
|
|||
am.httpClient = hc |
|||
} |
|||
|
|||
client := &acmez.Client{ |
|||
Client: &acme.Client{ |
|||
Directory: caURL, |
|||
PollTimeout: certObtainTimeout, |
|||
UserAgent: buildUAString(), |
|||
HTTPClient: hc, |
|||
}, |
|||
ChallengeSolvers: make(map[string]acmez.Solver), |
|||
} |
|||
if am.Logger != nil { |
|||
l := am.Logger.Named("acme_client") |
|||
client.Client.Logger, client.Logger = l, l |
|||
} |
|||
|
|||
// configure challenges (most of the time, DNS challenge is
|
|||
// exclusive of other ones because it is usually only used
|
|||
// in situations where the default challenges would fail)
|
|||
if am.DNS01Solver == nil { |
|||
// enable HTTP-01 challenge
|
|||
if !am.DisableHTTPChallenge { |
|||
useHTTPPort := HTTPChallengePort |
|||
if HTTPPort > 0 && HTTPPort != HTTPChallengePort { |
|||
useHTTPPort = HTTPPort |
|||
} |
|||
if am.AltHTTPPort > 0 { |
|||
useHTTPPort = am.AltHTTPPort |
|||
} |
|||
client.ChallengeSolvers[acme.ChallengeTypeHTTP01] = distributedSolver{ |
|||
acmeManager: am, |
|||
solver: &httpSolver{ |
|||
acmeManager: am, |
|||
address: net.JoinHostPort(am.ListenHost, strconv.Itoa(useHTTPPort)), |
|||
}, |
|||
caURL: client.Directory, |
|||
} |
|||
} |
|||
|
|||
// enable TLS-ALPN-01 challenge
|
|||
if !am.DisableTLSALPNChallenge { |
|||
useTLSALPNPort := TLSALPNChallengePort |
|||
if HTTPSPort > 0 && HTTPSPort != TLSALPNChallengePort { |
|||
useTLSALPNPort = HTTPSPort |
|||
} |
|||
if am.AltTLSALPNPort > 0 { |
|||
useTLSALPNPort = am.AltTLSALPNPort |
|||
} |
|||
client.ChallengeSolvers[acme.ChallengeTypeTLSALPN01] = distributedSolver{ |
|||
acmeManager: am, |
|||
solver: &tlsALPNSolver{ |
|||
config: am.config, |
|||
address: net.JoinHostPort(am.ListenHost, strconv.Itoa(useTLSALPNPort)), |
|||
}, |
|||
caURL: client.Directory, |
|||
} |
|||
} |
|||
} else { |
|||
// use DNS challenge exclusively
|
|||
client.ChallengeSolvers[acme.ChallengeTypeDNS01] = am.DNS01Solver |
|||
} |
|||
|
|||
// register account if it is new
|
|||
if account.Status == "" { |
|||
if am.NewAccountFunc != nil { |
|||
err = am.NewAccountFunc(ctx, am, account) |
|||
if err != nil { |