Line data Source code
1 : /*
2 : * gnome-keyring
3 : *
4 : * Copyright (C) 2010 Stefan Walter
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 :
21 : #include "config.h"
22 :
23 : #include "gkm-assertion.h"
24 : #include "gkm-attributes.h"
25 : #define DEBUG_FLAG GKM_DEBUG_OBJECT
26 : #include "gkm-debug.h"
27 : #include "gkm-object.h"
28 : #include "gkm-trust.h"
29 : #include "gkm-util.h"
30 :
31 : #include "pkcs11/pkcs11i.h"
32 : #include "pkcs11/pkcs11x.h"
33 :
34 : #include <glib/gi18n.h>
35 :
36 : enum {
37 : PROP_0,
38 : PROP_TRUST,
39 : PROP_TYPE,
40 : PROP_PURPOSE,
41 : PROP_PEER
42 : };
43 :
44 : struct _GkmAssertionPrivate {
45 : GkmTrust *trust;
46 : gulong type;
47 : gchar *purpose;
48 : gchar *peer;
49 : };
50 :
51 832 : G_DEFINE_TYPE_WITH_PRIVATE (GkmAssertion, gkm_assertion, GKM_TYPE_OBJECT);
52 :
53 : /* -----------------------------------------------------------------------------
54 : * INTERNAL
55 : */
56 :
57 :
58 : /* -----------------------------------------------------------------------------
59 : * OBJECT
60 : */
61 :
62 : static CK_RV
63 210 : gkm_assertion_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE_PTR attr)
64 : {
65 210 : GkmAssertion *self = GKM_ASSERTION (base);
66 :
67 210 : switch (attr->type)
68 : {
69 0 : case CKA_PRIVATE:
70 0 : return gkm_attribute_set_bool (attr, CK_FALSE);
71 128 : case CKA_CLASS:
72 128 : return gkm_attribute_set_ulong (attr, CKO_X_TRUST_ASSERTION);
73 0 : case CKA_MODIFIABLE:
74 0 : return gkm_attribute_set_bool (attr, CK_FALSE);
75 :
76 : /* Various trust flags */
77 5 : case CKA_X_ASSERTION_TYPE:
78 5 : return gkm_attribute_set_ulong (attr, self->pv->type);
79 3 : case CKA_X_PURPOSE:
80 3 : return gkm_attribute_set_string (attr, self->pv->purpose);
81 0 : case CKA_X_PEER:
82 0 : if (!self->pv->peer) {
83 0 : gkm_debug ("CKR_ATTRIBUTE_TYPE_INVALID: no CKA_X_PEER on assertion");
84 0 : return CKR_ATTRIBUTE_TYPE_INVALID;
85 : }
86 0 : return gkm_attribute_set_string (attr, self->pv->peer);
87 :
88 : /* Certificate reference values */
89 20 : case CKA_SERIAL_NUMBER:
90 : case CKA_ISSUER:
91 : case CKA_X_CERTIFICATE_VALUE:
92 20 : return gkm_object_get_attribute (GKM_OBJECT (self->pv->trust), session, attr);
93 :
94 54 : default:
95 54 : break;
96 : };
97 :
98 54 : return GKM_OBJECT_CLASS (gkm_assertion_parent_class)->get_attribute (base, session, attr);
99 : }
100 :
101 : static GObject*
102 47 : gkm_assertion_constructor (GType type, guint n_props, GObjectConstructParam *props)
103 : {
104 47 : GkmAssertion *self = GKM_ASSERTION (G_OBJECT_CLASS (gkm_assertion_parent_class)->constructor(type, n_props, props));
105 :
106 47 : g_return_val_if_fail (self, NULL);
107 47 : g_return_val_if_fail (self->pv->purpose, NULL);
108 47 : g_return_val_if_fail (self->pv->type, NULL);
109 :
110 47 : return G_OBJECT (self);
111 : }
112 :
113 : static void
114 47 : gkm_assertion_init (GkmAssertion *self)
115 : {
116 47 : self->pv = gkm_assertion_get_instance_private (self);
117 47 : }
118 :
119 : static void
120 47 : gkm_assertion_finalize (GObject *obj)
121 : {
122 47 : GkmAssertion *self = GKM_ASSERTION (obj);
123 :
124 47 : if (self->pv->trust)
125 4 : g_object_remove_weak_pointer (G_OBJECT (self->pv->trust), (gpointer*)&(self->pv->trust));
126 47 : self->pv->trust = NULL;
127 :
128 47 : g_free (self->pv->purpose);
129 47 : self->pv->purpose = NULL;
130 :
131 47 : g_free (self->pv->peer);
132 47 : self->pv->peer = NULL;
133 :
134 47 : G_OBJECT_CLASS (gkm_assertion_parent_class)->finalize (obj);
135 47 : }
136 :
137 :
138 : static void
139 188 : gkm_assertion_set_property (GObject *obj, guint prop_id, const GValue *value,
140 : GParamSpec *pspec)
141 : {
142 188 : GkmAssertion *self = GKM_ASSERTION (obj);
143 :
144 188 : switch (prop_id) {
145 47 : case PROP_TRUST:
146 47 : g_return_if_fail (!self->pv->trust);
147 47 : self->pv->trust = g_value_get_object (value);
148 47 : g_return_if_fail (self->pv->trust);
149 47 : g_object_add_weak_pointer (G_OBJECT (self->pv->trust), (gpointer*)&(self->pv->trust));
150 47 : break;
151 47 : case PROP_TYPE:
152 47 : self->pv->type = g_value_get_ulong (value);
153 47 : break;
154 47 : case PROP_PURPOSE:
155 47 : self->pv->purpose = g_value_dup_string (value);
156 47 : break;
157 47 : case PROP_PEER:
158 47 : self->pv->peer = g_value_dup_string (value);
159 47 : break;
160 0 : default:
161 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
162 0 : break;
163 : }
164 : }
165 :
166 : static void
167 0 : gkm_assertion_get_property (GObject *obj, guint prop_id, GValue *value,
168 : GParamSpec *pspec)
169 : {
170 0 : GkmAssertion *self = GKM_ASSERTION (obj);
171 :
172 0 : switch (prop_id) {
173 0 : case PROP_TRUST:
174 0 : g_value_set_object (value, gkm_assertion_get_trust_object (self));
175 0 : break;
176 0 : case PROP_TYPE:
177 0 : g_value_set_ulong (value, gkm_assertion_get_trust_type (self));
178 0 : break;
179 0 : case PROP_PURPOSE:
180 0 : g_value_set_string (value, gkm_assertion_get_purpose (self));
181 0 : break;
182 0 : case PROP_PEER:
183 0 : g_value_set_string (value, gkm_assertion_get_peer (self));
184 0 : break;
185 0 : default:
186 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
187 0 : break;
188 : }
189 0 : }
190 :
191 : static void
192 2 : gkm_assertion_class_init (GkmAssertionClass *klass)
193 : {
194 2 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
195 2 : GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
196 :
197 2 : gobject_class->constructor = gkm_assertion_constructor;
198 2 : gobject_class->finalize = gkm_assertion_finalize;
199 2 : gobject_class->set_property = gkm_assertion_set_property;
200 2 : gobject_class->get_property = gkm_assertion_get_property;
201 :
202 2 : gkm_class->get_attribute = gkm_assertion_get_attribute;
203 :
204 2 : g_object_class_install_property (gobject_class, PROP_TRUST,
205 : g_param_spec_object ("trust", "Trust", "Trust object this assertion belongs to",
206 : GKM_TYPE_TRUST, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
207 :
208 2 : g_object_class_install_property (gobject_class, PROP_TYPE,
209 : g_param_spec_ulong ("type", "Type", "PKCS#11 assertion type",
210 : 0, G_MAXULONG, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
211 :
212 2 : g_object_class_install_property (gobject_class, PROP_PURPOSE,
213 : g_param_spec_string ("purpose", "Purpose", "The purpose for the trust",
214 : NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
215 :
216 2 : g_object_class_install_property (gobject_class, PROP_PEER,
217 : g_param_spec_string ("peer", "Peer", "Optional peer this assertion applies to",
218 : NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
219 2 : }
220 :
221 : /* -----------------------------------------------------------------------------
222 : * PUBLIC
223 : */
224 :
225 : GkmAssertion*
226 0 : gkm_assertion_new (GkmTrust *trust, gulong type, const gchar *purpose, const gchar *peer)
227 : {
228 0 : return g_object_new (GKM_TYPE_ASSERTION,
229 0 : "module", gkm_object_get_module (GKM_OBJECT (trust)),
230 0 : "manager", gkm_object_get_manager (GKM_OBJECT (trust)),
231 : "trust", trust,
232 : "type", type,
233 : "purpose", purpose,
234 : "peer", peer,
235 : NULL);
236 : }
237 :
238 : const gchar*
239 49 : gkm_assertion_get_purpose (GkmAssertion *self)
240 : {
241 49 : g_return_val_if_fail (GKM_IS_ASSERTION (self), NULL);
242 49 : return self->pv->purpose;
243 : }
244 :
245 : const gchar*
246 49 : gkm_assertion_get_peer (GkmAssertion *self)
247 : {
248 49 : g_return_val_if_fail (GKM_IS_ASSERTION (self), NULL);
249 49 : return self->pv->peer;
250 : }
251 :
252 : gulong
253 26 : gkm_assertion_get_trust_type (GkmAssertion *self)
254 : {
255 26 : g_return_val_if_fail (GKM_IS_ASSERTION (self), 0UL);
256 26 : return self->pv->type;
257 : }
258 :
259 : GkmTrust*
260 5 : gkm_assertion_get_trust_object (GkmAssertion *self)
261 : {
262 5 : g_return_val_if_fail (GKM_IS_ASSERTION (self), NULL);
263 5 : return self->pv->trust;
264 : }
|