/
generate.go
191 lines (156 loc) · 3.83 KB
/
generate.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"io/ioutil"
"log"
"math/big"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
)
var generateCmd = &cobra.Command{
Use: "generate",
Short: "generate a client-local TLS key to use for authentication",
Run: runGenerate,
}
var (
flagGenerateForce bool
)
func init() {
generateCmd.Flags().BoolVarP(&flagGenerateForce, "force", "f", false,
"overwrite an existing certificate")
}
func runGenerate(cmd *cobra.Command, args []string) {
if haveCertAndKey() && !flagGenerateForce {
log.Println("have existing certificate and key and --force was not given")
return
}
cert, key, err := generateCertificate()
if err != nil {
log.Printf("could not generate certificate and key: %s", err)
os.Exit(1)
}
err = saveCertAndKey(cert, key)
if err != nil {
log.Printf("could not save certificate and key: %s", err)
os.Exit(1)
}
log.Println("successfully generated certificate and key")
}
func getKCPaths() (certPath, keyPath string) {
rootPath := os.ExpandEnv(filepath.Join("$HOME", ".ffsend"))
certPath = filepath.Join(rootPath, "cert.pem")
keyPath = filepath.Join(rootPath, "key.pem")
return
}
func haveCertAndKey() bool {
certPath, keyPath := getKCPaths()
for _, path := range []string{certPath, keyPath} {
st, err := os.Stat(path)
if err != nil {
return false
}
if !st.Mode().IsRegular() {
return false
}
}
return true
}
func loadCertAndKey() (cert []byte, key []byte, err error) {
certPath, keyPath := getKCPaths()
cert, err = ioutil.ReadFile(certPath)
if err != nil {
return nil, nil, err
}
key, err = ioutil.ReadFile(keyPath)
if err != nil {
return nil, nil, err
}
return cert, key, nil
}
func saveCertAndKey(cert, key []byte) error {
certPath, keyPath := getKCPaths()
// Write to files.
if err := os.MkdirAll(filepath.Dir(certPath), 0700); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(keyPath), 0700); err != nil {
return err
}
_ = os.Remove(certPath)
_ = os.Remove(keyPath)
if err := ioutil.WriteFile(certPath, cert, 0600); err != nil {
return err
}
if err := ioutil.WriteFile(keyPath, key, 0600); err != nil {
return err
}
return nil
}
// Generates a new self-signed private/public key and returns (cert, key, error).
func generateCertificate() ([]byte, []byte, error) {
template := x509.Certificate{
Subject: pkix.Name{
Organization: []string{"ffsend"},
},
NotBefore: time.Now(),
KeyUsage: x509.KeyUsageCertSign |
x509.KeyUsageKeyEncipherment |
x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
x509.ExtKeyUsageClientAuth,
},
BasicConstraintsValid: true,
IsCA: true,
}
// Set the common name to our hostname.
host, err := os.Hostname()
if err != nil {
return nil, nil, err
}
template.Subject.CommonName = host
// TODO: maybe append to IPAddresses / DNSNames with interfaces?
// We expire after a year.
template.NotAfter = template.NotBefore.Add(time.Hour * 24 * 365)
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
template.SerialNumber, err = rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, err
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
return nil, nil, err
}
// Great, all success!
var certBuf, keyBuf bytes.Buffer
if err = pem.Encode(
&certBuf,
&pem.Block{
Type: "CERTIFICATE",
Bytes: derBytes,
},
); err != nil {
return nil, nil, err
}
if err = pem.Encode(
&keyBuf,
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
},
); err != nil {
return nil, nil, err
}
return certBuf.Bytes(), keyBuf.Bytes(), nil
}