forked from Cloudxtreme/wiki-9
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tls.go
89 lines (74 loc) · 2.14 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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"log"
"math/big"
"net"
"time"
)
func (s *Server) generateTLSConfig(hosts []string) (*tls.Config, error) {
cert, err := generateCertificate(hosts)
if err != nil {
return nil, err
}
return &tls.Config{Certificates: []tls.Certificate{*cert}}, nil
}
// mostly from https://golang.org/src/crypto/tls/generate_cert.go
func generateCertificate(hosts []string) (*tls.Certificate, error) {
rsaBits := 2048
validFor := 365 * 24 * time.Hour
priv, err := rsa.GenerateKey(rand.Reader, rsaBits)
if err != nil {
log.Printf("Failed to generate private key: %v", err)
return nil, err
}
keyUsage := x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment
notBefore := time.Now()
notAfter := notBefore.Add(validFor)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Printf("Failed to generate serial number: %v", err)
return nil, err
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Acme Co"},
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: keyUsage,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
log.Printf("Failed to create certificate: %v", err)
return nil, err
}
certPem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
log.Printf("Unable to marshal private key: %v", err)
return nil, err
}
keyPem := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
cert, err := tls.X509KeyPair(certPem, keyPem)
if err != nil {
return nil, err
}
return &cert, nil
}