mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-08-03 23:29:32 +00:00
Merge in DNS/adguard-home from AGDNS-3060-imp-gocognit-logs to master
Squashed commit of the following:
commit 3026dc35662c71c125c60198f828e7da7e8e5b4f
Merge: 2b56f4236 df258512d
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jul 30 13:00:44 2025 +0300
Merge branch 'master' into AGDNS-3060-imp-gocognit-logs
commit 2b56f423649ef0e5734a3a1d9c0b5df178462e29
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Fri Jul 25 14:41:40 2025 +0300
all: imp docs
commit 101d043c85e8b813f6b28751edeeb6c6b43578a9
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jul 23 20:09:38 2025 +0300
all: imp code
commit 87cfa502f7abf5b8f8a18b71f4a99aab9a066961
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jul 23 14:51:33 2025 +0300
all: imp code
commit 07c1a04a4022f4e38282a538670d575fba949bb2
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Jul 22 20:04:58 2025 +0300
all: imp gocognit, logs
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
// Package aghtls contains utilities for work with TLS.
|
|
package aghtls
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"log/slog"
|
|
"slices"
|
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
)
|
|
|
|
// Init populates the cipherSuites map with the name-to-ID mapping of cipher
|
|
// suites from crypto/tls. It must be called only once, and it must be called
|
|
// before any function that calls [ParseCiphers].
|
|
//
|
|
// TODO(a.garipov): Propose a similar API to crypto/tls.
|
|
func Init(ctx context.Context, l *slog.Logger) {
|
|
suites := tls.CipherSuites()
|
|
cipherSuites = make(map[string]uint16, len(suites))
|
|
for _, s := range suites {
|
|
cipherSuites[s.Name] = s.ID
|
|
}
|
|
|
|
l.DebugContext(ctx, "known ciphers", "ciphers", cipherSuites)
|
|
}
|
|
|
|
// cipherSuites are a name-to-ID mapping of cipher suites from crypto/tls. It
|
|
// is filled by init. It must not be modified.
|
|
var cipherSuites map[string]uint16
|
|
|
|
// ParseCiphers parses a slice of cipher suites from cipher names.
|
|
func ParseCiphers(cipherNames []string) (cipherIDs []uint16, err error) {
|
|
if cipherNames == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
cipherIDs = make([]uint16, 0, len(cipherNames))
|
|
for _, name := range cipherNames {
|
|
id, ok := cipherSuites[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unknown cipher %q", name)
|
|
}
|
|
|
|
cipherIDs = append(cipherIDs, id)
|
|
}
|
|
|
|
return cipherIDs, nil
|
|
}
|
|
|
|
// SaferCipherSuites returns a set of default cipher suites with vulnerable and
|
|
// weak cipher suites removed.
|
|
func SaferCipherSuites() (safe []uint16) {
|
|
for _, s := range tls.CipherSuites() {
|
|
switch s.ID {
|
|
case
|
|
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
|
|
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
|
|
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
|
|
tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
|
|
// Less safe 3DES and CBC suites, go on.
|
|
default:
|
|
safe = append(safe, s.ID)
|
|
}
|
|
}
|
|
|
|
return safe
|
|
}
|
|
|
|
// CertificateHasIP returns true if cert has at least a single IP address among
|
|
// its subjectAltNames.
|
|
func CertificateHasIP(cert *x509.Certificate) (ok bool) {
|
|
return len(cert.IPAddresses) > 0 || slices.ContainsFunc(cert.DNSNames, netutil.IsValidIPString)
|
|
}
|