fix: fixed a bug in the https.proxy certificates cache due to a race condition which caused the same certificate to be generated more than once

This commit is contained in:
evilsocket 2019-02-19 10:51:12 +01:00
commit e3573b81e4
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
2 changed files with 9 additions and 9 deletions

View file

@ -211,7 +211,7 @@ func (p *HTTPProxy) TLSConfigFromCA(ca *tls.Certificate) func(host string, ctx *
cert := getCachedCert(hostname, port) cert := getCachedCert(hostname, port)
if cert == nil { if cert == nil {
p.Debug("creating spoofed certificate for %s:%d", tui.Yellow(hostname), port) p.Info("creating spoofed certificate for %s:%d", tui.Yellow(hostname), port)
cert, err = btls.SignCertificateForHost(ca, hostname, port) cert, err = btls.SignCertificateForHost(ca, hostname, port)
if err != nil { if err != nil {
p.Warning("cannot sign host certificate with provided CA: %s", err) p.Warning("cannot sign host certificate with provided CA: %s", err)
@ -219,6 +219,8 @@ func (p *HTTPProxy) TLSConfigFromCA(ca *tls.Certificate) func(host string, ctx *
} }
setCachedCert(hostname, port, cert) setCachedCert(hostname, port, cert)
} else {
p.Debug("serving spoofed certificate for %s:%d", tui.Yellow(hostname), port)
} }
config := tls.Config{ config := tls.Config{

View file

@ -11,23 +11,21 @@ var (
certLock = &sync.Mutex{} certLock = &sync.Mutex{}
) )
func getCachedCert(domain string, port int) *tls.Certificate { func keyFor(domain string, port int) string {
key := fmt.Sprintf("%s:%d", domain, port) return fmt.Sprintf("%s:%d", domain, port)
}
func getCachedCert(domain string, port int) *tls.Certificate {
certLock.Lock() certLock.Lock()
defer certLock.Unlock() defer certLock.Unlock()
if cert, found := certCache[keyFor(domain, port)]; found {
if cert, found := certCache[key]; found {
return cert return cert
} }
return nil return nil
} }
func setCachedCert(domain string, port int, cert *tls.Certificate) { func setCachedCert(domain string, port int, cert *tls.Certificate) {
key := fmt.Sprintf("%s:%d", domain, port)
certLock.Lock() certLock.Lock()
defer certLock.Unlock() defer certLock.Unlock()
certCache[keyFor(domain, port)] = cert
certCache[key] = cert
} }