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