Skip to content

Commit 5828f2c

Browse files
authored
Update encrypt.sh
1 parent 330c820 commit 5828f2c

File tree

1 file changed

+86
-20
lines changed

1 file changed

+86
-20
lines changed

FreeRadius/encrypt.sh

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,95 @@
11
#!/bin/bash
22

3-
# Generate and save a key for encryption (run this once)
3+
# Generate and save a key for encryption, this should be a variable in CI/CD (run this once)
44
# openssl rand -base64 32 > encryption.key
55

6-
# Load the encryption key
7-
ENCRYPTION_KEY=$(cat encryption.key)
6+
#Color
7+
RED='\033[1;31m'
8+
NOCOLOR='\033[0m'
89

9-
# Read the clients.conf file line by line
10-
while IFS= read -r line || [[ -n "$line" ]]; do
11-
if [[ "$line" =~ ^([[:space:]]*secret[[:space:]]*=[[:space:]]*)(ENC:.*) ]]; then
12-
# If the secret is already encrypted, keep it as is
13-
echo "$line"
14-
elif [[ "$line" =~ ^([[:space:]]*secret[[:space:]]*=[[:space:]]*)(.*) ]]; then
15-
PREFIX="${BASH_REMATCH[1]}" # Preserve spacing
16-
SECRET="${BASH_REMATCH[2]}" # Extract secret value
10+
# Check if ENCRYPTION_KEY variable is set
11+
if [ -z "$ENCRYPTION_KEY" ]; then
12+
echo -e ${RED}""ERROR:${NOCOLOR}" ENCRYPTION_KEY variable is not set" >&2
13+
exit 1
14+
fi
1715

18-
# Encrypt and encode in base64
19-
ENCRYPTED_SECRET=$(echo -n "$SECRET" | openssl enc -aes-256-cbc -salt -pbkdf2 -md sha256 -pass pass:"$ENCRYPTION_KEY" | base64 -w 0)
16+
# Prechecks
17+
echo "Checking Base64 Encoded variable"
18+
# Remove spaces and check if it's a proper 44-character base64 string
19+
clean_key=$(echo -n "$ENCRYPTION_KEY" | tr -d '[:space:]')
2020

21-
echo "${PREFIX}ENC:$ENCRYPTED_SECRET"
22-
else
23-
echo "$line"
21+
if [[ ${#clean_key} -ne 44 ]]; then
22+
echo -e ${RED}""ERROR:${NOCOLOR}" Invalid key length. Expected 44 characters."
23+
exit 1
24+
fi
25+
26+
# Base64 validation using regex (must be base64 with 0, 1, or 2 '=' padding)
27+
if ! [[ "$clean_key" =~ ^[A-Za-z0-9+/]*={0,2}$ ]]; then
28+
echo -e ${RED}""ERROR:${NOCOLOR}" Invalid Base64 format."
29+
exit 1
30+
fi
31+
32+
# Try decoding to validate Base64
33+
if ! echo -n "$clean_key" | base64 -d >/dev/null 2>&1; then
34+
echo -e ${RED}""ERROR:${NOCOLOR}" Invalid Base64. Decoding failed."
35+
exit 1
36+
fi
37+
38+
echo "✅ Base64 is valid."
39+
ENCRYPTION_KEY="$clean_key"
40+
41+
echo "Comparing SHA256 Hashes to see if our encryption key is what it should be"
42+
# SHA256 hash of the key to verify its the correct one
43+
stored_sha256=yourhashofthekeyshouldbehere
44+
45+
# Generate SHA-256 Hash of the key
46+
current_sha256=$(echo -n "$ENCRYPTION_KEY" | sha256sum | awk '{print $1}')
47+
48+
# Output Hashed vaules
49+
echo Generated Hash: $current_sha256
50+
echo Stored Hash: $stored_sha256
51+
52+
if [[ "$current_sha256" == "$stored_sha256" ]]; then
53+
echo "✅ Key matched the stored SHA-256 hash. Integrity verified."
54+
else
55+
echo -e ${RED}""ERROR:${NOCOLOR}" Key does not match the stored SHA-256 hash...Exiting.."
56+
exit 1
57+
fi
58+
59+
# List of specific files to encrypt (Modify as needed)
60+
FILES_TO_ENCRYPT=(
61+
"clients.conf"
62+
"mods-available/ldap"
63+
)
64+
65+
# Loop through the specified files
66+
for file in "${FILES_TO_ENCRYPT[@]}"; do
67+
if [ ! -f "$file" ]; then
68+
echo "⚠️ Skipping $file (File not found)"
69+
continue
2470
fi
25-
done < clients.conf > clients.conf.enc
2671

27-
# Replace the original file with the encrypted file
28-
mv clients.conf.enc clients.conf
29-
echo "Encryption complete."
72+
echo "🔐 Encrypting $file..."
73+
74+
while IFS= read -r line || [[ -n "$line" ]]; do
75+
# Match both 'secret' and 'password' fields.
76+
if [[ "$line" =~ ^([[:space:]]*(secret|password)[[:space:]]*=[[:space:]]*)ENC:(.*) ]]; then
77+
# If the secret is already encrypted, keep it as is.
78+
printf "%s\n" "$line"
79+
elif [[ "$line" =~ ^([[:space:]]*(secret|password)[[:space:]]*=[[:space:]]*)(.*) ]]; then
80+
PREFIX="${BASH_REMATCH[1]}" # Preserve spacing
81+
ENCODED_SECRET="${BASH_REMATCH[3]}" # Extract secret/password vaule
82+
# Encrypt and encode in base64
83+
ENCRYPTED_SECRET=$(printf "%s" "$ENCODED_SECRET" | openssl enc -aes-256-cbc -salt -pbkdf2 -md sha256 -pass pass:"$ENCRYPTION_KEY" | openssl base64 | tr -d '\n')
84+
printf "%s\n" "${PREFIX}ENC:$ENCRYPTED_SECRET"
85+
else
86+
printf "%s\n" "$line"
87+
fi
88+
done < "$file" > "${file}.dec"
89+
90+
# Move the decrypted file back to its original location
91+
mv "${file}.dec" "$file"
92+
echo "✅ Encryption complete for $file"
93+
done
94+
95+
echo "✅ All files have been encrypted."

0 commit comments

Comments
 (0)