Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /*
3 : : Copyright (C) 2010 Collabora Ltd
4 : :
5 : : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : : modify it under the terms of the GNU Library General Public License as
7 : : published by the Free Software Foundation; either version 2 of the
8 : : License, or (at your option) any later version.
9 : :
10 : : The Gnome Keyring Library is distributed in the hope that it will be useful,
11 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : Library General Public License for more details.
14 : :
15 : : You should have received a copy of the GNU Library General Public
16 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : : see <http://www.gnu.org/licenses/>.
18 : :
19 : : Author: Stef Walter <stefw@collabora.co.uk>
20 : : */
21 : :
22 : : #include "config.h"
23 : : #define GCR_COMPILATION 1
24 : :
25 : : #include "gcr/gcr.h"
26 : : #include "gcr/gcr-internal.h"
27 : : #include "gcr/gcr-fingerprint.h"
28 : :
29 : : #include "gck/gck-test.h"
30 : : #include "gck/pkcs11n.h"
31 : :
32 : : #include "egg/egg-asn1x.h"
33 : : #include "egg/egg-asn1-defs.h"
34 : : #include "egg/egg-testing.h"
35 : :
36 : : #include <glib.h>
37 : :
38 : : #include <errno.h>
39 : :
40 : : typedef struct {
41 : : GBytes *cert_rsa;
42 : : GBytes *key_rsa;
43 : : GBytes *cert_dsa;
44 : : GBytes *key_dsa;
45 : : } Test;
46 : :
47 : : static void
48 : 2 : setup (Test *test, gconstpointer unused)
49 : : {
50 : 2 : GError *error = NULL;
51 : : gchar *contents;
52 : : gsize length;
53 : :
54 : 2 : g_file_get_contents (SRCDIR "/gcr/fixtures/client.crt", &contents, &length, &error);
55 [ - + ]: 2 : g_assert_no_error (error);
56 : 2 : test->cert_rsa = g_bytes_new_take (contents, length);
57 : :
58 : 2 : g_file_get_contents (SRCDIR "/gcr/fixtures/client.key", &contents, &length, &error);
59 [ - + ]: 2 : g_assert_no_error (error);
60 : 2 : test->key_rsa = g_bytes_new_take (contents, length);
61 : :
62 : 2 : g_file_get_contents (SRCDIR "/gcr/fixtures/generic-dsa.crt", &contents, &length, &error);
63 [ - + ]: 2 : g_assert_no_error (error);
64 : 2 : test->cert_dsa = g_bytes_new_take (contents, length);
65 : :
66 : 2 : g_file_get_contents (SRCDIR "/gcr/fixtures/generic-dsa.key", &contents, &length, &error);
67 [ - + ]: 2 : g_assert_no_error (error);
68 : 2 : test->key_dsa = g_bytes_new_take (contents, length);
69 : 2 : }
70 : :
71 : : static void
72 : 2 : teardown (Test *test, gconstpointer unused)
73 : : {
74 : 2 : g_bytes_unref (test->cert_rsa);
75 : 2 : g_bytes_unref (test->key_rsa);
76 : 2 : g_bytes_unref (test->cert_dsa);
77 : 2 : g_bytes_unref (test->key_dsa);
78 : 2 : }
79 : :
80 : : static void
81 : 2 : on_parser_parsed (GcrParser *parser,
82 : : gpointer user_data)
83 : : {
84 : 2 : GckAttributes **attrs = user_data;
85 [ - + ]: 2 : g_assert (!*attrs);
86 : 2 : *attrs = gcr_parser_get_parsed_attributes (parser);
87 [ - + ]: 2 : g_assert (*attrs);
88 : 2 : gck_attributes_ref (*attrs);
89 : 2 : }
90 : :
91 : : static GckAttributes*
92 : 2 : parse_attributes_for_key (GBytes *data)
93 : : {
94 : : GcrParser *parser;
95 : 2 : GckAttributes *attrs = NULL;
96 : 2 : GError *error = NULL;
97 : :
98 : 2 : parser = gcr_parser_new ();
99 : 2 : g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), &attrs);
100 : 2 : gcr_parser_parse_bytes (parser, data, &error);
101 [ - + ]: 2 : g_assert_no_error (error);
102 : 2 : g_object_unref (parser);
103 : :
104 [ - + ]: 2 : g_assert (attrs);
105 : 2 : return attrs;
106 : : }
107 : :
108 : : static GckAttributes *
109 : 2 : build_attributes_for_cert (GBytes *data)
110 : : {
111 : 2 : GckBuilder builder = GCK_BUILDER_INIT;
112 : :
113 : 2 : gck_builder_add_data (&builder, CKA_VALUE, g_bytes_get_data (data, NULL),
114 : : g_bytes_get_size (data));
115 : 2 : gck_builder_add_ulong (&builder, CKA_CLASS, CKO_CERTIFICATE);
116 : 2 : gck_builder_add_ulong (&builder, CKA_CERTIFICATE_TYPE, CKC_X_509);
117 : :
118 : 2 : return gck_builder_end (&builder);
119 : : }
120 : :
121 : : static GBytes *
122 : 2 : parse_subject_public_key_info_for_cert (GBytes *data)
123 : : {
124 : : GBytes *info;
125 : : GNode *asn;
126 : :
127 : 2 : asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", data);
128 [ - + ]: 2 : g_assert (asn != NULL);
129 : :
130 : 2 : info = egg_asn1x_get_element_raw (egg_asn1x_node (asn, "tbsCertificate", "subjectPublicKeyInfo", NULL));
131 [ - + ]: 2 : g_assert (info != NULL);
132 : :
133 : 2 : egg_asn1x_destroy (asn);
134 : 2 : return info;
135 : : }
136 : :
137 : : static void
138 : 1 : test_rsa (Test *test, gconstpointer unused)
139 : : {
140 : : GckAttributes *key, *cert;
141 : : GBytes *info;
142 : : guchar *fingerprint1, *fingerprint2, *fingerprint3;
143 : : gsize n_fingerprint1, n_fingerprint2, n_fingerprint3;
144 : :
145 : 1 : key = parse_attributes_for_key (test->key_rsa);
146 : 1 : info = parse_subject_public_key_info_for_cert (test->cert_rsa);
147 : 1 : cert = build_attributes_for_cert (test->cert_rsa);
148 : :
149 : 1 : fingerprint1 = gcr_fingerprint_from_subject_public_key_info (g_bytes_get_data (info, NULL),
150 : : g_bytes_get_size (info),
151 : : G_CHECKSUM_SHA1, &n_fingerprint1);
152 : 1 : fingerprint2 = gcr_fingerprint_from_attributes (key, G_CHECKSUM_SHA1, &n_fingerprint2);
153 : 1 : fingerprint3 = gcr_fingerprint_from_attributes (cert, G_CHECKSUM_SHA1, &n_fingerprint3);
154 : :
155 [ + - + - ]: 1 : egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint2, n_fingerprint2);
156 [ + - + - ]: 1 : egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint3, n_fingerprint3);
157 : :
158 : 1 : g_free (fingerprint1);
159 : 1 : g_free (fingerprint2);
160 : 1 : g_free (fingerprint3);
161 : :
162 : 1 : g_bytes_unref (info);
163 : 1 : gck_attributes_unref (key);
164 : 1 : gck_attributes_unref (cert);
165 : 1 : }
166 : :
167 : : static void
168 : 1 : test_dsa (Test *test, gconstpointer unused)
169 : : {
170 : : GckAttributes *key, *cert;
171 : : GBytes *info;
172 : : guchar *fingerprint1, *fingerprint2, *fingerprint3;
173 : : gsize n_fingerprint1, n_fingerprint2, n_fingerprint3;
174 : :
175 : 1 : key = parse_attributes_for_key (test->key_dsa);
176 : 1 : info = parse_subject_public_key_info_for_cert (test->cert_dsa);
177 : 1 : cert = build_attributes_for_cert (test->cert_dsa);
178 : :
179 : 1 : fingerprint1 = gcr_fingerprint_from_subject_public_key_info (g_bytes_get_data (info, NULL),
180 : : g_bytes_get_size (info),
181 : : G_CHECKSUM_SHA1, &n_fingerprint1);
182 : 1 : fingerprint2 = gcr_fingerprint_from_attributes (key, G_CHECKSUM_SHA1, &n_fingerprint2);
183 : 1 : fingerprint3 = gcr_fingerprint_from_attributes (cert, G_CHECKSUM_SHA1, &n_fingerprint3);
184 : :
185 [ + - + - ]: 1 : egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint2, n_fingerprint2);
186 [ + - + - ]: 1 : egg_assert_cmpmem (fingerprint1, n_fingerprint1, ==, fingerprint3, n_fingerprint3);
187 : :
188 : 1 : g_free (fingerprint1);
189 : 1 : g_free (fingerprint2);
190 : 1 : g_free (fingerprint3);
191 : :
192 : 1 : g_bytes_unref (info);
193 : 1 : gck_attributes_unref (key);
194 : 1 : gck_attributes_unref (cert);
195 : 1 : }
196 : :
197 : : int
198 : 1 : main (int argc, char **argv)
199 : : {
200 : 1 : g_test_init (&argc, &argv, NULL);
201 : :
202 : 1 : g_test_add ("/gcr/fingerprint/rsa", Test, NULL, setup, test_rsa, teardown);
203 : 1 : g_test_add ("/gcr/fingerprint/dsa", Test, NULL, setup, test_dsa, teardown);
204 : :
205 : 1 : return g_test_run ();
206 : : }
|