41
template.IPAddresses = []net.IP{ip}
42
}
44
>
template.DNSNames = []string{commonName}
45
>
}
46
48
template.IPAddresses = []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)}
49
}
50
51
>
privateKey, err := rsa.GenerateKey(rand.Reader, keyLengthBits)
certificate.go
52
>
if err != nil {
53
return &tls.Certificate{}, err
54
}
55
56
>
cert, err := x509.CreateCertificate(rand.Reader, template, template, privateKey.Public(), privateKey)
certificate.go
57
>
if err != nil {
58
return &tls.Certificate{}, err
59
}
60
62
>
tlsCert.Certificate = append(tlsCert.Certificate, cert)
63
>
tlsCert.PrivateKey = privateKey
64
>
65
>
return &tlsCert, nil
66
}
67
68
// GenerateServerX509UsingCA generates a TLS serverCert that is self-signed
69
>
func generateServerX509UsingCAAndSerialNumber(commonName string, serialNumber int64, ca *tls.Certificate) (*tls.Certificate, *rsa.PrivateKey, error) {
certificate.go
70
>
now := time.Now().UTC()
71
>
72
>
i := serialNumber
73
>
if i == 0 {
74
>
i = mathrand.Int63n(100000000000000000)
75
>
}
76
78
>
SerialNumber: big.NewInt(i),
79
>
Subject: pkix.Name{
80
>
CommonName: commonName,
81
>
Country: []string{"USA"},
82
>
Organization: []string{"TemporalTechnologiesTesting"},
83
>
},
84
>
NotBefore: now.Add(-time.Minute),
85
>
NotAfter: now.AddDate(0, 0, 1), // 1 day expiry
86
>
BasicConstraintsValid: true,
87
>
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
88
>
KeyUsage: x509.KeyUsageDigitalSignature,
89
>
}
90
>
91
>
if ip := net.ParseIP(commonName); ip != nil {
92
>
if ip.IsLoopback() {
93
>
template.IPAddresses = []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)}
94
>
template.DNSNames = []string{"localhost"}
95
>
} else {
96
template.IPAddresses = []net.IP{ip}
97
}