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