version 1.134, 2005/11/28 05:16:53 |
version 1.140, 2006/03/20 18:26:55 |
|
|
*/ |
*/ |
|
|
#include "includes.h" |
#include "includes.h" |
RCSID("$OpenBSD$"); |
|
|
|
|
#include <sys/types.h> |
|
#include <sys/stat.h> |
|
|
#include <openssl/evp.h> |
#include <openssl/evp.h> |
#include <openssl/pem.h> |
#include <openssl/pem.h> |
|
|
|
|
|
|
if (key_type_name == NULL) |
if (key_type_name == NULL) |
name = _PATH_SSH_CLIENT_ID_RSA; |
name = _PATH_SSH_CLIENT_ID_RSA; |
else |
else { |
switch (key_type_from_name(key_type_name)) { |
switch (key_type_from_name(key_type_name)) { |
case KEY_RSA1: |
case KEY_RSA1: |
name = _PATH_SSH_CLIENT_IDENTITY; |
name = _PATH_SSH_CLIENT_IDENTITY; |
|
|
exit(1); |
exit(1); |
break; |
break; |
} |
} |
|
} |
snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); |
snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); |
fprintf(stderr, "%s (%s): ", prompt, identity_file); |
fprintf(stderr, "%s (%s): ", prompt, identity_file); |
if (fgets(buf, sizeof(buf), stdin) == NULL) |
if (fgets(buf, sizeof(buf), stdin) == NULL) |
|
|
return key; |
return key; |
} |
} |
|
|
|
static int |
|
get_line(FILE *fp, char *line, size_t len) |
|
{ |
|
int c; |
|
size_t pos = 0; |
|
|
|
line[0] = '\0'; |
|
while ((c = fgetc(fp)) != EOF) { |
|
if (pos >= len - 1) { |
|
fprintf(stderr, "input line too long.\n"); |
|
exit(1); |
|
} |
|
switch (c) { |
|
case '\r': |
|
c = fgetc(fp); |
|
if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) { |
|
fprintf(stderr, "unget: %s\n", strerror(errno)); |
|
exit(1); |
|
} |
|
return pos; |
|
case '\n': |
|
return pos; |
|
} |
|
line[pos++] = c; |
|
line[pos] = '\0'; |
|
} |
|
return pos; |
|
} |
|
|
static void |
static void |
do_convert_from_ssh2(struct passwd *pw) |
do_convert_from_ssh2(struct passwd *pw) |
{ |
{ |
Key *k; |
Key *k; |
int blen; |
int blen; |
u_int len; |
u_int len; |
char line[1024], *p; |
char line[1024]; |
u_char blob[8096]; |
u_char blob[8096]; |
char encoded[8096]; |
char encoded[8096]; |
struct stat st; |
struct stat st; |
|
|
exit(1); |
exit(1); |
} |
} |
encoded[0] = '\0'; |
encoded[0] = '\0'; |
while (fgets(line, sizeof(line), fp)) { |
while ((blen = get_line(fp, line, sizeof(line))) != -1) { |
if (!(p = strchr(line, '\n'))) { |
if (line[blen - 1] == '\\') |
fprintf(stderr, "input line too long.\n"); |
|
exit(1); |
|
} |
|
if (p > line && p[-1] == '\\') |
|
escaped++; |
escaped++; |
if (strncmp(line, "----", 4) == 0 || |
if (strncmp(line, "----", 4) == 0 || |
strstr(line, ": ") != NULL) { |
strstr(line, ": ") != NULL) { |
|
|
/* fprintf(stderr, "escaped: %s", line); */ |
/* fprintf(stderr, "escaped: %s", line); */ |
continue; |
continue; |
} |
} |
*p = '\0'; |
|
strlcat(encoded, line, sizeof(encoded)); |
strlcat(encoded, line, sizeof(encoded)); |
} |
} |
len = strlen(encoded); |
len = strlen(encoded); |
|
|
/* |
/* |
* Print the SSHFP RR. |
* Print the SSHFP RR. |
*/ |
*/ |
static void |
static int |
do_print_resource_record(struct passwd *pw, char *hname) |
do_print_resource_record(struct passwd *pw, char *fname, char *hname) |
{ |
{ |
Key *public; |
Key *public; |
char *comment = NULL; |
char *comment = NULL; |
struct stat st; |
struct stat st; |
|
|
if (!have_identity) |
if (fname == NULL) |
ask_filename(pw, "Enter file in which the key is"); |
ask_filename(pw, "Enter file in which the key is"); |
if (stat(identity_file, &st) < 0) { |
if (stat(fname, &st) < 0) { |
perror(identity_file); |
if (errno == ENOENT) |
|
return 0; |
|
perror(fname); |
exit(1); |
exit(1); |
} |
} |
public = key_load_public(identity_file, &comment); |
public = key_load_public(fname, &comment); |
if (public != NULL) { |
if (public != NULL) { |
export_dns_rr(hname, public, stdout, print_generic); |
export_dns_rr(hname, public, stdout, print_generic); |
key_free(public); |
key_free(public); |
xfree(comment); |
xfree(comment); |
exit(0); |
return 1; |
} |
} |
if (comment) |
if (comment) |
xfree(comment); |
xfree(comment); |
|
|
printf("failed to read v2 public key from %s.\n", identity_file); |
printf("failed to read v2 public key from %s.\n", fname); |
exit(1); |
exit(1); |
} |
} |
|
|
|
|
break; |
break; |
case 'D': |
case 'D': |
download = 1; |
download = 1; |
|
/*FALLTHROUGH*/ |
case 'U': |
case 'U': |
reader_id = optarg; |
reader_id = optarg; |
break; |
break; |
|
|
if (print_public) |
if (print_public) |
do_print_public(pw); |
do_print_public(pw); |
if (rr_hostname != NULL) { |
if (rr_hostname != NULL) { |
do_print_resource_record(pw, rr_hostname); |
unsigned int n = 0; |
|
|
|
if (have_identity) { |
|
n = do_print_resource_record(pw, |
|
identity_file, rr_hostname); |
|
if (n == 0) { |
|
perror(identity_file); |
|
exit(1); |
|
} |
|
exit(0); |
|
} else { |
|
|
|
n += do_print_resource_record(pw, |
|
_PATH_HOST_RSA_KEY_FILE, rr_hostname); |
|
n += do_print_resource_record(pw, |
|
_PATH_HOST_DSA_KEY_FILE, rr_hostname); |
|
|
|
if (n == 0) |
|
fatal("no keys found."); |
|
exit(0); |
|
} |
} |
} |
if (reader_id != NULL) { |
if (reader_id != NULL) { |
#ifdef SMARTCARD |
#ifdef SMARTCARD |
|
|
fprintf(stderr, "unknown key type %s\n", key_type_name); |
fprintf(stderr, "unknown key type %s\n", key_type_name); |
exit(1); |
exit(1); |
} |
} |
|
if (bits == 0) |
|
bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS; |
if (type == KEY_DSA && bits != 1024) |
if (type == KEY_DSA && bits != 1024) |
fatal("DSA keys must be 1024 bits"); |
fatal("DSA keys must be 1024 bits"); |
if (!quiet) |
if (!quiet) |
printf("Generating public/private %s key pair.\n", key_type_name); |
printf("Generating public/private %s key pair.\n", key_type_name); |
if (bits == 0) |
|
bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS; |
|
private = key_generate(type, bits); |
private = key_generate(type, bits); |
if (private == NULL) { |
if (private == NULL) { |
fprintf(stderr, "key_generate failed"); |
fprintf(stderr, "key_generate failed"); |