1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

Reworked the estream memory buffer allocation.

Committed already posted patches for the v2 card.
This commit is contained in:
Werner Koch 2009-06-29 10:43:57 +00:00
parent 19cb96e5d3
commit d8d1ca6151
8 changed files with 111 additions and 71 deletions

View File

@ -1,3 +1,12 @@
2009-06-29 Werner Koch <wk@g10code.com>
* configure.ac: Take care of --without-adns. Suggested by
Arfrever Frehtes Taifersar Arahesis.
2009-06-17 Werner Koch <wk@g10code.com>
Released 2.0.12.
2009-06-05 David Shaw <dshaw@jabberwocky.com> 2009-06-05 David Shaw <dshaw@jabberwocky.com>
* configure.ac: Remove Camellia restriction. * configure.ac: Remove Camellia restriction.

View File

@ -25,6 +25,7 @@ Release process:
* Run "make -C po update-po". * Run "make -C po update-po".
* Write NEWS entries and set the release date in NEWS. * Write NEWS entries and set the release date in NEWS.
* In configure.ac set "my_issvn" to "no". * In configure.ac set "my_issvn" to "no".
* Put a "Released <version>" line into the top level ChangeLog.
* Commit all changes to the SVN. * Commit all changes to the SVN.
* Update the SVN then (to sync the release number of all files). * Update the SVN then (to sync the release number of all files).
* Run "./autogen.sh --force" * Run "./autogen.sh --force"

View File

@ -1,5 +1,8 @@
2009-06-29 Werner Koch <wk@g10code.com> 2009-06-29 Werner Koch <wk@g10code.com>
* estream.c (BUFFER_ROUND_TO_BLOCK): Remove unused macro.
(es_func_mem_write): Rewrite reallocation part.
* estream.c (es_write_sanitized_utf8_buffer): Typo typo fix. * estream.c (es_write_sanitized_utf8_buffer): Typo typo fix.
2009-06-25 Werner Koch <wk@g10code.com> 2009-06-25 Werner Koch <wk@g10code.com>

View File

