Skip to content

Commit

Permalink
Turn 'Filename' into a dynamically allocated type with no arbitrary
Browse files Browse the repository at this point in the history
length limit, just as I did to FontSpec yesterday.

[originally from svn r9316]
  • Loading branch information
sgtatham committed Oct 2, 2011
1 parent 342690f commit 62cbc7d
Show file tree
Hide file tree
Showing 29 changed files with 286 additions and 231 deletions.
27 changes: 13 additions & 14 deletions cmdgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,9 @@ static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
int main(int argc, char **argv)
{
char *infile = NULL;
Filename infilename;
Filename *infilename, *outfilename;
enum { NOKEYGEN, RSA1, RSA2, DSA } keytype = NOKEYGEN;
char *outfile = NULL, *outfiletmp = NULL;
Filename outfilename;
enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH, SSHCOM } outtype = PRIVATE;
int bits = 1024;
char *comment = NULL, *origcomment = NULL;
Expand Down Expand Up @@ -536,7 +535,7 @@ int main(int argc, char **argv)
if (infile) {
infilename = filename_from_str(infile);

intype = key_type(&infilename);
intype = key_type(infilename);

switch (intype) {
/*
Expand Down Expand Up @@ -707,11 +706,11 @@ int main(int argc, char **argv)
* Find out whether the input key is encrypted.
*/
if (intype == SSH_KEYTYPE_SSH1)
encrypted = rsakey_encrypted(&infilename, &origcomment);
encrypted = rsakey_encrypted(infilename, &origcomment);
else if (intype == SSH_KEYTYPE_SSH2)
encrypted = ssh2_userkey_encrypted(&infilename, &origcomment);
encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
else
encrypted = import_encrypted(&infilename, intype, &origcomment);
encrypted = import_encrypted(infilename, intype, &origcomment);

/*
* If so, ask for a passphrase.
Expand Down Expand Up @@ -746,7 +745,7 @@ int main(int argc, char **argv)
unsigned char *blob;
int n, l, bloblen;

ret = rsakey_pubblob(&infilename, &vblob, &bloblen,
ret = rsakey_pubblob(infilename, &vblob, &bloblen,
&origcomment, &error);
blob = (unsigned char *)vblob;

Expand All @@ -768,7 +767,7 @@ int main(int argc, char **argv)
ssh1key->comment = dupstr(origcomment);
ssh1key->private_exponent = NULL;
} else {
ret = loadrsakey(&infilename, ssh1key, passphrase, &error);
ret = loadrsakey(infilename, ssh1key, passphrase, &error);
}
if (ret > 0)
error = NULL;
Expand All @@ -778,15 +777,15 @@ int main(int argc, char **argv)

case SSH_KEYTYPE_SSH2:
if (!load_encrypted) {
ssh2blob = ssh2_userkey_loadpub(&infilename, &ssh2alg,
ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
&ssh2bloblen, NULL, &error);
ssh2algf = find_pubkey_alg(ssh2alg);
if (ssh2algf)
bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
else
bits = -1;
} else {
ssh2key = ssh2_load_userkey(&infilename, passphrase, &error);
ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
}
if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
error = NULL;
Expand All @@ -800,7 +799,7 @@ int main(int argc, char **argv)

case SSH_KEYTYPE_OPENSSH:
case SSH_KEYTYPE_SSHCOM:
ssh2key = import_ssh2(&infilename, intype, passphrase, &error);
ssh2key = import_ssh2(infilename, intype, passphrase, &error);
if (ssh2key) {
if (ssh2key != SSH2_WRONG_PASSPHRASE)
error = NULL;
Expand Down Expand Up @@ -892,14 +891,14 @@ int main(int argc, char **argv)
case PRIVATE:
if (sshver == 1) {
assert(ssh1key);
ret = saversakey(&outfilename, ssh1key, passphrase);
ret = saversakey(outfilename, ssh1key, passphrase);
if (!ret) {
fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
return 1;
}
} else {
assert(ssh2key);
ret = ssh2_save_userkey(&outfilename, ssh2key, passphrase);
ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
if (!ret) {
fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
return 1;
Expand Down Expand Up @@ -1023,7 +1022,7 @@ int main(int argc, char **argv)
case SSHCOM:
assert(sshver == 2);
assert(ssh2key);
ret = export_ssh2(&outfilename, outtype, ssh2key, passphrase);
ret = export_ssh2(outfilename, outtype, ssh2key, passphrase);
if (!ret) {
fprintf(stderr, "puttygen: unable to export key\n");
return 1;
Expand Down
5 changes: 3 additions & 2 deletions cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,13 @@ int cmdline_process_param(char *p, char *value, int need_save, Conf *conf)
}

if (!strcmp(p, "-i")) {
Filename fn;
Filename *fn;
RETURN(2);
UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
SAVEABLE(0);
fn = filename_from_str(value);
conf_set_filename(conf, CONF_keyfile, &fn);
conf_set_filename(conf, CONF_keyfile, fn);
filename_free(fn);
}

if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
Expand Down
26 changes: 13 additions & 13 deletions conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct value {
union {
int intval;
char *stringval;
Filename fileval;
Filename *fileval;
FontSpec *fontval;
} u;
};
Expand Down Expand Up @@ -125,6 +125,8 @@ static void free_value(struct value *val, int type)
{
if (type == TYPE_STR)
sfree(val->u.stringval);
else if (type == TYPE_FILENAME)
filename_free(val->u.fileval);
else if (type == TYPE_FONT)
fontspec_free(val->u.fontval);
}
Expand All @@ -143,7 +145,7 @@ static void copy_value(struct value *to, struct value *from, int type)
to->u.stringval = dupstr(from->u.stringval);
break;
case TYPE_FILENAME:
to->u.fileval = from->u.fileval;
to->u.fileval = filename_copy(from->u.fileval);
break;
case TYPE_FONT:
to->u.fontval = fontspec_copy(from->u.fontval);
Expand Down Expand Up @@ -332,7 +334,7 @@ Filename *conf_get_filename(Conf *conf, int primary)
key.primary = primary;
entry = find234(conf->tree, &key, NULL);
assert(entry);
return &entry->value.u.fileval;
return entry->value.u.fileval;
}

FontSpec *conf_get_fontspec(Conf *conf, int primary)
Expand Down Expand Up @@ -418,7 +420,7 @@ void conf_set_filename(Conf *conf, int primary, const Filename *value)
assert(subkeytypes[primary] == TYPE_NONE);
assert(valuetypes[primary] == TYPE_FILENAME);
entry->key.primary = primary;
entry->value.u.fileval = *value; /* structure copy */
entry->value.u.fileval = filename_copy(value);
conf_insert(conf, entry);
}

Expand Down Expand Up @@ -457,7 +459,7 @@ int conf_serialised_size(Conf *conf)
size += 1 + strlen(entry->value.u.stringval);
break;
case TYPE_FILENAME:
size += sizeof(entry->value.u.fileval);
size += filename_serialise(entry->value.u.fileval, NULL);
break;
case TYPE_FONT:
size += fontspec_serialise(entry->value.u.fontval, NULL);
Expand Down Expand Up @@ -504,9 +506,7 @@ void conf_serialise(Conf *conf, void *vdata)
*data++ = 0;
break;
case TYPE_FILENAME:
memcpy(data, &entry->value.u.fileval,
sizeof(entry->value.u.fileval));
data += sizeof(entry->value.u.fileval);
data += filename_serialise(entry->value.u.fileval, data);
break;
case TYPE_FONT:
data += fontspec_serialise(entry->value.u.fontval, data);
Expand Down Expand Up @@ -580,16 +580,16 @@ int conf_deserialise(Conf *conf, void *vdata, int maxsize)
data = zero + 1;
break;
case TYPE_FILENAME:
if (maxsize < sizeof(entry->value.u.fileval)) {
entry->value.u.fileval =
filename_deserialise(data, maxsize, &used);
if (!entry->value.u.fileval) {
if (subkeytypes[entry->key.primary] == TYPE_STR)
sfree(entry->key.secondary.s);
sfree(entry);
goto done;
}
memcpy(&entry->value.u.fileval, data,
sizeof(entry->value.u.fileval));
data += sizeof(entry->value.u.fileval);
maxsize -= sizeof(entry->value.u.fileval);
data += used;
maxsize -= used;
break;
case TYPE_FONT:
entry->value.u.fontval =
Expand Down
9 changes: 4 additions & 5 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,11 @@ void conf_filesel_handler(union control *ctrl, void *dlg,
Conf *conf = (Conf *)data;

if (event == EVENT_REFRESH) {
dlg_filesel_set(ctrl, dlg, *conf_get_filename(conf, key));
dlg_filesel_set(ctrl, dlg, conf_get_filename(conf, key));
} else if (event == EVENT_VALCHANGE) {
Filename filename;
dlg_filesel_get(ctrl, dlg, &filename);
conf_set_filename(conf, key, &filename);
/* If Filenames ever become dynamic, free this one. */
Filename *filename = dlg_filesel_get(ctrl, dlg);
conf_set_filename(conf, key, filename);
filename_free(filename);
}
}

Expand Down
19 changes: 2 additions & 17 deletions dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,21 +521,6 @@ union control *ctrl_checkbox(struct controlset *, char *label, char shortcut,
handler_fn handler, intorptr context);
union control *ctrl_tabdelay(struct controlset *, union control *);

/*
* The standard file-selector handler expects the main `context'
* field to contain the `offsetof' a Filename field in the
* structure pointed to by `data'.
*/
void dlg_stdfilesel_handler(union control *ctrl, void *dlg,
void *data, int event);
/*
* The standard font-selector handler expects the main `context'
* field to contain the `offsetof' a Font field in the structure
* pointed to by `data'.
*/
void dlg_stdfontsel_handler(union control *ctrl, void *dlg,
void *data, int event);

/*
* Routines the platform-independent dialog code can call to read
* and write the values of controls.
Expand Down Expand Up @@ -565,8 +550,8 @@ int dlg_listbox_index(union control *ctrl, void *dlg);
int dlg_listbox_issel(union control *ctrl, void *dlg, int index);
void dlg_listbox_select(union control *ctrl, void *dlg, int index);
void dlg_text_set(union control *ctrl, void *dlg, char const *text);
void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn);
void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn);
void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn);
Filename *dlg_filesel_get(union control *ctrl, void *dlg);
void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fn);
FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg);
/*
Expand Down
8 changes: 4 additions & 4 deletions import.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ static struct openssh_key *load_openssh_key(const Filename *filename,
ret->encrypted = 0;
memset(ret->iv, 0, sizeof(ret->iv));

fp = f_open(*filename, "r", FALSE);
fp = f_open(filename, "r", FALSE);
if (!fp) {
errmsg = "unable to open key file";
goto error;
Expand Down Expand Up @@ -919,7 +919,7 @@ int openssh_write(const Filename *filename, struct ssh2_userkey *key,
* And save it. We'll use Unix line endings just in case it's
* subsequently transferred in binary mode.
*/
fp = f_open(*filename, "wb", TRUE); /* ensure Unix line endings */
fp = f_open(filename, "wb", TRUE); /* ensure Unix line endings */
if (!fp)
goto error;
fputs(header, fp);
Expand Down Expand Up @@ -1053,7 +1053,7 @@ static struct sshcom_key *load_sshcom_key(const Filename *filename,
ret->keyblob = NULL;
ret->keyblob_len = ret->keyblob_size = 0;

fp = f_open(*filename, "r", FALSE);
fp = f_open(filename, "r", FALSE);
if (!fp) {
errmsg = "unable to open key file";
goto error;
Expand Down Expand Up @@ -1672,7 +1672,7 @@ int sshcom_write(const Filename *filename, struct ssh2_userkey *key,
* And save it. We'll use Unix line endings just in case it's
* subsequently transferred in binary mode.
*/
fp = f_open(*filename, "wb", TRUE); /* ensure Unix line endings */
fp = f_open(filename, "wb", TRUE); /* ensure Unix line endings */
if (!fp)
goto error;
fputs("---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----\n", fp);
Expand Down
Loading

0 comments on commit 62cbc7d

Please sign in to comment.