and onionbalance module
[mirror/dsa-puppet.git] / modules / onion / files / tor-onion-name
1 #!/bin/bash
2
3 # Obtain the hidden service name from a tor hidden service RSA key
4
5 # Copyright (c) 2016 Peter Palfrader
6 #
7 # Permission is hereby granted, free of charge, to any person
8 # obtaining a copy of this software and associated documentation
9 # files (the "Software"), to deal in the Software without
10 # restriction, including without limitation the rights to use,
11 # copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the
13 # Software is furnished to do so, subject to the following
14 # conditions:
15 #
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 # OTHER DEALINGS IN THE SOFTWARE.
27
28 set -e
29 set -u
30 set -o pipefail
31
32 usage() {
33     echo "$0 [-p] [hidden service RSA key]"
34     echo "  Computes the Tor onion hostname from a given RSA public or private key."
35     echo "  Use -p to indicate you are passing a public key.  If none is given as an"
36     echo "  argument, one is read from stdin."
37 }
38
39 umask 077
40 export LC_ALL=C
41
42 if ! command -v openssl >/dev/null 2>&1 ; then
43     echo >&2 "This program needs the openssl command line tool".
44     exit 1
45 fi
46
47 tempdir=""
48 cleanup() {
49     cd /
50     if [ -n "$tempdir" ]; then
51         rm -rf "$tempdir"
52     fi
53 }
54 trap 'cleanup' EXIT
55 tempdir="$(mktemp -d)"
56
57 PUBIN=""
58
59 while getopts "ph" OPTION
60 do
61     case $OPTION in
62         h)
63             usage
64             exit
65             ;;
66         p)
67             PUBIN=1
68             ;;
69         *)
70             usage >&2
71             exit 1
72             ;;
73     esac
74 done
75 shift $(($OPTIND - 1))
76
77 if [ "$#" = 0 ]; then
78     KEY="$tempdir/key"
79     cat > "$KEY"
80 elif [ "$#" = 1 ]; then
81     KEY="$1"
82     shift
83 else
84     usage >&2
85     exit 1
86 fi
87
88 if [ -z "$PUBIN" ]; then
89   PKEY="$tempdir/pkey"
90   if ! openssl rsa -pubout < "$KEY" 2>&1 > "$PKEY" | (grep -Fxv 'writing RSA key' >&2 || true); then
91       echo >&2 "Maybe you need to use -p for using a public key?"
92       exit 1
93   fi
94   KEY="$PKEY"
95 fi
96
97 mod="$(openssl rsa -pubin < "$KEY" -modulus -noout | cut -d= -f 2)"
98 exp="$(openssl rsa -pubin < "$KEY" -text -noout | awk '$1=="Exponent:" {print $2}')"
99 cat > "$tempdir/asn" << EOF
100 asn1=SEQUENCE:seq_sect
101 [seq_sect]
102 field1=INTEGER:0x$mod
103 field2=INTEGER:$exp
104 EOF
105
106 openssl asn1parse -genconf "$tempdir/asn" -noout -out "$tempdir/blob"
107
108 python -c 'import base64, hashlib, sys; \
109     d = hashlib.sha1(sys.stdin.read()).digest()[0:10]; \
110     print "%s.onion"%(base64.b32encode(d).lower(),)
111   ' < "$tempdir/blob"
112 #if command -v base32  >/dev/null 2>&1 ; then
113 #    echo $(
114 #    perl -MDigest::SHA -e '
115 #        $/=undef;
116 #        $d=Digest::SHA::sha1(<>);
117 #        $d=substr($d,0,10);
118 #        print $d;
119 #      ' < "$tempdir/blob" | base32 | tr A-Z a-z
120 #        ).onion
121 #else
122 #    perl -MDigest::SHA -e '
123 #        eval("use MIME::Base32 qw( RFC )");
124 #        if ($@) {
125 #          print STDERR "This program needs either the base32 command line tool or the MIME::Base32 perl module.\n";
126 #          exit 1;
127 #        }
128 #        $/=undef;
129 #        $d=Digest::SHA::sha1(<>);
130 #        $d=substr($d,0,10);
131 #        print lc(MIME::Base32::encode($d)), ".onion\n"
132 #      ' < "$tempdir/blob"
133 #fi
134
135 # vim:set et:
136 # vim:set ts=4:
137 # vim:set shiftwidth=4: