Skip to content

Commit 9d49846

Browse files
author
Zach Brown
committed
Initial commit
0 parents  commit 9d49846

File tree

7 files changed

+330
-0
lines changed

7 files changed

+330
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Zach Brown <zach@prozach.org>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# CertTools
2+
3+
[![GoDoc Widget]][GoDoc]
4+
5+
CertTools is a set of settings and tools for TLS Certificates.
6+
7+
It includes parsers for taking strings/config options and enabling minimum TLS versions and Cipher Suites.
8+
It also includes some decent static defaults which can be used when creating a server.
9+
```
10+
server := &http.Server{
11+
Addr: ":8443",
12+
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
13+
w.Header().Set("Content-Type", "text/plain")
14+
w.Write([]byte("This is an example server.\n"))
15+
}),
16+
TLSConfig: &tls.Config{
17+
Certificates: []tls.Certificate{cert},
18+
MinVersion: autocert.SecureTLSMinVersion(),
19+
CipherSuites: autocert.SecureTLSCipherSuites(),
20+
},
21+
}
22+
```
23+
24+
CertTools can be used to programatically generate POTENTIALLY INSECURE certificates that can be used for your web server.
25+
It's horribly insecure and is designed basically for the situation where you don't want to mess with cert files, you want
26+
to use tls/https and you don't want to have the cert be different every time you load your app.

autocert.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package certtools
2+
3+
import (
4+
"crypto/ecdsa"
5+
"crypto/elliptic"
6+
"crypto/tls"
7+
"crypto/x509"
8+
"crypto/x509/pkix"
9+
"encoding/pem"
10+
"fmt"
11+
"io"
12+
"math/big"
13+
"time"
14+
)
15+
16+
// InsecureGlobalStatic is a non-random byte reader that can be used to generaate an insecure private key
17+
// This will generate the same bytes on every box (all zeros). It is horribly insecure.
18+
type InsecureGlobalStatic struct{}
19+
20+
func InsecureGlobalStaticReader() InsecureGlobalStatic {
21+
return InsecureGlobalStatic{}
22+
}
23+
24+
func (r InsecureGlobalStatic) Read(s []byte) (int, error) {
25+
// Set it to all zeros
26+
l := len(s)
27+
for x := 0; x < l; x++ {
28+
s[x] = 0
29+
}
30+
return l, nil
31+
}
32+
33+
// InsecureString is a non-random bytes reader that can be used to generate an insecure private key based on a provided string
34+
// The upside of this is that the same string input should yield the same bytes so you can send in something like the hostname
35+
// and it will generate the same output everytime you run your program.
36+
// The downside is that it is very insecure and should only be used for testing
37+
type InsecureString struct {
38+
seed []byte
39+
pos int
40+
length int
41+
}
42+
43+
func InsecureStringReader(seed string) *InsecureString {
44+
// Ensure there is at least one character in seed
45+
if len(seed) == 0 {
46+
seed = " "
47+
}
48+
return &InsecureString{
49+
seed: []byte(seed),
50+
pos: 0,
51+
length: len(seed),
52+
}
53+
}
54+
func (r *InsecureString) Read(s []byte) (int, error) {
55+
// Just repead the string over and over
56+
l := len(s)
57+
for x := 0; x < l; x++ {
58+
s[x] = r.seed[r.pos%r.length]
59+
r.pos++
60+
}
61+
return l, nil
62+
}
63+
64+
// AutoCert generates a self-signed cert using the specified private key mechanism
65+
func AutoCert(commonName string, orgName string, orgUnitName string, dnsNames []string, notBefore time.Time, notAfter time.Time, keyReader io.Reader) (tls.Certificate, error) {
66+
67+
if commonName == "" {
68+
return tls.Certificate{}, fmt.Errorf("commonName must not be blank")
69+
}
70+
71+
// Generate the key
72+
privKey, err := ecdsa.GenerateKey(elliptic.P256(), keyReader)
73+
if err != nil {
74+
return tls.Certificate{}, fmt.Errorf("Could not generate private key: %v\n", err)
75+
}
76+
77+
// Build Cert
78+
cert := x509.Certificate{
79+
SerialNumber: big.NewInt(0),
80+
Subject: pkix.Name{
81+
CommonName: commonName,
82+
},
83+
NotBefore: notBefore,
84+
NotAfter: notAfter,
85+
86+
IsCA: true,
87+
88+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
89+
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
90+
91+
BasicConstraintsValid: true,
92+
}
93+
94+
// If dnsNames is nil, use common name
95+
if dnsNames == nil {
96+
cert.DNSNames = []string{commonName}
97+
} else {
98+
cert.DNSNames = dnsNames
99+
}
100+
101+
if orgName != "" {
102+
cert.Subject.Organization = []string{orgName}
103+
}
104+
if orgUnitName != "" {
105+
cert.Subject.OrganizationalUnit = []string{orgUnitName}
106+
}
107+
108+
// Create Cert
109+
derBytes, err := x509.CreateCertificate(keyReader, &cert, &cert, &privKey.PublicKey, privKey)
110+
if err != nil {
111+
return tls.Certificate{}, fmt.Errorf("Failed to create certificate: %s", err)
112+
}
113+
114+
pKeyBytes, err := x509.MarshalECPrivateKey(privKey)
115+
if err != nil {
116+
return tls.Certificate{}, fmt.Errorf("Failed to marshal private key: %s", err)
117+
}
118+
119+
certBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
120+
privBytes := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: pKeyBytes})
121+
122+
tlsCert, err := tls.X509KeyPair(certBytes, privBytes)
123+
if err != nil {
124+
return tls.Certificate{}, fmt.Errorf("Failed to load key-pair: %s", err)
125+
}
126+
127+
return tlsCert, nil
128+
}

