t-gettokeninfo.c 4.9 KB

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