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