~noskcaj/ubuntu/vivid/gnome-keyring/3.15.90

« back to all changes in this revision

Viewing changes to gcr/tests/test-certificate-chain.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-14 22:13:02 UTC
  • mfrom: (1.3.1)
  • mto: (80.2.8 experimental) (1.1.77)
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: package-import@ubuntu.com-20120514221302-0l3gjmqpe6xopond
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
#include "config.h"
3
 
#include "test-suite.h"
4
 
 
5
 
#include "egg/egg-asn1x.h"
6
 
#include "egg/egg-asn1-defs.h"
7
 
 
8
 
#include "gcr/gcr.h"
9
 
#include "gcr/gcr-internal.h"
10
 
 
11
 
#include "gck/gck-mock.h"
12
 
#include "gck/gck-test.h"
13
 
 
14
 
#include "pkcs11/pkcs11.h"
15
 
#include "pkcs11/pkcs11x.h"
16
 
 
17
 
#include <glib.h>
18
 
 
19
 
#include <string.h>
20
 
 
21
 
/* ---------------------------------------------------------------------------
22
 
 * A Mock certificate that checks that it's always called on the
23
 
 * same thread. A GcrCertificate implemented on top of a non-thread-safe
24
 
 * crypto library would require this behavior.
25
 
 */
26
 
 
27
 
GType               mock_certificate_get_type               (void);
28
 
 
29
 
#define MOCK_CERTIFICATE(obj) \
30
 
        (G_TYPE_CHECK_INSTANCE_CAST ((obj), mock_certificate_get_type (), MockCertificate))
31
 
 
32
 
typedef struct _MockCertificate {
33
 
        GObject parent;
34
 
        GThread *created_on;
35
 
        gpointer data;
36
 
        gsize n_data;
37
 
} MockCertificate;
38
 
 
39
 
typedef struct _MockCertificateClass {
40
 
        GObjectClass parent_class;
41
 
} MockCertificateClass;
42
 
 
43
 
static void mock_certificate_iface (GcrCertificateIface *iface);
44
 
G_DEFINE_TYPE_WITH_CODE (MockCertificate, mock_certificate, G_TYPE_OBJECT,
45
 
                         G_IMPLEMENT_INTERFACE (GCR_TYPE_CERTIFICATE, mock_certificate_iface));
46
 
 
47
 
static void
48
 
mock_certificate_init (MockCertificate *self)
49
 
{
50
 
        self->created_on = g_thread_self ();
51
 
}
52
 
 
53
 
static void
54
 
mock_certificate_finalize (GObject *obj)
55
 
{
56
 
        MockCertificate *self = MOCK_CERTIFICATE (obj);
57
 
        g_assert (self->created_on == g_thread_self ());
58
 
        g_free (self->data);
59
 
        G_OBJECT_CLASS (mock_certificate_parent_class)->finalize (obj);
60
 
}
61
 
 
62
 
static void
63
 
mock_certificate_class_init (MockCertificateClass *klass)
64
 
{
65
 
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
66
 
        gobject_class->finalize = mock_certificate_finalize;
67
 
}
68
 
 
69
 
static gconstpointer
70
 
mock_certificate_real_get_der_data (GcrCertificate *base, gsize *n_data)
71
 
{
72
 
        MockCertificate *self = MOCK_CERTIFICATE (base);
73
 
        g_assert (self->created_on == g_thread_self ());
74
 
        *n_data = self->n_data;
75
 
        return self->data;
76
 
}
77
 
 
78
 
static void
79
 
mock_certificate_iface (GcrCertificateIface *iface)
80
 
{
81
 
        iface->get_der_data = (gpointer)mock_certificate_real_get_der_data;
82
 
}
83
 
 
84
 
static GcrCertificate*
85
 
mock_certificate_new (gconstpointer data, gsize n_data)
86
 
{
87
 
        MockCertificate *self = g_object_new (mock_certificate_get_type (), NULL);
88
 
        self->data = g_memdup (data, n_data);
89
 
        self->n_data = n_data;
90
 
        g_assert (self->created_on == g_thread_self ());
91
 
        return GCR_CERTIFICATE (self);
92
 
}
93
 
 
94
 
