-
Notifications
You must be signed in to change notification settings - Fork 2
/
x509crypt
executable file
·159 lines (135 loc) · 4.28 KB
/
x509crypt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# (c) 2020 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/x509crypt
#
#
# use x509 public/private certificates for file encryption
PROG="${0##*/}"
trap cleanup SIGINT SIGTERM SIGKILL SIGQUIT SIGABRT SIGSTOP SIGSEGV
cleanup() {
for file in "${PUBKEY}" "${SECRET}" "${ENC_SECRET}" "${TMP}"
do
[[ -n "${file}" ]] && rm -f "${file}"
done
exit
}
die_tampered() {
echo "File does not pass internal checking"
echo "Either tampering or transport failure has occured"
echo "Aborting"
cleanup
}
if [ -z "${1}" ]
then
echo "no action to take"
echo "${PROG} [-d] [infile] [keyfile]"
echo ""
echo " -d decrypt"
cleanup
fi
if [ "-d" = "${1}" ]
then
DECRYPT=1
shift
fi
if [ -z "${PROG##*decrypt*}" ]
then
DECRYPT=1
fi
if [ -z "${1}" ]
then
echo "no file to take action on"
cleanup
else
INFILE=${1}
shift
fi
if [ ! -r "${INFILE}" ]
then
echo "Can't read from >${INFILE}<, aborting"
cleanup
fi
if [ ${DECRYPT:-0} -eq 1 ]
then
if [ -z "${1}" ]
then
echo "no local receiver private-key specified"
cleanup
else
PRIVKEY=${1}
fi
if [ -n "$(grep 'BEGIN.*PRIVATE' "${PRIVKEY}" )" ]
then
SIZE=$(stat -c"%s" "${INFILE}")
# 576 bytes is the size of an encrypted null-length file
[[ ${SIZE} -lt 576 ]] && die_tampered
HMAC_START=$(( $SIZE - 32 ))
ENC_SECRET=$(mktemp /tmp/encsecret-XXXXXX.b64)
SECRET=$(mktemp /tmp/secret-XXXXXX.b64)
TMP=$(mktemp /tmp/temp-XXXXXX.b64)
dd if="${INFILE}" bs=512 count=1 of="${ENC_SECRET}" >/dev/null 2>&1
dd if="${INFILE}" bs=512 skip=1 of="${TMP}" >/dev/null 2>&1
truncate --size=$(($HMAC_START - 512 )) "${TMP}"
PHMAC=$( dd if="${INFILE}" bs=1 count=32 skip="${HMAC_START}" 2>/dev/null | hexdump -v -e '/1 "%02x"' )
OUTFILE=${INFILE//.enc}
[[ -r "${OUTFILE}" ]] && OUTFILE="${OUTFILE}.plain"
# decrypt the random secret using the private key
openssl rsautl -decrypt -inkey "${PRIVKEY}" -in "${ENC_SECRET}" -out "${SECRET}" >/dev/null 2>&1
[[ $? -ne 0 ]] && die_tampered
# decrypt the datafile using the decrypted secret
openssl enc -d -aes-256-cbc -in "${TMP}" -out "${OUTFILE}" -pass file:"${SECRET}" >/dev/null 2>&1
[[ $? -ne 0 ]] && die_tampered
# generate the HMAC from the recovered datafile
HMAC_KEY=$(base64 -d "${SECRET}" | hexdump -v -e '/1 "%02x"' -n 32 -s 0)
RHMAC=$(openssl dgst -hmac "${HMAC_KEY}" -binary -sha256 "${OUTFILE}" | hexdump -v -e '/1 "%02x"')
[[ "${RHMAC}" != "${PHMAC}" ]] && die_tampered
# else
echo "${OUTFILE} validates correctly"
else
echo "Can't find a private cert to decrypt ${INFILE}"
fi
ls -alF "${OUTFILE}"
else
if [ -z "${1}" ]
then
echo "no remote recipient public-key or certificate specified"
cleanup
else
UPUBKEY=${1}
fi
PUBKEY=$(mktemp /tmp/pubkey-XXXXXX.pem)
if [ -n "$(grep 'BEGIN PUBLIC KEY' "${UPUBKEY}")" ]
then
# we've got a public key, so it just works
cp "${UPUBKEY}" "${PUBKEY}"
elif [ -n "$(grep 'BEGIN CERTIFICATE' "${UPUBKEY}")" ]
then
openssl x509 -pubkey -noout -in "${UPUBKEY}" > "${PUBKEY}"
elif [ -n "$(grep 'BEGIN PRIVATE' "${UPUBKEY}")" ]
then
#it's a private cert, so extract the public key from it
openssl rsa -in "${UPUBKEY}" -out "${PUBKEY}" -outform PEM -pubout
else
echo "Can't identify your certificate/key, bailing out"
cleanup
fi
# Generate an encrypted single file of format:
# [0-511] encrypted secret
# [512 - DATAn] encrypted datafile
# [EOF-32 - EOF] HMAC digest
SECRET=$(mktemp /tmp/secret-XXXXXX.b64)
# Generate 128-byte (1024-bit) random secret
openssl rand -base64 -out "${SECRET}" 128
# 64 hex character key for the digest
HMAC_KEY=$(base64 -d "${SECRET}" | hexdump -v -e '/1 "%02x"' -n 32 -s 0)
# encrypt the random secret with the public key - turns into 512 bytes
openssl rsautl -encrypt -inkey "${PUBKEY}" -pubin -in "${SECRET}" > "${INFILE}.enc"
# encrypt the datafile using the random secret as the symmetrical key
openssl enc -aes-256-cbc -salt -in "${INFILE}" -pass file:"${SECRET}" >> "${INFILE}.enc"
# append the HMAC of the original binary for comparison, encrypted of course with the random secret
openssl dgst -hmac "${HMAC_KEY}" -binary -sha256 "${INFILE}" >> "${INFILE}.enc"
ls -alF "${INFILE}" "${INFILE}.enc"
fi
cleanup