freedombone-splitkey 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. # To get a random fragment
  14. # get a random fragment
  15. # fragment_files=($FRAGMENTS_DIR/*)
  16. # FRAGMENT_FILE="${files[RANDOM % ${#files[@]}]}"
  17. # License
  18. # =======
  19. #
  20. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  21. #
  22. # This program is free software: you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation, either version 3 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. #
  32. # You should have received a copy of the GNU General Public License
  33. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  34. PROJECT_NAME='freedombone'
  35. export TEXTDOMAIN=${PROJECT_NAME}-splitkey
  36. export TEXTDOMAINDIR="/usr/share/locale"
  37. KEY_FRAGMENTS=3
  38. MY_USERNAME=
  39. MY_EMAIL_ADDRESS=
  40. MY_NAME=
  41. function show_help {
  42. echo ''
  43. echo $'freedombone-splitkey -u [username] -n [number of fragments] -e [email address] --fullname [Full name]'
  44. echo ''
  45. exit 0
  46. }
  47. while [[ $# > 1 ]]
  48. do
  49. key="$1"
  50. case $key in
  51. -h|--help)
  52. show_help
  53. ;;
  54. -u|--user)
  55. shift
  56. MY_USERNAME="$1"
  57. ;;
  58. -n|--fragments)
  59. shift
  60. KEY_FRAGMENTS=$1
  61. ;;
  62. -e|--email)
  63. shift
  64. MY_EMAIL_ADDRESS=$1
  65. ;;
  66. --fullname)
  67. shift
  68. MY_NAME=$1
  69. ;;
  70. *)
  71. # unknown option
  72. ;;
  73. esac
  74. shift
  75. done
  76. if [ ! $MY_USERNAME ]; then
  77. show_help
  78. fi
  79. if [ ! -d /home/$MY_USERNAME ]; then
  80. echo $"User $MY_USERNAME does not exist on the system"
  81. exit 7270
  82. fi
  83. if [ ! -d /home/$MY_USERNAME/.gnupg ]; then
  84. echo $'No gpg key found'
  85. exit 5393
  86. fi
  87. FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
  88. if [ -d $FRAGMENTS_DIR ]; then
  89. exit 0
  90. fi
  91. # get the gpg key ID
  92. if [ ! $MY_EMAIL_ADDRESS ]; then
  93. MY_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
  94. fi
  95. KEYID=$(su -c "gpg --list-keys $MY_EMAIL_ADDRESS | grep 'pub '" - \
  96. $MY_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  97. if [ ${#KEYID} -lt 4 ]; then
  98. echo $"gpg key for $MY_EMAIL_ADDRESS was not found"
  99. return 3682
  100. fi
  101. MY_BACKUP_KEY_ID=$(gpg --list-keys "$MY_NAME (backup key)" | \
  102. grep 'pub ' | awk -F ' ' '{print $2}' | \
  103. awk -F '/' '{print $2}')
  104. if [ ${#MY_BACKUP_KEY_ID} -lt 4 ]; then
  105. echo $"gpg backup key for '$MY_NAME' was not found"
  106. return 58213
  107. fi
  108. # create the key file
  109. mkdir -p $FRAGMENTS_DIR
  110. KEYS_FILE=$FRAGMENTS_DIR/keyshare.asc
  111. gpg --output $FRAGMENTS_DIR/pubkey.txt --armor --export $KEYID
  112. if [ ! "$?" = "0" ]; then
  113. echo $"Unable to extract public key for $KEYID"
  114. exit 7835
  115. fi
  116. gpg --output $FRAGMENTS_DIR/privkey.txt \
  117. --armor --export-secret-key $KEYID
  118. if [ ! "$?" = "0" ]; then
  119. echo $"Unable to extract private key for $KEYID"
  120. exit 7823
  121. fi
  122. gpg --output $FRAGMENTS_DIR/backup_pubkey.txt \
  123. --armor --export $MY_BACKUP_KEY_ID
  124. if [ ! "$?" = "0" ]; then
  125. echo $"Unable to extract backup public key for $MY_BACKUP_KEY_ID"
  126. exit 62928
  127. fi
  128. gpg --output $FRAGMENTS_DIR/backup_privkey.txt \
  129. --armor --export-secret-key $MY_BACKUP_KEY_ID
  130. if [ ! "$?" = "0" ]; then
  131. echo $"Unable to extract backup private key for $MY_BACKUP_KEY_ID"
  132. exit 13783
  133. fi
  134. cat $FRAGMENTS_DIR/pubkey.txt \
  135. $FRAGMENTS_DIR/privkey.txt \
  136. $FRAGMENTS_DIR/backup_pubkey.txt \
  137. $FRAGMENTS_DIR/backup_privkey.txt > $KEYS_FILE
  138. shred -zu $FRAGMENTS_DIR/privkey.txt
  139. shred -zu $FRAGMENTS_DIR/pubkey.txt
  140. shred -zu $FRAGMENTS_DIR/backup_privkey.txt
  141. shred -zu $FRAGMENTS_DIR/backup_pubkey.txt
  142. KEY_SHARES=$((KEY_FRAGMENTS * 2))
  143. gfsplit -n $KEY_FRAGMENTS -m $KEY_SHARES $KEYS_FILE
  144. if [ ! "$?" = "0" ]; then
  145. echo $"Unable to split the gpg key"
  146. rm -rf $FRAGMENTS_DIR
  147. if [ -f $KEYS_FILE ]; then
  148. shred -zu $KEYS_FILE
  149. fi
  150. exit 63028
  151. fi
  152. shred -zu $KEYS_FILE
  153. # set permissions
  154. chown -R $MY_USERNAME:$MY_USERNAME $FRAGMENTS_DIR
  155. chmod -R 600 $FRAGMENTS_DIR
  156. echo $"$KEY_SHARES key shares created"
  157. exit 0