diff --git a/src/p11-getslotlist.c b/src/p11-getslotlist.c index 79bab9e..d494ed9 100644 --- a/src/p11-getslotlist.c +++ b/src/p11-getslotlist.c @@ -1,96 +1,106 @@ /* p11-getslotlist.c - Cryptoki implementation. Copyright (C) 2006 g10 Code GmbH This file is part of Scute. Scute is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Scute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scute; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, g10 Code GmbH gives permission to link this library: with the Mozilla Foundation's code for Mozilla (or with modified versions of it that use the same license as the "Mozilla" code), and distribute the linked executables. You must obey the GNU General Public License in all respects for all of the code used other than "Mozilla". If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #if HAVE_CONFIG_H #include #endif #include "cryptoki.h" #include "locking.h" #include "slots.h" - + +/* Return the list of available slots. With TOKENPRESENT set only + * slots with a present tokens are returned. If PSLOTLIST is NULL the + * function only counts the number of slots and stores that number at + * PULCOUNT. Further this also updates the inetrnal state and thus + * this needs to be called to check for new devices. If PSLOTLIST is + * not NULL it must point to an array which receives the slot + * information. PULCOUNT must point to a variable which initially + * holds the number of allocated slot items and will be updated on + * return to the stored number of slot items. + */ CK_RV CK_SPEC C_GetSlotList (CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) { CK_RV err = CKR_OK; CK_ULONG left; slot_iterator_t slot; if (pulCount == NULL_PTR) return CKR_ARGUMENTS_BAD; err = scute_global_lock (); if (err) return err; if (pSlotList == NULL_PTR) { err = slots_update (); if (err) goto out; } /* Leave LEFT positive for the whole search when only counting. */ left = pSlotList ? *pulCount : 1; *pulCount = 0; err = slots_iterate_first (&slot); if (err) goto out; while (!slots_iterate_last (&slot) && left && !err) { if (!tokenPresent || slot_token_present (slot)) { (*pulCount)++; if (pSlotList) { *(pSlotList++) = slot_get_id (slot); left--; } } err = slots_iterate_next (&slot); } if (err) goto out; if (!slots_iterate_last (&slot) && !left) { err = CKR_BUFFER_TOO_SMALL; goto out; } out: scute_global_unlock (); return err; } diff --git a/tests/t-getslotinfo.c b/tests/t-getslotinfo.c index 576d313..f63ad9a 100644 --- a/tests/t-getslotinfo.c +++ b/tests/t-getslotinfo.c @@ -1,118 +1,166 @@ /* t-getslotinfo.c - Regression test. Copyright (C) 2006, 2008 g10 Code GmbH This file is part of Scute. - + Scute is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Scute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scute; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, g10 Code GmbH gives permission to link this library: with the Mozilla Foundation's code for Mozilla (or with modified versions of it that use the same license as the "Mozilla" code), and distribute the linked executables. You must obey the GNU General Public License in all respects for all of the code used other than "Mozilla". If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include -#include +#include +#define PGM "t-getslotinfo" #include "t-support.h" + + int main (int argc, char *argv[]) { + int last_argc = -1; CK_RV err; - bool token = false; + int loop = 0; + int token = 0; CK_SLOT_ID_PTR slots; CK_ULONG slots_count; unsigned int i; - (void) argv; - - if (argc > 1) - token = true; + if (argc) + { argc--; argv++; } + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--")) + { + argc--; argv++; + break; + } + else if (!strcmp (*argv, "--help")) + { + fputs ("usage: " PGM " [options]\n" + "Options:\n" + " --loop N Run N times with a 2 second delay.\n" + " --token Only present tokens\n", + stdout); + exit (0); + } + else if (!strcmp (*argv, "--loop")) + { + argc--; argv++; + if (argc) + { + loop = atoi (*argv); + argc--; argv++; + } + } + else if (!strcmp (*argv, "--token")) + { + argc--; argv++; + token = 1; + } + else if (!strncmp (*argv, "--", 2)) + { + fprintf (stderr, "unknown option '%s'\n", *argv); + exit (1); + } + } init_cryptoki (); err = C_GetSlotList (token, NULL, &slots_count); fail_if_err (err); printf ("Number of slots%s: %lu\n", token ? " (with tokens)" : "", slots_count); + if (!slots_count) + return 0; /* Nothing to do. */ + slots = malloc (sizeof (CK_SLOT_ID) * slots_count); if (!slots) fail_if_err (CKR_HOST_MEMORY); err = C_GetSlotList (token, slots, &slots_count); fail_if_err (err); - // while (1) - { + again: for (i = 0; i < slots_count; i++) { CK_SLOT_INFO info; err = C_GetSlotInfo (slots[i], &info); fail_if_err (err); printf ("%2i. Slot ID %lu\n", i, slots[i]); printf (" %.64s\n", info.slotDescription); printf (" Manufacturer ID: %.32s\n", info.manufacturerID); printf (" Flags: %#lx", info.flags); if (info.flags) { - bool any = false; + int any = 0; CK_FLAGS xflags; xflags = info.flags & ~(CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE | CKF_HW_SLOT); printf (" == "); if (info.flags & CKF_TOKEN_PRESENT) { printf ("TOKEN_PRESENT"); - any = true; + any = 1; } if (info.flags & CKF_REMOVABLE_DEVICE) { printf ("%sREMOVABLE_DEVICE", any ? " | " : ""); - any = true; + any = 1; } if (info.flags & CKF_HW_SLOT) { printf ("%sHW_SLOT", any ? " | " : ""); - any = true; + any = 1; } if (xflags) printf ("%s%#lx", any ? " | " : "", xflags); } printf ("\n"); printf (" Hardware version: %i.%i\n", info.hardwareVersion.major, info.hardwareVersion.minor); printf (" Firmware version: %i.%i\n", info.firmwareVersion.major, info.firmwareVersion.minor); } + + if (loop > 0) + loop--; + if (loop) + { #ifdef WIN32 - _sleep (2); + _sleep (2); #else - sleep (2); + sleep (2); /* Why? */ #endif + goto again; } return 0; } diff --git a/tests/t-getslotlist.c b/tests/t-getslotlist.c index 17ad3e3..1256b4c 100644 --- a/tests/t-getslotlist.c +++ b/tests/t-getslotlist.c @@ -1,68 +1,98 @@ /* t-getslotlist.c - Regression test. Copyright (C) 2006 g10 Code GmbH This file is part of Scute. - + Scute is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Scute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scute; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, g10 Code GmbH gives permission to link this library: with the Mozilla Foundation's code for Mozilla (or with modified versions of it that use the same license as the "Mozilla" code), and distribute the linked executables. You must obey the GNU General Public License in all respects for all of the code used other than "Mozilla". If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include -#include +#include +#define PGM "t-getslotlist" #include "t-support.h" int main (int argc, char *argv[]) { + int last_argc = -1; CK_RV err; - bool token = false; + int token = 0; CK_SLOT_ID_PTR slots; CK_ULONG slots_count; unsigned int i; - (void) argc; - (void) argv; + if (argc) + { argc--; argv++; } + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--")) + { + argc--; argv++; + break; + } + else if (!strcmp (*argv, "--help")) + { + fputs ("usage: " PGM " [options]\n" + "Options:\n" + " --token Only present tokens\n", + stdout); + exit (0); + } + else if (!strcmp (*argv, "--token")) + { + argc--; argv++; + token = 1; + } + else if (!strncmp (*argv, "--", 2)) + { + fprintf (stderr, "unknown option '%s'\n", *argv); + exit (1); + } + } - if (argc > 1) - token = true; init_cryptoki (); err = C_GetSlotList (token, NULL, &slots_count); fail_if_err (err); printf ("Number of slots%s: %lu\n", token ? " (with tokens)" : "", slots_count); + if (!slots_count) + return 0; + slots = malloc (sizeof (CK_SLOT_ID) * slots_count); if (!slots) fail_if_err (CKR_HOST_MEMORY); err = C_GetSlotList (token, slots, &slots_count); fail_if_err (err); for (i = 0; i < slots_count; i++) printf ("%2i. Slot ID %lu\n", i, slots[i]); return 0; } diff --git a/tests/t-opensession.c b/tests/t-opensession.c index 77d86fe..9258c8b 100644 --- a/tests/t-opensession.c +++ b/tests/t-opensession.c @@ -1,88 +1,112 @@ /* t-opensession.c - Regression test. Copyright (C) 2006 g10 Code GmbH This file is part of Scute. - + Scute is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Scute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scute; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, g10 Code GmbH gives permission to link this library: with the Mozilla Foundation's code for Mozilla (or with modified versions of it that use the same license as the "Mozilla" code), and distribute the linked executables. You must obey the GNU General Public License in all respects for all of the code used other than "Mozilla". If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include -#include +#include +#define PGM "t-opensession" #include "t-support.h" int main (int argc, char *argv[]) { + int last_argc = -1; CK_RV err; CK_SLOT_ID_PTR slots; CK_SESSION_HANDLE_PTR sessions; CK_ULONG slots_count; unsigned int i; - (void) argc; - (void) argv; + if (argc) + { argc--; argv++; } + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--")) + { + argc--; argv++; + break; + } + else if (!strcmp (*argv, "--help")) + { + fputs ("usage: " PGM " [options]\n" + "No Options\n", + stdout); + exit (0); + } + else if (!strncmp (*argv, "--", 2)) + { + fprintf (stderr, "unknown option '%s'\n", *argv); + exit (1); + } + } init_cryptoki (); - err = C_GetSlotList (true, NULL, &slots_count); + err = C_GetSlotList (1, NULL, &slots_count); fail_if_err (err); if (slots_count == 0) { printf ("Skipping test because no token is present.\n"); return 77; } printf ("Number of slots with tokens: %lu\n", slots_count); + slots = malloc (sizeof (CK_SLOT_ID) * slots_count); if (!slots) fail_if_err (CKR_HOST_MEMORY); sessions = malloc (sizeof (CK_SESSION_HANDLE) * slots_count); if (!sessions) fail_if_err (CKR_HOST_MEMORY); - err = C_GetSlotList (true, slots, &slots_count); + err = C_GetSlotList (1, slots, &slots_count); fail_if_err (err); for (i = 0; i < slots_count; i++) { printf ("%2i. Slot ID %lu\n", i, slots[i]); err = C_OpenSession (slots[i], CKF_SERIAL_SESSION, NULL, NULL, &sessions[i]); fail_if_err (err); printf (" Session ID: %lu\n", sessions[i]); } for (i = 0; i < slots_count; i++) { err = C_CloseSession (sessions[i]); fail_if_err (err); } return 0; }