comparison dropbearkey.c @ 73:0bf5cebe622c

Dropbearkey can now print out pubkey portions
author Matt Johnston <matt@ucc.asn.au>
date Sat, 14 Aug 2004 17:35:28 +0000
parents b0316ce64e4b
children c85c88500ea6
comparison
equal deleted inserted replaced
72:9597c2e3b9d4 73:0bf5cebe622c
52 #include "genrsa.h" 52 #include "genrsa.h"
53 #include "gendss.h" 53 #include "gendss.h"
54 54
55 static void printhelp(char * progname); 55 static void printhelp(char * progname);
56 56
57 #define BUF_SIZE 2000
58
59 #define RSA_SIZE (1024/8) /* 1024 bit */ 57 #define RSA_SIZE (1024/8) /* 1024 bit */
60 #define DSS_SIZE (1024/8) /* 1024 bit */ 58 #define DSS_SIZE (1024/8) /* 1024 bit */
61 59
62 static void buf_writefile(buffer * buf, const char * filename); 60 static void buf_writefile(buffer * buf, const char * filename);
61 static void printpubkey(sign_key * key, int keytype);
62 static void justprintpub(const char* filename);
63 63
64 /* Print a help message */ 64 /* Print a help message */
65 static void printhelp(char * progname) { 65 static void printhelp(char * progname) {
66 66
67 fprintf(stderr, "Usage: %s -t <type> -f <filename> [-s bits]\n" 67 fprintf(stderr, "Usage: %s -t <type> -f <filename> [-s bits]\n"
68 "Options are:\n" 68 "Options are:\n"
69 "-t type Type of key to generate. One of:\n" 69 "-t type Type of key to generate. One of:\n"
70 #ifdef DROPBEAR_RSA 70 #ifdef DROPBEAR_RSA
71 " rsa\n" 71 " rsa\n"
72 #endif 72 #endif
73 #ifdef DROPBEAR_DSS 73 #ifdef DROPBEAR_DSS
74 " dss\n" 74 " dss\n"
75 #endif 75 #endif
76 "-f filename Use filename for the secret key\n" 76 "-f filename Use filename for the secret key\n"
77 "-s bits Key size in bits, should be " 77 "-s bits Key size in bits, should be a multiple of 8 (optional)\n"
78 "multiple of 8 (optional)\n", 78 "-y Just print the publickey and fingerprint for the\n private key in <filename>.\n",
79 progname); 79 progname);
80 } 80 }
81 81
82 #if defined(DBMULTI_dropbearkey) || !defined(DROPBEAR_MULTI) 82 #if defined(DBMULTI_dropbearkey) || !defined(DROPBEAR_MULTI)
83 #if defined(DBMULTI_dropbearkey) && defined(DROPBEAR_MULTI) 83 #if defined(DBMULTI_dropbearkey) && defined(DROPBEAR_MULTI)
86 int main(int argc, char ** argv) { 86 int main(int argc, char ** argv) {
87 #endif 87 #endif
88 88
89 int i; 89 int i;
90 char ** next = 0; 90 char ** next = 0;
91 sign_key *key; 91 sign_key *key = NULL;
92 buffer *buf; 92 buffer *buf = NULL;
93 char * filename = NULL; 93 char * filename = NULL;
94 int keytype = -1; 94 int keytype = -1;
95 char * typetext = NULL; 95 char * typetext = NULL;
96 char * sizetext = NULL; 96 char * sizetext = NULL;
97 unsigned int bits; 97 unsigned int bits;
98 unsigned int keysize; 98 unsigned int keysize;
99 int printpub = 0;
99 100
100 /* get the commandline options */ 101 /* get the commandline options */
101 for (i = 1; i < argc; i++) { 102 for (i = 1; i < argc; i++) {
103 if (argv[i] == NULL) {
104 continue; /* Whack */
105 }
102 if (next) { 106 if (next) {
103 *next = argv[i]; 107 *next = argv[i];
104 if (*next == NULL) { 108 next = NULL;
105 fprintf(stderr, "Invalid null argument");
106 }
107 next = 0x00;
108 continue; 109 continue;
109 } 110 }
110 111
111 if (argv[i][0] == '-') { 112 if (argv[i][0] == '-') {
112 switch (argv[i][1]) { 113 switch (argv[i][1]) {
117 next = &typetext; 118 next = &typetext;
118 break; 119 break;
119 case 's': 120 case 's':
120 next = &sizetext; 121 next = &sizetext;
121 break; 122 break;
123 case 'y':
124 printpub = 1;
125 break;
122 case 'h': 126 case 'h':
123 printhelp(argv[0]); 127 printhelp(argv[0]);
124 exit(EXIT_SUCCESS); 128 exit(EXIT_SUCCESS);
125 break; 129 break;
126 default: 130 default:
130 break; 134 break;
131 } 135 }
132 } 136 }
133 } 137 }
134 138
139 if (!filename) {
140 fprintf(stderr, "Must specify a key filename\n");
141 printhelp(argv[0]);
142 exit(EXIT_FAILURE);
143 }
144
145 if (printpub) {
146 justprintpub(filename);
147 /* Not reached */
148 }
149
135 /* check/parse args */ 150 /* check/parse args */
136 if (!typetext) { 151 if (!typetext) {
137 fprintf(stderr, "Must specify file type, one of:\n" 152 fprintf(stderr, "Must specify key type\n");
138 #ifdef DROPBEAR_RSA
139 "rsa\n"
140 #endif
141 #ifdef DROPBEAR_DSS
142 "dss\n"
143 #endif
144 "\n"
145 );
146 printhelp(argv[0]); 153 printhelp(argv[0]);
147 exit(EXIT_FAILURE); 154 exit(EXIT_FAILURE);
148 } 155 }
149 156
150 if (strlen(typetext) == 3) { 157 if (strlen(typetext) == 3) {
188 } else { 195 } else {
189 exit(EXIT_FAILURE); /* not reached */ 196 exit(EXIT_FAILURE); /* not reached */
190 } 197 }
191 } 198 }
192 199
193 if (!filename) {
194 fprintf(stderr, "Must specify a key filename\n");
195 printhelp(argv[0]);
196 exit(EXIT_FAILURE);
197 }
198 200
199 fprintf(stderr, "Will output %d bit %s secret key to '%s'\n", keysize*8, 201 fprintf(stderr, "Will output %d bit %s secret key to '%s'\n", keysize*8,
200 typetext, filename); 202 typetext, filename);
201 203
202 /* don't want the file readable by others */ 204 /* don't want the file readable by others */
220 default: 222 default:
221 fprintf(stderr, "Internal error, bad key type\n"); 223 fprintf(stderr, "Internal error, bad key type\n");
222 exit(EXIT_FAILURE); 224 exit(EXIT_FAILURE);
223 } 225 }
224 226
225 buf = buf_new(BUF_SIZE); 227 buf = buf_new(MAX_PRIVKEY_SIZE);
226 228
227 buf_put_priv_key(buf, key, keytype); 229 buf_put_priv_key(buf, key, keytype);
228 buf_setpos(buf, 0); 230 buf_setpos(buf, 0);
229 buf_writefile(buf, filename); 231 buf_writefile(buf, filename);
230 232
231 buf_burn(buf); 233 buf_burn(buf);
232 buf_free(buf); 234 buf_free(buf);
235
236 printpubkey(key, keytype);
237
233 sign_key_free(key); 238 sign_key_free(key);
234 239
235 fprintf(stderr, "Done.\n");
236
237 return EXIT_SUCCESS; 240 return EXIT_SUCCESS;
238 } 241 }
239 #endif 242 #endif
243
244 static void justprintpub(const char* filename) {
245
246 buffer *buf = NULL;
247 sign_key *key = NULL;
248 int keytype;
249 int ret;
250 int err = DROPBEAR_FAILURE;
251
252 buf = buf_new(MAX_PRIVKEY_SIZE);
253 ret = buf_readfile(buf, filename);
254
255 if (ret != DROPBEAR_SUCCESS) {
256 fprintf(stderr, "Failed reading '%s'\n", filename);
257 goto out;
258 }
259
260 key = new_sign_key();
261 keytype = DROPBEAR_SIGNKEY_ANY;
262
263 buf_setpos(buf, 0);
264 ret = buf_get_priv_key(buf, key, &keytype);
265 if (ret == DROPBEAR_FAILURE) {
266 fprintf(stderr, "Bad key in '%s'\n", filename);
267 goto out;
268 }
269
270 printpubkey(key, keytype);
271
272 err = DROPBEAR_SUCCESS;
273
274 out:
275 buf_burn(buf);
276 buf_free(buf);
277 buf = NULL;
278 sign_key_free(key);
279 key = NULL;
280 exit(err);
281 }
282
283 static void printpubkey(sign_key * key, int keytype) {
284
285 buffer * buf = NULL;
286 unsigned char base64key[MAX_PUBKEY_SIZE*2];
287 unsigned long base64len;
288 int err;
289 const char * typestring = NULL;
290 char *fp = NULL;
291 int len;
292
293 buf = buf_new(MAX_PUBKEY_SIZE);
294 buf_put_pub_key(buf, key, keytype);
295 buf_setpos(buf, 4);
296
297 len = buf->len - buf->pos;
298
299 base64len = sizeof(base64key);
300 err = base64_encode(buf_getptr(buf, len), len, base64key, &base64len);
301
302 if (err != CRYPT_OK) {
303 fprintf(stderr, "base64 failed");
304 }
305
306 typestring = signkey_name_from_type(keytype, &err);
307
308 fp = sign_key_fingerprint(buf_getptr(buf, len), len);
309
310 printf("Public key portion is:\n%s %s\nFingerprint: %s\n",
311 typestring, base64key, fp);
312
313 m_free(fp);
314 buf_free(buf);
315 }
240 316
241 /* Write a buffer to a file specified, failing if the file exists */ 317 /* Write a buffer to a file specified, failing if the file exists */
242 static void buf_writefile(buffer * buf, const char * filename) { 318 static void buf_writefile(buffer * buf, const char * filename) {
243 319
244 int fd; 320 int fd;