properly handle variable-size RSA keys in key_io.c

This commit is contained in:
Christian Grothoff 2015-03-15 18:37:08 +01:00
parent 4d98a1200a
commit 464077c547

View File

@ -20,9 +20,6 @@
* @author Benedikt Mueller * @author Benedikt Mueller
* @author Sree Harsha Totakura * @author Sree Harsha Totakura
* @author Christian Grothoff * @author Christian Grothoff
*
* TODO:
* - revisit IO with respect to variable-size RSA keys!
*/ */
#include "platform.h" #include "platform.h"
#include "key_io.h" #include "key_io.h"
@ -120,7 +117,8 @@ TALER_MINT_signkeys_iterate (const char *mint_base_dir,
* *
* @param filename the file to import the key from * @param filename the file to import the key from
* @param[OUT] dki set to the imported denomination key * @param[OUT] dki set to the imported denomination key
* @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure * @return #GNUNET_OK upon success;
* #GNUNET_SYSERR upon failure
*/ */
int int
TALER_MINT_read_denom_key (const char *filename, TALER_MINT_read_denom_key (const char *filename,
@ -130,45 +128,54 @@ TALER_MINT_read_denom_key (const char *filename,
size_t offset; size_t offset;
void *data; void *data;
struct GNUNET_CRYPTO_rsa_PrivateKey *priv; struct GNUNET_CRYPTO_rsa_PrivateKey *priv;
int ret;
ret = GNUNET_SYSERR;
data = NULL;
offset = sizeof (struct TALER_MINT_DenomKeyIssuePriv)
- offsetof (struct TALER_MINT_DenomKeyIssuePriv,
issue.signature);
/* FIXME: this is very wrong, does not support variable-size
encoding of RSA keys (private or public!) */
if (GNUNET_OK != GNUNET_DISK_file_size (filename, if (GNUNET_OK != GNUNET_DISK_file_size (filename,
&size, &size,
GNUNET_YES, GNUNET_YES,
GNUNET_YES)) GNUNET_YES))
goto cleanup; {
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Skipping inaccessable denomination key file `%s'\n",
filename);
return GNUNET_SYSERR;
}
offset = sizeof (struct TALER_MINT_DenomKeyIssue);
if (size <= offset) if (size <= offset)
{ {
GNUNET_break (0); GNUNET_break (0);
goto cleanup; return GNUNET_SYSERR;
} }
data = GNUNET_malloc (size); data = GNUNET_malloc (size);
if (size != GNUNET_DISK_fn_read (filename, if (size !=
data, GNUNET_DISK_fn_read (filename,
size)) data,
goto cleanup; size))
if (NULL == (priv = GNUNET_CRYPTO_rsa_private_key_decode (data + offset, {
size - offset))) GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
goto cleanup; "read",
filename);
GNUNET_free (data);
return GNUNET_SYSERR;
}
if (NULL ==
(priv = GNUNET_CRYPTO_rsa_private_key_decode (data + offset,
size - offset)))
{
GNUNET_free (data);
return GNUNET_SYSERR;
}
dki->denom_priv = priv; dki->denom_priv = priv;
memcpy (&dki->issue.signature, data, offset); dki->denom_pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
ret = GNUNET_OK; memcpy (&dki->issue,
data,
cleanup: offset);
GNUNET_free_non_null (data); GNUNET_free (data);
return ret; return GNUNET_OK;
} }
/** /**
* Exports a denomination key to the given file * Exports a denomination key to the given file.
* *
* @param filename the file where to write the denomination key * @param filename the file where to write the denomination key
* @param dki the denomination key * @param dki the denomination key
@ -194,9 +201,7 @@ TALER_MINT_write_denom_key (const char *filename,
GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))) GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE)))
goto cleanup; goto cleanup;
wsize = sizeof (struct TALER_MINT_DenomKeyIssuePriv) wsize = sizeof (struct TALER_MINT_DenomKeyIssue);
- offsetof (struct TALER_MINT_DenomKeyIssuePriv,
issue.signature);
if (GNUNET_SYSERR == (wrote = GNUNET_DISK_file_write (fh, if (GNUNET_SYSERR == (wrote = GNUNET_DISK_file_write (fh,
&dki->issue.signature, &dki->issue.signature,
wsize))) wsize)))