Browse Source

Consolidate key recovery

Bob Mottram 8 years ago
parent
commit
490f5fca7f
3 changed files with 166 additions and 289 deletions
  1. 0
    135
      src/freedombone-config
  2. 0
    154
      src/freedombone-recoverkey
  3. 166
    0
      src/freedombone-utils-keys

+ 0
- 135
src/freedombone-config View File

@@ -920,141 +920,6 @@ function interactive_gpg_from_remote {
920 920
     return 0
921 921
 }
922 922
 
923
-function reconstruct_key {
924
-    if [ ! -d /home/$MY_USERNAME/.gnupg_fragments ]; then
925
-        return
926
-    fi
927
-    cd /home/$MY_USERNAME/.gnupg_fragments
928
-    no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
929
-    if (( no_of_shares < 4 )); then
930
-        dialog --title $"Recover Encryption Keys" --msgbox $'Not enough fragments to reconstruct the key' 6 70
931
-        exit 7348
932
-    fi
933
-    apt-get -yq install libgfshare-bin gnupg
934
-    gfcombine /home/$MY_USERNAME/.gnupg_fragments/keyshare*
935
-    if [ ! "$?" = "0" ]; then
936
-        dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
937
-        exit 7348
938
-    fi
939
-
940
-    KEYS_FILE=/home/$MY_USERNAME/.gnupg_fragments/keyshare.asc
941
-    if [ ! -f $KEYS_FILE ]; then
942
-        dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
943
-    fi
944
-
945
-    su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME
946
-    if [ ! "$?" = "0" ]; then
947
-        echo $'Unable to import gpg key'
948
-        shred -zu $KEYS_FILE
949
-        rm -rf /home/$MY_USERNAME/.tempgnupg
950
-        exit 9654
951
-    fi
952
-    shred -zu $KEYS_FILE
953
-
954
-    dialog --title $"Recover Encryption Keys" --msgbox $'Key has been reconstructed' 6 70
955
-}
956
-
957
-function interactive_gpg_from_usb {
958
-    dialog --title $"Recover Encryption Keys" \
959
-           --msgbox $'Plug in a USB keydrive containing a copy of your full key or key fragment' 6 70
960
-
961
-    HOME_DIR=/home/$MY_USERNAME
962
-    GPG_LOADING="yes"
963
-    SSH_IMPORTED="no"
964
-    GPG_CTR=0
965
-    while [[ $GPG_LOADING == "yes" ]]
966
-    do
967
-        detect_usb_drive
968
-
969
-        if [ ! -b $USB_DRIVE ]; then
970
-            if (( GPG_CTR > 0 )); then
971
-                reconstruct_key
972
-                return 0
973
-            fi
974
-            dialog --title $"Recover Encryption Keys" --msgbox $'No USB drive found' 6 30
975
-            exit 739836
976
-        fi
977
-
978
-        GPG_USB_MOUNT='/mnt/usb'
979
-        umount -f $GPG_USB_MOUNT
980
-        if [ ! -d $GPG_USB_MOUNT ]; then
981
-            mkdir -p $GPG_USB_MOUNT
982
-        fi
983
-
984
-        if [ -f /dev/mapper/encrypted_usb ]; then
985
-            rm -rf /dev/mapper/encrypted_usb
986
-        fi
987
-        cryptsetup luksClose encrypted_usb
988
-        cryptsetup luksOpen $USB_DRIVE encrypted_usb
989
-        if [ "$?" = "0" ]; then
990
-            USB_DRIVE=/dev/mapper/encrypted_usb
991
-        fi
992
-        mount $USB_DRIVE $GPG_USB_MOUNT
993
-        if [ ! "$?" = "0" ]; then
994
-            if (( GPG_CTR > 0 )); then
995
-                rm -rf $GPG_USB_MOUNT
996
-                reconstruct_key
997
-                return 0
998
-            fi
999
-            dialog --title $"Recover Encryption Keys" \
1000
-                   --msgbox $"There was a problem mounting the USB drive to $GPG_USB_MOUNT" 6 70
1001
-            rm -rf $GPG_USB_MOUNT
1002
-            exit 74393
1003
-        fi
1004
-
1005
-        if [ ! -d $GPG_USB_MOUNT/.gnupg ]; then
1006
-            if [ ! -d $GPG_USB_MOUNT/.gnupg_fragments ]; then
1007
-                if (( GPG_CTR > 0 )); then
1008
-                    umount -f $GPG_USB_MOUNT
1009
-                    rm -rf $GPG_USB_MOUNT
1010
-                    reconstruct_key
1011
-                    return 0
1012
-                fi
1013
-                dialog --title $"Recover Encryption Keys" \
1014
-                       --msgbox $"The directory $GPG_USB_MOUNT/.gnupg or $GPG_USB_MOUNT/.gnupg_fragments was not found" 6 70
1015
-                umount -f $GPG_USB_MOUNT
1016
-                rm -rf $GPG_USB_MOUNT
1017
-                exit 723814
1018
-            fi
1019
-        fi
1020
-
1021
-        if [ -d $GPG_USB_MOUNT/.gnupg ]; then
1022
-            if [ ! -d $HOME_DIR/.gnupg ]; then
1023
-                mkdir $HOME_DIR/.gnupg
1024
-            fi
1025
-            cp -r $GPG_USB_MOUNT/.gnupg/* $HOME_DIR/.gnupg
1026
-            GPG_LOADING="no"
1027
-            dialog --title $"Recover Encryption Keys" \
1028
-                   --msgbox $"GPG Keyring loaded to $HOME_DIR" 6 70
1029
-        else
1030
-            if [ ! -d $HOME_DIR/.gnupg_fragments ]; then
1031
-                mkdir $HOME_DIR/.gnupg_fragments
1032
-            fi
1033
-            cp -r $GPG_USB_MOUNT/.gnupg_fragments/* $HOME_DIR/.gnupg_fragments
1034
-        fi
1035
-
1036
-        if [[ $SSH_IMPORTED == "no" ]]; then
1037
-            if [ -d $GPG_USB_MOUNT/.ssh ]; then
1038
-                if [ ! -d $HOME_DIR/.ssh ]; then
1039
-                    mkdir $HOME_DIR/.ssh
1040
-                fi
1041
-                cp $GPG_USB_MOUNT/.ssh/* $HOME_DIR/.ssh
1042
-                dialog --title $"Recover Encryption Keys" \
1043
-                       --msgbox $"ssh keys imported" 6 70
1044
-                SSH_IMPORTED="yes"
1045
-            fi
1046
-        fi
1047
-
1048
-        umount -f $GPG_USB_MOUNT
1049
-        rm -rf $GPG_USB_MOUNT
1050
-        if [[ $GPG_LOADING == "yes" ]]; then
1051
-            dialog --title $"Recover Encryption Keys" \
1052
-                   --msgbox $"Now remove the USB drive. Insert the next drive containing a key fragment, or select Ok to finish" 6 70
1053
-        fi
1054
-        GPG_CTR=$((GPG_CTR + 1))
1055
-    done
1056
-}
1057
-
1058 923
 function interactive_gpg {
1059 924
     GPG_CONFIGURED="no"
1060 925
     while [[ $GPG_CONFIGURED != "yes" ]]

+ 0
- 154
src/freedombone-recoverkey View File

@@ -108,160 +108,6 @@ fi
108 108
 
109 109
 FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
110 110
 
111
-function reconstruct_key {
112
-    if [ ! -d /home/$MY_USERNAME/.gnupg_fragments ]; then
113
-        return
114
-    fi
115
-    cd /home/$MY_USERNAME/.gnupg_fragments
116
-    no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
117
-    if (( no_of_shares < 4 )); then
118
-        dialog --title $"Encryption keys" --msgbox $'Not enough fragments to reconstruct the key' 6 70
119
-        exit 7348
120
-    fi
121
-    apt-get -yq install libgfshare-bin gnupg
122
-    gfcombine /home/$MY_USERNAME/.gnupg_fragments/keyshare*
123
-    if [ ! "$?" = "0" ]; then
124
-        dialog --title $"Encryption keys" --msgbox $'Unable to reconstruct the key' 6 70
125
-        exit 7348
126
-    fi
127
-
128
-    KEYS_FILE=/home/$MY_USERNAME/.gnupg_fragments/keyshare.asc
129
-    if [ ! -f $KEYS_FILE ]; then
130
-        dialog --title $"Encryption keys" --msgbox $'Unable to reconstruct the key' 6 70
131
-    fi
132
-
133
-    su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME
134
-    if [ ! "$?" = "0" ]; then
135
-        echo $'Unable to import gpg key'
136
-        shred -zu $KEYS_FILE
137
-        rm -rf /home/$MY_USERNAME/.tempgnupg
138
-        exit 9654
139
-    fi
140
-    shred -zu $KEYS_FILE
141
-
142
-    dialog --title $"Encryption keys" --msgbox $'Key has been reconstructed' 6 70
143
-}
144
-
145
-function interactive_gpg_from_usb {
146
-    dialog --title $"Encryption keys" \
147
-           --msgbox $'Plug in a USB drive containing a copy of your full key or key fragment' 6 70
148
-
149
-    HOME_DIR=/home/$MY_USERNAME
150
-    GPG_LOADING="yes"
151
-    SSH_IMPORTED="no"
152
-    GPG_CTR=0
153
-    while [[ $GPG_LOADING == "yes" ]]
154
-    do
155
-        if [ ! -b $GPG_USB_DRIVE ]; then
156
-            GPG_USB_DRIVE='/dev/sdc1'
157
-            if [ ! -b $GPG_USB_DRIVE ]; then
158
-                GPG_USB_DRIVE='/dev/sdd1'
159
-                if [ ! -b $GPG_USB_DRIVE ]; then
160
-                    if (( GPG_CTR > 0 )); then
161
-                        reconstruct_key
162
-                        return 0
163
-                    fi
164
-                    dialog --title $"Encryption keys" --msgbox $'No USB drive found' 6 30
165
-                    exit 27852
166
-                fi
167
-            fi
168
-        fi
169
-
170
-        GPG_USB_MOUNT='/mnt/usb'
171
-        umount -f $GPG_USB_MOUNT
172
-        if [ ! -d $GPG_USB_MOUNT ]; then
173
-            mkdir -p $GPG_USB_MOUNT
174
-        fi
175
-
176
-        if [ -f /dev/mapper/encrypted_usb ]; then
177
-            rm -rf /dev/mapper/encrypted_usb
178
-        fi
179
-        cryptsetup luksClose encrypted_usb
180
-        cryptsetup luksOpen $GPG_USB_DRIVE encrypted_usb
181
-        if [ "$?" = "0" ]; then
182
-            GPG_USB_DRIVE=/dev/mapper/encrypted_usb
183
-        fi
184
-        mount $GPG_USB_DRIVE $GPG_USB_MOUNT
185
-        if [ ! "$?" = "0" ]; then
186
-            if (( GPG_CTR > 0 )); then
187
-                rm -rf $GPG_USB_MOUNT
188
-                reconstruct_key
189
-                return 0
190
-            fi
191
-            dialog --title $"Encryption keys" \
192
-                   --msgbox $"There was a problem mounting the USB drive to $GPG_USB_MOUNT" 6 70
193
-            rm -rf $GPG_USB_MOUNT
194
-            exit 74393
195
-        fi
196
-
197
-        if [ ! -d $GPG_USB_MOUNT/.gnupg ]; then
198
-            if [ ! -d $GPG_USB_MOUNT/.gnupg_fragments ]; then
199
-                if (( GPG_CTR > 0 )); then
200
-                    umount -f $GPG_USB_MOUNT
201
-                    rm -rf $GPG_USB_MOUNT
202
-                    reconstruct_key
203
-                    return 0
204
-                fi
205
-                if [[ "$GPG_USB_DRIVE" == *"sda1" ]]; then
206
-                    GPG_USB_DRIVE=/dev/sdb1
207
-                    write_config_param USB_DRIVE "$GPG_USB_DRIVE"
208
-                    umount -f $GPG_USB_MOUNT
209
-                    rm -rf $GPG_USB_MOUNT
210
-                    ${PROJECT_NAME}-recoverkey -u "$MY_USERNAME" -d sdb
211
-                    exit 0
212
-                else
213
-                    dialog --title $"Encryption keys" \
214
-                           --msgbox $"The directory $GPG_USB_MOUNT/.gnupg or $GPG_USB_MOUNT/.gnupg_fragments was not found" 6 70
215
-                    umount -f $GPG_USB_MOUNT
216
-                    rm -rf $GPG_USB_MOUNT
217
-                    exit 723814
218
-                fi
219
-            fi
220
-        fi
221
-
222
-        if [ -d $GPG_USB_MOUNT/.gnupg ]; then
223
-            if [ ! -d $HOME_DIR/.gnupg ]; then
224
-                mkdir $HOME_DIR/.gnupg
225
-            fi
226
-            if [ -d $GPG_USB_MOUNT/letsencrypt ]; then
227
-                test -r $GPG_USB_MOUNT/letsencrypt
228
-                if [ $? -ne ]; then
229
-                   cp -rf $GPG_USB_MOUNT/letsencrypt /etc
230
-                fi
231
-            fi
232
-            cp -r $GPG_USB_MOUNT/.gnupg/* $HOME_DIR/.gnupg
233
-            GPG_LOADING="no"
234
-            dialog --title $"Encryption keys" \
235
-                   --msgbox $"GPG Keyring loaded to $HOME_DIR" 6 70
236
-        else
237
-            if [ ! -d $HOME_DIR/.gnupg_fragments ]; then
238
-                mkdir $HOME_DIR/.gnupg_fragments
239
-            fi
240
-            cp -r $GPG_USB_MOUNT/.gnupg_fragments/* $HOME_DIR/.gnupg_fragments
241
-        fi
242
-
243
-        if [[ $SSH_IMPORTED == "no" ]]; then
244
-            if [ -d $GPG_USB_MOUNT/.ssh ]; then
245
-                if [ ! -d $HOME_DIR/.ssh ]; then
246
-                    mkdir $HOME_DIR/.ssh
247
-                fi
248
-                cp $GPG_USB_MOUNT/.ssh/* $HOME_DIR/.ssh
249
-                dialog --title $"Encryption keys" \
250
-                       --msgbox $"ssh keys imported" 6 70
251
-                SSH_IMPORTED="yes"
252
-            fi
253
-        fi
254
-
255
-        umount -f $GPG_USB_MOUNT
256
-        rm -rf $GPG_USB_MOUNT
257
-        if [[ $GPG_LOADING == "yes" ]]; then
258
-            dialog --title $"Encryption keys" \
259
-                   --msgbox $"Now remove the USB drive. Insert the next drive containing a key fragment, or select Ok to finish" 6 70
260
-        fi
261
-        GPG_CTR=$((GPG_CTR + 1))
262
-    done
263
-}
264
-
265 111
 # if no remote backup list was given then assume recover from USB
266 112
 if [ ! $FRIENDS_SERVERS_LIST ]; then
267 113
     interactive_gpg_from_usb

+ 166
- 0
src/freedombone-utils-keys View File

@@ -0,0 +1,166 @@
1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# Encryption key related functions
12
+
13
+# License
14
+# =======
15
+#
16
+# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
17
+#
18
+# This program is free software: you can redistribute it and/or modify
19
+# it under the terms of the GNU Affero General Public License as published by
20
+# the Free Software Foundation, either version 3 of the License, or
21
+# (at your option) any later version.
22
+#
23
+# This program is distributed in the hope that it will be useful,
24
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
+# GNU Affero General Public License for more details.
27
+#
28
+# You should have received a copy of the GNU Affero General Public License
29
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
30
+
31
+function reconstruct_key {
32
+    if [ ! -d /home/$MY_USERNAME/.gnupg_fragments ]; then
33
+        return
34
+    fi
35
+    cd /home/$MY_USERNAME/.gnupg_fragments
36
+    no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
37
+    if (( no_of_shares < 4 )); then
38
+        dialog --title $"Recover Encryption Keys" --msgbox $'Not enough fragments to reconstruct the key' 6 70
39
+        exit 7348
40
+    fi
41
+    apt-get -yq install libgfshare-bin gnupg
42
+    gfcombine /home/$MY_USERNAME/.gnupg_fragments/keyshare*
43
+    if [ ! "$?" = "0" ]; then
44
+        dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
45
+        exit 7348
46
+    fi
47
+
48
+    KEYS_FILE=/home/$MY_USERNAME/.gnupg_fragments/keyshare.asc
49
+    if [ ! -f $KEYS_FILE ]; then
50
+        dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
51
+    fi
52
+
53
+    su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME
54
+    if [ ! "$?" = "0" ]; then
55
+        echo $'Unable to import gpg key'
56
+        shred -zu $KEYS_FILE
57
+        rm -rf /home/$MY_USERNAME/.tempgnupg
58
+        exit 9654
59
+    fi
60
+    shred -zu $KEYS_FILE
61
+
62
+    dialog --title $"Recover Encryption Keys" --msgbox $'Key has been reconstructed' 6 70
63
+}
64
+
65
+function interactive_gpg_from_usb {
66
+    dialog --title $"Recover Encryption Keys" \
67
+           --msgbox $'Plug in a USB keydrive containing a copy of your full key or key fragment' 6 70
68
+
69
+    HOME_DIR=/home/$MY_USERNAME
70
+    GPG_LOADING="yes"
71
+    SSH_IMPORTED="no"
72
+    GPG_CTR=0
73
+    while [[ $GPG_LOADING == "yes" ]]
74
+    do
75
+        detect_usb_drive
76
+
77
+        if [ ! -b $USB_DRIVE ]; then
78
+            if (( GPG_CTR > 0 )); then
79
+                reconstruct_key
80
+                return 0
81
+            fi
82
+            dialog --title $"Recover Encryption Keys" --msgbox $'No USB drive found' 6 30
83
+            exit 739836
84
+        fi
85
+
86
+        GPG_USB_MOUNT='/mnt/usb'
87
+        umount -f $GPG_USB_MOUNT
88
+        if [ ! -d $GPG_USB_MOUNT ]; then
89
+            mkdir -p $GPG_USB_MOUNT
90
+        fi
91
+
92
+        if [ -f /dev/mapper/encrypted_usb ]; then
93
+            rm -rf /dev/mapper/encrypted_usb
94
+        fi
95
+        cryptsetup luksClose encrypted_usb
96
+        cryptsetup luksOpen $USB_DRIVE encrypted_usb
97
+        if [ "$?" = "0" ]; then
98
+            USB_DRIVE=/dev/mapper/encrypted_usb
99
+        fi
100
+        mount $USB_DRIVE $GPG_USB_MOUNT
101
+        if [ ! "$?" = "0" ]; then
102
+            if (( GPG_CTR > 0 )); then
103
+                rm -rf $GPG_USB_MOUNT
104
+                reconstruct_key
105
+                return 0
106
+            fi
107
+            dialog --title $"Recover Encryption Keys" \
108
+                   --msgbox $"There was a problem mounting the USB drive to $GPG_USB_MOUNT" 6 70
109
+            rm -rf $GPG_USB_MOUNT
110
+            exit 74393
111
+        fi
112
+
113
+        if [ ! -d $GPG_USB_MOUNT/.gnupg ]; then
114
+            if [ ! -d $GPG_USB_MOUNT/.gnupg_fragments ]; then
115
+                if (( GPG_CTR > 0 )); then
116
+                    umount -f $GPG_USB_MOUNT
117
+                    rm -rf $GPG_USB_MOUNT
118
+                    reconstruct_key
119
+                    return 0
120
+                fi
121
+                dialog --title $"Recover Encryption Keys" \
122
+                       --msgbox $"The directory $GPG_USB_MOUNT/.gnupg or $GPG_USB_MOUNT/.gnupg_fragments was not found" 6 70
123
+                umount -f $GPG_USB_MOUNT
124
+                rm -rf $GPG_USB_MOUNT
125
+                exit 723814
126
+            fi
127
+        fi
128
+
129
+        if [ -d $GPG_USB_MOUNT/.gnupg ]; then
130
+            if [ ! -d $HOME_DIR/.gnupg ]; then
131
+                mkdir $HOME_DIR/.gnupg
132
+            fi
133
+            cp -r $GPG_USB_MOUNT/.gnupg/* $HOME_DIR/.gnupg
134
+            GPG_LOADING="no"
135
+            dialog --title $"Recover Encryption Keys" \
136
+                   --msgbox $"GPG Keyring loaded to $HOME_DIR" 6 70
137
+        else
138
+            if [ ! -d $HOME_DIR/.gnupg_fragments ]; then
139
+                mkdir $HOME_DIR/.gnupg_fragments
140
+            fi
141
+            cp -r $GPG_USB_MOUNT/.gnupg_fragments/* $HOME_DIR/.gnupg_fragments
142
+        fi
143
+
144
+        if [[ $SSH_IMPORTED == "no" ]]; then
145
+            if [ -d $GPG_USB_MOUNT/.ssh ]; then
146
+                if [ ! -d $HOME_DIR/.ssh ]; then
147
+                    mkdir $HOME_DIR/.ssh
148
+                fi
149
+                cp $GPG_USB_MOUNT/.ssh/* $HOME_DIR/.ssh
150
+                dialog --title $"Recover Encryption Keys" \
151
+                       --msgbox $"ssh keys imported" 6 70
152
+                SSH_IMPORTED="yes"
153
+            fi
154
+        fi
155
+
156
+        umount -f $GPG_USB_MOUNT
157
+        rm -rf $GPG_USB_MOUNT
158
+        if [[ $GPG_LOADING == "yes" ]]; then
159
+            dialog --title $"Recover Encryption Keys" \
160
+                   --msgbox $"Now remove the USB drive. Insert the next drive containing a key fragment, or select Ok to finish" 6 70
161
+        fi
162
+        GPG_CTR=$((GPG_CTR + 1))
163
+    done
164
+}
165
+
166
+# NOTE: deliberately there is no "exit 0"