]> git.scottworley.com Git - overonion/blob - overonion
c77ea94fbcfc699ca16bf789062a338c13decb21
[overonion] / overonion
1 #!/bin/bash
2
3 function die() {
4 echo "$*" >&2
5 exit 1
6 }
7
8 if (( $# != 2));then
9 die "usage: overonion e|d keyfile"
10 fi
11 mode=$1
12 if [[ "$mode" != e && "$mode" != d ]];then
13 die "Use 'e' for encrypt or 'd' for decrypt"
14 fi
15 keyfile=$2
16 if [[ ! -e "$keyfile" ]];then
17 die "Keyfile not found"
18 fi
19 if [[ ! -r "$keyfile" ]];then
20 die "Cannot read keyfile"
21 fi
22
23 num_layers=$(wc -l < "$keyfile")
24 if (( num_layers < 20 ));then
25 die "Keyfile doesn't have enough layers to be an onion"
26 fi
27
28 if [[ "$mode" == e ]];then
29 first_layer=$num_layers
30 next_layer=-1
31 openssl_decrypt=""
32 else
33 first_layer=1
34 next_layer=1
35 openssl_decrypt="-d"
36 fi
37
38 function go() {
39 layer=$1
40 if (( layer == 0 || layer > num_layers ));then
41 cat
42 else
43 operation=$(sed -n "${layer}{;s/ .*//;p;}" "$keyfile")
44 if [[ "$operation" == openssl-enc ]];then
45 openssl enc $openssl_decrypt "-$(sed -rn "${layer}s/[^ ]+ ([^ ]+) .*/\\1/p" "$keyfile")" \
46 -pass fd:37 37< <(sed -rn "${layer}s/^[^ ]+ [^ ]+ //p" "$keyfile")
47 elif [[ "$operation" == reverse ]];then
48 reverse
49 else
50 die "Unknown operation"
51 fi |
52 go $(( layer + next_layer ))
53 fi
54 }
55
56 go "$first_layer"