Mercurial > dropbear
comparison dropbearkey.c @ 299:740e782679be ucc-axis-hack
Various changes to compile+kind of run on UCC's axis board.
Note that fprintf(stdin -> printf( accounts for many of the changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 25 Mar 2006 12:57:09 +0000 |
parents | 0cfba3034be5 |
children |
comparison
equal
deleted
inserted
replaced
266:e37b160c414c | 299:740e782679be |
---|---|
62 static void justprintpub(const char* filename); | 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 printf( "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 |
134 case 'v': | 134 case 'v': |
135 debug_trace = 1; | 135 debug_trace = 1; |
136 break; | 136 break; |
137 #endif | 137 #endif |
138 default: | 138 default: |
139 fprintf(stderr, "Unknown argument %s\n", argv[i]); | 139 printf( "Unknown argument %s\n", argv[i]); |
140 printhelp(argv[0]); | 140 printhelp(argv[0]); |
141 exit(EXIT_FAILURE); | 141 exit(EXIT_FAILURE); |
142 break; | 142 break; |
143 } | 143 } |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 if (!filename) { | 147 if (!filename) { |
148 fprintf(stderr, "Must specify a key filename\n"); | 148 printf( "Must specify a key filename\n"); |
149 printhelp(argv[0]); | 149 printhelp(argv[0]); |
150 exit(EXIT_FAILURE); | 150 exit(EXIT_FAILURE); |
151 } | 151 } |
152 | 152 |
153 if (printpub) { | 153 if (printpub) { |
155 /* Not reached */ | 155 /* Not reached */ |
156 } | 156 } |
157 | 157 |
158 /* check/parse args */ | 158 /* check/parse args */ |
159 if (!typetext) { | 159 if (!typetext) { |
160 fprintf(stderr, "Must specify key type\n"); | 160 printf( "Must specify key type\n"); |
161 printhelp(argv[0]); | 161 printhelp(argv[0]); |
162 exit(EXIT_FAILURE); | 162 exit(EXIT_FAILURE); |
163 } | 163 } |
164 | 164 |
165 if (strlen(typetext) == 3) { | 165 if (strlen(typetext) == 3) { |
175 TRACE(("type is dss")) | 175 TRACE(("type is dss")) |
176 } | 176 } |
177 #endif | 177 #endif |
178 } | 178 } |
179 if (keytype == -1) { | 179 if (keytype == -1) { |
180 fprintf(stderr, "Unknown key type '%s'\n", typetext); | 180 printf( "Unknown key type '%s'\n", typetext); |
181 printhelp(argv[0]); | 181 printhelp(argv[0]); |
182 exit(EXIT_FAILURE); | 182 exit(EXIT_FAILURE); |
183 } | 183 } |
184 | 184 |
185 if (sizetext) { | 185 if (sizetext) { |
186 if (sscanf(sizetext, "%u", &bits) != 1) { | 186 if (sscanf(sizetext, "%u", &bits) != 1) { |
187 fprintf(stderr, "Bits must be an integer\n"); | 187 printf( "Bits must be an integer\n"); |
188 exit(EXIT_FAILURE); | 188 exit(EXIT_FAILURE); |
189 } | 189 } |
190 | 190 |
191 if (bits < 512 || bits > 4096 || (bits % 8 != 0)) { | 191 if (bits < 512 || bits > 4096 || (bits % 8 != 0)) { |
192 fprintf(stderr, "Bits must satisfy 512 <= bits <= 4096, and be a" | 192 printf( "Bits must satisfy 512 <= bits <= 4096, and be a" |
193 " multiple of 8\n"); | 193 " multiple of 8\n"); |
194 exit(EXIT_FAILURE); | 194 exit(EXIT_FAILURE); |
195 } | 195 } |
196 | 196 |
197 keysize = bits / 8; | 197 keysize = bits / 8; |
204 exit(EXIT_FAILURE); /* not reached */ | 204 exit(EXIT_FAILURE); /* not reached */ |
205 } | 205 } |
206 } | 206 } |
207 | 207 |
208 | 208 |
209 fprintf(stderr, "Will output %d bit %s secret key to '%s'\n", keysize*8, | 209 printf( "Will output %d bit %s secret key to '%s'\n", keysize*8, |
210 typetext, filename); | 210 typetext, filename); |
211 | 211 |
212 /* don't want the file readable by others */ | 212 /* don't want the file readable by others */ |
213 umask(077); | 213 umask(077); |
214 | 214 |
215 /* now we can generate the key */ | 215 /* now we can generate the key */ |
216 key = new_sign_key(); | 216 key = new_sign_key(); |
217 | 217 |
218 fprintf(stderr, "Generating key, this may take a while...\n"); | 218 printf( "Generating key, this may take a while...\n"); |
219 switch(keytype) { | 219 switch(keytype) { |
220 #ifdef DROPBEAR_RSA | 220 #ifdef DROPBEAR_RSA |
221 case DROPBEAR_SIGNKEY_RSA: | 221 case DROPBEAR_SIGNKEY_RSA: |
222 key->rsakey = gen_rsa_priv_key(keysize); /* 128 bytes = 1024 bit */ | 222 key->rsakey = gen_rsa_priv_key(keysize); /* 128 bytes = 1024 bit */ |
223 break; | 223 break; |
226 case DROPBEAR_SIGNKEY_DSS: | 226 case DROPBEAR_SIGNKEY_DSS: |
227 key->dsskey = gen_dss_priv_key(keysize); /* 128 bytes = 1024 bit */ | 227 key->dsskey = gen_dss_priv_key(keysize); /* 128 bytes = 1024 bit */ |
228 break; | 228 break; |
229 #endif | 229 #endif |
230 default: | 230 default: |
231 fprintf(stderr, "Internal error, bad key type\n"); | 231 printf( "Internal error, bad key type\n"); |
232 exit(EXIT_FAILURE); | 232 exit(EXIT_FAILURE); |
233 } | 233 } |
234 | 234 |
235 buf = buf_new(MAX_PRIVKEY_SIZE); | 235 buf = buf_new(MAX_PRIVKEY_SIZE); |
236 | 236 |
259 | 259 |
260 buf = buf_new(MAX_PRIVKEY_SIZE); | 260 buf = buf_new(MAX_PRIVKEY_SIZE); |
261 ret = buf_readfile(buf, filename); | 261 ret = buf_readfile(buf, filename); |
262 | 262 |
263 if (ret != DROPBEAR_SUCCESS) { | 263 if (ret != DROPBEAR_SUCCESS) { |
264 fprintf(stderr, "Failed reading '%s'\n", filename); | 264 printf( "Failed reading '%s'\n", filename); |
265 goto out; | 265 goto out; |
266 } | 266 } |
267 | 267 |
268 key = new_sign_key(); | 268 key = new_sign_key(); |
269 keytype = DROPBEAR_SIGNKEY_ANY; | 269 keytype = DROPBEAR_SIGNKEY_ANY; |
270 | 270 |
271 buf_setpos(buf, 0); | 271 buf_setpos(buf, 0); |
272 ret = buf_get_priv_key(buf, key, &keytype); | 272 ret = buf_get_priv_key(buf, key, &keytype); |
273 if (ret == DROPBEAR_FAILURE) { | 273 if (ret == DROPBEAR_FAILURE) { |
274 fprintf(stderr, "Bad key in '%s'\n", filename); | 274 printf( "Bad key in '%s'\n", filename); |
275 goto out; | 275 goto out; |
276 } | 276 } |
277 | 277 |
278 printpubkey(key, keytype); | 278 printpubkey(key, keytype); |
279 | 279 |
306 | 306 |
307 base64len = sizeof(base64key); | 307 base64len = sizeof(base64key); |
308 err = base64_encode(buf_getptr(buf, len), len, base64key, &base64len); | 308 err = base64_encode(buf_getptr(buf, len), len, base64key, &base64len); |
309 | 309 |
310 if (err != CRYPT_OK) { | 310 if (err != CRYPT_OK) { |
311 fprintf(stderr, "base64 failed"); | 311 printf( "base64 failed"); |
312 } | 312 } |
313 | 313 |
314 typestring = signkey_name_from_type(keytype, &err); | 314 typestring = signkey_name_from_type(keytype, &err); |
315 | 315 |
316 fp = sign_key_fingerprint(buf_getptr(buf, len), len); | 316 fp = sign_key_fingerprint(buf_getptr(buf, len), len); |
328 int fd; | 328 int fd; |
329 int len; | 329 int len; |
330 | 330 |
331 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); | 331 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); |
332 if (fd < 0) { | 332 if (fd < 0) { |
333 fprintf(stderr, "Couldn't create new file %s\n", filename); | 333 printf( "Couldn't create new file %s\n", filename); |
334 perror("Reason"); | 334 perror("Reason"); |
335 buf_burn(buf); | 335 buf_burn(buf); |
336 exit(EXIT_FAILURE); | 336 exit(EXIT_FAILURE); |
337 } | 337 } |
338 | 338 |
342 buf->len - buf->pos); | 342 buf->len - buf->pos); |
343 if (errno == EINTR) { | 343 if (errno == EINTR) { |
344 continue; | 344 continue; |
345 } | 345 } |
346 if (len <= 0) { | 346 if (len <= 0) { |
347 fprintf(stderr, "Failed writing file '%s'\n",filename); | 347 printf( "Failed writing file '%s'\n",filename); |
348 perror("Reason"); | 348 perror("Reason"); |
349 exit(EXIT_FAILURE); | 349 exit(EXIT_FAILURE); |
350 } | 350 } |
351 buf_incrpos(buf, len); | 351 buf_incrpos(buf, len); |
352 } | 352 } |