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