-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkcert.go
162 lines (141 loc) · 3.75 KB
/
mkcert.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
package testca
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
crand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"io"
"math"
"math/big"
"math/rand"
"time"
"github.com/pkg/errors"
)
// MakeSelfCertECDSA creates self-signed cert
func MakeSelfCertECDSA(hours int) (*x509.Certificate, crypto.PrivateKey, error) {
// key pair
key, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
if err != nil {
return nil, nil, errors.WithStack(err)
}
// certificate
certTemplate := &x509.Certificate{
SerialNumber: big.NewInt(rand.Int63n(math.MaxInt64)),
Subject: pkix.Name{
CommonName: "localhost",
},
NotBefore: time.Now().UTC().Add(-time.Hour),
NotAfter: time.Now().UTC().Add(time.Hour * time.Duration(hours)),
}
der, err := x509.CreateCertificate(crand.Reader, certTemplate, certTemplate, &key.PublicKey, key)
if err != nil {
return nil, nil, errors.WithStack(err)
}
crt, err := x509.ParseCertificate(der)
if err != nil {
return nil, nil, errors.WithStack(err)
}
return crt, key, nil
}
// MakeSelfCertECDSAPem creates self-signed cert in PEM format
func MakeSelfCertECDSAPem(hours int) (pemCert, pemKey []byte, err error) {
crt, key, err := MakeSelfCertECDSA(hours)
if err != nil {
return nil, nil, errors.WithStack(err)
}
keyBytes, err := x509.MarshalECPrivateKey(key.(*ecdsa.PrivateKey))
if err != nil {
return nil, nil, errors.WithStack(err)
}
pemKey = pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: keyBytes,
})
pemCert = pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: crt.Raw,
})
return
}
// MakeSelfCertRSA creates self-signed cert
func MakeSelfCertRSA(hours int) (*x509.Certificate, crypto.PrivateKey, error) {
// rsa key pair
key, err := rsa.GenerateKey(crand.Reader, 512)
if err != nil {
return nil, nil, errors.WithStack(err)
}
// certificate
certTemplate := &x509.Certificate{
SerialNumber: big.NewInt(rand.Int63n(math.MaxInt64)),
Subject: pkix.Name{
CommonName: "localhost",
},
NotBefore: time.Now().UTC().Add(-time.Hour),
NotAfter: time.Now().UTC().Add(time.Hour * time.Duration(hours)),
}
der, err := x509.CreateCertificate(crand.Reader, certTemplate, certTemplate, &key.PublicKey, key)
if err != nil {
return nil, nil, errors.WithStack(err)
}
crt, err := x509.ParseCertificate(der)
if err != nil {
return nil, nil, errors.WithStack(err)
}
return crt, key, nil
}
// MakeSelfCertRSAPem creates self-signed cert in PEM format
func MakeSelfCertRSAPem(hours int) (pemCert, pemKey []byte, err error) {
crt, key, err := MakeSelfCertRSA(hours)
if err != nil {
return nil, nil, errors.WithStack(err)
}
pemKey = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key.(*rsa.PrivateKey)),
})
pemCert = pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: crt.Raw,
})
return
}
// GenerateRSAKeyInPEM returns PEM encoded RSA key
func GenerateRSAKeyInPEM(rand io.Reader, size int) ([]byte, error) {
if rand == nil {
rand = crand.Reader
}
// key pair
key, err := rsa.GenerateKey(rand, size)
if err != nil {
return nil, errors.WithStack(err)
}
pemKey := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
return pemKey, nil
}
// GenerateECDSAKeyInPEM returns PEM encoded ECDSA key
func GenerateECDSAKeyInPEM(rand io.Reader, c elliptic.Curve) ([]byte, error) {
if rand == nil {
rand = crand.Reader
}
// key pair
key, err := ecdsa.GenerateKey(c, rand)
if err != nil {
return nil, errors.WithStack(err)
}
keyBytes, err := x509.MarshalECPrivateKey(key)
if err != nil {
return nil, errors.WithStack(err)
}
pemKey := pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: keyBytes,
})
return pemKey, nil
}