diff options
author | Marcus Brinkmann <mb@g10code.com> | 2006-07-29 18:40:54 +0200 |
---|---|---|
committer | Marcus Brinkmann <mb@g10code.com> | 2006-07-29 18:40:54 +0200 |
commit | 6d77c76ef2de1b944141f9b5fbcd9392aaf1a4e7 (patch) | |
tree | 49d596d702cfec2b8cc42ccaf8c90c82d5200ac5 /agent/preset-passphrase.c | |
parent | 2006-07-29 Marcus Brinkmann <marcus@g10code.de> (diff) | |
download | gnupg2-6d77c76ef2de1b944141f9b5fbcd9392aaf1a4e7.tar.xz gnupg2-6d77c76ef2de1b944141f9b5fbcd9392aaf1a4e7.zip |
2006-07-29 Marcus Brinkmann <marcus@g10code.de>
* preset-passphrase.c (preset_passphrase): Do not strip off last
character of passphrase.
(make_hexstring): New function.
* command.c (cmd_preset_passphrase): Use parse_hexstring to syntax
check passphrase argument. Truncate passphrase at delimiter.
Diffstat (limited to '')
-rw-r--r-- | agent/preset-passphrase.c | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/agent/preset-passphrase.c b/agent/preset-passphrase.c index 013c9411d..f876b0647 100644 --- a/agent/preset-passphrase.c +++ b/agent/preset-passphrase.c @@ -152,6 +152,38 @@ map_spwq_error (int err) } +/* Percent-Escape special characters. The string is valid until the + next invocation of the function. */ +static char * +make_hexstring (const char *src) +{ + int len = 2 * strlen (src) + 1; + char *dst; + char *res; + + res = dst = malloc (len); + if (!dst) + { + log_error ("can not escape string: %s\n", + gpg_strerror (gpg_error_from_errno (errno))); + return NULL; + } + +#define _tohex(nr) ((nr) < 10 ? ((nr) + '0') : (((nr) - 10) + 'A')) +#define tohex1(p) _tohex (*((unsigned char *) p) & 15) +#define tohex2(p) _tohex ((*((unsigned char *) p) >> 4) & 15) + + while (*src) + { + *(dst++) = tohex2 (src); + *(dst++) = tohex1 (src); + src++; + } + *dst = '\0'; + return res; +} + + static void preset_passphrase (const char *keygrip) { @@ -159,6 +191,7 @@ preset_passphrase (const char *keygrip) char *line; /* FIXME: Use secure memory. */ char passphrase[500]; + char *passphrase_esc; if (!opt_passphrase) { @@ -173,7 +206,6 @@ preset_passphrase (const char *keygrip) line = strchr (passphrase, '\n'); if (line) { - line--; if (line > passphrase && line[-1] == '\r') line--; *line = '\0'; @@ -182,8 +214,19 @@ preset_passphrase (const char *keygrip) /* FIXME: How to handle empty passwords? */ } + passphrase_esc = make_hexstring (opt_passphrase + ? opt_passphrase : passphrase); + if (!passphrase_esc) + { + /* Error message printed by callee. */ + return; + } + rc = asprintf (&line, "PRESET_PASSPHRASE %s -1 %s\n", keygrip, - opt_passphrase? opt_passphrase : passphrase); + passphrase_esc); + wipememory (passphrase_esc, strlen (passphrase_esc)); + free (passphrase_esc); + if (rc < 0) { log_error ("caching passphrase failed: %s\n", |