|
@@ -0,0 +1,139 @@
|
|
1
|
+#!/bin/bash
|
|
2
|
+#
|
|
3
|
+# .---. . .
|
|
4
|
+# | | |
|
|
5
|
+# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
6
|
+# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
7
|
+# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
8
|
+#
|
|
9
|
+# Freedom in the Cloud
|
|
10
|
+#
|
|
11
|
+# A script which splits a user's gpg key into fragments which
|
|
12
|
+# may then be shared
|
|
13
|
+
|
|
14
|
+# To get a random fragment
|
|
15
|
+# get a random fragment
|
|
16
|
+# fragment_files=($FRAGMENTS_DIR/*)
|
|
17
|
+# FRAGMENT_FILE="${files[RANDOM % ${#files[@]}]}"
|
|
18
|
+
|
|
19
|
+# License
|
|
20
|
+# =======
|
|
21
|
+#
|
|
22
|
+# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
|
|
23
|
+#
|
|
24
|
+# This program is free software: you can redistribute it and/or modify
|
|
25
|
+# it under the terms of the GNU General Public License as published by
|
|
26
|
+# the Free Software Foundation, either version 3 of the License, or
|
|
27
|
+# (at your option) any later version.
|
|
28
|
+#
|
|
29
|
+# This program is distributed in the hope that it will be useful,
|
|
30
|
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
31
|
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
32
|
+# GNU General Public License for more details.
|
|
33
|
+#
|
|
34
|
+# You should have received a copy of the GNU General Public License
|
|
35
|
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
36
|
+
|
|
37
|
+KEY_FRAGMENTS=3
|
|
38
|
+MY_USERNAME=
|
|
39
|
+MY_EMAIL_ADDRESS=
|
|
40
|
+
|
|
41
|
+function show_help {
|
|
42
|
+ echo ''
|
|
43
|
+ echo 'freedombone-splitkey -u [username] -n [number of fragments] -e [email address]'
|
|
44
|
+ echo ''
|
|
45
|
+ exit 0
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+while [[ $# > 1 ]]
|
|
49
|
+do
|
|
50
|
+key="$1"
|
|
51
|
+
|
|
52
|
+case $key in
|
|
53
|
+ -h|--help)
|
|
54
|
+ show_help
|
|
55
|
+ ;;
|
|
56
|
+ -u|--user)
|
|
57
|
+ shift
|
|
58
|
+ MY_USERNAME="$1"
|
|
59
|
+ ;;
|
|
60
|
+ -n|--fragments)
|
|
61
|
+ shift
|
|
62
|
+ KEY_FRAGMENTS=$1
|
|
63
|
+ ;;
|
|
64
|
+ -e|--email)
|
|
65
|
+ shift
|
|
66
|
+ MY_EMAIL_ADDRESS=$1
|
|
67
|
+ ;;
|
|
68
|
+ *)
|
|
69
|
+ # unknown option
|
|
70
|
+ ;;
|
|
71
|
+esac
|
|
72
|
+shift
|
|
73
|
+done
|
|
74
|
+
|
|
75
|
+if [ ! $MY_USERNAME ]; then
|
|
76
|
+ show_help
|
|
77
|
+fi
|
|
78
|
+if [ ! -d /home/$MY_USERNAME ]; then
|
|
79
|
+ echo "User $MY_USERNAME does not exist on the system"
|
|
80
|
+ exit 7270
|
|
81
|
+fi
|
|
82
|
+
|
|
83
|
+if [ ! -d /home/$MY_USERNAME/.gnupg ]; then
|
|
84
|
+ echo 'No gpg key found'
|
|
85
|
+ exit 5393
|
|
86
|
+fi
|
|
87
|
+
|
|
88
|
+FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
|
|
89
|
+if [ -d $FRAGMENTS_DIR ]; then
|
|
90
|
+ exit 0
|
|
91
|
+fi
|
|
92
|
+
|
|
93
|
+# get the gpg key ID
|
|
94
|
+if [ ! $MY_EMAIL_ADDRESS ]; then
|
|
95
|
+ MY_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
|
|
96
|
+fi
|
|
97
|
+KEYID=$(su -c "gpg --list-keys $MY_EMAIL_ADDRESS | grep 'pub '" - \
|
|
98
|
+ $MY_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
|
|
99
|
+
|
|
100
|
+# create the key file
|
|
101
|
+KEYS_FILE=/home/$MY_USERNAME/tempdatafile.asc
|
|
102
|
+gpg --output /home/$MY_USERNAME/pubkey.txt --armor --export $KEYID
|
|
103
|
+if [ ! "$?" = "0" ]; then
|
|
104
|
+ echo "Unable to extract public key for $KEYID"
|
|
105
|
+ exit 7835
|
|
106
|
+fi
|
|
107
|
+gpg --output /home/$MY_USERNAME/privkey.txt --armor --export-secret-key $KEYID
|
|
108
|
+if [ ! "$?" = "0" ]; then
|
|
109
|
+ echo "Unable to extract private key for $KEYID"
|
|
110
|
+ exit 7823
|
|
111
|
+fi
|
|
112
|
+cat /home/$MY_USERNAME/pubkey.txt /home/$MY_USERNAME/privkey.txt > $KEYS_FILE
|
|
113
|
+shred -zu /home/$MY_USERNAME/privkey.txt
|
|
114
|
+shred -zu /home/$MY_USERNAME/pubkey.txt
|
|
115
|
+
|
|
116
|
+# encrypt the keys file with a passphrase
|
|
117
|
+gpg --output $KEYS_FILE.gpg --symmetric $KEYS_FILE
|
|
118
|
+if [ ! "$?" = "0" ]; then
|
|
119
|
+ echo "Unable to encrypt the data prior to splitting"
|
|
120
|
+ exit 7352
|
|
121
|
+fi
|
|
122
|
+shred -zu $KEYS_FILE
|
|
123
|
+
|
|
124
|
+# generate fragments
|
|
125
|
+GPG_KEYS_SIZE_BYTES=$(wc -c <"$KEYS_FILE.gpg")
|
|
126
|
+GPG_BYTES_PER_FRAGMENT=$((GPG_KEYS_SIZE_BYTES / KEY_FRAGMENTS))
|
|
127
|
+GPG_BYTES_PER_FRAGMENT=$((GPG_BYTES_PER_FRAGMENT + 1))
|
|
128
|
+mkdir -p $FRAGMENTS_DIR
|
|
129
|
+echo "$GPG_BYTES_PER_FRAGMENT / $GPG_KEYS_SIZE_BYTES"
|
|
130
|
+split --bytes=$GPG_BYTES_PER_FRAGMENT $KEYS_FILE.gpg $FRAGMENTS_DIR/data
|
|
131
|
+#chown -R $MY_USERNAME:$MY_USERNAME $FRAGMENTS_DIR
|
|
132
|
+#chmod -R 600 $FRAGMENTS_DIR
|
|
133
|
+
|
|
134
|
+# delete the keys file
|
|
135
|
+shred -zu $KEYS_FILE.gpg
|
|
136
|
+
|
|
137
|
+echo "$KEY_FRAGMENTS key fragments created"
|
|
138
|
+
|
|
139
|
+exit 0
|