|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/tls" |
| 7 | + "crypto/x509" |
| 8 | + "crypto/x509/pkix" |
| 9 | + "math/big" |
| 10 | + "net" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// generateCA creates a self-signed CA certificate in memory. |
| 15 | +func generateCA() (*tls.Certificate, error) { |
| 16 | + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 17 | + if err != nil { |
| 18 | + return nil, err |
| 19 | + } |
| 20 | + |
| 21 | + serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + |
| 26 | + template := &x509.Certificate{ |
| 27 | + SerialNumber: serial, |
| 28 | + Subject: pkix.Name{ |
| 29 | + Organization: []string{"Buggregator Proxy CA"}, |
| 30 | + CommonName: "Buggregator Proxy CA", |
| 31 | + }, |
| 32 | + NotBefore: time.Now().Add(-time.Hour), |
| 33 | + NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour), |
| 34 | + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, |
| 35 | + BasicConstraintsValid: true, |
| 36 | + IsCA: true, |
| 37 | + MaxPathLen: 1, |
| 38 | + } |
| 39 | + |
| 40 | + certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + cert, err := x509.ParseCertificate(certDER) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return &tls.Certificate{ |
| 51 | + Certificate: [][]byte{certDER}, |
| 52 | + PrivateKey: key, |
| 53 | + Leaf: cert, |
| 54 | + }, nil |
| 55 | +} |
| 56 | + |
| 57 | +// generateHostCert creates a TLS certificate for the given host, signed by the CA. |
| 58 | +func generateHostCert(ca *tls.Certificate, host string) (*tls.Certificate, error) { |
| 59 | + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + template := &x509.Certificate{ |
| 70 | + SerialNumber: serial, |
| 71 | + Subject: pkix.Name{ |
| 72 | + CommonName: host, |
| 73 | + }, |
| 74 | + NotBefore: time.Now().Add(-time.Hour), |
| 75 | + NotAfter: time.Now().Add(365 * 24 * time.Hour), |
| 76 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, |
| 77 | + ExtKeyUsage: []x509.ExtKeyUsage{ |
| 78 | + x509.ExtKeyUsageServerAuth, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + if ip := net.ParseIP(host); ip != nil { |
| 83 | + template.IPAddresses = []net.IP{ip} |
| 84 | + } else { |
| 85 | + template.DNSNames = []string{host} |
| 86 | + } |
| 87 | + |
| 88 | + certDER, err := x509.CreateCertificate(rand.Reader, template, ca.Leaf, &key.PublicKey, ca.PrivateKey) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + return &tls.Certificate{ |
| 94 | + Certificate: [][]byte{certDER}, |
| 95 | + PrivateKey: key, |
| 96 | + }, nil |
| 97 | +} |
0 commit comments