File: baseca_test.go

package info (click to toggle)
golang-github-sigstore-fulcio 1.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 2,636 kB
  • sloc: sh: 399; makefile: 104; python: 27
file content (153 lines) | stat: -rw-r--r-- 4,927 bytes parent folder | download
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
// Copyright 2022 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 baseca

import (
	"context"
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/x509"
	"encoding/asn1"
	"reflect"
	"testing"

	ct "github.com/google/certificate-transparency-go"
	"github.com/sigstore/fulcio/pkg/ca"
	"github.com/sigstore/fulcio/pkg/certificate"
	"github.com/sigstore/fulcio/pkg/test"
	"github.com/sigstore/sigstore/pkg/cryptoutils"
	"github.com/sigstore/sigstore/pkg/signature"
)

func TestBaseCARoot(t *testing.T) {
	signer, _, err := signature.NewDefaultECDSASignerVerifier()
	if err != nil {
		t.Fatalf("unexpected error generating signer: %v", err)
	}

	rootCert, rootKey, _ := test.GenerateRootCA()
	subCert, _, _ := test.GenerateSubordinateCA(rootCert, rootKey)
	certChain := []*x509.Certificate{subCert, rootCert}

	bca := BaseCA{
		SignerWithChain: &ca.SignerCerts{Certs: certChain, Signer: signer},
	}

	rootChains, err := bca.TrustBundle(context.TODO())
	if err != nil {
		t.Fatalf("unexpected error reading root: %v", err)
	}
	if len(rootChains) != 1 {
		t.Fatalf("unexpected number of chains: %d", len(rootChains))
	}

	if !reflect.DeepEqual(certChain, rootChains[0]) {
		t.Fatal("expected cert chains to be equivalent")
	}
}

func TestBaseCAGetSignerWithChain(t *testing.T) {
	signer, _, err := signature.NewDefaultECDSASignerVerifier()
	if err != nil {
		t.Fatalf("unexpected error generating signer: %v", err)
	}

	rootCert, rootKey, _ := test.GenerateRootCA()
	subCert, _, _ := test.GenerateSubordinateCA(rootCert, rootKey)
	certChain := []*x509.Certificate{subCert, rootCert}

	bca := BaseCA{
		SignerWithChain: &ca.SignerCerts{Certs: certChain, Signer: signer},
	}

	foundCertChain, foundSigner := bca.GetSignerWithChain()

	if !reflect.DeepEqual(certChain, foundCertChain) {
		t.Fatal("expected cert chains to be equivalent")
	}

	if err := cryptoutils.EqualKeys(signer.Public(), foundSigner.Public()); err != nil {
		t.Fatalf("expected keys to be equivalent, expected %v, got %v, error %v", signer.Public(), foundSigner.Public(), err)
	}
}

type testPrincipal struct{}

func (tp testPrincipal) Name(context.Context) string {
	return "doesntmatter"
}

func (tp testPrincipal) Embed(_ context.Context, cert *x509.Certificate) (err error) {
	cert.EmailAddresses = []string{"alice@example.com"}
	cert.ExtraExtensions, err = certificate.Extensions{
		Issuer: "example.com",
	}.Render()
	return
}

func TestCreatePrecertificateAndIssueFinalCertificate(t *testing.T) {
	rootCert, rootKey, _ := test.GenerateRootCA()
	subCert, subKey, _ := test.GenerateSubordinateCA(rootCert, rootKey)

	priv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	certChain := []*x509.Certificate{subCert, rootCert}

	bca := BaseCA{
		SignerWithChain: &ca.SignerCerts{Certs: certChain, Signer: subKey},
	}

	precsc, err := bca.CreatePrecertificate(context.TODO(), testPrincipal{}, priv.Public())

	if err != nil {
		t.Fatalf("error generating precertificate: %v", err)
	}
	if !subKey.Equal(precsc.PrivateKey) {
		t.Fatal("subordinate private keys are not equal")
	}
	if !reflect.DeepEqual(certChain, precsc.CertChain) {
		t.Fatal("certificate chains are not equal")
	}

	// check cert doesn't verify due to poison extension
	rootPool := x509.NewCertPool()
	rootPool.AddCert(precsc.CertChain[1])
	subPool := x509.NewCertPool()
	subPool.AddCert(precsc.CertChain[0])
	_, err = precsc.PreCert.Verify(x509.VerifyOptions{Roots: rootPool, Intermediates: subPool, KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}})
	if err == nil || err.Error() != "x509: unhandled critical extension" {
		t.Fatalf("expected unhandled critical ext error, got %v", err)
	}

	csc, err := bca.IssueFinalCertificate(context.TODO(), precsc, &ct.SignedCertificateTimestamp{SCTVersion: 1})
	if err != nil {
		t.Fatalf("error issuing certificate: %v", err)
	}
	// verify will now work since poison extension is removed
	_, err = csc.FinalCertificate.Verify(x509.VerifyOptions{Roots: rootPool, Intermediates: subPool, KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}})
	if err != nil {
		t.Fatalf("unexpected error verifying final certificate: %v", err)
	}
	var foundSct bool
	for _, ext := range csc.FinalCertificate.Extensions {
		if ext.Id.Equal(asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 4, 2}) {
			foundSct = true
		}
	}
	if !foundSct {
		t.Fatal("expected SCT extension to be in certificate")
	}
}