1 |
#include <stdio.h> |
2 |
#include <string.h> |
3 |
#include <unistd.h> |
4 |
#include <fcntl.h> |
5 |
#include <openssl/rsa.h> |
6 |
|
7 |
/* reverse an array in situ */ |
8 |
void |
9 |
reverse(unsigned char *p, unsigned int len) |
10 |
{ |
11 |
char temp; |
12 |
int i, j; |
13 |
|
14 |
for (i = 0, j = len - 1; i < j; i++, j--) |
15 |
{ |
16 |
temp = p[i]; |
17 |
p[i] = p[j]; |
18 |
p[j] = temp; |
19 |
} |
20 |
} |
21 |
|
22 |
int |
23 |
read_file(char *filename, unsigned char *buffer, unsigned int maxlen) |
24 |
{ |
25 |
int fd, len; |
26 |
|
27 |
fd = open(filename, O_RDONLY); |
28 |
if (fd == -1) |
29 |
{ |
30 |
perror(filename); |
31 |
return -1; |
32 |
} |
33 |
|
34 |
len = read(fd, buffer, maxlen); |
35 |
close(fd); |
36 |
return len; |
37 |
} |
38 |
|
39 |
int |
40 |
write_file(char *filename, unsigned char *buffer, unsigned int len) |
41 |
{ |
42 |
int fd; |
43 |
|
44 |
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
45 |
if (fd == -1) |
46 |
{ |
47 |
perror(filename); |
48 |
return -1; |
49 |
} |
50 |
|
51 |
len = write(fd, buffer, len); |
52 |
close(fd); |
53 |
return len; |
54 |
} |
55 |
|
56 |
BIGNUM * |
57 |
parse_bignum(unsigned char **buf, unsigned int len) |
58 |
{ |
59 |
BIGNUM *bn; |
60 |
|
61 |
reverse(*buf, len); |
62 |
bn = BN_bin2bn(*buf, len, NULL); |
63 |
*buf += len; |
64 |
|
65 |
return bn; |
66 |
} |
67 |
|
68 |
RSA * |
69 |
parse_rsa2(unsigned char *buf, unsigned int len) |
70 |
{ |
71 |
RSA *rsa; |
72 |
unsigned int modlen; |
73 |
|
74 |
if (strncmp(buf, "RSA2", 4) != 0) |
75 |
{ |
76 |
fprintf(stderr, "Not an RSA2 private key\n"); |
77 |
return NULL; |
78 |
} |
79 |
|
80 |
/* FIXME: should be a DWORD */ |
81 |
modlen = *(buf + 4); |
82 |
if (len < (20 + (9 * modlen / 2))) |
83 |
{ |
84 |
fprintf(stderr, "Input file truncated?\n"); |
85 |
return NULL; |
86 |
} |
87 |
|
88 |
buf += 16; |
89 |
|
90 |
rsa = RSA_new(); |
91 |
if (rsa == NULL) |
92 |
return NULL; |
93 |
|
94 |
rsa->e = parse_bignum(&buf, 4); |
95 |
rsa->n = parse_bignum(&buf, modlen); |
96 |
rsa->p = parse_bignum(&buf, modlen / 2); |
97 |
rsa->q = parse_bignum(&buf, modlen / 2); |
98 |
rsa->dmp1 = parse_bignum(&buf, modlen / 2); |
99 |
rsa->dmq1 = parse_bignum(&buf, modlen / 2); |
100 |
rsa->iqmp = parse_bignum(&buf, modlen / 2); |
101 |
rsa->d = parse_bignum(&buf, modlen); |
102 |
|
103 |
return rsa; |
104 |
} |
105 |
|
106 |
int |
107 |
main(int argc, char *argv[]) |
108 |
{ |
109 |
unsigned char buffer[1024]; |
110 |
unsigned char *p = buffer; |
111 |
unsigned int len; |
112 |
RSA *rsa; |
113 |
|
114 |
if (argc < 3) |
115 |
{ |
116 |
fprintf(stderr, "Usage: %s <infile> <outfile>\n", argv[0]); |
117 |
return 0; |
118 |
} |
119 |
|
120 |
if ((len = read_file(argv[1], buffer, sizeof(buffer))) == -1) |
121 |
{ |
122 |
fprintf(stderr, "Failed to read file\n"); |
123 |
return 1; |
124 |
} |
125 |
|
126 |
if ((rsa = parse_rsa2(buffer, len)) == NULL) |
127 |
{ |
128 |
fprintf(stderr, "Failed to parse RSA key\n"); |
129 |
return 1; |
130 |
} |
131 |
|
132 |
len = i2d_RSAPrivateKey(rsa, &p); |
133 |
if (write_file(argv[2], buffer, len) == -1) |
134 |
{ |
135 |
fprintf(stderr, "Failed to write file\n"); |
136 |
return 1; |
137 |
} |
138 |
|
139 |
return 0; |
140 |
} |