/
certutils.go
90 lines (79 loc) · 2.62 KB
/
certutils.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
package keys
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"time"
)
// This was taken from https://golang.org/src/crypto/tls/generate_cert.go.
// All credit to the Go Authors.
func pemBlockForKey(priv interface{}) (*pem.Block, error) {
switch k := priv.(type) {
case *rsa.PrivateKey:
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}, nil
case *ecdsa.PrivateKey:
b, err := x509.MarshalECPrivateKey(k)
if err != nil {
return nil, err
}
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}, nil
default:
return nil, nil
}
}
// SerializeCertificate serializes a TLS certificate into the cert and key PEM
// files.
func SerializeCertificate(certificate *tls.Certificate) (*pem.Block, *pem.Block, error) {
certpem := &pem.Block{Type: "CERTIFICATE", Bytes: certificate.Certificate[0]}
keypem, err := pemBlockForKey(certificate.PrivateKey)
return certpem, keypem, err
}
// SelfSignedCertificate generates a self-signed certificate.
// Much of this is from https://golang.org/src/crypto/tls/generate_cert.go.
// All credit to the Go Authors.
func SelfSignedCertificate(dnsNames []string) (*pem.Block, *pem.Block, error) {
privkey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, err
}
now := time.Now()
template := &x509.Certificate{
IsCA: true,
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: "default.autocert.smartgrid.store",
Country: []string{"United States of America"},
Organization: []string{"University of California, Berkeley"},
OrganizationalUnit: []string{"Software Defined Buildings"},
Locality: []string{"Berkeley"},
Province: []string{"California"},
StreetAddress: []string{"410 Soda Hall"},
},
NotBefore: now.Add(-time.Hour),
NotAfter: now.Add(time.Hour * 24 * 365),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: dnsNames,
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, &privkey.PublicKey, privkey)
if err != nil {
return nil, nil, err
}
cert := &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}
key, err := pemBlockForKey(privkey)
if err != nil {
return nil, nil, err
}
return cert, key, nil
}