python-dataproperty: bump version to 0.17.0
[buildroot-gz.git] / package / crda / 0001-crda-support-python-3-in-utils-key2pub.py.patch
blob99eb11cba7a4f9a65ac673d4aba0d67513cd7341
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
11 fixed:
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>
21 ---
22 Status: submitted upstream by author but not (yet) accepted
23 URL: http://www.spinics.net/lists/linux-wireless/msg138936.html
25 Makefile | 2 +-
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
31 --- a/Makefile
32 +++ b/Makefile
33 @@ -112,7 +112,7 @@ $(REG_BIN):
34 keys-%.c: utils/key2pub.py $(wildcard $(PUBKEY_DIR)/*.pem)
35 $(NQ) ' GEN ' $@
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
41 $(NQ) ' CC ' $@
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
46 @@ -1,126 +1,128 @@
47 #!/usr/bin/env python
49 +import io
50 import sys
51 try:
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')
57 - sys.exit(1)
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')
63 + sys.exit(1)
65 +def bitwise_collect(value, radix_bits):
66 + words = []
67 + radix_mask = (1 << radix_bits) - 1
68 + while value != 0:
69 + words.append(value & radix_mask)
70 + value >>= radix_bits
71 + return words
73 def print_ssl_64(output, name, val):
74 - while val[0] == '\0':
75 - val = val[1:]
76 - while len(val) % 8:
77 - val = '\0' + val
78 - vnew = []
79 - while len(val):
80 - vnew.append((val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]))
81 - val = val[8:]
82 - vnew.reverse()
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))
88 idx = 0
89 - for v1, v2, v3, v4, v5, v6, v7, v8 in vnew:
90 + for vword in vwords:
91 if not idx:
92 - output.write('\t')
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)))
94 + output.write(u'\t')
95 + output.write(u'0x{:016x}ULL, '.format(vword))
96 idx += 1
97 if idx == 2:
98 idx = 0
99 - output.write('\n')
100 + output.write(u'\n')
101 if idx:
102 - output.write('\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':
109 - val = val[1:]
110 - while len(val) % 4:
111 - val = '\0' + val
112 - vnew = []
113 - while len(val):
114 - vnew.append((val[0], val[1], val[2], val[3], ))
115 - val = val[4:]
116 - vnew.reverse()
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))
122 idx = 0
123 - for v1, v2, v3, v4 in vnew:
124 + for vword in vwords:
125 if not idx:
126 - output.write('\t')
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))
130 idx += 1
131 if idx == 4:
132 idx = 0
133 - output.write('\n')
134 + output.write(u'\n')
135 if idx:
136 - output.write('\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')
146 import struct
147 - output.write('#include <stdint.h>\n')
148 if len(struct.pack('@L', 0)) == 8:
149 return print_ssl_64(output, name, val)
150 else:
151 return print_ssl_32(output, name, val)
153 def print_ssl_keys(output, n):
154 - output.write(r'''
155 + output.write(u'''
156 struct pubkey {
157 struct bignum_st e, n;
160 -#define KEY(data) { \
161 - .d = data, \
162 - .top = sizeof(data)/sizeof(data[0]), \
163 +#define KEY(data) { \\
164 + .d = 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[] = {
172 ''')
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')
178 pass
180 def print_gcrypt(output, name, val):
181 - output.write('#include <stdint.h>\n')
182 - while val[0] == '\0':
183 - val = val[1:]
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)
187 + vwords.reverse()
189 + output.write(u'#include <stdint.h>\n')
190 + output.write(u'static const uint8_t %s[%d] = {\n' % (name, len(vwords)))
191 idx = 0
192 - for v in val:
193 + for vword in vwords:
194 if not idx:
195 - output.write('\t')
196 - output.write('0x%.2x, ' % ord(v))
197 + output.write(u'\t')
198 + output.write(u'0x{:02x}, '.format(vword))
199 idx += 1
200 if idx == 8:
201 idx = 0
202 - output.write('\n')
203 + output.write(u'\n')
204 if idx:
205 - output.write('\n')
206 - output.write('};\n\n')
207 + output.write(u'\n')
208 + output.write(u'};\n\n')
210 def print_gcrypt_keys(output, n):
211 - output.write(r'''
212 + output.write(u'''
213 struct key_params {
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[] = {
227 ''')
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')
237 modes = {
238 '--ssl': (print_ssl, print_ssl_keys),
239 @@ -135,21 +137,21 @@ except IndexError:
240 mode = None
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())))
245 sys.exit(2)
247 -output = open(outfile, 'w')
248 +output = io.open(outfile, 'w')
250 # load key
251 idx = 0
252 for f in files:
253 - try:
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)
266 idx += 1
268 modes[mode][1](output, idx - 1)
270 2.7.3