chiark / gitweb /
more progress. recovery seems to be working now.
[distorted-keys] / reveal
CommitLineData
53263601
MW
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###
599c8f75
MW
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
53263601
MW
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###
599c8f75 17### distorted-keys is distributed in the hope that it will be useful,
53263601
MW
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
599c8f75 23### along with distorted-keys; if not, write to the Free Software Foundation,
53263601
MW
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
31RECOV KEEPER [KEY]
32Reveal a share of a recovery key distributed among keepers.
33
34If enough shares have been revealed, reconstruct the recovery private key.
35The key is read from KEY, or stdin if KEY is omitted or \`-'.
36HELP
37dohelp
38
39## Parse the command line.
40case $# in
41 2) if [ -t 0 ]; then echo >&2 "$quis: stdin is a terminal"; exit 1; fi ;;
42 3) ;;
43 *) echo >&2 "$usage"; exit 1 ;;
44esac
45recov=$1 keeper=$2; shift 2
599c8f75 46checklabel "recovery key" "$recov"
53263601 47case "$recov" in
599c8f75
MW
48 */*) ;;
49 *) recov=$recov/current ;;
53263601
MW
50esac
51checkword "keeper set label" "$keeper"
52
53## Grab the key, because we'll need to read it several times.
54tmp=$(mktmp); cleanup rmtmp
599c8f75 55secret=$(cat -- "$@")
53263601
MW
56pub=$(ec_public /dev/stdin <<EOF
57$secret
58EOF
59)
60
61## Read the threshold from the recovery metadata.
62read param <$KEYS/recov/$recov/$keeper.param
63case "$param" in
64 shamir-params:*) ;;
65 *)
66 echo >&2 "$quis: secret sharing parameter file damaged (wrong header)"
67 exit 1
68 ;;
69esac
70t=";${param#*:}"
71case "$t" in
72 *";t="*) ;;
73 *)
74 echo >&2 "$quis: secret sharing parameter file damaged (missing t)"
75 exit 1
76 ;;
77esac
78t=${t#*;t=}
79t=${t%%;*}
80
81## Find out which keeper index it corresponds to.
82read n hunoz <$KEYS/keeper/$keeper/meta
83i=0
84foundp=nil
599c8f75 85: "$pub"
53263601
MW
86while [ $i -lt $n ]; do
87 read cand <$KEYS/keeper/$keeper/$i.pub
599c8f75 88 : "$cand"
53263601
MW
89 case "$pub" in "$cand") foundp=t; break ;; esac
90 i=$(( i + 1 ))
91done
92case $foundp in
93 nil) echo >&2 "$quis: key doesn't match keeper \`$keeper'"; exit 1 ;;
94esac
95
96## Establish the recovery staging area. See whether we've done enough
97## already.
599c8f75 98mem=$(userv root claim-mem-dir </dev/null)
53263601 99tag=$(echo $recov | tr / .)
599c8f75
MW
100mkdir -p -m700 $mem/keys.reveal
101reveal=$mem/keys.reveal/$tag
102if [ ! -d $reveal ]; then mkdir -m700 $reveal; fi
103cd $reveal
104if [ -f secret ]; then
53263601
MW
105 echo >&2 "$quis: secret $recov already revealed"
106 exit 1
107fi
599c8f75 108if [ -f $keeper.$i ]; then
53263601
MW
109 echo >&2 "$quis: share $i already revealed"
110 exit 1
111fi
112
113## Decrypt the share.
599c8f75 114umask 077
53263601
MW
115ec_decrypt /dev/stdin \
116 -i$KEYS/recov/$recov/$keeper.$i.share \
599c8f75 117 -o$keeper.$i.new <<EOF
53263601
MW
118$secret
119EOF
599c8f75 120mv $keeper.$i.new $keeper.$i
53263601
MW
121
122## See if there's enough for a recovery.
123n=0
599c8f75 124for j in $keeper.*; do if [ -f "$j" ]; then n=$(( n + 1 )); fi; done
53263601
MW
125if [ $n -lt $t ]; then
126 echo >&2 "$quis: share $i revealed; $(( t - n )) more required"
127else
599c8f75
MW
128 cat $KEYS/recov/$recov/$keeper.param $keeper.* >$keeper.shares
129 shamir recover <$keeper.shares >secret.new
130 pubx=$(ec_public secret.new)
53263601
MW
131 puby=$(cat $KEYS/recov/$recov/pub)
132 case "$pubx" in
133 "$puby") ;;
134 *)
135 echo >&2 "quis: recovered secret key doesn't match public key"
136 exit 1
137 ;;
138 esac
599c8f75 139 mv secret.new secret
53263601
MW
140 echo >&2 "$quis: secret $recov revealed"
141fi
142
143###----- That's all, folks --------------------------------------------------