Debian Codegraph @

fakeca/configuration.go

raw symbols SCIP
1 package fakeca
2
3 import (
4 "crypto"
5 "crypto/rand"
6 "crypto/rsa"
7 "crypto/x509"
8 "crypto/x509/pkix"
9 "fmt"
10 "math"
11 "math/big"
12 "time"
13 )
14
15 type configuration struct {
16 subject *pkix.Name
17 issuer *Identity
18 nextSN *int64
19 priv *crypto.Signer
20 isCA bool
21 notBefore *time.Time
22 notAfter *time.Time
23 issuingCertificateURL []string
24 ocspServer []string
25 keyUsage x509.KeyUsage
26 }
27
28 func (c *configuration) generate() *Identity {
29 templ := &x509.Certificate{
30 Subject: c.getSubject(),
31 IsCA: c.isCA,
32 BasicConstraintsValid: true,
33 NotAfter: c.getNotAfter(),
34 NotBefore: c.getNotBefore(),
35 IssuingCertificateURL: c.issuingCertificateURL,
36 OCSPServer: c.ocspServer,
37 KeyUsage: c.keyUsage,
38 }
39
40 var (
41 parent *x509.Certificate
42 thisPriv = c.getPrivateKey()
43 priv crypto.Signer
44 )
45
46 if c.issuer != nil {
47 parent = c.issuer.Certificate
48 templ.SerialNumber = big.NewInt(c.issuer.IncrementSN())
49 priv = c.issuer.PrivateKey
50 } else {
51 parent = templ
52 templ.SerialNumber = randSN()
53 priv = thisPriv
54 }
55
56 der, err := x509.CreateCertificate(rand.Reader, templ, parent, thisPriv.Public(), priv)
57 if err != nil {
58 panic(err)
59 }
60
61 cert, err := x509.ParseCertificate(der)
62 if err != nil {
63 panic(err)
64 }
65
66 return &Identity{
67 Certificate: cert,
68 PrivateKey: thisPriv,
69 Issuer: c.issuer,
70 NextSN: c.getNextSN(),
71 }
72 }
73
74 var (
75 // DefaultCountry is the default subject Country.
76 DefaultCountry = []string{"US"}
77
78 // DefaultProvince is the default subject Province.
79 DefaultProvince = []string{"CA"}
80
81 // DefaultLocality is the default subject Locality.
82 DefaultLocality = []string{"San Francisco"}
83
84 // DefaultStreetAddress is the default subject StreetAddress.
85 DefaultStreetAddress = []string(nil)
86
87 // DefaultPostalCode is the default subject PostalCode.
88 DefaultPostalCode = []string(nil)
89
90 // DefaultCommonName is the default subject CommonName.
91 DefaultCommonName = "fakeca"
92
93 cnCounter int64
94 )
95
96 func (c *configuration) getSubject() pkix.Name {
97 if c.subject != nil {
98 return *c.subject
99 }
100
101 var cn string
102 if cnCounter == 0 {
103 cn = DefaultCommonName
104 } else {
105 cn = fmt.Sprintf("%s #%d", DefaultCommonName, cnCounter)
106 }
107 cnCounter++
108
109 return pkix.Name{
110 Country: DefaultCountry,
111 Province: DefaultProvince,
112 Locality: DefaultLocality,
113 StreetAddress: DefaultStreetAddress,
114 PostalCode: DefaultPostalCode,
115 CommonName: cn,
116 }
117 }
118
119 func (c *configuration) getNextSN() int64 {
120 if c.nextSN == nil {
121 sn := randSN().Int64()
122 c.nextSN = &sn
123 }
124
125 return *c.nextSN
126 }
127
128 func randSN() *big.Int {
129 i, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
130 if err != nil {
131 panic(err)
132 }
133
134 return i
135 }
136
137 func (c *configuration) getPrivateKey() crypto.Signer {
138 if c.priv == nil {
139 priv, err := rsa.GenerateKey(rand.Reader, 2048)
140 if err != nil {
141 panic(err)
142 }
143
144 signer := crypto.Signer(priv)
145
146 c.priv = &signer
147 }
148
149 return *c.priv
150 }
151
152 func (c *configuration) getNotBefore() time.Time {
153 if c.notBefore == nil {
154 return time.Unix(0, 0)
155 }
156
157 return *c.notBefore
158 }
159
160 func (c *configuration) getNotAfter() time.Time {
161 if c.notAfter == nil {
162 return time.Now().Add(time.Hour * 24 * 365 * 10)
163 }
164
165 return *c.notAfter
166 }
167
168 // Option is an option that can be passed to New().
169 type Option option
170 type option func(c *configuration)
171
172 // Subject is an Option that sets a identity's subject field.
173 func Subject(value pkix.Name) Option {
174 return func(c *configuration) {
175 c.subject = &value
176 }
177 }
178
179 // NextSerialNumber is an Option that determines the SN of the next issued
180 // certificate.
181 func NextSerialNumber(value int64) Option {
182 return func(c *configuration) {
183 c.nextSN = &value
184 }
185 }
186
187 // PrivateKey is an Option for setting the identity's private key.
188 func PrivateKey(value crypto.Signer) Option {
189 return func(c *configuration) {
190 c.priv = &value
191 }
192 }
193
194 // Issuer is an Option for setting the identity's issuer.
195 func Issuer(value *Identity) Option {
196 return func(c *configuration) {
197 c.issuer = value
198 }
199 }
200
201 // NotBefore is an Option for setting the identity's certificate's NotBefore.
202 func NotBefore(value time.Time) Option {
203 return func(c *configuration) {
204 c.notBefore = &value
205 }
206 }
207
208 // NotAfter is an Option for setting the identity's certificate's NotAfter.
209 func NotAfter(value time.Time) Option {
210 return func(c *configuration) {
211 c.notAfter = &value
212 }
213 }
214
215 // IssuingCertificateURL is an Option for setting the identity's certificate's
216 // IssuingCertificateURL.
217 func IssuingCertificateURL(value ...string) Option {
218 return func(c *configuration) {
219 c.issuingCertificateURL = append(c.issuingCertificateURL, value...)
220 }
221 }
222
223 // OCSPServer is an Option for setting the identity's certificate's OCSPServer.
224 func OCSPServer(value ...string) Option {
225 return func(c *configuration) {
226 c.ocspServer = append(c.ocspServer, value...)
227 }
228 }
229
230 // KeyUsage is an Option for setting the identity's certificate's KeyUsage.
231 func KeyUsage(ku x509.KeyUsage) Option {
232 return func(c *configuration) {
233 c.keyUsage = ku
234 }
235 }
236
237 // IsCA is an Option for making an identity a certificate authority.
238 var IsCA Option = func(c *configuration) {
239 c.isCA = true
240 }