Lines 9-92
except ImportError, e:
Link Here
|
9 |
sys.stderr.write('On Debian GNU/Linux the package is called "python-m2crypto".\n') |
9 |
sys.stderr.write('On Debian GNU/Linux the package is called "python-m2crypto".\n') |
10 |
sys.exit(1) |
10 |
sys.exit(1) |
11 |
|
11 |
|
12 |
def print_ssl_64(output, name, val): |
12 |
def print_bignum(output, name, val): |
13 |
while val[0] == '\0': |
|
|
14 |
val = val[1:] |
15 |
while len(val) % 8: |
16 |
val = '\0' + val |
17 |
vnew = [] |
18 |
while len(val): |
19 |
vnew.append((val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7])) |
20 |
val = val[8:] |
21 |
vnew.reverse() |
22 |
output.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew))) |
23 |
idx = 0 |
24 |
for v1, v2, v3, v4, v5, v6, v7, v8 in vnew: |
25 |
if not idx: |
26 |
output.write('\t') |
27 |
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))) |
28 |
idx += 1 |
29 |
if idx == 2: |
30 |
idx = 0 |
31 |
output.write('\n') |
32 |
if idx: |
33 |
output.write('\n') |
34 |
output.write('};\n\n') |
35 |
|
36 |
def print_ssl_32(output, name, val): |
37 |
while val[0] == '\0': |
38 |
val = val[1:] |
39 |
while len(val) % 4: |
40 |
val = '\0' + val |
41 |
vnew = [] |
42 |
while len(val): |
43 |
vnew.append((val[0], val[1], val[2], val[3], )) |
44 |
val = val[4:] |
45 |
vnew.reverse() |
46 |
output.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew))) |
47 |
idx = 0 |
48 |
for v1, v2, v3, v4 in vnew: |
49 |
if not idx: |
50 |
output.write('\t') |
51 |
output.write('0x%.2x%.2x%.2x%.2x, ' % (ord(v1), ord(v2), ord(v3), ord(v4))) |
52 |
idx += 1 |
53 |
if idx == 4: |
54 |
idx = 0 |
55 |
output.write('\n') |
56 |
if idx: |
57 |
output.write('\n') |
58 |
output.write('};\n\n') |
59 |
|
60 |
def print_ssl(output, name, val): |
61 |
import struct |
62 |
output.write('#include <stdint.h>\n') |
63 |
if len(struct.pack('@L', 0)) == 8: |
64 |
return print_ssl_64(output, name, val) |
65 |
else: |
66 |
return print_ssl_32(output, name, val) |
67 |
|
68 |
def print_ssl_keys(output, n): |
69 |
output.write(r''' |
70 |
struct pubkey { |
71 |
struct bignum_st e, n; |
72 |
}; |
73 |
|
74 |
#define KEY(data) { \ |
75 |
.d = data, \ |
76 |
.top = sizeof(data)/sizeof(data[0]), \ |
77 |
} |
78 |
|
79 |
#define KEYS(e,n) { KEY(e), KEY(n), } |
80 |
|
81 |
static struct pubkey keys[] = { |
82 |
''') |
83 |
for n in xrange(n + 1): |
84 |
output.write(' KEYS(e_%d, n_%d),\n' % (n, n)) |
85 |
output.write('};\n') |
86 |
pass |
87 |
|
88 |
def print_gcrypt(output, name, val): |
89 |
output.write('#include <stdint.h>\n') |
90 |
while val[0] == '\0': |
13 |
while val[0] == '\0': |
91 |
val = val[1:] |
14 |
val = val[1:] |
92 |
output.write('static const uint8_t %s[%d] = {\n' % (name, len(val))) |
15 |
output.write('static const uint8_t %s[%d] = {\n' % (name, len(val))) |
Lines 103-113
def print_gcrypt(output, name, val):
Link Here
|
103 |
output.write('\n') |
26 |
output.write('\n') |
104 |
output.write('};\n\n') |
27 |
output.write('};\n\n') |
105 |
|
28 |
|
106 |
def print_gcrypt_keys(output, n): |
29 |
def print_keys(output, n): |
107 |
output.write(r''' |
30 |
output.write(r''' |
108 |
struct key_params { |
31 |
struct key_params { |
109 |
const uint8_t *e, *n; |
32 |
const uint8_t *e, *n; |
110 |
uint32_t len_e, len_n; |
33 |
const uint32_t len_e, len_n; |
111 |
}; |
34 |
}; |
112 |
|
35 |
|
113 |
#define KEYS(_e, _n) { \ |
36 |
#define KEYS(_e, _n) { \ |
Lines 120-144
static const struct key_params __attribute__ ((unused)) keys[] = {
Link Here
|
120 |
for n in xrange(n + 1): |
43 |
for n in xrange(n + 1): |
121 |
output.write(' KEYS(e_%d, n_%d),\n' % (n, n)) |
44 |
output.write(' KEYS(e_%d, n_%d),\n' % (n, n)) |
122 |
output.write('};\n') |
45 |
output.write('};\n') |
123 |
|
|
|
124 |
|
46 |
|
125 |
modes = { |
|
|
126 |
'--ssl': (print_ssl, print_ssl_keys), |
127 |
'--gcrypt': (print_gcrypt, print_gcrypt_keys), |
128 |
} |
129 |
|
47 |
|
130 |
try: |
48 |
files = sys.argv[1:-1] |
131 |
mode = sys.argv[1] |
49 |
outfile = sys.argv[-1] |
132 |
files = sys.argv[2:-1] |
|
|
133 |
outfile = sys.argv[-1] |
134 |
except IndexError: |
135 |
mode = None |
136 |
|
50 |
|
137 |
if not mode in modes: |
51 |
if len(files) == 0: |
138 |
print 'Usage: %s [%s] input-file... output-file' % (sys.argv[0], '|'.join(modes.keys())) |
52 |
print 'Usage: %s input-file... output-file' % (sys.argv[0], ) |
139 |
sys.exit(2) |
53 |
sys.exit(2) |
140 |
|
54 |
|
141 |
output = open(outfile, 'w') |
55 |
output = open(outfile, 'w') |
|
|
56 |
output.write('#include <stdint.h>\n\n\n') |
142 |
|
57 |
|
143 |
# load key |
58 |
# load key |
144 |
idx = 0 |
59 |
idx = 0 |
Lines 148-155
for f in files:
Link Here
|
148 |
except RSA.RSAError: |
63 |
except RSA.RSAError: |
149 |
key = RSA.load_key(f) |
64 |
key = RSA.load_key(f) |
150 |
|
65 |
|
151 |
modes[mode][0](output, 'e_%d' % idx, key.e[4:]) |
66 |
print_bignum(output, 'e_%d' % idx, key.e[4:]) |
152 |
modes[mode][0](output, 'n_%d' % idx, key.n[4:]) |
67 |
print_bignum(output, 'n_%d' % idx, key.n[4:]) |
153 |
idx += 1 |
68 |
idx += 1 |
154 |
|
69 |
|
155 |
modes[mode][1](output, idx - 1) |
70 |
print_keys(output, idx - 1) |
156 |
- |
|
|