feat(updates): add GPG signature verification for releases

Implement release signature verification using embedded public key.
Ensures binary integrity before applying updates.
This commit is contained in:
Finessse
2025-12-31 02:02:16 +07:00
parent 0ce9661a88
commit 64358ecfd8
3 changed files with 129 additions and 20 deletions
+47
View File
@@ -0,0 +1,47 @@
package updates
import (
_ "embed"
)
// PublicKey is the embedded GPG public key used to verify release signatures.
// This key should be replaced with the actual release signing key before deployment.
//
// To generate a new GPG key pair for signing releases:
// 1. gpg --full-generate-key (select RSA and RSA, 4096 bits)
// 2. gpg --armor --export KEY_ID > release-signing-key.asc
// 3. Replace the content of release-signing-key.asc below
//
// The private key should be kept secure and used only in the CI/CD pipeline
// to sign release binaries.
//
//go:embed release-signing-key.asc
var publicKeyArmored string
// GetPublicKey returns the embedded public key for signature verification.
func GetPublicKey() string {
return publicKeyArmored
}
// IsKeyConfigured returns true if a real public key has been configured.
// This checks if the key is not the placeholder content.
func IsKeyConfigured() bool {
// Check if the key contains the actual PGP public key block markers
return len(publicKeyArmored) > 0 &&
contains(publicKeyArmored, "-----BEGIN PGP PUBLIC KEY BLOCK-----") &&
contains(publicKeyArmored, "-----END PGP PUBLIC KEY BLOCK-----")
}
// contains is a simple helper to check if a string contains a substring.
func contains(s, substr string) bool {
return len(s) >= len(substr) && searchString(s, substr)
}
func searchString(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
+12
View File
@@ -0,0 +1,12 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----
IMPORTANT: This is a placeholder key file.
Replace this entire file with your actual GPG public key before deployment.
To generate a signing key:
gpg --full-generate-key
gpg --armor --export YOUR_KEY_ID > internal/updates/release-signing-key.asc
The key should be an RSA key of at least 4096 bits for security.
-----END PGP PUBLIC KEY BLOCK-----
+70 -20
View File
@@ -3,10 +3,13 @@ package updates
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/ProtonMail/go-crypto/openpgp"
)
// VerifyResult contains the result of a verification check.
@@ -16,9 +19,8 @@ type VerifyResult struct {
Checksum string `json:"checksum,omitempty"`
}
// VerifyDownload verifies the downloaded file.
// For now, this does basic checksum verification.
// In the future, this can be extended to verify GPG signatures.
// VerifyDownload verifies the downloaded file using GPG signature verification.
// The binary must be signed with the embedded public key to be considered valid.
func VerifyDownload(result *DownloadResult) (*VerifyResult, error) {
if result == nil || result.FilePath == "" {
return &VerifyResult{
@@ -53,33 +55,34 @@ func VerifyDownload(result *DownloadResult) (*VerifyResult, error) {
}, nil
}
// If we have a signature file, try to verify
// If we have a signature file, verify the GPG signature
if result.SignaturePath != "" {
sigValid, err := verifySignatureFile(result.FilePath, result.SignaturePath)
if err != nil {
// Signature verification failed - but file may still be valid
// Signature verification failed - file is NOT valid
return &VerifyResult{
Valid: true, // Still valid, just no signature verification
Message: fmt.Sprintf("signature verification skipped: %v", err),
Valid: false,
Message: fmt.Sprintf("signature verification failed: %v", err),
Checksum: checksum,
}, nil
}
if !sigValid {
return &VerifyResult{
Valid: false,
Message: "signature verification failed",
Message: "signature verification failed: invalid signature",
}, nil
}
return &VerifyResult{
Valid: true,
Message: "signature verified successfully",
Message: "GPG signature verified successfully",
Checksum: checksum,
}, nil
}
// No signature file provided - fail secure
return &VerifyResult{
Valid: true,
Message: "basic verification passed (no signature available)",
Valid: false,
Message: "no signature file available for verification",
Checksum: checksum,
}, nil
}
@@ -99,25 +102,72 @@ func computeSHA256(filePath string) (string, error) {
return hex.EncodeToString(h.Sum(nil)), nil
}
// verifySignatureFile attempts to verify a GPG/PGP signature.
// This is a placeholder - real implementation would use golang.org/x/crypto/openpgp
// or similar library with an embedded public key.
// verifySignatureFile verifies a GPG detached signature against the binary file.
// It uses the embedded public key to verify the signature.
func verifySignatureFile(filePath, sigPath string) (bool, error) {
// Check signature file exists
// Check if public key is configured
if !IsKeyConfigured() {
return false, errors.New("GPG public key not configured: please replace the placeholder key with your actual signing key")
}
// Read the public key
keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(GetPublicKey()))
if err != nil {
return false, fmt.Errorf("failed to read public key: %w", err)
}
// Open the binary file for verification
binaryFile, err := os.Open(filePath)
if err != nil {
return false, fmt.Errorf("failed to open binary file: %w", err)
}
defer binaryFile.Close()
// Read the signature file
sigData, err := os.ReadFile(sigPath)
if err != nil {
return false, fmt.Errorf("failed to read signature file: %w", err)
}
// For now, just check it's not empty and looks like a signature
sigStr := strings.TrimSpace(string(sigData))
if len(sigStr) < 64 {
return false, fmt.Errorf("signature file too short")
return false, errors.New("signature file is too short to be valid")
}
// TODO: Implement actual GPG signature verification
// This requires embedding a public key and using openpgp library
return false, fmt.Errorf("GPG signature verification not yet implemented")
// Determine if signature is armored (ASCII) or binary
if strings.HasPrefix(sigStr, "-----BEGIN PGP SIGNATURE-----") {
// Armored signature - use CheckArmoredDetachedSignature
signer, err := openpgp.CheckArmoredDetachedSignature(keyring, binaryFile, strings.NewReader(sigStr), nil)
if err != nil {
return false, fmt.Errorf("armored signature verification failed: %w", err)
}
if signer == nil {
return false, errors.New("signature is valid but signer not found in keyring")
}
return true, nil
}
// Binary signature - use CheckDetachedSignature
sigReader, err := os.Open(sigPath)
if err != nil {
return false, fmt.Errorf("failed to open signature file: %w", err)
}
defer sigReader.Close()
// Reset binary file position after the armored check read it
if _, err := binaryFile.Seek(0, io.SeekStart); err != nil {
return false, fmt.Errorf("failed to reset binary file position: %w", err)
}
signer, err := openpgp.CheckDetachedSignature(keyring, binaryFile, sigReader, nil)
if err != nil {
return false, fmt.Errorf("binary signature verification failed: %w", err)
}
if signer == nil {
return false, errors.New("signature is valid but signer not found in keyring")
}
return true, nil
}
// VerifyChecksum verifies a file against an expected SHA256 checksum.