example/example.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"crypto/tls"
5+
"flag"
6+
"fmt"
7+
"net"
8+
"net/http"
9+
"os"
10+
"time"
11+
12+
"github.com/snowzach/certtools"
13+
)
14+
15+
func main() {
16+
17+
// Get the hostname
18+
hostname, err := os.Hostname()
19+
if err != nil {
20+
hostname = "localhost"
21+
}
22+
23+
address := flag.String("l", ":1234", "What address to listen on")
24+
cn := flag.String("cn", hostname, "The common name for the certificate")
25+
o := flag.String("o", "", "The org for the certificate")
26+
ou := flag.String("ou", "", "The org unit for the certificate")
27+
28+
// Good starting at unix epoch for 100 years
29+
var notBefore time.Time
30+
var notAfter time.Time = notBefore.Add(100 * 365 * 24 * time.Hour)
31+
32+
// This will generate the same certificate every time it is run on the same host
33+
cert, err := certtools.AutoCert(*cn, *o, *ou, nil, notBefore, notAfter, certtools.InsecureStringReader(hostname))
34+
35+
// Build the server and manually specify TLS Config
36+
server := &http.Server{
37+
Addr: *address,
38+
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
39+
w.Header().Set("Content-Type", "text/plain")
40+
w.Write([]byte("This is an example server.\n"))
41+
}),
42+
TLSConfig: &tls.Config{
43+
Certificates: []tls.Certificate{cert},
44+
MinVersion: certtools.SecureTLSMinVersion(),
45+
CipherSuites: certtools.SecureTLSCipherSuites(),
46+
},
47+
}
48+
49+
// Listen
50+
listener, err := net.Listen("tcp", *address)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
fmt.Printf("Listening on %s\n", *address)
56+
57+
// Serve
58+
if err := server.Serve(tls.NewListener(listener, server.TLSConfig)); err != nil {
59+
panic(err)
60+
}
61+
62+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module github.com/snowzach/certtools

