mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-03-22 14:39:24 -05:00
Centralise public-key output code into sshpubk.c.
There was a fair amount of duplication between Windows and Unix PuTTYgen, and some confusion over writing things to FILE * and formatting them internally into strings. I think all the public-key output code now lives in sshpubk.c, and there's only one copy of the code to generate each format.
This commit is contained in:
parent
f274b56a57
commit
eef0235a0f
127
cmdgen.c
127
cmdgen.c
@ -173,56 +173,6 @@ void help(void)
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int save_ssh2_pubkey(char *filename, char *comment,
|
|
||||||
void *v_pub_blob, int pub_len)
|
|
||||||
{
|
|
||||||
unsigned char *pub_blob = (unsigned char *)v_pub_blob;
|
|
||||||
char *p;
|
|
||||||
int i, column;
|
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
if (filename) {
|
|
||||||
fp = fopen(filename, "wb");
|
|
||||||
if (!fp)
|
|
||||||
return 0;
|
|
||||||
} else
|
|
||||||
fp = stdout;
|
|
||||||
|
|
||||||
fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
|
|
||||||
|
|
||||||
if (comment) {
|
|
||||||
fprintf(fp, "Comment: \"");
|
|
||||||
for (p = comment; *p; p++) {
|
|
||||||
if (*p == '\\' || *p == '\"')
|
|
||||||
fputc('\\', fp);
|
|
||||||
fputc(*p, fp);
|
|
||||||
}
|
|
||||||
fprintf(fp, "\"\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
column = 0;
|
|
||||||
while (i < pub_len) {
|
|
||||||
char buf[5];
|
|
||||||
int n = (pub_len - i < 3 ? pub_len - i : 3);
|
|
||||||
base64_encode_atom(pub_blob + i, n, buf);
|
|
||||||
i += n;
|
|
||||||
buf[4] = '\0';
|
|
||||||
fputs(buf, fp);
|
|
||||||
if (++column >= 16) {
|
|
||||||
fputc('\n', fp);
|
|
||||||
column = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (column > 0)
|
|
||||||
fputc('\n', fp);
|
|
||||||
|
|
||||||
fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
|
|
||||||
if (filename)
|
|
||||||
fclose(fp);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int move(char *from, char *to)
|
static int move(char *from, char *to)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@ -990,79 +940,32 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
case PUBLIC:
|
case PUBLIC:
|
||||||
case PUBLICO:
|
case PUBLICO:
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
|
if (outfile)
|
||||||
|
fp = f_open(outfilename, "w", FALSE);
|
||||||
|
else
|
||||||
|
fp = stdout;
|
||||||
|
|
||||||
if (sshver == 1) {
|
if (sshver == 1) {
|
||||||
FILE *fp;
|
ssh1_write_pubkey(fp, ssh1key);
|
||||||
char *dec1, *dec2;
|
} else {
|
||||||
|
|
||||||
assert(ssh1key);
|
|
||||||
|
|
||||||
if (outfile)
|
|
||||||
fp = f_open(outfilename, "w", FALSE);
|
|
||||||
else
|
|
||||||
fp = stdout;
|
|
||||||
dec1 = bignum_decimal(ssh1key->exponent);
|
|
||||||
dec2 = bignum_decimal(ssh1key->modulus);
|
|
||||||
fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
|
|
||||||
dec1, dec2, ssh1key->comment);
|
|
||||||
sfree(dec1);
|
|
||||||
sfree(dec2);
|
|
||||||
if (outfile)
|
|
||||||
fclose(fp);
|
|
||||||
} else if (outtype == PUBLIC) {
|
|
||||||
if (!ssh2blob) {
|
if (!ssh2blob) {
|
||||||
assert(ssh2key);
|
assert(ssh2key);
|
||||||
ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
|
ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
|
||||||
&ssh2bloblen);
|
&ssh2bloblen);
|
||||||
}
|
}
|
||||||
save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
|
|
||||||
ssh2blob, ssh2bloblen);
|
|
||||||
} else if (outtype == PUBLICO) {
|
|
||||||
char *buffer, *p;
|
|
||||||
int i;
|
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
if (!ssh2blob) {
|
ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
|
||||||
assert(ssh2key);
|
ssh2blob, ssh2bloblen,
|
||||||
ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
|
(outtype == PUBLIC ?
|
||||||
&ssh2bloblen);
|
SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
|
||||||
|
SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
|
||||||
}
|
}
|
||||||
if (!ssh2alg) {
|
|
||||||
assert(ssh2key);
|
|
||||||
ssh2alg = ssh2key->alg->name;
|
|
||||||
}
|
|
||||||
if (ssh2key)
|
|
||||||
comment = ssh2key->comment;
|
|
||||||
else
|
|
||||||
comment = origcomment;
|
|
||||||
|
|
||||||
buffer = snewn(strlen(ssh2alg) +
|
|
||||||
4 * ((ssh2bloblen+2) / 3) +
|
|
||||||
strlen(comment) + 3, char);
|
|
||||||
strcpy(buffer, ssh2alg);
|
|
||||||
p = buffer + strlen(buffer);
|
|
||||||
*p++ = ' ';
|
|
||||||
i = 0;
|
|
||||||
while (i < ssh2bloblen) {
|
|
||||||
int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
|
|
||||||
base64_encode_atom(ssh2blob + i, n, p);
|
|
||||||
i += n;
|
|
||||||
p += 4;
|
|
||||||
}
|
|
||||||
if (*comment) {
|
|
||||||
*p++ = ' ';
|
|
||||||
strcpy(p, comment);
|
|
||||||
} else
|
|
||||||
*p++ = '\0';
|
|
||||||
|
|
||||||
if (outfile)
|
|
||||||
fp = f_open(outfilename, "w", FALSE);
|
|
||||||
else
|
|
||||||
fp = stdout;
|
|
||||||
fprintf(fp, "%s\n", buffer);
|
|
||||||
if (outfile)
|
if (outfile)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
sfree(buffer);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
6
ssh.h
6
ssh.h
@ -716,6 +716,12 @@ enum {
|
|||||||
SSH_KEYTYPE_SSH2_PUBLIC_RFC4716,
|
SSH_KEYTYPE_SSH2_PUBLIC_RFC4716,
|
||||||
SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH
|
SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH
|
||||||
};
|
};
|
||||||
|
char *ssh1_pubkey_str(struct RSAKey *ssh1key);
|
||||||
|
void ssh1_write_pubkey(FILE *fp, struct RSAKey *ssh1key);
|
||||||
|
char *ssh2_pubkey_openssh_str(struct ssh2_userkey *key);
|
||||||
|
void ssh2_write_pubkey(FILE *fp, const char *comment,
|
||||||
|
const void *v_pub_blob, int pub_len,
|
||||||
|
int keytype);
|
||||||
int key_type(const Filename *filename);
|
int key_type(const Filename *filename);
|
||||||
char *key_type_to_str(int type);
|
char *key_type_to_str(int type);
|
||||||
|
|
||||||
|
135
sshpubk.c
135
sshpubk.c
@ -1433,6 +1433,141 @@ int ssh2_save_userkey(const Filename *filename, struct ssh2_userkey *key,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------
|
||||||
|
* Output public keys.
|
||||||
|
*/
|
||||||
|
char *ssh1_pubkey_str(struct RSAKey *key)
|
||||||
|
{
|
||||||
|
char *buffer;
|
||||||
|
char *dec1, *dec2;
|
||||||
|
|
||||||
|
dec1 = bignum_decimal(key->exponent);
|
||||||
|
dec2 = bignum_decimal(key->modulus);
|
||||||
|
buffer = dupprintf("%d %s %s%s%s", bignum_bitcount(key->modulus),
|
||||||
|
dec1, dec2,
|
||||||
|
key->comment ? " " : "",
|
||||||
|
key->comment ? key->comment : "");
|
||||||
|
sfree(dec1);
|
||||||
|
sfree(dec2);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ssh1_write_pubkey(FILE *fp, struct RSAKey *key)
|
||||||
|
{
|
||||||
|
char *buffer = ssh1_pubkey_str(key);
|
||||||
|
fprintf(fp, "%s\n", buffer);
|
||||||
|
sfree(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ssh2_pubkey_openssh_str_internal(const char *comment,
|
||||||
|
const void *v_pub_blob,
|
||||||
|
int pub_len)
|
||||||
|
{
|
||||||
|
const unsigned char *ssh2blob = (const unsigned char *)v_pub_blob;
|
||||||
|
const char *alg;
|
||||||
|
int alglen;
|
||||||
|
char *buffer, *p;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (pub_len < 4) {
|
||||||
|
alg = NULL;
|
||||||
|
} else {
|
||||||
|
alglen = GET_32BIT(ssh2blob);
|
||||||
|
if (alglen > 0 && alglen < pub_len - 4) {
|
||||||
|
alg = (const char *)ssh2blob + 4;
|
||||||
|
} else {
|
||||||
|
alg = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!alg) {
|
||||||
|
alg = "INVALID-ALGORITHM";
|
||||||
|
alglen = strlen(alg);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = snewn(alglen +
|
||||||
|
4 * ((pub_len+2) / 3) +
|
||||||
|
(comment ? strlen(comment) : 0) + 3, char);
|
||||||
|
p = buffer + sprintf(buffer, "%.*s ", alglen, alg);
|
||||||
|
i = 0;
|
||||||
|
while (i < pub_len) {
|
||||||
|
int n = (pub_len - i < 3 ? pub_len - i : 3);
|
||||||
|
base64_encode_atom(ssh2blob + i, n, p);
|
||||||
|
i += n;
|
||||||
|
p += 4;
|
||||||
|
}
|
||||||
|
if (*comment) {
|
||||||
|
*p++ = ' ';
|
||||||
|
strcpy(p, comment);
|
||||||
|
} else
|
||||||
|
*p++ = '\0';
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ssh2_pubkey_openssh_str(struct ssh2_userkey *key)
|
||||||
|
{
|
||||||
|
int bloblen;
|
||||||
|
unsigned char *blob;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
blob = key->alg->public_blob(key->data, &bloblen);
|
||||||
|
ret = ssh2_pubkey_openssh_str_internal(key->comment, blob, bloblen);
|
||||||
|
sfree(blob);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ssh2_write_pubkey(FILE *fp, const char *comment,
|
||||||
|
const void *v_pub_blob, int pub_len,
|
||||||
|
int keytype)
|
||||||
|
{
|
||||||
|
unsigned char *pub_blob = (unsigned char *)v_pub_blob;
|
||||||
|
|
||||||
|
if (keytype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716) {
|
||||||
|
const char *p;
|
||||||
|
int i, column;
|
||||||
|
|
||||||
|
fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
|
||||||
|
|
||||||
|
if (comment) {
|
||||||
|
fprintf(fp, "Comment: \"");
|
||||||
|
for (p = comment; *p; p++) {
|
||||||
|
if (*p == '\\' || *p == '\"')
|
||||||
|
fputc('\\', fp);
|
||||||
|
fputc(*p, fp);
|
||||||
|
}
|
||||||
|
fprintf(fp, "\"\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
column = 0;
|
||||||
|
while (i < pub_len) {
|
||||||
|
char buf[5];
|
||||||
|
int n = (pub_len - i < 3 ? pub_len - i : 3);
|
||||||
|
base64_encode_atom(pub_blob + i, n, buf);
|
||||||
|
i += n;
|
||||||
|
buf[4] = '\0';
|
||||||
|
fputs(buf, fp);
|
||||||
|
if (++column >= 16) {
|
||||||
|
fputc('\n', fp);
|
||||||
|
column = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (column > 0)
|
||||||
|
fputc('\n', fp);
|
||||||
|
|
||||||
|
fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
|
||||||
|
} else if (keytype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH) {
|
||||||
|
char *buffer = ssh2_pubkey_openssh_str_internal(comment,
|
||||||
|
v_pub_blob, pub_len);
|
||||||
|
fprintf(fp, "%s\n", buffer);
|
||||||
|
sfree(buffer);
|
||||||
|
} else {
|
||||||
|
assert(0 && "Bad key type in ssh2_write_pubkey");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
* Determine the type of a private key file.
|
* Determine the type of a private key file.
|
||||||
*/
|
*/
|
||||||
|
@ -383,69 +383,23 @@ static void hidemany(HWND hwnd, const int *ids, int hideit)
|
|||||||
|
|
||||||
static void setupbigedit1(HWND hwnd, int id, int idstatic, struct RSAKey *key)
|
static void setupbigedit1(HWND hwnd, int id, int idstatic, struct RSAKey *key)
|
||||||
{
|
{
|
||||||
char *buffer;
|
char *buffer = ssh1_pubkey_str(key);
|
||||||
char *dec1, *dec2;
|
|
||||||
|
|
||||||
dec1 = bignum_decimal(key->exponent);
|
|
||||||
dec2 = bignum_decimal(key->modulus);
|
|
||||||
buffer = dupprintf("%d %s %s %s", bignum_bitcount(key->modulus),
|
|
||||||
dec1, dec2, key->comment);
|
|
||||||
SetDlgItemText(hwnd, id, buffer);
|
SetDlgItemText(hwnd, id, buffer);
|
||||||
SetDlgItemText(hwnd, idstatic,
|
SetDlgItemText(hwnd, idstatic,
|
||||||
"&Public key for pasting into authorized_keys file:");
|
"&Public key for pasting into authorized_keys file:");
|
||||||
sfree(dec1);
|
|
||||||
sfree(dec2);
|
|
||||||
sfree(buffer);
|
sfree(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setupbigedit2(HWND hwnd, int id, int idstatic,
|
static void setupbigedit2(HWND hwnd, int id, int idstatic,
|
||||||
struct ssh2_userkey *key)
|
struct ssh2_userkey *key)
|
||||||
{
|
{
|
||||||
unsigned char *pub_blob;
|
char *buffer = ssh2_pubkey_openssh_str(key);
|
||||||
char *buffer, *p;
|
|
||||||
int pub_len;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
pub_blob = key->alg->public_blob(key->data, &pub_len);
|
|
||||||
buffer = snewn(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
|
|
||||||
strlen(key->comment) + 3, char);
|
|
||||||
strcpy(buffer, key->alg->name);
|
|
||||||
p = buffer + strlen(buffer);
|
|
||||||
*p++ = ' ';
|
|
||||||
i = 0;
|
|
||||||
while (i < pub_len) {
|
|
||||||
int n = (pub_len - i < 3 ? pub_len - i : 3);
|
|
||||||
base64_encode_atom(pub_blob + i, n, p);
|
|
||||||
i += n;
|
|
||||||
p += 4;
|
|
||||||
}
|
|
||||||
*p++ = ' ';
|
|
||||||
strcpy(p, key->comment);
|
|
||||||
SetDlgItemText(hwnd, id, buffer);
|
SetDlgItemText(hwnd, id, buffer);
|
||||||
SetDlgItemText(hwnd, idstatic, "&Public key for pasting into "
|
SetDlgItemText(hwnd, idstatic, "&Public key for pasting into "
|
||||||
"OpenSSH authorized_keys file:");
|
"OpenSSH authorized_keys file:");
|
||||||
sfree(pub_blob);
|
|
||||||
sfree(buffer);
|
sfree(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int save_ssh1_pubkey(char *filename, struct RSAKey *key)
|
|
||||||
{
|
|
||||||
char *dec1, *dec2;
|
|
||||||
FILE *fp;
|
|
||||||
|
|
||||||
fp = fopen(filename, "wb");
|
|
||||||
if (!fp)
|
|
||||||
return 0;
|
|
||||||
dec1 = bignum_decimal(key->exponent);
|
|
||||||
dec2 = bignum_decimal(key->modulus);
|
|
||||||
fprintf(fp, "%d %s %s %s\n",
|
|
||||||
bignum_bitcount(key->modulus), dec1, dec2, key->comment);
|
|
||||||
fclose(fp);
|
|
||||||
sfree(dec1);
|
|
||||||
sfree(dec2);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Warn about the obsolescent key file format.
|
* Warn about the obsolescent key file format.
|
||||||
*/
|
*/
|
||||||
@ -1326,17 +1280,29 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
|
|||||||
if (ret != IDYES)
|
if (ret != IDYES)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (state->ssh2) {
|
fp = fopen(filename, "w");
|
||||||
ret = save_ssh2_pubkey(filename, &state->ssh2key);
|
if (!fp) {
|
||||||
|
MessageBox(hwnd, "Unable to open key file",
|
||||||
|
"PuTTYgen Error", MB_OK | MB_ICONERROR);
|
||||||
} else {
|
} else {
|
||||||
ret = save_ssh1_pubkey(filename, &state->key);
|
if (state->ssh2) {
|
||||||
|
int bloblen;
|
||||||
|
unsigned char *blob;
|
||||||
|
blob = state->ssh2key.alg->public_blob
|
||||||
|
(state->ssh2key.data, &bloblen);
|
||||||
|
ssh2_write_pubkey(fp, state->ssh2key.comment,
|
||||||
|
blob, bloblen,
|
||||||
|
SSH_KEYTYPE_SSH2_PUBLIC_RFC4716);
|
||||||
|
} else {
|
||||||
|
ssh1_write_pubkey(fp, &state->key);
|
||||||
}
|
}
|
||||||
if (ret <= 0) {
|
if (fclose(fp) < 0) {
|
||||||
MessageBox(hwnd, "Unable to save key file",
|
MessageBox(hwnd, "Unable to save key file",
|
||||||
"PuTTYgen Error", MB_OK | MB_ICONERROR);
|
"PuTTYgen Error", MB_OK | MB_ICONERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case IDC_LOAD:
|
case IDC_LOAD:
|
||||||
case IDC_IMPORT:
|
case IDC_IMPORT:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user