mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-06-30 19:12:48 -05:00
Reorganise ssh_keyalg and use it as a vtable.
After Pavel Kryukov pointed out that I have to put _something_ in the 'ssh_key' structure, I thought of an actually useful thing to put there: why not make it store a pointer to the ssh_keyalg structure? Then ssh_key becomes a classoid - or perhaps 'traitoid' is a closer analogy - in the same style as Socket and Plug. And just like Socket and Plug, I've also arranged a system of wrapper macros that avoid the need to mention the 'object' whose method you're invoking twice at each call site. The new vtable pointer directly replaces an existing field of struct ec_key (which was usable by several different ssh_keyalgs, so it already had to store a pointer to the currently active one), and also replaces the 'alg' field of the ssh2_userkey structure that wraps up a cryptographic key with its comment field. I've also taken the opportunity to clean things up a bit in general: most of the methods now have new and clearer names (e.g. you'd never know that 'newkey' made a public-only key while 'createkey' made a public+private key pair unless you went and looked it up, but now they're called 'new_pub' and 'new_priv' you might be in with a chance), and I've completely removed the openssh_private_npieces field after realising that it was duplicating information that is actually _more_ conveniently obtained by calling the new_priv_openssh method (formerly openssh_createkey) and throwing away the result.
This commit is contained in:
28
cmdgen.c
28
cmdgen.c
@ -248,7 +248,6 @@ int main(int argc, char **argv)
|
||||
struct RSAKey *ssh1key = NULL;
|
||||
strbuf *ssh2blob = NULL;
|
||||
char *ssh2alg = NULL;
|
||||
const ssh_keyalg *ssh2algf = NULL;
|
||||
char *old_passphrase = NULL, *new_passphrase = NULL;
|
||||
int load_encrypted;
|
||||
progfn_t progressfn = is_interactive() ? progress_update : no_progress;
|
||||
@ -722,22 +721,19 @@ int main(int argc, char **argv)
|
||||
struct dss_key *dsskey = snew(struct dss_key);
|
||||
dsa_generate(dsskey, bits, progressfn, &prog);
|
||||
ssh2key = snew(struct ssh2_userkey);
|
||||
ssh2key->data = &dsskey->sshk;
|
||||
ssh2key->alg = &ssh_dss;
|
||||
ssh2key->key = &dsskey->sshk;
|
||||
ssh1key = NULL;
|
||||
} else if (keytype == ECDSA) {
|
||||
struct ec_key *ec = snew(struct ec_key);
|
||||
ec_generate(ec, bits, progressfn, &prog);
|
||||
ssh2key = snew(struct ssh2_userkey);
|
||||
ssh2key->data = &ec->sshk;
|
||||
ssh2key->alg = ec->signalg;
|
||||
ssh2key->key = &ec->sshk;
|
||||
ssh1key = NULL;
|
||||
} else if (keytype == ED25519) {
|
||||
struct ec_key *ec = snew(struct ec_key);
|
||||
ec_edgenerate(ec, bits, progressfn, &prog);
|
||||
ssh2key = snew(struct ssh2_userkey);
|
||||
ssh2key->data = &ec->sshk;
|
||||
ssh2key->alg = &ssh_ecdsa_ed25519;
|
||||
ssh2key->key = &ec->sshk;
|
||||
ssh1key = NULL;
|
||||
} else {
|
||||
struct RSAKey *rsakey = snew(struct RSAKey);
|
||||
@ -747,8 +743,7 @@ int main(int argc, char **argv)
|
||||
ssh1key = rsakey;
|
||||
} else {
|
||||
ssh2key = snew(struct ssh2_userkey);
|
||||
ssh2key->data = &rsakey->sshk;
|
||||
ssh2key->alg = &ssh_rsa;
|
||||
ssh2key->key = &rsakey->sshk;
|
||||
}
|
||||
}
|
||||
progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
|
||||
@ -838,10 +833,10 @@ int main(int argc, char **argv)
|
||||
ssh2blob = strbuf_new();
|
||||
if (ssh2_userkey_loadpub(infilename, &ssh2alg, BinarySink_UPCAST(ssh2blob),
|
||||
&origcomment, &error)) {
|
||||
ssh2algf = find_pubkey_alg(ssh2alg);
|
||||
if (ssh2algf)
|
||||
bits = ssh2algf->pubkey_bits(
|
||||
ssh2algf, make_ptrlen(ssh2blob->s, ssh2blob->len));
|
||||
const ssh_keyalg *alg = find_pubkey_alg(ssh2alg);
|
||||
if (alg)
|
||||
bits = ssh_key_public_bits(
|
||||
alg, make_ptrlen(ssh2blob->s, ssh2blob->len));
|
||||
else
|
||||
bits = -1;
|
||||
} else {
|
||||
@ -995,7 +990,7 @@ int main(int argc, char **argv)
|
||||
if (!ssh2blob) {
|
||||
assert(ssh2key);
|
||||
ssh2blob = strbuf_new();
|
||||
ssh2key->alg->public_blob(ssh2key->data, BinarySink_UPCAST(ssh2blob));
|
||||
ssh_key_public_blob(ssh2key->key, BinarySink_UPCAST(ssh2blob));
|
||||
}
|
||||
|
||||
ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
|
||||
@ -1020,8 +1015,7 @@ int main(int argc, char **argv)
|
||||
fingerprint = rsa_ssh1_fingerprint(ssh1key);
|
||||
} else {
|
||||
if (ssh2key) {
|
||||
fingerprint = ssh2_fingerprint(ssh2key->alg,
|
||||
ssh2key->data);
|
||||
fingerprint = ssh2_fingerprint(ssh2key->key);
|
||||
} else {
|
||||
assert(ssh2blob);
|
||||
fingerprint = ssh2_fingerprint_blob(
|
||||
@ -1085,7 +1079,7 @@ int main(int argc, char **argv)
|
||||
if (ssh1key)
|
||||
freersakey(ssh1key);
|
||||
if (ssh2key) {
|
||||
ssh2key->alg->freekey(ssh2key->data);
|
||||
ssh_key_free(ssh2key->key);
|
||||
sfree(ssh2key);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user