/* ----------------------------------------------------------------------------
95
 
 * TESTS
96
 
 */
97
 
 
98
 
static GcrCertificate *cert_self = NULL;
99
 
static GcrCertificate *cert_ca = NULL;
100
 
static GcrCertificate *cert_signed = NULL;
101
 
static CK_FUNCTION_LIST funcs;
102
 
 
103
 
TESTING_SETUP (certificate_chain)
104
 
{
105
 
        GList *modules = NULL;
106
 
        CK_FUNCTION_LIST_PTR f;
107
 
        guchar *contents;
108
 
        gsize n_contents;
109
 
        const gchar *uris[2];
110
 
        CK_RV rv;
111
 
        GckModule *module;
112
 
 
113
 
        rv = gck_mock_C_GetFunctionList (&f);
114
 
        gck_assert_cmprv (rv, ==, CKR_OK);
115
 
        memcpy (&funcs, f, sizeof (funcs));
116
 
 
117
 
        /* Open a session */
118
 
        rv = (funcs.C_Initialize) (NULL);
119
 
        gck_assert_cmprv (rv, ==, CKR_OK);
120
 
 
121
 
        g_assert (!modules);
122
 
        module = gck_module_new (&funcs, 0);
123
 
        modules = g_list_prepend (modules, module);
124
 
        gcr_pkcs11_set_modules (modules);
125
 
        uris[0] = GCK_MOCK_SLOT_ONE_URI;
126
 
        uris[1] = NULL;
127
 
        gcr_pkcs11_set_trust_lookup_uris (uris);
128
 
        gcr_pkcs11_set_trust_store_uri (GCK_MOCK_SLOT_ONE_URI);
129
 
        gck_list_unref_free (modules);
130
 
 
131
 
        /* A self-signed certificate */
132
 
        contents = testing_data_read ("der-certificate.crt", &n_contents);
133
 
        cert_self = gcr_simple_certificate_new (contents, n_contents);
134
 
        g_free (contents);
135
 
 
136
 
        /* A signed certificate */
137
 
        contents = testing_data_read ("dhansak-collabora.cer", &n_contents);
138
 
        cert_signed = mock_certificate_new (contents, n_contents);
139
 
        g_free (contents);
140
 
 
141
 
        /* The signer for the above certificate */
142
 
        contents = testing_data_read ("collabora-ca.cer", &n_contents);
143
 
        cert_ca = mock_certificate_new (contents, n_contents);
144
 
        g_free (contents);
145
 
}
146
 
 
147
 
static void
148
 
add_certificate_to_module (GcrCertificate *certificate)
149
 
{
150
 
        GckAttributes *attrs;
151
 
        gconstpointer data;
152
 
        gsize n_data, n_subject;
153
 
        gpointer subject;
154
 
 
155
 
        data = gcr_certificate_get_der_data (certificate, &n_data);
156
 
        g_assert (data);
157
 
 
158
 
        subject = gcr_certificate_get_subject_raw (certificate, &n_subject);
159
 
        g_assert (subject);
160
 
 
161
 
        /* Add a certificate to the module */
162
 
        attrs = gck_attributes_new ();
163
 
        gck_attributes_add_data (attrs, CKA_VALUE, data, n_data);
164
 
        gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_CERTIFICATE);
165
 
        gck_attributes_add_ulong (attrs, CKA_CERTIFICATE_TYPE, CKC_X_509);
166
 
        gck_attributes_add_data (attrs, CKA_SUBJECT, subject, n_subject);
167
 
        gck_mock_module_take_object (attrs);
168
 
 
169
 
        g_free (subject);
170
 
}
171
 
 
172
 
static void
173
 
add_anchor_to_module (GcrCertificate *certificate, const gchar *purpose)
174
 
{
175
 
        GckAttributes *attrs;
176
 
        gconstpointer data;
177
 
        gsize n_data;
178
 
 
179
 
        data = gcr_certificate_get_der_data (certificate, &n_data);
180
 
        g_assert (data);
181
 
 
182
 
        /* And add a pinned certificate for the signed certificate */
183
 
        attrs = gck_attributes_new ();
184
 
        gck_attributes_add_data (attrs, CKA_X_CERTIFICATE_VALUE, data, n_data);
185
 
        gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_X_TRUST_ASSERTION);
