-
Notifications
You must be signed in to change notification settings - Fork 18
/
signature.go
344 lines (298 loc) · 8.03 KB
/
signature.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package v1
import (
"fmt"
"reflect"
"strings"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/equivalent"
"github.com/open-component-model/ocm/pkg/signing"
)
const (
// ExcludeFromSignature used in digest field for normalisationAlgorithm (in combination with NoDigest for hashAlgorithm and value)
// to indicate the resource content should not be part of the signature.
ExcludeFromSignature = "EXCLUDE-FROM-SIGNATURE"
// NoDigest used in digest field for hashAlgorithm and value (in combination with ExcludeFromSignature for normalisationAlgorithm)
// to indicate the resource content should not be part of the signature.
NoDigest = "NO-DIGEST"
)
// Signatures is a list of signatures.
type Signatures []Signature
func (s Signatures) Equivalent(o Signatures) equivalent.EqualState {
if len(s) != len(o) {
return equivalent.StateNotEquivalent()
}
outer:
for _, a := range s {
if b := o.GetByName(a.Name); b != nil {
if reflect.DeepEqual(&a, b) {
continue outer
}
}
return equivalent.StateNotEquivalent()
}
return equivalent.StateEquivalent()
}
func (s Signatures) Len() int {
return len(s)
}
func (s Signatures) Get(i int) *Signature {
if i >= 0 && i < len(s) {
return &s[i]
}
return nil
}
func (s Signatures) Copy() Signatures {
if s == nil {
return nil
}
out := make(Signatures, s.Len())
for i, v := range s {
out[i] = *v.Copy()
}
return out
}
func (s Signatures) GetIndex(name string) int {
for i, v := range s {
if v.Name == name {
return i
}
}
return -1
}
func (s Signatures) GetByName(name string) *Signature {
if idx := s.GetIndex(name); idx >= 0 {
return &s[idx]
}
return nil
}
func (s *Signatures) Set(sig Signature) {
if idx := s.GetIndex(sig.Name); idx < 0 {
*s = append(*s, sig)
} else {
(*s)[idx] = sig
}
}
// DigestSpec defines a digest.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type DigestSpec struct {
HashAlgorithm string `json:"hashAlgorithm"`
NormalisationAlgorithm string `json:"normalisationAlgorithm"`
Value string `json:"value"`
}
func (d *DigestSpec) String() string {
return fmt.Sprintf("%s:%s[%s]", d.HashAlgorithm, d.Value, d.NormalisationAlgorithm)
}
func (d *DigestSpec) IsComplete() bool {
return d != nil && d.HashAlgorithm != "" && d.NormalisationAlgorithm != "" && d.Value != ""
}
func (d *DigestSpec) IsNone() bool {
return d == nil || *d == DigestSpec{}
}
func (d *DigestSpec) IsExcluded() bool {
return d != nil && *d == excluded
}
// Copy provides a copy of the digest spec.
func (d *DigestSpec) Copy() *DigestSpec {
if d == nil {
return nil
}
r := *d
return &r
}
func (d *DigestSpec) Equal(o *DigestSpec) bool {
if d == o {
return true
}
if d == nil || o == nil {
return false
}
return *d == *o
}
func (d *DigestSpec) Equivalent(o *DigestSpec) equivalent.EqualState {
if d == nil {
d, o = o, d
}
if d == nil {
return equivalent.StateNotArtifactEqual(false)
}
if (d.IsExcluded() && o == nil) || reflect.DeepEqual(d, o) {
return equivalent.StateEquivalent()
}
return equivalent.StateNotArtifactEqual(d != nil && o != nil)
}
// SignatureSpec defines a signature.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type SignatureSpec struct {
Algorithm string `json:"algorithm"`
Value string `json:"value"`
MediaType string `json:"mediaType"`
Issuer string `json:"issuer,omitempty"`
}
// ConvertToSigning converts a cd signature to a signing signature.
func (s *SignatureSpec) ConvertToSigning() *signing.Signature {
return &signing.Signature{
Value: s.Value,
MediaType: s.MediaType,
Algorithm: s.Algorithm,
Issuer: s.Issuer,
}
}
// TimestampSpec provides a timestamp and the
// appropriate pem encoded TSA value.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type TimestampSpec struct {
// Value contains the PEM encoded TSA value.
Value string `json:"value"`
Time *Timestamp `json:"time,omitempty"`
// later, we can add other storage kind, like localBlob.
}
// Signature defines a digest and corresponding signature, identifiable by name.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type Signature struct {
Name string `json:"name"`
Digest DigestSpec `json:"digest"`
Signature SignatureSpec `json:"signature"`
Timestamp *TimestampSpec `json:"timestamp,omitempty"`
}
// Copy provides a copy of the signature data.
func (s *Signature) Copy() *Signature {
if s == nil {
return nil
}
r := *s
return &r
}
// ConvertToSigning converts a cd signature to a signing signature.
func (s *Signature) ConvertToSigning() *signing.Signature {
return s.Signature.ConvertToSigning()
}
func SignatureSpecFor(s *signing.Signature) *SignatureSpec {
return &SignatureSpec{
Value: s.Value,
MediaType: s.MediaType,
Algorithm: s.Algorithm,
Issuer: s.Issuer,
}
}
var excluded = DigestSpec{
HashAlgorithm: NoDigest,
NormalisationAlgorithm: ExcludeFromSignature,
Value: NoDigest,
}
// NewExcludeFromSignatureDigest returns the special digest notation to indicate the resource content should not be part of the signature.
func NewExcludeFromSignatureDigest() *DigestSpec {
e := excluded
return &e
}
////////////////////////////////////////////////////////////////////////////////
// NestedDigests defines a list of nested components.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type NestedDigests []NestedComponentDigests
func (d NestedDigests) String() string {
r := ""
sep := ""
for _, e := range d {
r = fmt.Sprintf("%s%s%s", r, sep, e.String())
sep = "\n"
}
return r
}
func (d NestedDigests) Copy() NestedDigests {
if d == nil {
return nil
}
r := make([]NestedComponentDigests, len(d))
for i, e := range d {
r[i] = *e.Copy()
}
return r
}
func (d NestedDigests) Lookup(name, version string) *NestedComponentDigests {
for _, e := range d {
if e.Name == name && e.Version == version {
return &e
}
}
return nil
}
// NestedComponentDigests defines nested components.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type NestedComponentDigests struct {
Name string `json:"name"`
Version string `json:"version"`
Digest *DigestSpec `json:"digest,omitempty"`
Resources ArtefactDigests `json:"resourceDigests,omitempty"`
}
func (d *NestedComponentDigests) String() string {
r := []string{
fmt.Sprintf("%s:%s: %s", d.Name, d.Version, d.Digest.String()),
}
for _, a := range d.Resources {
r = append(r, " "+a.String())
}
return strings.Join(r, "\n")
}
func (d *NestedComponentDigests) Lookup(name, version string, extra Identity) *ArtefactDigest {
if d == nil {
return nil
}
return d.Resources.Lookup(name, version, extra)
}
func (d *NestedComponentDigests) Copy() *NestedComponentDigests {
if d == nil {
return nil
}
r := *d
r.Digest = d.Digest.Copy()
r.Resources = make([]ArtefactDigest, len(d.Resources))
for i, e := range d.Resources {
r.Resources[i] = *e.Copy()
}
return &r
}
// ArtefactDigests defines a list of artefact digest information.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ArtefactDigests []ArtefactDigest
func (d ArtefactDigests) Lookup(name, version string, extra Identity) *ArtefactDigest {
for _, e := range d {
if e.Name == name && e.Version == version && e.ExtraIdentity.Equals(extra) {
return &e
}
}
return nil
}
func (d ArtefactDigests) Match(o ArtefactDigests) bool {
if len(d) != len(o) {
return false
}
for _, e := range d {
i := o.Lookup(e.Name, e.Version, e.ExtraIdentity)
if i == nil || i.Digest != e.Digest {
return false
}
}
return true
}
// ArtefactDigest defines artefact digest information.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ArtefactDigest struct {
Name string `json:"name"`
Version string `json:"version"`
ExtraIdentity Identity `json:"extraIdentity,omitempty"`
Digest DigestSpec `json:"digest"`
}
func (d *ArtefactDigest) Copy() *ArtefactDigest {
r := *d
r.ExtraIdentity = d.ExtraIdentity.Copy()
return &r
}
func (d *ArtefactDigest) String() string {
return fmt.Sprintf("%s:%s[%s]: %s", d.Name, d.Version, d.ExtraIdentity.String(), d.Digest.String())
}