2019-06-30 17:07:58 -05:00
|
|
|
// 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.
|
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
package caddytls
|
|
|
|
|
|
|
|
import (
|
2019-09-24 17:46:39 -05:00
|
|
|
"crypto/x509"
|
2019-04-25 14:54:48 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-09-24 17:46:39 -05:00
|
|
|
"io/ioutil"
|
2019-06-20 21:36:29 -05:00
|
|
|
"net/url"
|
2019-04-26 13:35:39 -05:00
|
|
|
"time"
|
2019-04-25 14:54:48 -05:00
|
|
|
|
2019-07-02 13:37:06 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-09-12 18:31:10 -05:00
|
|
|
"github.com/go-acme/lego/v3/challenge"
|
2019-04-25 14:54:48 -05:00
|
|
|
"github.com/mholt/certmagic"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-21 11:46:35 -05:00
|
|
|
caddy.RegisterModule(ACMEManagerMaker{})
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
|
2019-06-04 23:43:21 -05:00
|
|
|
// ACMEManagerMaker makes an ACME manager
|
|
|
|
// for managing certificates using ACME.
|
|
|
|
// If crafting one manually rather than
|
|
|
|
// through the config-unmarshal process
|
|
|
|
// (provisioning), be sure to call
|
|
|
|
// SetDefaults to ensure sane defaults
|
|
|
|
// after you have configured this struct
|
|
|
|
// to your liking.
|
|
|
|
type ACMEManagerMaker struct {
|
2019-09-24 17:46:39 -05:00
|
|
|
CA string `json:"ca,omitempty"`
|
|
|
|
Email string `json:"email,omitempty"`
|
|
|
|
RenewAhead caddy.Duration `json:"renew_ahead,omitempty"`
|
|
|
|
KeyType string `json:"key_type,omitempty"`
|
|
|
|
ACMETimeout caddy.Duration `json:"acme_timeout,omitempty"`
|
|
|
|
MustStaple bool `json:"must_staple,omitempty"`
|
|
|
|
Challenges ChallengesConfig `json:"challenges,omitempty"`
|
|
|
|
OnDemand bool `json:"on_demand,omitempty"`
|
|
|
|
Storage json.RawMessage `json:"storage,omitempty"`
|
|
|
|
TrustedRootsPEMFiles []string `json:"trusted_roots_pem_files,omitempty"`
|
|
|
|
|
|
|
|
storage certmagic.Storage
|
|
|
|
rootPool *x509.CertPool
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (ACMEManagerMaker) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
Name: "tls.management.acme",
|
|
|
|
New: func() caddy.Module { return new(ACMEManagerMaker) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager is a no-op to satisfy the ManagerMaker interface,
|
2019-06-04 23:43:21 -05:00
|
|
|
// because this manager type is a special case.
|
2019-08-21 11:46:35 -05:00
|
|
|
func (m ACMEManagerMaker) NewManager(interactive bool) (certmagic.Manager, error) {
|
2019-04-26 13:35:39 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-04-25 14:54:48 -05:00
|
|
|
|
2019-06-04 23:43:21 -05:00
|
|
|
// Provision sets up m.
|
2019-06-14 12:58:28 -05:00
|
|
|
func (m *ACMEManagerMaker) Provision(ctx caddy.Context) error {
|
2019-04-25 14:54:48 -05:00
|
|
|
// DNS providers
|
2019-07-18 10:15:23 -05:00
|
|
|
if m.Challenges.DNSRaw != nil {
|
2019-06-04 23:43:21 -05:00
|
|
|
val, err := ctx.LoadModuleInline("provider", "tls.dns", m.Challenges.DNSRaw)
|
2019-04-25 14:54:48 -05:00
|
|
|
if err != nil {
|
2019-07-18 10:15:23 -05:00
|
|
|
return fmt.Errorf("loading DNS provider module: %s", err)
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
2019-06-04 23:43:21 -05:00
|
|
|
m.Challenges.DNS = val.(challenge.Provider)
|
2019-09-14 19:05:45 -05:00
|
|
|
m.Challenges.DNSRaw = nil // allow GC to deallocate
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// policy-specific storage implementation
|
|
|
|
if m.Storage != nil {
|
2019-07-05 10:59:46 -05:00
|
|
|
val, err := ctx.LoadModuleInline("module", "caddy.storage", m.Storage)
|
2019-04-25 14:54:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading TLS storage module: %s", err)
|
|
|
|
}
|
2019-06-14 12:58:28 -05:00
|
|
|
cmStorage, err := val.(caddy.StorageConverter).CertMagicStorage()
|
2019-04-25 14:54:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating TLS storage configuration: %v", err)
|
|
|
|
}
|
|
|
|
m.storage = cmStorage
|
2019-09-14 19:05:45 -05:00
|
|
|
m.Storage = nil // allow GC to deallocate
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
|
2019-09-24 17:46:39 -05:00
|
|
|
// add any custom CAs to trust store
|
|
|
|
if len(m.TrustedRootsPEMFiles) > 0 {
|
|
|
|
m.rootPool = x509.NewCertPool()
|
|
|
|
for _, pemFile := range m.TrustedRootsPEMFiles {
|
|
|
|
pemData, err := ioutil.ReadFile(pemFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading trusted root CA's PEM file: %s: %v", pemFile, err)
|
|
|
|
}
|
|
|
|
if !m.rootPool.AppendCertsFromPEM(pemData) {
|
|
|
|
return fmt.Errorf("unable to add %s to trust pool: %v", pemFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:35:39 -05:00
|
|
|
// makeCertMagicConfig converts m into a certmagic.Config, because
|
|
|
|
// this is a special case where the default manager is the certmagic
|
|
|
|
// Config and not a separate manager.
|
2019-06-14 12:58:28 -05:00
|
|
|
func (m *ACMEManagerMaker) makeCertMagicConfig(ctx caddy.Context) certmagic.Config {
|
2019-04-26 13:35:39 -05:00
|
|
|
storage := m.storage
|
|
|
|
if storage == nil {
|
2019-05-16 17:05:38 -05:00
|
|
|
storage = ctx.Storage()
|
2019-04-26 13:35:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var ond *certmagic.OnDemandConfig
|
2019-06-20 21:36:29 -05:00
|
|
|
if m.OnDemand {
|
|
|
|
var onDemand *OnDemandConfig
|
|
|
|
appVal, err := ctx.App("tls")
|
|
|
|
if err == nil {
|
|
|
|
onDemand = appVal.(*TLS).Automation.OnDemand
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:35:39 -05:00
|
|
|
ond = &certmagic.OnDemandConfig{
|
2019-06-20 21:36:29 -05:00
|
|
|
DecisionFunc: func(name string) error {
|
|
|
|
if onDemand != nil {
|
|
|
|
if onDemand.Ask != "" {
|
|
|
|
err := onDemandAskRequest(onDemand.Ask, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check the rate limiter last, since
|
|
|
|
// even checking consumes a token; so
|
|
|
|
// don't even bother checking if the
|
|
|
|
// other regulations fail anyway
|
|
|
|
if onDemand.RateLimit != nil {
|
|
|
|
if !onDemandRateLimiter.Allow() {
|
|
|
|
return fmt.Errorf("on-demand rate limit exceeded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2019-04-26 13:35:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return certmagic.Config{
|
2019-05-21 15:22:33 -05:00
|
|
|
CA: m.CA,
|
2019-04-26 13:35:39 -05:00
|
|
|
Email: m.Email,
|
|
|
|
Agreed: true,
|
|
|
|
DisableHTTPChallenge: m.Challenges.HTTP.Disabled,
|
|
|
|
DisableTLSALPNChallenge: m.Challenges.TLSALPN.Disabled,
|
|
|
|
RenewDurationBefore: time.Duration(m.RenewAhead),
|
|
|
|
AltHTTPPort: m.Challenges.HTTP.AlternatePort,
|
|
|
|
AltTLSALPNPort: m.Challenges.TLSALPN.AlternatePort,
|
2019-06-04 23:43:21 -05:00
|
|
|
DNSProvider: m.Challenges.DNS,
|
2019-04-26 13:35:39 -05:00
|
|
|
KeyType: supportedCertKeyTypes[m.KeyType],
|
|
|
|
CertObtainTimeout: time.Duration(m.ACMETimeout),
|
|
|
|
OnDemand: ond,
|
|
|
|
MustStaple: m.MustStaple,
|
|
|
|
Storage: storage,
|
2019-09-24 17:46:39 -05:00
|
|
|
TrustedRoots: m.rootPool,
|
2019-04-26 13:35:39 -05:00
|
|
|
// TODO: listenHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 21:36:29 -05:00
|
|
|
// 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 deterine 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
|
|
|
|
}
|
|
|
|
|
2019-06-04 23:43:21 -05:00
|
|
|
// Interface guard
|
2019-08-21 11:46:35 -05:00
|
|
|
var _ ManagerMaker = (*ACMEManagerMaker)(nil)
|