Debian Codegraph @

pkcs11/xdg-store/dump-trust-file.c

raw
1 /*
2 * gnome-keyring
3 *
4 * Copyright 2010 (C) Collabora Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see
18 * <http://www.gnu.org/licenses/>.
19 *
20 * Author: Stef Walter <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "egg/egg-asn1x.h"
26 #include "egg/egg-asn1-defs.h"
27 #include "egg/egg-dn.h"
28 #include "egg/egg-error.h"
29 #include "egg/egg-hex.h"
30
31 #include <stdlib.h>
32
33 /* Bring in the relevant definitions */
34 #include "xdg-store/gkm-xdg-asn1-defs.h"
35
36 static void
37 barf_and_die (const char *msg, const char *detail)
38 {
39 if (detail)
40 g_printerr ("dump-trust-file: %s: %s\n", msg, detail);
41 else
42 g_printerr ("dump-trust-file: %s\n", msg);
43 exit (1);
44 }
45
46 static void
47 dump_certificate_reference (GNode *asn)
48 {
49 gchar *issuer, *serial;
50 GBytes *data;
51 GNode *name;
52 GBytes *element;
53
54 /* Parse the name out */
55 name = egg_asn1x_create (pkix_asn1_tab, "Name");
56 g_return_if_fail (name);
57 element = egg_asn1x_get_element_raw (egg_asn1x_node (asn, "issuer", NULL));
58 g_return_if_fail (element);
59 if (!egg_asn1x_decode (name, element))
60 barf_and_die ("couldn't parse certificate", egg_asn1x_message (name));
61 g_bytes_unref (element);
62
63 issuer = egg_dn_read (egg_asn1x_node (name, "rdnSequence", NULL));
64 g_return_if_fail (issuer);
65
66 data = egg_asn1x_get_integer_as_raw (egg_asn1x_node (asn, "serialNumber", NULL));
67 g_return_if_fail (data != NULL);
68 serial = egg_hex_encode (g_bytes_get_data (data, NULL), g_bytes_get_size (data));
69
70 g_print ("Reference\n");
71 g_print (" issuer: %s\n", issuer);
72 g_print (" serial: 0x%s\n", serial);
73
74 egg_asn1x_destroy (name);
75
76 g_free (serial);
77 g_free (issuer);
78 }
79
80 static void
81 dump_certificate_complete (GNode *asn)
82 {
83 GNode *cert;
84 gchar *issuer, *serial, *subject;
85 GBytes *element;
86 GBytes *data;
87
88 /* Parse the certificate out */
89 cert = egg_asn1x_create (pkix_asn1_tab, "Certificate");
90 g_return_if_fail (cert);
91
92 element = egg_asn1x_get_element_raw (asn);
93 g_return_if_fail (element);
94 if (!egg_asn1x_decode (cert, element))
95 barf_and_die ("couldn't parse certificate", egg_asn1x_message (cert));
96 g_bytes_unref (element);
97
98 issuer = egg_dn_read (egg_asn1x_node (cert, "tbsCertificate", "issuer", "rdnSequence", NULL));
99 g_return_if_fail (issuer);
100
101 subject = egg_dn_read (egg_asn1x_node (cert, "tbsCertificate", "subject", "rdnSequence", NULL));
102 g_return_if_fail (subject);
103
104 data = egg_asn1x_get_integer_as_raw (egg_asn1x_node (cert, "tbsCertificate", "serialNumber", NULL));
105 g_return_if_fail (data != NULL);
106 serial = egg_hex_encode (g_bytes_get_data (data, NULL), g_bytes_get_size (data));
107 g_bytes_unref (data);
108
109 g_print ("Complete\n");
110 g_print (" issuer: %s\n", issuer);
111 g_print (" subject: %s\n", subject);
112 g_print (" serial: 0x%s\n", serial);
113
114 egg_asn1x_destroy (cert);
115
116 g_free (serial);
117 g_free (issuer);
118 g_free (subject);
119 }
120
121
122 static void
123 dump_assertion (GNode *asn)
124 {
125 gchar *purpose, *peer;
126 GQuark level;
127
128 purpose = egg_asn1x_get_string_as_utf8 (egg_asn1x_node (asn, "purpose", NULL), NULL);
129 g_return_if_fail (purpose);
130
131 level = egg_asn1x_get_enumerated (egg_asn1x_node (asn, "level", NULL));
132 g_return_if_fail (level);
133
134 if (egg_asn1x_have (egg_asn1x_node (asn, "peer", NULL)))
135 peer = egg_asn1x_get_string_as_utf8 (egg_asn1x_node (asn, "peer", NULL), NULL);
136 else
137 peer = NULL;
138
139 g_print ("Assertion\n");
140 g_print (" purpose: %s\n", purpose);
141 g_print (" level: %s\n", g_quark_to_string (level));
142 if (peer)
143 g_print (" peer: %s\n", peer);
144
145 g_free (purpose);
146 g_free (peer);
147 }
148
149 int
150 main(int argc, char* argv[])
151 {
152 GError *err = NULL;
153 gchar *contents;
154 gsize n_contents;
155 GNode *asn, *node;
156 GBytes *bytes;
157 gint i, count;
158
159 if (argc != 2) {
160 g_printerr ("usage: dump-trust-file file\n");
161 return 2;
162 }
163
164 if (!g_file_get_contents (argv[1], &contents, &n_contents, &err))
165 barf_and_die ("couldn't load file", egg_error_message (err));
166
167 asn = egg_asn1x_create (xdg_asn1_tab, "trust-1");
168 g_return_val_if_fail (asn, 1);
169
170 bytes = g_bytes_new_take (contents, n_contents);
171 if (!egg_asn1x_decode (asn, bytes))
172 barf_and_die ("couldn't parse file", egg_asn1x_message (asn));
173 g_bytes_unref (bytes);
174
175 /* Print out the certificate we refer to first */
176 node = egg_asn1x_node (asn, "reference", "certReference", NULL);
177 if (egg_asn1x_have (node)) {
178 dump_certificate_reference (node);
179 } else {
180 node = egg_asn1x_node (asn, "reference", "certComplete", NULL);
181 if (egg_asn1x_have (node))
182 dump_certificate_complete (node);
183 else
184 barf_and_die ("unsupported certificate reference", NULL);
185 }
186
187 /* Then the assertions */
188 count = egg_asn1x_count (egg_asn1x_node (asn, "assertions", NULL));
189 for (i = 0; i < count; ++i) {
190 node = egg_asn1x_node (asn, "assertions", i + 1, NULL);
191 dump_assertion (node);
192 }
193
194 egg_asn1x_destroy (asn);
195
196 return 0;
197 }