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