1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
xrdp: A Remote Desktop Protocol server.
Copyright (C) Jay Sorg 2007
rsa key generator for xrdp
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rc4.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <openssl/bn.h>
#include <openssl/x509v3.h>
#include <openssl/rsa.h>
#include "os_calls.h"
#include "arch.h"
#define MY_KEY_SIZE 512
static char g_rev_exponent[4] = { 0x00, 0x01, 0x00, 0x01 };
/*****************************************************************************/
static void
reverse(char* p, int len)
{
int i;
int j;
char temp;
for (i = 0, j = len - 1; i < j; i++, j--)
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
/*****************************************************************************/
static int APP_CC
out_params(void)
{
g_writeln("");
g_writeln("key gen utility example './keygen xrdp'");
g_writeln("");
return 0;
}
/*****************************************************************************/
static int APP_CC
sign_key(char* e_data, int e_len, char* n_data, int n_len,
char* d_data, int d_len, char* sign_data, int sign_len)
{
g_writeln("todo sign here");
g_writeln("");
return 0;
}
/*****************************************************************************/
static int APP_CC
write_out_line(int fd, char* name, char* data, int len)
{
int max;
int error;
int index;
int data_item;
int buf_pos;
char* buf;
char* text;
text = (char*)g_malloc(256, 0);
max = len;
max = max * 10;
buf_pos = g_strlen(name);
max = max + buf_pos + 16;
buf = (char*)g_malloc(max, 0);
g_strncpy(buf, name, max - 1);
buf[buf_pos] = '=';
buf_pos++;
for (index = 0; index < len; index++)
{
data_item = (unsigned char)data[index];
g_snprintf(text, 255, "0x%2.2x", data_item);
if (index != 0)
{
buf[buf_pos] = ',';
buf_pos++;
}
buf[buf_pos] = text[0];
buf_pos++;
buf[buf_pos] = text[1];
buf_pos++;
buf[buf_pos] = text[2];
buf_pos++;
buf[buf_pos] = text[3];
buf_pos++;
}
buf[buf_pos] = '\n';
buf_pos++;
buf[buf_pos] = 0;
error = g_file_write(fd, buf, buf_pos) == -1;
g_free(buf);
g_free(text);
return error;
}
/*****************************************************************************/
static int APP_CC
save_all(char* e_data, int e_len, char* n_data, int n_len,
char* d_data, int d_len, char* sign_data, int sign_len)
{
int fd;
g_writeln("saving to rsakeys.ini");
g_writeln("");
if (g_file_exist("rsakeys.ini"))
{
g_file_delete("rsakeys.ini");
}
fd = g_file_open("rsakeys.ini");
if (fd > 0)
{
g_file_write(fd, "[keys]\n", 7);
write_out_line(fd, "pub_exp", e_data, e_len);
write_out_line(fd, "pub_mod", n_data, n_len);
write_out_line(fd, "pub_sig", sign_data, sign_len);
write_out_line(fd, "pri_exp", d_data, d_len);
}
g_file_close(fd);
return 0;
}
/*****************************************************************************/
static int APP_CC
key_gen(void)
{
char* e_data;
char* n_data;
char* d_data;
char* sign_data;
unsigned char* p;
int len;
int e_len;
int n_len;
int d_len;
int sign_len;
int error;
int offset;
BN_CTX* my_ctx;
RSA* my_key;
BIGNUM* my_e;
e_data = 0;
n_data = 0;
d_data = 0;
sign_data = 0;
e_len = 0;
n_len = 0;
d_len = 0;
sign_len = 0;
error = 0;
my_ctx = BN_CTX_new();
my_e = BN_new();
p = (unsigned char*)g_rev_exponent;
len = sizeof(g_rev_exponent);
BN_bin2bn(p, len, my_e);
my_key = RSA_new();
g_writeln("");
g_writeln("Generating %d bit rsa key...", MY_KEY_SIZE);
g_writeln("");
if (error == 0)
{
/* RSA_generate_key_ex returns boolean */
error = RSA_generate_key_ex(my_key, MY_KEY_SIZE, my_e, 0) == 0;
if (error != 0)
{
g_writeln("error %d in key_gen, RSA_generate_key_ex", error);
}
}
if (error == 0)
{
g_writeln("RSA_generate_key_ex ok");
g_writeln("");
e_len = BN_num_bytes(my_key->e);
if (e_len > 0)
{
e_data = (char*)g_malloc(e_len, 1);
offset = (((e_len + 3) / 4) * 4) - e_len;
e_len = e_len + offset;
p = (unsigned char*)(e_data + offset);
BN_bn2bin(my_key->e, p);
reverse(e_data, e_len);
g_writeln("public exponent size %d bytes", e_len);
g_hexdump(e_data, e_len);
g_writeln("");
}
n_len = BN_num_bytes(my_key->n);
if (n_len > 0)
{
n_data = (char*)g_malloc(n_len, 1);
offset = (((n_len + 3) / 4) * 4) - n_len;
n_len = n_len + offset;
p = (unsigned char*)(n_data + offset);
BN_bn2bin(my_key->n, p);
reverse(n_data, n_len);
g_writeln("public modulus size %d bytes", n_len);
g_hexdump(n_data, n_len);
g_writeln("");
/* signature is same size as public modulus */
sign_data = (char*)g_malloc(n_len, 1);
sign_len = n_len;
}
d_len = BN_num_bytes(my_key->d);
if (d_len > 0)
{
d_data = (char*)g_malloc(d_len, 0);
offset = (((d_len + 3) / 4) * 4) - d_len;
d_len = d_len + offset;
p = (unsigned char*)(d_data + offset);
BN_bn2bin(my_key->d, p);
reverse(d_data, d_len);
g_writeln("private exponent size %d bytes", d_len);
g_hexdump(d_data, d_len);
g_writeln("");
}
error = sign_key(e_data, e_len, n_data, n_len, d_data, d_len,
sign_data, sign_len);
if (error != 0)
{
g_writeln("error %d in key_gen, sign_key", error);
}
}
if (error == 0)
{
error = save_all(e_data, e_len, n_data, n_len, d_data, d_len,
sign_data, sign_len);
if (error != 0)
{
g_writeln("error %d in key_gen, save_all", error);
}
}
BN_free(my_e);
RSA_free(my_key);
BN_CTX_free(my_ctx);
g_free(e_data);
g_free(n_data);
g_free(d_data);
g_free(sign_data);
return error;
}
/*****************************************************************************/
int DEFAULT_CC
main(int argc, char** argv)
{
if (argc == 2)
{
if (g_strcasecmp(argv[1], "xrdp") == 0)
{
return key_gen();
}
}
out_params();
return 0;
}
|