chiark / gitweb /
keys.reveal: Rename variables to avoid them being clobbered.
[distorted-keys] / keys.reveal
... / ...
CommitLineData
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
26set -e
27case "${KEYSLIB+t}" in t) ;; *) echo >&2 "$0: KEYSLIB unset"; exit 1 ;; esac
28. "$KEYSLIB"/keyfunc.sh
29
30defhelp <<HELP
31[-i INST] RECOV KEEPER
32Reveal a share of a recovery key distributed among keepers.
33
34If enough shares have been revealed, reconstruct the recovery private key.
35The keeper nub is read from stdin.
36HELP
37
38## Parse the command line.
39inst=current
40while getopts "i:" opt; do
41 case "$opt" in
42 i) inst=$OPTARG ;;
43 *) usage_err ;;
44 esac
45done
46shift $(( $OPTIND - 1 ))
47case $# in 2) ;; *) usage_err ;; esac
48recov=$1 keeper=$2; shift 2
49checkword "recovery instance" "$inst"
50checkword "recovery key" "$recov"
51checkword "keeper set label" "$keeper"
52
53## Check that this is a sensible thing to do.
54keepdir=$KEYS/keeper/$keeper
55if [ ! -f $keepdir/meta ]; then
56 echo >&2 "$quis: unknown keeper set \`$keeper'"
57 exit 1
58fi
59if [ ! -h $KEYS/recov/$recov/current ]; then
60 echo >&2 "$quis: unknown recovery key \`$recov'"
61 exit 1
62fi
63rdir=$KEYS/recov/$recov/$inst
64if [ ! -f $rdir/$keeper.param ]; then
65 echo >&2 "$quis: recovery key \`$recov' not kept by keeper set \`$keeper'"
66 exit 1
67fi
68
69## Grab the key, because we'll need to read it several times.
70mktmp
71cat >$tmp/secret
72
73## Read the threshold from the recovery metadata.
74t=$(sharethresh $rdir/$keeper.param)
75
76## Find out which keeper index it corresponds to.
77read n hunoz <$keepdir/meta
78i=0
79foundnubp=nil
80while [ $i -lt $n ]; do
81 c_sysprepare $keepdir/$i
82 nubbin=$(nubid <$tmp/secret)
83 nubid=$(cat $keepdir/$i/nubid)
84 case "$nubbin" in "$nubid") foundnubp=t; break ;; esac
85 i=$(( $i + 1 ))
86done
87case $foundnubp in
88 nil) echo >&2 "$quis: nub doesn't match keeper \`$keeper'"; exit 1 ;;
89esac
90
91## Establish the recovery staging area. See whether we've done enough
92## already.
93reqsafe
94tag=$recov.$inst
95mkdir -p -m700 $SAFE/keys.reveal
96reveal=$SAFE/keys.reveal/$tag
97if [ ! -d $reveal ]; then mkdir -m700 $reveal; fi
98cd $reveal
99if [ -f nub ]; then
100 echo >&2 "$quis: recovery key \`$recov/$inst' already revealed"
101 exit 1
102fi
103
104## Decrypt the share.
105umask 077
106if [ -f $keeper.$i.share ]; then
107 echo >&2 "$quis: share $i already revealed"
108else
109 c_sysdecrypt $keepdir/$i $tmp/secret \
110 <$rdir/$keeper.$i.share \
111 >$keeper.$i.new
112 mv $keeper.$i.new $keeper.$i.share
113fi
114
115## See if there's enough for a recovery.
116n=0
117for j in $keeper.*.share; do if [ -f "$j" ]; then n=$(( $n + 1 )); fi; done
118if [ $n -lt $t ]; then
119 echo >&2 "$quis: share $i revealed; $(( $t - $n )) more required"
120else
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"
136fi
137
138###----- That's all, folks --------------------------------------------------