mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
Refactor the key-components mechanism a bit.
Having recently pulled it out into its own file, I think it could also do with a bit of tidying. In this rework: - the substructure for a single component now has a globally visible struct tag, so you can make a variable pointing at it, saving verbiage in every piece of code looping over a key_components - the 'is_mp_int' flag has been replaced with a type enum, so that more types can be added without further upheaval - the printing loop in cmdgen.c for puttygen --dump has factored out the initial 'name=' prefix on each line so that it isn't repeated per component type - the storage format for text components is now a strbuf rather than a plain char *, which I think is generally more useful.
This commit is contained in:
parent
cf36b9215f
commit
68514ac8a1
21
cmdgen.c
21
cmdgen.c
@ -1312,16 +1312,23 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < kc->ncomponents; i++) {
|
||||
if (kc->components[i].is_mp_int) {
|
||||
char *hex = mp_get_hex(kc->components[i].mp);
|
||||
fprintf(fp, "%s=0x%s\n", kc->components[i].name, hex);
|
||||
key_component *comp = &kc->components[i];
|
||||
fprintf(fp, "%s=", comp->name);
|
||||
switch (comp->type) {
|
||||
case KCT_MPINT: {
|
||||
char *hex = mp_get_hex(comp->mp);
|
||||
fprintf(fp, "0x%s\n", hex);
|
||||
smemclr(hex, strlen(hex));
|
||||
sfree(hex);
|
||||
} else {
|
||||
fprintf(fp, "%s=\"", kc->components[i].name);
|
||||
write_c_string_literal(fp, ptrlen_from_asciz(
|
||||
kc->components[i].text));
|
||||
break;
|
||||
}
|
||||
case KCT_TEXT:
|
||||
fputs("\"", fp);
|
||||
write_c_string_literal(fp, ptrlen_from_strbuf(comp->str));
|
||||
fputs("\"\n", fp);
|
||||
break;
|
||||
default:
|
||||
unreachable("bad key component type");
|
||||
}
|
||||
}
|
||||
|
||||
|
20
ssh.h
20
ssh.h
@ -542,16 +542,20 @@ struct eddsa_key {
|
||||
WeierstrassPoint *ecdsa_public(mp_int *private_key, const ssh_keyalg *alg);
|
||||
EdwardsPoint *eddsa_public(mp_int *private_key, const ssh_keyalg *alg);
|
||||
|
||||
typedef enum KeyComponentType {
|
||||
KCT_TEXT, KCT_MPINT
|
||||
} KeyComponentType;
|
||||
typedef struct key_component {
|
||||
char *name;
|
||||
KeyComponentType type;
|
||||
union {
|
||||
strbuf *str; /* used for KCT_TEXT */
|
||||
mp_int *mp; /* used for KCT_MPINT */
|
||||
};
|
||||
} key_component;
|
||||
typedef struct key_components {
|
||||
size_t ncomponents, componentsize;
|
||||
struct {
|
||||
char *name;
|
||||
bool is_mp_int;
|
||||
union {
|
||||
char *text;
|
||||
mp_int *mp;
|
||||
};
|
||||
} *components;
|
||||
key_component *components;
|
||||
} key_components;
|
||||
key_components *key_components_new(void);
|
||||
void key_components_add_text(key_components *kc,
|
||||
|
@ -309,7 +309,7 @@ FUNC(uint, ssh_key_public_bits, ARG(keyalg, self), ARG(val_string_ptrlen, blob))
|
||||
FUNC(uint, key_components_count, ARG(val_keycomponents, kc))
|
||||
FUNC(opt_val_string_asciz_const, key_components_nth_name,
|
||||
ARG(val_keycomponents, kc), ARG(uint, n))
|
||||
FUNC(opt_val_string_asciz_const, key_components_nth_str,
|
||||
FUNC(opt_val_string, key_components_nth_str,
|
||||
ARG(val_keycomponents, kc), ARG(uint, n))
|
||||
FUNC(opt_val_mpint, key_components_nth_mp, ARG(val_keycomponents, kc),
|
||||
ARG(uint, n))
|
||||
|
@ -1227,16 +1227,18 @@ const char *key_components_nth_name(key_components *kc, size_t n)
|
||||
return (n >= kc->ncomponents ? NULL :
|
||||
kc->components[n].name);
|
||||
}
|
||||
const char *key_components_nth_str(key_components *kc, size_t n)
|
||||
strbuf *key_components_nth_str(key_components *kc, size_t n)
|
||||
{
|
||||
return (n >= kc->ncomponents ? NULL :
|
||||
kc->components[n].is_mp_int ? NULL :
|
||||
kc->components[n].text);
|
||||
if (n >= kc->ncomponents)
|
||||
return NULL;
|
||||
if (kc->components[n].type != KCT_TEXT)
|
||||
return NULL;
|
||||
return strbuf_dup(ptrlen_from_strbuf(kc->components[n].str));
|
||||
}
|
||||
mp_int *key_components_nth_mp(key_components *kc, size_t n)
|
||||
{
|
||||
return (n >= kc->ncomponents ? NULL :
|
||||
!kc->components[n].is_mp_int ? NULL :
|
||||
kc->components[n].type != KCT_MPINT ? NULL :
|
||||
mp_copy(kc->components[n].mp));
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,8 @@ void key_components_add_text(key_components *kc,
|
||||
sgrowarray(kc->components, kc->componentsize, kc->ncomponents);
|
||||
size_t n = kc->ncomponents++;
|
||||
kc->components[n].name = dupstr(name);
|
||||
kc->components[n].is_mp_int = false;
|
||||
kc->components[n].text = dupstr(value);
|
||||
kc->components[n].type = KCT_TEXT;
|
||||
kc->components[n].str = strbuf_dup_nm(ptrlen_from_asciz(value));
|
||||
}
|
||||
|
||||
void key_components_add_mp(key_components *kc,
|
||||
@ -26,19 +26,24 @@ void key_components_add_mp(key_components *kc,
|
||||
sgrowarray(kc->components, kc->componentsize, kc->ncomponents);
|
||||
size_t n = kc->ncomponents++;
|
||||
kc->components[n].name = dupstr(name);
|
||||
kc->components[n].is_mp_int = true;
|
||||
kc->components[n].type = KCT_MPINT;
|
||||
kc->components[n].mp = mp_copy(value);
|
||||
}
|
||||
|
||||
void key_components_free(key_components *kc)
|
||||
{
|
||||
for (size_t i = 0; i < kc->ncomponents; i++) {
|
||||
sfree(kc->components[i].name);
|
||||
if (kc->components[i].is_mp_int) {
|
||||
mp_free(kc->components[i].mp);
|
||||
} else {
|
||||
smemclr(kc->components[i].text, strlen(kc->components[i].text));
|
||||
sfree(kc->components[i].text);
|
||||
key_component *comp = &kc->components[i];
|
||||
sfree(comp->name);
|
||||
switch (comp->type) {
|
||||
case KCT_MPINT:
|
||||
mp_free(comp->mp);
|
||||
break;
|
||||
case KCT_TEXT:
|
||||
strbuf_free(comp->str);
|
||||
break;
|
||||
default:
|
||||
unreachable("bad key component type");
|
||||
}
|
||||
}
|
||||
sfree(kc->components);
|
||||
|
Loading…
Reference in New Issue
Block a user