5 hashes
=(sha sha1 mdc2 ripemd160 sha224 sha256 sha384 sha512 md4 md5 dss1
)
14 die
"usage: overonion e|d keyfile"
17 if [[ "$mode" != e
&& "$mode" != d
]];then
18 die
"Use 'e' for encrypt or 'd' for decrypt"
21 if [[ ! -e "$keyfile" ]];then
22 die
"Keyfile not found"
24 if [[ ! -r "$keyfile" ]];then
25 die
"Cannot read keyfile"
28 num_layers
=$(wc -l < "$keyfile")
29 if (( num_layers
< 20 ));then
30 die
"Keyfile doesn't have enough layers to be an onion"
33 if [[ "$mode" == e
]];then
34 first_layer
=$num_layers
43 function verify_hash
() {
44 (( $(wc -l < "$1") == 2 && $(uniq "$1" | wc -l) == 1 ))
49 if (( layer
== 0 || layer
> num_layers
));then
52 operation
=$(sed -n "${layer}{;s/ .*//;p;}" "$keyfile")
53 if [[ "$operation" == openssl
-enc ]];then
54 openssl enc
$openssl_decrypt "-$(sed -rn "${layer}s/[^ ]+ ([^ ]+) .*/\\1/p" "$keyfile")" \
55 -pass fd:37 37< <(sed -rn "${layer}s
/^
[^
]+ [^
]+ //p
" "$keyfile")
56 elif [[ "$operation" == reverse ]];then
58 elif [[ "$operation" == openssl-dgst ]];then
59 tee >(sed -rn "${layer}s
/^
[^
]+ [^
]+ //p
" "$keyfile" > "$hash_dir/$layer"
60 openssl dgst -binary "-$(sed -rn "${layer}s/[^ ]+ ([^ ]+) .
*/\\1/p
" "$keyfile")" |
61 base64
--wrap=0 | sed 's/$/\n/' >> "$hash_dir/$layer"
62 # Dying here doesn't terminate the pipeline. :(
63 verify_hash
"$hash_dir/$layer" || die
"Hash check $layer failed" )
65 die
"Unknown operation"
67 go
$(( layer + next_layer ))
71 function record_hashes
() {
72 if [[ "$mode" == d
]] || (( $# < 2 ));then
78 tee >(openssl dgst
-binary "-$hash" | base64
--wrap=0 |
79 sed "s/^/openssl-dgst $hash /;s/$/\n/" > "$hash_dir/$stage-$hash") |
80 record_hashes
"$stage" "$@"
84 record_hashes inner
"${hashes[@]}" | go
"$first_layer" | record_hashes outer
"${hashes[@]}"
86 if [[ "$mode" == e
]];then
87 # Add the hashes to keyfile
88 key_aside_dir
=$(mktemp -d "$keyfile.XXXXXXXXXX")
89 key_aside
="$key_aside_dir/key.orig"
90 mv "$keyfile" "$key_aside"
91 cat "$hash_dir"/outer
-* "$key_aside" "$hash_dir"/inner
* > "$keyfile"
93 rmdir "$key_aside_dir"
96 for hash_result
in "$hash_dir"/*;do
97 verify_hash
"$hash_result" || die
"Hash check $(basename "$hash_result") failed"