-
Notifications
You must be signed in to change notification settings - Fork 5
/
certify.go
199 lines (166 loc) · 5.87 KB
/
certify.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package certify
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"net"
"strings"
"time"
)
// Certificate hold certificate information
type Certificate struct {
SerialNumber *big.Int
Subject pkix.Name
NotBefore time.Time
NotAfter time.Time
IPAddress []net.IP
DNSNames []string
IsCA bool
Parent *x509.Certificate
ParentPrivateKey interface{}
KeyUsage x509.KeyUsage
ExtentedKeyUsage []x509.ExtKeyUsage
SubjectKeyId []byte
AuthorityKeyId []byte
}
// Result hold created certificate in []byte format
type Result struct {
ByteCert []byte
Cert *x509.Certificate
}
// GetSerial returns serial and an error
func GetSerial() (*big.Int, error) {
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return nil, err
}
return serial, nil
}
// SetTemplate set template for x509.Certificate from given Certificate struct
func (c *Certificate) SetTemplate() x509.Certificate {
return x509.Certificate{
SerialNumber: c.SerialNumber,
Subject: c.Subject,
NotBefore: c.NotBefore,
NotAfter: c.NotAfter,
ExtKeyUsage: c.ExtentedKeyUsage,
KeyUsage: c.KeyUsage,
IsCA: c.IsCA,
IPAddresses: c.IPAddress,
DNSNames: c.DNSNames,
BasicConstraintsValid: true,
SubjectKeyId: c.SubjectKeyId,
AuthorityKeyId: c.AuthorityKeyId,
}
}
// GetCertificate generate certificate and returns it in Result struct
func (c *Certificate) GetCertificate(pkey *ecdsa.PrivateKey) (*Result, error) {
serial, err := GetSerial()
if err != nil {
return nil, err
}
c.SerialNumber = serial
template := c.SetTemplate()
if c.Parent == nil {
c.Parent = &template
}
if c.ParentPrivateKey == nil {
c.ParentPrivateKey = pkey
}
der, err := x509.CreateCertificate(rand.Reader, &template, c.Parent, &pkey.PublicKey, c.ParentPrivateKey)
if err != nil {
return nil, err
}
return &Result{ByteCert: der, Cert: c.Parent}, nil
}
// String returns certificate in string format
func (r *Result) String() string {
var w bytes.Buffer
if err := pem.Encode(&w, &pem.Block{
Type: "CERTIFICATE",
Bytes: r.ByteCert,
}); err != nil {
return ""
}
return w.String()
}
// ParseCertificate returns parsed certificate and error
func ParseCertificate(cert []byte) (*x509.Certificate, error) {
p, _ := pem.Decode(cert)
if p == nil {
return nil, fmt.Errorf("can't decode CA cert file")
}
c, err := x509.ParseCertificate(p.Bytes)
if err != nil {
return nil, err
}
return c, nil
}
// CertInfo returns certificate information
func CertInfo(cert *x509.Certificate) string {
var buf bytes.Buffer
buf.WriteString("Certificate\n")
buf.WriteString(fmt.Sprintf("%4sData:\n", ""))
buf.WriteString(fmt.Sprintf("%8sVersion: %d\n", "", cert.Version))
buf.WriteString(fmt.Sprintf("%8sSerial Number:\n%12s%v\n", "", "", formatKeyIDWithColon(cert.SerialNumber.Bytes())))
buf.WriteString(fmt.Sprintf("%8sSignature Algorithm: %v\n", "", cert.SignatureAlgorithm))
buf.WriteString(fmt.Sprintf("%8sIssuer: %v\n", "", strings.Replace(cert.Issuer.String(), ",", ", ", -1)))
buf.WriteString(fmt.Sprintf("%8sValidity:\n", ""))
buf.WriteString(fmt.Sprintf("%12sNotBefore: %v\n", "", cert.NotBefore.Format("Jan 2 15:04:05 2006 GMT")))
buf.WriteString(fmt.Sprintf("%12sNotAfter : %v\n", "", cert.NotAfter.Format("Jan 2 15:04:05 2006 GMT")))
buf.WriteString(fmt.Sprintf("%8sSubject: %v\n", "", strings.Replace(cert.Subject.String(), ",", ", ", -1)))
buf.WriteString(fmt.Sprintf("%8sSubject Public Key Info:\n", ""))
buf.WriteString(fmt.Sprintf("%12sPublic Key Algorithm: %v\n", "", cert.PublicKeyAlgorithm))
if cert.PublicKeyAlgorithm == x509.ECDSA {
if ecdsakey, ok := cert.PublicKey.(*ecdsa.PublicKey); ok {
buf.WriteString(fmt.Sprintf("%16sPublic Key: (%d bit)\n", "", ecdsakey.Params().BitSize))
buf.WriteString(fmt.Sprintf("%16sNIST Curve: %s\n", "", ecdsakey.Params().Name))
}
}
if cert.PublicKeyAlgorithm == x509.RSA {
if rsakey, ok := cert.PublicKey.(*rsa.PublicKey); ok {
buf.WriteString(fmt.Sprintf("%16sRSA Public-Key: (%d bit)\n", "", rsakey.N.BitLen()))
buf.WriteString(fmt.Sprintf("%16sExponent: %d (%#x)\n", "", rsakey.E, rsakey.E))
}
}
buf.WriteString(fmt.Sprintf("%8sX509v3 extensions:\n", ""))
if len(parseExtKeyUsage(cert.ExtKeyUsage)) != 0 {
buf.WriteString(fmt.Sprintf("%12sX509v3 Extended Key Usage:\n", ""))
buf.WriteString(fmt.Sprintf("%16s%v\n", "", parseExtKeyUsage(cert.ExtKeyUsage)))
}
if cert.KeyUsage != 0 {
buf.WriteString(fmt.Sprintf("%12sX509v3 Key Usage:\n", ""))
buf.WriteString(fmt.Sprintf("%16s%v\n", "", strings.Join(parseKeyUsage(cert.KeyUsage), ", ")))
}
buf.WriteString(fmt.Sprintf("%12sX509v3 Basic Constraints:\n", ""))
buf.WriteString(fmt.Sprintf("%16sCA: %v\n", "", cert.IsCA))
if cert.SubjectKeyId != nil {
buf.WriteString(fmt.Sprintf("%12sX509v3 Subject Key Identifier:\n", ""))
buf.WriteString(fmt.Sprintf("%16s%v\n", "", formatKeyIDWithColon(cert.SubjectKeyId)))
}
if cert.AuthorityKeyId != nil {
buf.WriteString(fmt.Sprintf("%12sX509v3 Authority Key Identifier:\n", ""))
buf.WriteString(fmt.Sprintf("%16s%v\n", "", formatKeyIDWithColon(cert.AuthorityKeyId)))
}
if len(cert.IPAddresses) != 0 || len(cert.DNSNames) != 0 {
buf.WriteString(fmt.Sprintf("%12sX509v3 Subject Alternative Name:\n", ""))
if len(cert.IPAddresses) != 0 {
var ips []string
for _, ip := range cert.IPAddresses {
ips = append(ips, ip.String())
}
buf.WriteString(fmt.Sprintf("%16sIP Address: %v\n", "", strings.Join(ips, ", ")))
}
if len(cert.DNSNames) != 0 {
buf.WriteString(fmt.Sprintf("%16sDNS: %v\n", "", strings.Join(cert.DNSNames, ", ")))
}
}
buf.WriteString(fmt.Sprintf("%4sSignature Algorithm: %v\n", "", cert.SignatureAlgorithm))
return buf.String()
}