2016-08-18 12:20:11 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-05-26 15:03:14 +00:00
|
|
|
"fmt"
|
2016-08-18 12:20:11 +00:00
|
|
|
"reflect"
|
2016-12-14 17:10:10 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-08-18 12:20:11 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-12-30 08:21:13 +00:00
|
|
|
|
|
|
|
"github.com/containous/traefik/log"
|
2018-03-05 19:54:04 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2018-03-26 12:12:03 +00:00
|
|
|
acme "github.com/xenolf/lego/acmev2"
|
2016-08-18 12:20:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Account is used to store lets encrypt registration info
|
|
|
|
type Account struct {
|
|
|
|
Email string
|
|
|
|
Registration *acme.RegistrationResource
|
|
|
|
PrivateKey []byte
|
|
|
|
DomainsCertificate DomainsCertificates
|
2016-09-23 16:27:01 +00:00
|
|
|
ChallengeCerts map[string]*ChallengeCert
|
2018-01-15 15:04:05 +00:00
|
|
|
HTTPChallenge map[string]map[string][]byte
|
2016-09-23 16:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChallengeCert stores a challenge certificate
|
|
|
|
type ChallengeCert struct {
|
|
|
|
Certificate []byte
|
|
|
|
PrivateKey []byte
|
|
|
|
certificate *tls.Certificate
|
2016-08-18 12:20:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 19:54:04 +00:00
|
|
|
// Init account struct
|
2016-08-18 12:20:11 +00:00
|
|
|
func (a *Account) Init() error {
|
|
|
|
err := a.DomainsCertificate.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-23 16:27:01 +00:00
|
|
|
|
|
|
|
for _, cert := range a.ChallengeCerts {
|
|
|
|
if cert.certificate == nil {
|
|
|
|
certificate, err := tls.X509KeyPair(cert.Certificate, cert.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cert.certificate = &certificate
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-09-23 16:27:01 +00:00
|
|
|
if cert.certificate.Leaf == nil {
|
|
|
|
leaf, err := x509.ParseCertificate(cert.certificate.Certificate[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cert.certificate.Leaf = leaf
|
|
|
|
}
|
|
|
|
}
|
2016-08-18 12:20:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:27:01 +00:00
|
|
|
// NewAccount creates an account
|
2018-03-26 12:12:03 +00:00
|
|
|
func NewAccount(email string, certs []*DomainsCertificate) (*Account, error) {
|
2016-08-18 12:20:11 +00:00
|
|
|
// Create a user. New accounts need an email and private key to start
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2018-03-26 12:12:03 +00:00
|
|
|
domainsCerts := DomainsCertificates{Certs: certs}
|
2018-03-05 19:54:04 +00:00
|
|
|
err = domainsCerts.Init()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
return &Account{
|
|
|
|
Email: email,
|
|
|
|
PrivateKey: x509.MarshalPKCS1PrivateKey(privateKey),
|
2016-09-23 16:27:01 +00:00
|
|
|
DomainsCertificate: DomainsCertificates{Certs: domainsCerts.Certs},
|
|
|
|
ChallengeCerts: map[string]*ChallengeCert{}}, nil
|
2016-08-18 12:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmail returns email
|
2016-09-23 16:27:01 +00:00
|
|
|
func (a *Account) GetEmail() string {
|
2016-08-18 12:20:11 +00:00
|
|
|
return a.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRegistration returns lets encrypt registration resource
|
2016-09-23 16:27:01 +00:00
|
|
|
func (a *Account) GetRegistration() *acme.RegistrationResource {
|
2016-08-18 12:20:11 +00:00
|
|
|
return a.Registration
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrivateKey returns private key
|
2016-09-23 16:27:01 +00:00
|
|
|
func (a *Account) GetPrivateKey() crypto.PrivateKey {
|
2016-08-18 12:20:11 +00:00
|
|
|
if privateKey, err := x509.ParsePKCS1PrivateKey(a.PrivateKey); err == nil {
|
|
|
|
return privateKey
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
log.Errorf("Cannot unmarshall private key %+v", a.PrivateKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Certificate is used to store certificate info
|
|
|
|
type Certificate struct {
|
|
|
|
Domain string
|
|
|
|
CertURL string
|
|
|
|
CertStableURL string
|
|
|
|
PrivateKey []byte
|
|
|
|
Certificate []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// DomainsCertificates stores a certificate for multiple domains
|
|
|
|
type DomainsCertificates struct {
|
|
|
|
Certs []*DomainsCertificate
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2016-12-14 17:10:10 +00:00
|
|
|
func (dc *DomainsCertificates) Len() int {
|
|
|
|
return len(dc.Certs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *DomainsCertificates) Swap(i, j int) {
|
|
|
|
dc.Certs[i], dc.Certs[j] = dc.Certs[j], dc.Certs[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *DomainsCertificates) Less(i, j int) bool {
|
|
|
|
if reflect.DeepEqual(dc.Certs[i].Domains, dc.Certs[j].Domains) {
|
|
|
|
return dc.Certs[i].tlsCert.Leaf.NotAfter.After(dc.Certs[j].tlsCert.Leaf.NotAfter)
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-12-14 17:10:10 +00:00
|
|
|
if dc.Certs[i].Domains.Main == dc.Certs[j].Domains.Main {
|
|
|
|
return strings.Join(dc.Certs[i].Domains.SANs, ",") < strings.Join(dc.Certs[j].Domains.SANs, ",")
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-12-14 17:10:10 +00:00
|
|
|
return dc.Certs[i].Domains.Main < dc.Certs[j].Domains.Main
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *DomainsCertificates) removeDuplicates() {
|
|
|
|
sort.Sort(dc)
|
|
|
|
for i := 0; i < len(dc.Certs); i++ {
|
|
|
|
for i2 := i + 1; i2 < len(dc.Certs); i2++ {
|
|
|
|
if reflect.DeepEqual(dc.Certs[i].Domains, dc.Certs[i2].Domains) {
|
|
|
|
// delete
|
2017-02-02 20:07:44 +00:00
|
|
|
log.Warnf("Remove duplicate cert: %+v, expiration :%s", dc.Certs[i2].Domains, dc.Certs[i2].tlsCert.Leaf.NotAfter.String())
|
2016-12-14 17:10:10 +00:00
|
|
|
dc.Certs = append(dc.Certs[:i2], dc.Certs[i2+1:]...)
|
|
|
|
i2--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 19:54:04 +00:00
|
|
|
// Init DomainsCertificates
|
2016-08-18 12:20:11 +00:00
|
|
|
func (dc *DomainsCertificates) Init() error {
|
|
|
|
dc.lock.Lock()
|
|
|
|
defer dc.lock.Unlock()
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
for _, domainsCertificate := range dc.Certs {
|
|
|
|
tlsCert, err := tls.X509KeyPair(domainsCertificate.Certificate.Certificate, domainsCertificate.Certificate.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
domainsCertificate.tlsCert = &tlsCert
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-12-14 17:10:10 +00:00
|
|
|
if domainsCertificate.tlsCert.Leaf == nil {
|
|
|
|
leaf, err := x509.ParseCertificate(domainsCertificate.tlsCert.Certificate[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-12-14 17:10:10 +00:00
|
|
|
domainsCertificate.tlsCert.Leaf = leaf
|
|
|
|
}
|
2016-08-18 12:20:11 +00:00
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-12-14 17:10:10 +00:00
|
|
|
dc.removeDuplicates()
|
2016-08-18 12:20:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-05 19:54:04 +00:00
|
|
|
func (dc *DomainsCertificates) renewCertificates(acmeCert *Certificate, domain types.Domain) error {
|
2016-08-18 12:20:11 +00:00
|
|
|
dc.lock.Lock()
|
|
|
|
defer dc.lock.Unlock()
|
|
|
|
|
|
|
|
for _, domainsCertificate := range dc.Certs {
|
|
|
|
if reflect.DeepEqual(domain, domainsCertificate.Domains) {
|
|
|
|
tlsCert, err := tls.X509KeyPair(acmeCert.Certificate, acmeCert.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
domainsCertificate.Certificate = acmeCert
|
|
|
|
domainsCertificate.tlsCert = &tlsCert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2017-09-13 08:34:04 +00:00
|
|
|
return fmt.Errorf("certificate to renew not found for domain %s", domain.Main)
|
2016-08-18 12:20:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 19:54:04 +00:00
|
|
|
func (dc *DomainsCertificates) addCertificateForDomains(acmeCert *Certificate, domain types.Domain) (*DomainsCertificate, error) {
|
2016-08-18 12:20:11 +00:00
|
|
|
dc.lock.Lock()
|
|
|
|
defer dc.lock.Unlock()
|
|
|
|
|
|
|
|
tlsCert, err := tls.X509KeyPair(acmeCert.Certificate, acmeCert.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
cert := DomainsCertificate{Domains: domain, Certificate: acmeCert, tlsCert: &tlsCert}
|
|
|
|
dc.Certs = append(dc.Certs, &cert)
|
|
|
|
return &cert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *DomainsCertificates) getCertificateForDomain(domainToFind string) (*DomainsCertificate, bool) {
|
|
|
|
dc.lock.RLock()
|
|
|
|
defer dc.lock.RUnlock()
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
for _, domainsCertificate := range dc.Certs {
|
2018-03-26 12:12:03 +00:00
|
|
|
for _, domain := range domainsCertificate.Domains.ToStrArray() {
|
2018-03-27 14:18:03 +00:00
|
|
|
if strings.HasPrefix(domain, "*.") && types.MatchDomain(domainToFind, domain) {
|
|
|
|
return domainsCertificate, true
|
|
|
|
}
|
2016-08-18 12:20:11 +00:00
|
|
|
if domain == domainToFind {
|
|
|
|
return domainsCertificate, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2018-03-05 19:54:04 +00:00
|
|
|
func (dc *DomainsCertificates) exists(domainToFind types.Domain) (*DomainsCertificate, bool) {
|
2016-08-18 12:20:11 +00:00
|
|
|
dc.lock.RLock()
|
|
|
|
defer dc.lock.RUnlock()
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
for _, domainsCertificate := range dc.Certs {
|
|
|
|
if reflect.DeepEqual(domainToFind, domainsCertificate.Domains) {
|
|
|
|
return domainsCertificate, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:38:03 +00:00
|
|
|
func (dc *DomainsCertificates) toDomainsMap() map[string]*tls.Certificate {
|
|
|
|
domainsCertificatesMap := make(map[string]*tls.Certificate)
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2018-02-26 10:38:03 +00:00
|
|
|
for _, domainCertificate := range dc.Certs {
|
|
|
|
certKey := domainCertificate.Domains.Main
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2018-02-26 10:38:03 +00:00
|
|
|
if domainCertificate.Domains.SANs != nil {
|
|
|
|
sort.Strings(domainCertificate.Domains.SANs)
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2018-02-26 10:38:03 +00:00
|
|
|
for _, dnsName := range domainCertificate.Domains.SANs {
|
|
|
|
if dnsName != domainCertificate.Domains.Main {
|
|
|
|
certKey += fmt.Sprintf(",%s", dnsName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
domainsCertificatesMap[certKey] = domainCertificate.tlsCert
|
|
|
|
}
|
|
|
|
return domainsCertificatesMap
|
|
|
|
}
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
// DomainsCertificate contains a certificate for multiple domains
|
|
|
|
type DomainsCertificate struct {
|
2018-03-05 19:54:04 +00:00
|
|
|
Domains types.Domain
|
2016-08-18 12:20:11 +00:00
|
|
|
Certificate *Certificate
|
|
|
|
tlsCert *tls.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *DomainsCertificate) needRenew() bool {
|
|
|
|
for _, c := range dc.tlsCert.Certificate {
|
|
|
|
crt, err := x509.ParseCertificate(c)
|
|
|
|
if err != nil {
|
|
|
|
// If there's an error, we assume the cert is broken, and needs update
|
|
|
|
return true
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2017-01-31 14:28:40 +00:00
|
|
|
// <= 30 days left, renew certificate
|
2017-12-18 08:14:03 +00:00
|
|
|
if crt.NotAfter.Before(time.Now().Add(24 * 30 * time.Hour)) {
|
2016-08-18 12:20:11 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|