mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 14:13:48 +03:00
66863aad3b
* caddytls: Add support for ZeroSSL; add Caddyfile support for issuers Configuring issuers explicitly in a Caddyfile is not easily compatible with existing ACME-specific parameters such as email or acme_ca which infer the kind of issuer it creates (this is complicated now because the ZeroSSL issuer wraps the ACME issuer)... oh well, we can revisit that later if we need to. New Caddyfile global option: { cert_issuer <name> ... } Or, alternatively, as a tls subdirective: tls { issuer <name> ... } For example, to use ZeroSSL with an API key: { cert_issuser zerossl API_KEY } For now, that still uses ZeroSSL's ACME endpoint; it fetches EAB credentials for you. You can also provide the EAB credentials directly just like any other ACME endpoint: { cert_issuer acme { eab KEY_ID MAC_KEY } } All these examples use the new global option (or tls subdirective). You can still use traditional/existing options with ZeroSSL, since it's just another ACME endpoint: { acme_ca https://acme.zerossl.com/v2/DV90 acme_eab KEY_ID MAC_KEY } That's all there is to it. You just can't mix-and-match acme_* options with cert_issuer, because it becomes confusing/ambiguous/complicated to merge the settings. * Fix broken test This test was asserting buggy behavior, oops - glad this branch both discovers and fixes the bug at the same time! * Fix broken test (post-merge) * Update modules/caddytls/acmeissuer.go Fix godoc comment Co-authored-by: Francis Lavoie <lavofr@gmail.com> * Add support for ZeroSSL's EAB-by-email endpoint Also transform the ACMEIssuer into ZeroSSLIssuer implicitly if set to the ZeroSSL endpoint without EAB (the ZeroSSLIssuer is needed to generate EAB if not already provided); this is now possible with either an API key or an email address. * go.mod: Use latest certmagic, acmez, and x/net * Wrap underlying logic rather than repeating it Oops, duh * Form-encode email info into request body for EAB endpoint Co-authored-by: Francis Lavoie <lavofr@gmail.com>
363 lines
11 KiB
Go
363 lines
11 KiB
Go
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
//
|
|
// 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 caddytls
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/url"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
|
"github.com/caddyserver/certmagic"
|
|
"github.com/mholt/acmez"
|
|
"github.com/mholt/acmez/acme"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterModule(ACMEIssuer{})
|
|
}
|
|
|
|
// ACMEIssuer makes an ACME manager
|
|
// for managing certificates using ACME.
|
|
//
|
|
// TODO: support multiple ACME endpoints (probably
|
|
// requires an array of these structs) - caddy would
|
|
// also have to load certs from the backup CAs if the
|
|
// first one is expired...
|
|
type ACMEIssuer struct {
|
|
// The URL to the CA's ACME directory endpoint.
|
|
CA string `json:"ca,omitempty"`
|
|
|
|
// The URL to the test CA's ACME directory endpoint.
|
|
// This endpoint is only used during retries if there
|
|
// is a failure using the primary CA.
|
|
TestCA string `json:"test_ca,omitempty"`
|
|
|
|
// Your email address, so the CA can contact you if necessary.
|
|
// Not required, but strongly recommended to provide one so
|
|
// you can be reached if there is a problem. Your email is
|
|
// not sent to any Caddy mothership or used for any purpose
|
|
// other than ACME transactions.
|
|
Email string `json:"email,omitempty"`
|
|
|
|
// If using an ACME CA that requires an external account
|
|
// binding, specify the CA-provided credentials here.
|
|
ExternalAccount *acme.EAB `json:"external_account,omitempty"`
|
|
|
|
// Time to wait before timing out an ACME operation.
|
|
ACMETimeout caddy.Duration `json:"acme_timeout,omitempty"`
|
|
|
|
// Configures the various ACME challenge types.
|
|
Challenges *ChallengesConfig `json:"challenges,omitempty"`
|
|
|
|
// An array of files of CA certificates to accept when connecting to the
|
|
// ACME CA. Generally, you should only use this if the ACME CA endpoint
|
|
// is internal or for development/testing purposes.
|
|
TrustedRootsPEMFiles []string `json:"trusted_roots_pem_files,omitempty"`
|
|
|
|
rootPool *x509.CertPool
|
|
template certmagic.ACMEManager
|
|
magic *certmagic.Config
|
|
logger *zap.Logger
|
|
}
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
func (ACMEIssuer) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "tls.issuance.acme",
|
|
New: func() caddy.Module { return new(ACMEIssuer) },
|
|
}
|
|
}
|
|
|
|
// Provision sets up iss.
|
|
func (iss *ACMEIssuer) Provision(ctx caddy.Context) error {
|
|
iss.logger = ctx.Logger(iss)
|
|
|
|
// DNS providers
|
|
if iss.Challenges != nil && iss.Challenges.DNS != nil && iss.Challenges.DNS.ProviderRaw != nil {
|
|
val, err := ctx.LoadModule(iss.Challenges.DNS, "ProviderRaw")
|
|
if err != nil {
|
|
return fmt.Errorf("loading DNS provider module: %v", err)
|
|
}
|
|
|
|
if deprecatedProvider, ok := val.(acmez.Solver); ok {
|
|
// TODO: For a temporary amount of time, we are allowing the use of DNS
|
|
// providers from go-acme/lego since there are so many providers implemented
|
|
// using that API -- they are adapted as an all-in-one Caddy module in this
|
|
// repository: https://github.com/caddy-dns/lego-deprecated - the module is a
|
|
// acmez.Solver type, so we use it directly. The user must set environment
|
|
// variables to configure it. Remove this shim once a sufficient number of
|
|
// DNS providers are implemented for the libdns APIs instead.
|
|
iss.Challenges.DNS.solver = deprecatedProvider
|
|
} else {
|
|
iss.Challenges.DNS.solver = &certmagic.DNS01Solver{
|
|
DNSProvider: val.(certmagic.ACMEDNSProvider),
|
|
TTL: time.Duration(iss.Challenges.DNS.TTL),
|
|
PropagationTimeout: time.Duration(iss.Challenges.DNS.PropagationTimeout),
|
|
}
|
|
}
|
|
}
|
|
|
|
// add any custom CAs to trust store
|
|
if len(iss.TrustedRootsPEMFiles) > 0 {
|
|
iss.rootPool = x509.NewCertPool()
|
|
for _, pemFile := range iss.TrustedRootsPEMFiles {
|
|
pemData, err := ioutil.ReadFile(pemFile)
|
|
if err != nil {
|
|
return fmt.Errorf("loading trusted root CA's PEM file: %s: %v", pemFile, err)
|
|
}
|
|
if !iss.rootPool.AppendCertsFromPEM(pemData) {
|
|
return fmt.Errorf("unable to add %s to trust pool: %v", pemFile, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
var err error
|
|
iss.template, err = iss.makeIssuerTemplate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (iss *ACMEIssuer) makeIssuerTemplate() (certmagic.ACMEManager, error) {
|
|
template := certmagic.ACMEManager{
|
|
CA: iss.CA,
|
|
TestCA: iss.TestCA,
|
|
Email: iss.Email,
|
|
CertObtainTimeout: time.Duration(iss.ACMETimeout),
|
|
TrustedRoots: iss.rootPool,
|
|
ExternalAccount: iss.ExternalAccount,
|
|
Logger: iss.logger,
|
|
}
|
|
|
|
if iss.Challenges != nil {
|
|
if iss.Challenges.HTTP != nil {
|
|
template.DisableHTTPChallenge = iss.Challenges.HTTP.Disabled
|
|
template.AltHTTPPort = iss.Challenges.HTTP.AlternatePort
|
|
}
|
|
if iss.Challenges.TLSALPN != nil {
|
|
template.DisableTLSALPNChallenge = iss.Challenges.TLSALPN.Disabled
|
|
template.AltTLSALPNPort = iss.Challenges.TLSALPN.AlternatePort
|
|
}
|
|
if iss.Challenges.DNS != nil {
|
|
template.DNS01Solver = iss.Challenges.DNS.solver
|
|
}
|
|
template.ListenHost = iss.Challenges.BindHost
|
|
}
|
|
|
|
return template, nil
|
|
}
|
|
|
|
// SetConfig sets the associated certmagic config for this issuer.
|
|
// This is required because ACME needs values from the config in
|
|
// order to solve the challenges during issuance. This implements
|
|
// the ConfigSetter interface.
|
|
func (iss *ACMEIssuer) SetConfig(cfg *certmagic.Config) {
|
|
iss.magic = cfg
|
|
}
|
|
|
|
// TODO: I kind of hate how each call to these methods needs to
|
|
// make a new ACME manager to fill in defaults before using; can
|
|
// we find the right place to do that just once and then re-use?
|
|
|
|
// PreCheck implements the certmagic.PreChecker interface.
|
|
func (iss *ACMEIssuer) PreCheck(ctx context.Context, names []string, interactive bool) error {
|
|
return certmagic.NewACMEManager(iss.magic, iss.template).PreCheck(ctx, names, interactive)
|
|
}
|
|
|
|
// Issue obtains a certificate for the given csr.
|
|
func (iss *ACMEIssuer) Issue(ctx context.Context, csr *x509.CertificateRequest) (*certmagic.IssuedCertificate, error) {
|
|
return certmagic.NewACMEManager(iss.magic, iss.template).Issue(ctx, csr)
|
|
}
|
|
|
|
// IssuerKey returns the unique issuer key for the configured CA endpoint.
|
|
func (iss *ACMEIssuer) IssuerKey() string {
|
|
return certmagic.NewACMEManager(iss.magic, iss.template).IssuerKey()
|
|
}
|
|
|
|
// Revoke revokes the given certificate.
|
|
func (iss *ACMEIssuer) Revoke(ctx context.Context, cert certmagic.CertificateResource, reason int) error {
|
|
return certmagic.NewACMEManager(iss.magic, iss.template).Revoke(ctx, cert, reason)
|
|
}
|
|
|
|
// GetACMEIssuer returns iss. This is useful when other types embed ACMEIssuer, because
|
|
// type-asserting them to *ACMEIssuer will fail, but type-asserting them to an interface
|
|
// with only this method will succeed, and will still allow the embedded ACMEIssuer
|
|
// to be accessed and manipulated.
|
|
func (iss *ACMEIssuer) GetACMEIssuer() *ACMEIssuer { return iss }
|
|
|
|
// UnmarshalCaddyfile deserializes Caddyfile tokens into iss.
|
|
//
|
|
// ... acme {
|
|
// dir <directory_url>
|
|
// test_dir <test_directory_url>
|
|
// email <email>
|
|
// timeout <duration>
|
|
// disable_http_challenge
|
|
// disable_tlsalpn_challenge
|
|
// alt_http_port <port>
|
|
// alt_tlsalpn_port <port>
|
|
// eab <key_id> <mac_key>
|
|
// trusted_roots <pem_files...>
|
|
// }
|
|
//
|
|
func (iss *ACMEIssuer) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
for d.Next() {
|
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
|
switch d.Val() {
|
|
case "dir":
|
|
if !d.AllArgs(&iss.CA) {
|
|
return d.ArgErr()
|
|
}
|
|
|
|
case "test_dir":
|
|
if !d.AllArgs(&iss.TestCA) {
|
|
return d.ArgErr()
|
|
}
|
|
|
|
case "email":
|
|
if !d.AllArgs(&iss.Email) {
|
|
return d.ArgErr()
|
|
}
|
|
|
|
case "timeout":
|
|
var timeoutStr string
|
|
if !d.AllArgs(&timeoutStr) {
|
|
return d.ArgErr()
|
|
}
|
|
timeout, err := caddy.ParseDuration(timeoutStr)
|
|
if err != nil {
|
|
return d.Errf("invalid timeout duration %s: %v", timeoutStr, err)
|
|
}
|
|
iss.ACMETimeout = caddy.Duration(timeout)
|
|
|
|
case "disable_http_challenge":
|
|
if d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
if iss.Challenges == nil {
|
|
iss.Challenges = new(ChallengesConfig)
|
|
}
|
|
if iss.Challenges.HTTP == nil {
|
|
iss.Challenges.HTTP = new(HTTPChallengeConfig)
|
|
}
|
|
iss.Challenges.HTTP.Disabled = true
|
|
|
|
case "disable_tlsalpn_challenge":
|
|
if d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
if iss.Challenges == nil {
|
|
iss.Challenges = new(ChallengesConfig)
|
|
}
|
|
if iss.Challenges.TLSALPN == nil {
|
|
iss.Challenges.TLSALPN = new(TLSALPNChallengeConfig)
|
|
}
|
|
iss.Challenges.TLSALPN.Disabled = true
|
|
|
|
case "alt_http_port":
|
|
if !d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
port, err := strconv.Atoi(d.Val())
|
|
if err != nil {
|
|
return d.Errf("invalid port %s: %v", d.Val(), err)
|
|
}
|
|
if iss.Challenges == nil {
|
|
iss.Challenges = new(ChallengesConfig)
|
|
}
|
|
if iss.Challenges.HTTP == nil {
|
|
iss.Challenges.HTTP = new(HTTPChallengeConfig)
|
|
}
|
|
iss.Challenges.HTTP.AlternatePort = port
|
|
|
|
case "alt_tlsalpn_port":
|
|
if !d.NextArg() {
|
|
return d.ArgErr()
|
|
}
|
|
port, err := strconv.Atoi(d.Val())
|
|
if err != nil {
|
|
return d.Errf("invalid port %s: %v", d.Val(), err)
|
|
}
|
|
if iss.Challenges == nil {
|
|
iss.Challenges = new(ChallengesConfig)
|
|
}
|
|
if iss.Challenges.TLSALPN == nil {
|
|
iss.Challenges.TLSALPN = new(TLSALPNChallengeConfig)
|
|
}
|
|
iss.Challenges.TLSALPN.AlternatePort = port
|
|
|
|
case "eab":
|
|
iss.ExternalAccount = new(acme.EAB)
|
|
if !d.AllArgs(&iss.ExternalAccount.KeyID, &iss.ExternalAccount.MACKey) {
|
|
return d.ArgErr()
|
|
}
|
|
|
|
case "trusted_roots":
|
|
iss.TrustedRootsPEMFiles = d.RemainingArgs()
|
|
|
|
default:
|
|
return d.Errf("unrecognized ACME issuer property: %s", d.Val())
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// onDemandAskRequest makes a request to the ask URL
|
|
// to see if a certificate can be obtained for name.
|
|
// The certificate request should be denied if this
|
|
// returns an error.
|
|
func onDemandAskRequest(ask string, name string) error {
|
|
askURL, err := url.Parse(ask)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing ask URL: %v", err)
|
|
}
|
|
qs := askURL.Query()
|
|
qs.Set("domain", name)
|
|
askURL.RawQuery = qs.Encode()
|
|
|
|
resp, err := onDemandAskClient.Get(askURL.String())
|
|
if err != nil {
|
|
return fmt.Errorf("error checking %v to determine if certificate for hostname '%s' should be allowed: %v",
|
|
ask, name, err)
|
|
}
|
|
resp.Body.Close()
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
return fmt.Errorf("certificate for hostname '%s' not allowed; non-2xx status code %d returned from %v",
|
|
name, resp.StatusCode, ask)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Interface guards
|
|
var (
|
|
_ certmagic.PreChecker = (*ACMEIssuer)(nil)
|
|
_ certmagic.Issuer = (*ACMEIssuer)(nil)
|
|
_ certmagic.Revoker = (*ACMEIssuer)(nil)
|
|
_ caddy.Provisioner = (*ACMEIssuer)(nil)
|
|
_ ConfigSetter = (*ACMEIssuer)(nil)
|
|
_ caddyfile.Unmarshaler = (*ACMEIssuer)(nil)
|
|
)
|