⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssh-keygen.c

📁 OpenSSL Source code for SFTP, SSH, and many others
💻 C
📖 第 1 页 / 共 2 页
字号:
						quoted = !quoted;				}				if (!*cp)					continue;				*cp++ = '\0';			}			ep = cp;			public = key_new(KEY_RSA1);			if (key_read(public, &cp) != 1) {				cp = ep;				key_free(public);				public = key_new(KEY_UNSPEC);				if (key_read(public, &cp) != 1) {					key_free(public);					continue;				}			}			comment = *cp ? cp : comment;			fp = key_fingerprint(public, fptype, rep);			printf("%d %s %s\n", key_size(public), fp,			    comment ? comment : "no comment");			xfree(fp);			key_free(public);			invalid = 0;		}		fclose(f);	}	if (invalid) {		printf("%s is not a public key file.\n", identity_file);		exit(1);	}	exit(0);}/* * Perform changing a passphrase.  The argument is the passwd structure * for the current user. */static voiddo_change_passphrase(struct passwd *pw){	char *comment;	char *old_passphrase, *passphrase1, *passphrase2;	struct stat st;	Key *private;	if (!have_identity)		ask_filename(pw, "Enter file in which the key is");	if (stat(identity_file, &st) < 0) {		perror(identity_file);		exit(1);	}	/* Try to load the file with empty passphrase. */	private = key_load_private(identity_file, "", &comment);	if (private == NULL) {		if (identity_passphrase)			old_passphrase = xstrdup(identity_passphrase);		else			old_passphrase =			    read_passphrase("Enter old passphrase: ",			    RP_ALLOW_STDIN);		private = key_load_private(identity_file, old_passphrase,		    &comment);		memset(old_passphrase, 0, strlen(old_passphrase));		xfree(old_passphrase);		if (private == NULL) {			printf("Bad passphrase.\n");			exit(1);		}	}	printf("Key has comment '%s'\n", comment);	/* Ask the new passphrase (twice). */	if (identity_new_passphrase) {		passphrase1 = xstrdup(identity_new_passphrase);		passphrase2 = NULL;	} else {		passphrase1 =			read_passphrase("Enter new passphrase (empty for no "			    "passphrase): ", RP_ALLOW_STDIN);		passphrase2 = read_passphrase("Enter same passphrase again: ",		    RP_ALLOW_STDIN);		/* Verify that they are the same. */		if (strcmp(passphrase1, passphrase2) != 0) {			memset(passphrase1, 0, strlen(passphrase1));			memset(passphrase2, 0, strlen(passphrase2));			xfree(passphrase1);			xfree(passphrase2);			printf("Pass phrases do not match.  Try again.\n");			exit(1);		}		/* Destroy the other copy. */		memset(passphrase2, 0, strlen(passphrase2));		xfree(passphrase2);	}	/* Save the file using the new passphrase. */	if (!key_save_private(private, identity_file, passphrase1, comment)) {		printf("Saving the key failed: %s.\n", identity_file);		memset(passphrase1, 0, strlen(passphrase1));		xfree(passphrase1);		key_free(private);		xfree(comment);		exit(1);	}	/* Destroy the passphrase and the copy of the key in memory. */	memset(passphrase1, 0, strlen(passphrase1));	xfree(passphrase1);	key_free(private);		 /* Destroys contents */	xfree(comment);	printf("Your identification has been saved with the new passphrase.\n");	exit(0);}/* * Change the comment of a private key file. */static voiddo_change_comment(struct passwd *pw){	char new_comment[1024], *comment, *passphrase;	Key *private;	Key *public;	struct stat st;	FILE *f;	int fd;	if (!have_identity)		ask_filename(pw, "Enter file in which the key is");	if (stat(identity_file, &st) < 0) {		perror(identity_file);		exit(1);	}	private = key_load_private(identity_file, "", &comment);	if (private == NULL) {		if (identity_passphrase)			passphrase = xstrdup(identity_passphrase);		else if (identity_new_passphrase)			passphrase = xstrdup(identity_new_passphrase);		else			passphrase = read_passphrase("Enter passphrase: ",			    RP_ALLOW_STDIN);		/* Try to load using the passphrase. */		private = key_load_private(identity_file, passphrase, &comment);		if (private == NULL) {			memset(passphrase, 0, strlen(passphrase));			xfree(passphrase);			printf("Bad passphrase.\n");			exit(1);		}	} else {		passphrase = xstrdup("");	}	if (private->type != KEY_RSA1) {		fprintf(stderr, "Comments are only supported for RSA1 keys.\n");		key_free(private);		exit(1);	}	printf("Key now has comment '%s'\n", comment);	if (identity_comment) {		strlcpy(new_comment, identity_comment, sizeof(new_comment));	} else {		printf("Enter new comment: ");		fflush(stdout);		if (!fgets(new_comment, sizeof(new_comment), stdin)) {			memset(passphrase, 0, strlen(passphrase));			key_free(private);			exit(1);		}		if (strchr(new_comment, '\n'))			*strchr(new_comment, '\n') = 0;	}	/* Save the file using the new passphrase. */	if (!key_save_private(private, identity_file, passphrase, new_comment)) {		printf("Saving the key failed: %s.\n", identity_file);		memset(passphrase, 0, strlen(passphrase));		xfree(passphrase);		key_free(private);		xfree(comment);		exit(1);	}	memset(passphrase, 0, strlen(passphrase));	xfree(passphrase);	public = key_from_private(private);	key_free(private);	strlcat(identity_file, ".pub", sizeof(identity_file));	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);	if (fd == -1) {		printf("Could not save your public key in %s\n", identity_file);		exit(1);	}	f = fdopen(fd, "w");	if (f == NULL) {		printf("fdopen %s failed", identity_file);		exit(1);	}	if (!key_write(public, f))		fprintf(stderr, "write key failed");	key_free(public);	fprintf(f, " %s\n", new_comment);	fclose(f);	xfree(comment);	printf("The comment in your key file has been changed.\n");	exit(0);}static voidusage(void){	fprintf(stderr, "Usage: %s [options]\n", __progname);	fprintf(stderr, "Options:\n");	fprintf(stderr, "  -b bits     Number of bits in the key to create.\n");	fprintf(stderr, "  -c          Change comment in private and public key files.\n");	fprintf(stderr, "  -e          Convert OpenSSH to IETF SECSH key file.\n");	fprintf(stderr, "  -f filename Filename of the key file.\n");	fprintf(stderr, "  -i          Convert IETF SECSH to OpenSSH key file.\n");	fprintf(stderr, "  -l          Show fingerprint of key file.\n");	fprintf(stderr, "  -p          Change passphrase of private key file.\n");	fprintf(stderr, "  -q          Quiet.\n");	fprintf(stderr, "  -y          Read private key file and print public key.\n");	fprintf(stderr, "  -t type     Specify type of key to create.\n");	fprintf(stderr, "  -B          Show bubblebabble digest of key file.\n");	fprintf(stderr, "  -C comment  Provide new comment.\n");	fprintf(stderr, "  -N phrase   Provide new passphrase.\n");	fprintf(stderr, "  -P phrase   Provide old passphrase.\n");#ifdef SMARTCARD	fprintf(stderr, "  -D reader   Download public key from smartcard.\n");	fprintf(stderr, "  -U reader   Upload private key to smartcard.\n");#endif /* SMARTCARD */	exit(1);}/* * Main program for key management. */intmain(int ac, char **av){	char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;	char *reader_id = NULL;	Key *private, *public;	struct passwd *pw;	struct stat st;	int opt, type, fd, download = 0;	FILE *f;	extern int optind;	extern char *optarg;	__progname = get_progname(av[0]);	SSLeay_add_all_algorithms();	/* we need this for the home * directory.  */	pw = getpwuid(getuid());	if (!pw) {		printf("You don't exist, go away!\n");		exit(1);	}	if (gethostname(hostname, sizeof(hostname)) < 0) {		perror("gethostname");		exit(1);	}	while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {		switch (opt) {		case 'b':			bits = atoi(optarg);			if (bits < 512 || bits > 32768) {				printf("Bits has bad value.\n");				exit(1);			}			break;		case 'l':			print_fingerprint = 1;			break;		case 'B':			print_bubblebabble = 1;			break;		case 'p':			change_passphrase = 1;			break;		case 'c':			change_comment = 1;			break;		case 'f':			strlcpy(identity_file, optarg, sizeof(identity_file));			have_identity = 1;			break;		case 'P':			identity_passphrase = optarg;			break;		case 'N':			identity_new_passphrase = optarg;			break;		case 'C':			identity_comment = optarg;			break;		case 'q':			quiet = 1;			break;		case 'R':			/* unused */			exit(0);			break;		case 'e':		case 'x':			/* export key */			convert_to_ssh2 = 1;			break;		case 'i':		case 'X':			/* import key */			convert_from_ssh2 = 1;			break;		case 'y':			print_public = 1;			break;		case 'd':			key_type_name = "dsa";			break;		case 't':			key_type_name = optarg;			break;		case 'D':			download = 1;		case 'U':			reader_id = optarg;			break;		case '?':		default:			usage();		}	}	if (optind < ac) {		printf("Too many arguments.\n");		usage();	}	if (change_passphrase && change_comment) {		printf("Can only have one of -p and -c.\n");		usage();	}	if (print_fingerprint || print_bubblebabble)		do_fingerprint(pw);	if (change_passphrase)		do_change_passphrase(pw);	if (convert_to_ssh2)		do_convert_to_ssh2(pw);	if (change_comment)		do_change_comment(pw);	if (print_public)		do_print_public(pw);	if (reader_id != NULL) {#ifdef SMARTCARD		if (download)			do_download(pw, reader_id);		else			do_upload(pw, reader_id);#else /* SMARTCARD */		fatal("no support for smartcards.");#endif /* SMARTCARD */	}	init_rng();	seed_rng();	arc4random_stir();	if (convert_from_ssh2)		do_convert_from_ssh2(pw);	if (key_type_name == NULL) {		printf("You must specify a key type (-t).\n");		usage();	}	type = key_type_from_name(key_type_name);	if (type == KEY_UNSPEC) {		fprintf(stderr, "unknown key type %s\n", key_type_name);		exit(1);	}	if (!quiet)		printf("Generating public/private %s key pair.\n", key_type_name);	private = key_generate(type, bits);	if (private == NULL) {		fprintf(stderr, "key_generate failed");		exit(1);	}	public  = key_from_private(private);	if (!have_identity)		ask_filename(pw, "Enter file in which to save the key");	/* Create ~/.ssh directory if it doesn\'t already exist. */	snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);	if (strstr(identity_file, dotsshdir) != NULL &&	    stat(dotsshdir, &st) < 0) {		if (mkdir(dotsshdir, 0700) < 0)			error("Could not create directory '%s'.", dotsshdir);		else if (!quiet)			printf("Created directory '%s'.\n", dotsshdir);	}	/* If the file already exists, ask the user to confirm. */	if (stat(identity_file, &st) >= 0) {		char yesno[3];		printf("%s already exists.\n", identity_file);		printf("Overwrite (y/n)? ");		fflush(stdout);		if (fgets(yesno, sizeof(yesno), stdin) == NULL)			exit(1);		if (yesno[0] != 'y' && yesno[0] != 'Y')			exit(1);	}	/* Ask for a passphrase (twice). */	if (identity_passphrase)		passphrase1 = xstrdup(identity_passphrase);	else if (identity_new_passphrase)		passphrase1 = xstrdup(identity_new_passphrase);	else {passphrase_again:		passphrase1 =			read_passphrase("Enter passphrase (empty for no "			    "passphrase): ", RP_ALLOW_STDIN);		passphrase2 = read_passphrase("Enter same passphrase again: ",		    RP_ALLOW_STDIN);		if (strcmp(passphrase1, passphrase2) != 0) {			/*			 * The passphrases do not match.  Clear them and			 * retry.			 */			memset(passphrase1, 0, strlen(passphrase1));			memset(passphrase2, 0, strlen(passphrase2));			xfree(passphrase1);			xfree(passphrase2);			printf("Passphrases do not match.  Try again.\n");			goto passphrase_again;		}		/* Clear the other copy of the passphrase. */		memset(passphrase2, 0, strlen(passphrase2));		xfree(passphrase2);	}	if (identity_comment) {		strlcpy(comment, identity_comment, sizeof(comment));	} else {		/* Create default commend field for the passphrase. */		snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);	}	/* Save the key with the given passphrase and comment. */	if (!key_save_private(private, identity_file, passphrase1, comment)) {		printf("Saving the key failed: %s.\n", identity_file);		memset(passphrase1, 0, strlen(passphrase1));		xfree(passphrase1);		exit(1);	}	/* Clear the passphrase. */	memset(passphrase1, 0, strlen(passphrase1));	xfree(passphrase1);	/* Clear the private key and the random number generator. */	key_free(private);	arc4random_stir();	if (!quiet)		printf("Your identification has been saved in %s.\n", identity_file);	strlcat(identity_file, ".pub", sizeof(identity_file));	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);	if (fd == -1) {		printf("Could not save your public key in %s\n", identity_file);		exit(1);	}	f = fdopen(fd, "w");	if (f == NULL) {		printf("fdopen %s failed", identity_file);		exit(1);	}	if (!key_write(public, f))		fprintf(stderr, "write key failed");	fprintf(f, " %s\n", comment);	fclose(f);	if (!quiet) {		char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);		printf("Your public key has been saved in %s.\n",		    identity_file);		printf("The key fingerprint is:\n");		printf("%s %s\n", fp, comment);		xfree(fp);	}	key_free(public);	exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -