forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
208 lines (185 loc) · 7.3 KB
/
instance.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
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 ctfe
import (
"context"
"errors"
"fmt"
"io/ioutil"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/google/certificate-transparency-go/trillian/ctfe/configpb"
"github.com/google/certificate-transparency-go/trillian/util"
"github.com/google/certificate-transparency-go/x509"
"github.com/google/trillian"
"github.com/google/trillian/crypto"
"github.com/google/trillian/crypto/keys"
"github.com/google/trillian/monitoring"
)
// LogConfigFromFile creates a slice of LogConfig options from the given
// filename, which should contain text-protobuf encoded configuration data.
func LogConfigFromFile(filename string) ([]*configpb.LogConfig, error) {
cfgText, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var cfg configpb.LogConfigSet
if err := proto.UnmarshalText(string(cfgText), &cfg); err != nil {
return nil, fmt.Errorf("failed to parse log config: %v", err)
}
if len(cfg.Config) == 0 {
return nil, errors.New("empty log config found")
}
return cfg.Config, nil
}
// MultiLogConfigFromFile creates a LogMultiConfig proto from the given
// filename, which should contain text-protobuf encoded configuration data.
// Does not do full validation of the config but checks that it is non empty.
func MultiLogConfigFromFile(filename string) (*configpb.LogMultiConfig, error) {
cfgText, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var cfg configpb.LogMultiConfig
if err := proto.UnmarshalText(string(cfgText), &cfg); err != nil {
return nil, fmt.Errorf("failed to parse multi-backend log config: %v", err)
}
if len(cfg.LogConfigs.GetConfig()) == 0 || len(cfg.Backends.GetBackend()) == 0 {
return nil, errors.New("config is missing backends and/or log configs")
}
return &cfg, nil
}
var stringToKeyUsage = map[string]x509.ExtKeyUsage{
"Any": x509.ExtKeyUsageAny,
"ServerAuth": x509.ExtKeyUsageServerAuth,
"ClientAuth": x509.ExtKeyUsageClientAuth,
"CodeSigning": x509.ExtKeyUsageCodeSigning,
"EmailProtection": x509.ExtKeyUsageEmailProtection,
"IPSECEndSystem": x509.ExtKeyUsageIPSECEndSystem,
"IPSECTunnel": x509.ExtKeyUsageIPSECTunnel,
"IPSECUser": x509.ExtKeyUsageIPSECUser,
"TimeStamping": x509.ExtKeyUsageTimeStamping,
"OCSPSigning": x509.ExtKeyUsageOCSPSigning,
"MicrosoftServerGatedCrypto": x509.ExtKeyUsageMicrosoftServerGatedCrypto,
"NetscapeServerGatedCrypto": x509.ExtKeyUsageNetscapeServerGatedCrypto,
}
// SetUpInstance sets up a log instance that uses the specified client to communicate
// with the Trillian RPC back end.
func SetUpInstance(ctx context.Context, client trillian.TrillianLogClient, cfg *configpb.LogConfig, deadline time.Duration, mf monitoring.MetricFactory) (*PathHandlers, error) {
// Check config validity.
if len(cfg.RootsPemFile) == 0 {
return nil, errors.New("need to specify RootsPemFile")
}
if cfg.PrivateKey == nil {
return nil, errors.New("need to specify PrivateKey")
}
// Load the trusted roots
roots := NewPEMCertPool()
for _, pemFile := range cfg.RootsPemFile {
if err := roots.AppendCertsFromPEMFile(pemFile); err != nil {
return nil, fmt.Errorf("failed to read trusted roots: %v", err)
}
}
// Load the private key for this log.
var keyProto ptypes.DynamicAny
if err := ptypes.UnmarshalAny(cfg.PrivateKey, &keyProto); err != nil {
return nil, fmt.Errorf("failed to unmarshal cfg.PrivateKey: %v", err)
}
key, err := keys.NewSigner(ctx, keyProto.Message)
if err != nil {
return nil, fmt.Errorf("failed to load private key: %v", err)
}
signer := crypto.NewSHA256Signer(key)
var keyUsages []x509.ExtKeyUsage
if len(cfg.ExtKeyUsages) > 0 {
for _, kuStr := range cfg.ExtKeyUsages {
if ku, present := stringToKeyUsage[kuStr]; present {
keyUsages = append(keyUsages, ku)
} else {
return nil, fmt.Errorf("unknown extended key usage: %s", kuStr)
}
}
} else {
keyUsages = []x509.ExtKeyUsage{x509.ExtKeyUsageAny}
}
var naStart, naLimit *time.Time
if cfg.NotAfterStart != nil {
t, err := ptypes.Timestamp(cfg.NotAfterStart)
if err != nil {
return nil, fmt.Errorf("invalid not_after_start: %v", err)
}
naStart = &t
}
if cfg.NotAfterLimit != nil {
t, err := ptypes.Timestamp(cfg.NotAfterLimit)
if err != nil {
return nil, fmt.Errorf("invalid not_after_limit: %v", err)
}
naLimit = &t
}
validationOpts := CertValidationOpts{
trustedRoots: roots,
rejectExpired: cfg.RejectExpired,
notAfterStart: naStart,
notAfterLimit: naLimit,
acceptOnlyCA: cfg.AcceptOnlyCa,
extKeyUsages: keyUsages,
}
// Create and register the handlers using the RPC client we just set up
logCtx := NewLogContext(cfg.LogId, cfg.Prefix, validationOpts, client, signer, deadline, new(util.SystemTimeSource), mf)
handlers := logCtx.Handlers(cfg.Prefix)
return &handlers, nil
}
// ValidateLogMultiConfig checks that a config is valid for use with multiple
// backend log servers. The backend set must define a set of log backends
// with distinct (non empty) names and non empty backend specs. The log configs
// must all specify a log backend and each must be one of those defined in
// the backend set.
func ValidateLogMultiConfig(cfg *configpb.LogMultiConfig) (map[string]*configpb.LogBackend, error) {
// Check the backends have unique non empty names and build the map.
backendMap := make(map[string]*configpb.LogBackend)
for _, backend := range cfg.Backends.Backend {
if len(backend.Name) == 0 {
return nil, fmt.Errorf("empty name for backend: %v", backend)
}
if len(backend.BackendSpec) == 0 {
return nil, fmt.Errorf("empty backend_spec for backend: %v", backend)
}
if _, ok := backendMap[backend.Name]; ok {
return nil, fmt.Errorf("duplicate backend name: %v", backend)
}
backendMap[backend.Name] = backend
}
// Check that logs all reference a defined backend.
for _, logCfg := range cfg.LogConfigs.Config {
if _, ok := backendMap[logCfg.LogBackendName]; !ok {
return nil, fmt.Errorf("log config: references undefined backend: %s: %v", logCfg.LogBackendName, logCfg)
}
}
return backendMap, nil
}
// ToMultiLogConfig creates a multi backend config proto from the data
// loaded from a single-backend configuration file. All the log configs
// reference a default backend spec as provided.
func ToMultiLogConfig(cfg []*configpb.LogConfig, beSpec string) *configpb.LogMultiConfig {
defaultBackend := &configpb.LogBackend{Name: "default", BackendSpec: beSpec}
for _, c := range cfg {
c.LogBackendName = defaultBackend.Name
}
return &configpb.LogMultiConfig{
LogConfigs: &configpb.LogConfigSet{Config: cfg},
Backends: &configpb.LogBackendSet{Backend: []*configpb.LogBackend{defaultBackend}},
}
}