comparison dropbearkey.c @ 118:5312ca05ed48 private-rez

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