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