| 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 |
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 |
gkm_assertion_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE_PTR attr) |
| 64 |
{ |
| 65 |
GkmAssertion *self = GKM_ASSERTION (base); |
| 66 |
|
| 67 |
switch (attr->type) |
| 68 |
{ |
| 69 |
case CKA_PRIVATE: |
| 70 |
return gkm_attribute_set_bool (attr, CK_FALSE); |
| 71 |
case CKA_CLASS: |
| 72 |
return gkm_attribute_set_ulong (attr, CKO_X_TRUST_ASSERTION); |
| 73 |
case CKA_MODIFIABLE: |
| 74 |
return gkm_attribute_set_bool (attr, CK_FALSE); |
| 75 |
|
| 76 |
/* Various trust flags */ |
| 77 |
case CKA_X_ASSERTION_TYPE: |
| 78 |
return gkm_attribute_set_ulong (attr, self->pv->type); |
| 79 |
case CKA_X_PURPOSE: |
| 80 |
return gkm_attribute_set_string (attr, self->pv->purpose); |
| 81 |
case CKA_X_PEER: |
| 82 |
if (!self->pv->peer) { |
| 83 |
gkm_debug ("CKR_ATTRIBUTE_TYPE_INVALID: no CKA_X_PEER on assertion"); |
| 84 |
return CKR_ATTRIBUTE_TYPE_INVALID; |
| 85 |
} |
| 86 |
return gkm_attribute_set_string (attr, self->pv->peer); |
| 87 |
|
| 88 |
/* Certificate reference values */ |
| 89 |
case CKA_SERIAL_NUMBER: |
| 90 |
case CKA_ISSUER: |
| 91 |
case CKA_X_CERTIFICATE_VALUE: |
| 92 |
return gkm_object_get_attribute (GKM_OBJECT (self->pv->trust), session, attr); |
| 93 |
|
| 94 |
default: |
| 95 |
break; |
| 96 |
}; |
| 97 |
|
| 98 |
return GKM_OBJECT_CLASS (gkm_assertion_parent_class)->get_attribute (base, session, attr); |
| 99 |
} |
| 100 |
|
| 101 |
static GObject* |
| 102 |
gkm_assertion_constructor (GType type, guint n_props, GObjectConstructParam *props) |
| 103 |
{ |
| 104 |
GkmAssertion *self = GKM_ASSERTION (G_OBJECT_CLASS (gkm_assertion_parent_class)->constructor(type, n_props, props)); |
| 105 |
|
| 106 |
g_return_val_if_fail (self, NULL); |
| 107 |
g_return_val_if_fail (self->pv->purpose, NULL); |
| 108 |
g_return_val_if_fail (self->pv->type, NULL); |
| 109 |
|
| 110 |
return G_OBJECT (self); |
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
gkm_assertion_init (GkmAssertion *self) |
| 115 |
{ |
| 116 |
self->pv = gkm_assertion_get_instance_private (self); |
| 117 |
} |
| 118 |
|
| 119 |
static void |
| 120 |
gkm_assertion_finalize (GObject *obj) |
| 121 |
{ |
| 122 |
GkmAssertion *self = GKM_ASSERTION (obj); |
| 123 |
|
| 124 |
if (self->pv->trust) |
| 125 |
g_object_remove_weak_pointer (G_OBJECT (self->pv->trust), (gpointer*)&(self->pv->trust)); |
| 126 |
self->pv->trust = NULL; |
| 127 |
|
| 128 |
g_free (self->pv->purpose); |
| 129 |
self->pv->purpose = NULL; |
| 130 |
|
| 131 |
g_free (self->pv->peer); |
| 132 |
self->pv->peer = NULL; |
| 133 |
|
| 134 |
G_OBJECT_CLASS (gkm_assertion_parent_class)->finalize (obj); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
static void |
| 139 |
gkm_assertion_set_property (GObject *obj, guint prop_id, const GValue *value, |
| 140 |
GParamSpec *pspec) |
| 141 |
{ |
| 142 |
GkmAssertion *self = GKM_ASSERTION (obj); |
| 143 |
|
| 144 |
switch (prop_id) { |
| 145 |
case PROP_TRUST: |
| 146 |
g_return_if_fail (!self->pv->trust); |
| 147 |
self->pv->trust = g_value_get_object (value); |
| 148 |
g_return_if_fail (self->pv->trust); |
| 149 |
g_object_add_weak_pointer (G_OBJECT (self->pv->trust), (gpointer*)&(self->pv->trust)); |
| 150 |
break; |
| 151 |
case PROP_TYPE: |
| 152 |
self->pv->type = g_value_get_ulong (value); |
| 153 |
break; |
| 154 |
case PROP_PURPOSE: |
| 155 |
self->pv->purpose = g_value_dup_string (value); |
| 156 |
break; |
| 157 |
case PROP_PEER: |
| 158 |
self->pv->peer = g_value_dup_string (value); |
| 159 |
break; |
| 160 |
default: |
| 161 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
gkm_assertion_get_property (GObject *obj, guint prop_id, GValue *value, |
| 168 |
GParamSpec *pspec) |
| 169 |
{ |
| 170 |
GkmAssertion *self = GKM_ASSERTION (obj); |
| 171 |
|
| 172 |
switch (prop_id) { |
| 173 |
case PROP_TRUST: |
| 174 |
g_value_set_object (value, gkm_assertion_get_trust_object (self)); |
| 175 |
break; |
| 176 |
case PROP_TYPE: |
| 177 |
g_value_set_ulong (value, gkm_assertion_get_trust_type (self)); |
| 178 |
break; |
| 179 |
case PROP_PURPOSE: |
| 180 |
g_value_set_string (value, gkm_assertion_get_purpose (self)); |
| 181 |
break; |
| 182 |
case PROP_PEER: |
| 183 |
g_value_set_string (value, gkm_assertion_get_peer (self)); |
| 184 |
break; |
| 185 |
default: |
| 186 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
gkm_assertion_class_init (GkmAssertionClass *klass) |
| 193 |
{ |
| 194 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 195 |
GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass); |
| 196 |
|
| 197 |
gobject_class->constructor = gkm_assertion_constructor; |
| 198 |
gobject_class->finalize = gkm_assertion_finalize; |
| 199 |
gobject_class->set_property = gkm_assertion_set_property; |
| 200 |
gobject_class->get_property = gkm_assertion_get_property; |
| 201 |
|
| 202 |
gkm_class->get_attribute = gkm_assertion_get_attribute; |
| 203 |
|
| 204 |
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 |
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 |
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 |
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 |
} |
| 220 |
|
| 221 |
/* ----------------------------------------------------------------------------- |
| 222 |
* PUBLIC |
| 223 |
*/ |
| 224 |
|
| 225 |
GkmAssertion* |
| 226 |
gkm_assertion_new (GkmTrust *trust, gulong type, const gchar *purpose, const gchar *peer) |
| 227 |
{ |
| 228 |
return g_object_new (GKM_TYPE_ASSERTION, |
| 229 |
"module", gkm_object_get_module (GKM_OBJECT (trust)), |
| 230 |
"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 |
gkm_assertion_get_purpose (GkmAssertion *self) |
| 240 |
{ |
| 241 |
g_return_val_if_fail (GKM_IS_ASSERTION (self), NULL); |
| 242 |
return self->pv->purpose; |
| 243 |
} |
| 244 |
|
| 245 |
const gchar* |
| 246 |
gkm_assertion_get_peer (GkmAssertion *self) |
| 247 |
{ |
| 248 |
g_return_val_if_fail (GKM_IS_ASSERTION (self), NULL); |
| 249 |
return self->pv->peer; |
| 250 |
} |
| 251 |
|
| 252 |
gulong |
| 253 |
gkm_assertion_get_trust_type (GkmAssertion *self) |
| 254 |
{ |
| 255 |
g_return_val_if_fail (GKM_IS_ASSERTION (self), 0UL); |
| 256 |
return self->pv->type; |
| 257 |
} |
| 258 |
|
| 259 |
GkmTrust* |
| 260 |
gkm_assertion_get_trust_object (GkmAssertion *self) |
| 261 |
{ |
| 262 |
g_return_val_if_fail (GKM_IS_ASSERTION (self), NULL); |
| 263 |
return self->pv->trust; |
| 264 |
} |