forked from volatiletech/abcweb
/
cert.go
67 lines (56 loc) · 1.71 KB
/
cert.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
package cert
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"math/big"
"time"
"github.com/spf13/afero"
)
// Template is a helper function to create a cert template with a
// serial number and other required fields
func Template(appName, commonName string) (*x509.Certificate, error) {
// generate a random serial number (a real cert authority would have some logic behind this)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, errors.New("failed to generate serial number: " + err.Error())
}
tmpl := x509.Certificate{
IsCA: true,
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{appName},
CommonName: commonName,
},
SignatureAlgorithm: x509.SHA256WithRSA,
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 365 * 4), // valid for 4 years
BasicConstraintsValid: true,
}
return &tmpl, nil
}
// WriteCertFile writes the cert.pem certificate file
func WriteCertFile(outFile afero.File, template *x509.Certificate, pub interface{}, priv interface{}) error {
certDER, err := x509.CreateCertificate(rand.Reader, template, template, pub, priv)
if err != nil {
return err
}
// PEM encode the certificate (this is a standard TLS encoding)
b := &pem.Block{
Type: "CERTIFICATE",
Bytes: certDER,
}
return pem.Encode(outFile, b)
}
// WritePrivateKey writes the private.key private key file
func WritePrivateKey(outFile afero.File, key *rsa.PrivateKey) error {
b := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
return pem.Encode(outFile, b)
}