Commit Graph

360 Commits

Author SHA1 Message Date
Matthew Holt
579abc82db
Finish cert revocation checking enhancements 2022-02-01 13:24:11 -07:00
Matthew Holt
bded7eab59
WIP 2022-02-01 11:04:25 -07:00
Matthew Holt
f849f878ef
Move force check 2022-02-01 10:50:23 -07:00
Matthew Holt
60df7f89b9
Some debug logs 2022-02-01 10:00:49 -07:00
Matt Holt
eef59acc1d
Fix force-renewing revoked on-demand certs (#166)
* Fix force-renewing revoked on-demand certs

Follow-up to 9245be5a2f

* One more fix for on-demand logic of revoked certs

* OCSP revocation checks at startup, too

Required significant refactoring, hope it works.
Yet again way too late at night for this...
2022-02-01 09:05:24 -07:00
Matthew Holt
9245be5a2f
Automatically replace revoked certs managed on-demand
When I initially wrote the auto-replace feature, it was for the standard mode of operation,
which I presumed the vast majority of CertMagic deployments use. At the time, On-Demand
mode of operation was fairly niche. And at the time, it looked tricky to properly enable this feature for on-demand certificates, so I shelved it considering it would be low-impact anyway.
So on-demand certificates didn't benefit from auto-replace in the case of revocation (oh well,
no other servers / ACME clients do that at all anyway).

I guess since that time, the use of CertMagic's exclusive on-demand feature has risen in
popularity. But there is no way to tell, and I had no real way of knowing whether any
significant use of the feature is being had since Caddy has no telemetry. (We used to
have telemetry -- benign, anonymous technical stats to help us understand usage -- but
unfortunately public backlash forced us to end the program.) Based on public feedback
forced by external events, it seems that on-demand TLS deployments are probably rare,
but each of those few deployments actually serve thousands of sites/domains. (The
true importance of this feature would have been clear months ago if Caddy had telemetry,
as Caddy is the primary importer of CertMagic.)

This commit should enable auto-replace for on-demand certificates. It required some
refactoring and some decisions that aren't *entirely* clear are right, but that's how it
goes.

I haven't tested this. (Last time I worked on this feature it took me about 2 days to test properly.)
2022-01-30 21:58:34 -07:00
Matthew Holt
b6b3db32bc
ci: Update Go versions (Go 1.16 required)
We now use the io/fs package.
2022-01-05 17:47:33 -07:00
Matthew Holt
468bfd25e4
Avoid infinite loop in rare cases with stale locks
Should fix caddyserver/caddy#4448

