blob: 0469ca5b675549166f74e956265b777de7efda32 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11006 *
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".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Ben Lindstroma3700052001-04-05 23:26:32 +000015RCSID("$OpenBSD: ssh-keygen.c,v 1.55 2001/04/05 10:42:54 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Millereba71ba2000-04-29 23:57:08 +100017#include <openssl/evp.h>
18#include <openssl/pem.h>
Damien Millereba71ba2000-04-29 23:57:08 +100019
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100021#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000022#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100023#include "authfile.h"
24#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110025#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "pathnames.h"
28#include "log.h"
29#include "readpass.h"
Damien Miller874d77b2000-10-14 16:23:11 +110030
Damien Millereba71ba2000-04-29 23:57:08 +100031/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032int bits = 1024;
33
Damien Miller5428f641999-11-25 11:54:57 +110034/*
35 * Flag indicating that we just want to change the passphrase. This can be
36 * set on the command line.
37 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038int change_passphrase = 0;
39
Damien Miller5428f641999-11-25 11:54:57 +110040/*
41 * Flag indicating that we just want to change the comment. This can be set
42 * on the command line.
43 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044int change_comment = 0;
45
46int quiet = 0;
47
Damien Miller10f6f6b1999-11-17 17:29:08 +110048/* Flag indicating that we just want to see the key fingerprint */
49int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000050int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110051
Damien Miller431f66b1999-11-21 18:31:57 +110052/* The identity file name, given on the command line or entered by the user. */
53char identity_file[1024];
54int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
56/* This is set to the passphrase if given on the command line. */
57char *identity_passphrase = NULL;
58
59/* This is set to the new passphrase if given on the command line. */
60char *identity_new_passphrase = NULL;
61
62/* This is set to the new comment if given on the command line. */
63char *identity_comment = NULL;
64
Damien Millereba71ba2000-04-29 23:57:08 +100065/* Dump public key file in format used by real and the original SSH 2 */
66int convert_to_ssh2 = 0;
67int convert_from_ssh2 = 0;
68int print_public = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110069
Damien Millere39cacc2000-11-29 12:18:44 +110070/* default to RSA for SSH-1 */
71char *key_type_name = "rsa1";
Damien Millereba71ba2000-04-29 23:57:08 +100072
Damien Miller431f66b1999-11-21 18:31:57 +110073/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110074#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110075extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000076#else
77char *__progname;
78#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millereba71ba2000-04-29 23:57:08 +100080char hostname[MAXHOSTNAMELEN];
81
Damien Miller431f66b1999-11-21 18:31:57 +110082void
83ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084{
Damien Miller95def091999-11-25 00:26:21 +110085 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110086 char *name = NULL;
87
88 switch (key_type_from_name(key_type_name)) {
89 case KEY_RSA1:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090 name = _PATH_SSH_CLIENT_IDENTITY;
Damien Millere39cacc2000-11-29 12:18:44 +110091 break;
92 case KEY_DSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000093 name = _PATH_SSH_CLIENT_ID_DSA;
Damien Millere39cacc2000-11-29 12:18:44 +110094 break;
95 case KEY_RSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000096 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Millere39cacc2000-11-29 12:18:44 +110097 break;
98 default:
99 fprintf(stderr, "bad key type");
100 exit(1);
101 break;
102 }
103 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000104 fprintf(stderr, "%s (%s): ", prompt, identity_file);
105 fflush(stderr);
Damien Miller95def091999-11-25 00:26:21 +1100106 if (fgets(buf, sizeof(buf), stdin) == NULL)
107 exit(1);
108 if (strchr(buf, '\n'))
109 *strchr(buf, '\n') = 0;
110 if (strcmp(buf, "") != 0)
111 strlcpy(identity_file, buf, sizeof(identity_file));
112 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100113}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Ben Lindstromd0fca422001-03-26 13:44:06 +0000115Key *
116try_load_pem_key(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000117{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000118 char *pass;
119 Key *prv;
120
Ben Lindstroma3700052001-04-05 23:26:32 +0000121 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000122 if (prv == NULL) {
123 pass = read_passphrase("Enter passphrase: ", 1);
124 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000125 memset(pass, 0, strlen(pass));
126 xfree(pass);
127 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000128 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000129}
130
Damien Miller874d77b2000-10-14 16:23:11 +1100131#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
132#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
133#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000134#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000135
136void
137do_convert_to_ssh2(struct passwd *pw)
138{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000139 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000140 int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000141 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000142 struct stat st;
143
144 if (!have_identity)
145 ask_filename(pw, "Enter file in which the key is");
146 if (stat(identity_file, &st) < 0) {
147 perror(identity_file);
148 exit(1);
149 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000150 prv = try_load_pem_key(identity_file);
151 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000152 fprintf(stderr, "load failed\n");
153 exit(1);
154 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000155 key_to_blob(prv, &blob, &len);
Damien Miller874d77b2000-10-14 16:23:11 +1100156 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000157 fprintf(stdout,
Damien Miller874d77b2000-10-14 16:23:11 +1100158 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
Ben Lindstromd0fca422001-03-26 13:44:06 +0000159 key_size(prv), key_type(prv),
Damien Millereba71ba2000-04-29 23:57:08 +1000160 pw->pw_name, hostname);
161 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100162 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000163 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000164 xfree(blob);
165 exit(0);
166}
167
168void
Damien Miller874d77b2000-10-14 16:23:11 +1100169buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
170{
171 int bits = buffer_get_int(b);
172 int bytes = (bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000173
Damien Miller874d77b2000-10-14 16:23:11 +1100174 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000175 fatal("buffer_get_bignum_bits: input buffer too small: "
176 "need %d have %d", bytes, buffer_len(b));
Ben Lindstrom46c16222000-12-22 01:43:59 +0000177 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100178 buffer_consume(b, bytes);
179}
180
181Key *
182do_convert_private_ssh2_from_blob(char *blob, int blen)
183{
184 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100185 Key *key = NULL;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000186 int ignore, magic, rlen, ktype;
Damien Miller874d77b2000-10-14 16:23:11 +1100187 char *type, *cipher;
188
189 buffer_init(&b);
190 buffer_append(&b, blob, blen);
191
192 magic = buffer_get_int(&b);
193 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
194 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
195 buffer_free(&b);
196 return NULL;
197 }
198 ignore = buffer_get_int(&b);
199 type = buffer_get_string(&b, NULL);
200 cipher = buffer_get_string(&b, NULL);
201 ignore = buffer_get_int(&b);
202 ignore = buffer_get_int(&b);
203 ignore = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100204
205 if (strcmp(cipher, "none") != 0) {
206 error("unsupported cipher %s", cipher);
207 xfree(cipher);
208 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000209 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100210 return NULL;
211 }
212 xfree(cipher);
213
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000214 if (strstr(type, "dsa")) {
215 ktype = KEY_DSA;
216 } else if (strstr(type, "rsa")) {
217 ktype = KEY_RSA;
218 } else {
219 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100220 return NULL;
221 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000222 key = key_new_private(ktype);
223 xfree(type);
224
225 switch (key->type) {
226 case KEY_DSA:
227 buffer_get_bignum_bits(&b, key->dsa->p);
228 buffer_get_bignum_bits(&b, key->dsa->g);
229 buffer_get_bignum_bits(&b, key->dsa->q);
230 buffer_get_bignum_bits(&b, key->dsa->pub_key);
231 buffer_get_bignum_bits(&b, key->dsa->priv_key);
232 break;
233 case KEY_RSA:
234 if (!BN_set_word(key->rsa->e, (u_long) buffer_get_char(&b))) {
235 buffer_free(&b);
236 key_free(key);
237 return NULL;
238 }
239 buffer_get_bignum_bits(&b, key->rsa->d);
240 buffer_get_bignum_bits(&b, key->rsa->n);
241 buffer_get_bignum_bits(&b, key->rsa->iqmp);
242 buffer_get_bignum_bits(&b, key->rsa->q);
243 buffer_get_bignum_bits(&b, key->rsa->p);
244 generate_additional_parameters(key->rsa);
245 break;
246 }
Damien Miller874d77b2000-10-14 16:23:11 +1100247 rlen = buffer_len(&b);
248 if(rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000249 error("do_convert_private_ssh2_from_blob: "
250 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100251 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000252#ifdef DEBUG_PK
253 {
254 u_int slen;
255 u_char *sig, data[10] = "abcde12345";
256
257 key_sign(key, &sig, &slen, data, sizeof data);
258 key_verify(key, sig, slen, data, sizeof data);
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000259 xfree(sig);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000260 }
261#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100262 return key;
263}
264
265void
Damien Millereba71ba2000-04-29 23:57:08 +1000266do_convert_from_ssh2(struct passwd *pw)
267{
268 Key *k;
269 int blen;
270 char line[1024], *p;
271 char blob[8096];
272 char encoded[8096];
273 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100274 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000275 FILE *fp;
276
277 if (!have_identity)
278 ask_filename(pw, "Enter file in which the key is");
279 if (stat(identity_file, &st) < 0) {
280 perror(identity_file);
281 exit(1);
282 }
283 fp = fopen(identity_file, "r");
284 if (fp == NULL) {
285 perror(identity_file);
286 exit(1);
287 }
288 encoded[0] = '\0';
289 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000290 if (!(p = strchr(line, '\n'))) {
291 fprintf(stderr, "input line too long.\n");
292 exit(1);
293 }
294 if (p > line && p[-1] == '\\')
295 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000296 if (strncmp(line, "----", 4) == 0 ||
297 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100298 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
299 private = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000300 fprintf(stderr, "ignore: %s", line);
301 continue;
302 }
Damien Miller30c3d422000-05-09 11:02:59 +1000303 if (escaped) {
304 escaped--;
305 fprintf(stderr, "escaped: %s", line);
306 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000307 }
308 *p = '\0';
309 strlcat(encoded, line, sizeof(encoded));
310 }
Ben Lindstrom46c16222000-12-22 01:43:59 +0000311 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000312 if (blen < 0) {
313 fprintf(stderr, "uudecode failed.\n");
314 exit(1);
315 }
Damien Miller874d77b2000-10-14 16:23:11 +1100316 k = private ?
317 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100318 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100319 if (k == NULL) {
320 fprintf(stderr, "decode blob failed.\n");
321 exit(1);
322 }
323 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000324 (k->type == KEY_DSA ?
325 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
326 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100327 key_write(k, stdout);
328 if (!ok) {
329 fprintf(stderr, "key write failed");
330 exit(1);
331 }
Damien Millereba71ba2000-04-29 23:57:08 +1000332 key_free(k);
333 fprintf(stdout, "\n");
334 fclose(fp);
335 exit(0);
336}
337
338void
339do_print_public(struct passwd *pw)
340{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000341 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000342 struct stat st;
343
344 if (!have_identity)
345 ask_filename(pw, "Enter file in which the key is");
346 if (stat(identity_file, &st) < 0) {
347 perror(identity_file);
348 exit(1);
349 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000350 prv = try_load_pem_key(identity_file);
351 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000352 fprintf(stderr, "load failed\n");
353 exit(1);
354 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000355 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000356 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000357 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000358 fprintf(stdout, "\n");
359 exit(0);
360}
361
Damien Miller10f6f6b1999-11-17 17:29:08 +1100362void
363do_fingerprint(struct passwd *pw)
364{
Damien Miller98c7ad62000-03-09 21:27:49 +1100365 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000366 Key *public;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000367 char *comment = NULL, *cp, *ep, line[16*1024], *fp;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000368 int i, skip = 0, num = 1, invalid = 1, rep, fptype;
Damien Miller95def091999-11-25 00:26:21 +1100369 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100370
Ben Lindstromd0fca422001-03-26 13:44:06 +0000371 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
372 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000373
Damien Miller95def091999-11-25 00:26:21 +1100374 if (!have_identity)
375 ask_filename(pw, "Enter file in which the key is");
376 if (stat(identity_file, &st) < 0) {
377 perror(identity_file);
378 exit(1);
379 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000380 public = key_load_public(identity_file, &comment);
381 if (public != NULL) {
382 fp = key_fingerprint(public, fptype, rep);
383 printf("%d %s %s\n", key_size(public), fp, comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 key_free(public);
385 xfree(comment);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000386 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100387 exit(0);
388 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000389 if (comment)
390 xfree(comment);
Damien Miller98c7ad62000-03-09 21:27:49 +1100391
392 f = fopen(identity_file, "r");
393 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100394 while (fgets(line, sizeof(line), f)) {
395 i = strlen(line) - 1;
396 if (line[i] != '\n') {
397 error("line %d too long: %.40s...", num, line);
398 skip = 1;
399 continue;
Damien Miller95def091999-11-25 00:26:21 +1100400 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100401 num++;
402 if (skip) {
403 skip = 0;
404 continue;
405 }
406 line[i] = '\0';
407
408 /* Skip leading whitespace, empty and comment lines. */
409 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
410 ;
411 if (!*cp || *cp == '\n' || *cp == '#')
412 continue ;
413 i = strtol(cp, &ep, 10);
414 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
415 int quoted = 0;
416 comment = cp;
417 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
418 if (*cp == '\\' && cp[1] == '"')
419 cp++; /* Skip both */
420 else if (*cp == '"')
421 quoted = !quoted;
422 }
423 if (!*cp)
424 continue;
425 *cp++ = '\0';
426 }
427 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000428 public = key_new(KEY_RSA1);
429 if (key_read(public, &cp) != 1) {
430 cp = ep;
431 key_free(public);
432 public = key_new(KEY_UNSPEC);
433 if (key_read(public, &cp) != 1) {
434 key_free(public);
435 continue;
436 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100437 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000438 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000439 fp = key_fingerprint(public, fptype, rep);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000440 printf("%d %s %s\n", key_size(public), fp,
Ben Lindstrom2941f112000-12-29 16:50:13 +0000441 comment ? comment : "no comment");
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000442 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000443 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000444 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100445 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100446 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100447 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100448 if (invalid) {
449 printf("%s is not a valid key file.\n", identity_file);
450 exit(1);
451 }
Damien Miller95def091999-11-25 00:26:21 +1100452 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100453}
454
Damien Miller95def091999-11-25 00:26:21 +1100455/*
456 * Perform changing a passphrase. The argument is the passwd structure
457 * for the current user.
458 */
Damien Miller10f6f6b1999-11-17 17:29:08 +1100459void
460do_change_passphrase(struct passwd *pw)
461{
Damien Miller95def091999-11-25 00:26:21 +1100462 char *comment;
463 char *old_passphrase, *passphrase1, *passphrase2;
464 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000465 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100466
Damien Miller95def091999-11-25 00:26:21 +1100467 if (!have_identity)
468 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100469 if (stat(identity_file, &st) < 0) {
470 perror(identity_file);
471 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472 }
Damien Miller95def091999-11-25 00:26:21 +1100473 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000474 private = key_load_private(identity_file, "", &comment);
475 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100476 if (identity_passphrase)
477 old_passphrase = xstrdup(identity_passphrase);
478 else
479 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000480 private = key_load_private(identity_file, old_passphrase , &comment);
481 memset(old_passphrase, 0, strlen(old_passphrase));
482 xfree(old_passphrase);
483 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100484 printf("Bad passphrase.\n");
485 exit(1);
486 }
Damien Miller95def091999-11-25 00:26:21 +1100487 }
488 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
Damien Miller95def091999-11-25 00:26:21 +1100490 /* Ask the new passphrase (twice). */
491 if (identity_new_passphrase) {
492 passphrase1 = xstrdup(identity_new_passphrase);
493 passphrase2 = NULL;
494 } else {
495 passphrase1 =
496 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
497 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
498
499 /* Verify that they are the same. */
500 if (strcmp(passphrase1, passphrase2) != 0) {
501 memset(passphrase1, 0, strlen(passphrase1));
502 memset(passphrase2, 0, strlen(passphrase2));
503 xfree(passphrase1);
504 xfree(passphrase2);
505 printf("Pass phrases do not match. Try again.\n");
506 exit(1);
507 }
508 /* Destroy the other copy. */
509 memset(passphrase2, 0, strlen(passphrase2));
510 xfree(passphrase2);
511 }
512
513 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000514 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100515 printf("Saving the key failed: %s: %s.\n",
516 identity_file, strerror(errno));
517 memset(passphrase1, 0, strlen(passphrase1));
518 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000519 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100520 xfree(comment);
521 exit(1);
522 }
523 /* Destroy the passphrase and the copy of the key in memory. */
524 memset(passphrase1, 0, strlen(passphrase1));
525 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000526 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100527 xfree(comment);
528
529 printf("Your identification has been saved with the new passphrase.\n");
530 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531}
532
Damien Miller95def091999-11-25 00:26:21 +1100533/*
534 * Change the comment of a private key file.
535 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536void
537do_change_comment(struct passwd *pw)
538{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000539 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000540 Key *private;
541 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100542 struct stat st;
543 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000544 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545
Damien Miller95def091999-11-25 00:26:21 +1100546 if (!have_identity)
547 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100548 if (stat(identity_file, &st) < 0) {
549 perror(identity_file);
550 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000552 private = key_load_private(identity_file, "", &comment);
553 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100554 if (identity_passphrase)
555 passphrase = xstrdup(identity_passphrase);
556 else if (identity_new_passphrase)
557 passphrase = xstrdup(identity_new_passphrase);
558 else
559 passphrase = read_passphrase("Enter passphrase: ", 1);
560 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000561 private = key_load_private(identity_file, passphrase, &comment);
562 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100563 memset(passphrase, 0, strlen(passphrase));
564 xfree(passphrase);
565 printf("Bad passphrase.\n");
566 exit(1);
567 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000568 } else {
569 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100570 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000571 if (private->type != KEY_RSA1) {
572 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
573 key_free(private);
574 exit(1);
575 }
Damien Miller95def091999-11-25 00:26:21 +1100576 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577
Damien Miller95def091999-11-25 00:26:21 +1100578 if (identity_comment) {
579 strlcpy(new_comment, identity_comment, sizeof(new_comment));
580 } else {
581 printf("Enter new comment: ");
582 fflush(stdout);
583 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
584 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000585 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100586 exit(1);
587 }
Damien Miller95def091999-11-25 00:26:21 +1100588 if (strchr(new_comment, '\n'))
589 *strchr(new_comment, '\n') = 0;
590 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591
Damien Miller95def091999-11-25 00:26:21 +1100592 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000593 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100594 printf("Saving the key failed: %s: %s.\n",
595 identity_file, strerror(errno));
596 memset(passphrase, 0, strlen(passphrase));
597 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000598 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100599 xfree(comment);
600 exit(1);
601 }
Damien Miller95def091999-11-25 00:26:21 +1100602 memset(passphrase, 0, strlen(passphrase));
603 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000604 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000605 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606
Damien Miller95def091999-11-25 00:26:21 +1100607 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000608 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
609 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100610 printf("Could not save your public key in %s\n", identity_file);
611 exit(1);
612 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000613 f = fdopen(fd, "w");
614 if (f == NULL) {
615 printf("fdopen %s failed", identity_file);
616 exit(1);
617 }
Damien Millereba71ba2000-04-29 23:57:08 +1000618 if (!key_write(public, f))
619 fprintf(stderr, "write key failed");
620 key_free(public);
621 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100622 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623
Damien Miller95def091999-11-25 00:26:21 +1100624 xfree(comment);
625
626 printf("The comment in your key file has been changed.\n");
627 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628}
629
Damien Miller431f66b1999-11-21 18:31:57 +1100630void
631usage(void)
632{
Ben Lindstromf1107f52001-03-22 02:05:32 +0000633 printf("Usage: %s [-lBpqxXyc] [-t type] [-b bits] [-f file] [-C comment] "
Ben Lindstromb7c92322001-03-05 05:10:52 +0000634 "[-N new-pass] [-P pass]\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100635 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100636}
637
Damien Miller95def091999-11-25 00:26:21 +1100638/*
639 * Main program for key management.
640 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641int
642main(int ac, char **av)
643{
Damien Miller95def091999-11-25 00:26:21 +1100644 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000645 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100646 struct passwd *pw;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000647 int opt, type, fd;
Damien Miller95def091999-11-25 00:26:21 +1100648 struct stat st;
649 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100650
Damien Miller95def091999-11-25 00:26:21 +1100651 extern int optind;
652 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000653
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000654 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000655 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100656 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000657
Damien Millerd3a18572000-06-07 19:55:44 +1000658 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000659
Damien Miller5428f641999-11-25 11:54:57 +1100660 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100661 pw = getpwuid(getuid());
662 if (!pw) {
663 printf("You don't exist, go away!\n");
664 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665 }
Damien Millereba71ba2000-04-29 23:57:08 +1000666 if (gethostname(hostname, sizeof(hostname)) < 0) {
667 perror("gethostname");
668 exit(1);
669 }
Damien Miller5428f641999-11-25 11:54:57 +1100670
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000671 while ((opt = getopt(ac, av, "dqpclBRxXyb:f:t:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100672 switch (opt) {
673 case 'b':
674 bits = atoi(optarg);
675 if (bits < 512 || bits > 32768) {
676 printf("Bits has bad value.\n");
677 exit(1);
678 }
679 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000680
Damien Miller95def091999-11-25 00:26:21 +1100681 case 'l':
682 print_fingerprint = 1;
683 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000685 case 'B':
686 print_bubblebabble = 1;
687 break;
688
Damien Miller95def091999-11-25 00:26:21 +1100689 case 'p':
690 change_passphrase = 1;
691 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692
Damien Miller95def091999-11-25 00:26:21 +1100693 case 'c':
694 change_comment = 1;
695 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696
Damien Miller95def091999-11-25 00:26:21 +1100697 case 'f':
698 strlcpy(identity_file, optarg, sizeof(identity_file));
699 have_identity = 1;
700 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701
Damien Miller95def091999-11-25 00:26:21 +1100702 case 'P':
703 identity_passphrase = optarg;
704 break;
705
706 case 'N':
707 identity_new_passphrase = optarg;
708 break;
709
710 case 'C':
711 identity_comment = optarg;
712 break;
713
714 case 'q':
715 quiet = 1;
716 break;
717
Damien Millereba71ba2000-04-29 23:57:08 +1000718 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100719 /* unused */
720 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000721 break;
722
723 case 'x':
724 convert_to_ssh2 = 1;
725 break;
726
727 case 'X':
728 convert_from_ssh2 = 1;
729 break;
730
731 case 'y':
732 print_public = 1;
733 break;
734
735 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100736 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000737 break;
738
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739 case 't':
740 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100741 break;
742
Damien Miller95def091999-11-25 00:26:21 +1100743 case '?':
744 default:
745 usage();
746 }
747 }
748 if (optind < ac) {
749 printf("Too many arguments.\n");
750 usage();
751 }
752 if (change_passphrase && change_comment) {
753 printf("Can only have one of -p and -c.\n");
754 usage();
755 }
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000756 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +1100757 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100758 if (change_passphrase)
759 do_change_passphrase(pw);
Damien Miller95def091999-11-25 00:26:21 +1100760 if (change_comment)
761 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000762 if (convert_to_ssh2)
763 do_convert_to_ssh2(pw);
764 if (convert_from_ssh2)
765 do_convert_from_ssh2(pw);
766 if (print_public)
767 do_print_public(pw);
Damien Miller95def091999-11-25 00:26:21 +1100768
769 arc4random_stir();
770
Damien Millere39cacc2000-11-29 12:18:44 +1100771 type = key_type_from_name(key_type_name);
772 if (type == KEY_UNSPEC) {
773 fprintf(stderr, "unknown key type %s\n", key_type_name);
774 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000775 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100776 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100777 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100778 private = key_generate(type, bits);
779 if (private == NULL) {
780 fprintf(stderr, "key_generate failed");
781 exit(1);
782 }
783 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100784
785 if (!have_identity)
786 ask_filename(pw, "Enter file in which to save the key");
787
788 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000789 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100790 if (strstr(identity_file, dotsshdir) != NULL &&
791 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000792 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100793 error("Could not create directory '%s'.", dotsshdir);
794 else if (!quiet)
795 printf("Created directory '%s'.\n", dotsshdir);
796 }
797 /* If the file already exists, ask the user to confirm. */
798 if (stat(identity_file, &st) >= 0) {
799 char yesno[3];
800 printf("%s already exists.\n", identity_file);
801 printf("Overwrite (y/n)? ");
802 fflush(stdout);
803 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
804 exit(1);
805 if (yesno[0] != 'y' && yesno[0] != 'Y')
806 exit(1);
807 }
808 /* Ask for a passphrase (twice). */
809 if (identity_passphrase)
810 passphrase1 = xstrdup(identity_passphrase);
811 else if (identity_new_passphrase)
812 passphrase1 = xstrdup(identity_new_passphrase);
813 else {
814passphrase_again:
815 passphrase1 =
816 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
817 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
818 if (strcmp(passphrase1, passphrase2) != 0) {
819 /* The passphrases do not match. Clear them and retry. */
820 memset(passphrase1, 0, strlen(passphrase1));
821 memset(passphrase2, 0, strlen(passphrase2));
822 xfree(passphrase1);
823 xfree(passphrase2);
824 printf("Passphrases do not match. Try again.\n");
825 goto passphrase_again;
826 }
827 /* Clear the other copy of the passphrase. */
828 memset(passphrase2, 0, strlen(passphrase2));
829 xfree(passphrase2);
830 }
831
Damien Miller95def091999-11-25 00:26:21 +1100832 if (identity_comment) {
833 strlcpy(comment, identity_comment, sizeof(comment));
834 } else {
Damien Miller4af51302000-04-16 11:18:38 +1000835 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +1100836 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
837 }
838
839 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000840 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100841 printf("Saving the key failed: %s: %s.\n",
Damien Millereba71ba2000-04-29 23:57:08 +1000842 identity_file, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100843 memset(passphrase1, 0, strlen(passphrase1));
844 xfree(passphrase1);
845 exit(1);
846 }
847 /* Clear the passphrase. */
848 memset(passphrase1, 0, strlen(passphrase1));
849 xfree(passphrase1);
850
851 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100852 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100853 arc4random_stir();
854
855 if (!quiet)
856 printf("Your identification has been saved in %s.\n", identity_file);
857
Damien Miller95def091999-11-25 00:26:21 +1100858 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000859 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
860 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100861 printf("Could not save your public key in %s\n", identity_file);
862 exit(1);
863 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000864 f = fdopen(fd, "w");
865 if (f == NULL) {
866 printf("fdopen %s failed", identity_file);
867 exit(1);
868 }
Damien Millereba71ba2000-04-29 23:57:08 +1000869 if (!key_write(public, f))
870 fprintf(stderr, "write key failed");
871 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100872 fclose(f);
873
874 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +0000875 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Damien Millereba71ba2000-04-29 23:57:08 +1000876 printf("Your public key has been saved in %s.\n",
877 identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100878 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +0000879 printf("%s %s\n", fp, comment);
880 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +1100881 }
Damien Millereba71ba2000-04-29 23:57:08 +1000882
883 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100884 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885}