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