-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgencert.go
86 lines (76 loc) · 2.28 KB
/
gencert.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"log"
"math/big"
"net"
"os"
"path/filepath"
"strings"
"time"
)
func pemBlockForKey(priv *rsa.PrivateKey) *pem.Block {
return &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
}
}
func genCert(hosts string) {
priv, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
log.Fatalf("failed to generate private key: %s", err)
}
notBefore := time.Now()
notAfter := notBefore.Add(365 * 24 * time.Hour)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Fatalf("failed to generate serial number: %s", err)
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Acme Co"},
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
for _, h := range strings.Split(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.Fatalf("Failed to create certificate: %s", err)
}
crt, err := os.Create(filepath.Join(usrDir, ".pchat", i+".crt"))
if err != nil {
log.Fatalf("failed to open %s.crt for writing: %s", i, err)
}
if err := pem.Encode(crt, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
log.Fatalf("failed to write data to %s.crt: %s", i, err)
}
if err := crt.Close(); err != nil {
log.Fatalf("error closing %s.crt: %s", i, err)
}
key, err := os.OpenFile(filepath.Join(usrDir, ".pchat", i+".key"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("failed to open %s.key for writing: %s", i, err)
}
if err := pem.Encode(key, pemBlockForKey(priv)); err != nil {
log.Fatalf("failed to write data to %s.key: %s", i, err)
}
if err := key.Close(); err != nil {
log.Fatalf("error closing %s.key: %s", i, err)
}
}