1 From 797f2836c48f9ba2446629ae4b6867ca1a5ea512 Mon Sep 17 00:00:00 2001
2 From: Taahir Ahmed <ahmed.taahir@gmail.com>
3 Date: Wed, 30 Mar 2016 11:23:54 -0300
4 Subject: [PATCH 1/2] crda: support python 3 in utils/key2pub.py
6 utils/key2pub.py can now be run under either python 2.7 or python 3.x.
7 This required some minor syntactical changes as well as switching from
8 M2Crypto to pycrypto, since M2Crypto doesn't support python 3.x.
10 In addition, some errors in the generated source file keys-ssl.h are
13 * The correct OpenSSL header for BN_ULONG is included.
15 * The generated constants are given the 'ull' suffix to prevent
16 warnings about constants that are too large.
18 [Gustavo: don't call /utils/key2pub.py since that doesn't compute]
20 Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
22 Status: submitted upstream by author but not (yet) accepted
23 URL: http://www.spinics.net/lists/linux-wireless/msg138936.html
26 utils/key2pub.py | 146 ++++++++++++++++++++++++++++---------------------------
27 2 files changed, 75 insertions(+), 73 deletions(-)
29 diff --git a/Makefile b/Makefile
30 index 1f25509..523a96e 100644
33 @@ -112,7 +112,7 @@ $(REG_BIN):
34 keys-%.c: utils/key2pub.py $(wildcard $(PUBKEY_DIR)/*.pem)
36 $(NQ) ' Trusted pubkeys:' $(wildcard $(PUBKEY_DIR)/*.pem)
37 - $(Q)./utils/key2pub.py --$* $(wildcard $(PUBKEY_DIR)/*.pem) $@
38 + $(Q) python utils/key2pub.py --$* $(wildcard $(PUBKEY_DIR)/*.pem) $@
40 $(LIBREG): regdb.h reglib.h reglib.c
42 diff --git a/utils/key2pub.py b/utils/key2pub.py
43 index 3e84cd2..c76cbbb 100755
44 --- a/utils/key2pub.py
45 +++ b/utils/key2pub.py
52 - from M2Crypto import RSA
53 -except ImportError, e:
54 - sys.stderr.write('ERROR: Failed to import the "M2Crypto" module: %s\n' % e.message)
55 - sys.stderr.write('Please install the "M2Crypto" Python module.\n')
56 - sys.stderr.write('On Debian GNU/Linux the package is called "python-m2crypto".\n')
58 + from Crypto.PublicKey import RSA
59 +except ImportError as e:
60 + sys.stderr.write('ERROR: Failed to import the "Crypto.PublicKey" module: %s\n' % e.message)
61 + sys.stderr.write('Please install the "Crypto.PublicKey" Python module.\n')
62 + sys.stderr.write('On Debian GNU/Linux the package is called "python-crypto".\n')
65 +def bitwise_collect(value, radix_bits):
67 + radix_mask = (1 << radix_bits) - 1
69 + words.append(value & radix_mask)
70 + value >>= radix_bits
73 def print_ssl_64(output, name, val):
74 - while val[0] == '\0':
80 - vnew.append((val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]))
83 - output.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew)))
84 + # OpenSSL expects 64-bit words given least-significant-word first.
85 + vwords = bitwise_collect(val, 64)
87 + output.write(u'static BN_ULONG {}[] = {{\n'.format(name))
89 - for v1, v2, v3, v4, v5, v6, v7, v8 in vnew:
90 + for vword in vwords:
93 - output.write('0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x, ' % (ord(v1), ord(v2), ord(v3), ord(v4), ord(v5), ord(v6), ord(v7), ord(v8)))
95 + output.write(u'0x{:016x}ULL, '.format(vword))
100 + output.write(u'\n')
103 - output.write('};\n\n')
104 + output.write(u'\n')
105 + output.write(u'};\n\n')
107 def print_ssl_32(output, name, val):
108 - while val[0] == '\0':
110 - while len(val) % 4:
114 - vnew.append((val[0], val[1], val[2], val[3], ))
117 - output.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew)))
118 + # OpenSSL expects 32-bit words given least-significant-word first.
119 + vwords = bitwise_collect(val, 32)
121 + output.write(u'static BN_ULONG {}[] = {{\n'.format(name))
123 - for v1, v2, v3, v4 in vnew:
124 + for vword in vwords:
127 - output.write('0x%.2x%.2x%.2x%.2x, ' % (ord(v1), ord(v2), ord(v3), ord(v4)))
128 + output.write(u'\t')
129 + output.write(u'0x{:08x}, '.format(vword))
134 + output.write(u'\n')
137 - output.write('};\n\n')
138 + output.write(u'\n')
139 + output.write(u'};\n\n')
141 def print_ssl(output, name, val):
143 + output.write(u'#include <stdint.h>\n')
144 + output.write(u'#include <openssl/bn.h>\n')
147 - output.write('#include <stdint.h>\n')
148 if len(struct.pack('@L', 0)) == 8:
149 return print_ssl_64(output, name, val)
151 return print_ssl_32(output, name, val)
153 def print_ssl_keys(output, n):
157 struct bignum_st e, n;
160 -#define KEY(data) { \
162 - .top = sizeof(data)/sizeof(data[0]), \
163 +#define KEY(data) { \\
165 + .top = sizeof(data)/sizeof(data[0]), \\
168 -#define KEYS(e,n) { KEY(e), KEY(n), }
169 +#define KEYS(e,n) { KEY(e), KEY(n), }
171 static struct pubkey keys[] = {
173 for n in xrange(n + 1):
174 - output.write(' KEYS(e_%d, n_%d),\n' % (n, n))
175 - output.write('};\n')
176 + output.write(u' KEYS(e_{0}, n_{0}),\n'.format(n))
177 + output.write(u'};\n')
180 def print_gcrypt(output, name, val):
181 - output.write('#include <stdint.h>\n')
182 - while val[0] == '\0':
184 - output.write('static const uint8_t %s[%d] = {\n' % (name, len(val)))
185 + # gcrypt expects 8-bit words most-significant-word first
186 + vwords = bitwise_collect(val, 8)
189 + output.write(u'#include <stdint.h>\n')
190 + output.write(u'static const uint8_t %s[%d] = {\n' % (name, len(vwords)))
193 + for vword in vwords:
196 - output.write('0x%.2x, ' % ord(v))
197 + output.write(u'\t')
198 + output.write(u'0x{:02x}, '.format(vword))
203 + output.write(u'\n')
206 - output.write('};\n\n')
207 + output.write(u'\n')
208 + output.write(u'};\n\n')
210 def print_gcrypt_keys(output, n):
214 const uint8_t *e, *n;
215 uint32_t len_e, len_n;
218 -#define KEYS(_e, _n) { \
219 - .e = _e, .len_e = sizeof(_e), \
220 - .n = _n, .len_n = sizeof(_n), \
221 +#define KEYS(_e, _n) { \\
222 + .e = _e, .len_e = sizeof(_e), \\
223 + .n = _n, .len_n = sizeof(_n), \\
226 static const struct key_params keys[] = {
228 - for n in xrange(n + 1):
229 - output.write(' KEYS(e_%d, n_%d),\n' % (n, n))
230 - output.write('};\n')
232 + for n in range(n + 1):
233 + output.write(u' KEYS(e_{0}, n_{0}),\n'.format(n))
234 + output.write(u'};\n')
238 '--ssl': (print_ssl, print_ssl_keys),
239 @@ -135,21 +137,21 @@ except IndexError:
242 if not mode in modes:
243 - print 'Usage: %s [%s] input-file... output-file' % (sys.argv[0], '|'.join(modes.keys()))
244 + print('Usage: {} [{}] input-file... output-file'.format(sys.argv[0], '|'.join(modes.keys())))
247 -output = open(outfile, 'w')
248 +output = io.open(outfile, 'w')
254 - key = RSA.load_pub_key(f)
255 - except RSA.RSAError:
256 - key = RSA.load_key(f)
258 - modes[mode][0](output, 'e_%d' % idx, key.e[4:])
259 - modes[mode][0](output, 'n_%d' % idx, key.n[4:])
260 + key_contents = io.open(f, 'rb').read()
261 + key = RSA.importKey(key_contents)
263 + modes[mode][0](output, 'e_{}'.format(idx), key.e)
264 + modes[mode][0](output, 'n_{}'.format(idx), key.n)
268 modes[mode][1](output, idx - 1)