2019-06-10 09:33:19 +01:00
|
|
|
package home
|
2019-02-27 15:53:16 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-26 16:44:19 +00:00
|
|
|
"context"
|
2019-02-27 15:53:16 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
2021-10-25 17:27:22 +01:00
|
|
|
"crypto/ed25519"
|
2019-02-27 15:53:16 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-02-19 12:28:06 +00:00
|
|
|
"os"
|
2019-02-27 15:53:16 +00:00
|
|
|
"strings"
|
2020-02-19 12:28:06 +00:00
|
|
|
"sync"
|
2019-02-27 15:53:16 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-19 17:45:50 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-12-16 17:54:59 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2022-11-02 11:37:41 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
2021-08-03 17:55:04 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-05-24 15:28:11 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-02-27 15:53:16 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-07-29 15:40:31 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-02-27 15:53:16 +00:00
|
|
|
)
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// tlsManager contains the current configuration and state of AdGuard Home TLS
|
|
|
|
// encryption.
|
|
|
|
type tlsManager struct {
|
|
|
|
// status is the current status of the configuration. It is never nil.
|
|
|
|
status *tlsConfigStatus
|
2020-02-19 12:28:06 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// certLastMod is the last modification time of the certificate file.
|
|
|
|
certLastMod time.Time
|
|
|
|
|
|
|
|
confLock sync.Mutex
|
|
|
|
conf tlsConfigSettings
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 12:41:54 +00:00
|
|
|
// newTLSManager initializes the manager of TLS configuration. m is always
|
|
|
|
// non-nil while any returned error indicates that the TLS configuration isn't
|
|
|
|
// valid. Thus TLS may be initialized later, e.g. via the web UI.
|
2022-10-14 17:37:14 +01:00
|
|
|
func newTLSManager(conf tlsConfigSettings) (m *tlsManager, err error) {
|
|
|
|
m = &tlsManager{
|
|
|
|
status: &tlsConfigStatus{},
|
|
|
|
conf: conf,
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
if m.conf.Enabled {
|
|
|
|
err = m.load()
|
|
|
|
if err != nil {
|
2022-11-25 12:41:54 +00:00
|
|
|
m.conf.Enabled = false
|
|
|
|
|
|
|
|
return m, err
|
2022-10-14 17:37:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m.setCertFileTime()
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
return m, nil
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// load reloads the TLS configuration from files or data from the config file.
|
|
|
|
func (m *tlsManager) load() (err error) {
|
|
|
|
err = loadTLSConf(&m.conf, m.status)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDiskConfig - write config
|
2022-10-14 17:37:14 +01:00
|
|
|
func (m *tlsManager) WriteDiskConfig(conf *tlsConfigSettings) {
|
|
|
|
m.confLock.Lock()
|
|
|
|
*conf = m.conf
|
|
|
|
m.confLock.Unlock()
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// setCertFileTime sets t.certLastMod from the certificate. If there are
|
|
|
|
// errors, setCertFileTime logs them.
|
|
|
|
func (m *tlsManager) setCertFileTime() {
|
|
|
|
if len(m.conf.CertificatePath) == 0 {
|
2020-02-19 12:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
fi, err := os.Stat(m.conf.CertificatePath)
|
2020-02-19 12:28:06 +00:00
|
|
|
if err != nil {
|
2022-10-14 17:37:14 +01:00
|
|
|
log.Error("tls: looking up certificate path: %s", err)
|
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
m.certLastMod = fi.ModTime().UTC()
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// start updates the configuration of t and starts it.
|
|
|
|
func (m *tlsManager) start() {
|
|
|
|
m.registerWebHandlers()
|
2020-02-19 12:28:06 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
m.confLock.Lock()
|
|
|
|
tlsConf := m.conf
|
|
|
|
m.confLock.Unlock()
|
2021-01-26 16:44:19 +00:00
|
|
|
|
2022-10-14 17:37:14 +01: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.
|
2021-01-26 16:44:19 +00:00
|
|
|
Context.web.TLSConfigChanged(context.Background(), tlsConf)
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// reload updates the configuration and restarts t.
|
|
|
|
func (m *tlsManager) reload() {
|
|
|
|
m.confLock.Lock()
|
|
|
|
tlsConf := m.conf
|
|
|
|
m.confLock.Unlock()
|
2020-02-19 12:28:06 +00:00
|
|
|
|
|
|
|
if !tlsConf.Enabled || len(tlsConf.CertificatePath) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
fi, err := os.Stat(tlsConf.CertificatePath)
|
|
|
|
if err != nil {
|
2022-10-14 17:37:14 +01:00
|
|
|
log.Error("tls: %s", err)
|
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
if fi.ModTime().UTC().Equal(m.certLastMod) {
|
|
|
|
log.Debug("tls: certificate file isn't modified")
|
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
log.Debug("tls: certificate file is modified")
|
|
|
|
|
|
|
|
m.confLock.Lock()
|
|
|
|
err = m.load()
|
|
|
|
m.confLock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("tls: reloading: %s", err)
|
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
m.certLastMod = fi.ModTime().UTC()
|
2020-02-19 12:28:06 +00:00
|
|
|
|
|
|
|
_ = reconfigureDNSServer()
|
2020-11-03 14:44:03 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
m.confLock.Lock()
|
|
|
|
tlsConf = m.conf
|
|
|
|
m.confLock.Unlock()
|
|
|
|
|
|
|
|
// The background context is used because the TLSConfigChanged wraps context
|
|
|
|
// with timeout on its own and shuts down the server, which handles current
|
|
|
|
// request.
|
2021-01-26 16:44:19 +00:00
|
|
|
Context.web.TLSConfigChanged(context.Background(), tlsConf)
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// loadTLSConf loads and validates the TLS configuration. The returned error is
|
|
|
|
// also set in status.WarningValidation.
|
|
|
|
func loadTLSConf(tlsConf *tlsConfigSettings, status *tlsConfigStatus) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
status.WarningValidation = err.Error()
|
2022-11-01 10:09:52 +00:00
|
|
|
if status.ValidCert && status.ValidKey && status.ValidPair {
|
|
|
|
// Do not return warnings since those aren't critical.
|
|
|
|
err = nil
|
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
}
|
|
|
|
}()
|
2019-08-13 10:32:52 +01:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
tlsConf.CertificateChainData = []byte(tlsConf.CertificateChain)
|
|
|
|
tlsConf.PrivateKeyData = []byte(tlsConf.PrivateKey)
|
|
|
|
|
|
|
|
if tlsConf.CertificatePath != "" {
|
|
|
|
if tlsConf.CertificateChain != "" {
|
|
|
|
return errors.Error("certificate data and file can't be set together")
|
2019-08-13 10:32:52 +01:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
tlsConf.CertificateChainData, err = os.ReadFile(tlsConf.CertificatePath)
|
2019-08-13 10:32:52 +01:00
|
|
|
if err != nil {
|
2022-10-14 17:37:14 +01:00
|
|
|
return fmt.Errorf("reading cert file: %w", err)
|
2019-08-13 10:32:52 +01:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
// Set status.ValidCert to true to signal the frontend that the
|
|
|
|
// certificate opens successfully while the private key can't be opened.
|
2019-08-13 10:32:52 +01:00
|
|
|
status.ValidCert = true
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
if tlsConf.PrivateKeyPath != "" {
|
|
|
|
if tlsConf.PrivateKey != "" {
|
|
|
|
return errors.Error("private key data and file can't be set together")
|
2019-08-13 10:32:52 +01:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
tlsConf.PrivateKeyData, err = os.ReadFile(tlsConf.PrivateKeyPath)
|
2019-08-13 10:32:52 +01:00
|
|
|
if err != nil {
|
2022-10-14 17:37:14 +01:00
|
|
|
return fmt.Errorf("reading key file: %w", err)
|
2019-08-13 10:32:52 +01:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
2019-08-13 10:32:52 +01:00
|
|
|
status.ValidKey = true
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
err = validateCertificates(
|
|
|
|
status,
|
|
|
|
tlsConf.CertificateChainData,
|
|
|
|
tlsConf.PrivateKeyData,
|
|
|
|
tlsConf.ServerName,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating certificate pair: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-08-13 10:32:52 +01:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// tlsConfigStatus contains the status of a certificate chain and key pair.
|
2020-02-19 12:28:06 +00:00
|
|
|
type tlsConfigStatus struct {
|
2022-10-14 17:37:14 +01:00
|
|
|
// Subject is the subject of the first certificate in the chain.
|
|
|
|
Subject string `json:"subject,omitempty"`
|
|
|
|
|
|
|
|
// Issuer is the issuer of the first certificate in the chain.
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
|
|
|
|
// KeyType is the type of the private key.
|
|
|
|
KeyType string `json:"key_type,omitempty"`
|
|
|
|
|
|
|
|
// NotBefore is the NotBefore field of the first certificate in the chain.
|
|
|
|
NotBefore time.Time `json:"not_before,omitempty"`
|
|
|
|
|
|
|
|
// NotAfter is the NotAfter field of the first certificate in the chain.
|
|
|
|
NotAfter time.Time `json:"not_after,omitempty"`
|
|
|
|
|
|
|
|
// WarningValidation is a validation warning message with the issue
|
|
|
|
// description.
|
|
|
|
WarningValidation string `json:"warning_validation,omitempty"`
|
|
|
|
|
|
|
|
// DNSNames is the value of SubjectAltNames field of the first certificate
|
|
|
|
// in the chain.
|
|
|
|
DNSNames []string `json:"dns_names"`
|
|
|
|
|
|
|
|
// ValidCert is true if the specified certificate chain is a valid chain of
|
|
|
|
// X509 certificates.
|
|
|
|
ValidCert bool `json:"valid_cert"`
|
|
|
|
|
|
|
|
// ValidChain is true if the specified certificate chain is verified and
|
|
|
|
// issued by a known CA.
|
|
|
|
ValidChain bool `json:"valid_chain"`
|
|
|
|
|
|
|
|
// ValidKey is true if the key is a valid private key.
|
|
|
|
ValidKey bool `json:"valid_key"`
|
|
|
|
|
|
|
|
// ValidPair is true if both certificate and private key are correct for
|
|
|
|
// each other.
|
|
|
|
ValidPair bool `json:"valid_pair"`
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// tlsConfig is the TLS configuration and status response.
|
2020-02-19 12:28:06 +00:00
|
|
|
type tlsConfig struct {
|
2022-10-14 17:37:14 +01:00
|
|
|
*tlsConfigStatus `json:",inline"`
|
2021-08-27 11:42:31 +01:00
|
|
|
tlsConfigSettingsExt `json:",inline"`
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// tlsConfigSettingsExt is used to (un)marshal the PrivateKeySaved field to
|
|
|
|
// ensure that clients don't send and receive previously saved private keys.
|
2021-08-27 11:42:31 +01:00
|
|
|
type tlsConfigSettingsExt struct {
|
2021-08-03 17:55:04 +01:00
|
|
|
tlsConfigSettings `json:",inline"`
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
// PrivateKeySaved is true if the private key is saved as a string and omit
|
|
|
|
// key from answer.
|
2021-08-27 11:42:31 +01:00
|
|
|
PrivateKeySaved bool `yaml:"-" json:"private_key_saved,inline"`
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
func (m *tlsManager) handleTLSStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m.confLock.Lock()
|
2020-02-19 12:28:06 +00:00
|
|
|
data := tlsConfig{
|
2021-08-27 11:42:31 +01:00
|
|
|
tlsConfigSettingsExt: tlsConfigSettingsExt{
|
2022-10-14 17:37:14 +01:00
|
|
|
tlsConfigSettings: m.conf,
|
2021-08-27 11:42:31 +01:00
|
|
|
},
|
2022-10-14 17:37:14 +01:00
|
|
|
tlsConfigStatus: m.status,
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
m.confLock.Unlock()
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
marshalTLS(w, r, data)
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
func (m *tlsManager) handleTLSValidate(w http.ResponseWriter, r *http.Request) {
|
2020-02-19 12:28:06 +00:00
|
|
|
setts, err := unmarshalTLS(r)
|
2019-02-27 15:53:16 +00:00
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
|
|
|
|
|
2019-02-27 15:53:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-27 11:42:31 +01:00
|
|
|
if setts.PrivateKeySaved {
|
2022-10-14 17:37:14 +01:00
|
|
|
setts.PrivateKey = m.conf.PrivateKey
|
2021-08-27 11:42:31 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
if setts.Enabled {
|
2022-08-03 12:36:18 +01:00
|
|
|
err = validatePorts(
|
2022-03-23 13:00:32 +00:00
|
|
|
tcpPort(config.BindPort),
|
|
|
|
tcpPort(setts.PortHTTPS),
|
|
|
|
tcpPort(setts.PortDNSOverTLS),
|
|
|
|
tcpPort(setts.PortDNSCrypt),
|
2022-08-03 12:36:18 +01:00
|
|
|
udpPort(config.DNS.Port),
|
|
|
|
udpPort(setts.PortDNSOverQUIC),
|
2021-12-28 14:00:52 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-08-03 12:36:18 +01:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
2021-12-16 17:54:59 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:08:05 +01:00
|
|
|
if !webCheckPortAvailable(setts.PortHTTPS) {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"port %d is not available, cannot enable HTTPS on it",
|
|
|
|
setts.PortHTTPS,
|
|
|
|
)
|
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// Skip the error check, since we are only interested in the value of
|
|
|
|
// status.WarningValidation.
|
|
|
|
status := &tlsConfigStatus{}
|
|
|
|
_ = loadTLSConf(&setts.tlsConfigSettings, status)
|
|
|
|
resp := tlsConfig{
|
2021-08-27 11:42:31 +01:00
|
|
|
tlsConfigSettingsExt: setts,
|
|
|
|
tlsConfigStatus: status,
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
2021-12-16 17:54:59 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
marshalTLS(w, r, resp)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
func (m *tlsManager) setConfig(newConf tlsConfigSettings, status *tlsConfigStatus) (restartHTTPS bool) {
|
|
|
|
m.confLock.Lock()
|
|
|
|
defer m.confLock.Unlock()
|
2021-08-03 17:55:04 +01:00
|
|
|
|
|
|
|
// 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.
|
2022-10-14 17:37:14 +01:00
|
|
|
newConf.DNSCryptConfigFile = m.conf.DNSCryptConfigFile
|
|
|
|
newConf.PortDNSCrypt = m.conf.PortDNSCrypt
|
|
|
|
if !cmp.Equal(m.conf, newConf, cmp.AllowUnexported(dnsforward.TLSConfig{})) {
|
2021-08-03 17:55:04 +01:00
|
|
|
log.Info("tls config has changed, restarting https server")
|
|
|
|
restartHTTPS = true
|
|
|
|
} else {
|
2022-10-14 17:37:14 +01:00
|
|
|
log.Info("tls: config has not changed")
|
2021-08-03 17:55:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: don't do just `t.conf = data` because we must preserve all other members of t.conf
|
2022-10-14 17:37:14 +01:00
|
|
|
m.conf.Enabled = newConf.Enabled
|
|
|
|
m.conf.ServerName = newConf.ServerName
|
|
|
|
m.conf.ForceHTTPS = newConf.ForceHTTPS
|
|
|
|
m.conf.PortHTTPS = newConf.PortHTTPS
|
|
|
|
m.conf.PortDNSOverTLS = newConf.PortDNSOverTLS
|
|
|
|
m.conf.PortDNSOverQUIC = newConf.PortDNSOverQUIC
|
|
|
|
m.conf.CertificateChain = newConf.CertificateChain
|
|
|
|
m.conf.CertificatePath = newConf.CertificatePath
|
|
|
|
m.conf.CertificateChainData = newConf.CertificateChainData
|
|
|
|
m.conf.PrivateKey = newConf.PrivateKey
|
|
|
|
m.conf.PrivateKeyPath = newConf.PrivateKeyPath
|
|
|
|
m.conf.PrivateKeyData = newConf.PrivateKeyData
|
|
|
|
m.status = status
|
2021-08-03 17:55:04 +01:00
|
|
|
|
|
|
|
return restartHTTPS
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
func (m *tlsManager) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req, err := unmarshalTLS(r)
|
2019-02-27 15:53:16 +00:00
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
|
|
|
|
|
2019-02-27 15:53:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
if req.PrivateKeySaved {
|
|
|
|
req.PrivateKey = m.conf.PrivateKey
|
2021-08-27 11:42:31 +01:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
if req.Enabled {
|
2022-08-03 12:36:18 +01:00
|
|
|
err = validatePorts(
|
2022-03-23 13:00:32 +00:00
|
|
|
tcpPort(config.BindPort),
|
2022-10-14 17:37:14 +01:00
|
|
|
tcpPort(req.PortHTTPS),
|
|
|
|
tcpPort(req.PortDNSOverTLS),
|
|
|
|
tcpPort(req.PortDNSCrypt),
|
2022-08-03 12:36:18 +01:00
|
|
|
udpPort(config.DNS.Port),
|
2022-10-14 17:37:14 +01:00
|
|
|
udpPort(req.PortDNSOverQUIC),
|
2021-12-28 14:00:52 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(e.burkov): Investigate and perhaps check other ports.
|
2022-10-14 17:37:14 +01:00
|
|
|
if !webCheckPortAvailable(req.PortHTTPS) {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(
|
|
|
|
r,
|
|
|
|
w,
|
|
|
|
http.StatusBadRequest,
|
2022-10-14 17:37:14 +01:00
|
|
|
"port %d is not available, cannot enable https on it",
|
|
|
|
req.PortHTTPS,
|
2021-12-16 17:54:59 +00:00
|
|
|
)
|
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
status := &tlsConfigStatus{}
|
|
|
|
err = loadTLSConf(&req.tlsConfigSettings, status)
|
|
|
|
if err != nil {
|
|
|
|
resp := tlsConfig{
|
|
|
|
tlsConfigSettingsExt: req,
|
|
|
|
tlsConfigStatus: status,
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
marshalTLS(w, r, resp)
|
2021-08-03 17:55:04 +01:00
|
|
|
|
2019-08-13 10:32:52 +01:00
|
|
|
return
|
|
|
|
}
|
2021-08-03 17:55:04 +01:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
restartHTTPS := m.setConfig(req.tlsConfigSettings, status)
|
|
|
|
m.setCertFileTime()
|
2020-02-19 12:28:06 +00:00
|
|
|
onConfigModified()
|
2021-08-03 17:55:04 +01:00
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
err = reconfigureDNSServer()
|
2019-02-27 15:53:16 +00:00
|
|
|
if err != nil {
|
2021-12-16 17:54:59 +00:00
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "%s", err)
|
2021-08-03 17:55:04 +01:00
|
|
|
|
2019-02-27 15:53:16 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-03 17:55:04 +01:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
resp := tlsConfig{
|
|
|
|
tlsConfigSettingsExt: req,
|
|
|
|
tlsConfigStatus: m.status,
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
2021-08-03 17:55:04 +01:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
marshalTLS(w, r, resp)
|
2020-04-24 13:50:57 +01:00
|
|
|
if f, ok := w.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00: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 15:53:16 +00:00
|
|
|
if restartHTTPS {
|
|
|
|
go func() {
|
2022-10-14 17:37:14 +01:00
|
|
|
Context.web.TLSConfigChanged(context.Background(), req.tlsConfigSettings)
|
2019-02-27 15:53:16 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 12:36:18 +01:00
|
|
|
// validatePorts validates the uniqueness of TCP and UDP ports for AdGuard Home
|
|
|
|
// DNS protocols.
|
|
|
|
func validatePorts(
|
2023-01-23 09:47:59 +00:00
|
|
|
bindPort, dohPort, dotPort, dnscryptTCPPort tcpPort,
|
2022-08-03 12:36:18 +01:00
|
|
|
dnsPort, doqPort udpPort,
|
|
|
|
) (err error) {
|
|
|
|
tcpPorts := aghalg.UniqChecker[tcpPort]{}
|
|
|
|
addPorts(
|
|
|
|
tcpPorts,
|
|
|
|
tcpPort(bindPort),
|
|
|
|
tcpPort(dohPort),
|
|
|
|
tcpPort(dotPort),
|
|
|
|
tcpPort(dnscryptTCPPort),
|
|
|
|
)
|
|
|
|
|
|
|
|
err = tcpPorts.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating tcp ports: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
udpPorts := aghalg.UniqChecker[udpPort]{}
|
|
|
|
addPorts(udpPorts, udpPort(dnsPort), udpPort(doqPort))
|
|
|
|
|
|
|
|
err = udpPorts.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating udp ports: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
// validateCertChain verifies certs using the first as the main one and others
|
|
|
|
// as intermediate. srvName stands for the expected DNS name.
|
|
|
|
func validateCertChain(certs []*x509.Certificate, srvName string) (err error) {
|
|
|
|
main, others := certs[0], certs[1:]
|
2022-10-14 17:37:14 +01:00
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
pool := x509.NewCertPool()
|
|
|
|
for _, cert := range others {
|
|
|
|
log.Info("tls: got an intermediate cert")
|
|
|
|
pool.AddCert(cert)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
opts := x509.VerifyOptions{
|
|
|
|
DNSName: srvName,
|
|
|
|
Roots: Context.tlsRoots,
|
|
|
|
Intermediates: pool,
|
|
|
|
}
|
|
|
|
_, err = main.Verify(opts)
|
2022-10-14 17:37:14 +01:00
|
|
|
if err != nil {
|
2022-11-01 10:09:52 +00:00
|
|
|
return fmt.Errorf("certificate does not verify: %w", err)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 14:07:49 +00:00
|
|
|
// errNoIPInCert is the error that is returned from [parseCertChain] if the leaf
|
|
|
|
// certificate doesn't contain IPs.
|
|
|
|
const errNoIPInCert errors.Error = `certificates has no IP addresses; ` +
|
|
|
|
`DNS-over-TLS won't be advertised via DDR`
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
// parseCertChain parses the certificate chain from raw data, and returns it.
|
|
|
|
// If ok is true, the returned error, if any, is not critical.
|
|
|
|
func parseCertChain(chain []byte) (parsedCerts []*x509.Certificate, ok bool, err error) {
|
|
|
|
log.Debug("tls: got certificate chain: %d bytes", len(chain))
|
2019-02-27 15:53:16 +00:00
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
var certs []*pem.Block
|
|
|
|
for decoded, pemblock := pem.Decode(chain); decoded != nil; {
|
|
|
|
if decoded.Type == "CERTIFICATE" {
|
|
|
|
certs = append(certs, decoded)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, pemblock = pem.Decode(pemblock)
|
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
parsedCerts, err = parsePEMCerts(certs)
|
2019-02-27 15:53:16 +00:00
|
|
|
if err != nil {
|
2022-11-01 10:09:52 +00:00
|
|
|
return nil, false, err
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
log.Info("tls: number of certs: %d", len(parsedCerts))
|
|
|
|
|
2022-11-02 11:37:41 +00:00
|
|
|
if !aghtls.CertificateHasIP(parsedCerts[0]) {
|
2022-11-22 14:07:49 +00:00
|
|
|
err = errNoIPInCert
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
return parsedCerts, true, err
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// parsePEMCerts parses multiple PEM-encoded certificates.
|
|
|
|
func parsePEMCerts(certs []*pem.Block) (parsedCerts []*x509.Certificate, err error) {
|
|
|
|
for i, cert := range certs {
|
|
|
|
var parsed *x509.Certificate
|
|
|
|
parsed, err = x509.ParseCertificate(cert.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing certificate at index %d: %w", i, err)
|
|
|
|
}
|
2019-02-27 15:53:16 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
parsedCerts = append(parsedCerts, parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parsedCerts) == 0 {
|
|
|
|
return nil, errors.Error("empty certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedCerts, nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
// validatePKey validates the private key, returning its type. It returns an
|
|
|
|
// empty string if error occurs.
|
|
|
|
func validatePKey(pkey []byte) (keyType string, err error) {
|
2022-10-14 17:37:14 +01:00
|
|
|
var key *pem.Block
|
|
|
|
|
|
|
|
// Go through all pem blocks, but take first valid pem block and drop the
|
|
|
|
// rest.
|
2022-11-01 10:09:52 +00:00
|
|
|
for decoded, pemblock := pem.Decode([]byte(pkey)); decoded != nil; {
|
2019-02-27 15:53:16 +00:00
|
|
|
if decoded.Type == "PRIVATE KEY" || strings.HasSuffix(decoded.Type, " PRIVATE KEY") {
|
|
|
|
key = decoded
|
2021-11-01 10:33:12 +00:00
|
|
|
|
2019-02-27 15:53:16 +00:00
|
|
|
break
|
|
|
|
}
|
2022-11-01 10:09:52 +00:00
|
|
|
|
|
|
|
decoded, pemblock = pem.Decode(pemblock)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if key == nil {
|
2022-11-01 10:09:52 +00:00
|
|
|
return "", errors.Error("no valid keys were found")
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
_, keyType, err = parsePrivateKey(key.Bytes)
|
2019-02-27 15:53:16 +00:00
|
|
|
if err != nil {
|
2022-11-01 10:09:52 +00:00
|
|
|
return "", fmt.Errorf("parsing private key: %w", err)
|
2022-10-14 17:37:14 +01:00
|
|
|
}
|
2021-11-01 10:33:12 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
if keyType == keyTypeED25519 {
|
2022-11-01 10:09:52 +00:00
|
|
|
return "", errors.Error(
|
2022-10-14 17:37:14 +01:00
|
|
|
"ED25519 keys are not supported by browsers; " +
|
|
|
|
"did you mean to use X25519 for key exchange?",
|
|
|
|
)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
return keyType, nil
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
// validateCertificates processes certificate data and its private key. status
|
|
|
|
// must not be nil, since it's used to accumulate the validation results. Other
|
|
|
|
// parameters are optional.
|
2022-10-14 17:37:14 +01:00
|
|
|
func validateCertificates(
|
|
|
|
status *tlsConfigStatus,
|
|
|
|
certChain []byte,
|
|
|
|
pkey []byte,
|
|
|
|
serverName string,
|
|
|
|
) (err error) {
|
|
|
|
// Check only the public certificate separately from the key.
|
|
|
|
if len(certChain) > 0 {
|
2022-11-01 10:09:52 +00:00
|
|
|
var certs []*x509.Certificate
|
|
|
|
certs, status.ValidCert, err = parseCertChain(certChain)
|
|
|
|
if !status.ValidCert {
|
|
|
|
// Don't wrap the error, since it's informative enough as is.
|
2022-10-14 17:37:14 +01:00
|
|
|
return err
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
2022-11-01 10:09:52 +00:00
|
|
|
|
|
|
|
mainCert := certs[0]
|
|
|
|
status.Subject = mainCert.Subject.String()
|
|
|
|
status.Issuer = mainCert.Issuer.String()
|
|
|
|
status.NotAfter = mainCert.NotAfter
|
|
|
|
status.NotBefore = mainCert.NotBefore
|
|
|
|
status.DNSNames = mainCert.DNSNames
|
|
|
|
|
|
|
|
if chainErr := validateCertChain(certs, serverName); chainErr != nil {
|
|
|
|
// Let self-signed certs through and don't return this error to set
|
|
|
|
// its message into the status.WarningValidation afterwards.
|
|
|
|
err = chainErr
|
|
|
|
} else {
|
|
|
|
status.ValidChain = true
|
|
|
|
}
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// Validate the private key by parsing it.
|
|
|
|
if len(pkey) > 0 {
|
2022-11-01 10:09:52 +00:00
|
|
|
var keyErr error
|
|
|
|
status.KeyType, keyErr = validatePKey(pkey)
|
|
|
|
if keyErr != nil {
|
|
|
|
// Don't wrap the error, since it's informative enough as is.
|
|
|
|
return keyErr
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
2022-11-01 10:09:52 +00:00
|
|
|
|
|
|
|
status.ValidKey = true
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// If both are set, validate together.
|
|
|
|
if len(certChain) > 0 && len(pkey) > 0 {
|
2022-11-01 10:09:52 +00:00
|
|
|
_, pairErr := tls.X509KeyPair(certChain, pkey)
|
|
|
|
if pairErr != nil {
|
|
|
|
return fmt.Errorf("certificate-key pair: %w", pairErr)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
status.ValidPair = true
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:09:52 +00:00
|
|
|
return err
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 10:33:12 +00:00
|
|
|
// Key types.
|
|
|
|
const (
|
|
|
|
keyTypeECDSA = "ECDSA"
|
|
|
|
keyTypeED25519 = "ED25519"
|
|
|
|
keyTypeRSA = "RSA"
|
|
|
|
)
|
|
|
|
|
2019-02-27 15:53:16 +00: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 17:27:22 +01: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 10:33:12 +00:00
|
|
|
return key, keyTypeRSA, nil
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 17:27:22 +01:00
|
|
|
if key, err = x509.ParsePKCS8PrivateKey(der); err == nil {
|
2019-02-27 15:53:16 +00:00
|
|
|
switch key := key.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
2021-11-01 10:33:12 +00:00
|
|
|
return key, keyTypeRSA, nil
|
2019-02-27 15:53:16 +00:00
|
|
|
case *ecdsa.PrivateKey:
|
2021-11-01 10:33:12 +00:00
|
|
|
return key, keyTypeECDSA, nil
|
2021-10-25 17:27:22 +01:00
|
|
|
case ed25519.PrivateKey:
|
2021-11-01 10:33:12 +00:00
|
|
|
return key, keyTypeED25519, nil
|
2019-02-27 15:53:16 +00:00
|
|
|
default:
|
2021-10-25 17:27:22 +01:00
|
|
|
return nil, "", fmt.Errorf(
|
|
|
|
"tls: found unknown private key type %T in PKCS#8 wrapping",
|
|
|
|
key,
|
|
|
|
)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:27:22 +01:00
|
|
|
if key, err = x509.ParseECPrivateKey(der); err == nil {
|
2021-11-01 10:33:12 +00:00
|
|
|
return key, keyTypeECDSA, nil
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 15:28:11 +01:00
|
|
|
return nil, "", errors.Error("tls: failed to parse private key")
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalTLS handles base64-encoded certificates transparently
|
2021-08-27 11:42:31 +01:00
|
|
|
func unmarshalTLS(r *http.Request) (tlsConfigSettingsExt, error) {
|
|
|
|
data := tlsConfigSettingsExt{}
|
2019-02-27 15:53:16 +00:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return data, fmt.Errorf("failed to parse new TLS config json: %w", err)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.CertificateChain != "" {
|
2021-03-12 11:32:08 +00:00
|
|
|
var cert []byte
|
|
|
|
cert, err = base64.StdEncoding.DecodeString(data.CertificateChain)
|
2019-02-27 15:53:16 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return data, fmt.Errorf("failed to base64-decode certificate chain: %w", err)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
2021-03-12 11:32:08 +00:00
|
|
|
|
|
|
|
data.CertificateChain = string(cert)
|
2019-08-13 10:32:52 +01:00
|
|
|
if data.CertificatePath != "" {
|
|
|
|
return data, fmt.Errorf("certificate data and file can't be set together")
|
|
|
|
}
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.PrivateKey != "" {
|
2021-03-12 11:32:08 +00:00
|
|
|
var key []byte
|
|
|
|
key, err = base64.StdEncoding.DecodeString(data.PrivateKey)
|
2019-02-27 15:53:16 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return data, fmt.Errorf("failed to base64-decode private key: %w", err)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:32:08 +00:00
|
|
|
data.PrivateKey = string(key)
|
2019-08-13 10:32:52 +01:00
|
|
|
if data.PrivateKeyPath != "" {
|
|
|
|
return data, fmt.Errorf("private key data and file can't be set together")
|
|
|
|
}
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
func marshalTLS(w http.ResponseWriter, r *http.Request, data tlsConfig) {
|
2019-02-27 15:53:16 +00:00
|
|
|
if data.CertificateChain != "" {
|
|
|
|
encoded := base64.StdEncoding.EncodeToString([]byte(data.CertificateChain))
|
|
|
|
data.CertificateChain = encoded
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.PrivateKey != "" {
|
2021-08-27 11:42:31 +01:00
|
|
|
data.PrivateKeySaved = true
|
|
|
|
data.PrivateKey = ""
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 17:04:26 +01:00
|
|
|
_ = aghhttp.WriteJSONResponse(w, r, data)
|
2019-02-27 15:53:16 +00:00
|
|
|
}
|
2020-02-19 12:28:06 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// registerWebHandlers registers HTTP handlers for TLS configuration.
|
|
|
|
func (m *tlsManager) registerWebHandlers() {
|
|
|
|
httpRegister(http.MethodGet, "/control/tls/status", m.handleTLSStatus)
|
|
|
|
httpRegister(http.MethodPost, "/control/tls/configure", m.handleTLSConfigure)
|
|
|
|
httpRegister(http.MethodPost, "/control/tls/validate", m.handleTLSValidate)
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|