Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 840e446

Browse files
committed
Support IP-only hostname and ignore hello.ServerName
Signed-off-by: YanzheL <[email protected]>
1 parent f5ee1fb commit 840e446

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

dfdaemon/proxy/cert.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"crypto/x509/pkix"
2525
"errors"
2626
"math/big"
27+
"net"
2728
"time"
2829

2930
"github.com/sirupsen/logrus"
@@ -37,7 +38,8 @@ type LeafCertSpec struct {
3738
signatureAlgorithm x509.SignatureAlgorithm
3839
}
3940

40-
func genLeafCert(ca *tls.Certificate, leafCertSpec *LeafCertSpec, commonName string, dnsNames ...string) (*tls.Certificate, error) {
41+
// genLeafCert generates a Leaf TLS certificate and sign it with given CA
42+
func genLeafCert(ca *tls.Certificate, leafCertSpec *LeafCertSpec, host string) (*tls.Certificate, error) {
4143
now := time.Now().Add(-1 * time.Hour).UTC()
4244
if !ca.Leaf.IsCA {
4345
return nil, errors.New("CA cert is not a CA")
@@ -50,14 +52,19 @@ func genLeafCert(ca *tls.Certificate, leafCertSpec *LeafCertSpec, commonName str
5052
}
5153
tmpl := &x509.Certificate{
5254
SerialNumber: serialNumber,
53-
Subject: pkix.Name{CommonName: commonName},
55+
Subject: pkix.Name{CommonName: host},
5456
NotBefore: now,
5557
NotAfter: now.Add(24 * time.Hour),
5658
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageDataEncipherment | x509.KeyUsageKeyAgreement,
5759
BasicConstraintsValid: true,
58-
DNSNames: dnsNames,
5960
SignatureAlgorithm: leafCertSpec.signatureAlgorithm,
6061
}
62+
ip := net.ParseIP(host)
63+
if ip == nil {
64+
tmpl.DNSNames = []string{host}
65+
} else {
66+
tmpl.IPAddresses = []net.IP{ip}
67+
}
6168
newCert, err := x509.CreateCertificate(rand.Reader, tmpl, ca.Leaf, leafCertSpec.publicKey, ca.PrivateKey)
6269
if err != nil {
6370
logrus.Errorf("failed to generate leaf cert %s", err)

dfdaemon/proxy/proxy.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,16 @@ func (proxy *Proxy) handleHTTPS(w http.ResponseWriter, r *http.Request) {
333333
proxy.cert.Leaf.SignatureAlgorithm}
334334
host, _, _ := net.SplitHostPort(r.Host)
335335
sConfig.GetCertificate = func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
336-
cConfig.ServerName = hello.ServerName
336+
cConfig.ServerName = host
337337
logrus.Debugf("Generate temporal leaf TLS cert for ServerName <%s>, host <%s>", hello.ServerName, host)
338-
// cacheKey is (ServerName, host) pair. Just use string concatenation, no need for a tuple struct here.
339-
// I'm curious about whether `hello.ServerName` is always same as `host`?
340-
cacheKey := hello.ServerName + "," + host
338+
// It's assumed that `hello.ServerName` is always same as `host`, in practice.
339+
cacheKey := host
341340
cached, hit := proxy.certCache.Get(cacheKey)
342341
if hit && time.Now().Before(cached.(*tls.Certificate).Leaf.NotAfter) { // If cache hit and the cert is not expired
343342
logrus.Debugf("TLS Cache hit, cacheKey = <%s>", cacheKey)
344343
return cached.(*tls.Certificate), nil
345344
}
346-
cert, err := genLeafCert(proxy.cert, &leafCertSpec, hello.ServerName, host)
345+
cert, err := genLeafCert(proxy.cert, &leafCertSpec, host)
347346
if err == nil {
348347
// Put cert in cache only if there is no error. So all certs in cache are always valid.
349348
// But certs in cache maybe expired (After 24 hours, see the default duration of generated certs)

0 commit comments

Comments
 (0)