-
Notifications
You must be signed in to change notification settings - Fork 3
/
tls.go
119 lines (105 loc) · 4.02 KB
/
tls.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
package common
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"time"
)
const EncryptionBits = 4096
const CertValidity = time.Hour * 24 * 365
func ValidDaysRemaining(pem []byte) int {
cert, err := x509.ParseCertificate(pem)
if err != nil {
Log.Warnf("could not parse certificate, %v", err)
return -1
}
return int(cert.NotAfter.Sub(time.Now()).Hours() / 24)
}
func GenerateSelfSignedCA(commonName string) ([]byte, []byte, error) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 256)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate serial number: %s", err)
}
template := &x509.Certificate{
SerialNumber: serialNumber,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
NotBefore: time.Now(),
NotAfter: time.Now().Add(CertValidity),
Subject: pkix.Name{CommonName: commonName},
IsCA: true,
MaxPathLenZero: true,
BasicConstraintsValid: true,
}
key, err := rsa.GenerateKey(rand.Reader, EncryptionBits)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate key: %s", err)
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return nil, nil, fmt.Errorf("failed to create certificate: %s", err)
}
var certPem, keyPem bytes.Buffer
if err := pem.Encode(&certPem, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return nil, nil, fmt.Errorf("failed to encode certificate: %s", err)
}
if err := pem.Encode(&keyPem, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}); err != nil {
return nil, nil, fmt.Errorf("failed to encode key: %s", err)
}
return certPem.Bytes(), keyPem.Bytes(), nil
}
func GenerateSignedCert(commonName string, dnsNames []string, caPem []byte, caKey []byte) ([]byte, []byte, error) {
key, err := rsa.GenerateKey(rand.Reader, EncryptionBits)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate key: %s", err)
}
csrDerBytes, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{}, key)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate csr: %s", err.Error())
}
csr, err := x509.ParseCertificateRequest(csrDerBytes)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse csr: %s", err.Error())
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 256)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate serial number: %s", err)
}
template := &x509.Certificate{
SerialNumber: serialNumber,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
NotBefore: time.Now(),
NotAfter: time.Now().Add(CertValidity),
Subject: pkix.Name{CommonName: commonName},
DNSNames: dnsNames,
}
caTlsPair, err := tls.X509KeyPair(caPem, caKey)
if err != nil {
return nil, nil, fmt.Errorf("failed to load CA key pair: %s", err)
}
ca, err := x509.ParseCertificate(caTlsPair.Certificate[0])
if err != nil {
return nil, nil, fmt.Errorf("failed to parse CA certificate: %s", err)
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, ca, csr.PublicKey, caTlsPair.PrivateKey)
if err != nil {
return nil, nil, fmt.Errorf("failed to create certificate: %s", err)
}
var certPem, keyPem bytes.Buffer
if err := pem.Encode(&certPem, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return nil, nil, fmt.Errorf("failed to encode certificate: %s", err)
}
if err := pem.Encode(&keyPem, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}); err != nil {
return nil, nil, fmt.Errorf("failed to encode key: %s", err)
}
return certPem.Bytes(), keyPem.Bytes(), nil
}