-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssl.go
113 lines (98 loc) · 3.23 KB
/
ssl.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
package web
import (
"crypto/tls"
"crypto/x509"
"net/http"
"strings"
"golang.org/x/crypto/acme"
"github.com/getfider/fider/app/pkg/blob"
"github.com/getfider/fider/app/pkg/dbx"
"github.com/getfider/fider/app/pkg/di"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
"golang.org/x/crypto/acme/autocert"
)
func getDefaultTLSConfig() *tls.Config {
return &tls.Config{
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.X25519,
tls.CurveP256,
},
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
},
}
}
//CertificateManager is used to manage SSL certificates
type CertificateManager struct {
cert tls.Certificate
leaf *x509.Certificate
autossl autocert.Manager
}
//NewCertificateManager creates a new CertificateManager
func NewCertificateManager(certFile, keyFile string, db *dbx.Database) (*CertificateManager, error) {
manager := &CertificateManager{
autossl: autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: blob.NewAutoCert(di.NewBlobStorage(db)),
Client: acmeClient(),
},
}
if certFile != "" && keyFile != "" {
var err error
manager.cert, err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, errors.Wrap(err, "failed to load X509KeyPair for %s and %s", certFile, keyFile)
}
manager.leaf, err = x509.ParseCertificate(manager.cert.Certificate[0])
if err != nil {
return nil, errors.Wrap(err, "failed to parse x509 certificate")
}
}
return manager, nil
}
//GetCertificate decides which certificate to use
//It first tries to use loaded certificate for incoming request if it's compatible
//Otherwise fallsback to a automatically generated certificate by Let's Encrypt
func (m *CertificateManager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
if m.leaf != nil {
serverName := strings.Trim(strings.ToLower(hello.ServerName), ".")
// If ServerName is empty or does't contain a dot, just return the certificate
if serverName == "" || !strings.Contains(serverName, ".") {
return &m.cert, nil
}
if env.IsSingleHostMode() {
return &m.cert, nil
} else if strings.HasSuffix(serverName, env.MultiTenantDomain()) {
if m.leaf.VerifyHostname(serverName) == nil {
return &m.cert, nil
}
return nil, errors.New(`ssl: invalid server name "%s"`, serverName)
}
}
return m.autossl.GetCertificate(hello)
}
//StartHTTPServer creates a new HTTP server on port 80 that is used for the ACME HTTP Challenge
func (m *CertificateManager) StartHTTPServer() {
http.ListenAndServe(":80", m.autossl.HTTPHandler(nil))
}
func acmeClient() *acme.Client {
if env.IsTest() {
return &acme.Client{
DirectoryURL: "https://acme-staging.api.letsencrypt.org/directory",
}
}
return nil
}