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