Fix SEGV bug in "rhizome extract file" command

Add optional <key> argument, validate and parse it properly.
This commit is contained in:
Andrew Bettison 2012-06-01 18:12:59 +09:30
parent 051191ba32
commit 304db6e5f4
3 changed files with 41 additions and 18 deletions

View File

@ -1313,16 +1313,21 @@ int cli_fileid(const char *arg)
return rhizome_str_is_file_hash(arg);
}
int cli_bundlekey(const char *arg)
{
return rhizome_str_is_bundle_crypt_key(arg);
}
int app_rhizome_extract_file(int argc, const char *const *argv, struct command_line_option *o)
{
const char *fileid, *filepath;
const char *fileid, *filepath, *keyhex;
unsigned char key[RHIZOME_CRYPT_KEY_STRLEN + 1];
if (cli_arg(argc, argv, o, "fileid", &fileid, cli_fileid, NULL)
|| cli_arg(argc, argv, o, "filepath", &filepath, NULL, "") == -1)
return -1;
/* Read key if is available */
// cli_arg(argc, argv, 0, "key", &keyhex,NULL,NULL);
cli_arg(argc, argv, o, "key", &keyhex, cli_bundlekey, NULL);
if (keyhex && stowBytes(key, keyhex, RHIZOME_CRYPT_KEY_BYTES) == -1)
return -1;
/* Ensure the Rhizome database exists and is open */
if (create_serval_instance_dir() == -1)
return -1;
@ -1332,7 +1337,7 @@ int app_rhizome_extract_file(int argc, const char *const *argv, struct command_l
We don't provide a decryption key here, because we don't know it.
(We probably should allow the user to provide one).
*/
int ret = rhizome_retrieve_file(fileid, filepath,NULL);
int ret = rhizome_retrieve_file(fileid, filepath, keyhex ? key : NULL);
switch (ret) {
case 0: ret = 1; break;
case 1: ret = 0; break;
@ -1697,7 +1702,7 @@ command_line_option command_line_options[]={
"List all manifests and files in Rhizome"},
{app_rhizome_extract_manifest,{"rhizome","extract","manifest","<manifestid>","[<manifestpath>]",NULL},CLIFLAG_STANDALONE,
"Extract a manifest from Rhizome and write it to the given path"},
{app_rhizome_extract_file,{"rhizome","extract","file","<fileid>","[<filepath>]",NULL},CLIFLAG_STANDALONE,
{app_rhizome_extract_file,{"rhizome","extract","file","<fileid>","[<filepath>]","[<key>]",NULL},CLIFLAG_STANDALONE,
"Extract a file from Rhizome and write it to the given path"},
{app_keyring_create,{"keyring","create",NULL},0,
"Create a new keyring file."},

View File

@ -29,8 +29,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define RHIZOME_FILEHASH_BYTES SHA512_DIGEST_LENGTH
#define RHIZOME_FILEHASH_STRLEN (RHIZOME_FILEHASH_BYTES * 2)
#define RHIZOME_CRYPT_PAGE_SIZE 4096
#define RHIZOME_CRYPT_KEY_BYTES crypto_stream_xsalsa20_ref_KEYBYTES
#define RHIZOME_CRYPT_KEY_BYTES crypto_stream_xsalsa20_ref_KEYBYTES
#define RHIZOME_CRYPT_KEY_STRLEN (RHIZOME_CRYPT_KEY_BYTES * 2)
#define RHIZOME_CRYPT_PAGE_SIZE 4096
#define RHIZOME_HTTP_PORT 4110
@ -197,6 +198,8 @@ int rhizome_opendb();
int rhizome_manifest_createid(rhizome_manifest *m);
int rhizome_strn_is_manifest_id(const char *text);
int rhizome_str_is_manifest_id(const char *text);
int rhizome_strn_is_bundle_crypt_key(const char *text);
int rhizome_str_is_bundle_crypt_key(const char *text);
int rhizome_strn_is_file_hash(const char *text);
int rhizome_str_is_file_hash(const char *text);
int rhizome_write_manifest_file(rhizome_manifest *m, const char *filename);

View File

@ -30,25 +30,40 @@ unsigned char *rhizome_bundle_shared_secret(rhizome_manifest *m)
return NULL;
}
int rhizome_strn_is_manifest_id(const char *id)
static inline int _is_xsubstring(const char *text, int len)
{
int i;
for (i = 0; i != crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES * 2; ++i)
if (!isxdigit(id[i]))
while (len--)
if (!isxdigit(*text++))
return 0;
return 1;
}
int rhizome_str_is_bundle_crypt_key(const char *id)
static inline int _is_xstring(const char *text, int len)
{
size_t len = strlen(id);
return len == RHIZOME_CRYPT_KEY_BYTES * 2 && rhizome_strn_is_manifest_id(id);
while (len--)
if (!isxdigit(*text++))
return 0;
return *text == '\0';
}
int rhizome_strn_is_manifest_id(const char *id)
{
return _is_xsubstring(id, RHIZOME_MANIFEST_ID_STRLEN);
}
int rhizome_str_is_manifest_id(const char *id)
{
size_t len = strlen(id);
return len == crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES * 2 && rhizome_strn_is_manifest_id(id);
return _is_xstring(id, RHIZOME_MANIFEST_ID_STRLEN);
}
int rhizome_strn_is_bundle_crypt_key(const char *key)
{
return _is_xstring(key, RHIZOME_CRYPT_KEY_STRLEN);
}
int rhizome_str_is_bundle_crypt_key(const char *key)
{
return _is_xsubstring(key, RHIZOME_CRYPT_KEY_STRLEN);
}
int rhizome_manifest_createid(rhizome_manifest *m)