| 1 | package tls |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "crypto/rsa" |
| 6 | "crypto/tls" |
| 7 | "crypto/x509" |
| 8 | "crypto/x509/pkix" |
| 9 | "fmt" |
| 10 | "math/big" |
| 11 | "net" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/bettercap/bettercap/log" |
| 15 | ) |
| 16 | |
| 17 | func getServerCertificate(host string, port int) *x509.Certificate { |
| 18 | log.Debug("Fetching TLS certificate from %s:%d ...", host, port) |
| 19 | |
| 20 | config := tls.Config{InsecureSkipVerify: true} |
| 21 | conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), &config) |
| 22 | if err != nil { |
| 23 | log.Warning("Could not fetch TLS certificate from %s:%d: %s", host, port, err) |
| 24 | return nil |
| 25 | } |
| 26 | defer conn.Close() |
| 27 | |
| 28 | state := conn.ConnectionState() |
| 29 | |
| 30 | return state.PeerCertificates[0] |
| 31 | } |
| 32 | |
| 33 | func SignCertificateForHost(ca *tls.Certificate, host string, port int) (cert *tls.Certificate, err error) { |
| 34 | var x509ca *x509.Certificate |
| 35 | var template x509.Certificate |
| 36 | |
| 37 | if x509ca, err = x509.ParseCertificate(ca.Certificate[0]); err != nil { |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | srvCert := getServerCertificate(host, port) |
| 42 | if srvCert == nil { |
| 43 | log.Debug("Could not fetch TLS certificate, falling back to default template.") |
| 44 | |
| 45 | notBefore := time.Now() |
| 46 | aYear := time.Duration(365*24) * time.Hour |
| 47 | notAfter := notBefore.Add(aYear) |
| 48 | serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) |
| 49 | serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | |
| 54 | template = x509.Certificate{ |
| 55 | SerialNumber: serialNumber, |
| 56 | Issuer: x509ca.Subject, |
| 57 | Subject: pkix.Name{ |
| 58 | Country: []string{"US"}, |
| 59 | Locality: []string{"Scottsdale"}, |
| 60 | Organization: []string{"GoDaddy.com, Inc."}, |
| 61 | OrganizationalUnit: []string{"https://certs.godaddy.com/repository/"}, |
| 62 | CommonName: "Go Daddy Secure Certificate Authority - G2", |
| 63 | }, |
| 64 | NotBefore: notBefore, |
| 65 | NotAfter: notAfter, |
| 66 | KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 67 | ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, |
| 68 | BasicConstraintsValid: true, |
| 69 | } |
| 70 | |
| 71 | if ip := net.ParseIP(host); ip != nil { |
| 72 | template.IPAddresses = append(template.IPAddresses, ip) |
| 73 | } else { |
| 74 | template.DNSNames = append(template.DNSNames, host) |
| 75 | } |
| 76 | |
| 77 | } else { |
| 78 | template = x509.Certificate{ |
| 79 | SerialNumber: srvCert.SerialNumber, |
| 80 | Issuer: x509ca.Subject, |
| 81 | Subject: srvCert.Subject, |
| 82 | NotBefore: srvCert.NotBefore, |
| 83 | NotAfter: srvCert.NotAfter, |
| 84 | KeyUsage: srvCert.KeyUsage, |
| 85 | ExtKeyUsage: srvCert.ExtKeyUsage, |
| 86 | IPAddresses: srvCert.IPAddresses, |
| 87 | DNSNames: srvCert.DNSNames, |
| 88 | BasicConstraintsValid: true, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | var certpriv *rsa.PrivateKey |
| 93 | if certpriv, err = rsa.GenerateKey(rand.Reader, 1024); err != nil { |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | var derBytes []byte |
| 98 | if derBytes, err = x509.CreateCertificate(rand.Reader, &template, x509ca, &certpriv.PublicKey, ca.PrivateKey); err != nil { |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | return &tls.Certificate{ |
| 103 | Certificate: [][]byte{derBytes, ca.Certificate[0]}, |
| 104 | PrivateKey: certpriv, |
| 105 | }, nil |
| 106 | } |