-
Notifications
You must be signed in to change notification settings - Fork 73
Add a script that can sign a file based on an ssh key #948
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cbbd94c
Add a script that can sign a file based on an ssh key
ocaisa fe29597
Add test for script
ocaisa d3441d1
Test the signing aspect of the script in the container
ocaisa 2a88400
More accurately represent expected workflow
ocaisa a4f66c2
Use $TARBALL everywhere
ocaisa ea15255
Remove references to tarball in script, can be any file
ocaisa 524760c
Address review and update the script
ocaisa 0c8b7cb
Another round of tidying up
ocaisa 9d90c12
Keep the signed file the same in tests, change the allowed signers in…
ocaisa 4896ac2
Use correct key for second case
ocaisa 739ca78
Use correct key for second case
ocaisa 2be3113
Use conservative umask for entire script
ocaisa d7bc52a
umask too restrictive
ocaisa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| #!/bin/bash | ||
| # | ||
| # SSH Signature Signing and Verification Script | ||
| # - Sign a file using an SSH private key. | ||
| # - Verify a signed file using an allowed signers file. | ||
| # | ||
| # Generates a signature file named `<file>.sig` in the same directory. | ||
| # | ||
| # Author: Alan O'Cais | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| # Usage message | ||
| usage() { | ||
| cat <<EOF | ||
| Usage: | ||
| $0 sign <private_key> <file> | ||
| $0 verify <allowed_signers_file> <file> [signature_file] | ||
|
|
||
| Options: | ||
| sign: | ||
| - <private_key>: Path to SSH private key (use KEY_PASSPHRASE env for passphrase) | ||
| - <file>: File to sign | ||
|
|
||
| verify: | ||
| - <allowed_signers_file>: Path to the allowed signers file | ||
| - <file>: File to verify | ||
| - [signature_file]: Optional, defaults to '<file>.sig' | ||
|
|
||
| Example allowed signers format: | ||
| identity_1 <public-key> | ||
| EOF | ||
| exit 9 | ||
| } | ||
|
|
||
| # Error codes | ||
| FILE_PROBLEM=1 | ||
| CONVERSION_FAILURE=2 | ||
| VALIDATION_FAILED=3 | ||
|
|
||
| # Ensure minimum arguments | ||
| [ "$#" -lt 3 ] && usage | ||
|
|
||
| MODE="$1" | ||
| FILE_TO_SIGN="$3" | ||
|
|
||
| # Ensure the target file exists | ||
| if [ ! -f "$FILE_TO_SIGN" ]; then | ||
| echo "Error: File '$FILE_TO_SIGN' not found." | ||
| exit $FILE_PROBLEM | ||
| fi | ||
|
|
||
| # Use a very conservatuve umask throughout this script since we are dealing with sensitive things | ||
| umask 077 || { echo "Error: Failed to set 0177 umask."; exit $FILE_PROBLEM; } | ||
|
|
||
| # Create a restricted temporary directory and ensure cleanup on exit | ||
| TEMP_DIR=$(mktemp -d) || { echo "Error: Failed to create temporary directory."; exit $FILE_PROBLEM; } | ||
| trap 'rm -rf "$TEMP_DIR"' EXIT | ||
|
|
||
| # Converts the SSH private key to OpenSSH format and generates a public key | ||
| convert_private_key() { | ||
| local input_key="$1" | ||
| local output_key="$2" | ||
|
|
||
| echo "Converting SSH key to OpenSSH format..." | ||
ocaisa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cp "$input_key" "$output_key" || { echo "Error: Failed to copy $input_key to $output_key"; exit $FILE_PROBLEM; } | ||
|
|
||
| # This saves the key in the default OpenSSH format (which is required for signing) | ||
| ssh-keygen -p -f "$output_key" -P "${KEY_PASSPHRASE:-}" -N "${KEY_PASSPHRASE:-}" || { | ||
| echo "Error: Failed to convert key to OpenSSH format." | ||
| exit $CONVERSION_FAILURE | ||
| } | ||
|
|
||
| # Extract the public key from the private key | ||
| ssh-keygen -y -f "$input_key" -P "${KEY_PASSPHRASE:-}" > "${output_key}.pub" || { | ||
| echo "Error: Failed to extract public key." | ||
| exit $CONVERSION_FAILURE | ||
| } | ||
| } | ||
|
|
||
| # Sign mode | ||
| if [ "$MODE" == "sign" ]; then | ||
| PRIVATE_KEY="$2" | ||
| TEMP_KEY="$TEMP_DIR/converted_key" | ||
| SIG_FILE="${FILE_TO_SIGN}.sig" | ||
|
|
||
| # Check for key and existing signature | ||
| [ ! -f "$PRIVATE_KEY" ] && { echo "Error: Private key not found."; exit $FILE_PROBLEM; } | ||
| [ -f "$SIG_FILE" ] && { echo "Error: Signature already exists. Remove to re-sign."; exit $FILE_PROBLEM; } | ||
|
|
||
| convert_private_key "$PRIVATE_KEY" "$TEMP_KEY" | ||
|
|
||
| echo "Signing the file..." | ||
| ssh-keygen -Y sign -f "$TEMP_KEY" -P "${KEY_PASSPHRASE:-}" -n file "$FILE_TO_SIGN" | ||
|
|
||
| [ ! -f "$SIG_FILE" ] && { echo "Error: Signing failed."; exit $FILE_PROBLEM; } | ||
| echo "Signature created: $SIG_FILE" | ||
|
|
||
| cat <<EOF | ||
|
|
||
| For verification, your allowed signers file could contain: | ||
| identity_1 $(cat "${TEMP_KEY}.pub") | ||
| EOF | ||
|
|
||
| echo "Validating the signature..." | ||
| ssh-keygen -Y check-novalidate -n file -f "${TEMP_KEY}.pub" -s "$SIG_FILE" < "$FILE_TO_SIGN" || { | ||
| echo "Error: Signature validation failed." | ||
| exit $VALIDATION_FAILED | ||
| } | ||
|
|
||
| # Verify mode | ||
| elif [ "$MODE" == "verify" ]; then | ||
| ALLOWED_SIGNERS_FILE="$2" | ||
| SIG_FILE="${4:-${FILE_TO_SIGN}.sig}" | ||
|
|
||
| # Ensure required files exist | ||
| for file in "$ALLOWED_SIGNERS_FILE" "$SIG_FILE"; do | ||
| [ ! -f "$file" ] && { echo "Error: File '$file' not found."; exit $FILE_PROBLEM; } | ||
| done | ||
|
|
||
| echo "Verifying the signature against allowed signers..." | ||
|
|
||
| # Iterate through each principal in the allowed signers file | ||
| while IFS= read -r line || [[ -n "$line" ]]; do | ||
| [[ -z "$line" || "$line" == \#* ]] && continue | ||
|
|
||
| # Extract and process each principal | ||
| principals=$(echo "$line" | cut -d' ' -f1) | ||
| IFS=',' read -ra principal_list <<< "$principals" | ||
|
|
||
| for principal in "${principal_list[@]}"; do | ||
| echo "Checking principal: $principal" | ||
| if ssh-keygen -Y verify -f "$ALLOWED_SIGNERS_FILE" -n file -I "$principal" -s "$SIG_FILE" < "$FILE_TO_SIGN"; then | ||
| echo "Signature is valid for principal: $principal" | ||
| exit 0 | ||
| fi | ||
| done | ||
| done < "$ALLOWED_SIGNERS_FILE" | ||
|
|
||
| echo "Error: No valid signature found." | ||
| exit $VALIDATION_FAILED | ||
|
|
||
| else | ||
| usage | ||
| fi | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.