Debian Codegraph @

internal/third_party/go-securesystemslib/dsse/verify.go

raw symbols SCIP
1 package dssescip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/
2
3 import (
4 "contextscip-go gomod github.com/golang/go/src go1.26.0 context/"
5 "cryptoscip-go gomod github.com/golang/go/src go1.26.0 crypto/"
6 "errorsscip-go gomod github.com/golang/go/src go1.26.0 errors/"
7 "fmtscip-go gomod github.com/golang/go/src go1.26.0 fmt/"
8
9 "golang.org/x/crypto/sshscip-go gomod golang.org/x/crypto v0.53.0 `golang.org/x/crypto/ssh`/"
10 )
11
12 // ErrNoSignature indicates that an envelope did not contain any signatures.
13 var ErrNoSignaturescip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/ErrNoSignature. = errorsscip-go gomod github.com/golang/go/src go1.26.0 errors/.Newscip-go gomod github.com/golang/go/src go1.26.0 errors/New().("no signature found")
14
15 type EnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier# struct {
16 providersscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#providers. []Verifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#
17 thresholdscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#threshold. int
18 }
19
20 type AcceptedKeyscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey# struct {
21 Publicscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#Public. cryptoscip-go gomod github.com/golang/go/src go1.26.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.26.0 crypto/PublicKey#
22 KeyIDscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#KeyID. string
23 Sigscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#Sig. Signaturescip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Signature#
24 }
25
26 func (ev *EnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#) Verifyscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#Verify().(ctx contextscip-go gomod github.com/golang/go/src go1.26.0 context/.Contextscip-go gomod github.com/golang/go/src go1.26.0 context/Context#, e *Envelopescip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Envelope#) ([]AcceptedKeyscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#, error) {
27 if e == nil {
28 return nil, errorsscip-go gomod github.com/golang/go/src go1.26.0 errors/.Newscip-go gomod github.com/golang/go/src go1.26.0 errors/New().("cannot verify a nil envelope")
29 }
30
31 if len(e.Signaturesscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Envelope#Signatures.) == 0 {
32 return nil, ErrNoSignaturescip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/ErrNoSignature.
33 }
34
35 // Decode payload (i.e serialized body)
36 body, err := e.DecodeB64Payloadscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Envelope#DecodeB64Payload().()
37 if err != nil {
38 return nil, err
39 }
40 // Generate PAE(payloadtype, serialized body)
41 paeEnc := PAEscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/PAE().(e.PayloadTypescip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Envelope#PayloadType., body)
42
43 // If *any* signature is found to be incorrect, it is skipped
44 var acceptedKeys []AcceptedKeyscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#
45 usedKeyids := make(map[string]string)
46 unverified_providers := make([]Verifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#, len(ev.providersscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#providers.))
47 copy(unverified_providers, ev.providersscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#providers.)
48 for _, s := range e.Signaturesscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Envelope#Signatures. {
49 sig, err := b64Decodescip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/b64Decode().(s.Sigscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Signature#Sig.)
50 if err != nil {
51 return nil, err
52 }
53
54 // Loop over the providers.
55 // If provider and signature include key IDs but do not match skip.
56 // If a provider recognizes the key, we exit
57 // the loop and use the result.
58 providers := unverified_providers
59 for i, v := range providers {
60 keyID, err := v.KeyIDscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#KeyID.()
61
62 // Verifiers that do not provide a keyid will be generated one using public.
63 if err != nil || keyID == "" {
64 keyID, err = SHA256KeyIDscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/SHA256KeyID().(v.Publicscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#Public.())
65 if err != nil {
66 keyID = ""
67 }
68 }
69
70 if s.KeyIDscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Signature#KeyID. != "" && keyID != "" && err == nil && s.KeyIDscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Signature#KeyID. != keyID {
71 continue
72 }
73
74 if v, supportsSignatureExtension := v.(SupportsSignatureExtensionscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/SupportsSignatureExtension#); supportsSignatureExtension {
75 if s.Extensionscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Signature#Extension. == nil || s.Extensionscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Signature#Extension..Kindscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Extension#Kind. != v.ExpectedExtensionKindscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/SupportsSignatureExtension#ExpectedExtensionKind.() {
76 continue
77 }
78 v.SetExtensionscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/SupportsSignatureExtension#SetExtension.(s.Extensionscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Signature#Extension..Extscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Extension#Ext.)
79 }
80
81 err = v.Verifyscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#Verify.(ctx, paeEnc, sig)
82 if err != nil {
83 continue
84 }
85
86 acceptedKey := AcceptedKeyscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#{
87 Publicscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#Public.: v.Publicscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#Public.(),
88 KeyIDscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#KeyID.: keyID,
89 Sigscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/AcceptedKey#Sig.: s,
90 }
91 unverified_providers = removeIndexscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/removeIndex().(providers, i)
92
93 // See https://github.com/in-toto/in-toto/pull/251
94 if _, ok := usedKeyids[keyID]; ok {
95 fmtscip-go gomod github.com/golang/go/src go1.26.0 fmt/.Printfscip-go gomod github.com/golang/go/src go1.26.0 fmt/Printf().("Found envelope signed by different subkeys of the same main key, Only one of them is counted towards the step threshold, KeyID=%s\n", keyID)
96 continue
97 }
98
99 usedKeyids[keyID] = ""
100 acceptedKeys = append(acceptedKeys, acceptedKey)
101 break
102 }
103 }
104
105 // Sanity if with some reflect magic this happens.
106 if ev.thresholdscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#threshold. <= 0 || ev.thresholdscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#threshold. > len(ev.providersscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#providers.) {
107 return nil, errorsscip-go gomod github.com/golang/go/src go1.26.0 errors/.Newscip-go gomod github.com/golang/go/src go1.26.0 errors/New().("invalid threshold")
108 }
109
110 if len(usedKeyids) < ev.thresholdscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#threshold. {
111 return acceptedKeys, fmtscip-go gomod github.com/golang/go/src go1.26.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.26.0 fmt/Errorf().("accepted signatures do not match threshold, Found: %d, Expected %d", len(acceptedKeys), ev.thresholdscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#threshold.)
112 }
113
114 return acceptedKeys, nil
115 }
116
117 func NewEnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/NewEnvelopeVerifier().(v ...Verifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#) (*EnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#, error) {
118 return NewMultiEnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/NewMultiEnvelopeVerifier().(1, v...)
119 }
120
121 func NewMultiEnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/NewMultiEnvelopeVerifier().(threshold int, p ...Verifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#) (*EnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#, error) {
122 if threshold <= 0 || threshold > len(p) {
123 return nil, errorsscip-go gomod github.com/golang/go/src go1.26.0 errors/.Newscip-go gomod github.com/golang/go/src go1.26.0 errors/New().("invalid threshold")
124 }
125
126 ev := EnvelopeVerifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#{
127 providersscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#providers.: p,
128 thresholdscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/EnvelopeVerifier#threshold.: threshold,
129 }
130
131 return &ev, nil
132 }
133
134 func SHA256KeyIDscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/SHA256KeyID().(pub cryptoscip-go gomod github.com/golang/go/src go1.26.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.26.0 crypto/PublicKey#) (string, error) {
135 // Generate public key fingerprint
136 sshpk, err := sshscip-go gomod golang.org/x/crypto v0.53.0 `golang.org/x/crypto/ssh`/.NewPublicKeyscip-go gomod golang.org/x/crypto v0.53.0 `golang.org/x/crypto/ssh`/NewPublicKey().(pub)
137 if err != nil {
138 return "", err
139 }
140 fingerprint := sshscip-go gomod golang.org/x/crypto v0.53.0 `golang.org/x/crypto/ssh`/.FingerprintSHA256scip-go gomod golang.org/x/crypto v0.53.0 `golang.org/x/crypto/ssh`/FingerprintSHA256().(sshpk)
141 return fingerprint, nil
142 }
143
144 func removeIndexscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/removeIndex().(v []Verifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier#, index int) []Verifierscip-go gomod github.com/gittuf/gittuf . `github.com/gittuf/gittuf/internal/third_party/go-securesystemslib/dsse`/Verifier# {
145 return append(v[:index], v[index+1:]...)
146 }