Skip to content

Commit 4c4e04b

Browse files
committed
make verifySignature public, enhanced tests, remove board word
1 parent b8d8a9c commit 4c4e04b

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

Diff for: arduino/security/signature_test.go

+30-8
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ import (
1919
"testing"
2020

2121
"github.com/arduino/go-paths-helper"
22+
rice "github.com/cmaglie/go.rice"
2223
"github.com/stretchr/testify/require"
2324
)
2425

2526
var (
26-
PackageIndexPath = paths.New("testdata/package_index.json")
27-
PackageSignaturePath = paths.New("testdata/package_index.json.sig")
28-
BoardIndexPath = paths.New("testdata/module_firmware_index.json")
29-
BoardSignaturePath = paths.New("testdata/module_firmware_index.json.sig")
30-
BoardKey = paths.New("testdata/module_firmware_index_public.gpg.key")
31-
InvalidIndexPath = paths.New("testdata/invalid_file.json")
27+
PackageIndexPath = paths.New("testdata/package_index.json")
28+
PackageSignaturePath = paths.New("testdata/package_index.json.sig")
29+
ModuleFWIndexPath = paths.New("testdata/module_firmware_index.json")
30+
ModuleFWSignaturePath = paths.New("testdata/module_firmware_index.json.sig")
31+
ModuleFWIndexKey = paths.New("testdata/module_firmware_index_public.gpg.key")
32+
InvalidIndexPath = paths.New("testdata/invalid_file.json")
3233
)
3334

3435
func TestVerifyArduinoDetachedSignature(t *testing.T) {
@@ -45,13 +46,34 @@ func TestVerifyArduinoDetachedSignature(t *testing.T) {
4546
}
4647

4748
func TestVerifyDetachedSignature(t *testing.T) {
48-
res, signer, err := VerifyDetachedSignature(BoardIndexPath, BoardSignaturePath, BoardKey)
49+
res, signer, err := VerifyDetachedSignature(ModuleFWIndexPath, ModuleFWSignaturePath, ModuleFWIndexKey)
4950
require.NoError(t, err)
5051
require.NotNil(t, signer)
5152
require.True(t, res)
5253
require.Equal(t, uint64(0x82f2d7c7c5a22a73), signer.PrimaryKey.KeyId)
5354

54-
res, signer, err = VerifyDetachedSignature(InvalidIndexPath, PackageSignaturePath, BoardKey)
55+
res, signer, err = VerifyDetachedSignature(InvalidIndexPath, PackageSignaturePath, ModuleFWIndexKey)
56+
require.False(t, res)
57+
require.Nil(t, signer)
58+
require.Error(t, err)
59+
}
60+
61+
func TestVerifySignature(t *testing.T) {
62+
keysBox, err := rice.FindBox("keys")
63+
if err != nil {
64+
panic("could not find bundled signature keys")
65+
}
66+
arduinoKeyringFile, err := keysBox.Open("arduino_public.gpg.key")
67+
if err != nil {
68+
panic("could not find bundled signature keys")
69+
}
70+
res, signer, err := VerifySignature(PackageIndexPath, PackageSignaturePath, arduinoKeyringFile)
71+
require.NoError(t, err)
72+
require.NotNil(t, signer)
73+
require.True(t, res)
74+
require.Equal(t, uint64(0x7baf404c2dfab4ae), signer.PrimaryKey.KeyId)
75+
76+
res, signer, err = VerifySignature(InvalidIndexPath, PackageSignaturePath, arduinoKeyringFile)
5577
require.False(t, res)
5678
require.Nil(t, signer)
5779
require.Error(t, err)

Diff for: arduino/security/signatures.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
// signaturePath file) matches the given targetPath file and is an authentic
3030
// signature from the bundled trusted keychain. If any of the above conditions
3131
// fails this function returns false. The PGP entity in the trusted keychain that
32-
// produced the signature is returned too.
32+
// produced the signature is returned too. This function use the default and bundled
33+
// arduino_public.gpg.key
3334
func VerifyArduinoDetachedSignature(targetPath *paths.Path, signaturePath *paths.Path) (bool, *openpgp.Entity, error) {
3435
keysBox, err := rice.FindBox("keys")
3536
if err != nil {
@@ -39,7 +40,7 @@ func VerifyArduinoDetachedSignature(targetPath *paths.Path, signaturePath *paths
3940
if err != nil {
4041
panic("could not find bundled signature keys")
4142
}
42-
return verifySignature(targetPath, signaturePath, arduinoKeyringFile)
43+
return VerifySignature(targetPath, signaturePath, arduinoKeyringFile)
4344
}
4445

4546
// VerifyDetachedSignature checks that the detached GPG signature (in the
@@ -54,14 +55,15 @@ func VerifyDetachedSignature(targetPath *paths.Path, signaturePath *paths.Path,
5455
panic("could not open signature keys")
5556
}
5657
defer arduinoKeyringFile.Close()
57-
return verifySignature(targetPath, signaturePath, arduinoKeyringFile)
58+
return VerifySignature(targetPath, signaturePath, arduinoKeyringFile)
5859
}
5960

60-
//verifySignature is an helper function that checks that the detached GPG signature (in the
61+
// VerifySignature checks that the detached GPG signature (in the
6162
// signaturePath file) matches the given targetPath file and is an authentic
62-
// signature. If any of the above conditions fails this function returns false.
63+
// signature. This function allows to pass an io.Reader to read the custom key.
64+
// If any of the above conditions fails this function returns false.
6365
// The PGP entity in the trusted keychain that produced the signature is returned too.
64-
func verifySignature(targetPath *paths.Path, signaturePath *paths.Path, arduinoKeyringFile io.Reader) (bool, *openpgp.Entity, error) {
66+
func VerifySignature(targetPath *paths.Path, signaturePath *paths.Path, arduinoKeyringFile io.Reader) (bool, *openpgp.Entity, error) {
6567
keyRing, err := openpgp.ReadKeyRing(arduinoKeyringFile)
6668
if err != nil {
6769
return false, nil, fmt.Errorf("retrieving Arduino public keys: %s", err)

0 commit comments

Comments
 (0)