]>
Commit | Line | Data |
---|---|---|
dbfe3c19 DK |
1 | #!/bin/bash |
2 | # | |
3 | # Copyright (c) 2008, Dave Korn. | |
4 | # | |
5 | # This program is free software; you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation; either version 2 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # A copy of the GNU General Public License can be found at | |
11 | # http://www.gnu.org/ | |
12 | # | |
13 | # Written by Dave Korn <dave.korn.cygwin@gmail.com> | |
14 | # | |
15 | # | |
dd9d95a3 | 16 | # Converts a gpg pub key file to a text s-expr for |
dbfe3c19 DK |
17 | # building into setup.exe's signature verification. |
18 | # Relies on having pgpdump installed. | |
19 | # | |
20 | # Usage: | |
21 | # gpg-key-to-s-expr.sh [-q|-e|-Q|-c|-C|-1] KEYFILE | |
22 | # | |
23 | # -q means surround output in quotes to make it easier | |
24 | # for use in a C #include fragment. -e means escape the | |
25 | # line ends. -Q means both. -c means output a C-style | |
26 | # string for printing, -C makes the strine a one-liner and | |
27 | # outputs a C header as well. -1 means generate all the | |
28 | # output on a single line. Only one option should be | |
29 | # specified or the behaviour is undefined. | |
30 | ||
dd9d95a3 JT |
31 | # Usage: find_a_line ALG COEFFICIENT PGPDUMPFILE |
32 | # Returns the hex data for the named coefficient.. | |
dbfe3c19 | 33 | function find_a_line() { |
dd9d95a3 | 34 | grep -m1 "$1 $2([0-9]* bits) -" < "$3" \ |
dbfe3c19 DK |
35 | | sed -e 's/^.*- //g' | tr -d ' ' |
36 | } | |
37 | ||
38 | # Usage: line_to_sexpr HEXDATA | |
39 | # Convert hex data from find_a_line to s-expr format: | |
40 | # prepends 00 to avoid signedness problems if high bit | |
41 | # is set, and surrounds with hash marks. | |
42 | function line_to_sexpr() { | |
43 | echo "#"`echo "$1" | sed -e's/^\([89A-Fa-f]\)/00\1/g'`"#" | |
44 | } | |
45 | ||
46 | quotes= | |
47 | escapes= | |
48 | starts= | |
49 | mid= | |
50 | header= | |
51 | nl="\n" | |
52 | ind=" " | |
53 | ||
54 | if [ x$1 == x-q ] ; | |
55 | then | |
56 | quotes='"' | |
57 | shift ; | |
58 | fi | |
59 | if [ x$1 == x-e ] ; | |
60 | then | |
61 | escapes='\\' | |
62 | shift ; | |
63 | fi | |
64 | if [ x$1 == x-Q ] ; | |
65 | then | |
66 | quotes='"' | |
67 | escapes='\\' | |
68 | shift ; | |
69 | fi | |
70 | if [ x$1 == x-c ] ; | |
71 | then | |
72 | quotes='"' | |
73 | escapes="\\\\n$quotes" | |
74 | starts="$quotes" | |
75 | shift ; | |
76 | fi | |
77 | if [ x$1 == x-C ] ; | |
78 | then | |
79 | quotes='"' | |
80 | escapes="$quotes" | |
81 | mid="$quotes" | |
82 | header="\\n/* Autogenerated from: $2\\n *\\t\\t by: $0\\n *\\t\\t at: `date "+%c"`\\t\\t\\t*/\\n\\n" | |
83 | shift ; | |
84 | fi | |
85 | if [ x$1 == x-1 ] ; | |
86 | then | |
87 | nl= | |
88 | ind= | |
89 | shift ; | |
90 | fi | |
91 | ||
92 | if [ $# -ne 1 ] ; | |
93 | then | |
94 | echo "Missing KEYFILE arg" | |
95 | exit 1 ; | |
96 | fi | |
97 | ||
98 | TMPFILE=`mktemp -t "$(basename "$1").XXXXXXXXXX"` || exit 1 | |
99 | ||
100 | pgpdump -milpu "$1" >"$TMPFILE" || exit 1 | |
101 | ||
102 | # Yes, this could be done *far* more efficiently in any one of | |
103 | # perl/awk/python/$YOURFAVOURITETOOL than by spawning a whole | |
104 | # bunch of bashes, greps and seds. No, I don't care. Don't bug | |
105 | # me about it until we have to run this script a million times a day! | |
106 | ||
dd9d95a3 JT |
107 | alg=`grep -m1 "Pub alg" $TMPFILE | sed -E -e 's/^.*pub (.*)\)/\1/g'` |
108 | ||
109 | case $alg in | |
110 | 1) | |
111 | rsa_n=`find_a_line RSA n $TMPFILE` | |
112 | rsa_e=`find_a_line RSA e $TMPFILE` | |
113 | ||
114 | rsa_n=`line_to_sexpr "$rsa_n"` | |
115 | rsa_e=`line_to_sexpr "$rsa_e"` | |
116 | ||
117 | echo -e $header$quotes"(public-key $escapes$nl\ | |
118 | $starts$ind$mid(rsa $escapes$nl\ | |
119 | $starts$ind$ind$mid(n $rsa_n) $escapes$nl\ | |
120 | $starts$ind$ind$mid(e $rsa_e) $escapes$nl\ | |
121 | $starts$ind$mid)$escapes$nl\ | |
122 | $starts$mid)$quotes$nl"; | |
123 | ;; | |
dbfe3c19 | 124 | |
dd9d95a3 JT |
125 | 17) |
126 | dsa_p=`find_a_line DSA p $TMPFILE` | |
127 | dsa_q=`find_a_line DSA q $TMPFILE` | |
128 | dsa_g=`find_a_line DSA g $TMPFILE` | |
129 | dsa_y=`find_a_line DSA y $TMPFILE` | |
dbfe3c19 | 130 | |
dd9d95a3 JT |
131 | dsa_p=`line_to_sexpr "$dsa_p"` |
132 | dsa_q=`line_to_sexpr "$dsa_q"` | |
133 | dsa_g=`line_to_sexpr "$dsa_g"` | |
134 | dsa_y=`line_to_sexpr "$dsa_y"` | |
135 | ||
136 | echo -e $header$quotes"(public-key $escapes$nl\ | |
dbfe3c19 DK |
137 | $starts$ind$mid(dsa $escapes$nl\ |
138 | $starts$ind$ind$mid(p $dsa_p) $escapes$nl\ | |
139 | $starts$ind$ind$mid(q $dsa_q) $escapes$nl\ | |
140 | $starts$ind$ind$mid(g $dsa_g) $escapes$nl\ | |
141 | $starts$ind$ind$mid(y $dsa_y)$escapes$nl\ | |
142 | $starts$ind$mid)$escapes$nl\ | |
143 | $starts$mid)$quotes$nl"; | |
144 | ||
dd9d95a3 JT |
145 | ;; |
146 | esac | |
147 | ||
dbfe3c19 | 148 | rm "$TMPFILE" |