minimal build
[platform/upstream/gcr.git] / gck / gck-dump.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gck-dump - the GObject PKCS#11 wrapper library
3
4    Copyright (C) 2010 Collabora Ltd
5
6    The Gnome Keyring Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The Gnome Keyring Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the Gnome Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20
21    Author: Stef Walter <stefw@collabora.co.uk>
22 */
23
24 #include "config.h"
25
26 #include "gck.h"
27 #include "gck-private.h"
28
29 #include "egg/egg-hex.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "pkcs11i.h"
35 #include "pkcs11x.h"
36
37 static void
38 dump_class_value (gulong klass)
39 {
40         switch (klass) {
41         #define DX(x) case x: g_printerr ("%s", #x); break;
42         DX(CKO_DATA);
43         DX(CKO_CERTIFICATE);
44         DX(CKO_PUBLIC_KEY);
45         DX(CKO_PRIVATE_KEY);
46         DX(CKO_SECRET_KEY);
47         DX(CKO_HW_FEATURE);
48         DX(CKO_DOMAIN_PARAMETERS);
49         DX(CKO_MECHANISM);
50         DX(CKO_G_COLLECTION);
51         DX(CKO_G_SEARCH);
52         DX(CKO_G_CREDENTIAL);
53         DX(CKO_X_TRUST_ASSERTION);
54         #undef DX
55
56         default:
57                 g_printerr ("%s0x%08x",
58                             (klass & CKO_VENDOR_DEFINED) == CKA_VENDOR_DEFINED ?
59                                      "CKO_VENDOR_DEFINED|" : "",
60                             (unsigned int)klass);
61                 break;
62         }
63 }
64
65 static void
66 dump_assertion_type_value (gulong type)
67 {
68         switch (type) {
69         #define DX(x) case x: g_printerr ("%s", #x); break;
70         DX(CKT_X_DISTRUSTED_CERTIFICATE);
71         DX(CKT_X_PINNED_CERTIFICATE);
72         DX(CKT_X_ANCHORED_CERTIFICATE);
73         #undef DX
74
75         default:
76                 g_printerr ("%u", (unsigned int)type);
77                 break;
78         }
79 }
80
81 static void
82 dump_attribute_value (const GckAttribute *attr)
83 {
84         gchar *data;
85         gsize len;
86
87         g_assert (attr->length != G_MAXULONG);
88
89         if (attr->value == NULL) {
90                 g_printerr ("[null]");
91                 return;
92         }
93
94         switch (attr->type) {
95         case CKA_CLASS:
96                 if (attr->length == sizeof (CK_OBJECT_CLASS)) {
97                         dump_class_value (*(CK_ULONG_PTR)attr->value);
98                         return;
99                 }
100                 break;
101
102         case CKA_X_ASSERTION_TYPE:
103                 if (attr->length == sizeof (CK_X_ASSERTION_TYPE)) {
104                         dump_assertion_type_value (*(CK_X_ASSERTION_TYPE*)attr->value);
105                         return;
106                 }
107                 break;
108
109         case CKA_CERTIFICATE_TYPE:
110         case CKA_CERTIFICATE_CATEGORY:
111         case CKA_JAVA_MIDP_SECURITY_DOMAIN:
112         case CKA_KEY_TYPE:
113         case CKA_PRIME_BITS:
114         case CKA_SUB_PRIME_BITS:
115         case CKA_VALUE_BITS:
116         case CKA_VALUE_LEN:
117         case CKA_KEY_GEN_MECHANISM:
118         case CKA_HW_FEATURE_TYPE:
119         case CKA_PIXEL_X:
120         case CKA_PIXEL_Y:
121         case CKA_RESOLUTION:
122         case CKA_CHAR_ROWS:
123         case CKA_CHAR_COLUMNS:
124         case CKA_BITS_PER_PIXEL:
125         case CKA_MECHANISM_TYPE:
126         case CKA_G_DESTRUCT_IDLE:
127         case CKA_G_DESTRUCT_AFTER:
128         case CKA_G_DESTRUCT_USES:
129         case CKA_G_OBJECT:
130         case CKA_G_CREDENTIAL:
131                 if (attr->length == sizeof (CK_ULONG)) {
132                         g_printerr ("%llu", (unsigned long long)*(CK_ULONG_PTR)attr->value);
133                         return;
134                 }
135                 break;
136
137         case CKA_TOKEN:
138         case CKA_PRIVATE:
139         case CKA_TRUSTED:
140         case CKA_SENSITIVE:
141         case CKA_ENCRYPT:
142         case CKA_DECRYPT:
143         case CKA_WRAP:
144         case CKA_UNWRAP:
145         case CKA_SIGN:
146         case CKA_SIGN_RECOVER:
147         case CKA_VERIFY:
148         case CKA_VERIFY_RECOVER:
149         case CKA_DERIVE:
150         case CKA_EXTRACTABLE:
151         case CKA_LOCAL:
152         case CKA_NEVER_EXTRACTABLE:
153         case CKA_ALWAYS_SENSITIVE:
154         case CKA_MODIFIABLE:
155         case CKA_ALWAYS_AUTHENTICATE:
156         case CKA_WRAP_WITH_TRUSTED:
157         case CKA_RESET_ON_INIT:
158         case CKA_HAS_RESET:
159         case CKA_COLOR:
160         case CKA_G_LOCKED:
161         case CKA_G_LOGIN_COLLECTION:
162                 if (attr->length == sizeof (CK_BBOOL)) {
163                         g_printerr ("%s", (*(CK_BBOOL*)attr->value) ? "TRUE" : "FALSE");
164                         return;
165                 }
166                 break;
167
168         case CKA_LABEL:
169         case CKA_URL:
170         case CKA_CHAR_SETS:
171         case CKA_ENCODING_METHODS:
172         case CKA_MIME_TYPES:
173         case CKA_G_COLLECTION:
174         case CKA_G_SCHEMA:
175         case CKA_X_PURPOSE:
176         case CKA_X_PEER:
177                 if (g_utf8_validate ((const gchar *)attr->value, attr->length, NULL)) {
178                         int length = MIN (32, attr->length);
179                         g_printerr ("%.*s%s", length, (gchar*)attr->value,
180                                     length < attr->length ? "..." : "");
181                         return;
182                 }
183                 break;
184
185         case CKA_START_DATE:
186         case CKA_END_DATE:
187         case CKA_G_CREATED:
188         case CKA_G_MODIFIED:
189                 if (attr->length == sizeof (CK_DATE)) {
190                         const CK_DATE* date = (const CK_DATE *)attr->value;
191                         g_printerr ("%.4s-%.2s-%.2s", date->year, date->month, date->day);
192                         return;
193                 }
194                 break;
195
196         default:
197                 break;
198         };
199
200         len = MIN (20, attr->length);
201         data = egg_hex_encode_full (attr->value, len, TRUE, ":", 1);
202         g_printerr ("%s%s", data, len < attr->length ? "..." : "");
203         g_free (data);
204 }
205
206 static void
207 dump_attribute_type (const GckAttribute *attr)
208 {
209         switch (attr->type) {
210         #define DX(x) case x: g_printerr ("%s", #x); break;
211         DX(CKA_CLASS);
212         DX(CKA_TOKEN);
213         DX(CKA_PRIVATE);
214         DX(CKA_LABEL);
215         DX(CKA_APPLICATION);
216         DX(CKA_VALUE);
217         DX(CKA_OBJECT_ID);
218         DX(CKA_CERTIFICATE_TYPE);
219         DX(CKA_ISSUER);
220         DX(CKA_SERIAL_NUMBER);
221         DX(CKA_AC_ISSUER);
222         DX(CKA_OWNER);
223         DX(CKA_ATTR_TYPES);
224         DX(CKA_TRUSTED);
225         DX(CKA_CERTIFICATE_CATEGORY);
226         DX(CKA_JAVA_MIDP_SECURITY_DOMAIN);
227         DX(CKA_URL);
228         DX(CKA_HASH_OF_SUBJECT_PUBLIC_KEY);
229         DX(CKA_HASH_OF_ISSUER_PUBLIC_KEY);
230         DX(CKA_CHECK_VALUE);
231         DX(CKA_KEY_TYPE);
232         DX(CKA_SUBJECT);
233         DX(CKA_ID);
234         DX(CKA_SENSITIVE);
235         DX(CKA_ENCRYPT);
236         DX(CKA_DECRYPT);
237         DX(CKA_WRAP);
238         DX(CKA_UNWRAP);
239         DX(CKA_SIGN);
240         DX(CKA_SIGN_RECOVER);
241         DX(CKA_VERIFY);
242         DX(CKA_VERIFY_RECOVER);
243         DX(CKA_DERIVE);
244         DX(CKA_START_DATE);
245         DX(CKA_END_DATE);
246         DX(CKA_MODULUS);
247         DX(CKA_MODULUS_BITS);
248         DX(CKA_PUBLIC_EXPONENT);
249         DX(CKA_PRIVATE_EXPONENT);
250         DX(CKA_PRIME_1);
251         DX(CKA_PRIME_2);
252         DX(CKA_EXPONENT_1);
253         DX(CKA_EXPONENT_2);
254         DX(CKA_COEFFICIENT);
255         DX(CKA_PRIME);
256         DX(CKA_SUBPRIME);
257         DX(CKA_BASE);
258         DX(CKA_PRIME_BITS);
259         DX(CKA_SUB_PRIME_BITS);
260         DX(CKA_VALUE_BITS);
261         DX(CKA_VALUE_LEN);
262         DX(CKA_EXTRACTABLE);
263         DX(CKA_LOCAL);
264         DX(CKA_NEVER_EXTRACTABLE);
265         DX(CKA_ALWAYS_SENSITIVE);
266         DX(CKA_KEY_GEN_MECHANISM);
267         DX(CKA_MODIFIABLE);
268         /* DX(CKA_ECDSA_PARAMS); */
269         DX(CKA_EC_PARAMS);
270         DX(CKA_EC_POINT);
271         DX(CKA_SECONDARY_AUTH);
272         DX(CKA_AUTH_PIN_FLAGS);
273         DX(CKA_ALWAYS_AUTHENTICATE);
274         DX(CKA_WRAP_WITH_TRUSTED);
275         DX(CKA_HW_FEATURE_TYPE);
276         DX(CKA_RESET_ON_INIT);
277         DX(CKA_HAS_RESET);
278         DX(CKA_PIXEL_X);
279         DX(CKA_PIXEL_Y);
280         DX(CKA_RESOLUTION);
281         DX(CKA_CHAR_ROWS);
282         DX(CKA_CHAR_COLUMNS);
283         DX(CKA_COLOR);
284         DX(CKA_BITS_PER_PIXEL);
285         DX(CKA_CHAR_SETS);
286         DX(CKA_ENCODING_METHODS);
287         DX(CKA_MIME_TYPES);
288         DX(CKA_MECHANISM_TYPE);
289         DX(CKA_REQUIRED_CMS_ATTRIBUTES);
290         DX(CKA_DEFAULT_CMS_ATTRIBUTES);
291         DX(CKA_SUPPORTED_CMS_ATTRIBUTES);
292         DX(CKA_WRAP_TEMPLATE);
293         DX(CKA_UNWRAP_TEMPLATE);
294         DX(CKA_ALLOWED_MECHANISMS);
295
296         /* GNOME */
297         DX(CKA_G_LOCKED);
298         DX(CKA_G_CREATED);
299         DX(CKA_G_MODIFIED);
300         DX(CKA_G_FIELDS);
301         DX(CKA_G_COLLECTION);
302         DX(CKA_G_MATCHED);
303         DX(CKA_G_SCHEMA);
304         DX(CKA_G_LOGIN_COLLECTION);
305         DX(CKA_G_DESTRUCT_IDLE);
306         DX(CKA_G_DESTRUCT_AFTER);
307         DX(CKA_G_DESTRUCT_USES);
308         DX(CKA_G_OBJECT);
309         DX(CKA_G_CREDENTIAL);
310         DX(CKA_G_CREDENTIAL_TEMPLATE);
311         DX(CKA_X_ASSERTION_TYPE);
312         DX(CKA_X_CERTIFICATE_VALUE);
313         DX(CKA_X_PURPOSE);
314         DX(CKA_X_PEER);
315         #undef DX
316
317         default:
318                 g_printerr ("%s0x%08x",
319                             (attr->type & CKA_VENDOR_DEFINED) == CKA_VENDOR_DEFINED ?
320                                      "CKA_VENDOR_DEFINED|" : "",
321                             (unsigned int)attr->type);
322                 break;
323         }
324 }
325
326 /**
327  * gck_attribute_dump:
328  * @attr: The attribute
329  *
330  * Dump the specified attribute using g_printerr().
331  */
332 void
333 gck_attribute_dump (const GckAttribute *attr)
334 {
335         dump_attribute_type (attr);
336         if (attr->length == G_MAXULONG) {
337                 g_printerr ("\n    [invalid]\n");
338         } else {
339                 g_printerr ("\n    [%lu] ", (unsigned long)attr->length);
340                 dump_attribute_value (attr);
341                 g_printerr ("\n");
342         }
343 }
344
345 /**
346  * gck_attributes_dump:
347  * @attrs: The attributes
348  *
349  * Dump the attributes using g_printerr().
350  */
351 void
352 gck_attributes_dump (GckAttributes *attrs)
353 {
354         const GckAttribute *attr;
355         guint i, count;
356
357         for (i = 0, count = gck_attributes_count (attrs); i < count; ++i) {
358                 attr = gck_attributes_at (attrs, i);
359                 gck_attribute_dump (attr);
360         }
361 }