Debian Codegraph @

src/bin/dump/common.h

raw
1 /*
2 * Copyright (c) 2013 .SE (The Internet Infrastructure Foundation)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 common.h
29
30 Common definitions for SoftHSMv2 dump.
31 *****************************************************************************/
32
33 #ifndef _SOFTHSM_V2_COMMON_H
34 #define _SOFTHSM_V2_COMMON_H
35
36 #include <config.h>
37
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <map>
42 #include <set>
43 #include <string>
44 #include <stdexcept>
45 #include <vector>
46 #include "tables.h"
47
48 // Table of attribute types
49 std::map<unsigned long, std::string> CKA_table;
50
51 // Dump an attribute type
52 void dumpCKA(unsigned long cka, int size)
53 {
54 // Lazy fill
55 if (CKA_table.empty())
56 {
57 fill_CKA_table(CKA_table);
58 }
59 std::string msg;
60 try
61 {
62 msg = CKA_table.at(cka);
63 printf("%.*s", size, msg.c_str());
64 }
65 catch (const std::out_of_range&)
66 {
67 if (cka & CKA_VENDOR_DEFINED)
68 {
69 cka &= ~CKA_VENDOR_DEFINED;
70 printf("CKA_VENDOR_DEFINED | 0x%lx", cka);
71 }
72 else
73 {
74 printf("unknown 0x%lx", cka);
75 }
76 }
77 }
78
79 // Table of mechanism types
80 std::map<unsigned long, std::string> CKM_table;
81
82 // Dump a mechanism type
83 void dumpCKM(unsigned long cka, int size)
84 {
85 // Lazy fill
86 if (CKM_table.empty())
87 {
88 fill_CKM_table(CKM_table);
89 }
90 std::string msg;
91 try
92 {
93 msg = CKM_table.at(cka);
94 printf("%.*s", size, msg.c_str());
95 }
96 catch (const std::out_of_range&)
97 {
98 if (cka & CKM_VENDOR_DEFINED)
99 {
100 cka &= ~CKM_VENDOR_DEFINED;
101 printf("CKM_VENDOR_DEFINED | 0x%lx", cka);
102 }
103 else
104 {
105 printf("unknown 0x%lx", cka);
106 }
107 }
108 }
109
110 // Table of object classes
111 std::map<unsigned long, std::string> CKO_table;
112
113 // Dump a object class
114 void dumpCKO(unsigned long cka, int size)
115 {
116 // Lazy fill
117 if (CKO_table.empty())
118 {
119 fill_CKO_table(CKO_table);
120 }
121 std::string msg;
122 try
123 {
124 msg = CKO_table.at(cka);
125 printf("%.*s", size, msg.c_str());
126 }
127 catch (const std::out_of_range&)
128 {
129 if (cka & CKO_VENDOR_DEFINED)
130 {
131 cka &= ~CKO_VENDOR_DEFINED;
132 printf("CKO_VENDOR_DEFINED | 0x%lx", cka);
133 }
134 else
135 {
136 printf("unknown 0x%lx", cka);
137 }
138 }
139 }
140
141 // Table of hw feature types
142 std::map<unsigned long, std::string> CKH_table;
143
144 // Dump a hw feature type
145 void dumpCKH(unsigned long cka, int size)
146 {
147 // Lazy fill
148 if (CKH_table.empty())
149 {
150 fill_CKH_table(CKH_table);
151 }
152 std::string msg;
153 try
154 {
155 msg = CKH_table.at(cka);
156 printf("%.*s", size, msg.c_str());
157 }
158 catch (const std::out_of_range&)
159 {
160 if (cka & CKH_VENDOR_DEFINED)
161 {
162 cka &= ~CKH_VENDOR_DEFINED;
163 printf("CKH_VENDOR_DEFINED | 0x%lx", cka);
164 }
165 else
166 {
167 printf("unknown 0x%lx", cka);
168 }
169 }
170 }
171
172 // Table of key types
173 std::map<unsigned long, std::string> CKK_table;
174
175 // Dump a key type
176 void dumpCKK(unsigned long cka, int size)
177 {
178 // Lazy fill
179 if (CKK_table.empty())
180 {
181 fill_CKK_table(CKK_table);
182 }
183 std::string msg;
184 try
185 {
186 msg = CKK_table.at(cka);
187 printf("%.*s", size, msg.c_str());
188 }
189 catch (const std::out_of_range&)
190 {
191 if (cka & CKK_VENDOR_DEFINED)
192 {
193 cka &= ~CKK_VENDOR_DEFINED;
194 printf("CKK_VENDOR_DEFINED | 0x%lx", cka);
195 }
196 else
197 {
198 printf("unknown 0x%lx", cka);
199 }
200 }
201 }
202
203 // Table of certificate types
204 std::map<unsigned long, std::string> CKC_table;
205
206 // Dump a certificate type
207 void dumpCKC(unsigned long cka, int size)
208 {
209 // Lazy fill
210 if (CKC_table.empty())
211 {
212 fill_CKC_table(CKC_table);
213 }
214 std::string msg;
215 try
216 {
217 msg = CKC_table.at(cka);
218 printf("%.*s", size, msg.c_str());
219 }
220 catch (const std::out_of_range&)
221 {
222 if (cka & CKC_VENDOR_DEFINED)
223 {
224 cka &= ~CKC_VENDOR_DEFINED;
225 printf("CKC_VENDOR_DEFINED | 0x%lx", cka);
226 }
227 else
228 {
229 printf("unknown 0x%lx", cka);
230 }
231 }
232 }
233
234 // Dump a PKCS#11 integer type
235 void dumpCKx(uint64_t cka, uint64_t value, int size)
236 {
237 if ((uint32_t)value == (uint32_t)~0)
238 {
239 printf("CK_UNAVAILABLE_INFORMATION");
240 return;
241 }
242
243 switch ((unsigned long) cka)
244 {
245 case CKA_CLASS:
246 if ((uint64_t)((uint32_t)value) != value)
247 {
248 printf("overflow object class");
249 break;
250 }
251 dumpCKO((unsigned long) value, size);
252 break;
253 case CKA_CERTIFICATE_TYPE:
254 if ((uint64_t)((uint32_t)value) != value)
255 {
256 printf("overflow certificate type");
257 break;
258 }
259 dumpCKC((unsigned long) value, size);
260 break;
261 case CKA_KEY_TYPE:
262 if ((uint64_t)((uint32_t)value) != value)
263 {
264 printf("overflow key type");
265 break;
266 }
267 dumpCKK((unsigned long) value, size);
268 break;
269 case CKA_KEY_GEN_MECHANISM:
270 if ((uint64_t)((uint32_t)value) != value)
271 {
272 printf("overflow mechanism type");
273 break;
274 }
275 dumpCKM((unsigned long) value, size);
276 break;
277 case CKA_HW_FEATURE_TYPE:
278 if ((uint64_t)((uint32_t)value) != value)
279 {
280 printf("overflow hw feature type");
281 break;
282 }
283 dumpCKH((unsigned long) value, size);
284 break;
285 default:
286 printf("CK_ULONG %lu(0x%lx)",
287 (unsigned long) value,
288 (unsigned long) value);
289 break;
290 }
291 }
292
293 // Dump a boolean (in fact unsigned 8 bit long) value, true is 0xff
294 void dumpBool(uint8_t value, bool inArray = false)
295 {
296 printf("%02hhx %s", value, inArray ? " " : "");
297 switch (value)
298 {
299 case 0:
300 printf("FALSE");
301 break;
302 case 0xff:
303 printf("TRUE");
304 break;
305 default:
306 printf("(invalid) TRUE");
307 break;
308 }
309 }
310
311 // Dump a boolean (in fact unsigned 8 bit long) value, true is 1
312 void dumpBool1(uint8_t value, bool inArray = false)
313 {
314 printf("%02hhx %s", value, inArray ? " " : "");
315 switch (value)
316 {
317 case 0:
318 printf("FALSE");
319 break;
320 case 1:
321 printf("TRUE");
322 break;
323 default:
324 printf("(invalid) TRUE");
325 break;
326 }
327 }
328
329 // Dump an unsigned 64 bit long value
330 void dumpULong(uint64_t value, bool inArray = false)
331 {
332 for (int i = 56; i >= 0; i -= 8)
333 {
334 uint8_t v;
335 v = (value >> i) & 0xff;
336 printf("%02hhx ", v);
337 }
338 if (inArray)
339 {
340 printf(" ");
341 }
342 }
343
344 // Dump an unsigned 32 bit long value
345 void dumpU32(uint32_t value, bool inArray = false)
346 {
347 for (int i = 24; i >= 0; i -= 8)
348 {
349 uint8_t v;
350 v = (value >> i) & 0xff;
351 printf("%02hhx ", v);
352 }
353 printf(" ");
354 if (inArray)
355 {
356 printf(" ");
357 }
358 }
359
360 // Dump a byte string (aka uint8_t vector) value
361 void dumpBytes(const std::vector<uint8_t>& value, bool inArray = false)
362 {
363 size_t len = value.size();
364 size_t i = 0;
365 while (i + 8 <= len)
366 {
367 for (size_t j = 0; j < 8; j++)
368 {
369 printf("%02hhx ", value[i + j]);
370 }
371 if (inArray)
372 {
373 printf(" ");
374 }
375 printf("<");
376 for (size_t j = 0; j < 8; j++)
377 {
378 uint8_t c = value[i + j];
379 if (isgraph((int) c) == 0)
380 {
381 printf(".");
382 }
383 else
384 {
385 printf("%c", (int) c);
386 }
387 }
388 printf(">\n");
389 i += 8;
390 }
391 len -= i;
392 if (len == 0)
393 {
394 return;
395 }
396
397 for (size_t j = 0; j < len; j++)
398 {
399 printf("%02hhx ", value[i + j]);
400 }
401 for (size_t j = len; j < 8; j++)
402 {
403 printf(" ");
404 }
405 if (inArray)
406 {
407 printf(" ");
408 }
409 printf("<");
410 for (size_t j = 0; j < len; j++)
411 {
412 uint8_t c = value[i + j];
413 if (isgraph((int) c) == 0)
414 {
415 printf(".");
416 }
417 else
418 {
419 printf("%c", (int) c);
420 }
421 }
422 for (size_t j =len; j < 8; j++)
423 {
424 printf(" ");
425 }
426 printf(">\n");
427 }
428
429 // Attribute (in an array) template
430 template<typename T, typename K, typename I>
431 class AttributeTK
432 {
433 public:
434 T type;
435 K kind;
436
437 uint8_t boolValue;
438 I ulongValue;
439 std::vector<uint8_t> bytestrValue;
440 std::set<I> mechSetValue;
441
442 // Dump an array (in fact an Attribute vector) value
443 void dumpType() const;
444 void dumpKind() const;
445 void dumpBoolValue() const;
446 void dumpULongValue(I value) const;
447 bool isBoolean() const;
448 bool isInteger() const;
449 bool isBinary() const;
450 bool isMechSet() const;
451 void dump() const {
452 dumpType();
453 if ((sizeof(type) > 4) &&
454 ((uint64_t)((uint32_t)type) != type))
455 {
456 printf("overflow attribute type\n");
457 }
458 else
459 {
460 dumpCKA((unsigned long) type, 47);
461 printf("\n");
462 }
463
464 dumpKind();
465 if (isBoolean())
466 {
467 printf("boolean attribute\n");
468 dumpBoolValue();
469 printf("\n");
470 }
471 else if (isInteger())
472 {
473 printf("unsigned long attribute\n");
474 dumpULongValue(ulongValue);
475 dumpCKx(type, ulongValue, 47);
476 printf("\n");
477 }
478 else if (isBinary())
479 {
480 printf("byte string attribute\n");
481 I size = bytestrValue.size();
482 dumpULongValue(size);
483 printf("(length %lu)\n", (unsigned long) size);
484 dumpBytes(bytestrValue, true);
485 }
486 else if (isMechSet())
487 {
488 printf("mechanism set attribute\n");
489 I size = mechSetValue.size();
490 dumpULongValue(size);
491 printf("(length %lu)\n", (unsigned long) size);
492 for (typename std::set<I>::const_iterator i = mechSetValue.begin(); i != mechSetValue.end(); ++i)
493 {
494 dumpULongValue(*i);
495 dumpCKM(*i, 47);
496 printf("\n");
497 }
498 }
499 else
500 {
501 printf("unknown attribute format\n");
502 }
503 }
504 };
505
506 #endif // !_SOFTHSM_V2_COMMON_H