You can create a new certificate, with code that looks like the following. You'll need the CA private key for this:
func GenerateCertificate(ca *x509.Certificate, caKey crypto.PrivateKey, req x509.CertificateRequest, durYear, durMonth int, keyUsage x509.KeyUsage, extKeyUsage []x509.ExtKeyUsage, rsaKeySize int) (certificate, key *pem.Block, err error) {
cert := &x509.Certificate{
Version: req.Version,
SerialNumber: RandomBigInt(),
Subject: req.Subject,
Extensions: req.Extensions,
ExtraExtensions: req.ExtraExtensions,
DNSNames: req.DNSNames,
EmailAddresses: req.EmailAddresses,
IPAddresses: req.IPAddresses,
URIs: req.URIs,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(durYear, durMonth, 0),
ExtKeyUsage: extKeyUsage,
KeyUsage: keyUsage,
}
priv, _ := rsa.GenerateKey(rand.Reader, rsaKeySize)
pub := &priv.PublicKey
var data []byte
data, err = x509.CreateCertificate(rand.Reader, cert, ca, pub, caKey)
if err != nil {
return
}
// Public key
certificate = &pem.Block{Type: "CERTIFICATE", Bytes: data}
// Private key
key = &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}
return
}
Use it as:
subject := pkix.Name{CommonName:"name"}
cert, certKey, err := GenerateCertificate(caCert, key, x509.CertificateRequest{Subject: subject}, 1, 0, x509.KeyUsageDigitalSignature,
[]x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, 2048)
You need to find out the key usage, ext key usage, etc. or copy them from the old cert. You can initialize the certificate request passed into GenerateCertificate from the old certificate you have.
If you need a self-signed cert, you can use something like below (I use this to generate a self-signed CA). You have to copy information from the old cert into this one.
func GenerateCA(subject pkix.Name, duryear, durmonth int, rsaKeySize int) (certificate, key *pem.Block, err error) {
ca := &x509.Certificate{
SerialNumber: RandomBigInt(),
Subject: subject,
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(duryear, durmonth, 0),
IsCA: false, // or true?
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
priv, _ := rsa.GenerateKey(rand.Reader, rsaKeySize)
pub := &priv.PublicKey
var data []byte
data, err = x509.CreateCertificate(rand.Reader, ca, ca, pub, priv)
if err != nil {
return
}
// Public key
certificate = &pem.Block{Type: "CERTIFICATE", Bytes: data}
// Private key
key = &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}
return
}