leaf returns a certificate for host signed by the authority, making it the first time it's asked for.
(host string)
| 73 | // leaf returns a certificate for host signed by the authority, making it the |
| 74 | // first time it's asked for. |
| 75 | func (ca *certAuthority) leaf(host string) (*tls.Certificate, error) { |
| 76 | host = strings.ToLower(strings.TrimSuffix(host, ".")) |
| 77 | ca.mu.Lock() |
| 78 | defer ca.mu.Unlock() |
| 79 | if c, ok := ca.leaves[host]; ok { |
| 80 | return c, nil |
| 81 | } |
| 82 | key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | serial, err := randomSerial() |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | tmpl := &x509.Certificate{ |
| 91 | SerialNumber: serial, |
| 92 | Subject: pkix.Name{CommonName: host}, |
| 93 | NotBefore: ca.cert.NotBefore, |
| 94 | NotAfter: ca.cert.NotAfter, |
| 95 | KeyUsage: x509.KeyUsageDigitalSignature, |
| 96 | ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 97 | } |
| 98 | if ip := net.ParseIP(host); ip != nil { |
| 99 | tmpl.IPAddresses = []net.IP{ip} |
| 100 | } else { |
| 101 | tmpl.DNSNames = []string{host} |
| 102 | } |
| 103 | der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.cert, &key.PublicKey, ca.key) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | c := &tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key} |
| 108 | ca.leaves[host] = c |
| 109 | return c, nil |
| 110 | } |
| 111 | |
| 112 | // serverConfig presents a certificate for the host gh names in its TLS |
| 113 | // handshake, or for fallback when it names none. |