]>
git.scottworley.com Git - overonion/blob - overonion
5eb26b68f42c23335cb2ef6dfa9690a3452973ff
13 die
"usage: overonion e|d keyfile"
16 if [[ "$mode" != e
&& "$mode" != d
]];then
17 die
"Use 'e' for encrypt or 'd' for decrypt"
20 if [[ ! -e "$keyfile" ]];then
21 die
"Keyfile not found"
23 if [[ ! -r "$keyfile" ]];then
24 die
"Cannot read keyfile"
27 num_layers
=$(wc -l < "$keyfile")
28 if (( num_layers
< 20 ));then
29 die
"Keyfile doesn't have enough layers to be an onion"
32 hash_fields
=$(awk '/^openssl-dgst / { print NF }' "$keyfile" | uniq )
34 if [[ "$mode" == e
]];then
35 first_layer
=$num_layers
38 if [[ "$hash_fields" != 4 ]];then
39 die
"Refusing to encrypt with already-used key"
45 if [[ "$hash_fields" != 5 ]];then
46 die
"Key does not appear to have been used for encryption (it has no embedded hashes). Refusing to decrypt."
52 if (( layer
== 0 || layer
> num_layers
));then
55 operation
=$(sed -n "${layer}{;s/ .*//;p;}" "$keyfile")
56 if [[ "$operation" == openssl
-enc ]];then
57 openssl enc
$openssl_decrypt "-$(sed -rn "${layer}s/[^ ]+ ([^ ]+) .*/\\1/p" "$keyfile")" \
58 -pass fd:37 37< <(sed -rn "${layer}s
/^
[^
]+ [^
]+ //p
" "$keyfile")
59 elif [[ "$operation" == reverse ]];then
61 elif [[ "$operation" == openssl-dgst ]];then
62 tee >(echo "$(sed -n "${layer}p" "$keyfile") $
(
64 awk -vlayer="$layer" 'NR == layer { print $3 }' "$keyfile" | base64
-d
66 awk -vlayer="$layer" 'NR == layer { print $4 }' "$keyfile" | base64
-d
68 openssl dgst
-binary "-$(sed -rn "${layer}s/^[^ ]+ ([^ ]+).*/\\1/p" "$keyfile")" |
69 base64 --wrap=0)" > "$hash_dir/$layer")
71 die
"Unknown operation"
73 go
$(( layer + next_layer ))
79 for hash_result
in "$hash_dir"/*;do
80 layer
=$(basename "$hash_result")
81 if [[ "$mode" == e
]];then
82 # Add the hashes to keyfile
83 key_aside_dir
=$(mktemp -d "$keyfile.XXXXXXXXXX")
84 key_aside
="$key_aside_dir/key.orig"
85 mv "$keyfile" "$key_aside"
86 sed "${layer}s,.*,$(< "$hash_result")," "$key_aside" > "$keyfile"
88 rmdir "$key_aside_dir"
91 if [[ "$(awk '{ print $5 == $6 ? "hash ok" : "mismatch" }' "$hash_result")" != "hash ok" ]];then
92 die
"Hash check $layer failed"