186
 
        gck_attributes_add_ulong (attrs, CKA_X_ASSERTION_TYPE, CKT_X_ANCHORED_CERTIFICATE);
187
 
        gck_attributes_add_string (attrs, CKA_X_PURPOSE, purpose);
188
 
        gck_mock_module_take_object (attrs);
189
 
}
190
 
 
191
 
static void
192
 
add_pinned_to_module (GcrCertificate *certificate, const gchar *purpose, const gchar *host)
193
 
{
194
 
        GckAttributes *attrs;
195
 
        gconstpointer data;
196
 
        gsize n_data;
197
 
 
198
 
        data = gcr_certificate_get_der_data (certificate, &n_data);
199
 
        g_assert (data);
200
 
 
201
 
        /* And add a pinned certificate for the signed certificate */
202
 
        attrs = gck_attributes_new ();
203
 
        gck_attributes_add_data (attrs, CKA_X_CERTIFICATE_VALUE, data, n_data);
204
 
        gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_X_TRUST_ASSERTION);
205
 
        gck_attributes_add_ulong (attrs, CKA_X_ASSERTION_TYPE, CKT_X_PINNED_CERTIFICATE);
206
 
        gck_attributes_add_string (attrs, CKA_X_PURPOSE, purpose);
207
 
        gck_attributes_add_string (attrs, CKA_X_PEER, host);
208
 
        gck_mock_module_take_object (attrs);
209
 
}
210
 
 
211
 
TESTING_TEARDOWN (certificate_chain)
212
 
{
213
 
        CK_RV rv;
214
 
 
215
 
        g_object_unref (cert_self);
216
 
        cert_self = NULL;
217
 
 
218
 
        g_object_unref (cert_signed);
219
 
        cert_signed = NULL;
220
 
 
221
 
        g_object_unref (cert_ca);
222
 
        cert_ca = NULL;
223
 
 
224
 
        rv = (funcs.C_Finalize) (NULL);
225
 
        gck_assert_cmprv (rv, ==, CKR_OK);
226
 
}
227
 
 
228
 
TESTING_TEST (certificate_chain_new)
229
 
{
230
 
        GcrCertificateChain *chain;
231
 
 
232
 
        chain = gcr_certificate_chain_new ();
233
 
 
234
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
235
 
                          GCR_CERTIFICATE_CHAIN_UNKNOWN);
236
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 0);
237
 
 
238
 
        g_assert (gcr_certificate_chain_get_endpoint (chain) == NULL);
239
 
 
240
 
        g_object_unref (chain);
241
 
}
242
 
 
243
 
TESTING_TEST (certificate_chain_new_with_cert)
244
 
{
245
 
        GcrCertificateChain *chain;
246
 
        GcrCertificate *check;
247
 
        guint status, length;
248
 
 
249
 
        chain = gcr_certificate_chain_new ();
250
 
        gcr_certificate_chain_add (chain, cert_signed);
251
 
        gcr_certificate_chain_add (chain, cert_ca);
252
 
 
253
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
254
 
                          GCR_CERTIFICATE_CHAIN_UNKNOWN);
255
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 2);
256
 
 
257
 
        status = G_MAXUINT;
258
 
        length = 0;
259
 
        g_object_get (chain, "status", &status, "length", &length, NULL);
260
 
        g_assert_cmpuint (status, ==, GCR_CERTIFICATE_CHAIN_UNKNOWN);
261
 
        g_assert_cmpuint (length, ==, 2);
262
 
 
263
 
        check = gcr_certificate_chain_get_certificate (chain, 1);
264
 
        g_assert (check == cert_ca);
265
 
 
266
 
        /* Not yet completed */
267
 
        check = gcr_certificate_chain_get_anchor (chain);
268
 
        g_assert (check == NULL);
269
 
 
270
 
        check = gcr_certificate_chain_get_endpoint (chain);
271
 
        g_assert (check == cert_signed);
272
 
 
273
 
        g_object_unref (chain);
274
 
}
275
 
 
276
 
TESTING_TEST (certificate_chain_selfsigned)
277
 
{
278
 
        GcrCertificateChain *chain;
279
 
        GError *error = NULL;
280
 
 
281
 
        chain = gcr_certificate_chain_new ();
282
 
 
283
 
        /* Add a self-signed certificate */
284
 
        gcr_certificate_chain_add (chain, cert_self);
285
 
 
286
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
287
 
                                          NULL, 0, NULL, &error))
288
 
                g_assert_not_reached ();
289
 
        g_assert_no_error (error);
290
 
 
291
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
292
 
                          GCR_CERTIFICATE_CHAIN_SELFSIGNED);
293
 
 
294
 
        g_object_unref (chain);
295
 
}
296
 
 
297
 
TESTING_TEST (certificate_chain_incomplete)
298
 
{
299
 
        GcrCertificateChain *chain;
300
 
        GError *error = NULL;
301
 
 
302
 
        chain = gcr_certificate_chain_new ();
303
 
 
304
 
        /* Add a signed certificate */
305
 
        gcr_certificate_chain_add (chain, cert_signed);
306
 
 
307
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
308
 
                                          NULL, 0, NULL, &error))
309
 
                g_assert_not_reached ();
310
 
        g_assert_no_error (error);
311
 
 
312
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
313
 
                          GCR_CERTIFICATE_CHAIN_INCOMPLETE);
314
 
 
315
 
        g_object_unref (chain);
316
 
}
317
 
 
318
 
TESTING_TEST (certificate_chain_empty)
319
 
{
320
 
        GcrCertificateChain *chain;
321
 
        GError *error = NULL;
322
 
 
323
 
        chain = gcr_certificate_chain_new ();
324
 
 
325
 
        /* Add no certificate */
326
 
 
327
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
328
 
                                          NULL, 0, NULL, &error))
329
 
                g_assert_not_reached ();
330
 
        g_assert_no_error (error);
331
 
 
332
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
333
 
                          GCR_CERTIFICATE_CHAIN_UNKNOWN);
334
 
 
335
 
        g_object_unref (chain);
336
 
}
337
 
 
338
 
TESTING_TEST (certificate_chain_trim_extras)
339
 
{
340
 
        GcrCertificateChain *chain;
341
 
        GError *error = NULL;
342
 
 
343
 
        chain = gcr_certificate_chain_new ();
344
 
 
345
 
        /* Add two unrelated certificates */
346
 
        gcr_certificate_chain_add (chain, cert_self);
347
 
        gcr_certificate_chain_add (chain, cert_signed);
348
 
 
349
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 2);
350
 
 
351
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
352
 
                                          NULL, 0, NULL, &error))
353
 
                g_assert_not_reached ();
354
 
        g_assert_no_error (error);
355
 
 
356
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
357
 
                          GCR_CERTIFICATE_CHAIN_SELFSIGNED);
358
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 1);
359
 
 
360
 
        g_object_unref (chain);
361
 
}
362
 
 
363
 
static void
364
 
fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data)
365
 
{
366
 
        *((GAsyncResult**)user_data) = result;
367
 
        g_object_ref (result);
368
 
        testing_wait_stop ();
369
 
}
370
 
 
371
 
TESTING_TEST (certificate_chain_complete_async)
372
 
{
373
 
        GcrCertificateChain *chain;
374
 
        GError *error = NULL;
375
 
        GAsyncResult *result = NULL;
376
 
 
377
 
        chain = gcr_certificate_chain_new ();
378
 
 
379
 
        /* Add a whole bunch of certificates */
380
 
        gcr_certificate_chain_add (chain, cert_signed);
381
 
        gcr_certificate_chain_add (chain, cert_ca);
382
 
        gcr_certificate_chain_add (chain, cert_self);
383
 
 
384
 
        gcr_certificate_chain_build_async (chain, GCR_PURPOSE_CLIENT_AUTH,
385
 
                                           NULL, 0, NULL, fetch_async_result, &result);
386
 
        testing_wait_until (500);
387
 
        if (!gcr_certificate_chain_build_finish (chain, result, &error))
388
 
                g_assert_not_reached ();
389
 
        g_assert_no_error (error);
390
 
        g_object_unref (result);
391
 
 
392
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
393
 
                          GCR_CERTIFICATE_CHAIN_SELFSIGNED);
