Debian Codegraph @

pkcs11/gkm/gkm-certificate.c

raw
1 /*
2 * gnome-keyring
3 *
4 * Copyright (C) 2008 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-attributes.h"
24 #include "gkm-certificate.h"
25 #include "gkm-certificate-key.h"
26 #include "gkm-crypto.h"
27 #include "gkm-data-asn1.h"
28 #include "gkm-data-der.h"
29 #define DEBUG_FLAG GKM_DEBUG_STORAGE
30 #include "gkm-debug.h"
31 #include "gkm-factory.h"
32 #include "gkm-sexp-key.h"
33 #include "gkm-manager.h"
34 #include "gkm-session.h"
35 #include "gkm-sexp.h"
36 #include "gkm-serializable.h"
37 #include "gkm-transaction.h"
38 #include "gkm-util.h"
39
40 #include "egg/egg-dn.h"
41 #include "egg/egg-asn1x.h"
42
43 #include "pkcs11/pkcs11.h"
44 #include "pkcs11/pkcs11i.h"
45
46 #include <glib/gi18n.h>
47
48 enum {
49 PROP_0,
50 PROP_LABEL,
51 PROP_PUBLIC_KEY
52 };
53
54 struct _GkmCertificatePrivate {
55 GkmCertificateKey *key;
56 GNode *asn1;
57 GBytes *der;
58 gchar *label;
59 };
60
61 static GQuark OID_BASIC_CONSTRAINTS;
62 static GQuark OID_ENHANCED_USAGE;
63
64 static void gkm_certificate_serializable (GkmSerializableIface *iface);
65
66 G_DEFINE_TYPE_EXTENDED (GkmCertificate, gkm_certificate, GKM_TYPE_OBJECT, 0,
67 G_ADD_PRIVATE (GkmCertificate)
68 G_IMPLEMENT_INTERFACE (GKM_TYPE_SERIALIZABLE, gkm_certificate_serializable));
69
70 /* -----------------------------------------------------------------------------
71 * INTERNAL
72 */
73
74 static void
75 init_quarks (void)
76 {
77 static gsize quarks_inited = 0;
78
79 if (g_once_init_enter (&quarks_inited)) {
80 #define QUARK(name, value) \
81 name = g_quark_from_static_string(value)
82
83 QUARK (OID_BASIC_CONSTRAINTS, "2.5.29.19");
84 QUARK (OID_ENHANCED_USAGE, "2.5.29.37");
85
86 #undef QUARK
87
88 g_once_init_leave (&quarks_inited, 1);
89 }
90 }
91
92 static gint
93 find_certificate_extension (GkmCertificate *self, GQuark oid)
94 {
95 GQuark exoid;
96 GNode *node;
97 guint index;
98
99 g_assert (oid);
100 g_assert (GKM_IS_CERTIFICATE (self));
101 g_assert (self->pv->asn1);
102
103 for (index = 1; TRUE; ++index) {
104
105 /* Make sure it is present */
106 node = egg_asn1x_node (self->pv->asn1, "tbsCertificate", "extensions", index, NULL);
107 if (node == NULL)
108 break;
109
110 /* See if it's the same */
111 exoid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (node, "extnID", NULL));
112
113 if(exoid == oid)
114 return index;
115 }
116
117 return 0;
118 }
119
120 static GkmObject*
121 factory_create_certificate (GkmSession *session, GkmTransaction *transaction,
122 CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs)
123 {
124 CK_ATTRIBUTE_PTR attr;
125 GkmCertificate *cert;
126 GBytes *bytes;
127 gboolean ret;
128
129 g_return_val_if_fail (GKM_IS_TRANSACTION (transaction), NULL);
130 g_return_val_if_fail (attrs || !n_attrs, NULL);
131
132 /* Dig out the value */
133 attr = gkm_attributes_find (attrs, n_attrs, CKA_VALUE);
134 if (attr == NULL) {
135 gkm_transaction_fail (transaction, CKR_TEMPLATE_INCOMPLETE);
136 return NULL;
137 }
138
139 cert = g_object_new (GKM_TYPE_CERTIFICATE,
140 "module", gkm_session_get_module (session),
141 "manager", gkm_manager_for_template (attrs, n_attrs, session),
142 NULL);
143
144 /* Load the certificate from the data specified */
145 bytes = g_bytes_new (attr->pValue, attr->ulValueLen);
146 ret = gkm_serializable_load (GKM_SERIALIZABLE (cert), NULL, bytes);
147 g_bytes_unref (bytes);
148
149 if(!ret) {
150 gkm_transaction_fail (transaction, CKR_ATTRIBUTE_VALUE_INVALID);
151 g_object_unref (cert);
152 return NULL;
153 }
154
155 /* We calculate these attributes automatically */
156 gkm_attributes_consume (attrs, n_attrs,
157 CKA_VALUE, CKA_SUBJECT, CKA_SERIAL_NUMBER, CKA_ID,
158 G_MAXULONG);
159
160 gkm_session_complete_object_creation (session, transaction, GKM_OBJECT (cert),
161 TRUE, attrs, n_attrs);
162 return GKM_OBJECT (cert);
163 }
164
165 /* -----------------------------------------------------------------------------
166 * KEY
167 */
168
169 static CK_RV
170 gkm_certificate_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE* attr)
171 {
172 GkmCertificate *self = GKM_CERTIFICATE (base);
173 CK_ULONG category;
174 GBytes *cdata;
175 guchar *data;
176 gsize n_data;
177 time_t when;
178 CK_RV rv;
179
180 switch (attr->type) {
181
182 case CKA_CLASS:
183 return gkm_attribute_set_ulong (attr, CKO_CERTIFICATE);
184
185 case CKA_PRIVATE:
186 return gkm_attribute_set_bool (attr, FALSE);
187
188 case CKA_LABEL:
189 return gkm_attribute_set_string (attr, gkm_certificate_get_label (self));
190
191 case CKA_CERTIFICATE_TYPE:
192 return gkm_attribute_set_ulong (attr, CKC_X_509);
193
194 case CKA_TRUSTED:
195 return gkm_attribute_set_bool (attr, FALSE);
196
197 case CKA_CERTIFICATE_CATEGORY:
198 if (!gkm_certificate_calc_category (self, session, &category))
199 return CKR_FUNCTION_FAILED;
200 return gkm_attribute_set_ulong (attr, category);
201
202 case CKA_CHECK_VALUE:
203 g_return_val_if_fail (self->pv->der != NULL, CKR_GENERAL_ERROR);
204 n_data = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
205 g_return_val_if_fail (n_data && n_data > 3, CKR_GENERAL_ERROR);
206 data = g_new0 (guchar, n_data);
207 gcry_md_hash_buffer (GCRY_MD_SHA1, data,
208 g_bytes_get_data (self->pv->der, NULL),
209 g_bytes_get_size (self->pv->der));
210 rv = gkm_attribute_set_data (attr, data, 3);
211 g_free (data);
212 return rv;
213
214 case CKA_START_DATE:
215 case CKA_END_DATE:
216 g_return_val_if_fail (self->pv->asn1, CKR_GENERAL_ERROR);
217 when = egg_asn1x_get_time_as_long (egg_asn1x_node (self->pv->asn1,
218 "tbsCertificate", "validity",
219 attr->type == CKA_START_DATE ? "notBefore" : "notAfter",
220 NULL));
221 if (when < 0)
222 return CKR_FUNCTION_FAILED;
223 return gkm_attribute_set_date (attr, when);
224
225 case CKA_SUBJECT:
226 g_return_val_if_fail (self->pv->asn1, CKR_GENERAL_ERROR);
227 cdata = egg_asn1x_get_element_raw (egg_asn1x_node (self->pv->asn1, "tbsCertificate", "subject", NULL));
228 g_return_val_if_fail (cdata, CKR_GENERAL_ERROR);
229 rv = gkm_attribute_set_bytes (attr, cdata);
230 g_bytes_unref (cdata);
231 return rv;
232
233 case CKA_ID:
234 if (!self->pv->key)
235 return gkm_attribute_set_data (attr, NULL, 0);
236 return gkm_object_get_attribute (GKM_OBJECT (self->pv->key), session, attr);
237
238 case CKA_ISSUER:
239 g_return_val_if_fail (self->pv->asn1, CKR_GENERAL_ERROR);
240 cdata = egg_asn1x_get_element_raw (egg_asn1x_node (self->pv->asn1, "tbsCertificate", "issuer", NULL));
241 g_return_val_if_fail (cdata, CKR_GENERAL_ERROR);
242 rv = gkm_attribute_set_bytes (attr, cdata);
243 g_bytes_unref (cdata);
244 return rv;
245
246 case CKA_SERIAL_NUMBER:
247 g_return_val_if_fail (self->pv->asn1, CKR_GENERAL_ERROR);
248 cdata = egg_asn1x_get_element_raw (egg_asn1x_node (self->pv->asn1, "tbsCertificate", "serialNumber", NULL));
249 g_return_val_if_fail (cdata, CKR_GENERAL_ERROR);
250 rv = gkm_attribute_set_bytes (attr, cdata);
251 g_bytes_unref (cdata);
252 return rv;
253
254 case CKA_VALUE:
255 g_return_val_if_fail (self->pv->der != NULL, CKR_GENERAL_ERROR);
256 return gkm_attribute_set_bytes (attr, self->pv->der);
257
258 /* These are only used for strange online certificates which we don't support */
259 case CKA_URL:
260 case CKA_HASH_OF_SUBJECT_PUBLIC_KEY:
261 case CKA_HASH_OF_ISSUER_PUBLIC_KEY:
262 return gkm_attribute_set_data (attr, "", 0);
263
264 /* What in the world is this doing in the spec? */
265 case CKA_JAVA_MIDP_SECURITY_DOMAIN:
266 return gkm_attribute_set_ulong (attr, 0); /* 0 = unspecified */
267 };
268
269 return GKM_OBJECT_CLASS (gkm_certificate_parent_class)->get_attribute (base, session, attr);
270 }
271
272 static GObject*
273 gkm_certificate_constructor (GType type, guint n_props, GObjectConstructParam *props)
274 {
275 GkmCertificate *self = GKM_CERTIFICATE (G_OBJECT_CLASS (gkm_certificate_parent_class)->constructor(type, n_props, props));
276 g_return_val_if_fail (self, NULL);
277
278
279 return G_OBJECT (self);
280 }
281
282 static void
283 gkm_certificate_init (GkmCertificate *self)
284 {
285 self->pv = gkm_certificate_get_instance_private (self);
286
287 }
288
289 static void
290 gkm_certificate_dispose (GObject *obj)
291 {
292 GkmCertificate *self = GKM_CERTIFICATE (obj);
293
294 if (self->pv->key)
295 g_object_unref (self->pv->key);
296 self->pv->key = NULL;
297
298 G_OBJECT_CLASS (gkm_certificate_parent_class)->dispose (obj);
299 }
300
301 static void
302 gkm_certificate_finalize (GObject *obj)
303 {
304 GkmCertificate *self = GKM_CERTIFICATE (obj);
305
306 g_assert (!self->pv->key);
307 if (self->pv->der)
308 g_bytes_unref (self->pv->der);
309 g_free (self->pv->label);
310 egg_asn1x_destroy (self->pv->asn1);
311
312 G_OBJECT_CLASS (gkm_certificate_parent_class)->finalize (obj);
313 }
314
315 static void
316 gkm_certificate_set_property (GObject *obj, guint prop_id, const GValue *value,
317 GParamSpec *pspec)
318 {
319 GkmCertificate *self = GKM_CERTIFICATE (obj);
320
321 switch (prop_id) {
322 case PROP_LABEL:
323 gkm_certificate_set_label (self, g_value_get_string (value));
324 break;
325 default:
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
327 break;
328 }
329 }
330
331 static void
332 gkm_certificate_get_property (GObject *obj, guint prop_id, GValue *value,
333 GParamSpec *pspec)
334 {
335 GkmCertificate *self = GKM_CERTIFICATE (obj);
336
337 switch (prop_id) {
338 case PROP_LABEL:
339 g_value_set_string (value, gkm_certificate_get_label (self));
340 break;
341 case PROP_PUBLIC_KEY:
342 g_value_set_object (value, gkm_certificate_get_public_key (self));
343 break;
344 default:
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
346 break;
347 }
348 }
349
350 static void
351 gkm_certificate_class_init (GkmCertificateClass *klass)
352 {
353 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
354 GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
355
356 gobject_class->constructor = gkm_certificate_constructor;
357 gobject_class->dispose = gkm_certificate_dispose;
358 gobject_class->finalize = gkm_certificate_finalize;
359 gobject_class->set_property = gkm_certificate_set_property;
360 gobject_class->get_property = gkm_certificate_get_property;
361
362 gkm_class->get_attribute = gkm_certificate_real_get_attribute;
363
364 g_object_class_install_property (gobject_class, PROP_PUBLIC_KEY,
365 g_param_spec_object ("public-key", "Public Key", "Public key contained in certificate",
366 GKM_TYPE_CERTIFICATE_KEY, G_PARAM_READABLE));
367
368 g_object_class_install_property (gobject_class, PROP_PUBLIC_KEY,
369 g_param_spec_string ("label", "Label", "Label of the certificate",
370 "", G_PARAM_READWRITE));
371
372 init_quarks ();
373 }
374
375 static gboolean
376 gkm_certificate_real_load (GkmSerializable *base,
377 GkmSecret *login,
378 GBytes *data)
379 {
380 GkmCertificate *self = GKM_CERTIFICATE (base);
381 GNode *asn1 = NULL;
382 GkmDataResult res;
383 GBytes *keydata;
384 gcry_sexp_t sexp;
385 GkmSexp *wrapper;
386
387 if (g_bytes_get_size (data) == 0) {
388 gkm_debug ("cannot load empty certificate file");
389 return FALSE;
390 }
391
392 /* Parse the ASN1 data */
393 res = gkm_data_der_read_certificate (data, &asn1);
394 if (res != GKM_DATA_SUCCESS) {
395 gkm_debug ("couldn't parse certificate data");
396 return FALSE;
397 }
398
399 /* Generate a raw public key from our certificate */
400 keydata = egg_asn1x_encode (egg_asn1x_node (asn1, "tbsCertificate", "subjectPublicKeyInfo", NULL), NULL);
401 g_return_val_if_fail (keydata, FALSE);
402
403 /* Now create us a nice public key with that identifier */
404 res = gkm_data_der_read_public_key_info (keydata, &sexp);
405 g_bytes_unref (keydata);
406
407 switch (res) {
408
409 /* Create ourselves a public key with that */
410 case GKM_DATA_SUCCESS:
411 wrapper = gkm_sexp_new (sexp);
412 if (!self->pv->key)
413 self->pv->key = gkm_certificate_key_new (gkm_object_get_module (GKM_OBJECT (self)),
414 gkm_object_get_manager (GKM_OBJECT (self)),
415 self);
416 gkm_sexp_key_set_base (GKM_SEXP_KEY (self->pv->key), wrapper);
417 gkm_sexp_unref (wrapper);
418 break;
419
420 /* Unknown type of public key for this certificate, just ignore */
421 case GKM_DATA_UNRECOGNIZED:
422 if (self->pv->key)
423 g_object_unref (self->pv->key);
424 self->pv->key = NULL;
425 break;
426
427 /* Bad key, drop certificate */
428 case GKM_DATA_FAILURE:
429 case GKM_DATA_LOCKED:
430 g_warning ("couldn't parse certificate key data");
431 egg_asn1x_destroy (asn1);
432 return FALSE;
433
434 default:
435 g_assert_not_reached ();
436 break;
437 }
438
439 g_bytes_ref (data);
440 if (self->pv->der)
441 g_bytes_unref (self->pv->der);
442 self->pv->der = data;
443
444 egg_asn1x_destroy (self->pv->asn1);
445 self->pv->asn1 = asn1;
446
447 return TRUE;
448 }
449
450 static GBytes *
451 gkm_certificate_real_save (GkmSerializable *base,
452 GkmSecret *login)
453 {
454 GkmCertificate *self = GKM_CERTIFICATE (base);
455
456 g_return_val_if_fail (GKM_IS_CERTIFICATE (self), FALSE);
457
458 return g_bytes_ref (self->pv->der);
459 }
460
461 static void
462 gkm_certificate_serializable (GkmSerializableIface *iface)
463 {
464 iface->extension = ".cer";
465 iface->load = gkm_certificate_real_load;
466 iface->save = gkm_certificate_real_save;
467 }
468
469 /* -----------------------------------------------------------------------------
470 * PUBLIC
471 */
472
473 gboolean
474 gkm_certificate_calc_category (GkmCertificate *self, GkmSession *session, CK_ULONG* category)
475 {
476 GBytes *extension;
477 GkmManager *manager;
478 GkmDataResult res;
479 gboolean is_ca;
480 GkmObject *object;
481
482 g_return_val_if_fail (GKM_IS_CERTIFICATE (self), CKR_GENERAL_ERROR);
483 g_return_val_if_fail (category, CKR_GENERAL_ERROR);
484
485 /* First see if we have a private key for this certificate */
486 manager = gkm_object_get_manager (GKM_OBJECT (self));
487 if (manager != NULL) {
488 object = gkm_manager_find_related (manager, session, CKO_PRIVATE_KEY, GKM_OBJECT (self));
489 if (object != NULL) {
490 *category = 1; /* token user */
491 return TRUE;
492 }
493 }
494
495 /* Read in the Basic Constraints section */
496 extension = gkm_certificate_get_extension (self, OID_BASIC_CONSTRAINTS, NULL);
497 if (extension != NULL) {
498 res = gkm_data_der_read_basic_constraints (extension, &is_ca, NULL);
499
500 if (res != GKM_DATA_SUCCESS)
501 return FALSE;
502
503 if (is_ca)
504 *category = 2; /* authority */
505 else
506 *category = 3; /* other entity */
507
508 } else {
509 *category = 0; /* unspecified */
510 }
511
512 return TRUE;
513 }
514
515 GkmCertificateKey*
516 gkm_certificate_get_public_key (GkmCertificate *self)
517 {
518 g_return_val_if_fail (GKM_IS_CERTIFICATE (self), NULL);
519 return self->pv->key;
520 }
521
522 GBytes *
523 gkm_certificate_get_extension (GkmCertificate *self, GQuark oid,
524 gboolean *critical)
525 {
526 guchar *val;
527 gsize n_val;
528 gint index;
529
530 g_return_val_if_fail (GKM_IS_CERTIFICATE (self), NULL);
531 g_return_val_if_fail (self->pv->asn1, NULL);
532 g_return_val_if_fail (oid, NULL);
533
534 index = find_certificate_extension (self, oid);
535 if (index <= 0)
536 return NULL;
537
538 /* Read the critical status */
539 if (critical) {
540 val = egg_asn1x_get_string_as_raw (egg_asn1x_node (self->pv->asn1, "tbsCertificate",
541 "extensions", index, "critical", NULL), NULL, &n_val);
542
543 /*
544 * We're pretty liberal in what we accept as critical. The goal
545 * here is not to accidentally mark as non-critical what some
546 * other x509 implementation meant to say critical.
547 */
548 if (!val || n_val < 1 || g_ascii_toupper (val[0]) != 'T')
549 *critical = FALSE;
550 else
551 *critical = TRUE;
552 g_free (val);
553 }
554
555 /* And the extension value */
556 return egg_asn1x_get_string_as_bytes (egg_asn1x_node (self->pv->asn1, "tbsCertificate",
557 "extensions", index, "extnValue", NULL));
558 }
559
560 const gchar*
561 gkm_certificate_get_label (GkmCertificate *self)
562 {
563 gchar *label;
564
565 g_return_val_if_fail (GKM_IS_CERTIFICATE (self), "");
566
567 if (!self->pv->label) {
568 g_return_val_if_fail (self->pv->asn1, "");
569
570 /* Look for the CN in the certificate */
571 label = egg_dn_read_part (egg_asn1x_node (self->pv->asn1, "tbsCertificate", "subject", "rdnSequence", NULL), "cn");
572
573 /* Otherwise use the full DN */
574 if (!label)
575 label = egg_dn_read (egg_asn1x_node (self->pv->asn1, "tbsCertificate", "subject", "rdnSequence", NULL));
576
577 if (!label)
578 label = g_strdup (_("Unnamed Certificate"));
579
580 self->pv->label = label;
581
582 }
583
584 return self->pv->label;
585 }
586
587 void
588 gkm_certificate_set_label (GkmCertificate *self, const gchar *label)
589 {
590 g_return_if_fail (GKM_IS_CERTIFICATE (self));
591 g_free (self->pv->label);
592 self->pv->label = g_strdup (label);
593 g_object_notify (G_OBJECT (self), "label");
594 }
595
596 guchar*
597 gkm_certificate_hash (GkmCertificate *self, int hash_algo, gsize *n_hash)
598 {
599 guchar *hash;
600
601 g_return_val_if_fail (GKM_IS_CERTIFICATE (self), NULL);
602 g_return_val_if_fail (self->pv->der != NULL, NULL);
603 g_return_val_if_fail (n_hash, NULL);
604
605 *n_hash = gcry_md_get_algo_dlen (hash_algo);
606 g_return_val_if_fail (*n_hash > 0, NULL);
607
608 hash = g_malloc0 (*n_hash);
609 gcry_md_hash_buffer (hash_algo, hash, g_bytes_get_data (self->pv->der, NULL),
610 g_bytes_get_size (self->pv->der));
611
612 return hash;
613 }
614
615 gconstpointer
616 gkm_certificate_der_data (GkmCertificate *self, gsize *n_data)
617 {
618 g_return_val_if_fail (GKM_IS_CERTIFICATE (self), NULL);
619 g_return_val_if_fail (self->pv->der != NULL, NULL);
620 g_return_val_if_fail (n_data, NULL);
621
622 *n_data = g_bytes_get_size (self->pv->der);
623 return g_bytes_get_data (self->pv->der, NULL);
624 }
625
626 GkmFactory*
627 gkm_certificate_get_factory (void)
628 {
629 static CK_OBJECT_CLASS klass = CKO_CERTIFICATE;
630 static CK_CERTIFICATE_TYPE type = CKC_X_509;
631
632 static CK_ATTRIBUTE attributes[] = {
633 { CKA_CLASS, &klass, sizeof (klass) },
634 { CKA_CERTIFICATE_TYPE, &type, sizeof (type) },
635 };
636
637 static GkmFactory factory = {
638 attributes,
639 G_N_ELEMENTS (attributes),
640 factory_create_certificate
641 };
642
643 return &factory;
644 }