2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2019-02-27 18:53:16 +03:00
|
|
|
|
|
|
|
import (
|
2021-01-26 19:44:19 +03:00
|
|
|
"context"
|
2019-02-27 18:53:16 +03:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
2021-10-25 19:27:22 +03:00
|
|
|
"crypto/ed25519"
|
2019-02-27 18:53:16 +03:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-02-19 15:28:06 +03:00
|
|
|
"os"
|
2021-04-21 18:50:33 +03:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-02-27 18:53:16 +03:00
|
|
|
"strings"
|
2020-02-19 15:28:06 +03:00
|
|
|
"sync"
|
2019-02-27 18:53:16 +03:00
|
|
|
"time"
|
|
|
|
|
2022-01-19 20:45:50 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-12-16 20:54:59 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2021-08-03 19:55:04 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-27 18:53:16 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-02-27 18:53:16 +03:00
|
|
|
)
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
var tlsWebHandlersRegistered = false
|
|
|
|
|
|
|
|
// TLSMod - TLS module object
|
|
|
|
type TLSMod struct {
|
|
|
|
certLastMod time.Time // last modification time of the certificate file
|
|
|
|
status tlsConfigStatus
|
2021-08-03 19:55:04 +03:00
|
|
|
confLock sync.Mutex
|
|
|
|
conf tlsConfigSettings
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create TLS module
|
|
|
|
func tlsCreate(conf tlsConfigSettings) *TLSMod {
|
|
|
|
t := &TLSMod{}
|
|
|
|
t.conf = conf
|
|
|
|
if t.conf.Enabled {
|
|
|
|
if !t.load() {
|
2020-05-16 02:02:50 +03:00
|
|
|
// Something is not valid - return an empty TLS config
|
|
|
|
return &TLSMod{conf: tlsConfigSettings{
|
|
|
|
Enabled: conf.Enabled,
|
|
|
|
ServerName: conf.ServerName,
|
|
|
|
PortHTTPS: conf.PortHTTPS,
|
|
|
|
PortDNSOverTLS: conf.PortDNSOverTLS,
|
2020-08-27 15:03:07 +03:00
|
|
|
PortDNSOverQUIC: conf.PortDNSOverQUIC,
|
2021-06-18 18:13:36 +03:00
|
|
|
AllowUnencryptedDoH: conf.AllowUnencryptedDoH,
|
2020-05-16 02:02:50 +03:00
|
|
|
}}
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
|
|
|
t.setCertFileTime()
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TLSMod) load() bool {
|
|
|
|
if !tlsLoadConfig(&t.conf, &t.status) {
|
2020-05-16 01:37:12 +03:00
|
|
|
log.Error("failed to load TLS config: %s", t.status.WarningValidation)
|
2020-02-19 15:28:06 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate current TLS config and update warnings (it could have been loaded from file)
|
|
|
|
data := validateCertificates(string(t.conf.CertificateChainData), string(t.conf.PrivateKeyData), t.conf.ServerName)
|
|
|
|
if !data.ValidPair {
|
2020-05-16 02:02:50 +03:00
|
|
|
log.Error("failed to validate certificate: %s", data.WarningValidation)
|
2020-02-19 15:28:06 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
t.status = data
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - close module
|
|
|
|
func (t *TLSMod) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig - write config
|
|
|
|
func (t *TLSMod) WriteDiskConfig(conf *tlsConfigSettings) {
|
|
|
|
t.confLock.Lock()
|
|
|
|
*conf = t.conf
|
|
|
|
t.confLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TLSMod) setCertFileTime() {
|
|
|
|
if len(t.conf.CertificatePath) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(t.conf.CertificatePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("TLS: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.certLastMod = fi.ModTime().UTC()
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:44:19 +03:00
|
|
|
// Start updates the configuration of TLSMod and starts it.
|
2020-02-19 15:28:06 +03:00
|
|
|
func (t *TLSMod) Start() {
|
|
|
|
if !tlsWebHandlersRegistered {
|
|
|
|
tlsWebHandlersRegistered = true
|
|
|
|
t.registerWebHandlers()
|
|
|
|
}
|
|
|
|
|
|
|
|
t.confLock.Lock()
|
|
|
|
tlsConf := t.conf
|
|
|
|
t.confLock.Unlock()
|
2021-01-26 19:44:19 +03:00
|
|
|
|
|
|
|
// The background context is used because the TLSConfigChanged wraps
|
|
|
|
// context with timeout on its own and shuts down the server, which
|
|
|
|
// handles current request.
|
|
|
|
Context.web.TLSConfigChanged(context.Background(), tlsConf)
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:44:19 +03:00
|
|
|
// Reload updates the configuration of TLSMod and restarts it.
|
2020-02-19 15:28:06 +03:00
|
|
|
func (t *TLSMod) Reload() {
|
|
|
|
t.confLock.Lock()
|
|
|
|
tlsConf := t.conf
|
|
|
|
t.confLock.Unlock()
|
|
|
|
|
|
|
|
if !tlsConf.Enabled || len(tlsConf.CertificatePath) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(tlsConf.CertificatePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("TLS: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fi.ModTime().UTC().Equal(t.certLastMod) {
|
|
|
|
log.Debug("TLS: certificate file isn't modified")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("TLS: certificate file is modified")
|
|
|
|
|
|
|
|
t.confLock.Lock()
|
|
|
|
r := t.load()
|
|
|
|
t.confLock.Unlock()
|
|
|
|
if !r {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.certLastMod = fi.ModTime().UTC()
|
|
|
|
|
|
|
|
_ = reconfigureDNSServer()
|
2020-11-03 17:44:03 +03:00
|
|
|
|
|
|
|
t.confLock.Lock()
|
|
|
|
tlsConf = t.conf
|
|
|
|
t.confLock.Unlock()
|
2021-01-26 19:44:19 +03:00
|
|
|
// The background context is used because the TLSConfigChanged wraps
|
|
|
|
// context with timeout on its own and shuts down the server, which
|
|
|
|
// handles current request.
|
|
|
|
Context.web.TLSConfigChanged(context.Background(), tlsConf)
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:32:52 +03:00
|
|
|
// Set certificate and private key data
|
2020-02-19 15:28:06 +03:00
|
|
|
func tlsLoadConfig(tls *tlsConfigSettings, status *tlsConfigStatus) bool {
|
2019-08-13 12:32:52 +03:00
|
|
|
tls.CertificateChainData = []byte(tls.CertificateChain)
|
|
|
|
tls.PrivateKeyData = []byte(tls.PrivateKey)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if tls.CertificatePath != "" {
|
|
|
|
if tls.CertificateChain != "" {
|
|
|
|
status.WarningValidation = "certificate data and file can't be set together"
|
|
|
|
return false
|
|
|
|
}
|
2021-05-21 14:55:42 +03:00
|
|
|
tls.CertificateChainData, err = os.ReadFile(tls.CertificatePath)
|
2019-08-13 12:32:52 +03:00
|
|
|
if err != nil {
|
|
|
|
status.WarningValidation = err.Error()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
status.ValidCert = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if tls.PrivateKeyPath != "" {
|
|
|
|
if tls.PrivateKey != "" {
|
|
|
|
status.WarningValidation = "private key data and file can't be set together"
|
|
|
|
return false
|
|
|
|
}
|
2021-05-21 14:55:42 +03:00
|
|
|
tls.PrivateKeyData, err = os.ReadFile(tls.PrivateKeyPath)
|
2019-08-13 12:32:52 +03:00
|
|
|
if err != nil {
|
|
|
|
status.WarningValidation = err.Error()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
status.ValidKey = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
type tlsConfigStatus struct {
|
|
|
|
ValidCert bool `json:"valid_cert"` // ValidCert is true if the specified certificates chain is a valid chain of X509 certificates
|
|
|
|
ValidChain bool `json:"valid_chain"` // ValidChain is true if the specified certificates chain is verified and issued by a known CA
|
|
|
|
Subject string `json:"subject,omitempty"` // Subject is the subject of the first certificate in the chain
|
|
|
|
Issuer string `json:"issuer,omitempty"` // Issuer is the issuer of the first certificate in the chain
|
|
|
|
NotBefore time.Time `json:"not_before,omitempty"` // NotBefore is the NotBefore field of the first certificate in the chain
|
|
|
|
NotAfter time.Time `json:"not_after,omitempty"` // NotAfter is the NotAfter field of the first certificate in the chain
|
|
|
|
DNSNames []string `json:"dns_names"` // DNSNames is the value of SubjectAltNames field of the first certificate in the chain
|
|
|
|
|
|
|
|
// key status
|
|
|
|
ValidKey bool `json:"valid_key"` // ValidKey is true if the key is a valid private key
|
|
|
|
KeyType string `json:"key_type,omitempty"` // KeyType is one of RSA or ECDSA
|
|
|
|
|
|
|
|
// is usable? set by validator
|
|
|
|
ValidPair bool `json:"valid_pair"` // ValidPair is true if both certificate and private key are correct
|
|
|
|
|
|
|
|
// warnings
|
|
|
|
WarningValidation string `json:"warning_validation,omitempty"` // WarningValidation is a validation warning message with the issue description
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
// field ordering is important -- yaml fields will mirror ordering from here
|
|
|
|
type tlsConfig struct {
|
2021-08-27 13:42:31 +03:00
|
|
|
tlsConfigStatus `json:",inline"`
|
|
|
|
tlsConfigSettingsExt `json:",inline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// tlsConfigSettingsExt is used to (un)marshal PrivateKeySaved to ensure that
|
|
|
|
// clients don't send and receive previously saved private keys.
|
|
|
|
type tlsConfigSettingsExt struct {
|
2021-08-03 19:55:04 +03:00
|
|
|
tlsConfigSettings `json:",inline"`
|
2021-08-27 13:42:31 +03:00
|
|
|
// If private key saved as a string, we set this flag to true
|
|
|
|
// and omit key from answer.
|
|
|
|
PrivateKeySaved bool `yaml:"-" json:"private_key_saved,inline"`
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
func (t *TLSMod) handleTLSStatus(w http.ResponseWriter, r *http.Request) {
|
2020-02-19 15:28:06 +03:00
|
|
|
t.confLock.Lock()
|
|
|
|
data := tlsConfig{
|
2021-08-27 13:42:31 +03:00
|
|
|
tlsConfigSettingsExt: tlsConfigSettingsExt{
|
|
|
|
tlsConfigSettings: t.conf,
|
|
|
|
},
|
|
|
|
tlsConfigStatus: t.status,
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
|
|
|
t.confLock.Unlock()
|
2021-12-16 20:54:59 +03:00
|
|
|
marshalTLS(w, r, data)
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TLSMod) handleTLSValidate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
setts, err := unmarshalTLS(r)
|
2019-02-27 18:53:16 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
|
|
|
|
|
2019-02-27 18:53:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:42:31 +03:00
|
|
|
if setts.PrivateKeySaved {
|
|
|
|
setts.PrivateKey = t.conf.PrivateKey
|
|
|
|
}
|
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
if setts.Enabled {
|
2022-01-19 20:45:50 +03:00
|
|
|
uc := aghalg.UniqChecker{}
|
2021-12-28 17:00:52 +03:00
|
|
|
addPorts(
|
2022-01-19 20:45:50 +03:00
|
|
|
uc,
|
2021-12-16 20:54:59 +03:00
|
|
|
config.BindPort,
|
|
|
|
config.BetaBindPort,
|
|
|
|
config.DNS.Port,
|
|
|
|
setts.PortHTTPS,
|
|
|
|
setts.PortDNSOverTLS,
|
|
|
|
setts.PortDNSOverQUIC,
|
|
|
|
setts.PortDNSCrypt,
|
2021-12-28 17:00:52 +03:00
|
|
|
)
|
|
|
|
|
2022-01-19 20:45:50 +03:00
|
|
|
err = uc.Validate(aghalg.IntIsBefore)
|
2021-12-28 17:00:52 +03:00
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "validating ports: %s", err)
|
2021-12-16 20:54:59 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
if !WebCheckPortAvailable(setts.PortHTTPS) {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"port %d is not available, cannot enable HTTPS on it",
|
|
|
|
setts.PortHTTPS,
|
|
|
|
)
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
return
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:32:52 +03:00
|
|
|
status := tlsConfigStatus{}
|
2021-08-27 13:42:31 +03:00
|
|
|
if tlsLoadConfig(&setts.tlsConfigSettings, &status) {
|
2020-02-19 15:28:06 +03:00
|
|
|
status = validateCertificates(string(setts.CertificateChainData), string(setts.PrivateKeyData), setts.ServerName)
|
2019-08-13 12:32:52 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
data := tlsConfig{
|
2021-08-27 13:42:31 +03:00
|
|
|
tlsConfigSettingsExt: setts,
|
|
|
|
tlsConfigStatus: status,
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
2021-12-16 20:54:59 +03:00
|
|
|
|
|
|
|
marshalTLS(w, r, data)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 19:55:04 +03:00
|
|
|
func (t *TLSMod) setConfig(newConf tlsConfigSettings, status tlsConfigStatus) (restartHTTPS bool) {
|
|
|
|
t.confLock.Lock()
|
|
|
|
defer t.confLock.Unlock()
|
|
|
|
|
|
|
|
// Reset the DNSCrypt data before comparing, since we currently do not
|
|
|
|
// accept these from the frontend.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Define a custom comparer for dnsforward.TLSConfig.
|
|
|
|
newConf.DNSCryptConfigFile = t.conf.DNSCryptConfigFile
|
|
|
|
newConf.PortDNSCrypt = t.conf.PortDNSCrypt
|
|
|
|
if !cmp.Equal(t.conf, newConf, cmp.AllowUnexported(dnsforward.TLSConfig{})) {
|
|
|
|
log.Info("tls config has changed, restarting https server")
|
|
|
|
restartHTTPS = true
|
|
|
|
} else {
|
|
|
|
log.Info("tls config has not changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: don't do just `t.conf = data` because we must preserve all other members of t.conf
|
|
|
|
t.conf.Enabled = newConf.Enabled
|
|
|
|
t.conf.ServerName = newConf.ServerName
|
|
|
|
t.conf.ForceHTTPS = newConf.ForceHTTPS
|
|
|
|
t.conf.PortHTTPS = newConf.PortHTTPS
|
|
|
|
t.conf.PortDNSOverTLS = newConf.PortDNSOverTLS
|
|
|
|
t.conf.PortDNSOverQUIC = newConf.PortDNSOverQUIC
|
|
|
|
t.conf.CertificateChain = newConf.CertificateChain
|
|
|
|
t.conf.CertificatePath = newConf.CertificatePath
|
|
|
|
t.conf.CertificateChainData = newConf.CertificateChainData
|
|
|
|
t.conf.PrivateKey = newConf.PrivateKey
|
|
|
|
t.conf.PrivateKeyPath = newConf.PrivateKeyPath
|
|
|
|
t.conf.PrivateKeyData = newConf.PrivateKeyData
|
|
|
|
t.status = status
|
|
|
|
|
|
|
|
return restartHTTPS
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
|
2019-02-27 18:53:16 +03:00
|
|
|
data, err := unmarshalTLS(r)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
|
|
|
|
|
2019-02-27 18:53:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:42:31 +03:00
|
|
|
if data.PrivateKeySaved {
|
|
|
|
data.PrivateKey = t.conf.PrivateKey
|
|
|
|
}
|
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
if data.Enabled {
|
2022-01-19 20:45:50 +03:00
|
|
|
uc := aghalg.UniqChecker{}
|
2021-12-28 17:00:52 +03:00
|
|
|
addPorts(
|
2022-01-19 20:45:50 +03:00
|
|
|
uc,
|
2021-12-16 20:54:59 +03:00
|
|
|
config.BindPort,
|
|
|
|
config.BetaBindPort,
|
|
|
|
config.DNS.Port,
|
|
|
|
data.PortHTTPS,
|
|
|
|
data.PortDNSOverTLS,
|
|
|
|
data.PortDNSOverQUIC,
|
|
|
|
data.PortDNSCrypt,
|
2021-12-28 17:00:52 +03:00
|
|
|
)
|
|
|
|
|
2022-01-19 20:45:50 +03:00
|
|
|
err = uc.Validate(aghalg.IntIsBefore)
|
2021-12-28 17:00:52 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(e.burkov): Investigate and perhaps check other ports.
|
2020-02-19 15:28:06 +03:00
|
|
|
if !WebCheckPortAvailable(data.PortHTTPS) {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"port %d is not available, cannot enable HTTPS on it",
|
|
|
|
data.PortHTTPS,
|
|
|
|
)
|
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
return
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:32:52 +03:00
|
|
|
status := tlsConfigStatus{}
|
2021-08-27 13:42:31 +03:00
|
|
|
if !tlsLoadConfig(&data.tlsConfigSettings, &status) {
|
2020-02-19 15:28:06 +03:00
|
|
|
data2 := tlsConfig{
|
2021-08-27 13:42:31 +03:00
|
|
|
tlsConfigSettingsExt: data,
|
|
|
|
tlsConfigStatus: t.status,
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
2021-12-16 20:54:59 +03:00
|
|
|
marshalTLS(w, r, data2)
|
2021-08-03 19:55:04 +03:00
|
|
|
|
2019-08-13 12:32:52 +03:00
|
|
|
return
|
|
|
|
}
|
2021-08-03 19:55:04 +03:00
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
status = validateCertificates(string(data.CertificateChainData), string(data.PrivateKeyData), data.ServerName)
|
2021-07-29 17:40:31 +03:00
|
|
|
|
2021-08-27 13:42:31 +03:00
|
|
|
restartHTTPS := t.setConfig(data.tlsConfigSettings, status)
|
2020-02-19 15:28:06 +03:00
|
|
|
t.setCertFileTime()
|
|
|
|
onConfigModified()
|
2021-08-03 19:55:04 +03:00
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
err = reconfigureDNSServer()
|
2019-02-27 18:53:16 +03:00
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "%s", err)
|
2021-08-03 19:55:04 +03:00
|
|
|
|
2019-02-27 18:53:16 +03:00
|
|
|
return
|
|
|
|
}
|
2021-08-03 19:55:04 +03:00
|
|
|
|
2020-02-19 15:28:06 +03:00
|
|
|
data2 := tlsConfig{
|
2021-08-27 13:42:31 +03:00
|
|
|
tlsConfigSettingsExt: data,
|
|
|
|
tlsConfigStatus: t.status,
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
2021-08-03 19:55:04 +03:00
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
marshalTLS(w, r, data2)
|
2020-04-24 15:50:57 +03:00
|
|
|
if f, ok := w.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
// The background context is used because the TLSConfigChanged wraps context
|
|
|
|
// with timeout on its own and shuts down the server, which handles current
|
|
|
|
// request. It is also should be done in a separate goroutine due to the
|
|
|
|
// same reason.
|
2019-02-27 18:53:16 +03:00
|
|
|
if restartHTTPS {
|
|
|
|
go func() {
|
2021-08-27 13:42:31 +03:00
|
|
|
Context.web.TLSConfigChanged(context.Background(), data.tlsConfigSettings)
|
2019-02-27 18:53:16 +03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 12:15:08 +03:00
|
|
|
func verifyCertChain(data *tlsConfigStatus, certChain, serverName string) error {
|
2019-10-21 16:07:55 +03:00
|
|
|
log.Tracef("TLS: got certificate: %d bytes", len(certChain))
|
2019-02-27 18:53:16 +03:00
|
|
|
|
|
|
|
// now do a more extended validation
|
2020-10-30 19:18:51 +03:00
|
|
|
var certs []*pem.Block // PEM-encoded certificates
|
2019-02-27 18:53:16 +03:00
|
|
|
|
|
|
|
pemblock := []byte(certChain)
|
|
|
|
for {
|
|
|
|
var decoded *pem.Block
|
|
|
|
decoded, pemblock = pem.Decode(pemblock)
|
|
|
|
if decoded == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if decoded.Type == "CERTIFICATE" {
|
|
|
|
certs = append(certs, decoded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var parsedCerts []*x509.Certificate
|
|
|
|
|
|
|
|
for _, cert := range certs {
|
|
|
|
parsed, err := x509.ParseCertificate(cert.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
data.WarningValidation = fmt.Sprintf("Failed to parse certificate: %s", err)
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error(data.WarningValidation)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
parsedCerts = append(parsedCerts, parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parsedCerts) == 0 {
|
2020-11-06 12:15:08 +03:00
|
|
|
data.WarningValidation = "You have specified an empty certificate"
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error(data.WarningValidation)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data.ValidCert = true
|
|
|
|
|
|
|
|
// spew.Dump(parsedCerts)
|
|
|
|
|
|
|
|
opts := x509.VerifyOptions{
|
|
|
|
DNSName: serverName,
|
2020-03-04 15:11:17 +03:00
|
|
|
Roots: Context.tlsRoots,
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("number of certs - %d", len(parsedCerts))
|
|
|
|
if len(parsedCerts) > 1 {
|
|
|
|
// set up an intermediate
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
for _, cert := range parsedCerts[1:] {
|
|
|
|
log.Printf("got an intermediate cert")
|
|
|
|
pool.AddCert(cert)
|
|
|
|
}
|
|
|
|
opts.Intermediates = pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: save it as a warning rather than error it out -- shouldn't be a big problem
|
|
|
|
mainCert := parsedCerts[0]
|
|
|
|
_, err := mainCert.Verify(opts)
|
|
|
|
if err != nil {
|
|
|
|
// let self-signed certs through
|
|
|
|
data.WarningValidation = fmt.Sprintf("Your certificate does not verify: %s", err)
|
|
|
|
} else {
|
|
|
|
data.ValidChain = true
|
|
|
|
}
|
|
|
|
// spew.Dump(chains)
|
|
|
|
|
|
|
|
// update status
|
|
|
|
if mainCert != nil {
|
|
|
|
notAfter := mainCert.NotAfter
|
|
|
|
data.Subject = mainCert.Subject.String()
|
|
|
|
data.Issuer = mainCert.Issuer.String()
|
|
|
|
data.NotAfter = notAfter
|
|
|
|
data.NotBefore = mainCert.NotBefore
|
|
|
|
data.DNSNames = mainCert.DNSNames
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validatePkey(data *tlsConfigStatus, pkey string) error {
|
|
|
|
// now do a more extended validation
|
2020-10-30 19:18:51 +03:00
|
|
|
var key *pem.Block // PEM-encoded certificates
|
2019-02-27 18:53:16 +03:00
|
|
|
|
|
|
|
// go through all pem blocks, but take first valid pem block and drop the rest
|
|
|
|
pemblock := []byte(pkey)
|
|
|
|
for {
|
|
|
|
var decoded *pem.Block
|
|
|
|
decoded, pemblock = pem.Decode(pemblock)
|
|
|
|
if decoded == nil {
|
|
|
|
break
|
|
|
|
}
|
2021-11-01 13:33:12 +03:00
|
|
|
|
2019-02-27 18:53:16 +03:00
|
|
|
if decoded.Type == "PRIVATE KEY" || strings.HasSuffix(decoded.Type, " PRIVATE KEY") {
|
|
|
|
key = decoded
|
2021-11-01 13:33:12 +03:00
|
|
|
|
2019-02-27 18:53:16 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == nil {
|
|
|
|
data.WarningValidation = "No valid keys were found"
|
2021-11-01 13:33:12 +03:00
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error(data.WarningValidation)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the decoded key
|
2021-11-01 13:33:12 +03:00
|
|
|
_, keyType, err := parsePrivateKey(key.Bytes)
|
2019-02-27 18:53:16 +03:00
|
|
|
if err != nil {
|
|
|
|
data.WarningValidation = fmt.Sprintf("Failed to parse private key: %s", err)
|
2021-10-25 19:27:22 +03:00
|
|
|
|
2021-11-01 13:33:12 +03:00
|
|
|
return errors.Error(data.WarningValidation)
|
|
|
|
} else if keyType == keyTypeED25519 {
|
|
|
|
data.WarningValidation = "ED25519 keys are not supported by browsers; " +
|
|
|
|
"did you mean to use X25519 for key exchange?"
|
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
return errors.Error(data.WarningValidation)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data.ValidKey = true
|
2021-11-01 13:33:12 +03:00
|
|
|
data.KeyType = keyType
|
|
|
|
|
2019-02-27 18:53:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 15:28:38 +03:00
|
|
|
// Process certificate data and its private key.
|
|
|
|
// All parameters are optional.
|
|
|
|
// On error, return partially set object
|
|
|
|
// with 'WarningValidation' field containing error description.
|
2019-02-27 18:53:16 +03:00
|
|
|
func validateCertificates(certChain, pkey, serverName string) tlsConfigStatus {
|
|
|
|
var data tlsConfigStatus
|
|
|
|
|
|
|
|
// check only public certificate separately from the key
|
|
|
|
if certChain != "" {
|
|
|
|
if verifyCertChain(&data, certChain, serverName) != nil {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate private key (right now the only validation possible is just parsing it)
|
|
|
|
if pkey != "" {
|
|
|
|
if validatePkey(&data, pkey) != nil {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if both are set, validate both in unison
|
|
|
|
if pkey != "" && certChain != "" {
|
|
|
|
_, err := tls.X509KeyPair([]byte(certChain), []byte(pkey))
|
|
|
|
if err != nil {
|
|
|
|
data.WarningValidation = fmt.Sprintf("Invalid certificate or key: %s", err)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
data.ValidPair = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2021-11-01 13:33:12 +03:00
|
|
|
// Key types.
|
|
|
|
const (
|
|
|
|
keyTypeECDSA = "ECDSA"
|
|
|
|
keyTypeED25519 = "ED25519"
|
|
|
|
keyTypeRSA = "RSA"
|
|
|
|
)
|
|
|
|
|
2019-02-27 18:53:16 +03:00
|
|
|
// Attempt to parse the given private key DER block. OpenSSL 0.9.8 generates
|
|
|
|
// PKCS#1 private keys by default, while OpenSSL 1.0.0 generates PKCS#8 keys.
|
|
|
|
// OpenSSL ecparam generates SEC1 EC private keys for ECDSA. We try all three.
|
2021-10-25 19:27:22 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Find out if this version of parsePrivateKey from the stdlib
|
|
|
|
// is actually necessary.
|
|
|
|
func parsePrivateKey(der []byte) (key crypto.PrivateKey, typ string, err error) {
|
|
|
|
if key, err = x509.ParsePKCS1PrivateKey(der); err == nil {
|
2021-11-01 13:33:12 +03:00
|
|
|
return key, keyTypeRSA, nil
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:27:22 +03:00
|
|
|
if key, err = x509.ParsePKCS8PrivateKey(der); err == nil {
|
2019-02-27 18:53:16 +03:00
|
|
|
switch key := key.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
2021-11-01 13:33:12 +03:00
|
|
|
return key, keyTypeRSA, nil
|
2019-02-27 18:53:16 +03:00
|
|
|
case *ecdsa.PrivateKey:
|
2021-11-01 13:33:12 +03:00
|
|
|
return key, keyTypeECDSA, nil
|
2021-10-25 19:27:22 +03:00
|
|
|
case ed25519.PrivateKey:
|
2021-11-01 13:33:12 +03:00
|
|
|
return key, keyTypeED25519, nil
|
2019-02-27 18:53:16 +03:00
|
|
|
default:
|
2021-10-25 19:27:22 +03:00
|
|
|
return nil, "", fmt.Errorf(
|
|
|
|
"tls: found unknown private key type %T in PKCS#8 wrapping",
|
|
|
|
key,
|
|
|
|
)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:27:22 +03:00
|
|
|
if key, err = x509.ParseECPrivateKey(der); err == nil {
|
2021-11-01 13:33:12 +03:00
|
|
|
return key, keyTypeECDSA, nil
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
return nil, "", errors.Error("tls: failed to parse private key")
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalTLS handles base64-encoded certificates transparently
|
2021-08-27 13:42:31 +03:00
|
|
|
func unmarshalTLS(r *http.Request) (tlsConfigSettingsExt, error) {
|
|
|
|
data := tlsConfigSettingsExt{}
|
2019-02-27 18:53:16 +03:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return data, fmt.Errorf("failed to parse new TLS config json: %w", err)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.CertificateChain != "" {
|
2021-03-12 14:32:08 +03:00
|
|
|
var cert []byte
|
|
|
|
cert, err = base64.StdEncoding.DecodeString(data.CertificateChain)
|
2019-02-27 18:53:16 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return data, fmt.Errorf("failed to base64-decode certificate chain: %w", err)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
2021-03-12 14:32:08 +03:00
|
|
|
|
|
|
|
data.CertificateChain = string(cert)
|
2019-08-13 12:32:52 +03:00
|
|
|
if data.CertificatePath != "" {
|
|
|
|
return data, fmt.Errorf("certificate data and file can't be set together")
|
|
|
|
}
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.PrivateKey != "" {
|
2021-03-12 14:32:08 +03:00
|
|
|
var key []byte
|
|
|
|
key, err = base64.StdEncoding.DecodeString(data.PrivateKey)
|
2019-02-27 18:53:16 +03:00
|
|
|
if err != nil {
|
2020-11-05 15:20:57 +03:00
|
|
|
return data, fmt.Errorf("failed to base64-decode private key: %w", err)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:32:08 +03:00
|
|
|
data.PrivateKey = string(key)
|
2019-08-13 12:32:52 +03:00
|
|
|
if data.PrivateKeyPath != "" {
|
|
|
|
return data, fmt.Errorf("private key data and file can't be set together")
|
|
|
|
}
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
func marshalTLS(w http.ResponseWriter, r *http.Request, data tlsConfig) {
|
2019-02-27 18:53:16 +03:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if data.CertificateChain != "" {
|
|
|
|
encoded := base64.StdEncoding.EncodeToString([]byte(data.CertificateChain))
|
|
|
|
data.CertificateChain = encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.PrivateKey != "" {
|
2021-08-27 13:42:31 +03:00
|
|
|
data.PrivateKeySaved = true
|
|
|
|
data.PrivateKey = ""
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err := json.NewEncoder(w).Encode(data)
|
|
|
|
if err != nil {
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"Failed to marshal json with TLS status: %s",
|
|
|
|
err,
|
|
|
|
)
|
2019-02-27 18:53:16 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-19 15:28:06 +03:00
|
|
|
|
|
|
|
// registerWebHandlers registers HTTP handlers for TLS configuration
|
|
|
|
func (t *TLSMod) registerWebHandlers() {
|
2021-02-04 14:15:34 +03:00
|
|
|
httpRegister(http.MethodGet, "/control/tls/status", t.handleTLSStatus)
|
|
|
|
httpRegister(http.MethodPost, "/control/tls/configure", t.handleTLSConfigure)
|
|
|
|
httpRegister(http.MethodPost, "/control/tls/validate", t.handleTLSValidate)
|
2020-02-19 15:28:06 +03:00
|
|
|
}
|
2021-04-21 18:50:33 +03:00
|
|
|
|
|
|
|
// LoadSystemRootCAs tries to load root certificates from the operating system.
|
|
|
|
// It returns nil in case nothing is found so that that Go.crypto will use it's
|
|
|
|
// default algorithm to find system root CA list.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/internal/issues/1311.
|
|
|
|
func LoadSystemRootCAs() (roots *x509.CertPool) {
|
|
|
|
// TODO(e.burkov): Use build tags instead.
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directories with the system root certificates, which aren't supported
|
|
|
|
// by Go.crypto.
|
|
|
|
dirs := []string{
|
|
|
|
// Entware.
|
|
|
|
"/opt/etc/ssl/certs",
|
|
|
|
}
|
|
|
|
roots = x509.NewCertPool()
|
|
|
|
for _, dir := range dirs {
|
2021-05-21 14:55:42 +03:00
|
|
|
dirEnts, err := os.ReadDir(dir)
|
2021-04-21 18:50:33 +03:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
continue
|
2021-05-21 14:55:42 +03:00
|
|
|
} else if err != nil {
|
2021-04-21 18:50:33 +03:00
|
|
|
log.Error("opening directory: %q: %s", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootsAdded bool
|
2021-05-21 14:55:42 +03:00
|
|
|
for _, de := range dirEnts {
|
2021-04-21 18:50:33 +03:00
|
|
|
var certData []byte
|
2021-05-21 14:55:42 +03:00
|
|
|
certData, err = os.ReadFile(filepath.Join(dir, de.Name()))
|
2021-04-21 18:50:33 +03:00
|
|
|
if err == nil && roots.AppendCertsFromPEM(certData) {
|
|
|
|
rootsAdded = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rootsAdded {
|
|
|
|
return roots
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|