-
Notifications
You must be signed in to change notification settings - Fork 13
/
certificate_generator.go
143 lines (117 loc) · 4 KB
/
certificate_generator.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package types
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"time"
"github.com/cloudfoundry/bosh-utils/errors"
"gopkg.in/yaml.v2"
)
type CertificateGenerator struct {
loader CertsLoader
}
type CertResponse struct {
Certificate string `json:"certificate" yaml:"certificate"`
PrivateKey string `json:"private_key" yaml:"private_key"`
CA string `json:"ca" yaml:"ca"`
}
type certParams struct {
CommonName string `yaml:"common_name"`
AlternativeNames []string `yaml:"alternative_names"`
IsCA bool `yaml:"is_ca"`
CAName string `yaml:"ca"`
}
func NewCertificateGenerator(loader CertsLoader) CertificateGenerator {
return CertificateGenerator{loader: loader}
}
func (cfg CertificateGenerator) Generate(parameters interface{}) (interface{}, error) {
var params certParams
err := objToStruct(parameters, ¶ms)
if err != nil {
return nil, errors.Error("Failed to generate certificate, parameters are invalid.")
}
return cfg.generateCertificate(params)
}
func (cfg CertificateGenerator) generateCertificate(cParams certParams) (CertResponse, error) {
var certResponse CertResponse
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return certResponse, errors.WrapError(err, "Generating Serial Number")
}
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return certResponse, errors.WrapError(err, "Generating Key")
}
now := time.Now()
notAfter := now.Add(365 * 24 * time.Hour)
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Country: []string{"USA"},
Organization: []string{"Cloud Foundry"},
CommonName: cParams.CommonName,
},
NotBefore: now,
NotAfter: notAfter,
BasicConstraintsValid: true,
IsCA: cParams.IsCA,
}
var certificateRaw []byte
var rootCARaw []byte
if cParams.IsCA {
template.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageCRLSign
certificateRaw, err = x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
if err != nil {
return certResponse, errors.WrapError(err, "Generating CA certificate")
}
rootCARaw = certificateRaw
} else {
template.KeyUsage = x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
template.ExtKeyUsage = append(template.ExtKeyUsage, x509.ExtKeyUsageServerAuth)
if cfg.loader == nil {
panic("Expected CertificateGenerator to have Loader set")
}
rootCA, rootPKey, err := cfg.loader.LoadCerts(cParams.CAName)
if err != nil {
return certResponse, errors.WrapError(err, "Loading certificates")
}
for _, altName := range cParams.AlternativeNames {
possibleIP := net.ParseIP(altName)
if possibleIP == nil {
template.DNSNames = append(template.DNSNames, altName)
} else {
template.IPAddresses = append(template.IPAddresses, possibleIP)
}
}
certificateRaw, err = x509.CreateCertificate(rand.Reader, &template, rootCA, &privateKey.PublicKey, rootPKey)
if err != nil {
return certResponse, errors.WrapError(err, "Generating certificate")
}
rootCARaw = rootCA.Raw
}
encodedCert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certificateRaw})
encodedPrivatekey := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
encodedRootCACert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: rootCARaw})
certResponse = CertResponse{
Certificate: string(encodedCert),
PrivateKey: string(encodedPrivatekey),
CA: string(encodedRootCACert),
}
return certResponse, nil
}
func objToStruct(input interface{}, str interface{}) error {
valBytes, err := yaml.Marshal(input)
if err != nil {
return errors.WrapErrorf(err, "Expected input to be serializable")
}
err = yaml.Unmarshal(valBytes, str)
if err != nil {
return errors.WrapErrorf(err, "Expected input to be deserializable")
}
return nil
}