forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
125 lines (113 loc) · 4.35 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
// 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 (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"time"
"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"
)
// LogConfig describes the configuration options for a log instance.
type LogConfig struct {
LogID int64
Prefix string
RootsPEMFile []string
PrivKeyPEMFile string
PrivKeyPassword string
// The public key is included for the convenience of test tools (and obviously should
// match the private key above); it is not used by the CT personality.
PubKeyPEMFile string
RejectExpired bool
ExtKeyUsages []string
}
// LogConfigFromFile creates a slice of LogConfig options from the given
// filename, which should contain JSON encoded configuration data.
func LogConfigFromFile(filename string) ([]LogConfig, error) {
if len(filename) == 0 {
return nil, errors.New("log config filename empty")
}
cfgData, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read log config: %v", err)
}
var cfg []LogConfig
if err := json.Unmarshal(cfgData, &cfg); err != nil {
return nil, fmt.Errorf("failed to parse config data: %v", err)
}
if len(cfg) == 0 {
return nil, errors.New("empty log config found")
}
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 (cfg LogConfig) SetUpInstance(client trillian.TrillianLogClient, 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 len(cfg.PrivKeyPEMFile) == 0 {
return nil, errors.New("need to specify PrivKeyPEMFile")
}
// 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.
key, err := keys.NewFromPrivatePEMFile(cfg.PrivKeyPEMFile, cfg.PrivKeyPassword)
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}
}
// Create and register the handlers using the RPC client we just set up
ctx := NewLogContext(cfg.LogID, cfg.Prefix, roots, cfg.RejectExpired, keyUsages, client, signer, deadline, new(util.SystemTimeSource), mf)
handlers := ctx.Handlers(cfg.Prefix)
return &handlers, nil
}