-
Notifications
You must be signed in to change notification settings - Fork 3
/
lc-tlscert.go
204 lines (174 loc) · 5.49 KB
/
lc-tlscert.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
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2014 Jason Woods.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Derived from Golang src/pkg/crypto/tls/generate_cert.go
* Copyright 2009 The Go Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package main
import (
"bufio"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"net"
"os"
"strconv"
"time"
)
var input *bufio.Reader
func init() {
input = bufio.NewReader(os.Stdin)
}
func readString(prompt string) string {
fmt.Printf("%s: ", prompt)
var line []byte
for {
data, prefix, _ := input.ReadLine()
line = append(line, data...)
if !prefix {
break
}
}
return string(line)
}
func readNumber(prompt string) (num int64) {
var err error
for {
if num, err = strconv.ParseInt(readString(prompt), 0, 64); err != nil {
fmt.Println("Please enter a valid numerical value")
continue
}
break
}
return
}
func anyKey() {
input.ReadRune()
}
func main() {
var err error
template := x509.Certificate{
Subject: pkix.Name{
Organization: []string{"Log Courier"},
},
NotBefore: time.Now(),
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
}
fmt.Println("Specify the Common Name for the certificate. The common name")
fmt.Println("can be anything, but is usually set to the server's primary")
fmt.Println("DNS name. Even if you plan to connect via IP address you")
fmt.Println("should specify the DNS name here.")
fmt.Println()
template.Subject.CommonName = readString("Common name")
fmt.Println()
fmt.Println("The next step is to add any additional DNS names and IP")
fmt.Println("addresses that clients may use to connect to the server. If")
fmt.Println("you plan to connect to the server via IP address and not DNS")
fmt.Println("then you must specify those IP addresses here.")
fmt.Println("When you are finished, just press enter.")
fmt.Println()
var cnt = 0
var val string
for {
cnt++
if val = readString(fmt.Sprintf("DNS or IP address %d", cnt)); val == "" {
break
}
if ip := net.ParseIP(val); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, val)
}
}
fmt.Println()
fmt.Println("How long should the certificate be valid for? A year (365")
fmt.Println("days) is usual but requires the certificate to be regenerated")
fmt.Println("within a year or the certificate will cease working.")
fmt.Println()
template.NotAfter = template.NotBefore.Add(time.Duration(readNumber("Number of days")) * time.Hour * 24)
fmt.Println("Common name:", template.Subject.CommonName)
fmt.Println("DNS SANs:")
if len(template.DNSNames) == 0 {
fmt.Println(" None")
} else {
for _, e := range template.DNSNames {
fmt.Println(" ", e)
}
}
fmt.Println("IP SANs:")
if len(template.IPAddresses) == 0 {
fmt.Println(" None")
} else {
for _, e := range template.IPAddresses {
fmt.Println(" ", e)
}
}
fmt.Println()
fmt.Println("The certificate can now be generated")
fmt.Println("Press any key to begin generating the self-signed certificate.")
anyKey()
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println("Failed to generate private key:", err)
os.Exit(1)
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
template.SerialNumber, err = rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
fmt.Println("Failed to generate serial number:", err)
os.Exit(1)
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
fmt.Println("Failed to create certificate:", err)
os.Exit(1)
}
certOut, err := os.Create("selfsigned.crt")
if err != nil {
fmt.Println("Failed to open selfsigned.pem for writing:", err)
os.Exit(1)
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close()
keyOut, err := os.OpenFile("selfsigned.key", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Println("failed to open selfsigned.key for writing:", err)
os.Exit(1)
}
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
keyOut.Close()
fmt.Println("Successfully generated certificate")
fmt.Println(" Certificate: selfsigned.crt")
fmt.Println(" Private Key: selfsigned.key")
fmt.Println()
fmt.Println("Copy and paste the following into your Log Courier")
fmt.Println("configuration, adjusting paths as necessary:")
fmt.Println(" \"transport\": \"tls\",")
fmt.Println(" \"ssl ca\": \"path/to/selfsigned.crt\",")
fmt.Println()
fmt.Println("Copy and paste the following into your LogStash configuration, ")
fmt.Println("adjusting paths as necessary:")
fmt.Println(" ssl_certificate => \"path/to/selfsigned.crt\",")
fmt.Println(" ssl_key => \"path/to/selfsigned.key\",")
}