Skip to content

Commit cbf26e4

Browse files
authored
Support Verification in sigstore/cosign with X.509 Certificate Chain (#581)
1 parent 8a3361c commit cbf26e4

10 files changed

Lines changed: 408 additions & 17 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2026 The Sigstore Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package certificate
16+
17+
import (
18+
"bytes"
19+
"crypto/x509"
20+
)
21+
22+
func IsSelfSigned(certificate *x509.Certificate) bool {
23+
if !bytes.Equal(certificate.RawSubject, certificate.RawIssuer) {
24+
return false
25+
}
26+
return certificate.CheckSignatureFrom(certificate) == nil
27+
}

‎pkg/bundle/bundle.go‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"golang.org/x/mod/semver"
3030
"google.golang.org/protobuf/encoding/protojson"
3131

32+
"github.com/sigstore/sigstore-go/internal/certificate"
3233
"github.com/sigstore/sigstore-go/internal/limits"
3334
"github.com/sigstore/sigstore-go/pkg/tlog"
3435
"github.com/sigstore/sigstore-go/pkg/verify"
@@ -53,17 +54,22 @@ func ErrValidationError(err error) error {
5354

5455
type Bundle struct {
5556
*protobundle.Bundle
56-
hasInclusionPromise bool
57-
hasInclusionProof bool
57+
hasInclusionPromise bool
58+
hasInclusionProof bool
59+
allowCertificateChain bool
5860
}
5961

60-
func NewBundle(pbundle *protobundle.Bundle) (*Bundle, error) {
62+
func NewBundle(pbundle *protobundle.Bundle, opts ...Option) (*Bundle, error) {
6163
bundle := &Bundle{
6264
Bundle: pbundle,
6365
hasInclusionPromise: false,
6466
hasInclusionProof: false,
6567
}
6668

69+
for _, opt := range opts {
70+
opt(bundle)
71+
}
72+
6773
err := bundle.validate()
6874
if err != nil {
6975
return nil, err
@@ -109,13 +115,17 @@ func (b *Bundle) validate() error {
109115
}
110116
}
111117

112-
// if bundle version >= v0.3, require verification material to not be X.509 certificate chain (only single certificate is allowed)
118+
// if bundle version >= v0.3, only allow certificate chain if option is set
113119
if semver.Compare(bundleVersion, "v0.3") >= 0 {
114-
certs := b.VerificationMaterial.GetX509CertificateChain()
120+
if !b.allowCertificateChain {
121+
certs := b.VerificationMaterial.GetX509CertificateChain()
115122

116-
if certs != nil {
117-
return errors.New("verification material cannot be X.509 certificate chain (for bundle v0.3)")
123+
if certs != nil {
124+
return errors.New("verification material cannot be X.509 certificate chain (for bundle v0.3)")
125+
}
118126
}
127+
} else if b.allowCertificateChain {
128+
return errors.New("certificate chain verification is only supported for bundle v0.3 and later")
119129
}
120130

121131
// if bundle version is >= v0.4, return error as this version is not supported
@@ -205,10 +215,14 @@ func validateBundle(b *protobundle.Bundle) error {
205215
return nil
206216
}
207217

208-
func LoadJSONFromPath(path string) (*Bundle, error) {
218+
func LoadJSONFromPath(path string, opts ...Option) (*Bundle, error) {
209219
var bundle Bundle
210220
bundle.Bundle = new(protobundle.Bundle)
211221

222+
for _, opt := range opts {
223+
opt(&bundle)
224+
}
225+
212226
contents, err := os.ReadFile(path)
213227
if err != nil {
214228
return nil, err
@@ -259,8 +273,25 @@ func (b *Bundle) VerificationContent() (verify.VerificationContent, error) {
259273
if err != nil {
260274
return nil, ErrValidationError(err)
261275
}
276+
var intermediates []*x509.Certificate
277+
if b.allowCertificateChain {
278+
for _, cert := range certs[1:] {
279+
intermediate, err := x509.ParseCertificate(cert.RawBytes)
280+
if err != nil {
281+
return nil, ErrValidationError(err)
282+
}
283+
if certificate.IsSelfSigned(intermediate) {
284+
return nil, ErrValidationError(errors.New("self-signed certificate found in certificate chain"))
285+
}
286+
if !intermediate.IsCA {
287+
return nil, ErrValidationError(errors.New("non-CA certificate found in certificate chain"))
288+
}
289+
intermediates = append(intermediates, intermediate)
290+
}
291+
}
262292
cert := &Certificate{
263-
certificate: parsedCert,
293+
certificate: parsedCert,
294+
intermediates: intermediates,
264295
}
265296
return cert, nil
266297
case *protobundle.VerificationMaterial_Certificate:

‎pkg/bundle/bundle_test.go‎

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,236 @@ func TestTlogEntriesCountAtCap(t *testing.T) {
11501150
require.NotContains(t, err.Error(), "too many tlog entries")
11511151
}
11521152
}
1153+
1154+
func TestNewBundleWithAllowCertificateChain(t *testing.T) {
1155+
t.Parallel()
1156+
1157+
v03BundleWithCertChain := &protobundle.Bundle{
1158+
MediaType: "application/vnd.dev.sigstore.bundle.v0.3+json",
1159+
VerificationMaterial: &protobundle.VerificationMaterial{
1160+
Content: &protobundle.VerificationMaterial_X509CertificateChain{
1161+
X509CertificateChain: &protocommon.X509CertificateChain{},
1162+
},
1163+
},
1164+
Content: &protobundle.Bundle_MessageSignature{},
1165+
}
1166+
1167+
for _, tc := range []struct {
1168+
name string
1169+
opts []Option
1170+
wantErr bool
1171+
}{
1172+
{"v0.3 with cert chain without option", nil, true},
1173+
{"v0.3 with cert chain with AllowCertificateChain", []Option{AllowCertificateChain()}, false},
1174+
} {
1175+
t.Run(tc.name, func(t *testing.T) {
1176+
t.Parallel()
1177+
bundle, err := NewBundle(v03BundleWithCertChain, tc.opts...)
1178+
if tc.wantErr {
1179+
require.Error(t, err)
1180+
require.Contains(t, err.Error(), "verification material cannot be X.509 certificate chain")
1181+
} else {
1182+
require.NoError(t, err)
1183+
require.NotNil(t, bundle)
1184+
}
1185+
})
1186+
}
1187+
}
1188+
1189+
func TestVerificationContentIntermediates(t *testing.T) {
1190+
t.Parallel()
1191+
rootKey, err := rsa.GenerateKey(rand.Reader, 2048)
1192+
require.NoError(t, err)
1193+
intermediateKey, err := rsa.GenerateKey(rand.Reader, 2048)
1194+
require.NoError(t, err)
1195+
subordinateIntermediateKey, err := rsa.GenerateKey(rand.Reader, 2048)
1196+
require.NoError(t, err)
1197+
leafKey, err := rsa.GenerateKey(rand.Reader, 2048)
1198+
require.NoError(t, err)
1199+
1200+
rootCert := &x509.Certificate{
1201+
SerialNumber: big.NewInt(1),
1202+
BasicConstraintsValid: true,
1203+
IsCA: true,
1204+
}
1205+
intermediateCert := &x509.Certificate{
1206+
SerialNumber: big.NewInt(2),
1207+
BasicConstraintsValid: true,
1208+
IsCA: true,
1209+
}
1210+
subordinateIntermediateCert := &x509.Certificate{
1211+
SerialNumber: big.NewInt(3),
1212+
BasicConstraintsValid: true,
1213+
IsCA: true,
1214+
}
1215+
leafCert := &x509.Certificate{SerialNumber: big.NewInt(4)}
1216+
1217+
rootDer, err := x509.CreateCertificate(rand.Reader, rootCert, rootCert, &rootKey.PublicKey, rootKey)
1218+
require.NoError(t, err)
1219+
intermediateDer, err := x509.CreateCertificate(rand.Reader, intermediateCert, rootCert, &intermediateKey.PublicKey, rootKey)
1220+
require.NoError(t, err)
1221+
subordinateIntermediateDer, err := x509.CreateCertificate(rand.Reader, subordinateIntermediateCert, intermediateCert, &subordinateIntermediateKey.PublicKey, intermediateKey)
1222+
require.NoError(t, err)
1223+
leafDer, err := x509.CreateCertificate(rand.Reader, leafCert, subordinateIntermediateCert, &leafKey.PublicKey, subordinateIntermediateKey)
1224+
require.NoError(t, err)
1225+
1226+
tests := []struct {
1227+
name string
1228+
bundle Bundle
1229+
allowCertificateChain bool
1230+
wantSerials []*big.Int
1231+
wantErr bool
1232+
wantErrContains string
1233+
}{
1234+
{
1235+
name: "single certificate verification material",
1236+
bundle: Bundle{Bundle: &protobundle.Bundle{
1237+
VerificationMaterial: &protobundle.VerificationMaterial{
1238+
Content: &protobundle.VerificationMaterial_Certificate{
1239+
Certificate: &protocommon.X509Certificate{RawBytes: leafDer},
1240+
},
1241+
},
1242+
}},
1243+
allowCertificateChain: true,
1244+
wantSerials: nil,
1245+
},
1246+
{
1247+
name: "certificate chain with leaf only",
1248+
bundle: Bundle{Bundle: &protobundle.Bundle{
1249+
VerificationMaterial: &protobundle.VerificationMaterial{
1250+
Content: &protobundle.VerificationMaterial_X509CertificateChain{
1251+
X509CertificateChain: &protocommon.X509CertificateChain{
1252+
Certificates: []*protocommon.X509Certificate{
1253+
{RawBytes: leafDer},
1254+
},
1255+
},
1256+
},
1257+
},
1258+
}},
1259+
allowCertificateChain: true,
1260+
wantSerials: nil,
1261+
},
1262+
{
1263+
name: "certificate chain with leaf and intermediate",
1264+
bundle: Bundle{Bundle: &protobundle.Bundle{
1265+
VerificationMaterial: &protobundle.VerificationMaterial{
1266+
Content: &protobundle.VerificationMaterial_X509CertificateChain{
1267+
X509CertificateChain: &protocommon.X509CertificateChain{
1268+
Certificates: []*protocommon.X509Certificate{
1269+
{RawBytes: leafDer},
1270+
{RawBytes: subordinateIntermediateDer},
1271+
},
1272+
},
1273+
},
1274+
},
1275+
}},
1276+
allowCertificateChain: true,
1277+
wantSerials: []*big.Int{subordinateIntermediateCert.SerialNumber},
1278+
},
1279+
{
1280+
name: "certificate chain with leaf and multiple intermediates",
1281+
bundle: Bundle{Bundle: &protobundle.Bundle{
1282+
VerificationMaterial: &protobundle.VerificationMaterial{
1283+
Content: &protobundle.VerificationMaterial_X509CertificateChain{
1284+
X509CertificateChain: &protocommon.X509CertificateChain{
1285+
Certificates: []*protocommon.X509Certificate{
1286+
{RawBytes: leafDer},
1287+
{RawBytes: subordinateIntermediateDer},
1288+
{RawBytes: intermediateDer},
1289+
},
1290+
},
1291+
},
1292+
},
1293+
}},
1294+
allowCertificateChain: true,
1295+
wantSerials: []*big.Int{subordinateIntermediateCert.SerialNumber, intermediateCert.SerialNumber},
1296+
},
1297+
{
1298+
name: "certificate chain with intermediate but certificate chain option disabled",
1299+
bundle: Bundle{Bundle: &protobundle.Bundle{
1300+
VerificationMaterial: &protobundle.VerificationMaterial{
1301+
Content: &protobundle.VerificationMaterial_X509CertificateChain{
1302+
X509CertificateChain: &protocommon.X509CertificateChain{
1303+
Certificates: []*protocommon.X509Certificate{
1304+
{RawBytes: leafDer},
1305+
{RawBytes: subordinateIntermediateDer},
1306+
},
1307+
},
1308+
},
1309+
},
1310+
}},
1311+
allowCertificateChain: false,
1312+
wantSerials: nil,
1313+
},
1314+
{
1315+
name: "certificate chain with leaf, intermediate, and self-signed root is rejected",
1316+
bundle: Bundle{Bundle: &protobundle.Bundle{
1317+
MediaType: "application/vnd.dev.sigstore.bundle.v0.3+json",
1318+
VerificationMaterial: &protobundle.VerificationMaterial{
1319+
Content: &protobundle.VerificationMaterial_X509CertificateChain{
1320+
X509CertificateChain: &protocommon.X509CertificateChain{
1321+
Certificates: []*protocommon.X509Certificate{
1322+
{RawBytes: leafDer},
1323+
{RawBytes: subordinateIntermediateDer},
1324+
{RawBytes: rootDer},
1325+
},
1326+
},
1327+
},
1328+
},
1329+
}},
1330+
allowCertificateChain: true,
1331+
wantErr: true,
1332+
wantErrContains: "self-signed certificate found in certificate chain",
1333+
},
1334+
}
1335+
1336+
for _, tt := range tests {
1337+
t.Run(tt.name, func(t *testing.T) {
1338+
t.Parallel()
1339+
tt.bundle.allowCertificateChain = tt.allowCertificateChain
1340+
vc, err := tt.bundle.VerificationContent()
1341+
if tt.wantErr {
1342+
require.Error(t, err)
1343+
if tt.wantErrContains != "" {
1344+
require.ErrorContains(t, err, tt.wantErrContains)
1345+
}
1346+
return
1347+
}
1348+
require.NoError(t, err)
1349+
got := vc.Intermediates()
1350+
require.Len(t, got, len(tt.wantSerials))
1351+
for i, cert := range got {
1352+
require.True(t, cert.IsCA)
1353+
require.Equal(t, tt.wantSerials[i], cert.SerialNumber)
1354+
}
1355+
})
1356+
}
1357+
}
1358+
1359+
func TestVerificationContentIntermediatesParseError(t *testing.T) {
1360+
t.Parallel()
1361+
leafKey, err := rsa.GenerateKey(rand.Reader, 2048)
1362+
require.NoError(t, err)
1363+
leafTmpl := &x509.Certificate{SerialNumber: big.NewInt(1)}
1364+
leafDer, err := x509.CreateCertificate(rand.Reader, leafTmpl, leafTmpl, &leafKey.PublicKey, leafKey)
1365+
require.NoError(t, err)
1366+
1367+
bundle := Bundle{
1368+
Bundle: &protobundle.Bundle{
1369+
VerificationMaterial: &protobundle.VerificationMaterial{
1370+
Content: &protobundle.VerificationMaterial_X509CertificateChain{
1371+
X509CertificateChain: &protocommon.X509CertificateChain{
1372+
Certificates: []*protocommon.X509Certificate{
1373+
{RawBytes: leafDer},
1374+
{RawBytes: []byte("not a certificate")},
1375+
},
1376+
},
1377+
},
1378+
},
1379+
},
1380+
allowCertificateChain: true,
1381+
}
1382+
_, err = bundle.VerificationContent()
1383+
require.Error(t, err)
1384+
require.ErrorIs(t, err, ErrValidation)
1385+
}

‎pkg/bundle/options.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2026 The Sigstore Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package bundle
16+
17+
// Option configures optional behaviour when constructing a Bundle.
18+
type Option func(*Bundle)
19+
20+
// AllowCertificateChain permits bundles with version >= v0.3 to contain
21+
// X.509 certificate chains in their verification material. By default,
22+
// v0.3+ bundles require a single certificate rather than a chain.
23+
func AllowCertificateChain() Option {
24+
return func(b *Bundle) {
25+
b.allowCertificateChain = true
26+
}
27+
}

0 commit comments

Comments
 (0)