-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_keys.go
130 lines (110 loc) · 3.19 KB
/
generate_keys.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
120
121
122
123
124
125
126
127
128
129
130
// MARK: Key generation
// see https://golang.org/src/crypto/tls/generate_cert.go
// this is essentially the above with almost everything removed
// except the minimum code to generate a self-signed ed25519 key pair
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"os"
"time"
)
func publicKey(priv interface{}) interface{} {
switch k := priv.(type) {
case *ecdsa.PrivateKey:
return &k.PublicKey
default:
return nil
}
}
func pemBlockForKey(priv interface{}) *pem.Block {
switch k := priv.(type) {
case *ecdsa.PrivateKey:
b, err := x509.MarshalECPrivateKey(k)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to marshal ECDSA private key: %v", err)
os.Exit(2)
}
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
default:
return nil
}
}
func generate_cert(path string, name string) error {
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return err
}
notBefore := time.Now()
notAfter := notBefore.Add(10 * 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{"P-Drop Application"},
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
// hosts := strings.Split(*host, ",")
// for _, h := range hosts {
// if ip := net.ParseIP(h); ip != nil {
// template.IPAddresses = append(template.IPAddresses, ip)
// } else {
// template.DNSNames = append(template.DNSNames, h)
// }
// }
host, err := os.Hostname()
if err != nil {
return err
}
template.DNSNames = append(template.DNSNames, host)
// if *isCA {
// template.IsCA = true
// template.KeyUsage |= x509.KeyUsageCertSign
// }
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
if err != nil {
log.Fatalf("Failed to create certificate: %s", err)
}
certPath := path + "/" + name + ".crt"
keyPath := path + "/" + name + ".key"
certOut, err := os.Create(certPath)
if err != nil {
log.Fatalf("failed to open %s for writing: %s", certPath, err)
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
log.Fatalf("failed to write data to %s: %s", certPath, err)
}
if err := certOut.Close(); err != nil {
log.Fatalf("error closing %s: %s", certPath, err)
}
log.Printf("wrote %s\n", certPath)
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Print("failed to open %s for writing: %s", keyPath, err)
return err
}
if err := pem.Encode(keyOut, pemBlockForKey(priv)); err != nil {
log.Fatalf("failed to write data to %s: %s", keyPath, err)
}
if err := keyOut.Close(); err != nil {
log.Fatalf("error closing key.pem: %s", err)
}
log.Printf("wrote %s\n", keyPath)
return nil
}