chiark / gitweb /
keys.list-recov: Remove spurious `.recov' suffix from listed secrets.
[distorted-keys] / keys.reveal
1 #! /bin/sh
2 ###
3 ### Reveal shares of a secret distributed among keepers
4 ###
5 ### (c) 2011 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the distorted.org.uk key management suite.
11 ###
12 ### distorted-keys is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### distorted-keys is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with distorted-keys; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 set -e
27 case "${KEYSLIB+t}" in t) ;; *) echo >&2 "$0: KEYSLIB unset"; exit 1 ;; esac
28 . "$KEYSLIB"/keyfunc.sh
29
30 defhelp <<HELP
31 [-i INST] RECOV KEEPER
32 Reveal a share of a recovery key distributed among keepers.
33
34 If enough shares have been revealed, reconstruct the recovery private key.
35 The keeper nub is read from stdin.
36 HELP
37
38 ## Parse the command line.
39 inst=current
40 while getopts "i:" opt; do
41   case "$opt" in
42     i) inst=$OPTARG ;;
43     *) usage_err ;;
44   esac
45 done
46 shift $(( $OPTIND - 1 ))
47 case $# in 2) ;; *) usage_err ;; esac
48 recov=$1 keeper=$2; shift 2
49 checkword "recovery instance" "$inst"
50 checkword "recovery key" "$recov"
51 checkword "keeper set label" "$keeper"
52
53 ## Check that this is a sensible thing to do.
54 kdir=$KEYS/keeper/$keeper
55 if [ ! -f $kdir/meta ]; then
56   echo >&2 "$quis: unknown keeper set \`$keeper'"
57   exit 1
58 fi
59 if [ ! -l $KEYS/recov/$recov/current ]; then
60   echo >&2 "$quis: unknown recovery key \`$recov'"
61   exit 1
62 fi
63 rdir=$KEYS/recov/$recov/$inst
64 if [ ! -f $rdir/$keeper.param ]; then
65   echo >&2 "$quis: recovery key \`$recov' not kept by keeper set \`$keeper'"
66   exit 1
67 fi
68
69 ## Grab the key, because we'll need to read it several times.
70 mktmp
71 cat >$tmp/secret
72
73 ## Read the threshold from the recovery metadata.
74 t=$(sharethresh $rdir/$keeper.param)
75
76 ## Find out which keeper index it corresponds to.
77 read n hunoz <$kdir/meta
78 i=0
79 foundp=nil
80 while [ $i -lt $n ]; do
81   c_sysprepare $kdir/$i
82   nubbin=$(nubid <$tmp/secret)
83   nubid=$(cat $kdir/$i/nubid)
84   case "$nubbin" in "$nubid") foundp=t; break ;; esac
85   i=$(( $i + 1 ))
86 done
87 case $foundp in
88   nil) echo >&2 "$quis: nub doesn't match keeper \`$keeper'"; exit 1 ;;
89 esac
90
91 ## Establish the recovery staging area.  See whether we've done enough
92 ## already.
93 reqsafe
94 tag=$recov.$inst
95 mkdir -p -m700 $SAFE/keys.reveal
96 reveal=$SAFE/keys.reveal/$tag
97 if [ ! -d $reveal ]; then mkdir -m700 $reveal; fi
98 cd $reveal
99 if [ -f nub ]; then
100   echo >&2 "$quis: recovery key \`$recov/$inst' already revealed"
101   exit 1
102 fi
103
104 ## Decrypt the share.
105 umask 077
106 if [ -f $keeper.$i.share ]; then
107   echo >&2 "$quis: share $i already revealed"
108 else
109   c_sysdecrypt $kdir/$i $tmp/secret \
110     <$rdir/$keeper.$i.share \
111     >$keeper.$i.new
112   mv $keeper.$i.new $keeper.$i.share
113 fi
114
115 ## See if there's enough for a recovery.
116 n=0
117 for j in $keeper.*.share; do if [ -f "$j" ]; then n=$(( $n + 1 )); fi; done
118 if [ $n -lt $t ]; then
119   echo >&2 "$quis: share $i revealed; $(( $t - $n )) more required"
120 else
121   cat $rdir/$keeper.param $keeper.*.share >$keeper.shares
122   $bindir/shamir recover <$keeper.shares >nub.new
123   c_sysprepare $rdir/store
124   nubbin=$(nubid <nub.new)
125   nubid=$(cat $rdir/store/nubid)
126   case "$nubbin" in
127     "$nubid") ;;
128     *)
129       echo >&2 "$quis: recovered nub doesn't match stored hash"
130       exit 1
131       ;;
132   esac
133   mv nub.new nub
134   rm -f $keeper.*
135   echo >&2 "$quis: recovery key \`$recov/$inst' revealed"
136 fi
137
138 ###----- That's all, folks --------------------------------------------------