securecert.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package certtools
2+
3+
import (
4+
"crypto/tls"
5+
"fmt"
6+
)
7+
8+
var (
9+
10+
// tlsVersions is a string array of TLS versions suitable for use by tlsMinVersion
11+
tlsVersions = map[string]uint16{
12+
`VersionTLS10`: tls.VersionTLS10,
13+
`VersionTLS11`: tls.VersionTLS11,
14+
`VersionTLS12`: tls.VersionTLS12,
15+
}
16+
17+
defaultTLSMinVersion uint16 = tls.VersionTLS12
18+
19+
// tlsCipherSuites is a map of TLS CipherSuites from crypto/tls
20+
// Available CipherSuites defined at https://golang.org/pkg/crypto/tls/#pkg-constants
21+
tlsCipherSuites = map[string]uint16{
22+
`TLS_RSA_WITH_RC4_128_SHA`: tls.TLS_RSA_WITH_RC4_128_SHA,
23+
`TLS_RSA_WITH_3DES_EDE_CBC_SHA`: tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
24+
`TLS_RSA_WITH_AES_128_CBC_SHA`: tls.TLS_RSA_WITH_AES_128_CBC_SHA,
25+
`TLS_RSA_WITH_AES_256_CBC_SHA`: tls.TLS_RSA_WITH_AES_256_CBC_SHA,
26+
`TLS_RSA_WITH_AES_128_CBC_SHA256`: tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
27+
`TLS_RSA_WITH_AES_128_GCM_SHA256`: tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
28+
`TLS_RSA_WITH_AES_256_GCM_SHA384`: tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
29+
`TLS_ECDHE_ECDSA_WITH_RC4_128_SHA`: tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
30+
`TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA`: tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
31+
`TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA`: tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
32+
`TLS_ECDHE_RSA_WITH_RC4_128_SHA`: tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
33+
`TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA`: tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
34+
`TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA`: tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
35+
`TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA`: tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
36+
`TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256`: tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
37+
`TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256`: tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
38+
`TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256`: tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
39+
`TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256`: tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
40+
`TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384`: tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
41+
`TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384`: tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
42+
`TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305`: tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
43+
`TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305`: tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
44+
}
45+
46+
// defaultTLSCipherSuites is the A+ and A list of suites from: https://www.owasp.org/index.php/TLS_Cipher_String_Cheat_Sheet
47+
defaultTLSCipherSuites = []uint16{
48+
// These are compatible with grpc
49+
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
50+
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
51+
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
52+
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
53+
// These are compatible with autocert
54+
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
55+
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
56+
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
57+
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
58+
}
59+
)
60+
61+
// SecureTLSMinVersion returns a secure TLSMinVersion=defaultTLSMinVersion
62+
func SecureTLSMinVersion() uint16 {
63+
return defaultTLSMinVersion
64+
}
65+
66+
// This takes a string TLS version and converts it into the go tls version. A blank version will return defaultTLSMinVersion
67+
func ParseTLSVersion(version string) (uint16, error) {
68+
// Blank/Default value is tls.VersionTLS12
69+
if version == "" {
70+
return defaultTLSMinVersion, nil
71+
}
72+
// Otherwise parse values
73+
if value, ok := tlsVersions[version]; ok {
74+
return value, nil
75+
}
76+
return defaultTLSMinVersion, fmt.Errorf("Unknown tls version: %s", version)
77+
}
78+
79+
// SecureTLSCipherSuites returns secure TLSCipherSuites=defaultTLSCipherSuites
80+
func SecureTLSCipherSuites() []uint16 {
81+
return defaultTLSCipherSuites
82+
}
83+
84+
// This parses a list of tlsCipherSuite strings and returns a slice of tls.CypherSuite values. Blank/Default = defaultTLSCipherSuites
85+
func ParseTLSCipherSuites(suites []string) ([]uint16, error) {
86+
ret := make([]uint16, 0)
87+
88+
// By default (nothing passed) enable all suites
89+
if len(suites) == 0 {
90+
return defaultTLSCipherSuites, nil
91+
} else {
92+
for _, suite := range suites {
93+
if value, ok := tlsCipherSuites[suite]; ok {
94+
ret = append(ret, value)
95+
} else {
96+
return nil, fmt.Errorf("Unknown tls cipher suite %s", suite)
97+
}
98+
}
99+
}
100+
101+
return ret, nil
102+
}

0 commit comments

Comments
 (0)