@ -1,5 +1,5 @@
/* estream.c - Extended Stream I/O Library /* estream.c - Extended Stream I/O Library
* Copyright (C) 2004, 2005, 2006, 2007 g10 Code GmbH * Copyright (C) 2004, 2005, 2006, 2007, 2009 g10 Code GmbH
* *
* This file is part of Libestream. * This file is part of Libestream.
* *
@ -91,12 +91,6 @@ typedef void (*func_free_t) (void *mem);
/* Macros. */
#define BUFFER_ROUND_TO_BLOCK(size, block_size) \
/* Locking. */ /* Locking. */
#ifdef HAVE_PTH #ifdef HAVE_PTH
@ -400,11 +394,11 @@ typedef struct estream_cookie_mem
{ {
unsigned int modeflags; /* Open flags. */ unsigned int modeflags; /* Open flags. */
unsigned char *memory; /* Allocated data buffer. */ unsigned char *memory; /* Allocated data buffer. */
size_t memory_size; /* Allocated size of memory. */ size_t memory_size; /* Allocated size of MEMORY. */
size_t memory_limit; /* Maximum allowed allocation size or size_t memory_limit; /* Caller supplied maximum allowed
0 for no limit. */ allocation size or 0 for no limit. */
size_t offset; /* Current offset in MEMORY. */ size_t offset; /* Current offset in MEMORY. */
size_t data_len; /* Length of data in MEMORY. */ size_t data_len; /* Used length of data in MEMORY. */
size_t block_size; /* Block size. */ size_t block_size; /* Block size. */
struct { struct {
unsigned int grow: 1; /* MEMORY is allowed to grow. */ unsigned int grow: 1; /* MEMORY is allowed to grow. */
@ -414,7 +408,11 @@ typedef struct estream_cookie_mem
} *estream_cookie_mem_t; } *estream_cookie_mem_t;
/* Create function for memory objects. */ /* Create function for memory objects. DATA is either NULL or a user
supplied buffer with the initial conetnt of the memory buffer. If
DATA is NULL, DATA_N and DATA_LEN need to be 0 as well. If DATA is
not NULL, DATA_N gives the allocated size of DATA and DATA_LEN the
used length in DATA. */
static int static int
es_func_mem_create (void *ES__RESTRICT *ES__RESTRICT cookie, es_func_mem_create (void *ES__RESTRICT *ES__RESTRICT cookie,
unsigned char *ES__RESTRICT data, size_t data_n, unsigned char *ES__RESTRICT data, size_t data_n,
@ -427,6 +425,12 @@ es_func_mem_create (void *ES__RESTRICT *ES__RESTRICT cookie,
estream_cookie_mem_t mem_cookie; estream_cookie_mem_t mem_cookie;
int err; int err;
if (!data && (data_n || data_len))
{
errno = EINVAL;
return -1;
}
mem_cookie = mem_alloc (sizeof (*mem_cookie)); mem_cookie = mem_alloc (sizeof (*mem_cookie));
if (!mem_cookie) if (!mem_cookie)
err = -1; err = -1;
@ -477,6 +481,7 @@ es_func_mem_write (void *cookie, const void *buffer, size_t size)
{ {
estream_cookie_mem_t mem_cookie = cookie; estream_cookie_mem_t mem_cookie = cookie;
ssize_t ret; ssize_t ret;
size_t nleft;
if (!size) if (!size)
return 0; /* A flush is a NOP for memory objects. */ return 0; /* A flush is a NOP for memory objects. */
@ -487,28 +492,33 @@ es_func_mem_write (void *cookie, const void *buffer, size_t size)
mem_cookie->offset = mem_cookie->data_len; mem_cookie->offset = mem_cookie->data_len;
} }
if (!mem_cookie->flags.grow) assert (mem_cookie->memory_size >= mem_cookie->offset);
{ nleft = mem_cookie->memory_size - mem_cookie->offset;
/* We are not allowed to grow, thus limit the size to the left
space. FIXME: Does the grow flag and its sematics make sense
at all? */
if (size > mem_cookie->memory_size - mem_cookie->offset)
size = mem_cookie->memory_size - mem_cookie->offset;
}
if (size > (mem_cookie->memory_size - mem_cookie->offset)) /* If we are not allowed to grow limit the size to the left space. */
if (!mem_cookie->flags.grow && size > nleft)
size = nleft;
/* Enlarge the memory buffer if needed. */
if (size > nleft)
{ {
unsigned char *newbuf; unsigned char *newbuf;
size_t newsize; size_t newsize;
newsize = mem_cookie->memory_size + mem_cookie->block_size; if (!mem_cookie->memory_size)
#warning READ the code and see how it should work newsize = size; /* Not yet allocated. */
newsize = mem_cookie->offset + size; else
newsize = mem_cookie->memory_size + (nleft - size);
if (newsize < mem_cookie->offset) if (newsize < mem_cookie->offset)
{ {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
/* Round up to the next block length. BLOCK_SIZE should always
be set; we check anyway. */
if (mem_cookie->block_size)
{
newsize += mem_cookie->block_size - 1; newsize += mem_cookie->block_size - 1;
if (newsize < mem_cookie->offset) if (newsize < mem_cookie->offset)
{ {
@ -517,7 +527,9 @@ es_func_mem_write (void *cookie, const void *buffer, size_t size)
} }
newsize /= mem_cookie->block_size; newsize /= mem_cookie->block_size;
newsize *= mem_cookie->block_size; newsize *= mem_cookie->block_size;
}
/* Check for a total limit. */
if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit) if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit)
{ {
errno = ENOSPC; errno = ENOSPC;
@ -531,7 +543,10 @@ es_func_mem_write (void *cookie, const void *buffer, size_t size)
mem_cookie->memory = newbuf; mem_cookie->memory = newbuf;
mem_cookie->memory_size = newsize; mem_cookie->memory_size = newsize;
assert (!(size > (mem_cookie->memory_size - mem_cookie->offset))); assert (mem_cookie->memory_size >= mem_cookie->offset);
nleft = mem_cookie->memory_size - mem_cookie->offset;
assert (size <= nleft);
} }
memcpy (mem_cookie->memory + mem_cookie->offset, buffer, size); memcpy (mem_cookie->memory + mem_cookie->offset, buffer, size);
@ -579,7 +594,6 @@ es_func_mem_seek (void *cookie, off_t *offset, int whence)
{ {
errno = ENOSPC; errno = ENOSPC;
return -1; return -1;
} }
newsize = pos_new + mem_cookie->block_size - 1; newsize = pos_new + mem_cookie->block_size - 1;
@ -590,6 +604,7 @@ es_func_mem_seek (void *cookie, off_t *offset, int whence)
} }
newsize /= mem_cookie->block_size; newsize /= mem_cookie->block_size;
newsize *= mem_cookie->block_size; newsize *= mem_cookie->block_size;
if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit) if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit)
{ {
errno = ENOSPC; errno = ENOSPC;

View File

@ -820,11 +820,13 @@ AC_ARG_WITH(adns,
CPPFLAGS="${CPPFLAGS} -I$withval/include" CPPFLAGS="${CPPFLAGS} -I$withval/include"
LDFLAGS="${LDFLAGS} -L$withval/lib" LDFLAGS="${LDFLAGS} -L$withval/lib"
fi]) fi])
AC_CHECK_HEADERS(adns.h, if test "$with_adns" != "no"; then
AC_CHECK_HEADERS(adns.h,
AC_CHECK_LIB(adns, adns_init, AC_CHECK_LIB(adns, adns_init,
[have_adns=yes], [have_adns=yes],
[CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags}]), [CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags}]),
[CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags}]) [CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags}])
fi
if test "$have_adns" = "yes"; then if test "$have_adns" = "yes"; then
ADNSLIBS="-ladns" ADNSLIBS="-ladns"
fi fi