394
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 2);
395
 
 
396
 
        g_object_unref (chain);
397
 
}
398
 
 
399
 
TESTING_TEST (certificate_chain_with_anchor)
400
 
{
401
 
        GcrCertificateChain *chain;
402
 
        GError *error = NULL;
403
 
 
404
 
        chain = gcr_certificate_chain_new ();
405
 
 
406
 
        /* Two certificates in chain with ca trust anchor */
407
 
        gcr_certificate_chain_add (chain, cert_signed);
408
 
        gcr_certificate_chain_add (chain, cert_ca);
409
 
        add_anchor_to_module (cert_ca, GCR_PURPOSE_CLIENT_AUTH);
410
 
 
411
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 2);
412
 
 
413
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
414
 
                                          NULL, 0, NULL, &error))
415
 
                g_assert_not_reached ();
416
 
        g_assert_no_error (error);
417
 
 
418
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
419
 
                          GCR_CERTIFICATE_CHAIN_ANCHORED);
420
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 2);
421
 
        g_assert (gcr_certificate_chain_get_anchor (chain) == cert_ca);
422
 
 
423
 
        g_object_unref (chain);
424
 
}
425
 
 
426
 
TESTING_TEST (certificate_chain_with_anchor_and_lookup_ca)
427
 
{
428
 
        GcrCertificateChain *chain;
429
 
        GError *error = NULL;
430
 
 
431
 
        chain = gcr_certificate_chain_new ();
432
 
 
433
 
        /* One signed certificate, with CA in pkcs11, and trust anchor */
434
 
        gcr_certificate_chain_add (chain, cert_signed);
435
 
        add_certificate_to_module (cert_ca);
436
 
        add_anchor_to_module (cert_ca, GCR_PURPOSE_CLIENT_AUTH);
437
 
 
438
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 1);
439
 
 
440
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
441
 
                                          NULL, 0, NULL, &error))
442
 
                g_assert_not_reached ();
443
 
        g_assert_no_error (error);
444
 
 
445
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
446
 
                          GCR_CERTIFICATE_CHAIN_ANCHORED);
447
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 2);
448
 
        g_assert (gcr_certificate_chain_get_anchor (chain) != NULL);
449
 
 
450
 
        g_object_unref (chain);
451
 
}
452
 
 
453
 
TESTING_TEST (certificate_chain_with_pinned)
454
 
{
455
 
        GcrCertificateChain *chain;
456
 
        GError *error = NULL;
457
 
 
458
 
        chain = gcr_certificate_chain_new ();
459
 
 
460
 
        /* One certificate, and add CA to pkcs11 */
461
 
        gcr_certificate_chain_add (chain, cert_signed);
462
 
        gcr_certificate_chain_add (chain, cert_ca);
463
 
        add_pinned_to_module (cert_signed, GCR_PURPOSE_CLIENT_AUTH, "pinned.example.com");
464
 
 
465
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 2);
466
 
 
467
 
        /* But we don't allow the lookup to happen */
468
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
469
 
                                          "pinned.example.com", 0, NULL, &error))
470
 
                g_assert_not_reached ();
471
 
        g_assert_no_error (error);
472
 
 
473
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
474
 
                          GCR_CERTIFICATE_CHAIN_PINNED);
475
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 1);
476
 
        g_assert (gcr_certificate_chain_get_anchor (chain) == NULL);
477
 
 
478
 
        g_object_unref (chain);
479
 
}
480
 
 
481
 
TESTING_TEST (certificate_chain_without_lookups)
482
 
{
483
 
        GcrCertificateChain *chain;
484
 
        GError *error = NULL;
485
 
 
486
 
        chain = gcr_certificate_chain_new ();
487
 
 
488
 
        /* One certificate, and add CA to pkcs11 */
489
 
        gcr_certificate_chain_add (chain, cert_signed);
490
 
        add_certificate_to_module (cert_ca);
491
 
 
492
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 1);
493
 
 
494
 
        /* But we don't allow the lookup to happen */
495
 
        if (!gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
496
 
                                          NULL, GCR_CERTIFICATE_CHAIN_FLAG_NO_LOOKUPS,
497
 
                                          NULL, &error))
498
 
                g_assert_not_reached ();
