1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 type CertGenerator struct {
25 func (gen CertGenerator) Generate() (cert tls.Certificate, err error) {
26 keyUsage := x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment
28 keyUsage |= x509.KeyUsageCertSign
30 notBefore := time.Now()
31 notAfter := time.Now().Add(time.Hour * 24 * 365)
32 snMax := new(big.Int).Lsh(big.NewInt(1), 128)
33 sn, err := rand.Int(rand.Reader, snMax)
35 err = fmt.Errorf("Failed to generate serial number: %w", err)
38 template := x509.Certificate{
41 Organization: []string{"N/A"},
46 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
47 BasicConstraintsValid: true,
50 for _, h := range gen.Hosts {
51 if ip := net.ParseIP(h); ip != nil {
52 template.IPAddresses = append(template.IPAddresses, ip)
54 template.DNSNames = append(template.DNSNames, h)
61 priv, err := rsa.GenerateKey(rand.Reader, bits)
63 err = fmt.Errorf("error generating key: %w", err)
66 certder, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
68 err = fmt.Errorf("error creating certificate: %w", err)
71 cert = tls.Certificate{
72 Certificate: [][]byte{certder},