|
| 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