Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

might work #1056

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ jobs:
with:
args: --timeout 5m

xpi-test-profile:
name: Profile the signer/xpi tests
runs-on: ubuntu-22.04
steps:
- name: Clone repository
uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: "./go.mod"

- name: Profile signer/xpi tests
run: go test -v -race -count=1 -o ./signer-xpi-tests -v -cpuprofile=cpu.prof -memprofile=mem.prof ./signer/xpi
- uses: actions/upload-artifact@v4
with:
name: signer-xpi-prof-${{ github.sha }}
path: |
cpu.prof
mem.prof
signer-xpi-tests

unit-tests:
name: Run Unit Tests
runs-on: ubuntu-22.04
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main
//go:generate ./version.sh

import (
"crypto/rsa"
"crypto/sha256"
"flag"
"fmt"
Expand Down Expand Up @@ -476,7 +477,7 @@ func (a *autographer) addSigners(signerConfs []signer.Configuration) error {
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
case xpi.Type:
s, err = xpi.New(signerConf, statsClient)
s, err = xpi.New(signerConf, rsa.GenerateKey, statsClient)
if err != nil {
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
Expand Down
13 changes: 10 additions & 3 deletions signer/xpi/cose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ func TestIntToCOSEAlg(t *testing.T) {
func TestGenerateCOSEKeyPair(t *testing.T) {
// returns an initialized XPI signer
initSigner := func(t *testing.T) *XPISigner {
keyGen := newTestRSAKeyGen()

testcase := validSignerConfigs[0]
s, err := New(testcase, nil)
s, err := New(testcase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand Down Expand Up @@ -507,7 +509,8 @@ func TestVerifyCOSESignaturesErrs(t *testing.T) {
t.Fatalf("error unmarshaling invalidSigBytes %q", err)
}

s, err := New(validSignerConfigs[0], nil)
keyGen := newTestRSAKeyGen()
s, err := New(validSignerConfigs[0], keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %q", err)
}
Expand Down Expand Up @@ -829,6 +832,7 @@ func TestVerifyCOSESignaturesErrs(t *testing.T) {
}

for i, testcase := range cases {
keyGen.Reset()
err := verifyCOSESignatures(testcase.fin, testcase.roots, testcase.opts, testcase.verificationTime)
anyMatches := false
for _, result := range testcase.results {
Expand All @@ -845,7 +849,8 @@ func TestVerifyCOSESignaturesErrs(t *testing.T) {
func TestIssueCOSESignatureErrs(t *testing.T) {
t.Parallel()

signer, err := New(validSignerConfigs[0], nil)
keyGen := newTestRSAKeyGen()
signer, err := New(validSignerConfigs[0], keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand All @@ -856,12 +861,14 @@ func TestIssueCOSESignatureErrs(t *testing.T) {
t.Fatalf("issueCOSESignature did not error on empty signer.issuerCert.Raw")
}

keyGen.Reset()
signer.issuerCert = nil
_, err = signer.issueCOSESignature("cn", []byte("manifest"), []*cose.Algorithm{cose.ES256})
if err == nil {
t.Fatalf("issueCOSESignature did not error on nil signer.issuerCert")
}

keyGen.Reset()
signer = nil
_, err = signer.issueCOSESignature("cn", []byte("manifest"), []*cose.Algorithm{cose.ES256})
if err == nil {
Expand Down
3 changes: 2 additions & 1 deletion signer/xpi/omnija_bench_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package xpi

import (
"crypto/rsa"
"testing"
)

func BenchmarkResignOmnija(b *testing.B) {
// initialize a system addon signer with an RSA key
testcase := validSignerConfigs[1]

s, err := New(testcase, nil)
s, err := New(testcase, rsa.GenerateKey, nil)
if err != nil {
b.Fatalf("signer initialization failed with: %v", err)
}
Expand Down
32 changes: 18 additions & 14 deletions signer/xpi/recommendation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ func TestMakeRecommendationFile(t *testing.T) {
t.Run("makes a recommendation file", func(t *testing.T) {
t.Parallel()

// initialize a signer
s, err := New(recTestCase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(recTestCase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("testcase signer initialization failed with: %v", err)
}
Expand Down Expand Up @@ -219,8 +219,8 @@ func TestMakeRecommendationFile(t *testing.T) {
t.Run("fails for signer not in rec mode", func(t *testing.T) {
t.Parallel()

// initialize a signer
s, err := New(validSignerConfigs[0], nil)
keyGen := newTestRSAKeyGen()
s, err := New(validSignerConfigs[0], keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("testcase %d signer initialization failed with: %v", 0, err)
}
Expand All @@ -247,8 +247,8 @@ func TestMakeRecommendationFile(t *testing.T) {
t.Fatalf("failed to unmarshal testcase for signer %q", err)
}

// initialize a signer
s, err := New(dupRecTestCase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(dupRecTestCase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("testcase signer initialization failed with: %v", err)
}
Expand All @@ -271,8 +271,8 @@ func TestMakeRecommendationFile(t *testing.T) {
t.Run("fails for invalid recommendation validity not_before after not_after", func(t *testing.T) {
t.Parallel()

// initialize a signer
s, err := New(recTestCase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(recTestCase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("testcase signer initialization failed with: %v", err)
}
Expand Down Expand Up @@ -349,8 +349,8 @@ func TestRecommendationNotIncludedInOtherSignerModes(t *testing.T) {
t.Run(tcName, func(t *testing.T) {
t.Parallel()

// initialize a signer
s, err := New(tc, nil)
keyGen := newTestRSAKeyGen()
s, err := New(tc, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("testcase %d signer initialization failed with: %v", i, err)
}
Expand Down Expand Up @@ -388,7 +388,8 @@ func TestSignFileWithRecommendation(t *testing.T) {
t.Run("signs unsignedbootstrap with PK7", func(t *testing.T) {
input := unsignedBootstrap

s, err := New(recTestCase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(recTestCase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand Down Expand Up @@ -420,7 +421,8 @@ func TestSignFileWithRecommendation(t *testing.T) {
t.Fatalf("failed to add issuer cert to pool")
}

s, err := New(recTestCase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(recTestCase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand All @@ -446,7 +448,8 @@ func TestSignFileWithRecommendation(t *testing.T) {
t.Run("signs unsignedbootstrap with PK7 fails for disallowed rec. state", func(t *testing.T) {
input := unsignedBootstrap

s, err := New(recTestCase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(recTestCase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand All @@ -463,7 +466,8 @@ func TestSignFileWithRecommendation(t *testing.T) {
t.Run("signs unsigned with rec PK7 and overwrites existing rec file", func(t *testing.T) {
input := unsignedBootstrap

s, err := New(recTestCase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(recTestCase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion signer/xpi/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *XPISigner) getRsaKey(size int) (*rsa.PrivateKey, error) {
start time.Time
)
start = time.Now()
key, err = rsa.GenerateKey(s.rand, size)
key, err = s.generateKey(s.rand, size)

if s.stats != nil {
s.stats.SendHistogram("xpi.rsa_cache.get_key", time.Since(start))
Expand Down
10 changes: 7 additions & 3 deletions signer/xpi/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func TestMakeEndEntity(t *testing.T) {
// returns an initialized XPI signer
initSigner := func(t *testing.T, testcaseid int) *XPISigner {
testcase := validSignerConfigs[testcaseid]
s, err := New(testcase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(testcase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand Down Expand Up @@ -116,7 +117,8 @@ func TestGetIssuerRSAKeySize(t *testing.T) {
// returns an initialized XPI signer
initSigner := func(t *testing.T, testcaseid int) *XPISigner {
testcase := validSignerConfigs[testcaseid]
s, err := New(testcase, nil)
keyGen := newTestRSAKeyGen()
s, err := New(testcase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand Down Expand Up @@ -158,8 +160,10 @@ func TestGetIssuerRSAKeySize(t *testing.T) {
func TestGetIssuerECDSACurve(t *testing.T) {
// returns an initialized XPI signer
initSigner := func(t *testing.T, testcaseid int) *XPISigner {
keyGen := newTestRSAKeyGen()

testcase := validSignerConfigs[testcaseid]
s, err := New(testcase, nil)
s, err := New(testcase, keyGen.GenerateKey, nil)
if err != nil {
t.Fatalf("signer initialization failed with: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion signer/xpi/xpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ type XPISigner struct {
// | | |
// not_before now / signing TS not_after
recommendationValidityDuration time.Duration

// generateKey is passed in for testing purposes but is usually
// rsa.GenerateKey.
generateKey func(io.Reader, int) (*rsa.PrivateKey, error)
}

// New initializes an XPI signer using a configuration
func New(conf signer.Configuration, stats *signer.StatsClient) (s *XPISigner, err error) {
func New(conf signer.Configuration, genKey func(io.Reader, int) (*rsa.PrivateKey, error), stats *signer.StatsClient) (s *XPISigner, err error) {
// TODO(AUT-160): instead of doing nil checks for stats all over XPISigner,
// we could just check it here once or provide a null object version of it for tests.
s = new(XPISigner)
Expand All @@ -129,6 +133,8 @@ func New(conf signer.Configuration, stats *signer.StatsClient) (s *XPISigner, er
s.PrivateKey = conf.PrivateKey

s.rand = conf.GetRand()
s.generateKey = genKey

s.issuerKey, s.issuerPublicKey, s.PublicKey, err = conf.GetKeys()
if err != nil {
return nil, fmt.Errorf("xpi: GetKeys failed to retrieve signer: %w", err)
Expand Down
Loading