-
Notifications
You must be signed in to change notification settings - Fork 21
/
trusted_material.go
172 lines (145 loc) · 5.3 KB
/
trusted_material.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
// Copyright 2023 The Sigstore Authors.
//
// 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 root
import (
"fmt"
"time"
"github.com/sigstore/sigstore/pkg/signature"
)
type TrustedMaterial interface {
TimestampingAuthorities() []CertificateAuthority
FulcioCertificateAuthorities() []CertificateAuthority
RekorLogs() map[string]*TransparencyLog
CTLogs() map[string]*TransparencyLog
PublicKeyVerifier(string) (TimeConstrainedVerifier, error)
}
type BaseTrustedMaterial struct{}
func (b *BaseTrustedMaterial) TimestampingAuthorities() []CertificateAuthority {
return []CertificateAuthority{}
}
func (b *BaseTrustedMaterial) FulcioCertificateAuthorities() []CertificateAuthority {
return []CertificateAuthority{}
}
func (b *BaseTrustedMaterial) RekorLogs() map[string]*TransparencyLog {
return map[string]*TransparencyLog{}
}
func (b *BaseTrustedMaterial) CTLogs() map[string]*TransparencyLog {
return map[string]*TransparencyLog{}
}
func (b *BaseTrustedMaterial) PublicKeyVerifier(_ string) (TimeConstrainedVerifier, error) {
return nil, fmt.Errorf("public key verifier not found")
}
type TrustedMaterialCollection []TrustedMaterial
// Ensure types implement interfaces
var _ TrustedMaterial = &BaseTrustedMaterial{}
var _ TrustedMaterial = TrustedMaterialCollection{}
func (tmc TrustedMaterialCollection) PublicKeyVerifier(keyID string) (TimeConstrainedVerifier, error) {
for _, tm := range tmc {
verifier, err := tm.PublicKeyVerifier(keyID)
if err == nil {
return verifier, nil
}
}
return nil, fmt.Errorf("public key verifier not found for keyID: %s", keyID)
}
func (tmc TrustedMaterialCollection) TimestampingAuthorities() []CertificateAuthority {
var certAuthorities []CertificateAuthority
for _, tm := range tmc {
certAuthorities = append(certAuthorities, tm.TimestampingAuthorities()...)
}
return certAuthorities
}
func (tmc TrustedMaterialCollection) FulcioCertificateAuthorities() []CertificateAuthority {
var certAuthorities []CertificateAuthority
for _, tm := range tmc {
certAuthorities = append(certAuthorities, tm.FulcioCertificateAuthorities()...)
}
return certAuthorities
}
func (tmc TrustedMaterialCollection) RekorLogs() map[string]*TransparencyLog {
rekorLogs := make(map[string]*TransparencyLog)
for _, tm := range tmc {
for keyID, tlogVerifier := range tm.RekorLogs() {
rekorLogs[keyID] = tlogVerifier
}
}
return rekorLogs
}
func (tmc TrustedMaterialCollection) CTLogs() map[string]*TransparencyLog {
rekorLogs := make(map[string]*TransparencyLog)
for _, tm := range tmc {
for keyID, tlogVerifier := range tm.CTLogs() {
rekorLogs[keyID] = tlogVerifier
}
}
return rekorLogs
}
type ValidityPeriodChecker interface {
ValidAtTime(time.Time) bool
}
type TimeConstrainedVerifier interface {
ValidityPeriodChecker
signature.Verifier
}
type TrustedPublicKeyMaterial struct {
BaseTrustedMaterial
publicKeyVerifier func(string) (TimeConstrainedVerifier, error)
}
func (tr *TrustedPublicKeyMaterial) PublicKeyVerifier(keyID string) (TimeConstrainedVerifier, error) {
return tr.publicKeyVerifier(keyID)
}
func NewTrustedPublicKeyMaterial(publicKeyVerifier func(string) (TimeConstrainedVerifier, error)) *TrustedPublicKeyMaterial {
return &TrustedPublicKeyMaterial{
publicKeyVerifier: publicKeyVerifier,
}
}
// ExpiringKey is a TimeConstrainedVerifier with a static validity period.
type ExpiringKey struct {
signature.Verifier
validityPeriodStart time.Time
validityPeriodEnd time.Time
}
var _ TimeConstrainedVerifier = &ExpiringKey{}
// ValidAtTime returns true if the key is valid at the given time. If the
// validity period start time is not set, the key is considered valid for all
// times before the end time. Likewise, if the validity period end time is not
// set, the key is considered valid for all times after the start time.
func (k *ExpiringKey) ValidAtTime(t time.Time) bool {
if !k.validityPeriodStart.IsZero() && t.Before(k.validityPeriodStart) {
return false
}
if !k.validityPeriodEnd.IsZero() && t.After(k.validityPeriodEnd) {
return false
}
return true
}
// NewExpiringKey returns a new ExpiringKey with the given validity period
func NewExpiringKey(verifier signature.Verifier, validityPeriodStart, validityPeriodEnd time.Time) *ExpiringKey {
return &ExpiringKey{
Verifier: verifier,
validityPeriodStart: validityPeriodStart,
validityPeriodEnd: validityPeriodEnd,
}
}
// NewTrustedPublicKeyMaterialFromMapping returns a TrustedPublicKeyMaterial from a map of key IDs to
// ExpiringKeys.
func NewTrustedPublicKeyMaterialFromMapping(trustedPublicKeys map[string]*ExpiringKey) *TrustedPublicKeyMaterial {
return NewTrustedPublicKeyMaterial(func(keyID string) (TimeConstrainedVerifier, error) {
expiringKey, ok := trustedPublicKeys[keyID]
if !ok {
return nil, fmt.Errorf("public key not found for keyID: %s", keyID)
}
return expiringKey, nil
})
}