@@ -24,6 +24,7 @@ import (
2424 "crypto/x509/pkix"
2525 "errors"
2626 "math/big"
27+ "net"
2728 "time"
2829
2930 "github.com/sirupsen/logrus"
@@ -37,7 +38,8 @@ type LeafCertSpec struct {
3738 signatureAlgorithm x509.SignatureAlgorithm
3839}
3940
40- func genLeafCert (ca * tls.Certificate , leafCertSpec * LeafCertSpec , commonName string , dnsNames ... string ) (* tls.Certificate , error ) {
41+ // genLeafCert generates a Leaf TLS certificate and sign it with given CA
42+ func genLeafCert (ca * tls.Certificate , leafCertSpec * LeafCertSpec , host string ) (* tls.Certificate , error ) {
4143 now := time .Now ().Add (- 1 * time .Hour ).UTC ()
4244 if ! ca .Leaf .IsCA {
4345 return nil , errors .New ("CA cert is not a CA" )
@@ -50,14 +52,19 @@ func genLeafCert(ca *tls.Certificate, leafCertSpec *LeafCertSpec, commonName str
5052 }
5153 tmpl := & x509.Certificate {
5254 SerialNumber : serialNumber ,
53- Subject : pkix.Name {CommonName : commonName },
55+ Subject : pkix.Name {CommonName : host },
5456 NotBefore : now ,
5557 NotAfter : now .Add (24 * time .Hour ),
5658 KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageKeyEncipherment | x509 .KeyUsageDataEncipherment | x509 .KeyUsageKeyAgreement ,
5759 BasicConstraintsValid : true ,
58- DNSNames : dnsNames ,
5960 SignatureAlgorithm : leafCertSpec .signatureAlgorithm ,
6061 }
62+ ip := net .ParseIP (host )
63+ if ip == nil {
64+ tmpl .DNSNames = []string {host }
65+ } else {
66+ tmpl .IPAddresses = []net.IP {ip }
67+ }
6168 newCert , err := x509 .CreateCertificate (rand .Reader , tmpl , ca .Leaf , leafCertSpec .publicKey , ca .PrivateKey )
6269 if err != nil {
6370 logrus .Errorf ("failed to generate leaf cert %s" , err )
0 commit comments