2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2020-12-28 18:48:23 +00:00
|
|
|
"crypto/x509"
|
2021-01-03 16:03:04 +00:00
|
|
|
"crypto/x509/pkix"
|
2020-12-28 18:48:23 +00:00
|
|
|
"fmt"
|
2021-02-25 20:53:04 +00:00
|
|
|
"os"
|
2020-12-28 18:48:23 +00:00
|
|
|
"path/filepath"
|
2020-07-24 21:39:38 +00:00
|
|
|
"sync"
|
2021-01-03 16:03:04 +00:00
|
|
|
"time"
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CertManager defines a TLS certificate manager
|
|
|
|
type CertManager struct {
|
2021-01-03 16:03:04 +00:00
|
|
|
certPath string
|
|
|
|
keyPath string
|
|
|
|
configDir string
|
|
|
|
logSender string
|
2020-07-24 21:39:38 +00:00
|
|
|
sync.RWMutex
|
2021-01-03 16:03:04 +00:00
|
|
|
caCertificates []string
|
|
|
|
caRevocationLists []string
|
|
|
|
cert *tls.Certificate
|
|
|
|
rootCAs *x509.CertPool
|
|
|
|
crls []*pkix.CertificateList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload tries to reload certificate and CRLs
|
|
|
|
func (m *CertManager) Reload() error {
|
|
|
|
errCrt := m.loadCertificate()
|
|
|
|
errCRLs := m.LoadCRLs()
|
|
|
|
|
|
|
|
if errCrt != nil {
|
|
|
|
return errCrt
|
|
|
|
}
|
|
|
|
return errCRLs
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadCertificate loads the configured x509 key pair
|
2021-01-03 16:03:04 +00:00
|
|
|
func (m *CertManager) loadCertificate() error {
|
2020-07-24 21:39:38 +00:00
|
|
|
newCert, err := tls.LoadX509KeyPair(m.certPath, m.keyPath)
|
|
|
|
if err != nil {
|
2021-01-03 16:03:04 +00:00
|
|
|
logger.Warn(m.logSender, "", "unable to load X509 key pair, cert file %#v key file %#v error: %v",
|
2020-07-24 21:39:38 +00:00
|
|
|
m.certPath, m.keyPath, err)
|
|
|
|
return err
|
|
|
|
}
|
2021-01-03 16:03:04 +00:00
|
|
|
logger.Debug(m.logSender, "", "TLS certificate %#v successfully loaded", m.certPath)
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2021-01-03 16:03:04 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
m.cert = &newCert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCertificateFunc returns the loaded certificate
|
|
|
|
func (m *CertManager) GetCertificateFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
2021-01-03 16:03:04 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
return m.cert, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:03:04 +00:00
|
|
|
// IsRevoked returns true if the specified certificate has been revoked
|
|
|
|
func (m *CertManager) IsRevoked(crt *x509.Certificate, caCrt *x509.Certificate) bool {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
if crt == nil || caCrt == nil {
|
|
|
|
logger.Warn(m.logSender, "", "unable to verify crt %v ca crt %v", crt, caCrt)
|
|
|
|
return len(m.crls) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, crl := range m.crls {
|
|
|
|
if !crl.HasExpired(time.Now()) && caCrt.CheckCRLSignature(crl) == nil {
|
|
|
|
for _, rc := range crl.TBSCertList.RevokedCertificates {
|
|
|
|
if rc.SerialNumber.Cmp(crt.SerialNumber) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadCRLs tries to load certificate revocation lists from the given paths
|
|
|
|
func (m *CertManager) LoadCRLs() error {
|
|
|
|
if len(m.caRevocationLists) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var crls []*pkix.CertificateList
|
|
|
|
|
|
|
|
for _, revocationList := range m.caRevocationLists {
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsFileInputValid(revocationList) {
|
2021-01-03 16:03:04 +00:00
|
|
|
return fmt.Errorf("invalid root CA revocation list %#v", revocationList)
|
|
|
|
}
|
|
|
|
if revocationList != "" && !filepath.IsAbs(revocationList) {
|
|
|
|
revocationList = filepath.Join(m.configDir, revocationList)
|
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
crlBytes, err := os.ReadFile(revocationList)
|
2021-01-03 16:03:04 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn(m.logSender, "unable to read revocation list %#v", revocationList)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
crl, err := x509.ParseCRL(crlBytes)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(m.logSender, "unable to parse revocation list %#v", revocationList)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug(m.logSender, "", "CRL %#v successfully loaded", revocationList)
|
|
|
|
crls = append(crls, crl)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
m.crls = crls
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:48:23 +00:00
|
|
|
// GetRootCAs returns the set of root certificate authorities that servers
|
|
|
|
// use if required to verify a client certificate
|
|
|
|
func (m *CertManager) GetRootCAs() *x509.CertPool {
|
2021-01-03 16:03:04 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2020-12-28 18:48:23 +00:00
|
|
|
return m.rootCAs
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadRootCAs tries to load root CA certificate authorities from the given paths
|
2021-01-03 16:03:04 +00:00
|
|
|
func (m *CertManager) LoadRootCAs() error {
|
|
|
|
if len(m.caCertificates) == 0 {
|
2020-12-28 18:48:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCAs := x509.NewCertPool()
|
|
|
|
|
2021-01-03 16:03:04 +00:00
|
|
|
for _, rootCA := range m.caCertificates {
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsFileInputValid(rootCA) {
|
2020-12-28 18:48:23 +00:00
|
|
|
return fmt.Errorf("invalid root CA certificate %#v", rootCA)
|
|
|
|
}
|
|
|
|
if rootCA != "" && !filepath.IsAbs(rootCA) {
|
2021-01-03 16:03:04 +00:00
|
|
|
rootCA = filepath.Join(m.configDir, rootCA)
|
2020-12-28 18:48:23 +00:00
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
crt, err := os.ReadFile(rootCA)
|
2020-12-28 18:48:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rootCAs.AppendCertsFromPEM(crt) {
|
2021-01-03 16:03:04 +00:00
|
|
|
logger.Debug(m.logSender, "", "TLS certificate authority %#v successfully loaded", rootCA)
|
2020-12-28 18:48:23 +00:00
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("unable to load TLS certificate authority %#v", rootCA)
|
2021-01-03 16:03:04 +00:00
|
|
|
logger.Warn(m.logSender, "", "%v", err)
|
2020-12-28 18:48:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:03:04 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-12-28 18:48:23 +00:00
|
|
|
m.rootCAs = rootCAs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:50:22 +00:00
|
|
|
// SetCACertificates sets the root CA authorities file paths.
|
|
|
|
// This should not be changed at runtime
|
2021-01-03 16:03:04 +00:00
|
|
|
func (m *CertManager) SetCACertificates(caCertificates []string) {
|
|
|
|
m.caCertificates = caCertificates
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:50:22 +00:00
|
|
|
// SetCARevocationLists sets the CA revocation lists file paths.
|
|
|
|
// This should not be changed at runtime
|
2021-01-03 16:03:04 +00:00
|
|
|
func (m *CertManager) SetCARevocationLists(caRevocationLists []string) {
|
|
|
|
m.caRevocationLists = caRevocationLists
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// NewCertManager creates a new certificate manager
|
2021-01-03 16:03:04 +00:00
|
|
|
func NewCertManager(certificateFile, certificateKeyFile, configDir, logSender string) (*CertManager, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
manager := &CertManager{
|
2021-01-03 16:03:04 +00:00
|
|
|
cert: nil,
|
|
|
|
certPath: certificateFile,
|
|
|
|
keyPath: certificateKeyFile,
|
|
|
|
configDir: configDir,
|
|
|
|
logSender: logSender,
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-01-03 16:03:04 +00:00
|
|
|
err := manager.loadCertificate()
|
2020-07-24 21:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return manager, nil
|
|
|
|
}
|