t-gettokeninfo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* t-gettokeninfo.c - Regression test.
  2. Copyright (C) 2006 g10 Code GmbH
  3. This file is part of Scute.
  4. Scute is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Scute is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Scute; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. In addition, as a special exception, g10 Code GmbH gives permission
  16. to link this library: with the Mozilla Foundation's code for
  17. Mozilla (or with modified versions of it that use the same license
  18. as the "Mozilla" code), and distribute the linked executables. You
  19. must obey the GNU General Public License in all respects for all of
  20. the code used other than "Mozilla". If you modify this file, you
  21. may extend this exception to your version of the file, but you are
  22. not obligated to do so. If you do not wish to do so, delete this
  23. exception statement from your version. */
  24. #include <stdio.h>
  25. #include <stdbool.h>
  26. #include "t-support.h"
  27. int
  28. main (int argc, char *argv[])
  29. {
  30. CK_RV err;
  31. CK_SLOT_ID_PTR slots;
  32. CK_ULONG slots_count;
  33. unsigned int i;
  34. (void) argc;
  35. (void) argv;
  36. init_cryptoki ();
  37. err = C_GetSlotList (true, NULL, &slots_count);
  38. fail_if_err (err);
  39. if (slots_count == 0)
  40. {
  41. printf ("Skipping test because no token is present.\n");
  42. return 0;
  43. }
  44. printf ("Number of slots with tokens: %lu\n", slots_count);
  45. slots = malloc (sizeof (CK_SLOT_ID) * slots_count);
  46. if (!slots)
  47. fail_if_err (CKR_HOST_MEMORY);
  48. err = C_GetSlotList (true, slots, &slots_count);
  49. fail_if_err (err);
  50. for (i = 0; i < slots_count; i++)
  51. {
  52. CK_TOKEN_INFO info;
  53. err = C_GetTokenInfo (slots[i], &info);
  54. fail_if_err (err);
  55. printf ("%2i. Slot ID %lu\n", i, slots[i]);
  56. printf (" Label: %.32s\n", info.label);
  57. printf (" Manufacturer ID: %.32s\n", info.manufacturerID);
  58. printf (" Model: %.16s\n", info.model);
  59. printf (" Serial number: %.16s\n", info.serialNumber);
  60. printf (" Flags: %#lx", info.flags);
  61. if (info.flags)
  62. {
  63. bool any = false;
  64. CK_FLAGS xflags;
  65. xflags = info.flags
  66. & ~(CKF_RNG | CKF_WRITE_PROTECTED | CKF_LOGIN_REQUIRED
  67. | CKF_USER_PIN_INITIALIZED | CKF_RESTORE_KEY_NOT_NEEDED
  68. | CKF_CLOCK_ON_TOKEN | CKF_PROTECTED_AUTHENTICATION_PATH
  69. | CKF_DUAL_CRYPTO_OPERATIONS | CKF_TOKEN_INITIALIZED
  70. | CKF_SECONDARY_AUTHENTICATION | CKF_USER_PIN_COUNT_LOW
  71. | CKF_USER_PIN_FINAL_TRY | CKF_USER_PIN_LOCKED
  72. | CKF_USER_PIN_TO_BE_CHANGED | CKF_SO_PIN_COUNT_LOW
  73. | CKF_SO_PIN_FINAL_TRY | CKF_SO_PIN_LOCKED
  74. | CKF_SO_PIN_TO_BE_CHANGED);
  75. printf (" == ");
  76. #define DO_FLAG(sym) \
  77. if (info.flags & sym) \
  78. { \
  79. printf ("%s" #sym, any ? " | " : ""); \
  80. any = true; \
  81. }
  82. DO_FLAG (CKF_RNG);
  83. DO_FLAG (CKF_WRITE_PROTECTED);
  84. DO_FLAG (CKF_LOGIN_REQUIRED);
  85. DO_FLAG (CKF_USER_PIN_INITIALIZED);
  86. DO_FLAG (CKF_RESTORE_KEY_NOT_NEEDED);
  87. DO_FLAG (CKF_CLOCK_ON_TOKEN);
  88. DO_FLAG (CKF_PROTECTED_AUTHENTICATION_PATH);
  89. DO_FLAG (CKF_DUAL_CRYPTO_OPERATIONS);
  90. DO_FLAG (CKF_TOKEN_INITIALIZED);
  91. DO_FLAG (CKF_SECONDARY_AUTHENTICATION);
  92. DO_FLAG (CKF_USER_PIN_COUNT_LOW);
  93. DO_FLAG (CKF_USER_PIN_FINAL_TRY);
  94. DO_FLAG (CKF_USER_PIN_LOCKED);
  95. DO_FLAG (CKF_USER_PIN_TO_BE_CHANGED);
  96. DO_FLAG (CKF_SO_PIN_COUNT_LOW);
  97. DO_FLAG (CKF_SO_PIN_FINAL_TRY);
  98. DO_FLAG (CKF_SO_PIN_LOCKED);
  99. DO_FLAG (CKF_SO_PIN_TO_BE_CHANGED);
  100. if (xflags)
  101. printf ("%s%#lx", any ? " | " : "", xflags);
  102. }
  103. printf ("\n");
  104. printf (" Max session count: %li\n", info.ulMaxSessionCount);
  105. printf (" Session count: %li\n", info.ulSessionCount);
  106. printf (" Max rw session count: %li\n", info.ulMaxRwSessionCount);
  107. printf (" Rw session count: %li\n", info.ulRwSessionCount);
  108. printf (" Max PIN length: %li\n", info.ulMaxPinLen);
  109. printf (" Min PIN length: %li\n", info.ulMinPinLen);
  110. printf (" Total public memory: %li\n", info.ulTotalPublicMemory);
  111. printf (" Free public memory: %li\n", info.ulFreePublicMemory);
  112. printf (" Total private memory: %li\n", info.ulTotalPrivateMemory);
  113. printf (" Free private memory: %li\n", info.ulFreePrivateMemory);
  114. printf (" Hardware version: %i.%i\n", info.hardwareVersion.major,
  115. info.hardwareVersion.minor);
  116. printf (" Firmware version: %i.%i\n", info.firmwareVersion.major,
  117. info.firmwareVersion.minor);
  118. printf (" UTC time: %.16s\n", info.utcTime);
  119. }
  120. return 0;
  121. }