1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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
}
|