View File

@ -1,3 +1,8 @@
2009-06-18 Werner Koch <wk@g10code.com>
* app-openpgp.c (verify_chv2): Remove special case for v2 cards.
(get_public_key): Use extended mode.
2009-06-17 Werner Koch <wk@g10code.com> 2009-06-17 Werner Koch <wk@g10code.com>
* iso7816.c (iso7816_get_data): Add arg EXTENDED_MODE. Change all * iso7816.c (iso7816_get_data): Add arg EXTENDED_MODE. Change all

View File

@ -671,6 +671,9 @@ readn (int fd, void *buf, size_t buflen, size_t *nread)
while (nleft > 0) while (nleft > 0)
{ {
#ifdef USE_GNU_PTH #ifdef USE_GNU_PTH
# ifdef HAVE_W32_SYSTEM
# error Cannot use pth_read here because it expects a system HANDLE.
# endif
n = pth_read (fd, buf, nleft); n = pth_read (fd, buf, nleft);
#else #else
n = read (fd, buf, nleft); n = read (fd, buf, nleft);

View File

@ -1118,11 +1118,25 @@ get_public_key (app_t app, int keyno)
if (app->card_version > 0x0100) if (app->card_version > 0x0100)
{ {
int exmode, le_value;
/* We may simply read the public key out of these cards. */ /* We may simply read the public key out of these cards. */
if (app->app_local->cardcap.ext_lc_le)
{
exmode = 1; /* Use extended length. */
le_value = app->app_local->extcap.max_rsp_data;
}
else
{
exmode = 0;
le_value = 256; /* Use legacy value. */
}
err = iso7816_read_public_key err = iso7816_read_public_key
(app->slot, 0, (const unsigned char*)(keyno == 0? "\xB6" : (app->slot, exmode,
(const unsigned char*)(keyno == 0? "\xB6" :
keyno == 1? "\xB8" : "\xA4"), 2, keyno == 1? "\xB8" : "\xA4"), 2,
0, le_value,
&buffer, &buflen); &buffer, &buflen);
if (err) if (err)
{ {
@ -1579,20 +1593,8 @@ verify_chv2 (app_t app,
char *pinvalue; char *pinvalue;
if (app->did_chv2) if (app->did_chv2)
return 0; /* We already verified CHV2 (PW1 for v2 cards). */ return 0; /* We already verified CHV2. */
if (app->app_local->extcap.is_v2)
{
/* Version two cards don't have a CHV2 anymore. We need to
verify CHV1 (now called PW1) instead. */
rc = verify_a_chv (app, pincb, pincb_arg, 1, 0, &pinvalue);
if (rc)
return rc;
app->did_chv2 = 1;
}
else
{
/* Version 1 cards only. */
rc = verify_a_chv (app, pincb, pincb_arg, 2, 0, &pinvalue); rc = verify_a_chv (app, pincb, pincb_arg, 2, 0, &pinvalue);
if (rc) if (rc)
return rc; return rc;
@ -1600,10 +1602,10 @@ verify_chv2 (app_t app,
if (!app->did_chv1 && !app->force_chv1 && pinvalue) if (!app->did_chv1 && !app->force_chv1 && pinvalue)
{ {
/* For convenience we verify CHV1 here too. We do this only /* For convenience we verify CHV1 here too. We do this only if
if the card is not configured to require a verification the card is not configured to require a verification before
before each CHV1 controlled operation (force_chv1) and if each CHV1 controlled operation (force_chv1) and if we are not
we are not using the keypad (PINVALUE == NULL). */ using the keypad (PINVALUE == NULL). */
rc = iso7816_verify (app->slot, 0x81, pinvalue, strlen (pinvalue)); rc = iso7816_verify (app->slot, 0x81, pinvalue, strlen (pinvalue));
if (gpg_err_code (rc) == GPG_ERR_BAD_PIN) if (gpg_err_code (rc) == GPG_ERR_BAD_PIN)
rc = gpg_error (GPG_ERR_PIN_NOT_SYNCED); rc = gpg_error (GPG_ERR_PIN_NOT_SYNCED);
@ -1615,7 +1617,7 @@ verify_chv2 (app_t app,
else else
app->did_chv1 = 1; app->did_chv1 = 1;
} }
}
xfree (pinvalue); xfree (pinvalue);
return rc; return rc;