Skip to content

Commit 1ad33cd

Browse files
authored
Implemented package_index.json signature verification (#791)
* Added download of signature files for 'downloads.arduino.cc' domain * Slightly simplified tmp file creation * Use patched version of rice-box This version must be kept until the following pull request is merged upstream: GeertJohan/go.rice#159 * Added signature verification subroutines * Implemented package_index.json signature verification * Added missing license headers * Added negative test on signature check * Only copy signature if the file is present
1 parent 1fa4874 commit 1ad33cd

File tree

9 files changed

+7848
-8
lines changed

9 files changed

+7848
-8
lines changed

Diff for: arduino/security/keys/arduino_public.gpg.key

5.94 KB
Binary file not shown.

Diff for: arduino/security/rice-box.go

+43
Large diffs are not rendered by default.

Diff for: arduino/security/signature_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package security
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/go-paths-helper"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestSignatureVerification(t *testing.T) {
26+
res, signer, err := VerifyArduinoDetachedSignature(paths.New("testdata/package_index.json"), paths.New("testdata/package_index.json.sig"))
27+
require.NoError(t, err)
28+
require.NotNil(t, signer)
29+
require.True(t, res)
30+
require.Equal(t, uint64(0x7baf404c2dfab4ae), signer.PrimaryKey.KeyId)
31+
32+
res, signer, err = VerifyArduinoDetachedSignature(paths.New("testdata/invalid_file.json"), paths.New("testdata/package_index.json.sig"))
33+
require.False(t, res)
34+
require.Nil(t, signer)
35+
require.Error(t, err)
36+
}

Diff for: arduino/security/signatures.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package security
17+
18+
import (
19+
"fmt"
20+
21+
rice "github.com/GeertJohan/go.rice"
22+
"github.com/arduino/go-paths-helper"
23+
"golang.org/x/crypto/openpgp"
24+
)
25+
26+
// VerifyArduinoDetachedSignature that give signaturePath GPG signature match the given targetPath file
27+
// ant the is an authentic signature from Arduino.
28+
func VerifyArduinoDetachedSignature(targetPath *paths.Path, signaturePath *paths.Path) (bool, *openpgp.Entity, error) {
29+
keysBox, err := rice.FindBox("keys")
30+
if err != nil {
31+
panic("could not find bundled signature keys")
32+
}
33+
arduinoKeyringFile, err := keysBox.Open("arduino_public.gpg.key")
34+
if err != nil {
35+
panic("could not find bundled signature keys")
36+
}
37+
keyRing, err := openpgp.ReadKeyRing(arduinoKeyringFile)
38+
if err != nil {
39+
return false, nil, fmt.Errorf("retrieving Arduino public keys: %s", err)
40+
}
41+
42+
target, err := targetPath.Open()
43+
if err != nil {
44+
return false, nil, fmt.Errorf("opening target file: %s", err)
45+
}
46+
defer target.Close()
47+
signature, err := signaturePath.Open()
48+
if err != nil {
49+
return false, nil, fmt.Errorf("opening signature file: %s", err)
50+
}
51+
defer signature.Close()
52+
signer, err := openpgp.CheckDetachedSignature(keyRing, target, signature)
53+
return (signer != nil && err == nil), signer, err
54+
}

0 commit comments

Comments
 (0)