t-getmechanisminfo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* t-getmechanismlist.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_MECHANISM_TYPE_PTR mechanisms;
  53. CK_ULONG mechanisms_count;
  54. int j;
  55. printf ("%2i. Slot ID %lu\n", i, slots[i]);
  56. err = C_GetMechanismList (slots[i], NULL, &mechanisms_count);
  57. fail_if_err (err);
  58. printf (" Mechanisms: %lu\n", mechanisms_count);
  59. mechanisms = malloc (sizeof (CK_MECHANISM_TYPE) * mechanisms_count);
  60. if (!mechanisms)
  61. fail_if_err (CKR_HOST_MEMORY);
  62. err = C_GetMechanismList (slots[i], mechanisms, &mechanisms_count);
  63. fail_if_err (err);
  64. for (j = 0; j < mechanisms_count; j++)
  65. {
  66. CK_MECHANISM_INFO info;
  67. printf (" %2i. %s\n", j, mechanism_type_str (mechanisms[j]));
  68. err = C_GetMechanismInfo (slots[i], mechanisms[j], &info);
  69. fail_if_err (err);
  70. printf (" Minimum key size: %lu\n", info.ulMinKeySize);
  71. printf (" Maximum key size: %lu\n", info.ulMaxKeySize);
  72. printf (" Flags: %#lx", info.flags);
  73. if (info.flags)
  74. {
  75. bool any = false;
  76. CK_FLAGS xflags = 0;
  77. printf (" == ");
  78. #define DO_FLAG(sym) \
  79. if (info.flags & sym) \
  80. { \
  81. printf ("%s" #sym, any ? " | " : ""); \
  82. any = true; \
  83. xflags |= sym; \
  84. }
  85. DO_FLAG (CKF_HW);
  86. DO_FLAG (CKF_ENCRYPT);
  87. DO_FLAG (CKF_DECRYPT);
  88. DO_FLAG (CKF_DIGEST);
  89. DO_FLAG (CKF_SIGN);
  90. DO_FLAG (CKF_SIGN_RECOVER);
  91. DO_FLAG (CKF_VERIFY);
  92. DO_FLAG (CKF_VERIFY_RECOVER);
  93. DO_FLAG (CKF_GENERATE);
  94. DO_FLAG (CKF_GENERATE_KEY_PAIR);
  95. DO_FLAG (CKF_WRAP);
  96. DO_FLAG (CKF_UNWRAP);
  97. DO_FLAG (CKF_DERIVE);
  98. DO_FLAG (CKF_EXTENSION);
  99. xflags = info.flags & ~xflags;
  100. if (xflags)
  101. printf ("%s%#lx", any ? " | " : "", xflags);
  102. }
  103. printf ("\n");
  104. }
  105. free (mechanisms);
  106. }
  107. return 0;
  108. }