/
tls.go
82 lines (69 loc) · 2.37 KB
/
tls.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
// This file is part of *kellner*
//
// Copyright (C) 2015, Travelping GmbH <copyright@travelping.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net"
// enforce linking of several crypto-hashes
_ "crypto/sha256"
_ "crypto/sha512"
)
type tlsOptions struct {
keyFileName string
certFileName string
clientCasFileName string
requireClientCert bool
}
func initTLS(listener net.Listener, opts *tlsOptions) (net.Listener, error) {
cert, err := tls.LoadX509KeyPair(opts.certFileName, opts.keyFileName)
if err != nil {
return listener, fmt.Errorf("loading x509-keypair from %q - %q failed: %v", opts.certFileName, opts.keyFileName, err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
// disable ssl3 and tls1.0 (protect against beast, poodle etc)
MinVersion: tls.VersionTLS11,
NextProtos: []string{"http/1.1"},
// avoid rc4
CipherSuites: []uint16{
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
},
}
if opts.clientCasFileName != "" {
certBytes, err := ioutil.ReadFile(opts.clientCasFileName)
if err != nil {
return listener, fmt.Errorf("loading ca-certs from %q failed: %v", opts.clientCasFileName, err)
}
tlsConfig.ClientCAs = x509.NewCertPool()
if ok := tlsConfig.ClientCAs.AppendCertsFromPEM(certBytes); !ok {
return listener, fmt.Errorf("adding ca-certs from %q to the pool failed", opts.clientCasFileName)
}
log.Printf("added %d certs from %q to ca-certs", len(tlsConfig.ClientCAs.Subjects()), opts.clientCasFileName)
}
if opts.requireClientCert {
tlsConfig.ClientAuth = tls.RequireAnyClientCert
// user gave a list of client-cas. this indicates that she wants
// to check the http-client-certs
if tlsConfig.ClientCAs != nil {
log.Println("tls.RequireAndVerifyClientCert")
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
}
return tls.NewListener(listener, tlsConfig), nil
}