forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert.go
215 lines (175 loc) · 5.92 KB
/
cert.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
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"crypto/tls"
"io/ioutil"
"net/http"
"strings"
"github.com/TykTechnologies/tyk/certs"
"github.com/TykTechnologies/tyk/config"
"github.com/gorilla/mux"
)
type APICertificateStatusMessage struct {
CertID string `json:"id"`
Status string `json:"status"`
Message string `json:"message"`
}
type APIAllCertificates struct {
CertIDs []string `json:"certs"`
}
var cipherSuites = map[string]uint16{
"TLS_RSA_WITH_RC4_128_SHA": 0x0005,
"TLS_RSA_WITH_3DES_EDE_CBC_SHA": 0x000a,
"TLS_RSA_WITH_AES_128_CBC_SHA": 0x002f,
"TLS_RSA_WITH_AES_256_CBC_SHA": 0x0035,
"TLS_RSA_WITH_AES_128_CBC_SHA256": 0x003c,
"TLS_RSA_WITH_AES_128_GCM_SHA256": 0x009c,
"TLS_RSA_WITH_AES_256_GCM_SHA384": 0x009d,
"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA": 0xc007,
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA": 0xc009,
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA": 0xc00a,
"TLS_ECDHE_RSA_WITH_RC4_128_SHA": 0xc011,
"TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA": 0xc012,
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA": 0xc013,
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA": 0xc014,
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256": 0xc023,
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256": 0xc027,
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256": 0xc02f,
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256": 0xc02b,
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384": 0xc030,
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384": 0xc02c,
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305": 0xcca8,
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305": 0xcca9,
}
func getUpstreamCertificate(host string, spec *APISpec) (cert *tls.Certificate) {
var certID string
certMaps := []map[string]string{config.Global.Security.Certificates.Upstream}
if spec != nil && spec.UpstreamCertificates != nil {
certMaps = append(certMaps, spec.UpstreamCertificates)
}
for _, m := range certMaps {
if len(m) == 0 {
continue
}
if id, ok := m["*"]; ok {
certID = id
}
if id, ok := m[host]; ok {
certID = id
}
}
if certID == "" {
return nil
}
certs := CertificateManager.List([]string{certID}, certs.CertificatePrivate)
if len(certs) == 0 {
return nil
}
return certs[0]
}
// dummyGetCertificate needed because TLSConfig require setting Certificates array or GetCertificate function from start, even if it get overriden by `getTLSConfigForClient`
func dummyGetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return nil, nil
}
func getTLSConfigForClient(baseConfig *tls.Config, listenPort int) func(hello *tls.ClientHelloInfo) (*tls.Config, error) {
// Supporting legacy certificate configuration
serverCerts := []tls.Certificate{}
certNameMap := map[string]*tls.Certificate{}
for _, certData := range config.Global.HttpServerOptions.Certificates {
cert, err := tls.LoadX509KeyPair(certData.CertFile, certData.KeyFile)
if err != nil {
log.Errorf("Server error: loadkeys: %s", err)
continue
}
serverCerts = append(serverCerts, cert)
certNameMap[certData.Name] = &cert
}
for _, cert := range CertificateManager.List(config.Global.HttpServerOptions.SSLCertificates, certs.CertificatePrivate) {
serverCerts = append(serverCerts, *cert)
}
baseConfig.Certificates = serverCerts
baseConfig.BuildNameToCertificate()
for name, cert := range certNameMap {
baseConfig.NameToCertificate[name] = cert
}
return func(hello *tls.ClientHelloInfo) (*tls.Config, error) {
configMu.Lock()
defer configMu.Unlock()
newConfig := baseConfig.Clone()
isControlAPI := (listenPort != 0 && config.Global.ControlAPIPort == listenPort) || (config.Global.ControlAPIHostname == hello.ServerName)
if isControlAPI && config.Global.Security.ControlAPIUseMutualTLS {
newConfig.ClientAuth = tls.RequireAndVerifyClientCert
newConfig.ClientCAs = CertificateManager.CertPool(config.Global.Security.Certificates.ControlAPI)
return newConfig, nil
}
apisMu.RLock()
for _, spec := range apiSpecs {
if spec.UseMutualTLSAuth && spec.Domain != "" && spec.Domain == hello.ServerName {
newConfig.ClientAuth = tls.RequireAndVerifyClientCert
certIDs := append(spec.ClientCertificates, config.Global.Security.Certificates.API...)
newConfig.ClientCAs = CertificateManager.CertPool(certIDs)
break
}
}
apisMu.RUnlock()
return newConfig, nil
}
}
func certHandler(w http.ResponseWriter, r *http.Request) {
certID := mux.Vars(r)["certID"]
switch r.Method {
case "POST":
content, err := ioutil.ReadAll(r.Body)
if err != nil {
doJSONWrite(w, 405, apiError("Malformed request body"))
return
}
orgID := r.URL.Query().Get("org_id")
var certID string
if certID, err = CertificateManager.Add(content, orgID); err != nil {
doJSONWrite(w, 403, apiError(err.Error()))
return
}
doJSONWrite(w, 200, &APICertificateStatusMessage{certID, "ok", "Certificate added"})
case "GET":
if certID == "" {
orgID := r.URL.Query().Get("org_id")
certIds := CertificateManager.ListAllIds(orgID)
doJSONWrite(w, 200, &APIAllCertificates{certIds})
return
}
certIDs := strings.Split(certID, ",")
certificates := CertificateManager.List(certIDs, certs.CertificateAny)
if len(certIDs) == 1 {
if certificates[0] == nil {
doJSONWrite(w, 404, apiError("Certificate with given SHA256 fingerprint not found"))
return
}
doJSONWrite(w, 200, certs.ExtractCertificateMeta(certificates[0], certIDs[0]))
return
} else {
var meta []*certs.CertificateMeta
for ci, cert := range certificates {
if cert != nil {
meta = append(meta, certs.ExtractCertificateMeta(cert, certIDs[ci]))
} else {
meta = append(meta, nil)
}
}
doJSONWrite(w, 200, meta)
return
}
case "DELETE":
CertificateManager.Delete(certID)
doJSONWrite(w, 200, &apiStatusMessage{"ok", "removed"})
}
}
func getCipherAliases(ciphers []string) (cipherCodes []uint16) {
for k, v := range cipherSuites {
for _, str := range ciphers {
if str == k {
cipherCodes = append(cipherCodes, v)
}
}
}
return cipherCodes
}