Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MITM: recreating private/public keys all the time #368

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"math/big"
"math/rand"
mathrand "math/rand"
"net"
"runtime"
"sort"
"time"
"github.com/zond/gotomic"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do that without introducing another depdendency?

)


func hashSorted(lst []string) []byte {
c := make([]string, len(lst))
copy(c, lst)
Expand All @@ -36,8 +38,28 @@ func hashSortedBigInt(lst []string) *big.Int {
}

var goproxySignerVersion = ":goroxy1"
var certprivRSA crypto.Signer
var certprivECDSA crypto.Signer
var crtCache = gotomic.NewHash()

func signHost(ca tls.Certificate, hosts []string) (cert *tls.Certificate, err error) {

value, exist := crtCache.Get(gotomic.StringKey(hosts[0]))
if exist {
cert = value.(*tls.Certificate)
return
}

cert, err = generateCertificate(ca, hosts)

for _, host := range hosts {
crtCache.Put(gotomic.StringKey(host), cert)
}

return
}

func generateCertificate(ca tls.Certificate, hosts []string) (cert *tls.Certificate, err error) {
var x509ca *x509.Certificate

// Use the provided ca and not the global GoproxyCa for certificate generation.
Expand All @@ -50,7 +72,7 @@ func signHost(ca tls.Certificate, hosts []string) (cert *tls.Certificate, err er
panic(err)
}

serial := big.NewInt(rand.Int63())
serial := big.NewInt(mathrand.Int63())
template := x509.Certificate{
// TODO(elazar): instead of this ugly hack, just encode the certificate and hash the binary form.
SerialNumber: serial,
Expand All @@ -74,37 +96,29 @@ func signHost(ca tls.Certificate, hosts []string) (cert *tls.Certificate, err er
}
}

hash := hashSorted(append(hosts, goproxySignerVersion, ":"+runtime.Version()))
var csprng CounterEncryptorRand
if csprng, err = NewCounterEncryptorRandFromKey(ca.PrivateKey, hash); err != nil {
return
}

var certpriv crypto.Signer

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put the whitespace changes in a different commit?

switch ca.PrivateKey.(type) {
case *rsa.PrivateKey:
if certpriv, err = rsa.GenerateKey(&csprng, 2048); err != nil {
return
}
certpriv = certprivRSA
case *ecdsa.PrivateKey:
if certpriv, err = ecdsa.GenerateKey(elliptic.P256(), &csprng); err != nil {
return
}
certpriv = certprivECDSA
default:
err = fmt.Errorf("unsupported key type %T", ca.PrivateKey)
}

var derBytes []byte
if derBytes, err = x509.CreateCertificate(&csprng, &template, x509ca, certpriv.Public(), ca.PrivateKey); err != nil {
if derBytes, err = x509.CreateCertificate(rand.Reader, &template, x509ca, certpriv.Public(), ca.PrivateKey); err != nil {
return
}

return &tls.Certificate{
Certificate: [][]byte{derBytes, ca.Certificate[0]},
PrivateKey: certpriv,
}, nil
}

func init() {
// Avoid deterministic random numbers
rand.Seed(time.Now().UnixNano())
certprivRSA, _ = rsa.GenerateKey(rand.Reader, 2048);
certprivECDSA, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
}