3 # Copyright (c) 2008, Dave Korn.
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.
10 # A copy of the GNU General Public License can be found at
13 # Written by Dave Korn <dave.korn.cygwin@gmail.com>
16 # Converts a gpg pub key file to a text s-expr for
17 # building into setup.exe's signature verification.
18 # Relies on having pgpdump installed.
21 # gpg-key-to-s-expr.sh [-q|-e|-Q|-c|-C|-1] KEYFILE
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.
31 # Usage: find_a_line ALG COEFFICIENT PGPDUMPFILE
32 # Returns the hex data for the named coefficient..
33 function find_a_line
() {
34 grep -m1 "$1 $2([0-9]* bits) -" < "$3" \
35 |
sed -e 's/^.*- //g' |
tr -d ' '
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'`"#"
73 escapes
="\\\\n$quotes"
82 header
="\\n/* Autogenerated from: $2\\n *\\t\\t by: $0\\n *\\t\\t at: `date "+%c
"`\\t\\t\\t*/\\n\\n"
94 echo "Missing KEYFILE arg"
98 TMPFILE
=`mktemp -t "$(basename "$1").XXXXXXXXXX"` ||
exit 1
100 pgpdump
-milpu "$1" >"$TMPFILE" ||
exit 1
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!
107 alg
=`grep -m1 "Pub alg" $TMPFILE | sed -E -e 's/^.*pub (.*)\)/\1/g'`
111 rsa_n
=`find_a_line RSA n $TMPFILE`
112 rsa_e
=`find_a_line RSA e $TMPFILE`
114 rsa_n
=`line_to_sexpr "$rsa_n"`
115 rsa_e
=`line_to_sexpr "$rsa_e"`
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";
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`
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"`
136 echo -e $header$quotes"(public-key $escapes$nl\
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";