Refactor connection to the scdaemon

Move code to open connection to the scdaemon in a separate
function.
pull/1/head
Damien Goutte-Gattat 8 years ago
parent 396d092668
commit 4a22aa58f5
  1. 59
      src/scdrand.c

@ -181,7 +181,7 @@ get_challenge_data_cb(void *arg, const void *line, size_t len)
/*
* Get random data ("challenge") from the smartcard.
*
* @param scd_socket_name The pathname of the scdaemon socket.
* @param ctx Assuan context connected to a running scdaemon.
* @param buffer The buffer to store the returned data into.
* @param len Number of bytes of random data to retrieve. The buffer
* must be big enough to store the requested amount.
@ -191,23 +191,14 @@ get_challenge_data_cb(void *arg, const void *line, size_t len)
* in the provided buffer.
*/
static int
get_challenge(const char *scd_socket_name, unsigned char *buffer, size_t len)
get_challenge(assuan_context_t ctx, unsigned char *buffer, size_t len)
{
char command[12];
assuan_context_t ctx;
gpg_error_t ge;
struct challenge c;
snprintf(command, sizeof(command), "RANDOM %d", len);
if ( (ge = assuan_new(&ctx)) )
errx(EXIT_FAILURE, "Cannot create Assuan context: %s",
gpg_strerror(ge));
if ( (ge = assuan_socket_connect(ctx, scd_socket_name,
ASSUAN_INVALID_PID, 0)) )
errx(EXIT_FAILURE, "Cannot connect to scdaemon: %s", gpg_strerror(ge));
c.len = len;
c.data = buffer;
if ( (ge = assuan_transact(ctx, command, get_challenge_data_cb, &c,
@ -215,11 +206,36 @@ get_challenge(const char *scd_socket_name, unsigned char *buffer, size_t len)
errx(EXIT_FAILURE, "Cannot get challenge from card: %s",
gpg_strerror(ge));
assuan_release(ctx);
return c.len;
}
/*
* Connect to a running scdaemon.
*
* @return
* An Assuan context connected to the scdaemon.
*/
static assuan_context_t
connect_to_scdaemon(void)
{
char *socket_name;
assuan_context_t ctx;
gpg_error_t ge;
socket_name = get_scd_socket_name();
if ( (ge = assuan_new(&ctx)) )
errx(EXIT_FAILURE, "Cannot create Assuan context: %s",
gpg_strerror(ge));
if ( (ge = assuan_socket_connect(ctx, socket_name, ASSUAN_INVALID_PID, 0)) )
errx(EXIT_FAILURE, "Cannot connect to scdaemon: %s", gpg_strerror(ge));
free(socket_name);
return ctx;
}
/*
* Fork a new process solely charged with the task of adding entropy
* to the kernel pool (the only task that requires root privileges).
@ -303,9 +319,9 @@ get_uinteger_or_die(const char *arg)
int
main(int argc, char **argv)
{
int c, fd;
int c, fd, n;
unsigned nbytes;
char *scd_socket_name;
assuan_context_t ctx;
unsigned char random_buffer[MAX_RANDOM_BYTES];
struct option options[] = {
@ -350,16 +366,11 @@ main(int argc, char **argv)
#endif
assuan_set_gpg_err_source(GPG_ERR_SOURCE_USER_1);
scd_socket_name = get_scd_socket_name();
if ( scd_socket_name ) {
int n, i;
n = get_challenge(scd_socket_name, random_buffer, nbytes);
write(fd, random_buffer, n);
free(scd_socket_name);
}
ctx = connect_to_scdaemon();
n = get_challenge(ctx, random_buffer, nbytes);
write(fd, random_buffer, n);
assuan_release(ctx);
close(fd);
return EXIT_SUCCESS;

Loading…
Cancel
Save