]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | |
3 | * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
5 | * Identity and host key generation and maintenance. | |
6 | * | |
7 | * As far as I am concerned, the code I have written for this software | |
8 | * can be used freely for any purpose. Any derived versions of this | |
9 | * software must be clearly marked as such, and if the derived work is | |
10 | * incompatible with the protocol description in the RFC file, it must be | |
11 | * called by a name other than "ssh" or "Secure Shell". | |
12 | */ | |
13 | ||
14 | #include "includes.h" | |
e9a17296 | 15 | RCSID("$OpenBSD: ssh-keygen.c,v 1.94 2002/02/25 16:33:27 markus Exp $"); |
3c0ef626 | 16 | |
17 | #include <openssl/evp.h> | |
18 | #include <openssl/pem.h> | |
19 | ||
20 | #include "xmalloc.h" | |
21 | #include "key.h" | |
22 | #include "rsa.h" | |
23 | #include "authfile.h" | |
24 | #include "uuencode.h" | |
25 | #include "buffer.h" | |
26 | #include "bufaux.h" | |
27 | #include "pathnames.h" | |
28 | #include "log.h" | |
29 | #include "readpass.h" | |
30 | ||
31 | #ifdef SMARTCARD | |
32 | #include <sectok.h> | |
33 | #include <openssl/engine.h> | |
34 | #include "scard.h" | |
35 | #endif | |
36 | ||
37 | /* Number of bits in the RSA/DSA key. This value can be changed on the command line. */ | |
38 | int bits = 1024; | |
39 | ||
40 | /* | |
41 | * Flag indicating that we just want to change the passphrase. This can be | |
42 | * set on the command line. | |
43 | */ | |
44 | int change_passphrase = 0; | |
45 | ||
46 | /* | |
47 | * Flag indicating that we just want to change the comment. This can be set | |
48 | * on the command line. | |
49 | */ | |
50 | int change_comment = 0; | |
51 | ||
52 | int quiet = 0; | |
53 | ||
54 | /* Flag indicating that we just want to see the key fingerprint */ | |
55 | int print_fingerprint = 0; | |
56 | int print_bubblebabble = 0; | |
57 | ||
58 | /* The identity file name, given on the command line or entered by the user. */ | |
59 | char identity_file[1024]; | |
60 | int have_identity = 0; | |
61 | ||
62 | /* This is set to the passphrase if given on the command line. */ | |
63 | char *identity_passphrase = NULL; | |
64 | ||
65 | /* This is set to the new passphrase if given on the command line. */ | |
66 | char *identity_new_passphrase = NULL; | |
67 | ||
68 | /* This is set to the new comment if given on the command line. */ | |
69 | char *identity_comment = NULL; | |
70 | ||
71 | /* Dump public key file in format used by real and the original SSH 2 */ | |
72 | int convert_to_ssh2 = 0; | |
73 | int convert_from_ssh2 = 0; | |
74 | int print_public = 0; | |
75 | ||
e9a17296 | 76 | char *key_type_name = NULL; |
3c0ef626 | 77 | |
78 | /* argv0 */ | |
79 | #ifdef HAVE___PROGNAME | |
80 | extern char *__progname; | |
81 | #else | |
82 | char *__progname; | |
83 | #endif | |
84 | ||
85 | char hostname[MAXHOSTNAMELEN]; | |
86 | ||
87 | static void | |
88 | ask_filename(struct passwd *pw, const char *prompt) | |
89 | { | |
90 | char buf[1024]; | |
91 | char *name = NULL; | |
92 | ||
e9a17296 | 93 | if (key_type_name == NULL) |
3c0ef626 | 94 | name = _PATH_SSH_CLIENT_ID_RSA; |
e9a17296 | 95 | else |
96 | switch (key_type_from_name(key_type_name)) { | |
97 | case KEY_RSA1: | |
98 | name = _PATH_SSH_CLIENT_IDENTITY; | |
99 | break; | |
100 | case KEY_DSA: | |
101 | name = _PATH_SSH_CLIENT_ID_DSA; | |
102 | break; | |
103 | case KEY_RSA: | |
104 | name = _PATH_SSH_CLIENT_ID_RSA; | |
105 | break; | |
106 | default: | |
107 | fprintf(stderr, "bad key type"); | |
108 | exit(1); | |
109 | break; | |
110 | } | |
111 | ||
3c0ef626 | 112 | snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); |
113 | fprintf(stderr, "%s (%s): ", prompt, identity_file); | |
114 | fflush(stderr); | |
115 | if (fgets(buf, sizeof(buf), stdin) == NULL) | |
116 | exit(1); | |
117 | if (strchr(buf, '\n')) | |
118 | *strchr(buf, '\n') = 0; | |
119 | if (strcmp(buf, "") != 0) | |
120 | strlcpy(identity_file, buf, sizeof(identity_file)); | |
121 | have_identity = 1; | |
122 | } | |
123 | ||
124 | static Key * | |
125 | load_identity(char *filename) | |
126 | { | |
127 | char *pass; | |
128 | Key *prv; | |
129 | ||
130 | prv = key_load_private(filename, "", NULL); | |
131 | if (prv == NULL) { | |
132 | if (identity_passphrase) | |
133 | pass = xstrdup(identity_passphrase); | |
134 | else | |
135 | pass = read_passphrase("Enter passphrase: ", | |
136 | RP_ALLOW_STDIN); | |
137 | prv = key_load_private(filename, pass, NULL); | |
138 | memset(pass, 0, strlen(pass)); | |
139 | xfree(pass); | |
140 | } | |
141 | return prv; | |
142 | } | |
143 | ||
144 | #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" | |
145 | #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----" | |
146 | #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----" | |
147 | #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb | |
148 | ||
149 | static void | |
150 | do_convert_to_ssh2(struct passwd *pw) | |
151 | { | |
152 | Key *k; | |
e9a17296 | 153 | u_int len; |
3c0ef626 | 154 | u_char *blob; |
155 | struct stat st; | |
156 | ||
157 | if (!have_identity) | |
158 | ask_filename(pw, "Enter file in which the key is"); | |
159 | if (stat(identity_file, &st) < 0) { | |
160 | perror(identity_file); | |
161 | exit(1); | |
162 | } | |
163 | if ((k = key_load_public(identity_file, NULL)) == NULL) { | |
164 | if ((k = load_identity(identity_file)) == NULL) { | |
165 | fprintf(stderr, "load failed\n"); | |
166 | exit(1); | |
167 | } | |
168 | } | |
169 | if (key_to_blob(k, &blob, &len) <= 0) { | |
170 | fprintf(stderr, "key_to_blob failed\n"); | |
171 | exit(1); | |
172 | } | |
173 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); | |
174 | fprintf(stdout, | |
175 | "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n", | |
176 | key_size(k), key_type(k), | |
177 | pw->pw_name, hostname); | |
178 | dump_base64(stdout, blob, len); | |
179 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); | |
180 | key_free(k); | |
181 | xfree(blob); | |
182 | exit(0); | |
183 | } | |
184 | ||
185 | static void | |
186 | buffer_get_bignum_bits(Buffer *b, BIGNUM *value) | |
187 | { | |
188 | int bits = buffer_get_int(b); | |
189 | int bytes = (bits + 7) / 8; | |
190 | ||
191 | if (buffer_len(b) < bytes) | |
192 | fatal("buffer_get_bignum_bits: input buffer too small: " | |
193 | "need %d have %d", bytes, buffer_len(b)); | |
e9a17296 | 194 | BN_bin2bn(buffer_ptr(b), bytes, value); |
3c0ef626 | 195 | buffer_consume(b, bytes); |
196 | } | |
197 | ||
198 | static Key * | |
e9a17296 | 199 | do_convert_private_ssh2_from_blob(u_char *blob, u_int blen) |
3c0ef626 | 200 | { |
201 | Buffer b; | |
202 | Key *key = NULL; | |
203 | char *type, *cipher; | |
204 | u_char *sig, data[] = "abcde12345"; | |
205 | int magic, rlen, ktype, i1, i2, i3, i4; | |
206 | u_int slen; | |
207 | u_long e; | |
208 | ||
209 | buffer_init(&b); | |
210 | buffer_append(&b, blob, blen); | |
211 | ||
212 | magic = buffer_get_int(&b); | |
213 | if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { | |
214 | error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC); | |
215 | buffer_free(&b); | |
216 | return NULL; | |
217 | } | |
218 | i1 = buffer_get_int(&b); | |
219 | type = buffer_get_string(&b, NULL); | |
220 | cipher = buffer_get_string(&b, NULL); | |
221 | i2 = buffer_get_int(&b); | |
222 | i3 = buffer_get_int(&b); | |
223 | i4 = buffer_get_int(&b); | |
224 | debug("ignore (%d %d %d %d)", i1,i2,i3,i4); | |
225 | if (strcmp(cipher, "none") != 0) { | |
226 | error("unsupported cipher %s", cipher); | |
227 | xfree(cipher); | |
228 | buffer_free(&b); | |
229 | xfree(type); | |
230 | return NULL; | |
231 | } | |
232 | xfree(cipher); | |
233 | ||
234 | if (strstr(type, "dsa")) { | |
235 | ktype = KEY_DSA; | |
236 | } else if (strstr(type, "rsa")) { | |
237 | ktype = KEY_RSA; | |
238 | } else { | |
239 | xfree(type); | |
240 | return NULL; | |
241 | } | |
242 | key = key_new_private(ktype); | |
243 | xfree(type); | |
244 | ||
245 | switch (key->type) { | |
246 | case KEY_DSA: | |
247 | buffer_get_bignum_bits(&b, key->dsa->p); | |
248 | buffer_get_bignum_bits(&b, key->dsa->g); | |
249 | buffer_get_bignum_bits(&b, key->dsa->q); | |
250 | buffer_get_bignum_bits(&b, key->dsa->pub_key); | |
251 | buffer_get_bignum_bits(&b, key->dsa->priv_key); | |
252 | break; | |
253 | case KEY_RSA: | |
254 | e = buffer_get_char(&b); | |
255 | debug("e %lx", e); | |
256 | if (e < 30) { | |
257 | e <<= 8; | |
258 | e += buffer_get_char(&b); | |
259 | debug("e %lx", e); | |
260 | e <<= 8; | |
261 | e += buffer_get_char(&b); | |
262 | debug("e %lx", e); | |
263 | } | |
264 | if (!BN_set_word(key->rsa->e, e)) { | |
265 | buffer_free(&b); | |
266 | key_free(key); | |
267 | return NULL; | |
268 | } | |
269 | buffer_get_bignum_bits(&b, key->rsa->d); | |
270 | buffer_get_bignum_bits(&b, key->rsa->n); | |
271 | buffer_get_bignum_bits(&b, key->rsa->iqmp); | |
272 | buffer_get_bignum_bits(&b, key->rsa->q); | |
273 | buffer_get_bignum_bits(&b, key->rsa->p); | |
274 | rsa_generate_additional_parameters(key->rsa); | |
275 | break; | |
276 | } | |
277 | rlen = buffer_len(&b); | |
e9a17296 | 278 | if (rlen != 0) |
3c0ef626 | 279 | error("do_convert_private_ssh2_from_blob: " |
280 | "remaining bytes in key blob %d", rlen); | |
281 | buffer_free(&b); | |
282 | ||
283 | /* try the key */ | |
284 | key_sign(key, &sig, &slen, data, sizeof(data)); | |
285 | key_verify(key, sig, slen, data, sizeof(data)); | |
286 | xfree(sig); | |
287 | return key; | |
288 | } | |
289 | ||
290 | static void | |
291 | do_convert_from_ssh2(struct passwd *pw) | |
292 | { | |
293 | Key *k; | |
294 | int blen; | |
295 | char line[1024], *p; | |
296 | u_char blob[8096]; | |
297 | char encoded[8096]; | |
298 | struct stat st; | |
299 | int escaped = 0, private = 0, ok; | |
300 | FILE *fp; | |
301 | ||
302 | if (!have_identity) | |
303 | ask_filename(pw, "Enter file in which the key is"); | |
304 | if (stat(identity_file, &st) < 0) { | |
305 | perror(identity_file); | |
306 | exit(1); | |
307 | } | |
308 | fp = fopen(identity_file, "r"); | |
309 | if (fp == NULL) { | |
310 | perror(identity_file); | |
311 | exit(1); | |
312 | } | |
313 | encoded[0] = '\0'; | |
314 | while (fgets(line, sizeof(line), fp)) { | |
315 | if (!(p = strchr(line, '\n'))) { | |
316 | fprintf(stderr, "input line too long.\n"); | |
317 | exit(1); | |
318 | } | |
319 | if (p > line && p[-1] == '\\') | |
320 | escaped++; | |
321 | if (strncmp(line, "----", 4) == 0 || | |
322 | strstr(line, ": ") != NULL) { | |
323 | if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL) | |
324 | private = 1; | |
325 | if (strstr(line, " END ") != NULL) { | |
326 | break; | |
327 | } | |
328 | /* fprintf(stderr, "ignore: %s", line); */ | |
329 | continue; | |
330 | } | |
331 | if (escaped) { | |
332 | escaped--; | |
333 | /* fprintf(stderr, "escaped: %s", line); */ | |
334 | continue; | |
335 | } | |
336 | *p = '\0'; | |
337 | strlcat(encoded, line, sizeof(encoded)); | |
338 | } | |
e9a17296 | 339 | blen = uudecode(encoded, blob, sizeof(blob)); |
3c0ef626 | 340 | if (blen < 0) { |
341 | fprintf(stderr, "uudecode failed.\n"); | |
342 | exit(1); | |
343 | } | |
344 | k = private ? | |
345 | do_convert_private_ssh2_from_blob(blob, blen) : | |
346 | key_from_blob(blob, blen); | |
347 | if (k == NULL) { | |
348 | fprintf(stderr, "decode blob failed.\n"); | |
349 | exit(1); | |
350 | } | |
351 | ok = private ? | |
352 | (k->type == KEY_DSA ? | |
353 | PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) : | |
354 | PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) : | |
355 | key_write(k, stdout); | |
356 | if (!ok) { | |
357 | fprintf(stderr, "key write failed"); | |
358 | exit(1); | |
359 | } | |
360 | key_free(k); | |
e9a17296 | 361 | if (!private) |
362 | fprintf(stdout, "\n"); | |
3c0ef626 | 363 | fclose(fp); |
364 | exit(0); | |
365 | } | |
366 | ||
367 | static void | |
368 | do_print_public(struct passwd *pw) | |
369 | { | |
370 | Key *prv; | |
371 | struct stat st; | |
372 | ||
373 | if (!have_identity) | |
374 | ask_filename(pw, "Enter file in which the key is"); | |
375 | if (stat(identity_file, &st) < 0) { | |
376 | perror(identity_file); | |
377 | exit(1); | |
378 | } | |
379 | prv = load_identity(identity_file); | |
380 | if (prv == NULL) { | |
381 | fprintf(stderr, "load failed\n"); | |
382 | exit(1); | |
383 | } | |
384 | if (!key_write(prv, stdout)) | |
385 | fprintf(stderr, "key_write failed"); | |
386 | key_free(prv); | |
387 | fprintf(stdout, "\n"); | |
388 | exit(0); | |
389 | } | |
390 | ||
391 | #ifdef SMARTCARD | |
392 | #define NUM_RSA_KEY_ELEMENTS 5+1 | |
393 | #define COPY_RSA_KEY(x, i) \ | |
394 | do { \ | |
395 | len = BN_num_bytes(prv->rsa->x); \ | |
396 | elements[i] = xmalloc(len); \ | |
397 | debug("#bytes %d", len); \ | |
398 | if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \ | |
399 | goto done; \ | |
e9a17296 | 400 | } while (0) |
3c0ef626 | 401 | |
402 | static int | |
403 | get_AUT0(char *aut0) | |
404 | { | |
405 | EVP_MD *evp_md = EVP_sha1(); | |
406 | EVP_MD_CTX md; | |
407 | char *pass; | |
408 | ||
409 | pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN); | |
410 | if (pass == NULL) | |
411 | return -1; | |
412 | EVP_DigestInit(&md, evp_md); | |
413 | EVP_DigestUpdate(&md, pass, strlen(pass)); | |
414 | EVP_DigestFinal(&md, aut0, NULL); | |
415 | memset(pass, 0, strlen(pass)); | |
416 | xfree(pass); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | static void | |
421 | do_upload(struct passwd *pw, const char *sc_reader_id) | |
422 | { | |
423 | Key *prv = NULL; | |
424 | struct stat st; | |
425 | u_char *elements[NUM_RSA_KEY_ELEMENTS]; | |
426 | u_char key_fid[2]; | |
427 | u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63}; | |
428 | u_char AUT0[EVP_MAX_MD_SIZE]; | |
429 | int len, status = 1, i, fd = -1, ret; | |
430 | int sw = 0, cla = 0x00; | |
431 | ||
432 | for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++) | |
433 | elements[i] = NULL; | |
434 | if (!have_identity) | |
435 | ask_filename(pw, "Enter file in which the key is"); | |
436 | if (stat(identity_file, &st) < 0) { | |
437 | perror(identity_file); | |
438 | goto done; | |
439 | } | |
440 | prv = load_identity(identity_file); | |
441 | if (prv == NULL) { | |
442 | error("load failed"); | |
443 | goto done; | |
444 | } | |
445 | COPY_RSA_KEY(q, 0); | |
446 | COPY_RSA_KEY(p, 1); | |
447 | COPY_RSA_KEY(iqmp, 2); | |
448 | COPY_RSA_KEY(dmq1, 3); | |
449 | COPY_RSA_KEY(dmp1, 4); | |
450 | COPY_RSA_KEY(n, 5); | |
451 | len = BN_num_bytes(prv->rsa->n); | |
452 | fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw); | |
453 | if (fd < 0) { | |
454 | error("sectok_open failed: %s", sectok_get_sw(sw)); | |
455 | goto done; | |
456 | } | |
457 | if (! sectok_cardpresent(fd)) { | |
458 | error("smartcard in reader %s not present", | |
459 | sc_reader_id); | |
460 | goto done; | |
461 | } | |
462 | ret = sectok_reset(fd, 0, NULL, &sw); | |
463 | if (ret <= 0) { | |
464 | error("sectok_reset failed: %s", sectok_get_sw(sw)); | |
465 | goto done; | |
466 | } | |
467 | if ((cla = cyberflex_inq_class(fd)) < 0) { | |
468 | error("cyberflex_inq_class failed"); | |
469 | goto done; | |
470 | } | |
471 | memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0)); | |
472 | if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) { | |
473 | if (get_AUT0(AUT0) < 0 || | |
474 | cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) { | |
475 | error("cyberflex_verify_AUT0 failed"); | |
476 | goto done; | |
477 | } | |
478 | } | |
479 | key_fid[0] = 0x00; | |
480 | key_fid[1] = 0x12; | |
481 | if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements, | |
482 | &sw) < 0) { | |
483 | error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw)); | |
484 | goto done; | |
485 | } | |
486 | if (!sectok_swOK(sw)) | |
487 | goto done; | |
488 | log("cyberflex_load_rsa_priv done"); | |
489 | key_fid[0] = 0x73; | |
490 | key_fid[1] = 0x68; | |
491 | if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5], | |
492 | &sw) < 0) { | |
493 | error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw)); | |
494 | goto done; | |
495 | } | |
496 | if (!sectok_swOK(sw)) | |
497 | goto done; | |
498 | log("cyberflex_load_rsa_pub done"); | |
499 | status = 0; | |
500 | log("loading key done"); | |
501 | done: | |
502 | ||
503 | memset(elements[0], '\0', BN_num_bytes(prv->rsa->q)); | |
504 | memset(elements[1], '\0', BN_num_bytes(prv->rsa->p)); | |
505 | memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp)); | |
506 | memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1)); | |
507 | memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1)); | |
508 | memset(elements[5], '\0', BN_num_bytes(prv->rsa->n)); | |
509 | ||
510 | if (prv) | |
511 | key_free(prv); | |
512 | for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++) | |
513 | if (elements[i]) | |
514 | xfree(elements[i]); | |
515 | if (fd != -1) | |
516 | sectok_close(fd); | |
517 | exit(status); | |
518 | } | |
519 | ||
520 | static void | |
521 | do_download(struct passwd *pw, const char *sc_reader_id) | |
522 | { | |
523 | Key *pub = NULL; | |
524 | ||
525 | pub = sc_get_key(sc_reader_id); | |
526 | if (pub == NULL) | |
527 | fatal("cannot read public key from smartcard"); | |
528 | key_write(pub, stdout); | |
529 | key_free(pub); | |
530 | fprintf(stdout, "\n"); | |
531 | exit(0); | |
532 | } | |
533 | #endif /* SMARTCARD */ | |
534 | ||
535 | static void | |
536 | do_fingerprint(struct passwd *pw) | |
537 | { | |
538 | FILE *f; | |
539 | Key *public; | |
540 | char *comment = NULL, *cp, *ep, line[16*1024], *fp; | |
e9a17296 | 541 | int i, skip = 0, num = 1, invalid = 1; |
542 | enum fp_rep rep; | |
543 | enum fp_type fptype; | |
3c0ef626 | 544 | struct stat st; |
545 | ||
546 | fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5; | |
547 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX; | |
548 | ||
549 | if (!have_identity) | |
550 | ask_filename(pw, "Enter file in which the key is"); | |
551 | if (stat(identity_file, &st) < 0) { | |
552 | perror(identity_file); | |
553 | exit(1); | |
554 | } | |
555 | public = key_load_public(identity_file, &comment); | |
556 | if (public != NULL) { | |
557 | fp = key_fingerprint(public, fptype, rep); | |
558 | printf("%d %s %s\n", key_size(public), fp, comment); | |
559 | key_free(public); | |
560 | xfree(comment); | |
561 | xfree(fp); | |
562 | exit(0); | |
563 | } | |
564 | if (comment) | |
565 | xfree(comment); | |
566 | ||
567 | f = fopen(identity_file, "r"); | |
568 | if (f != NULL) { | |
569 | while (fgets(line, sizeof(line), f)) { | |
570 | i = strlen(line) - 1; | |
571 | if (line[i] != '\n') { | |
572 | error("line %d too long: %.40s...", num, line); | |
573 | skip = 1; | |
574 | continue; | |
575 | } | |
576 | num++; | |
577 | if (skip) { | |
578 | skip = 0; | |
579 | continue; | |
580 | } | |
581 | line[i] = '\0'; | |
582 | ||
583 | /* Skip leading whitespace, empty and comment lines. */ | |
584 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
585 | ; | |
586 | if (!*cp || *cp == '\n' || *cp == '#') | |
587 | continue ; | |
588 | i = strtol(cp, &ep, 10); | |
589 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { | |
590 | int quoted = 0; | |
591 | comment = cp; | |
592 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
593 | if (*cp == '\\' && cp[1] == '"') | |
594 | cp++; /* Skip both */ | |
595 | else if (*cp == '"') | |
596 | quoted = !quoted; | |
597 | } | |
598 | if (!*cp) | |
599 | continue; | |
600 | *cp++ = '\0'; | |
601 | } | |
602 | ep = cp; | |
603 | public = key_new(KEY_RSA1); | |
604 | if (key_read(public, &cp) != 1) { | |
605 | cp = ep; | |
606 | key_free(public); | |
607 | public = key_new(KEY_UNSPEC); | |
608 | if (key_read(public, &cp) != 1) { | |
609 | key_free(public); | |
610 | continue; | |
611 | } | |
612 | } | |
613 | comment = *cp ? cp : comment; | |
614 | fp = key_fingerprint(public, fptype, rep); | |
615 | printf("%d %s %s\n", key_size(public), fp, | |
616 | comment ? comment : "no comment"); | |
617 | xfree(fp); | |
618 | key_free(public); | |
619 | invalid = 0; | |
620 | } | |
621 | fclose(f); | |
622 | } | |
623 | if (invalid) { | |
624 | printf("%s is not a public key file.\n", identity_file); | |
625 | exit(1); | |
626 | } | |
627 | exit(0); | |
628 | } | |
629 | ||
630 | /* | |
631 | * Perform changing a passphrase. The argument is the passwd structure | |
632 | * for the current user. | |
633 | */ | |
634 | static void | |
635 | do_change_passphrase(struct passwd *pw) | |
636 | { | |
637 | char *comment; | |
638 | char *old_passphrase, *passphrase1, *passphrase2; | |
639 | struct stat st; | |
640 | Key *private; | |
641 | ||
642 | if (!have_identity) | |
643 | ask_filename(pw, "Enter file in which the key is"); | |
644 | if (stat(identity_file, &st) < 0) { | |
645 | perror(identity_file); | |
646 | exit(1); | |
647 | } | |
648 | /* Try to load the file with empty passphrase. */ | |
649 | private = key_load_private(identity_file, "", &comment); | |
650 | if (private == NULL) { | |
651 | if (identity_passphrase) | |
652 | old_passphrase = xstrdup(identity_passphrase); | |
653 | else | |
654 | old_passphrase = | |
655 | read_passphrase("Enter old passphrase: ", | |
656 | RP_ALLOW_STDIN); | |
657 | private = key_load_private(identity_file, old_passphrase, | |
658 | &comment); | |
659 | memset(old_passphrase, 0, strlen(old_passphrase)); | |
660 | xfree(old_passphrase); | |
661 | if (private == NULL) { | |
662 | printf("Bad passphrase.\n"); | |
663 | exit(1); | |
664 | } | |
665 | } | |
666 | printf("Key has comment '%s'\n", comment); | |
667 | ||
668 | /* Ask the new passphrase (twice). */ | |
669 | if (identity_new_passphrase) { | |
670 | passphrase1 = xstrdup(identity_new_passphrase); | |
671 | passphrase2 = NULL; | |
672 | } else { | |
673 | passphrase1 = | |
674 | read_passphrase("Enter new passphrase (empty for no " | |
675 | "passphrase): ", RP_ALLOW_STDIN); | |
676 | passphrase2 = read_passphrase("Enter same passphrase again: ", | |
e9a17296 | 677 | RP_ALLOW_STDIN); |
3c0ef626 | 678 | |
679 | /* Verify that they are the same. */ | |
680 | if (strcmp(passphrase1, passphrase2) != 0) { | |
681 | memset(passphrase1, 0, strlen(passphrase1)); | |
682 | memset(passphrase2, 0, strlen(passphrase2)); | |
683 | xfree(passphrase1); | |
684 | xfree(passphrase2); | |
685 | printf("Pass phrases do not match. Try again.\n"); | |
686 | exit(1); | |
687 | } | |
688 | /* Destroy the other copy. */ | |
689 | memset(passphrase2, 0, strlen(passphrase2)); | |
690 | xfree(passphrase2); | |
691 | } | |
692 | ||
693 | /* Save the file using the new passphrase. */ | |
694 | if (!key_save_private(private, identity_file, passphrase1, comment)) { | |
695 | printf("Saving the key failed: %s.\n", identity_file); | |
696 | memset(passphrase1, 0, strlen(passphrase1)); | |
697 | xfree(passphrase1); | |
698 | key_free(private); | |
699 | xfree(comment); | |
700 | exit(1); | |
701 | } | |
702 | /* Destroy the passphrase and the copy of the key in memory. */ | |
703 | memset(passphrase1, 0, strlen(passphrase1)); | |
704 | xfree(passphrase1); | |
705 | key_free(private); /* Destroys contents */ | |
706 | xfree(comment); | |
707 | ||
708 | printf("Your identification has been saved with the new passphrase.\n"); | |
709 | exit(0); | |
710 | } | |
711 | ||
712 | /* | |
713 | * Change the comment of a private key file. | |
714 | */ | |
715 | static void | |
716 | do_change_comment(struct passwd *pw) | |
717 | { | |
718 | char new_comment[1024], *comment, *passphrase; | |
719 | Key *private; | |
720 | Key *public; | |
721 | struct stat st; | |
722 | FILE *f; | |
723 | int fd; | |
724 | ||
725 | if (!have_identity) | |
726 | ask_filename(pw, "Enter file in which the key is"); | |
727 | if (stat(identity_file, &st) < 0) { | |
728 | perror(identity_file); | |
729 | exit(1); | |
730 | } | |
731 | private = key_load_private(identity_file, "", &comment); | |
732 | if (private == NULL) { | |
733 | if (identity_passphrase) | |
734 | passphrase = xstrdup(identity_passphrase); | |
735 | else if (identity_new_passphrase) | |
736 | passphrase = xstrdup(identity_new_passphrase); | |
737 | else | |
738 | passphrase = read_passphrase("Enter passphrase: ", | |
739 | RP_ALLOW_STDIN); | |
740 | /* Try to load using the passphrase. */ | |
741 | private = key_load_private(identity_file, passphrase, &comment); | |
742 | if (private == NULL) { | |
743 | memset(passphrase, 0, strlen(passphrase)); | |
744 | xfree(passphrase); | |
745 | printf("Bad passphrase.\n"); | |
746 | exit(1); | |
747 | } | |
748 | } else { | |
749 | passphrase = xstrdup(""); | |
750 | } | |
751 | if (private->type != KEY_RSA1) { | |
752 | fprintf(stderr, "Comments are only supported for RSA1 keys.\n"); | |
753 | key_free(private); | |
754 | exit(1); | |
e9a17296 | 755 | } |
3c0ef626 | 756 | printf("Key now has comment '%s'\n", comment); |
757 | ||
758 | if (identity_comment) { | |
759 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); | |
760 | } else { | |
761 | printf("Enter new comment: "); | |
762 | fflush(stdout); | |
763 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { | |
764 | memset(passphrase, 0, strlen(passphrase)); | |
765 | key_free(private); | |
766 | exit(1); | |
767 | } | |
768 | if (strchr(new_comment, '\n')) | |
769 | *strchr(new_comment, '\n') = 0; | |
770 | } | |
771 | ||
772 | /* Save the file using the new passphrase. */ | |
773 | if (!key_save_private(private, identity_file, passphrase, new_comment)) { | |
774 | printf("Saving the key failed: %s.\n", identity_file); | |
775 | memset(passphrase, 0, strlen(passphrase)); | |
776 | xfree(passphrase); | |
777 | key_free(private); | |
778 | xfree(comment); | |
779 | exit(1); | |
780 | } | |
781 | memset(passphrase, 0, strlen(passphrase)); | |
782 | xfree(passphrase); | |
783 | public = key_from_private(private); | |
784 | key_free(private); | |
785 | ||
786 | strlcat(identity_file, ".pub", sizeof(identity_file)); | |
787 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
788 | if (fd == -1) { | |
789 | printf("Could not save your public key in %s\n", identity_file); | |
790 | exit(1); | |
791 | } | |
792 | f = fdopen(fd, "w"); | |
793 | if (f == NULL) { | |
794 | printf("fdopen %s failed", identity_file); | |
795 | exit(1); | |
796 | } | |
797 | if (!key_write(public, f)) | |
798 | fprintf(stderr, "write key failed"); | |
799 | key_free(public); | |
800 | fprintf(f, " %s\n", new_comment); | |
801 | fclose(f); | |
802 | ||
803 | xfree(comment); | |
804 | ||
805 | printf("The comment in your key file has been changed.\n"); | |
806 | exit(0); | |
807 | } | |
808 | ||
809 | static void | |
810 | usage(void) | |
811 | { | |
812 | fprintf(stderr, "Usage: %s [options]\n", __progname); | |
813 | fprintf(stderr, "Options:\n"); | |
814 | fprintf(stderr, " -b bits Number of bits in the key to create.\n"); | |
815 | fprintf(stderr, " -c Change comment in private and public key files.\n"); | |
816 | fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n"); | |
817 | fprintf(stderr, " -f filename Filename of the key file.\n"); | |
818 | fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n"); | |
819 | fprintf(stderr, " -l Show fingerprint of key file.\n"); | |
820 | fprintf(stderr, " -p Change passphrase of private key file.\n"); | |
821 | fprintf(stderr, " -q Quiet.\n"); | |
822 | fprintf(stderr, " -y Read private key file and print public key.\n"); | |
823 | fprintf(stderr, " -t type Specify type of key to create.\n"); | |
824 | fprintf(stderr, " -B Show bubblebabble digest of key file.\n"); | |
825 | fprintf(stderr, " -C comment Provide new comment.\n"); | |
826 | fprintf(stderr, " -N phrase Provide new passphrase.\n"); | |
827 | fprintf(stderr, " -P phrase Provide old passphrase.\n"); | |
828 | #ifdef SMARTCARD | |
829 | fprintf(stderr, " -D reader Download public key from smartcard.\n"); | |
830 | fprintf(stderr, " -U reader Upload private key to smartcard.\n"); | |
831 | #endif /* SMARTCARD */ | |
832 | ||
833 | exit(1); | |
834 | } | |
835 | ||
836 | /* | |
837 | * Main program for key management. | |
838 | */ | |
839 | int | |
840 | main(int ac, char **av) | |
841 | { | |
e9a17296 | 842 | char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2; |
3c0ef626 | 843 | char *reader_id = NULL; |
844 | Key *private, *public; | |
845 | struct passwd *pw; | |
846 | struct stat st; | |
847 | int opt, type, fd, download = 0; | |
848 | FILE *f; | |
849 | ||
850 | extern int optind; | |
851 | extern char *optarg; | |
852 | ||
853 | __progname = get_progname(av[0]); | |
3c0ef626 | 854 | |
855 | SSLeay_add_all_algorithms(); | |
856 | ||
857 | /* we need this for the home * directory. */ | |
858 | pw = getpwuid(getuid()); | |
859 | if (!pw) { | |
860 | printf("You don't exist, go away!\n"); | |
861 | exit(1); | |
862 | } | |
863 | if (gethostname(hostname, sizeof(hostname)) < 0) { | |
864 | perror("gethostname"); | |
865 | exit(1); | |
866 | } | |
867 | ||
868 | while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) { | |
869 | switch (opt) { | |
870 | case 'b': | |
871 | bits = atoi(optarg); | |
872 | if (bits < 512 || bits > 32768) { | |
873 | printf("Bits has bad value.\n"); | |
874 | exit(1); | |
875 | } | |
876 | break; | |
877 | case 'l': | |
878 | print_fingerprint = 1; | |
879 | break; | |
880 | case 'B': | |
881 | print_bubblebabble = 1; | |
882 | break; | |
883 | case 'p': | |
884 | change_passphrase = 1; | |
885 | break; | |
886 | case 'c': | |
887 | change_comment = 1; | |
888 | break; | |
889 | case 'f': | |
890 | strlcpy(identity_file, optarg, sizeof(identity_file)); | |
891 | have_identity = 1; | |
892 | break; | |
893 | case 'P': | |
894 | identity_passphrase = optarg; | |
895 | break; | |
896 | case 'N': | |
897 | identity_new_passphrase = optarg; | |
898 | break; | |
899 | case 'C': | |
900 | identity_comment = optarg; | |
901 | break; | |
902 | case 'q': | |
903 | quiet = 1; | |
904 | break; | |
905 | case 'R': | |
906 | /* unused */ | |
907 | exit(0); | |
908 | break; | |
909 | case 'e': | |
910 | case 'x': | |
911 | /* export key */ | |
912 | convert_to_ssh2 = 1; | |
913 | break; | |
914 | case 'i': | |
915 | case 'X': | |
916 | /* import key */ | |
917 | convert_from_ssh2 = 1; | |
918 | break; | |
919 | case 'y': | |
920 | print_public = 1; | |
921 | break; | |
922 | case 'd': | |
923 | key_type_name = "dsa"; | |
924 | break; | |
925 | case 't': | |
926 | key_type_name = optarg; | |
927 | break; | |
928 | case 'D': | |
929 | download = 1; | |
930 | case 'U': | |
931 | reader_id = optarg; | |
932 | break; | |
933 | case '?': | |
934 | default: | |
935 | usage(); | |
936 | } | |
937 | } | |
938 | if (optind < ac) { | |
939 | printf("Too many arguments.\n"); | |
940 | usage(); | |
941 | } | |
942 | if (change_passphrase && change_comment) { | |
943 | printf("Can only have one of -p and -c.\n"); | |
944 | usage(); | |
945 | } | |
946 | if (print_fingerprint || print_bubblebabble) | |
947 | do_fingerprint(pw); | |
948 | if (change_passphrase) | |
949 | do_change_passphrase(pw); | |
950 | if (change_comment) | |
951 | do_change_comment(pw); | |
952 | if (convert_to_ssh2) | |
953 | do_convert_to_ssh2(pw); | |
954 | if (convert_from_ssh2) | |
955 | do_convert_from_ssh2(pw); | |
956 | if (print_public) | |
957 | do_print_public(pw); | |
958 | if (reader_id != NULL) { | |
959 | #ifdef SMARTCARD | |
960 | if (download) | |
961 | do_download(pw, reader_id); | |
962 | else | |
963 | do_upload(pw, reader_id); | |
964 | #else /* SMARTCARD */ | |
965 | fatal("no support for smartcards."); | |
966 | #endif /* SMARTCARD */ | |
967 | } | |
968 | ||
e9a17296 | 969 | init_rng(); |
970 | seed_rng(); | |
3c0ef626 | 971 | arc4random_stir(); |
972 | ||
e9a17296 | 973 | if (key_type_name == NULL) { |
974 | printf("You must specify a key type (-t).\n"); | |
975 | usage(); | |
976 | } | |
3c0ef626 | 977 | type = key_type_from_name(key_type_name); |
978 | if (type == KEY_UNSPEC) { | |
979 | fprintf(stderr, "unknown key type %s\n", key_type_name); | |
980 | exit(1); | |
981 | } | |
982 | if (!quiet) | |
983 | printf("Generating public/private %s key pair.\n", key_type_name); | |
984 | private = key_generate(type, bits); | |
985 | if (private == NULL) { | |
986 | fprintf(stderr, "key_generate failed"); | |
987 | exit(1); | |
988 | } | |
989 | public = key_from_private(private); | |
990 | ||
991 | if (!have_identity) | |
992 | ask_filename(pw, "Enter file in which to save the key"); | |
993 | ||
994 | /* Create ~/.ssh directory if it doesn\'t already exist. */ | |
995 | snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR); | |
996 | if (strstr(identity_file, dotsshdir) != NULL && | |
997 | stat(dotsshdir, &st) < 0) { | |
998 | if (mkdir(dotsshdir, 0700) < 0) | |
999 | error("Could not create directory '%s'.", dotsshdir); | |
1000 | else if (!quiet) | |
1001 | printf("Created directory '%s'.\n", dotsshdir); | |
1002 | } | |
1003 | /* If the file already exists, ask the user to confirm. */ | |
1004 | if (stat(identity_file, &st) >= 0) { | |
1005 | char yesno[3]; | |
1006 | printf("%s already exists.\n", identity_file); | |
1007 | printf("Overwrite (y/n)? "); | |
1008 | fflush(stdout); | |
1009 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) | |
1010 | exit(1); | |
1011 | if (yesno[0] != 'y' && yesno[0] != 'Y') | |
1012 | exit(1); | |
1013 | } | |
1014 | /* Ask for a passphrase (twice). */ | |
1015 | if (identity_passphrase) | |
1016 | passphrase1 = xstrdup(identity_passphrase); | |
1017 | else if (identity_new_passphrase) | |
1018 | passphrase1 = xstrdup(identity_new_passphrase); | |
1019 | else { | |
1020 | passphrase_again: | |
1021 | passphrase1 = | |
1022 | read_passphrase("Enter passphrase (empty for no " | |
1023 | "passphrase): ", RP_ALLOW_STDIN); | |
1024 | passphrase2 = read_passphrase("Enter same passphrase again: ", | |
1025 | RP_ALLOW_STDIN); | |
1026 | if (strcmp(passphrase1, passphrase2) != 0) { | |
1027 | /* | |
1028 | * The passphrases do not match. Clear them and | |
1029 | * retry. | |
1030 | */ | |
1031 | memset(passphrase1, 0, strlen(passphrase1)); | |
1032 | memset(passphrase2, 0, strlen(passphrase2)); | |
1033 | xfree(passphrase1); | |
1034 | xfree(passphrase2); | |
1035 | printf("Passphrases do not match. Try again.\n"); | |
1036 | goto passphrase_again; | |
1037 | } | |
1038 | /* Clear the other copy of the passphrase. */ | |
1039 | memset(passphrase2, 0, strlen(passphrase2)); | |
1040 | xfree(passphrase2); | |
1041 | } | |
1042 | ||
1043 | if (identity_comment) { | |
1044 | strlcpy(comment, identity_comment, sizeof(comment)); | |
1045 | } else { | |
1046 | /* Create default commend field for the passphrase. */ | |
1047 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); | |
1048 | } | |
1049 | ||
1050 | /* Save the key with the given passphrase and comment. */ | |
1051 | if (!key_save_private(private, identity_file, passphrase1, comment)) { | |
1052 | printf("Saving the key failed: %s.\n", identity_file); | |
1053 | memset(passphrase1, 0, strlen(passphrase1)); | |
1054 | xfree(passphrase1); | |
1055 | exit(1); | |
1056 | } | |
1057 | /* Clear the passphrase. */ | |
1058 | memset(passphrase1, 0, strlen(passphrase1)); | |
1059 | xfree(passphrase1); | |
1060 | ||
1061 | /* Clear the private key and the random number generator. */ | |
1062 | key_free(private); | |
1063 | arc4random_stir(); | |
1064 | ||
1065 | if (!quiet) | |
1066 | printf("Your identification has been saved in %s.\n", identity_file); | |
1067 | ||
1068 | strlcat(identity_file, ".pub", sizeof(identity_file)); | |
1069 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
1070 | if (fd == -1) { | |
1071 | printf("Could not save your public key in %s\n", identity_file); | |
1072 | exit(1); | |
1073 | } | |
1074 | f = fdopen(fd, "w"); | |
1075 | if (f == NULL) { | |
1076 | printf("fdopen %s failed", identity_file); | |
1077 | exit(1); | |
1078 | } | |
1079 | if (!key_write(public, f)) | |
1080 | fprintf(stderr, "write key failed"); | |
1081 | fprintf(f, " %s\n", comment); | |
1082 | fclose(f); | |
1083 | ||
1084 | if (!quiet) { | |
1085 | char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX); | |
1086 | printf("Your public key has been saved in %s.\n", | |
1087 | identity_file); | |
1088 | printf("The key fingerprint is:\n"); | |
1089 | printf("%s %s\n", fp, comment); | |
1090 | xfree(fp); | |
1091 | } | |
1092 | ||
1093 | key_free(public); | |
1094 | exit(0); | |
1095 | } |