[PATCH] handle record-based files correctly when doing file caching

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] handle record-based files correctly when doing file caching

Andreas Kemnade
---
 src/tools/pkcs15-tool.c |   46 +++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 5 deletions(-)

diff --git a/src/tools/pkcs15-tool.c b/src/tools/pkcs15-tool.c
index 603a753..42d7d4c 100644
--- a/src/tools/pkcs15-tool.c
+++ b/src/tools/pkcs15-tool.c
@@ -1515,11 +1515,47 @@ static int read_and_cache_file(const sc_path_t *path)
  printf("out of memory!");
  return -1;
  }
- r = sc_read_binary(card, 0, buf, size, 0);
- if (r < 0) {
- fprintf(stderr, "sc_read_binary() failed: %s\n", sc_strerror(r));
- free(buf);
- return -1;
+ if (tfile->ef_structure == SC_FILE_EF_LINEAR_VARIABLE_TLV) {
+ int i;
+ size_t l, record_len;
+ unsigned char *head = buf;
+
+ for (i=1;  ; i++) {
+ l = size - (head - buf);
+ if (l > 256) { l = 256; }
+ r = sc_read_record(p15card->card, i, head, l, SC_RECORD_BY_REC_NR);
+ if (r == SC_ERROR_RECORD_NOT_FOUND) {
+ r = 0;
+ break;
+ }
+ if (r < 0) {
+ free(buf);
+ return -1;
+ }
+ if (r < 2)
+ break;
+ record_len = head[1];
+ if (record_len != 0xff) {
+ memmove(head,head+2,r-2);
+ head += (r-2);
+ }
+ else {
+ if (r < 4)
+ break;
+ memmove(head,head+4,r-4);
+ head += (r-4);
+ }
+ }
+ r = head - buf;
+
+ } else {
+
+ r = sc_read_binary(card, 0, buf, size, 0);
+ if (r < 0) {
+ fprintf(stderr, "sc_read_binary() failed: %s\n", sc_strerror(r));
+ free(buf);
+ return -1;
+ }
  }
  r = sc_pkcs15_cache_file(p15card, path, buf, r);
  if (r) {
--
1.7.10.4


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Opensc-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] handle record-based files correctly when doing file caching

Douglas E Engert
Is this a general problem for this type of file?
If so why is the change only in pkcs15-tool, that is just a utility?

Should pkcs15-tool be calling sc_pkcs15_read_file which does handle SC_FILE_EF_LINEAR_VARIABLE_TLV?

On 1/27/2015 2:08 PM, Andreas Kemnade wrote:
> ---
>   src/tools/pkcs15-tool.c |   46 +++++++++++++++++++++++++++++++++++++++++-----
>   1 file changed, 41 insertions(+), 5 deletions(-)
>
> diff --git a/src/tools/pkcs15-tool.c b/src/tools/pkcs15-tool.c
> index 603a753..42d7d4c 100644
> --- a/src/tools/pkcs15-tool.c
> +++ b/src/tools/pkcs15-tool.c/

> @@ -1515,11 +1515,47 @@ static int read_and_cache_file(const sc_path_t *path)
>   printf("out of memory!");
>   return -1;
>   }
> - r = sc_read_binary(card, 0, buf, size, 0);
> - if (r < 0) {
> - fprintf(stderr, "sc_read_binary() failed: %s\n", sc_strerror(r));
> - free(buf);
> - return -1;
> + if (tfile->ef_structure == SC_FILE_EF_LINEAR_VARIABLE_TLV) {
> + int i;
> + size_t l, record_len;
> + unsigned char *head = buf;
> +
> + for (i=1;  ; i++) {
> + l = size - (head - buf);
> + if (l > 256) { l = 256; }
> + r = sc_read_record(p15card->card, i, head, l, SC_RECORD_BY_REC_NR);
> + if (r == SC_ERROR_RECORD_NOT_FOUND) {
> + r = 0;
> + break;
> + }
> + if (r < 0) {
> + free(buf);
> + return -1;
> + }
> + if (r < 2)
> + break;
> + record_len = head[1];
> + if (record_len != 0xff) {
> + memmove(head,head+2,r-2);
> + head += (r-2);
> + }
> + else {
> + if (r < 4)
> + break;
> + memmove(head,head+4,r-4);
> + head += (r-4);
> + }
> + }
> + r = head - buf;
> +
> + } else {
> +
> + r = sc_read_binary(card, 0, buf, size, 0);
> + if (r < 0) {
> + fprintf(stderr, "sc_read_binary() failed: %s\n", sc_strerror(r));
> + free(buf);
> + return -1;
> + }
>   }
>   r = sc_pkcs15_cache_file(p15card, path, buf, r);
>   if (r) {
>

--

  Douglas E. Engert  <[hidden email]>


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Opensc-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/opensc-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] handle record-based files correctly when doing file caching

Andreas Kemnade
Hi,

On Tue, 27 Jan 2015 16:47:51 -0600
Douglas E Engert <[hidden email]> wrote:

> Is this a general problem for this type of file?

The only occurance for that problem I know is
pkcs15-tool -L

> If so why is the change only in pkcs15-tool, that is just a utility?
>
> Should pkcs15-tool be calling sc_pkcs15_read_file which does handle SC_FILE_EF_LINEAR_VARIABLE_TLV?
>
Just calling sc_pkcs15_read_file would be wrong.
There are two differences:
1. in pkcs15-tool -L you want to access only public files (nothing protected by a pin)
2. in pkcs15-tool -L you do probably not want to read from the cache, since it is for filling the cache from the card.

Regards,
Andreas Kemnade

> On 1/27/2015 2:08 PM, Andreas Kemnade wrote:
> > ---
> >   src/tools/pkcs15-tool.c |   46 +++++++++++++++++++++++++++++++++++++++++-----
> >   1 file changed, 41 insertions(+), 5 deletions(-)
> >
> > diff --git a/src/tools/pkcs15-tool.c b/src/tools/pkcs15-tool.c
> > index 603a753..42d7d4c 100644
> > --- a/src/tools/pkcs15-tool.c
> > +++ b/src/tools/pkcs15-tool.c/
>
> > @@ -1515,11 +1515,47 @@ static int read_and_cache_file(const sc_path_t *path)
> >   printf("out of memory!");
> >   return -1;
> >   }
> > - r = sc_read_binary(card, 0, buf, size, 0);
> > - if (r < 0) {
> > - fprintf(stderr, "sc_read_binary() failed: %s\n", sc_strerror(r));
> > - free(buf);
> > - return -1;
> > + if (tfile->ef_structure == SC_FILE_EF_LINEAR_VARIABLE_TLV) {
> > + int i;
> > + size_t l, record_len;
> > + unsigned char *head = buf;
> > +
> > + for (i=1;  ; i++) {
> > + l = size - (head - buf);
> > + if (l > 256) { l = 256; }
> > + r = sc_read_record(p15card->card, i, head, l, SC_RECORD_BY_REC_NR);
> > + if (r == SC_ERROR_RECORD_NOT_FOUND) {
> > + r = 0;
> > + break;
> > + }
> > + if (r < 0) {
> > + free(buf);
> > + return -1;
> > + }
> > + if (r < 2)
> > + break;
> > + record_len = head[1];
> > + if (record_len != 0xff) {
> > + memmove(head,head+2,r-2);
> > + head += (r-2);
> > + }
> > + else {
> > + if (r < 4)
> > + break;
> > + memmove(head,head+4,r-4);
> > + head += (r-4);
> > + }
> > + }
> > + r = head - buf;
> > +
> > + } else {
> > +
> > + r = sc_read_binary(card, 0, buf, size, 0);
> > + if (r < 0) {
> > + fprintf(stderr, "sc_read_binary() failed: %s\n", sc_strerror(r));
> > + free(buf);
> > + return -1;
> > + }
> >   }
> >   r = sc_pkcs15_cache_file(p15card, path, buf, r);
> >   if (r) {
> >
>
> --
>
>   Douglas E. Engert  <[hidden email]>
>
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming. The Go Parallel Website,
> sponsored by Intel and developed in partnership with Slashdot Media, is your
> hub for all things parallel software development, from weekly thought
> leadership blogs to news, videos, case studies, tutorials and more. Take a
> look and join the conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> Opensc-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/opensc-devel
>
>

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Opensc-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/opensc-devel

signature.asc (205 bytes) Download Attachment