1 /*
2  * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
3  */
4 
5 /* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
6  *
7  * Redistribution and use in  source and binary forms, with or without
8  * modification, are permitted  provided that the following conditions are met:
9  *
10  * 1. Redistributions of  source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in  binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if any, must
18  *    include the following acknowledgment:
19  *
20  *    "This product includes software developed by IAIK of Graz University of
21  *     Technology."
22  *
23  *    Alternately, this acknowledgment may appear in the software itself, if
24  *    and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Graz University of Technology" and "IAIK of Graz University of
27  *    Technology" must not be used to endorse or promote products derived from
28  *    this software without prior written permission.
29  *
30  * 5. Products derived from this software may not be called
31  *    "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
32  *    written permission of Graz University of Technology.
33  *
34  *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36  *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37  *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
38  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41  *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  *  POSSIBILITY  OF SUCH DAMAGE.
46  */
47 
48 package sun.security.pkcs11.wrapper;
49 
50 
51 
52 /**
53  * class CK_TOKEN_INFO provides information about a token.<p>
54  * <B>PKCS#11 structure:</B>
55  * <PRE>
56  * typedef struct CK_TOKEN_INFO {&nbsp;&nbsp;
57  *   CK_UTF8CHAR label[32];&nbsp;&nbsp;
58  *   CK_UTF8CHAR manufacturerID[32];&nbsp;&nbsp;
59  *   CK_UTF8CHAR model[16];&nbsp;&nbsp;
60  *   CK_CHAR serialNumber[16];&nbsp;&nbsp;
61  *   CK_FLAGS flags;&nbsp;&nbsp;
62  *   CK_ULONG ulMaxSessionCount;&nbsp;&nbsp;
63  *   CK_ULONG ulSessionCount;&nbsp;&nbsp;
64  *   CK_ULONG ulMaxRwSessionCount;&nbsp;&nbsp;
65  *   CK_ULONG ulRwSessionCount;&nbsp;&nbsp;
66  *   CK_ULONG ulMaxPinLen;&nbsp;&nbsp;
67  *   CK_ULONG ulMinPinLen;&nbsp;&nbsp;
68  *   CK_ULONG ulTotalPublicMemory;&nbsp;&nbsp;
69  *   CK_ULONG ulFreePublicMemory;&nbsp;&nbsp;
70  *   CK_ULONG ulTotalPrivateMemory;&nbsp;&nbsp;
71  *   CK_ULONG ulFreePrivateMemory;&nbsp;&nbsp;
72  *   CK_VERSION hardwareVersion;&nbsp;&nbsp;
73  *   CK_VERSION firmwareVersion;&nbsp;&nbsp;
74  *   CK_CHAR utcTime[16];&nbsp;&nbsp;
75  * } CK_TOKEN_INFO;
76  * &nbsp;&nbsp;
77  * </PRE>
78  *
79  * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
80  * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
81  */
82 public class CK_TOKEN_INFO {
83 
84     /* label, manufacturerID, and model have been changed from
85      * CK_CHAR to CK_UTF8CHAR for v2.11. */
86     /**
87      * must be blank padded and only the first 32 chars will be used<p>
88      * <B>PKCS#11:</B>
89      * <PRE>
90      *   CK_UTF8CHAR label[32];
91      * </PRE>
92      */
93     public char[] label;           /* blank padded */
94 
95     /**
96      * must be blank padded and only the first 32 chars will be used<p>
97      * <B>PKCS#11:</B>
98      * <PRE>
99      *   CK_UTF8CHAR manufacturerID[32];
100      * </PRE>
101      */
102     public char[] manufacturerID;  /* blank padded */
103 
104     /**
105      * must be blank padded and only the first 16 chars will be used<p>
106      * <B>PKCS#11:</B>
107      * <PRE>
108      *   CK_UTF8CHAR model[16];
109      * </PRE>
110      */
111     public char[] model;           /* blank padded */
112 
113     /**
114      * must be blank padded and only the first 16 chars will be used<p>
115      * <B>PKCS#11:</B>
116      * <PRE>
117      *   CK_CHAR serialNumber[16];
118      * </PRE>
119      */
120     public char[] serialNumber;    /* blank padded */
121 
122     /**
123      * <B>PKCS#11:</B>
124      * <PRE>
125      *   CK_FLAGS flags;
126      * </PRE>
127      */
128     public long flags;               /* see below */
129 
130     /* ulMaxSessionCount, ulSessionCount, ulMaxRwSessionCount,
131      * ulRwSessionCount, ulMaxPinLen, and ulMinPinLen have all been
132      * changed from CK_USHORT to CK_ULONG for v2.0 */
133     /**
134      * <B>PKCS#11:</B>
135      * <PRE>
136      *   CK_ULONG ulMaxSessionCount;
137      * </PRE>
138      */
139     public long ulMaxSessionCount;     /* max open sessions */
140 
141     /**
142      * <B>PKCS#11:</B>
143      * <PRE>
144      *   CK_ULONG ulSessionCount;
145      * </PRE>
146      */
147     public long ulSessionCount;        /* sess. now open */
148 
149     /**
150      * <B>PKCS#11:</B>
151      * <PRE>
152      *   CK_ULONG ulMaxRwSessionCount;
153      * </PRE>
154      */
155     public long ulMaxRwSessionCount;   /* max R/W sessions */
156 
157     /**
158      * <B>PKCS#11:</B>
159      * <PRE>
160      *   CK_ULONG ulRwSessionCount;
161      * </PRE>
162      */
163     public long ulRwSessionCount;      /* R/W sess. now open */
164 
165     /**
166      * <B>PKCS#11:</B>
167      * <PRE>
168      *   CK_ULONG ulMaxPinLen;
169      * </PRE>
170      */
171     public long ulMaxPinLen;           /* in bytes */
172 
173     /**
174      * <B>PKCS#11:</B>
175      * <PRE>
176      *   CK_ULONG ulMinPinLen;
177      * </PRE>
178      */
179     public long ulMinPinLen;           /* in bytes */
180 
181     /**
182      * <B>PKCS#11:</B>
183      * <PRE>
184      *   CK_ULONG ulTotalPublicMemory;
185      * </PRE>
186      */
187     public long ulTotalPublicMemory;   /* in bytes */
188 
189     /**
190      * <B>PKCS#11:</B>
191      * <PRE>
192      *   CK_ULONG ulFreePublicMemory;
193      * </PRE>
194      */
195     public long ulFreePublicMemory;    /* in bytes */
196 
197     /**
198      * <B>PKCS#11:</B>
199      * <PRE>
200      *   CK_ULONG ulTotalPrivateMemory;
201      * </PRE>
202      */
203     public long ulTotalPrivateMemory;  /* in bytes */
204 
205     /**
206      * <B>PKCS#11:</B>
207      * <PRE>
208      *   CK_ULONG ulFreePrivateMemory;
209      * </PRE>
210      */
211     public long ulFreePrivateMemory;   /* in bytes */
212 
213     /* hardwareVersion, firmwareVersion, and time are new for
214      * v2.0 */
215     /**
216      * <B>PKCS#11:</B>
217      * <PRE>
218      *   CK_VERSION hardwareVersion;
219      * </PRE>
220      */
221     public CK_VERSION    hardwareVersion;       /* version of hardware */
222 
223     /**
224      * <B>PKCS#11:</B>
225      * <PRE>
226      *   CK_VERSION firmwareVersion;
227      * </PRE>
228      */
229     public CK_VERSION    firmwareVersion;       /* version of firmware */
230 
231     /**
232      * only the first 16 chars will be used
233      * <B>PKCS#11:</B>
234      * <PRE>
235      *   CK_CHAR utcTime[16];
236      * </PRE>
237      */
238     public char[] utcTime;           /* time */
239 
CK_TOKEN_INFO(char[] label, char[] vendor, char[] model, char[] serialNo, long flags, long sessionMax, long session, long rwSessionMax, long rwSession, long pinLenMax, long pinLenMin, long totalPubMem, long freePubMem, long totalPrivMem, long freePrivMem, CK_VERSION hwVer, CK_VERSION fwVer, char[] utcTime)240     public CK_TOKEN_INFO(char[] label, char[] vendor, char[] model,
241                          char[] serialNo, long flags,
242                          long sessionMax, long session,
243                          long rwSessionMax, long rwSession,
244                          long pinLenMax, long pinLenMin,
245                          long totalPubMem, long freePubMem,
246                          long totalPrivMem, long freePrivMem,
247                          CK_VERSION hwVer, CK_VERSION fwVer, char[] utcTime) {
248         this.label = label;
249         this.manufacturerID = vendor;
250         this.model = model;
251         this.serialNumber = serialNo;
252         this.flags = flags;
253         this.ulMaxSessionCount = sessionMax;
254         this.ulSessionCount = session;
255         this.ulMaxRwSessionCount = rwSessionMax;
256         this.ulRwSessionCount = rwSession;
257         this.ulMaxPinLen = pinLenMax;
258         this.ulMinPinLen = pinLenMin;
259         this.ulTotalPublicMemory = totalPubMem;
260         this.ulFreePublicMemory = freePubMem;
261         this.ulTotalPrivateMemory = totalPrivMem;
262         this.ulFreePrivateMemory = freePrivMem;
263         this.hardwareVersion = hwVer;
264         this.firmwareVersion = fwVer;
265         this.utcTime = utcTime;
266     }
267 
268     /**
269      * Returns the string representation of CK_TOKEN_INFO.
270      *
271      * @return the string representation of CK_TOKEN_INFO
272      */
toString()273     public String toString() {
274         StringBuilder sb = new StringBuilder();
275 
276         sb.append(Constants.INDENT);
277         sb.append("label: ");
278         sb.append(new String(label));
279         sb.append(Constants.NEWLINE);
280 
281         sb.append(Constants.INDENT);
282         sb.append("manufacturerID: ");
283         sb.append(new String(manufacturerID));
284         sb.append(Constants.NEWLINE);
285 
286         sb.append(Constants.INDENT);
287         sb.append("model: ");
288         sb.append(new String(model));
289         sb.append(Constants.NEWLINE);
290 
291         sb.append(Constants.INDENT);
292         sb.append("serialNumber: ");
293         sb.append(new String(serialNumber));
294         sb.append(Constants.NEWLINE);
295 
296         sb.append(Constants.INDENT);
297         sb.append("flags: ");
298         sb.append(Functions.tokenInfoFlagsToString(flags));
299         sb.append(Constants.NEWLINE);
300 
301         sb.append(Constants.INDENT);
302         sb.append("ulMaxSessionCount: ");
303         sb.append((ulMaxSessionCount == PKCS11Constants.CK_EFFECTIVELY_INFINITE)
304                   ? "CK_EFFECTIVELY_INFINITE"
305                   : (ulMaxSessionCount == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
306                     ? "CK_UNAVAILABLE_INFORMATION"
307                     : String.valueOf(ulMaxSessionCount));
308         sb.append(Constants.NEWLINE);
309 
310         sb.append(Constants.INDENT);
311         sb.append("ulSessionCount: ");
312         sb.append((ulSessionCount == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
313                   ? "CK_UNAVAILABLE_INFORMATION"
314                   : String.valueOf(ulSessionCount));
315         sb.append(Constants.NEWLINE);
316 
317         sb.append(Constants.INDENT);
318         sb.append("ulMaxRwSessionCount: ");
319         sb.append((ulMaxRwSessionCount == PKCS11Constants.CK_EFFECTIVELY_INFINITE)
320                   ? "CK_EFFECTIVELY_INFINITE"
321                   : (ulMaxRwSessionCount == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
322                     ? "CK_UNAVAILABLE_INFORMATION"
323                     : String.valueOf(ulMaxRwSessionCount));
324         sb.append(Constants.NEWLINE);
325 
326         sb.append(Constants.INDENT);
327         sb.append("ulRwSessionCount: ");
328         sb.append((ulRwSessionCount == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
329                   ? "CK_UNAVAILABLE_INFORMATION"
330                   : String.valueOf(ulRwSessionCount));
331         sb.append(Constants.NEWLINE);
332 
333         sb.append(Constants.INDENT);
334         sb.append("ulMaxPinLen: ");
335         sb.append(String.valueOf(ulMaxPinLen));
336         sb.append(Constants.NEWLINE);
337 
338         sb.append(Constants.INDENT);
339         sb.append("ulMinPinLen: ");
340         sb.append(String.valueOf(ulMinPinLen));
341         sb.append(Constants.NEWLINE);
342 
343         sb.append(Constants.INDENT);
344         sb.append("ulTotalPublicMemory: ");
345         sb.append((ulTotalPublicMemory == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
346                   ? "CK_UNAVAILABLE_INFORMATION"
347                   : String.valueOf(ulTotalPublicMemory));
348         sb.append(Constants.NEWLINE);
349 
350         sb.append(Constants.INDENT);
351         sb.append("ulFreePublicMemory: ");
352         sb.append((ulFreePublicMemory == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
353                   ? "CK_UNAVAILABLE_INFORMATION"
354                   : String.valueOf(ulFreePublicMemory));
355         sb.append(Constants.NEWLINE);
356 
357         sb.append(Constants.INDENT);
358         sb.append("ulTotalPrivateMemory: ");
359         sb.append((ulTotalPrivateMemory == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
360                   ? "CK_UNAVAILABLE_INFORMATION"
361                   : String.valueOf(ulTotalPrivateMemory));
362         sb.append(Constants.NEWLINE);
363 
364         sb.append(Constants.INDENT);
365         sb.append("ulFreePrivateMemory: ");
366         sb.append((ulFreePrivateMemory == PKCS11Constants.CK_UNAVAILABLE_INFORMATION)
367                   ? "CK_UNAVAILABLE_INFORMATION"
368                   : String.valueOf(ulFreePrivateMemory));
369         sb.append(Constants.NEWLINE);
370 
371         sb.append(Constants.INDENT);
372         sb.append("hardwareVersion: ");
373         sb.append(hardwareVersion.toString());
374         sb.append(Constants.NEWLINE);
375 
376         sb.append(Constants.INDENT);
377         sb.append("firmwareVersion: ");
378         sb.append(firmwareVersion.toString());
379         sb.append(Constants.NEWLINE);
380 
381         sb.append(Constants.INDENT);
382         sb.append("utcTime: ");
383         sb.append(new String(utcTime));
384         //buffer.append(Constants.NEWLINE);
385 
386         return sb.toString() ;
387     }
388 
389 }
390