Weaker mutual exclusion guarantees, but probably the better alternative
2022-01-05 17:32:21 -07:00
Simon Kirsten
9b2c6ac884
Fix cacheAlmostFull calculation for unbounded caches (#156) 2021-12-15 10:40:53 -07:00
Basit Ali
924f3153e9
Remove duplicate OnDemand assignment (#153) 2021-11-23 09:48:41 -07:00
Matthew Holt
f83201861a
Update dependencies (fix #152) 2021-11-08 12:57:05 -07:00
Matthew Holt
f3d648273f
Store key before cert in transaction (close #149) 2021-10-30 11:18:47 -06:00
Matthew Holt
1c89882bc5 Move unused test variable 2021-10-27 14:15:20 -06:00
Simão Gomes Viana
30664cac63
Check keyBlockDER for nil (#147)
I got following panic while Caddy was running:

2021/10/26 08:06:34 panic: certificate worker: runtime error: invalid memory address or nil pointer dereference
goroutine 43 [running]:
github.com/caddyserver/certmagic.(*jobManager).worker.func1()
	github.com/caddyserver/certmagic@v0.14.5/async.go:58 +0x65
panic({0x145d400, 0x23d6c50})
	runtime/panic.go:1038 +0x215
github.com/caddyserver/certmagic.decodePrivateKey({0xc000738c00, 0x0, 0x0})
	github.com/caddyserver/certmagic@v0.14.5/crypto.go:75 +0x2a
github.com/caddyserver/certmagic.(*Config).reusePrivateKey(0xc0003b77c0, {0xc0003b1640, 0x32})
	github.com/caddyserver/certmagic@v0.14.5/config.go:602 +0x2b9
github.com/caddyserver/certmagic.(*Config).obtainCert.func2({0x190d3b8, 0xc000655920})
	github.com/caddyserver/certmagic@v0.14.5/config.go:487 +0x1d6
github.com/caddyserver/certmagic.doWithRetry({0x190d310, 0xc0000b0440}, 0xc00003bd40, 0xc0007afba8)
	github.com/caddyserver/certmagic@v0.14.5/async.go:106 +0x1cc
github.com/caddyserver/certmagic.(*Config).obtainCert(0xc0003b77c0, {0x190d310, 0xc0000b0440}, {0xc0003b1640, 0x32}, 0x0)
	github.com/caddyserver/certmagic@v0.14.5/config.go:572 +0x58e
github.com/caddyserver/certmagic.(*Config).ObtainCertAsync(...)
	github.com/caddyserver/certmagic@v0.14.5/config.go:427
github.com/caddyserver/certmagic.(*Config).manageOne.func1()
	github.com/caddyserver/certmagic@v0.14.5/config.go:332 +0x6f
github.com/caddyserver/certmagic.(*jobManager).worker(0x23e0c60)
	github.com/caddyserver/certmagic@v0.14.5/async.go:73 +0x112
created by github.com/caddyserver/certmagic.(*jobManager).Submit
	github.com/caddyserver/certmagic@v0.14.5/async.go:50 +0x288

According to Go documentation: https://pkg.go.dev/encoding/pem#Decode
p can be nil (first parameter returned) and so it should be checked
before continuing as per this example:
https://pkg.go.dev/encoding/pem#example-Decode

I also added a test to verify that the fix works. Running the test
without the fix causes a panic.

Test: go test -count=1 './...'
2021-10-26 14:11:35 -06:00
Matthew Holt
c17cc71228
Extend on-demand timeout to 3 minutes
Motivated by https://caddy.community/t/zerossl-dns-challenge-failing-often-route53-plugin/13822/25?u=matt

This seems excessive but has been confirmed to fix the problem in most cases.
2021-10-06 20:57:44 -06:00
Matthew Holt
6b2f5f9b1b
Update readme
And keep two similar methods next to each other
2021-09-30 13:20:39 -06:00
Matthew Holt
1bbe11e291
Change ManageSync() to take a context (close #146)
This brings it in line with ObtainSync and RenewSync, and makes it consistent with ManageAsync.
2021-09-23 14:35:51 -06:00
Matthew Holt
88b8609b4d
Better fix for #142; patches 335551df 2021-09-22 14:28:00 -06:00
Matthew Holt
d9f599a2f7
Clarify NextProtos usage (#145) 2021-09-20 13:49:38 -06:00
Matthew Holt
047b545304
Adjust ACME client throttle to 10/10s (close #143) 2021-09-16 12:21:57 -06:00
Matthew Holt
b74a9ce808
Add one more debug log to handshake 2021-09-03 11:34:05 -06:00
Matthew Holt
066b08905b
More debug logging (cert selection, handshakes) 2021-08-30 12:54:05 -06:00
Matthew Holt
93c97f917b
Debug logs on cert cache add/remove; fix loop
Remove loop had some fishy behavior in some contrived test cases.
2021-08-30 11:52:13 -06:00
Matt Holt
335551df8f
Load certs from storage if cache is full (#142)
Should fix "no certificate available" errors.

https://caddy.community/t/error-tls-alert-internal-error-592-again/13272/9?u=matt
2021-08-26 14:54:13 -06:00
Matthew Holt
d7578507c8
go.mod: acmez 1.0 (also minor tweaks to readme) 2021-08-26 14:51:56 -06:00
Matthew Holt
b8cb679646
Remove legacy direct cert-matching behavior
This was implemented to solve the TLS-SNI challenge
back in the day, but that has long been decomissioned.
2021-07-16 11:38:42 -06:00
Herman Slatman
3966eeb00d
Fix TLS-ALPN-01 challenge for IP Identifiers (#139)
* Fix TLS-ALPN-01 challenge for IP Identifiers

See #133

* Add tests for challengeKey function

* Add more tests

* Fix PR comments

* Remove deletion of TLS-ALPN-01 challenge certificate
2021-07-15 18:41:44 -06:00
ryankoski
647f27cf26
Add nil checks for log (#135) 2021-06-16 13:16:43 -06:00
Matt Holt
07f7d0dec1
Allow forced renewals; fix renew on OCSP revoke; change key on compromise (#134)
* Begin refactor of ObtainCert and RenewCert to allow force renews

* Don't reuse private key in case of revocation due to key compromise

* Improve logging in renew

* Run OCSP check at start of cache maintenance

Otherwise we wait until first tick (currently 1 hour) which might be too long

* Fix obtain; move some things around

Obtain now tries to reuse private key if exists, but if it doesn't exist, that shouldn't be an error (so we clear the error in that case).

Moved the removal of compromised private keys to have logging make more sense.
2021-06-12 13:47:47 -06:00
Matthew Holt
388f3ed4c5
Log errors between issuer attempts
See https://caddy.community/t/no-certificate-created-for-long-domain-name/12729/7?u=matt
2021-06-09 13:11:36 -06:00
Peter Magnusson
b668c8b9d4
Use the configured resolvers exclusively (#131) (fix #132)
* feature: add optional !important suffix

if !important is added to any of the resolvers, then all are considered
exclusive and no other fallbacks will be added.

* fix: !important can be on it's own

* simplify recursiveNameservers

- use custom OR default nameservers
- add testing

* removed print line

* tests: fixed defaults when resolv.conf is found
2021-06-08 13:14:45 -06:00
Matthew Holt
a1d00125a6
Sync discovered contact email (fix #127) 2021-04-12 11:57:20 -06:00
Mohamed Akram
eba05a818e
go.mod: Update cpuid (#125) 2021-04-05 15:05:24 -06:00
Matthew Holt
14b314f4d6
Increase internal ACME transaction rate limit
From 1 every 6 seconds to 1 every 3 seconds. If this becomes a problem we'll reduce them again.
2021-04-01 12:30:23 -06:00
Marco Argentieri
e4f02628f4
Fix code examples (#123) 2021-03-19 11:51:34 -06:00
Matthew Holt
7550222c4a
Conform to libdns relative record names convention
See https://github.com/libdns/libdns/issues/12
2021-02-24 11:46:02 -07:00
Matthew Holt
6131a445f6
Remove old asset migration code
https://github.com/caddyserver/caddy/issues/3894
2021-02-22 15:17:10 -07:00
Matthew Holt
ebb8d8b435
Return Account from NewAccountFunc
This allows the callback to change the Account.

This useful if, for example, using the callback to set missing contact info on the Account.
2021-02-10 19:00:17 -07:00
Matthew Holt
df1d58e0e1
Improve wildcard matching tests
Honoring RFC 2818
2021-02-10 14:36:08 -07:00
Matthew Holt
d2311e1f3e
Don't maintain on-demand certs in background
On-demand certs are managed at handshake-time. Doing so in the background was
a temporary holdover until on-demand maintenance improved, which it since has.
Since background maintenance did not consult the "ask" endpoint or decision func,
it would sometimes renew certificates that were not desirable to renew.

See https://caddy.community/t/clean-up-caddy-certificates/11429/11?u=matt
2021-02-10 14:35:27 -07:00
Matthew Holt
267fdad76a
Add ClientCredentials() method
CertMagic has always been useful for TLS servers, with its Cache
type, which enables long-term automation of managed certs.
But there has never been a good way to use CertMagic with
client certificates, which can be automated the same way, but
which are used sporadically and instantaneously, rather than
during the long-running lifetime of a server.

This is a simple addition which provides a lot of value, so that
TLS clients can use CertMagic to automate their certificates.
The ClientCredentials() method returns chains of TLS certs that
are ready to be used in tls.Config structs to enable client auth.
2021-01-26 16:01:15 -07:00
Matthew Holt
2d9d97e41a
Better logging when cleaning up locks 2021-01-25 14:46:47 -07:00
Matthew Holt
7271e2f615
Export access to global challenge info cache
Turns out this is needed when solving the HTTP challenge in Caddy, in certain situations.

This does not provide access to challenge info in distributed challenge storage (that would require a Config, and isn't exported anyway since it is handled internally).
2021-01-23 14:28:02 -07:00
Matthew Holt
2a9ad38637
Refactor challenge info / solving
This allows any challenges initiated within the process to be solved by whatever HTTP or TLS server is running, even if they do not know about the challenges themselves.

This is useful when a process has multiple servers running, but only one can solve the challenges (which is often the case, since a socket belongs to one listener at a time) and they do not know about each other or share configs. The trick is to wrap the solvers with a thin wrapper that stores all the challenge info in memory while the challenge is active.

A nice side-effect is I've simplified/unified the code that gets the challenge info when actually solving the challenges.
2021-01-22 17:43:33 -07:00
Matthew Holt
892ccc9af1
Ability to specify ACME account by private key
This makes it possible to use existing ACME account when you have the private key but not the contact information. Often the case when the ACME account is created out-of-band of the ACME client.
2021-01-22 17:26:47 -07:00
Matthew Holt
07dc55f031
Look up account in storage first
This avoids contacting the server if we already have the account info in storage.
2021-01-20 16:01:53 -07:00
Matthew Holt
5259faf208
Support account lookup by private key
This enables adding existing accounts to storage so they
can be used in future ACME transactions, as long as the
private key is possessed.
2021-01-20 14:52:08 -07:00
Matthew Holt
5a926ec14f
Update subject certificate qualifications
I suppose * is a valid subject -- technically -- but it probably won't
be accepted by browsers. They usually only accept wildcards
for subdomains.

Related, but only tangentially:
https://github.com/caddyserver/caddy/issues/3977
2021-01-19 14:53:32 -07:00
Matthew Holt
725b69d53d
Configurable OCSP stapling
Allows disabling it entirely, or overriding responder URLs

See https://github.com/caddyserver/caddy/issues/3714
2021-01-07 15:45:22 -07:00
Matthew Holt
7891c83082
Improve IDN support
Convert IDNs to ASCII when generating CSRs and accessing storage.
2021-01-04 15:42:49 -07:00