-
Notifications
You must be signed in to change notification settings - Fork 55
/
certloader.go
268 lines (240 loc) · 8.32 KB
/
certloader.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2019 Google LLC
//
// 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
//
// https://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 certloader
import (
"crypto"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"log"
"os"
"github.com/WICG/webpackage/go/signedexchange"
"github.com/gofrs/flock"
"github.com/pkg/errors"
"github.com/ampproject/amppackager/packager/certfetcher"
"github.com/ampproject/amppackager/packager/util"
)
func CreateCertFetcher(config *util.Config, key crypto.PrivateKey, domain string,
developmentMode bool, autoRenewCert bool) (*certfetcher.CertFetcher, error) {
if !autoRenewCert {
// Certfetcher can be nil, if auto renew is off.
return nil, nil
}
if config.ACMEConfig == nil {
return nil, errors.New("missing ACMEConfig")
}
var acmeConfig *util.ACMEServerConfig
if developmentMode {
acmeConfig = config.ACMEConfig.Development
} else {
acmeConfig = config.ACMEConfig.Production
}
if acmeConfig == nil {
if developmentMode {
return nil, errors.New("missing ACMEConfig.Development")
} else {
return nil, errors.New("missing ACMEConfig.Production")
}
}
if acmeConfig.EmailAddress == "" {
return nil, errors.New("missing email address")
}
emailAddress := acmeConfig.EmailAddress
if acmeConfig.DiscoURL == "" {
return nil, errors.New("missing acme disco url")
}
acmeDiscoveryURL := acmeConfig.DiscoURL
// Fields for External Account Binding. Some CAs require them, others like
// DigiCert, do not. Either both eabKid and eabHmac has to be specified or
// none of them are specified.
if acmeConfig.EABKid == "" && acmeConfig.EABHmac != "" {
return nil, errors.New("EABKid is empty, but EABHmac is not empty, both values need to be set or empty")
}
if acmeConfig.EABKid != "" && acmeConfig.EABHmac == "" {
return nil, errors.New("EABKid is not empty, but EABHmac is empty, both values need to be set or empty")
}
eabKid := acmeConfig.EABKid
eabHmac := acmeConfig.EABHmac
if acmeConfig.HttpChallengePort == 0 &&
acmeConfig.HttpWebRootDir == "" &&
acmeConfig.TlsChallengePort == 0 &&
acmeConfig.DnsProvider == "" {
return nil, errors.New("one of HttpChallengePort, HttpWebRootDir, TlsChallengePort and DnsProvider must be present")
}
httpChallengePort := acmeConfig.HttpChallengePort
httpWebRootDir := acmeConfig.HttpWebRootDir
tlsChallengePort := acmeConfig.TlsChallengePort
dnsProvider := acmeConfig.DnsProvider
// TODO(banaag): Rather than making publishers create a CSR, generate one using the given KeyFile/CertFile and
// https://golang.org/pkg/crypto/x509/#CreateCertificateRequest.
csr, err := LoadCSRFromFile(config)
if err != nil {
return nil, errors.Wrap(err, "missing CSR")
}
// Create the cert fetcher that will auto-renew the cert.
certFetcher, err := certfetcher.New(emailAddress, eabKid, eabHmac, csr, key,
acmeDiscoveryURL, httpChallengePort, httpWebRootDir, tlsChallengePort,
dnsProvider, true)
if err != nil {
return nil, errors.Wrap(err, "creating certfetcher")
}
log.Println("Certfetcher created successfully.")
return certFetcher, nil
}
// Loads X509 certificates from disk.
// Returns appropriate errors if:
// The file can't be read.
// The certificate can't be parsed.
// No certificates found in the file.
// Certificates cannot be used to sign HTTP exchanges.
// (if developmentMode, print a warning that certs can't
// be used to sign HTTP exchanges).
// If there are no errors, the array of certificates is returned.
func LoadCertsFromFile(config *util.Config, developmentMode bool) ([]*x509.Certificate, error) {
return LoadAndValidateCertsFromFile(config.CertFile, !developmentMode)
}
func LoadAndValidateCertsFromFile(certPath string, requireSign bool) ([]*x509.Certificate, error) {
// Use independent .lock file; necessary on Windows to avoid "The process cannot
// access the file because another process has locked a portion of the file."
lockPath := certPath + ".lock"
lock := flock.New(lockPath)
locked, err := lock.TryRLock()
if err != nil {
return nil, errors.Wrapf(err, "obtaining exclusive lock for %s", lockPath)
}
if !locked {
return nil, errors.Errorf("unable to obtain exclusive lock for %s", lockPath)
}
defer func() {
if err = lock.Unlock(); err != nil {
log.Printf("Error unlocking %s; %+v", lockPath, err)
}
if err := os.Remove(lockPath); err != nil {
log.Printf("Error removing %s; %+v", lockPath, err)
}
}()
// TODO(twifkak): Document what cert/key storage formats this accepts.
certPem, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, errors.Wrapf(err, "reading %s", certPath)
}
certs, err := signedexchange.ParseCertificates(certPem)
if err != nil {
return nil, errors.Wrapf(err, "parsing %s", certPath)
}
if certs == nil || len(certs) == 0 {
return nil, errors.Errorf("no cert found in %s", certPath)
}
if err := util.CanSignHttpExchanges(certs[0]); err != nil {
if !requireSign {
log.Println("WARNING:", err)
} else {
return nil, err
}
}
return certs, nil
}
func WriteCertsToFile(certs []*x509.Certificate, filepath string) error {
if len(certs) < 2 {
return errors.New("Missing issuer in bundle")
}
// Use independent .lock file; necessary on Windows to avoid "The process cannot
// access the file because another process has locked a portion of the file."
lockPath := filepath + ".lock"
lock := flock.New(lockPath)
locked, err := lock.TryLock()
if err != nil {
return errors.Wrapf(err, "obtaining exclusive lock for %s", lockPath)
}
if !locked {
return errors.Errorf("unable to obtain exclusive lock for %s", lockPath)
}
defer func() {
if err = lock.Unlock(); err != nil {
log.Printf("Error unlocking %s; %+v", lockPath, err)
}
if err := os.Remove(lockPath); err != nil {
log.Printf("Error removing %s; %+v", lockPath, err)
}
}()
bundled := []byte{}
for _, cert := range certs {
pem := certToPEM(cert)
bundled = append(bundled, pem...)
}
if err := ioutil.WriteFile(filepath, bundled, 0600); err != nil {
return errors.Wrapf(err, "writing %s", filepath)
}
return nil
}
func RemoveFile(filepath string) error {
// Use independent .lock file; necessary on Windows to avoid "The process cannot
// access the file because another process has locked a portion of the file."
lockPath := filepath + ".lock"
lock := flock.New(lockPath)
locked, err := lock.TryLock()
if err != nil {
return errors.Wrapf(err, "obtaining exclusive lock for %s", lockPath)
}
if !locked {
return errors.Errorf("unable to obtain exclusive lock for %s", lockPath)
}
defer func() {
if err = lock.Unlock(); err != nil {
log.Printf("Error unlocking %s; %+v", lockPath, err)
}
if err := os.Remove(lockPath); err != nil {
log.Printf("Error removing %s; %+v", lockPath, err)
}
}()
if err := os.Remove(filepath); err != nil {
return errors.Wrapf(err, "removing %s", filepath)
}
return nil
}
func certToPEM(cert *x509.Certificate) []byte {
pemCert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
return pemCert
}
func LoadCSRFromFile(config *util.Config) (*x509.CertificateRequest, error) {
data, err := ioutil.ReadFile(config.CSRFile)
if err != nil {
return nil, errors.Wrapf(err, "reading %s", config.CSRFile)
}
block, _ := pem.Decode(data)
if block == nil {
return nil, errors.Errorf("pem decode: no key found in %s", config.CSRFile)
}
csr, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
return nil, errors.Wrapf(err, "parsing CSR %s", config.CSRFile)
}
return csr, nil
}
// Loads private key from file.
// Returns appropriate errors if:
// The file can't be read.
// The key can't be parsed.
// If there are no errors, the key is returned.
func LoadKeyFromFile(config *util.Config) (crypto.PrivateKey, error) {
keyPem, err := ioutil.ReadFile(config.KeyFile)
if err != nil {
return nil, errors.Wrapf(err, "reading %s", config.KeyFile)
}
key, err := util.ParsePrivateKey(keyPem)
if err != nil {
return nil, errors.Wrapf(err, "parsing %s", config.KeyFile)
}
return key, nil
}