499
 
        g_assert_no_error (error);
500
 
 
501
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
502
 
                          GCR_CERTIFICATE_CHAIN_INCOMPLETE);
503
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 1);
504
 
        g_assert (gcr_certificate_chain_get_anchor (chain) == NULL);
505
 
 
506
 
        g_object_unref (chain);
507
 
}
508
 
 
509
 
TESTING_TEST (certificate_chain_with_lookup_error)
510
 
{
511
 
        GcrCertificateChain *chain;
512
 
        GError *error = NULL;
513
 
 
514
 
        /* Make the lookup fail */
515
 
        funcs.C_GetAttributeValue = gck_mock_fail_C_GetAttributeValue;
516
 
 
517
 
        chain = gcr_certificate_chain_new ();
518
 
 
519
 
        /* Two certificates in chain with ca trust anchor */
520
 
        gcr_certificate_chain_add (chain, cert_signed);
521
 
        add_certificate_to_module (cert_ca);
522
 
 
523
 
        g_assert_cmpuint (gcr_certificate_chain_get_length (chain), ==, 1);
524
 
 
525
 
        if (gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
526
 
                                         NULL, 0, NULL, &error))
527
 
                g_assert_not_reached ();
528
 
        g_assert_error (error, GCK_ERROR, CKR_FUNCTION_FAILED);
529
 
        g_clear_error (&error);
530
 
 
531
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
532
 
                          GCR_CERTIFICATE_CHAIN_UNKNOWN);
533
 
 
534
 
        g_object_unref (chain);
535
 
}
536
 
 
537
 
TESTING_TEST (certificate_chain_with_anchor_error)
538
 
{
539
 
        GcrCertificateChain *chain;
540
 
        GError *error = NULL;
541
 
 
542
 
        /* Make the lookup fail */
543
 
        funcs.C_GetAttributeValue = gck_mock_fail_C_GetAttributeValue;
544
 
 
545
 
        chain = gcr_certificate_chain_new ();
546
 
 
547
 
        /* Two certificates in chain with ca trust anchor */
548
 
        gcr_certificate_chain_add (chain, cert_signed);
549
 
        add_certificate_to_module (cert_ca);
550
 
 
551
 
        if (gcr_certificate_chain_build (chain, GCR_PURPOSE_CLIENT_AUTH,
552
 
                                         NULL, 0, NULL, &error))
553
 
                g_assert_not_reached ();
554
 
        g_assert_error (error, GCK_ERROR, CKR_FUNCTION_FAILED);
555
 
        g_clear_error (&error);
556
 
 
557
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
558
 
                          GCR_CERTIFICATE_CHAIN_UNKNOWN);
559
 
 
560
 
        g_object_unref (chain);
561
 
}
562
 
 
563
 
TESTING_TEST (certificate_chain_with_anchor_error_async)
564
 
{
565
 
        GcrCertificateChain *chain;
566
 
        GError *error = NULL;
567
 
        GAsyncResult *result;
568
 
 
569
 
        /* Make the lookup fail */
570
 
        funcs.C_GetAttributeValue = gck_mock_fail_C_GetAttributeValue;
571
 
 
572
 
        chain = gcr_certificate_chain_new ();
573
 
 
574
 
        /* Two certificates in chain with ca trust anchor */
575
 
        gcr_certificate_chain_add (chain, cert_signed);
576
 
        add_certificate_to_module (cert_ca);
577
 
 
578
 
        gcr_certificate_chain_build_async (chain, GCR_PURPOSE_CLIENT_AUTH,
579
 
                                           NULL, 0, NULL, fetch_async_result, &result);
580
 
        testing_wait_until (500);
581
 
        if (gcr_certificate_chain_build_finish (chain, result, &error))
582
 
                g_assert_not_reached ();
583
 
        g_assert_error (error, GCK_ERROR, CKR_FUNCTION_FAILED);
584
 
        g_clear_error (&error);
585
 
        g_object_unref (result);
586
 
 
587
 
        g_assert_cmpuint (gcr_certificate_chain_get_status (chain), ==,
588
 
                          GCR_CERTIFICATE_CHAIN_UNKNOWN);
589
 
 
590
 
        g_object_unref (chain);
591
 
}