summaryrefslogtreecommitdiff
path: root/other/guess-who/keygen.cc
diff options
context:
space:
mode:
Diffstat (limited to 'other/guess-who/keygen.cc')
-rw-r--r--other/guess-who/keygen.cc119
1 files changed, 119 insertions, 0 deletions
diff --git a/other/guess-who/keygen.cc b/other/guess-who/keygen.cc
new file mode 100644
index 0000000..3e2af93
--- /dev/null
+++ b/other/guess-who/keygen.cc
@@ -0,0 +1,119 @@
1/* Key generator for rsa keys (SSH2!)
2 * (C) 2002 Sebastian Krahmer.
3 * WARNING: theres no random is the keys, so
4 * THE GENERATED KEYS ARE WEAK! Do not use it to
5 * generate your pubkeys, this program is for debugging only.
6 */
7#include <stdio.h>
8#include <sys/types.h>
9#include <netinet/in.h>
10
11extern "C" {
12#include <openssl/rsa.h>
13#include <openssl/pem.h>
14#include <openssl/bn.h>
15#include <openssl/evp.h>
16}
17
18#include <string.h>
19
20#include "base64.h"
21#include "misc.h"
22
23
24int rsa2blob(RSA *key, unsigned char *buf, size_t buflen)
25{
26 size_t blen = buflen;
27 int h;
28 unsigned char *tmp;
29
30 if (blen < 4)
31 return -1;
32
33 if (key->e->neg || key->n->neg)
34 printf("AAA");
35
36 size_t l = strlen("ssh-rsa");
37 unsigned char *ptr = buf;
38 *(unsigned int*)ptr = htonl(l);
39 ptr += 4; blen -= 4;
40 if (blen < l)
41 return -1;
42 memcpy(ptr, "ssh-rsa", l); // HAH!
43 ptr += l; blen -= l;
44
45 unsigned int n = BN_num_bytes(key->e);
46 if (blen < n+1+4)
47 return -1;
48
49 tmp = new unsigned char [n];
50 BN_bn2bin(key->e, tmp);
51 h = (tmp[0] & 0x80) ? 1 : 0;
52 *(unsigned int*)ptr = htonl(n+h);
53 ptr += 4; blen -= 4;
54 *ptr = 0;
55 memcpy(ptr+h, tmp, n);
56 ptr += n; blen -= n;
57 ptr += h; blen -= h;
58 delete [] tmp;
59
60 n = BN_num_bytes(key->n);
61 if (blen < n+1+4)
62 return -1;
63
64 tmp = new unsigned char [n];
65 BN_bn2bin(key->n, tmp);
66 h = (tmp[0] & 0x80) ? 1 : 0;
67 *(unsigned int*)ptr = htonl(n+h);
68 ptr += 4; blen -= 4;
69 *ptr = 0;
70 memcpy(ptr+h, tmp, n);
71 ptr += n; blen -= n;
72 ptr += h; blen -= h;
73 delete [] tmp;
74
75 return ptr-buf;
76}
77
78
79int main(int argc, char **argv)
80{
81
82 if (argc < 3) {
83 fprintf(stderr, "Usage: %s <bits> <privfile> <pubfile>\n",
84 *argv);
85 return -1;
86 }
87
88 RSA *r;
89 int bits = atoi(argv[1]);
90 r = RSA_generate_key(bits, 35, NULL, NULL);
91
92 FILE *f = fopen(argv[2], "w");
93 if (!f)
94 die("fopen");
95
96 PEM_write_RSAPrivateKey(f, r, NULL, NULL, 0, NULL, NULL);
97 fclose(f);
98
99 unsigned char key_string[1024];
100 char uu_key_string[2049];
101 memset(uu_key_string, 0, sizeof(uu_key_string));
102 memset(key_string, 0, sizeof(key_string));
103
104 int n = rsa2blob(r, key_string, sizeof(key_string));
105 printf("%d\n", n);
106 if (n < 0)
107 die("Not enough memory to store key.");
108 b64_ntop(key_string, n, uu_key_string, 2*n);
109
110 f = fopen(argv[3], "w");
111 if (!f)
112 die("fopen");
113
114 fprintf(f, "ssh-rsa %s icke@dort\n", uu_key_string);
115 fclose(f);
116
117 return 0;
118}
119