File: t-getslotinfo.c

package info (click to toggle)
scute 1%3A1.7.0%2Bgit20240826%2B8331f37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,468 kB
  • sloc: sh: 12,247; ansic: 8,701; makefile: 228; sed: 37
file content (157 lines) | stat: -rw-r--r-- 3,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* 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 Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <https://gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <stdio.h>
#include <string.h>

#define PGM "t-getslotinfo"
#include "t-support.h"



int
main (int argc, char *argv[])
{
  int last_argc = -1;
  CK_RV err;
  int loop = 0;
  int token = 0;
  CK_SLOT_ID_PTR slots;
  CK_ULONG slots_count;
  unsigned int i;

  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);

 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)
	{
	  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 = 1;
	    }
	  if (info.flags & CKF_REMOVABLE_DEVICE)
	    {
	      printf ("%sREMOVABLE_DEVICE", any ? " | " : "");
	      any = 1;
	    }
	  if (info.flags & CKF_HW_SLOT)
	    {
	      printf ("%sHW_SLOT", any ? " | " : "");
              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*1000);
#else
      sleep (2);  /* Why? */
#endif
      goto again;
    }

  return 0;
}