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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#! /bin/bash
# Paolo Lulli 2014
CONFIG_DIR=$HOME/.x509crypt
GLOBAL_CONFIG=${CONFIG_DIR}/config
setup_global_config(){
echo "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department"> ${GLOBAL_CONFIG}
echo "Created config file: [${GLOBAL_CONFIG}]"
exit 1
}
test -d $CONFIG_DIR || mkdir -p $CONFIG_DIR
test -f $GLOBAL_CONFIG || setup_global_config
generate_keys(){
echo "GENERATE KEYS"
profile=$1
CONFIG_CERT_NAME=${profile}
organization=$(cat ${GLOBAL_CONFIG})
openssl genrsa -out $CONFIG_DIR/$profile/certs/$CONFIG_CERT_NAME.key 4096
openssl req -batch -new -key $CONFIG_DIR/$profile/certs/$CONFIG_CERT_NAME.key \
-subj "$organization/CN=$profile"\
-out $CONFIG_DIR/$profile/certs/$CONFIG_CERT_NAME.csr
#-subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=example.com"\
#-config $CONFIG_DIR/$profile_name/conf/$profile_name-config
openssl x509 -req -days 365 -in $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.csr \
-signkey $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.key \
-out $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}
export_key(){
profile_name=$1
CONFIG_CERT_NAME=${profile}
cat $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}
dump_key(){
profile_name=$1
CONFIG_CERT_NAME=${profile}
cat $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt | openssl x509 -text
}
setup_profile(){
echo "SETUP PROFILE"
profile_name=$1
test -d $CONFIG_DIR/$profile_name || mkdir -p $CONFIG_DIR/$profile_name
test -d $CONFIG_DIR/$profile_name/conf || mkdir -p $CONFIG_DIR/$profile_name/conf
test -d $CONFIG_DIR/$profile_name/certs || mkdir -p $CONFIG_DIR/$profile_name/certs
}
file_decrypt(){
echo "FILE DECRYPT: $1"
test -d $CONFIG_DIR/$profile_name || echo "profile: [$profile_name] does not exist, EXIT"
test -d $CONFIG_DIR/$profile_name || exit 1
decrypted_file=$1".cleartext"
CONFIG_CERT_NAME=${profile_name}
#openssl smime -decrypt -binary -in $1 -inform DER -out $decrypted_file -inkey $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.key -passin pass:your_password
openssl smime -decrypt -binary -in $1 -inform DER -out $decrypted_file -inkey $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.key
}
file_encrypt(){
echo "FILE ENCRYPT: $1"
file_size=$(du -s $1 | awk -e '{print $1}')
if [ ${file_size} -gt 500000 ]; then
echo "File too big to encrypt with smime"
exit 1
fi
encrypted_file=$1".enc"
CONFIG_CERT_NAME=${profile_name}
openssl smime -encrypt -binary -aes-256-cbc -in $1 -out $encrypted_file -outform DER $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}
file_import(){
echo "FILE IMPORT: $1"
imported_file=$1
CONFIG_CERT_NAME=${profile_name}
test -f $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt && echo "profile: [$profile_name] exists, SKIPPING"
test -f $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt && exit 1
test -d $CONFIG_DIR/$profile_name || mkdir -p $CONFIG_DIR/$profile_name/certs
cp ${imported_file} $CONFIG_DIR/$profile_name/certs/$CONFIG_CERT_NAME.crt
}
list_profiles(){
echo "profiles"
echo "--------"
for i in $(ls $CONFIG_DIR | sed -e s/config//); do echo ${i}; done
}
usage(){
program=$(basename $0)
echo "${program} <profile> -e <file> : Encrypt <file> (safety checks)"
echo "${program} <profile> -E <file> : Encrypt <file>"
echo "${program} <profile> -d <file> : Decrypt <file>"
echo "${program} <profile> -g : Generate keys"
echo "${program} <profile> -x : eXport pub key"
echo "${program} <profile> -X : eXport pub key details"
echo "${program} <profile> -i <file> : Import pub key"
echo "${program} -l : List profiles"
}
if [ "$#" -gt 3 ]; then
usage
exit
fi
if [ "$#" -eq 0 ]; then
usage
exit
fi
if [ "$#" -eq 1 ]; then
if [ "$1" == "-l" ]; then
list_profiles
exit
fi
if [ "$2" == "-x" ]; then
export_key "${profile}"
exit
fi
usage
fi
if [ "$#" -eq 2 ]; then
profile=$1
if [ "$2" == "-g" ]; then
test -d $CONFIG_DIR/$profile && echo "profile: [$profile] exists, SKIPPING"
test -d $CONFIG_DIR/$profile && exit 1
setup_profile "${profile}"
generate_keys "${profile}"
exit
fi
if [ "$2" == "-x" ]; then
export_key "${profile}"
exit
fi
if [ "$2" == "-X" ]; then
dump_key "${profile}"
exit
fi
usage
fi
if [ "$#" -eq 3 ]; then
profile_name=$1
input_file=$3
if [ "$2" == "-E" ]; then
file_encrypt $input_file
exit
fi
if [ "$2" == "-e" ]; then
file_encrypt $input_file
file_decrypt $input_file".enc"
diff $input_file $input_file".enc.cleartext" || echo "ERROR: BAD ENCRYPTION"
rm $input_file".enc.cleartext"
exit
fi
if [ "$2" == "-d" ]; then
file_decrypt $input_file
exit
fi
if [ "$2" == "-i" ]; then
file_import $input_file
exit
fi
usage
fi
|