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