-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.go
146 lines (134 loc) · 3.46 KB
/
assembler.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
144
145
146
package assembler
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/chrjoh/certificateBar/certificate"
"github.com/chrjoh/certificateBar/key"
"gopkg.in/yaml.v2"
)
func Generate(filename string) Certs {
c := Certs{}
data := readFile(filename)
err := yaml.Unmarshal(data, &c)
if err != nil {
log.Fatalf("error: %v", err)
}
c.setupKeys()
c.setupTemplates()
c.setupSigner()
c.signAll()
return c
}
func (c *Certs) setupSigner() {
c.certSigners = make(map[string][]string)
for _, val := range c.Certificates {
parent := val.CertConfig.Parent
id := val.CertConfig.Id
if parent == id {
privKey := val.PrivateKey
// self signed certificate
val.CertBytes = certificate.Sign(val.CertTemplate, val.CertTemplate, key.PublicKey(privKey), privKey)
val.signed = true
} else if c.certSigners[parent] == nil {
c.certSigners[parent] = []string{id}
} else {
c.certSigners[parent] = append(c.certSigners[parent], id)
}
}
}
func (c *Certs) setupKeys() {
for _, cert := range c.Certificates {
rsaBitsLenght := 2048
if cert.CertConfig.KeyLength > 0 {
rsaBitsLenght = cert.CertConfig.KeyLength
}
cert.PrivateKey = key.GenerateKey(cert.CertConfig.KeyType, rsaBitsLenght)
}
}
func (c *Certs) findByid(id string) (*Cert, error) {
for _, cert := range c.Certificates {
if cert.CertConfig.Id == id {
return cert, nil
}
}
return &Cert{}, errors.New("No cert found")
}
func (c *Certs) setupTemplates() {
for _, cert := range c.Certificates {
d := cert.CertConfig
template := certificate.Certificate{
Id: d.Id,
Country: d.Pkix.Country,
Organization: d.Pkix.Organization,
OrganizationalUnit: d.Pkix.OrganizationUnit,
CommonName: d.Pkix.CommonName,
AlternativeNames: d.AltNames,
CA: d.CA,
PrivateKey: cert.PrivateKey,
SignatureAlg: d.HashAlg,
ValidFrom: d.ValidFrom(),
ValidTo: d.ValidTo(),
Usage: d.Usage,
}
cert.CertTemplate = certificate.CreateCertificateTemplate(template)
}
}
func (c *Certs) signAll() {
for {
sign := findSigners(c)
if len(sign) == 0 {
break
}
for _, s := range sign {
id := s.CertConfig.Id
signer, _ := c.findByid(id)
list := c.certSigners[id]
for _, certId := range list {
cert, _ := c.findByid(certId)
cert.CertBytes = certificate.Sign(cert.CertTemplate, signer.CertTemplate, key.PublicKey(cert.PrivateKey), signer.PrivateKey)
cert.signed = true
if s.Signers == nil {
cert.Signers = []string{id}
} else {
cert.Signers = append(s.Signers, id)
}
}
}
}
}
func (c Certs) Output() {
for _, cert := range c.Certificates {
if cert.signed {
certificate.WritePemToFile(cert.CertBytes, cert.CertConfig.Id+"_crt.pem")
key.WritePrivateKeyToPemFile(cert.PrivateKey, cert.CertConfig.Id+"_key.pem")
}
if len(cert.Signers) > 0 {
fmt.Printf("Certificate: %s, has certificate chain: %v\n", cert.CertConfig.Id, strings.Join(cert.Signers, ", "))
}
if !cert.signed {
fmt.Printf("Failed to sign: %s\n", cert.CertConfig.Id)
}
}
}
func findSigners(c *Certs) []*Cert {
sign := []*Cert{}
for _, val := range c.Certificates {
if val.signed && !val.toBeUsed && val.CertConfig.CA {
sign = append(sign, val)
val.toBeUsed = true
}
}
return sign
}
func readFile(name string) []byte {
data, err := ioutil.ReadFile(name)
if err != nil {
log.Println("Could not read file: %s\n", name)
os.Exit(1)
}
return data
}