blob: f4f700f79d737164f720e2aabdc8f3711727c27c [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 Lindstromca0bf572001-03-11 20:08:29 +000015RCSID("$OpenBSD: ssh-keygen.c,v 1.48 2001/03/11 16:39:03 deraadt 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"
Damien Millereba71ba2000-04-29 23:57:08 +100022#include "authfile.h"
23#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110024#include "buffer.h"
25#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "pathnames.h"
27#include "log.h"
28#include "readpass.h"
Damien Miller874d77b2000-10-14 16:23:11 +110029
Damien Millereba71ba2000-04-29 23:57:08 +100030/* Number of bits in the RSA/DSA key. This value can be changed on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031int bits = 1024;
32
Damien Miller5428f641999-11-25 11:54:57 +110033/*
34 * Flag indicating that we just want to change the passphrase. This can be
35 * set on the command line.
36 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037int change_passphrase = 0;
38
Damien Miller5428f641999-11-25 11:54:57 +110039/*
40 * Flag indicating that we just want to change the comment. This can be set
41 * on the command line.
42 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043int change_comment = 0;
44
45int quiet = 0;
46
Damien Miller10f6f6b1999-11-17 17:29:08 +110047/* Flag indicating that we just want to see the key fingerprint */
48int print_fingerprint = 0;
49
Damien Miller431f66b1999-11-21 18:31:57 +110050/* The identity file name, given on the command line or entered by the user. */
51char identity_file[1024];
52int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
54/* This is set to the passphrase if given on the command line. */
55char *identity_passphrase = NULL;
56
57/* This is set to the new passphrase if given on the command line. */
58char *identity_new_passphrase = NULL;
59
60/* This is set to the new comment if given on the command line. */
61char *identity_comment = NULL;
62
Damien Millereba71ba2000-04-29 23:57:08 +100063/* Dump public key file in format used by real and the original SSH 2 */
64int convert_to_ssh2 = 0;
65int convert_from_ssh2 = 0;
66int print_public = 0;
Ben Lindstroma8a73e62001-03-11 20:05:19 +000067int print_verbose = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +110068
Damien Millere39cacc2000-11-29 12:18:44 +110069/* default to RSA for SSH-1 */
70char *key_type_name = "rsa1";
Damien Millereba71ba2000-04-29 23:57:08 +100071
Damien Miller431f66b1999-11-21 18:31:57 +110072/* argv0 */
Damien Miller95def091999-11-25 00:26:21 +110073#ifdef HAVE___PROGNAME
Damien Miller431f66b1999-11-21 18:31:57 +110074extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000075#else
76char *__progname;
77#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Millereba71ba2000-04-29 23:57:08 +100079char hostname[MAXHOSTNAMELEN];
80
Damien Miller431f66b1999-11-21 18:31:57 +110081void
82ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083{
Damien Miller95def091999-11-25 00:26:21 +110084 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +110085 char *name = NULL;
86
87 switch (key_type_from_name(key_type_name)) {
88 case KEY_RSA1:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089 name = _PATH_SSH_CLIENT_IDENTITY;
Damien Millere39cacc2000-11-29 12:18:44 +110090 break;
91 case KEY_DSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092 name = _PATH_SSH_CLIENT_ID_DSA;
Damien Millere39cacc2000-11-29 12:18:44 +110093 break;
94 case KEY_RSA:
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Millere39cacc2000-11-29 12:18:44 +110096 break;
97 default:
98 fprintf(stderr, "bad key type");
99 exit(1);
100 break;
101 }
102 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000103 fprintf(stderr, "%s (%s): ", prompt, identity_file);
104 fflush(stderr);
Damien Miller95def091999-11-25 00:26:21 +1100105 if (fgets(buf, sizeof(buf), stdin) == NULL)
106 exit(1);
107 if (strchr(buf, '\n'))
108 *strchr(buf, '\n') = 0;
109 if (strcmp(buf, "") != 0)
110 strlcpy(identity_file, buf, sizeof(identity_file));
111 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100112}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Millereba71ba2000-04-29 23:57:08 +1000114int
115try_load_key(char *filename, Key *k)
116{
117 int success = 1;
118 if (!load_private_key(filename, "", k, NULL)) {
119 char *pass = read_passphrase("Enter passphrase: ", 1);
120 if (!load_private_key(filename, pass, k, NULL)) {
121 success = 0;
122 }
123 memset(pass, 0, strlen(pass));
124 xfree(pass);
125 }
126 return success;
127}
128
Damien Miller874d77b2000-10-14 16:23:11 +1100129#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
130#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
131#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000132#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000133
134void
135do_convert_to_ssh2(struct passwd *pw)
136{
137 Key *k;
138 int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000139 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000140 struct stat st;
141
142 if (!have_identity)
143 ask_filename(pw, "Enter file in which the key is");
144 if (stat(identity_file, &st) < 0) {
145 perror(identity_file);
146 exit(1);
147 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100148 k = key_new(KEY_UNSPEC);
Damien Millereba71ba2000-04-29 23:57:08 +1000149 if (!try_load_key(identity_file, k)) {
150 fprintf(stderr, "load failed\n");
151 exit(1);
152 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100153 key_to_blob(k, &blob, &len);
Damien Miller874d77b2000-10-14 16:23:11 +1100154 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
Damien Millereba71ba2000-04-29 23:57:08 +1000155 fprintf(stdout,
Damien Miller874d77b2000-10-14 16:23:11 +1100156 "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
157 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000158 pw->pw_name, hostname);
159 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100160 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Damien Millereba71ba2000-04-29 23:57:08 +1000161 key_free(k);
162 xfree(blob);
163 exit(0);
164}
165
166void
Damien Miller874d77b2000-10-14 16:23:11 +1100167buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
168{
169 int bits = buffer_get_int(b);
170 int bytes = (bits + 7) / 8;
171 if (buffer_len(b) < bytes)
172 fatal("buffer_get_bignum_bits: input buffer too small");
Ben Lindstrom46c16222000-12-22 01:43:59 +0000173 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
Damien Miller874d77b2000-10-14 16:23:11 +1100174 buffer_consume(b, bytes);
175}
176
177Key *
178do_convert_private_ssh2_from_blob(char *blob, int blen)
179{
180 Buffer b;
181 DSA *dsa;
182 Key *key = NULL;
183 int ignore, magic, rlen;
184 char *type, *cipher;
185
186 buffer_init(&b);
187 buffer_append(&b, blob, blen);
188
189 magic = buffer_get_int(&b);
190 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
191 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
192 buffer_free(&b);
193 return NULL;
194 }
195 ignore = buffer_get_int(&b);
196 type = buffer_get_string(&b, NULL);
197 cipher = buffer_get_string(&b, NULL);
198 ignore = buffer_get_int(&b);
199 ignore = buffer_get_int(&b);
200 ignore = buffer_get_int(&b);
201 xfree(type);
202
203 if (strcmp(cipher, "none") != 0) {
204 error("unsupported cipher %s", cipher);
205 xfree(cipher);
206 buffer_free(&b);
207 return NULL;
208 }
209 xfree(cipher);
210
211 key = key_new(KEY_DSA);
212 dsa = key->dsa;
213 dsa->priv_key = BN_new();
214 if (dsa->priv_key == NULL) {
215 error("alloc priv_key failed");
216 key_free(key);
217 return NULL;
218 }
219 buffer_get_bignum_bits(&b, dsa->p);
220 buffer_get_bignum_bits(&b, dsa->g);
221 buffer_get_bignum_bits(&b, dsa->q);
222 buffer_get_bignum_bits(&b, dsa->pub_key);
223 buffer_get_bignum_bits(&b, dsa->priv_key);
224 rlen = buffer_len(&b);
225 if(rlen != 0)
226 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen);
227 buffer_free(&b);
228 return key;
229}
230
231void
Damien Millereba71ba2000-04-29 23:57:08 +1000232do_convert_from_ssh2(struct passwd *pw)
233{
234 Key *k;
235 int blen;
236 char line[1024], *p;
237 char blob[8096];
238 char encoded[8096];
239 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100240 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000241 FILE *fp;
242
243 if (!have_identity)
244 ask_filename(pw, "Enter file in which the key is");
245 if (stat(identity_file, &st) < 0) {
246 perror(identity_file);
247 exit(1);
248 }
249 fp = fopen(identity_file, "r");
250 if (fp == NULL) {
251 perror(identity_file);
252 exit(1);
253 }
254 encoded[0] = '\0';
255 while (fgets(line, sizeof(line), fp)) {
Damien Miller30c3d422000-05-09 11:02:59 +1000256 if (!(p = strchr(line, '\n'))) {
257 fprintf(stderr, "input line too long.\n");
258 exit(1);
259 }
260 if (p > line && p[-1] == '\\')
261 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000262 if (strncmp(line, "----", 4) == 0 ||
263 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100264 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
265 private = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000266 fprintf(stderr, "ignore: %s", line);
267 continue;
268 }
Damien Miller30c3d422000-05-09 11:02:59 +1000269 if (escaped) {
270 escaped--;
271 fprintf(stderr, "escaped: %s", line);
272 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000273 }
274 *p = '\0';
275 strlcat(encoded, line, sizeof(encoded));
276 }
Ben Lindstrom46c16222000-12-22 01:43:59 +0000277 blen = uudecode(encoded, (u_char *)blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000278 if (blen < 0) {
279 fprintf(stderr, "uudecode failed.\n");
280 exit(1);
281 }
Damien Miller874d77b2000-10-14 16:23:11 +1100282 k = private ?
283 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100284 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100285 if (k == NULL) {
286 fprintf(stderr, "decode blob failed.\n");
287 exit(1);
288 }
289 ok = private ?
290 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
291 key_write(k, stdout);
292 if (!ok) {
293 fprintf(stderr, "key write failed");
294 exit(1);
295 }
Damien Millereba71ba2000-04-29 23:57:08 +1000296 key_free(k);
297 fprintf(stdout, "\n");
298 fclose(fp);
299 exit(0);
300}
301
302void
303do_print_public(struct passwd *pw)
304{
305 Key *k;
Damien Millereba71ba2000-04-29 23:57:08 +1000306 struct stat st;
307
308 if (!have_identity)
309 ask_filename(pw, "Enter file in which the key is");
310 if (stat(identity_file, &st) < 0) {
311 perror(identity_file);
312 exit(1);
313 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100314 k = key_new(KEY_UNSPEC);
Damien Millereba71ba2000-04-29 23:57:08 +1000315 if (!try_load_key(identity_file, k)) {
316 fprintf(stderr, "load failed\n");
317 exit(1);
318 }
Damien Millereba71ba2000-04-29 23:57:08 +1000319 if (!key_write(k, stdout))
320 fprintf(stderr, "key_write failed");
321 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000322 fprintf(stdout, "\n");
323 exit(0);
324}
325
Damien Miller10f6f6b1999-11-17 17:29:08 +1100326void
327do_fingerprint(struct passwd *pw)
328{
Damien Millerad833b32000-08-23 10:46:23 +1000329
Damien Miller98c7ad62000-03-09 21:27:49 +1100330 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000331 Key *public;
Damien Miller98c7ad62000-03-09 21:27:49 +1100332 char *comment = NULL, *cp, *ep, line[16*1024];
Damien Miller0bc1bd82000-11-13 22:57:25 +1100333 int i, skip = 0, num = 1, invalid = 1, success = 0;
Damien Miller95def091999-11-25 00:26:21 +1100334 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100335
Damien Miller95def091999-11-25 00:26:21 +1100336 if (!have_identity)
337 ask_filename(pw, "Enter file in which the key is");
338 if (stat(identity_file, &st) < 0) {
339 perror(identity_file);
340 exit(1);
341 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100342 public = key_new(KEY_RSA1);
Damien Millereba71ba2000-04-29 23:57:08 +1000343 if (load_public_key(identity_file, public, &comment)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100344 success = 1;
345 } else {
Damien Millereba71ba2000-04-29 23:57:08 +1000346 key_free(public);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100347 public = key_new(KEY_UNSPEC);
348 if (try_load_public_key(identity_file, public, &comment))
349 success = 1;
350 else
Ben Lindstrom2941f112000-12-29 16:50:13 +0000351 debug("try_load_public_key KEY_UNSPEC failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100352 }
353 if (success) {
Ben Lindstromca0bf572001-03-11 20:08:29 +0000354 char *digest_md5, *digest_bubblebabble;
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000355
356 digest_md5 = key_fingerprint_ex(public, SSH_FP_MD5, SSH_FP_HEX);
Ben Lindstromca0bf572001-03-11 20:08:29 +0000357 digest_bubblebabble = key_fingerprint_ex(public, SSH_FP_SHA1,
358 SSH_FP_BUBBLEBABBLE);
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000359
Ben Lindstromca0bf572001-03-11 20:08:29 +0000360 if (print_verbose) {
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000361 printf("comment: %s\n", comment);
362 printf("size: %d\n", key_size(public));
363 printf("md5: %s\n", digest_md5);
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000364 printf("bubblebabble: %s\n", digest_bubblebabble);
365 } else {
366 printf("%d %s %s\n", key_size(public), digest_md5, comment);
367 }
368
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369 key_free(public);
370 xfree(comment);
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000371 xfree(digest_md5);
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000372 xfree(digest_bubblebabble);
373
Damien Miller98c7ad62000-03-09 21:27:49 +1100374 exit(0);
375 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100376
377 f = fopen(identity_file, "r");
378 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100379 while (fgets(line, sizeof(line), f)) {
380 i = strlen(line) - 1;
381 if (line[i] != '\n') {
382 error("line %d too long: %.40s...", num, line);
383 skip = 1;
384 continue;
Damien Miller95def091999-11-25 00:26:21 +1100385 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100386 num++;
387 if (skip) {
388 skip = 0;
389 continue;
390 }
391 line[i] = '\0';
392
393 /* Skip leading whitespace, empty and comment lines. */
394 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
395 ;
396 if (!*cp || *cp == '\n' || *cp == '#')
397 continue ;
398 i = strtol(cp, &ep, 10);
399 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
400 int quoted = 0;
401 comment = cp;
402 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
403 if (*cp == '\\' && cp[1] == '"')
404 cp++; /* Skip both */
405 else if (*cp == '"')
406 quoted = !quoted;
407 }
408 if (!*cp)
409 continue;
410 *cp++ = '\0';
411 }
412 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000413 public = key_new(KEY_RSA1);
414 if (key_read(public, &cp) != 1) {
415 cp = ep;
416 key_free(public);
417 public = key_new(KEY_UNSPEC);
418 if (key_read(public, &cp) != 1) {
419 key_free(public);
420 continue;
421 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100422 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000423 comment = *cp ? cp : comment;
424 printf("%d %s %s\n", key_size(public),
425 key_fingerprint(public),
426 comment ? comment : "no comment");
427 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100428 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100429 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100430 }
Damien Millerad833b32000-08-23 10:46:23 +1000431 key_free(public);
Damien Miller98c7ad62000-03-09 21:27:49 +1100432 if (invalid) {
433 printf("%s is not a valid key file.\n", identity_file);
434 exit(1);
435 }
Damien Miller95def091999-11-25 00:26:21 +1100436 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100437}
438
Damien Miller95def091999-11-25 00:26:21 +1100439/*
440 * Perform changing a passphrase. The argument is the passwd structure
441 * for the current user.
442 */
Damien Miller10f6f6b1999-11-17 17:29:08 +1100443void
444do_change_passphrase(struct passwd *pw)
445{
Damien Miller95def091999-11-25 00:26:21 +1100446 char *comment;
447 char *old_passphrase, *passphrase1, *passphrase2;
448 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000449 Key *private;
450 Key *public;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100451 int type = KEY_RSA1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100452
Damien Miller95def091999-11-25 00:26:21 +1100453 if (!have_identity)
454 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100455 if (stat(identity_file, &st) < 0) {
456 perror(identity_file);
457 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100459 public = key_new(type);
460 if (!load_public_key(identity_file, public, NULL)) {
461 type = KEY_UNSPEC;
462 } else {
Damien Millereba71ba2000-04-29 23:57:08 +1000463 /* Clear the public key since we are just about to load the whole file. */
464 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100465 }
Damien Miller95def091999-11-25 00:26:21 +1100466 /* Try to load the file with empty passphrase. */
Damien Millereba71ba2000-04-29 23:57:08 +1000467 private = key_new(type);
468 if (!load_private_key(identity_file, "", private, &comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100469 if (identity_passphrase)
470 old_passphrase = xstrdup(identity_passphrase);
471 else
472 old_passphrase = read_passphrase("Enter old passphrase: ", 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000473 if (!load_private_key(identity_file, old_passphrase, private, &comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100474 memset(old_passphrase, 0, strlen(old_passphrase));
475 xfree(old_passphrase);
476 printf("Bad passphrase.\n");
477 exit(1);
478 }
Damien Miller95def091999-11-25 00:26:21 +1100479 memset(old_passphrase, 0, strlen(old_passphrase));
480 xfree(old_passphrase);
481 }
482 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483
Damien Miller95def091999-11-25 00:26:21 +1100484 /* Ask the new passphrase (twice). */
485 if (identity_new_passphrase) {
486 passphrase1 = xstrdup(identity_new_passphrase);
487 passphrase2 = NULL;
488 } else {
489 passphrase1 =
490 read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
491 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
492
493 /* Verify that they are the same. */
494 if (strcmp(passphrase1, passphrase2) != 0) {
495 memset(passphrase1, 0, strlen(passphrase1));
496 memset(passphrase2, 0, strlen(passphrase2));
497 xfree(passphrase1);
498 xfree(passphrase2);
499 printf("Pass phrases do not match. Try again.\n");
500 exit(1);
501 }
502 /* Destroy the other copy. */
503 memset(passphrase2, 0, strlen(passphrase2));
504 xfree(passphrase2);
505 }
506
507 /* Save the file using the new passphrase. */
Damien Millereba71ba2000-04-29 23:57:08 +1000508 if (!save_private_key(identity_file, passphrase1, private, comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100509 printf("Saving the key failed: %s: %s.\n",
510 identity_file, strerror(errno));
511 memset(passphrase1, 0, strlen(passphrase1));
512 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000513 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100514 xfree(comment);
515 exit(1);
516 }
517 /* Destroy the passphrase and the copy of the key in memory. */
518 memset(passphrase1, 0, strlen(passphrase1));
519 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000520 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100521 xfree(comment);
522
523 printf("Your identification has been saved with the new passphrase.\n");
524 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525}
526
Damien Miller95def091999-11-25 00:26:21 +1100527/*
528 * Change the comment of a private key file.
529 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530void
531do_change_comment(struct passwd *pw)
532{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000533 char new_comment[1024], *comment, *passphrase;
534 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100535 struct stat st;
536 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000537 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Miller95def091999-11-25 00:26:21 +1100539 if (!have_identity)
540 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100541 if (stat(identity_file, &st) < 0) {
542 perror(identity_file);
543 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544 }
Damien Miller5428f641999-11-25 11:54:57 +1100545 /*
546 * Try to load the public key from the file the verify that it is
547 * readable and of the proper format.
548 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100549 public = key_new(KEY_RSA1);
Damien Millereba71ba2000-04-29 23:57:08 +1000550 if (!load_public_key(identity_file, public, NULL)) {
Damien Miller95def091999-11-25 00:26:21 +1100551 printf("%s is not a valid key file.\n", identity_file);
Ben Lindstromd8a90212001-02-15 03:08:27 +0000552 printf("Comments are only supported in RSA1 keys\n");
Damien Miller95def091999-11-25 00:26:21 +1100553 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000554 }
Damien Miller5428f641999-11-25 11:54:57 +1100555
Damien Miller0bc1bd82000-11-13 22:57:25 +1100556 private = key_new(KEY_RSA1);
Damien Millereba71ba2000-04-29 23:57:08 +1000557 if (load_private_key(identity_file, "", private, &comment))
Damien Miller95def091999-11-25 00:26:21 +1100558 passphrase = xstrdup("");
559 else {
Damien Miller95def091999-11-25 00:26:21 +1100560 if (identity_passphrase)
561 passphrase = xstrdup(identity_passphrase);
562 else if (identity_new_passphrase)
563 passphrase = xstrdup(identity_new_passphrase);
564 else
565 passphrase = read_passphrase("Enter passphrase: ", 1);
566 /* Try to load using the passphrase. */
Damien Millereba71ba2000-04-29 23:57:08 +1000567 if (!load_private_key(identity_file, passphrase, private, &comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100568 memset(passphrase, 0, strlen(passphrase));
569 xfree(passphrase);
570 printf("Bad passphrase.\n");
571 exit(1);
572 }
573 }
574 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller95def091999-11-25 00:26:21 +1100576 if (identity_comment) {
577 strlcpy(new_comment, identity_comment, sizeof(new_comment));
578 } else {
579 printf("Enter new comment: ");
580 fflush(stdout);
581 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
582 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000583 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100584 exit(1);
585 }
Damien Miller95def091999-11-25 00:26:21 +1100586 if (strchr(new_comment, '\n'))
587 *strchr(new_comment, '\n') = 0;
588 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller95def091999-11-25 00:26:21 +1100590 /* Save the file using the new passphrase. */
Damien Millereba71ba2000-04-29 23:57:08 +1000591 if (!save_private_key(identity_file, passphrase, private, new_comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100592 printf("Saving the key failed: %s: %s.\n",
593 identity_file, strerror(errno));
594 memset(passphrase, 0, strlen(passphrase));
595 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000596 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100597 xfree(comment);
598 exit(1);
599 }
Damien Miller95def091999-11-25 00:26:21 +1100600 memset(passphrase, 0, strlen(passphrase));
601 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000602 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Damien Miller95def091999-11-25 00:26:21 +1100604 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000605 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
606 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100607 printf("Could not save your public key in %s\n", identity_file);
608 exit(1);
609 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000610 f = fdopen(fd, "w");
611 if (f == NULL) {
612 printf("fdopen %s failed", identity_file);
613 exit(1);
614 }
Damien Millereba71ba2000-04-29 23:57:08 +1000615 if (!key_write(public, f))
616 fprintf(stderr, "write key failed");
617 key_free(public);
618 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +1100619 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Damien Miller95def091999-11-25 00:26:21 +1100621 xfree(comment);
622
623 printf("The comment in your key file has been changed.\n");
624 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625}
626
Damien Miller431f66b1999-11-21 18:31:57 +1100627void
628usage(void)
629{
Ben Lindstrom7ab51172001-03-05 05:13:38 +0000630 printf("Usage: %s [-lpqxXyc] [-t type] [-b bits] [-f file] [-C comment] "
Ben Lindstromb7c92322001-03-05 05:10:52 +0000631 "[-N new-pass] [-P pass]\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100632 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +1100633}
634
Damien Miller95def091999-11-25 00:26:21 +1100635/*
636 * Main program for key management.
637 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638int
639main(int ac, char **av)
640{
Damien Miller95def091999-11-25 00:26:21 +1100641 char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000642 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +1100643 struct passwd *pw;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000644 int opt, type, fd;
Damien Miller95def091999-11-25 00:26:21 +1100645 struct stat st;
646 FILE *f;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100647
Damien Miller95def091999-11-25 00:26:21 +1100648 extern int optind;
649 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000651 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000652 init_rng();
653
Damien Millerd3a18572000-06-07 19:55:44 +1000654 SSLeay_add_all_algorithms();
Damien Millereba71ba2000-04-29 23:57:08 +1000655
Damien Miller5428f641999-11-25 11:54:57 +1100656 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +1100657 pw = getpwuid(getuid());
658 if (!pw) {
659 printf("You don't exist, go away!\n");
660 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661 }
Damien Millereba71ba2000-04-29 23:57:08 +1000662 if (gethostname(hostname, sizeof(hostname)) < 0) {
663 perror("gethostname");
664 exit(1);
665 }
Damien Miller5428f641999-11-25 11:54:57 +1100666
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000667 while ((opt = getopt(ac, av, "dqpclRxXyvb:f:t:P:N:C:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100668 switch (opt) {
669 case 'b':
670 bits = atoi(optarg);
671 if (bits < 512 || bits > 32768) {
672 printf("Bits has bad value.\n");
673 exit(1);
674 }
675 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676
Damien Miller95def091999-11-25 00:26:21 +1100677 case 'l':
678 print_fingerprint = 1;
679 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000680
Damien Miller95def091999-11-25 00:26:21 +1100681 case 'p':
682 change_passphrase = 1;
683 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
Damien Miller95def091999-11-25 00:26:21 +1100685 case 'c':
686 change_comment = 1;
687 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688
Damien Miller95def091999-11-25 00:26:21 +1100689 case 'f':
690 strlcpy(identity_file, optarg, sizeof(identity_file));
691 have_identity = 1;
692 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693
Damien Miller95def091999-11-25 00:26:21 +1100694 case 'P':
695 identity_passphrase = optarg;
696 break;
697
698 case 'N':
699 identity_new_passphrase = optarg;
700 break;
701
702 case 'C':
703 identity_comment = optarg;
704 break;
705
706 case 'q':
707 quiet = 1;
708 break;
709
Damien Millereba71ba2000-04-29 23:57:08 +1000710 case 'R':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100711 /* unused */
712 exit(0);
Damien Millereba71ba2000-04-29 23:57:08 +1000713 break;
714
715 case 'x':
716 convert_to_ssh2 = 1;
717 break;
718
719 case 'X':
720 convert_from_ssh2 = 1;
721 break;
722
723 case 'y':
724 print_public = 1;
725 break;
726
Ben Lindstroma8a73e62001-03-11 20:05:19 +0000727 case 'v':
728 print_verbose = 1;
729 break;
730
Damien Millereba71ba2000-04-29 23:57:08 +1000731 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +1100732 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +1000733 break;
734
Damien Miller0bc1bd82000-11-13 22:57:25 +1100735 case 't':
736 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100737 break;
738
Damien Miller95def091999-11-25 00:26:21 +1100739 case '?':
740 default:
741 usage();
742 }
743 }
744 if (optind < ac) {
745 printf("Too many arguments.\n");
746 usage();
747 }
748 if (change_passphrase && change_comment) {
749 printf("Can only have one of -p and -c.\n");
750 usage();
751 }
752 if (print_fingerprint)
753 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +1100754 if (change_passphrase)
755 do_change_passphrase(pw);
Damien Miller95def091999-11-25 00:26:21 +1100756 if (change_comment)
757 do_change_comment(pw);
Damien Millereba71ba2000-04-29 23:57:08 +1000758 if (convert_to_ssh2)
759 do_convert_to_ssh2(pw);
760 if (convert_from_ssh2)
761 do_convert_from_ssh2(pw);
762 if (print_public)
763 do_print_public(pw);
Damien Miller95def091999-11-25 00:26:21 +1100764
765 arc4random_stir();
766
Damien Millere39cacc2000-11-29 12:18:44 +1100767 type = key_type_from_name(key_type_name);
768 if (type == KEY_UNSPEC) {
769 fprintf(stderr, "unknown key type %s\n", key_type_name);
770 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +1000771 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100772 if (!quiet)
Damien Millere39cacc2000-11-29 12:18:44 +1100773 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100774 private = key_generate(type, bits);
775 if (private == NULL) {
776 fprintf(stderr, "key_generate failed");
777 exit(1);
778 }
779 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +1100780
781 if (!have_identity)
782 ask_filename(pw, "Enter file in which to save the key");
783
784 /* Create ~/.ssh directory if it doesn\'t already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000785 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100786 if (strstr(identity_file, dotsshdir) != NULL &&
787 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +1000788 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100789 error("Could not create directory '%s'.", dotsshdir);
790 else if (!quiet)
791 printf("Created directory '%s'.\n", dotsshdir);
792 }
793 /* If the file already exists, ask the user to confirm. */
794 if (stat(identity_file, &st) >= 0) {
795 char yesno[3];
796 printf("%s already exists.\n", identity_file);
797 printf("Overwrite (y/n)? ");
798 fflush(stdout);
799 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
800 exit(1);
801 if (yesno[0] != 'y' && yesno[0] != 'Y')
802 exit(1);
803 }
804 /* Ask for a passphrase (twice). */
805 if (identity_passphrase)
806 passphrase1 = xstrdup(identity_passphrase);
807 else if (identity_new_passphrase)
808 passphrase1 = xstrdup(identity_new_passphrase);
809 else {
810passphrase_again:
811 passphrase1 =
812 read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
813 passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
814 if (strcmp(passphrase1, passphrase2) != 0) {
815 /* The passphrases do not match. Clear them and retry. */
816 memset(passphrase1, 0, strlen(passphrase1));
817 memset(passphrase2, 0, strlen(passphrase2));
818 xfree(passphrase1);
819 xfree(passphrase2);
820 printf("Passphrases do not match. Try again.\n");
821 goto passphrase_again;
822 }
823 /* Clear the other copy of the passphrase. */
824 memset(passphrase2, 0, strlen(passphrase2));
825 xfree(passphrase2);
826 }
827
Damien Miller95def091999-11-25 00:26:21 +1100828 if (identity_comment) {
829 strlcpy(comment, identity_comment, sizeof(comment));
830 } else {
Damien Miller4af51302000-04-16 11:18:38 +1000831 /* Create default commend field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +1100832 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
833 }
834
835 /* Save the key with the given passphrase and comment. */
Damien Millereba71ba2000-04-29 23:57:08 +1000836 if (!save_private_key(identity_file, passphrase1, private, comment)) {
Damien Miller95def091999-11-25 00:26:21 +1100837 printf("Saving the key failed: %s: %s.\n",
Damien Millereba71ba2000-04-29 23:57:08 +1000838 identity_file, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100839 memset(passphrase1, 0, strlen(passphrase1));
840 xfree(passphrase1);
841 exit(1);
842 }
843 /* Clear the passphrase. */
844 memset(passphrase1, 0, strlen(passphrase1));
845 xfree(passphrase1);
846
847 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100848 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100849 arc4random_stir();
850
851 if (!quiet)
852 printf("Your identification has been saved in %s.\n", identity_file);
853
Damien Miller95def091999-11-25 00:26:21 +1100854 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000855 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
856 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100857 printf("Could not save your public key in %s\n", identity_file);
858 exit(1);
859 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000860 f = fdopen(fd, "w");
861 if (f == NULL) {
862 printf("fdopen %s failed", identity_file);
863 exit(1);
864 }
Damien Millereba71ba2000-04-29 23:57:08 +1000865 if (!key_write(public, f))
866 fprintf(stderr, "write key failed");
867 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100868 fclose(f);
869
870 if (!quiet) {
Damien Millereba71ba2000-04-29 23:57:08 +1000871 printf("Your public key has been saved in %s.\n",
872 identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100873 printf("The key fingerprint is:\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000874 printf("%s %s\n", key_fingerprint(public), comment);
Damien Miller95def091999-11-25 00:26:21 +1100875 }
Damien Millereba71ba2000-04-29 23:57:08 +1000876
877 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +1100878 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879}