Skip to content

Implement function to verify index signature with custom key #1304

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

Merged
merged 5 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
Binary file not shown.
28 changes: 25 additions & 3 deletions arduino/security/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,36 @@ import (
"github.com/stretchr/testify/require"
)

func TestSignatureVerification(t *testing.T) {
res, signer, err := VerifyArduinoDetachedSignature(paths.New("testdata/package_index.json"), paths.New("testdata/package_index.json.sig"))
var (
PackageIndexPath = paths.New("testdata/package_index.json")
PackageSignaturePath = paths.New("testdata/package_index.json.sig")
BoardIndexPath = paths.New("testdata/module_firmware_index.json")
BoardSignaturePath = paths.New("testdata/module_firmware_index.json.sig")
BoardKey = paths.New("keys/module_firmware_index_public.gpg.key")
InvalidIndexPath = paths.New("testdata/invalid_file.json")
)

func TestVerifyArduinoDetachedSignature(t *testing.T) {
res, signer, err := VerifyArduinoDetachedSignature(PackageIndexPath, PackageSignaturePath)
require.NoError(t, err)
require.NotNil(t, signer)
require.True(t, res)
require.Equal(t, uint64(0x7baf404c2dfab4ae), signer.PrimaryKey.KeyId)

res, signer, err = VerifyArduinoDetachedSignature(paths.New("testdata/invalid_file.json"), paths.New("testdata/package_index.json.sig"))
res, signer, err = VerifyArduinoDetachedSignature(InvalidIndexPath, PackageSignaturePath)
require.False(t, res)
require.Nil(t, signer)
require.Error(t, err)
}

func TestVerifyDetachedSignature(t *testing.T) {
res, signer, err := VerifyDetachedSignature(BoardIndexPath, BoardSignaturePath, BoardKey)
require.NoError(t, err)
require.NotNil(t, signer)
require.True(t, res)
require.Equal(t, uint64(0x82f2d7c7c5a22a73), signer.PrimaryKey.KeyId)

res, signer, err = VerifyDetachedSignature(InvalidIndexPath, PackageSignaturePath, BoardKey)
require.False(t, res)
require.Nil(t, signer)
require.Error(t, err)
Expand Down
26 changes: 25 additions & 1 deletion arduino/security/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package security

import (
"fmt"
"io"
"os"

"github.com/arduino/go-paths-helper"
rice "github.com/cmaglie/go.rice"
Expand All @@ -37,11 +39,33 @@ func VerifyArduinoDetachedSignature(targetPath *paths.Path, signaturePath *paths
if err != nil {
panic("could not find bundled signature keys")
}
return verifySignature(targetPath, signaturePath, arduinoKeyringFile)
}

// VerifyDetachedSignature checks that the detached GPG signature (in the
// signaturePath file) matches the given targetPath file and is an authentic
// signature from the bundled trusted keychain. The keyPath is the path of the public key used.
// This function allows to specify the path of the key to use.
// If any of the above conditions fails this function returns false.
// The PGP entity in the trusted keychain that produced the signature is returned too.
func VerifyDetachedSignature(targetPath *paths.Path, signaturePath *paths.Path, keyPath *paths.Path) (bool, *openpgp.Entity, error) {
arduinoKeyringFile, err := os.Open(keyPath.String())
if err != nil {
panic("could not open signature keys")
}
defer arduinoKeyringFile.Close()
return verifySignature(targetPath, signaturePath, arduinoKeyringFile)
}

//verifySignature is an helper function that checks that the detached GPG signature (in the
// signaturePath file) matches the given targetPath file and is an authentic
// signature. If any of the above conditions fails this function returns false.
// The PGP entity in the trusted keychain that produced the signature is returned too.
func verifySignature(targetPath *paths.Path, signaturePath *paths.Path, arduinoKeyringFile io.Reader) (bool, *openpgp.Entity, error) {
keyRing, err := openpgp.ReadKeyRing(arduinoKeyringFile)
if err != nil {
return false, nil, fmt.Errorf("retrieving Arduino public keys: %s", err)
}

target, err := targetPath.Open()
if err != nil {
return false, nil, fmt.Errorf("opening target file: %s", err)
Expand Down
Loading