forked from kubernetes-retired/kube-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509.go
133 lines (117 loc) · 3.59 KB
/
x509.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
package tlsutil
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"math"
"math/big"
"net"
"time"
)
var (
Duration365d = time.Hour * 24 * 365
)
type CACertConfig struct {
CommonName string
Organization string
Duration time.Duration
}
type ServerCertConfig struct {
CommonName string
DNSNames []string
IPAddresses []string
Duration time.Duration
}
type ClientCertConfig struct {
CommonName string
Organization []string
DNSNames []string
IPAddresses []string
Duration time.Duration
}
func NewSelfSignedCACertificate(cfg CACertConfig, key *rsa.PrivateKey) (*x509.Certificate, error) {
if cfg.Duration <= 0 {
return nil, errors.New("Self-signed CA cert duration must not be negative or zero.")
}
tmpl := x509.Certificate{
SerialNumber: new(big.Int).SetInt64(0),
Subject: pkix.Name{
CommonName: cfg.CommonName,
Organization: []string{cfg.Organization},
},
NotBefore: time.Now().UTC(),
NotAfter: time.Now().Add(cfg.Duration).UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
}
certDERBytes, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, key.Public(), key)
if err != nil {
return nil, err
}
return x509.ParseCertificate(certDERBytes)
}
func NewSignedServerCertificate(cfg ServerCertConfig, key *rsa.PrivateKey, caCert *x509.Certificate, caKey *rsa.PrivateKey) (*x509.Certificate, error) {
ips := make([]net.IP, len(cfg.IPAddresses))
for i, ipStr := range cfg.IPAddresses {
ips[i] = net.ParseIP(ipStr)
}
serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
if cfg.Duration <= 0 {
return nil, errors.New("Signed server cert duration must not be negative or zero.")
}
certTmpl := x509.Certificate{
Subject: pkix.Name{
CommonName: cfg.CommonName,
Organization: caCert.Subject.Organization,
},
DNSNames: cfg.DNSNames,
IPAddresses: ips,
SerialNumber: serial,
NotBefore: caCert.NotBefore,
NotAfter: time.Now().Add(cfg.Duration).UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
certDERBytes, err := x509.CreateCertificate(rand.Reader, &certTmpl, caCert, key.Public(), caKey)
if err != nil {
return nil, err
}
return x509.ParseCertificate(certDERBytes)
}
func NewSignedClientCertificate(cfg ClientCertConfig, key *rsa.PrivateKey, caCert *x509.Certificate, caKey *rsa.PrivateKey) (*x509.Certificate, error) {
ips := make([]net.IP, len(cfg.IPAddresses))
for i, ipStr := range cfg.IPAddresses {
ips[i] = net.ParseIP(ipStr)
}
serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
if cfg.Duration <= 0 {
return nil, errors.New("Signed client cert duration must not be negative or zero.")
}
certTmpl := x509.Certificate{
Subject: pkix.Name{
CommonName: cfg.CommonName,
Organization: append(caCert.Subject.Organization, cfg.Organization...),
},
DNSNames: cfg.DNSNames,
IPAddresses: ips,
SerialNumber: serial,
NotBefore: caCert.NotBefore,
NotAfter: time.Now().Add(cfg.Duration).UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
certDERBytes, err := x509.CreateCertificate(rand.Reader, &certTmpl, caCert, key.Public(), caKey)
if err != nil {
return nil, err
}
return x509.ParseCertificate(certDERBytes)
}