forked from agntcy/dir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.go
More file actions
174 lines (142 loc) · 5.17 KB
/
Copy pathverify.go
File metadata and controls
174 lines (142 loc) · 5.17 KB
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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package client
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
signv1 "github.com/agntcy/dir/api/sign/v1"
"github.com/agntcy/dir/utils/cosign"
"github.com/sigstore/sigstore-go/pkg/bundle"
"github.com/sigstore/sigstore-go/pkg/root"
"github.com/sigstore/sigstore-go/pkg/tuf"
"github.com/sigstore/sigstore-go/pkg/util"
"github.com/sigstore/sigstore-go/pkg/verify"
"github.com/theupdateframework/go-tuf/v2/metadata/fetcher"
)
// VerifyWithOIDC verifies the signature of the record using OIDC.
func (c *Client) VerifyWithOIDC(_ context.Context, req *signv1.VerifyRequest) (*signv1.VerifyResponse, error) {
// Validate request.
if req.GetRecord() == nil {
return nil, errors.New("record must be set")
}
if req.GetSignature() == nil {
return nil, errors.New("signature must be set")
}
// Extract signature data.
sigBundleRawJSON, err := base64.StdEncoding.DecodeString(req.GetSignature().GetContentBundle())
if err != nil {
return nil, fmt.Errorf("failed to decode signature: %w", err)
}
sigBundle := &bundle.Bundle{}
if err := sigBundle.UnmarshalJSON(sigBundleRawJSON); err != nil {
return nil, fmt.Errorf("failed to unmarshal signature bundle: %w", err)
}
// Convert the record to JSON.
recordJSON, err := json.Marshal(req.GetRecord())
if err != nil {
return nil, fmt.Errorf("failed to marshal record: %w", err)
}
oidcVerifier := req.GetProvider().GetOidc()
// Load identity verification options.
var identityPolicy verify.PolicyOption
{
// Create OIDC identity matcher for verification.
certID, err := verify.NewShortCertificateIdentity("", oidcVerifier.GetExpectedIssuer(), "", oidcVerifier.GetExpectedSigner())
if err != nil {
return nil, fmt.Errorf("failed to create certificate identity: %w", err)
}
identityPolicy = verify.WithCertificateIdentity(certID)
}
// Load trusted root material.
var trustedMaterial root.TrustedMaterialCollection
{
// Get staging TUF trusted root.
// TODO: allow switching between TUF environments.
fetcher := fetcher.NewDefaultFetcher()
fetcher.SetHTTPUserAgent(util.ConstructUserAgent())
tufOptions := &tuf.Options{
Root: tuf.StagingRoot(),
RepositoryBaseURL: tuf.StagingMirror,
Fetcher: fetcher,
DisableLocalCache: true, // read-only mode; prevent from pulling root CA to local dir
}
tufClient, err := tuf.New(tufOptions)
if err != nil {
return nil, fmt.Errorf("failed to create TUF client: %w", err)
}
trustedRoot, err := root.GetTrustedRoot(tufClient)
if err != nil {
return nil, fmt.Errorf("failed to get trusted root: %w", err)
}
trustedMaterial = append(trustedMaterial, trustedRoot)
}
// Create verifier session.
sev, err := verify.NewVerifier(trustedMaterial,
verify.WithSignedCertificateTimestamps(1),
verify.WithObserverTimestamps(1),
verify.WithTransparencyLog(1),
)
if err != nil {
return nil, fmt.Errorf("failed to create verifier: %w", err)
}
// Run verification
_, err = sev.Verify(sigBundle, verify.NewPolicy(verify.WithArtifact(bytes.NewReader(recordJSON)), identityPolicy))
if err != nil {
return nil, fmt.Errorf("failed to verify signature: %w", err)
}
response := &signv1.VerifyResponse{
Success: err == nil,
}
// Verify the signature.
return response, nil
}
// VerifyWithKey verifies the signature of the record using a PEM-encoded public key.
func (c *Client) VerifyWithKey(_ context.Context, req *signv1.VerifyRequest) (*signv1.VerifyResponse, error) {
keyVerifier := req.GetProvider().GetKey()
// Validate request.
if req.GetRecord() == nil {
return nil, errors.New("record must be set")
}
if req.GetSignature() == nil {
return nil, errors.New("signature must be set")
}
// Extract signature data.
sigBundleRawJSON, err := base64.StdEncoding.DecodeString(req.GetSignature().GetContentBundle())
if err != nil {
return nil, fmt.Errorf("failed to decode signature: %w", err)
}
sigBundle := &bundle.Bundle{}
if err := sigBundle.UnmarshalJSON(sigBundleRawJSON); err != nil {
return nil, fmt.Errorf("failed to unmarshal signature bundle: %w", err)
}
// Get the public key from the signature bundle and compare it with the provided key.
sigBundleVerificationMaterial := sigBundle.VerificationMaterial
if sigBundleVerificationMaterial == nil {
return nil, errors.New("signature bundle has no verification material")
}
pubKey := sigBundleVerificationMaterial.GetPublicKey()
if pubKey == nil {
return nil, errors.New("signature bundle verification material has no public key")
}
// Decode the PEM-encoded public key and generate the expected hint.
p, _ := pem.Decode(keyVerifier.GetPublicKey())
if p == nil {
return nil, errors.New("failed to decode PEM block containing public key")
}
if p.Type != "PUBLIC KEY" {
return nil, fmt.Errorf("unexpected PEM type: %s", p.Type)
}
expectedHint := string(cosign.GenerateHintFromPublicKey(p.Bytes))
if pubKey.GetHint() != expectedHint {
return nil, fmt.Errorf("public key hint mismatch: expected %s, got %s", expectedHint, pubKey.GetHint())
}
response := &signv1.VerifyResponse{
Success: err == nil,
}
return response, nil
}