summaryrefslogtreecommitdiff
path: root/gcr/test-fingerprint.c
blob: 002e082fa9306f04ee5dd78fc76efd875c82ea4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
   Copyright (C) 2010 Collabora Ltd

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   see <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stefw@collabora.co.uk>
*/

#include "config.h"
#define GCR_COMPILATION 1

#include "gcr/gcr.h"
#include "gcr/gcr-internal.h"
#include "gcr/gcr-fingerprint.h"

#include "gck/gck-test.h"
#include "gck/pkcs11n.h"

#include "egg/egg-asn1x.h"
#include "egg/egg-asn1-defs.h"
#include "egg/egg-testing.h"

#include <glib.h>

#include <errno.h>

typedef struct {
	GBytes *cert_rsa;
	GBytes *key_rsa;
	GBytes *cert_dsa;
	GBytes *key_dsa;
} Test;

static void
setup (Test *test, gconstpointer unused)
{
	GError *error = NULL;
	gchar *contents;
	gsize length;

	g_file_get_contents (SRCDIR "/gcr/fixtures/client.crt", &contents, &length, &error);
	g_assert_no_error (error);
	test->cert_rsa = g_bytes_new_take (contents, length);

	g_file_get_contents (SRCDIR "/gcr/fixtures/client.key", &contents, &length, &error);
	g_assert_no_error (error);
	test->key_rsa = g_bytes_new_take (contents, length);

	g_file_get_contents (SRCDIR "/gcr/fixtures/generic-dsa.crt", &contents, &length, &error);
	g_assert_no_error (error);
	test->cert_dsa = g_bytes_new_take (contents, length);

	g_file_get_contents (SRCDIR "/gcr/fixtures/generic-dsa.key", &contents, &length, &error);
	g_assert_no_error (error);
	test->key_dsa = g_bytes_new_take (contents, length);
}

static void
teardown (Test *test, gconstpointer unused)
{
	g_bytes_unref (test->cert_rsa);
	g_bytes_unref (test->key_rsa);
	g_bytes_unref (test->cert_dsa);
	g_bytes_unref (test->key_dsa);
}

static void
on_parser_parsed (GcrParser *parser,
                  gpointer user_data)
{
	GckAttributes **attrs = user_data;
	g_assert (!*attrs);
	*attrs = gcr_parser_get_parsed_attributes (parser);
	g_assert (*attrs);
	gck_attributes_ref (*attrs);
}

static GckAttributes*
parse_attributes_for_key (GBytes *data)
{
	GcrParser *parser;
	GckAttributes *attrs = NULL;
	GError *error = NULL;

	parser = gcr_parser_new ();
	g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), &attrs);
	gcr_parser_parse_bytes (parser, data, &error);
	g_assert_no_error (error);
	g_object_unref (parser);

	g_assert (attrs);
	return attrs;
}

static GckAttributes *
build_attributes_for_cert (GBytes *data)
{
	GckBuilder builder = GCK_BUILDER_INIT;

	gck_builder_add_data (&builder, CKA_VALUE, g_bytes_get_data (data, NULL),
	                      g_bytes_get_size (data));
	gck_builder_add_ulong (&builder, CKA_CLASS, CKO_CERTIFICATE);
	gck_builder_add_ulong (&builder, CKA_CERTIFICATE_TYPE, CKC_X_509);

	return gck_builder_end (&builder);
}

static GBytes *
parse_subject_public_key_info_for_cert (GBytes *data)
{
	GBytes *info;
	GNode *asn;

	asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", data);
	g_assert (asn != NULL);

	info = egg_asn1x_get_element_raw (egg_asn1x_node (asn, "tbsCertificate", "subjectPublicKeyInfo", NULL));
	g_assert (info != NULL);

	egg_asn1x_destroy (asn);
	return info;
}

static void
test_rsa (Test *test, gconstpointer unused)
{
	GckAttributes *key, *cert;
	GBytes *info;
	guchar *fingerprint1, *fingerprint2, *fingerprint3;
	gsize n_fingerprint1, n_fingerprint2, n_fingerprint3;

	key = parse_attributes_for_key (test->key_rsa);
	info = parse_subject_public_key_info_for_cert (test->cert_rsa);
	cert = build_attributes_for_cert (test->cert_rsa);

	fingerprint1 = gcr_fingerprint_from_subject_public_key_info (g_bytes_get_data (info, NULL),
	                                                             g_bytes_get_size (info),
	                                                             G_CHECKSUM_SHA1, &n_fingerprint1);
	fingerprint2 = gcr_fingerprint_from_attributes (key, G_CHECKSUM_SHA1, &n_fingerprint2);
	fingerprint3 = gcr_fingerprint_from_attributes (cert, G_CHECKSUM_SHA1, &n_fingerprint3);

	egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint2, n_fingerprint2);
	egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint3, n_fingerprint3);

	g_free (fingerprint1);
	g_free (fingerprint2);
	g_free (fingerprint3);

	g_bytes_unref (info);
	gck_attributes_unref (key);
	gck_attributes_unref (cert);
}

static void
test_dsa (Test *test, gconstpointer unused)
{
	GckAttributes *key, *cert;
	GBytes *info;
	guchar *fingerprint1, *fingerprint2, *fingerprint3;
	gsize n_fingerprint1, n_fingerprint2, n_fingerprint3;

	key = parse_attributes_for_key (test->key_dsa);
	info = parse_subject_public_key_info_for_cert (test->cert_dsa);
	cert = build_attributes_for_cert (test->cert_dsa);

	fingerprint1 = gcr_fingerprint_from_subject_public_key_info (g_bytes_get_data (info, NULL),
	                                                             g_bytes_get_size (info),
	                                                             G_CHECKSUM_SHA1, &n_fingerprint1);
	fingerprint2 = gcr_fingerprint_from_attributes (key, G_CHECKSUM_SHA1, &n_fingerprint2);
	fingerprint3 = gcr_fingerprint_from_attributes (cert, G_CHECKSUM_SHA1, &n_fingerprint3);

	egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint2, n_fingerprint2);
	egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint3, n_fingerprint3);

	g_free (fingerprint1);
	g_free (fingerprint2);
	g_free (fingerprint3);

	g_bytes_unref (info);
	gck_attributes_unref (key);
	gck_attributes_unref (cert);
}

int
main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/gcr/fingerprint/rsa", Test, NULL, setup, test_rsa, teardown);
	g_test_add ("/gcr/fingerprint/dsa", Test, NULL, setup, test_dsa, teardown);

	return g_test_run ();
}