-
Notifications
You must be signed in to change notification settings - Fork 1
/
tls.go
203 lines (185 loc) · 5.51 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
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
/*
* Copyright 2022 Armory, Inc.
*
* 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.
*/
package http
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"github.com/armory-io/go-commons/secrets"
"io/ioutil"
"os"
)
type (
ClientTLSSettings struct {
// Override server name to check on certificate
ServerName string `yaml:"serverName,omitempty" json:"serverName,omitempty"`
// Don't check server name
InsecureSkipVerify bool `yaml:"insecureSkipVerify" json:"insecureSkipVerify"`
// Key file if the cert file doesn't provide it
ClientKeyFile string `yaml:"clientKeyFile,omitempty" json:"clientKeyFile,omitempty"`
// Key password if the key is encrypted
ClientKeyPassword string `yaml:"clientKeyFilePassword,omitempty" json:"clientKeyFilePassword,omitempty"`
// If not provided, it will default to the system cacerts
CAcertFile string `yaml:"cacertFile,omitempty" json:"cacertFile,omitempty"`
// Client certificate file - useful for mTLS
ClientCertFile string `yaml:"clientCertFile,omitempty" json:"clientCertFile,omitempty"`
}
)
func GetTLSConfig(s *ClientTLSSettings) (*tls.Config, error) {
cfg := tls.Config{}
if s != nil {
cfg.InsecureSkipVerify = s.InsecureSkipVerify
if s.ServerName != "" {
cfg.ServerName = s.ServerName
}
if err := addCaCert(&cfg, s); err != nil {
return nil, err
}
if err := addClientOptions(&cfg, s); err != nil {
return nil, err
}
}
return &cfg, nil
}
func addClientOptions(cfg *tls.Config, s *ClientTLSSettings) error {
if s.ClientCertFile == "" {
return nil
}
cert, err := GetX509KeyPair(s.ClientCertFile, s.ClientKeyFile, s.ClientKeyPassword)
if err != nil {
return fmt.Errorf("error with certificate file %s: %w", s.ClientCertFile, err)
}
cfg.Certificates = []tls.Certificate{cert}
cfg.PreferServerCipherSuites = true
cfg.MinVersion = tls.VersionTLS12
return nil
}
func addCaCert(cfg *tls.Config, s *ClientTLSSettings) error {
// With mTLS, we'll parse our PEM to discover CAs with which to validate client certificates
caFile := s.CAcertFile
if caFile == "" {
return nil
}
if err := CheckFileExists(caFile); err != nil {
return fmt.Errorf("error with certificate authority file %s: %w", caFile, err)
}
// Create a CA certificate pool and add our server certificate
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
return err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
cfg.RootCAs = caCertPool
return nil
}
func CheckFileExists(filename string) error {
if secrets.IsEncryptedSecret(filename) {
d, err := secrets.NewDecrypter(context.TODO(), filename)
if err != nil {
return err
}
if !d.IsFile() {
return errors.New("no file referenced, use encryptedFile")
}
filename, err = d.Decrypt()
if err != nil {
return err
}
}
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return err
}
return nil
}
func GetX509KeyPair(certFile, keyFile, keyPassword string) (tls.Certificate, error) {
if err := CheckFileExists(certFile); err != nil {
return tls.Certificate{}, fmt.Errorf("error with certificate file %s: %w", certFile, err)
}
b, err := ioutil.ReadFile(certFile)
if err != nil {
return tls.Certificate{}, err
}
pemBlocks, pkey, err := readAndDecryptPEM(b, keyPassword)
// If private key not in the cert file, we look for it in the key file
if pkey == nil {
pkey, err = getPrivateKey(keyFile, keyPassword)
if err != nil {
return tls.Certificate{}, err
}
}
return tls.X509KeyPair(pem.EncodeToMemory(pemBlocks[0]), pkey)
}
// getPrivateKey attempts to load and decrypt the private key if needed
func getPrivateKey(keyFile, keyPassword string) ([]byte, error) {
if err := CheckFileExists(keyFile); err != nil {
return nil, fmt.Errorf("error with key file %s: %w", keyFile, err)
}
b, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
_, pkey, err := readAndDecryptPEM(b, keyPassword)
return pkey, err
}
// readAndDecryptPEM reads PEM data and attempts to decrypt if a private key is found encrypted
// using ssl.keyPassword provided in the config
func readAndDecryptPEM(data []byte, keyPassword string) ([]*pem.Block, []byte, error) {
var pemBlocks []*pem.Block
var v *pem.Block
var pkey []byte
for {
v, data = pem.Decode(data)
if v == nil {
break
}
if v.Type == "RSA PRIVATE KEY" {
if x509.IsEncryptedPEMBlock(v) {
pass, err := getKeyPassword(keyPassword)
if err != nil {
return nil, nil, err
}
pkey, _ = x509.DecryptPEMBlock(v, []byte(pass))
pkey = pem.EncodeToMemory(&pem.Block{
Type: v.Type,
Bytes: pkey,
})
} else {
pkey = pem.EncodeToMemory(v)
}
} else {
pemBlocks = append(pemBlocks, v)
}
}
return pemBlocks, pkey, nil
}
func getKeyPassword(keyPassword string) (string, error) {
if secrets.IsEncryptedSecret(keyPassword) {
d, err := secrets.NewDecrypter(context.TODO(), keyPassword)
if err != nil {
return "", err
}
return d.Decrypt()
}
if keyPassword == "" {
return "", fmt.Errorf("encrypted pem found but no password provided")
}
return keyPassword, nil
}