2022-04-17 01:02:07 +02:00
package lib
import (
2022-10-03 05:14:54 +02:00
"bufio"
2022-04-17 01:02:07 +02:00
"context"
"crypto/sha256"
"encoding/hex"
2023-11-11 20:26:39 +01:00
"errors"
2022-04-17 01:02:07 +02:00
"fmt"
"io"
"os"
2022-06-05 06:42:40 +02:00
"strings"
2022-04-17 01:02:07 +02:00
2023-11-11 20:26:39 +01:00
"github.com/ddworken/hishtory/client/hctx"
2023-11-10 05:51:47 +01:00
"github.com/ddworken/hishtory/shared"
2022-09-02 09:15:58 +02:00
"github.com/slsa-framework/slsa-verifier/options"
"github.com/slsa-framework/slsa-verifier/verifiers"
2022-04-17 01:02:07 +02:00
)
2023-11-11 20:26:39 +01:00
var errUserAbortUpdate error = errors . New ( "update cancelled" )
2023-09-05 21:45:17 +02:00
func verify ( ctx context . Context , provenance [ ] byte , artifactHash , source , branch , versionTag string ) error {
2022-09-02 09:15:58 +02:00
provenanceOpts := & options . ProvenanceOpts {
2023-11-05 09:15:54 +01:00
ExpectedSourceURI : source ,
ExpectedBranch : & branch ,
ExpectedDigest : artifactHash ,
}
if versionTag != "" {
provenanceOpts . ExpectedVersionedTag = & versionTag
2022-09-02 09:15:58 +02:00
}
builderOpts := & options . BuilderOpts { }
2024-03-24 22:16:08 +01:00
_ , _ , err := verifiers . VerifyArtifact ( ctx , provenance , artifactHash , provenanceOpts , builderOpts )
2022-09-02 09:15:58 +02:00
return err
2022-04-17 01:02:07 +02:00
}
2022-06-05 06:42:40 +02:00
func checkForDowngrade ( currentVersionS , newVersionS string ) error {
2023-11-10 05:51:47 +01:00
currentVersion , err := shared . ParseVersionString ( currentVersionS )
2022-06-05 06:42:40 +02:00
if err != nil {
2023-11-10 05:51:47 +01:00
return fmt . Errorf ( "failed to parse current version string: %w" , err )
2022-06-05 06:42:40 +02:00
}
2023-11-10 05:51:47 +01:00
newVersion , err := shared . ParseVersionString ( newVersionS )
2022-06-05 06:42:40 +02:00
if err != nil {
2023-11-10 05:51:47 +01:00
return fmt . Errorf ( "failed to parse new version string: %w" , err )
2022-06-05 06:42:40 +02:00
}
2023-11-10 05:51:47 +01:00
if currentVersion . GreaterThan ( newVersion ) {
2022-06-05 06:42:40 +02:00
return fmt . Errorf ( "failed to update because the new version (%#v) is a downgrade compared to the current version (%#v)" , newVersionS , currentVersionS )
}
return nil
}
2023-09-14 07:45:49 +02:00
func VerifyBinary ( ctx context . Context , binaryPath , attestationPath , versionTag string ) error {
2022-05-27 08:45:08 +02:00
if os . Getenv ( "HISHTORY_DISABLE_SLSA_ATTESTATION" ) == "true" {
return nil
}
2023-10-14 19:52:35 +02:00
resp , err := ApiGet ( ctx , "/api/v1/slsa-status?newVersion=" + versionTag )
2022-11-01 01:32:55 +01:00
if err != nil {
2023-11-11 20:26:39 +01:00
hctx . GetLogger ( ) . Infof ( "Failed to query SLSA status (err=%#v), assuming that SLSA is currently working" , err )
2022-11-01 01:32:55 +01:00
}
2023-11-11 20:26:39 +01:00
if err == nil && string ( resp ) != "OK" {
slsa_status_error :
fmt . Printf ( "SLSA verification is currently experiencing issues:%s\nWhat would you like to do?" , string ( resp ) )
fmt . Println ( "To abort the update, type 'a'" )
fmt . Println ( "To continue with the update even though it may fail, type 'c'" )
fmt . Println ( "To update and skip SLSA validation, type 's'" )
reader := bufio . NewReader ( os . Stdin )
userChoice , err := reader . ReadString ( '\n' )
if err != nil {
fmt . Printf ( "failed to read response: %v\n" , err )
goto slsa_status_error
}
userChoice = strings . TrimSpace ( userChoice )
switch userChoice {
case "a" , "A" :
return errUserAbortUpdate
case "c" , "C" :
// Continue the update and perform SLSA validation even though it may fail
goto slsa_status_error_continue_update
case "s" , "S" :
// Skip validation and return nil as if the binary passed validation
return nil
default :
fmt . Printf ( "user selection %#v was not one of 'a', 'c', 's'\n" , userChoice )
goto slsa_status_error
}
2022-11-01 01:32:55 +01:00
}
2023-11-11 20:26:39 +01:00
slsa_status_error_continue_update :
2022-06-05 06:42:40 +02:00
if err := checkForDowngrade ( Version , versionTag ) ; err != nil && os . Getenv ( "HISHTORY_ALLOW_DOWNGRADE" ) == "true" {
return err
}
2022-04-17 01:02:07 +02:00
attestation , err := os . ReadFile ( attestationPath )
if err != nil {
2023-09-05 21:08:55 +02:00
return fmt . Errorf ( "failed to read attestation file: %w" , err )
2022-04-17 01:02:07 +02:00
}
2022-05-27 08:45:08 +02:00
hash , err := getFileHash ( binaryPath )
if err != nil {
return err
}
2022-09-22 05:22:34 +02:00
return verify ( ctx , attestation , hash , "github.com/ddworken/hishtory" , "master" , versionTag )
2022-05-27 08:45:08 +02:00
}
func getFileHash ( binaryPath string ) ( string , error ) {
2022-04-17 01:02:07 +02:00
binaryFile , err := os . Open ( binaryPath )
if err != nil {
2023-09-05 21:08:55 +02:00
return "" , fmt . Errorf ( "failed to read binary for verification purposes: %w" , err )
2022-04-17 01:02:07 +02:00
}
defer binaryFile . Close ( )
hasher := sha256 . New ( )
if _ , err := io . Copy ( hasher , binaryFile ) ; err != nil {
2023-09-05 21:08:55 +02:00
return "" , fmt . Errorf ( "failed to hash binary: %w" , err )
2022-04-17 01:02:07 +02:00
}
hash := hex . EncodeToString ( hasher . Sum ( nil ) )
2022-05-27 08:45:08 +02:00
return hash , nil
2022-04-17 01:02:07 +02:00
}
2022-10-03 05:14:54 +02:00
2023-09-14 07:45:49 +02:00
func HandleSlsaFailure ( srcErr error ) error {
2023-11-11 20:26:39 +01:00
if errors . Is ( srcErr , errUserAbortUpdate ) {
return srcErr
}
2024-03-24 22:34:54 +01:00
fmt . Printf ( "\nFailed to verify SLSA provenance due to err: %v\nThis is likely due to a SLSA bug (SLSA is a brand new standard, and like all new things, has bugs). Ignoring this failure means falling back to the way most software does updates. Do you want to ignore this failure and update anyways? [y/N]" , srcErr )
2022-10-03 05:14:54 +02:00
reader := bufio . NewReader ( os . Stdin )
resp , err := reader . ReadString ( '\n' )
if err == nil && strings . TrimSpace ( resp ) == "y" {
fmt . Println ( "Proceeding with update..." )
return nil
}
2023-09-05 21:08:55 +02:00
return fmt . Errorf ( "failed to verify SLSA provenance of the updated binary, aborting update (to bypass, set `export HISHTORY_DISABLE_SLSA_ATTESTATION=true`): %w" , srcErr )
2022-10-03 05:14:54 +02:00
}