Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-data-asn1.c: Test ASN.1 routines
3 :
4 : Copyright (C) 2007 Stefan Walter
5 :
6 : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : The Gnome Keyring Library is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public
17 : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stef@memberwebs.com>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "gkm/gkm-data-asn1.h"
26 :
27 : #include <glib.h>
28 : #include <glib-object.h>
29 : #include <gcrypt.h>
30 :
31 : #include <stdlib.h>
32 : #include <stdio.h>
33 : #include <string.h>
34 :
35 : #include "egg/egg-asn1x.h"
36 : #include "egg/egg-asn1-defs.h"
37 : #include "egg/egg-libgcrypt.h"
38 : #include "egg/egg-secure-memory.h"
39 :
40 : typedef struct _EggAsn1xDef ASN1_ARRAY_TYPE;
41 : typedef struct _EggAsn1xDef asn1_static_node;
42 : #include "test.asn.h"
43 :
44 : #define TEST_STRING "test data to write and read in the ASN1 structures"
45 :
46 : static GQuark OID_ANSI_SECP256R1;
47 :
48 40 : EGG_SECURE_DEFINE_GLIB_GLOBALS();
49 :
50 : typedef struct {
51 : GNode *asn1_cert;
52 : } Test;
53 :
54 : static void
55 5 : setup (Test *test, gconstpointer unused)
56 : {
57 : GBytes *data;
58 : gchar *contents;
59 : gsize length;
60 :
61 5 : if (!g_file_get_contents (SRCDIR "/pkcs11/gkm/fixtures/test-certificate-1.der", &contents, &length, NULL))
62 0 : g_assert_not_reached ();
63 :
64 5 : data = g_bytes_new_take (contents, length);
65 5 : test->asn1_cert = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", data);
66 5 : g_assert (test->asn1_cert);
67 5 : g_bytes_unref (data);
68 5 : }
69 :
70 : static void
71 5 : teardown (Test *test, gconstpointer unused)
72 : {
73 5 : egg_asn1x_destroy (test->asn1_cert);
74 5 : }
75 :
76 : static void
77 1 : test_asn1_integers (Test *test, gconstpointer unused)
78 : {
79 : GNode *asn;
80 : gcry_mpi_t mpi, mpt;
81 : GBytes *data;
82 : gboolean ret;
83 :
84 1 : asn = egg_asn1x_create (test_asn1_tab, "TestIntegers");
85 1 : g_assert ("asn test structure is null" && asn != NULL);
86 :
87 : /* Make a random number */
88 1 : mpi = gcry_mpi_new (512);
89 1 : g_return_if_fail (mpi);
90 1 : gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM);
91 :
92 : /* Write the mpi out */
93 1 : ret = gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "mpi", NULL), mpi);
94 1 : g_assert ("couldn't write mpi to asn1" && ret);
95 :
96 : /* Now encode the whole caboodle */
97 1 : data = egg_asn1x_encode (asn, NULL);
98 1 : g_assert ("encoding asn1 didn't work" && data != NULL);
99 :
100 1 : egg_asn1x_destroy (asn);
101 :
102 : /* Now decode it all nicely */
103 1 : asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestIntegers", data);
104 1 : g_assert (asn != NULL);
105 :
106 1 : ret = gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "mpi", NULL), &mpt);
107 1 : egg_asn1x_destroy (asn);
108 1 : g_assert ("couldn't read mpi from asn1" && ret);
109 1 : g_assert ("mpi returned is null" && mpt != NULL);
110 1 : g_assert ("mpi is wrong number" && gcry_mpi_cmp (mpi, mpt) == 0);
111 :
112 1 : g_bytes_unref (data);
113 1 : gcry_mpi_release (mpi);
114 1 : gcry_mpi_release (mpt);
115 : }
116 :
117 : static void
118 1 : test_asn1_string_mpi (Test *test, gconstpointer unused)
119 : {
120 : GNode *asn;
121 : gcry_mpi_t mpi, mpt;
122 : GBytes *data;
123 : gboolean ret;
124 :
125 1 : asn = egg_asn1x_create (test_asn1_tab, "TestStringMpi");
126 1 : g_assert ("asn test structure is null" && asn != NULL);
127 :
128 : /* Make a random number */
129 1 : mpi = gcry_mpi_new (512);
130 1 : g_return_if_fail (mpi);
131 1 : gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM);
132 :
133 : /* Write the mpi out */
134 1 : ret = gkm_data_asn1_write_string_mpi (egg_asn1x_node (asn, "mpi", NULL), mpi);
135 1 : g_assert ("couldn't write mpi to bit string in asn1" && ret);
136 :
137 : /* Now encode the whole caboodle */
138 1 : data = egg_asn1x_encode (asn, NULL);
139 1 : g_assert ("encoding asn1 didn't work" && data != NULL);
140 :
141 1 : egg_asn1x_destroy (asn);
142 :
143 : /* Now decode it all nicely */
144 1 : asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestStringMpi", data);
145 1 : g_assert (asn != NULL);
146 :
147 1 : ret = gkm_data_asn1_read_string_mpi (egg_asn1x_node (asn, "mpi", NULL), &mpt);
148 1 : egg_asn1x_destroy (asn);
149 1 : g_assert ("couldn't read mpi from octet string in asn1" && ret);
150 1 : g_assert ("mpi returned is null" && mpt != NULL);
151 1 : g_assert ("mpi is wrong number" && gcry_mpi_cmp (mpi, mpt) == 0);
152 :
153 1 : g_bytes_unref (data);
154 1 : gcry_mpi_release (mpi);
155 1 : gcry_mpi_release (mpt);
156 : }
157 :
158 : static void
159 1 : test_asn1_bit_string (Test *test, gconstpointer unused)
160 : {
161 : GNode *asn;
162 : GBytes *data;
163 : gboolean ret;
164 : GBytes *source, *target;
165 : gsize target_bits, source_bits;
166 :
167 1 : asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
168 1 : g_assert ("asn test structure is null" && asn != NULL);
169 :
170 : /* Create a string */
171 1 : source = g_bytes_new (TEST_STRING, strlen(TEST_STRING));
172 1 : g_return_if_fail (source);
173 1 : source_bits = g_bytes_get_size(source)*8;
174 :
175 : /* Write the string out */
176 1 : ret = gkm_data_asn1_write_bit_string (egg_asn1x_node (asn, "data", NULL),
177 : source, source_bits);
178 1 : g_assert ("couldn't write string to asn1" && ret);
179 :
180 : /* Now encode the whole caboodle */
181 1 : data = egg_asn1x_encode (asn, NULL);
182 1 : g_assert ("encoding asn1 didn't work" && data != NULL);
183 :
184 1 : egg_asn1x_destroy (asn);
185 :
186 : /* Now decode it all nicely */
187 1 : asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestBitString", data);
188 1 : g_assert (asn != NULL);
189 :
190 1 : ret = gkm_data_asn1_read_bit_string (egg_asn1x_node (asn, "data", NULL),
191 : &target, &target_bits);
192 1 : egg_asn1x_destroy (asn);
193 1 : g_assert ("couldn't read bit string from asn1" && ret);
194 1 : g_assert ("bit string returned is null" && target != NULL);
195 1 : g_assert ("Source and target length differ" && target_bits == source_bits);
196 1 : g_assert ("Bit strings differ" && g_bytes_equal (source, target));
197 :
198 1 : g_bytes_unref (data);
199 1 : g_bytes_unref (source);
200 1 : g_bytes_unref (target);
201 : }
202 : /* XXX test some incomplete octets */
203 :
204 : static void
205 1 : test_asn1_string (Test *test, gconstpointer unused)
206 : {
207 : GNode *asn;
208 : GBytes *data;
209 : gboolean ret;
210 : GBytes *source, *target;
211 :
212 1 : asn = egg_asn1x_create (test_asn1_tab, "TestString");
213 1 : g_assert ("asn test structure is null" && asn != NULL);
214 :
215 : /* Create a string */
216 1 : source = g_bytes_new (TEST_STRING, strlen(TEST_STRING));
217 1 : g_return_if_fail (source);
218 :
219 : /* Write the string out */
220 1 : ret = gkm_data_asn1_write_string (egg_asn1x_node (asn, "data", NULL),
221 : source);
222 1 : g_assert ("couldn't write string to asn1" && ret);
223 :
224 : /* Now encode the whole caboodle */
225 1 : data = egg_asn1x_encode (asn, NULL);
226 1 : g_assert ("encoding asn1 didn't work" && data != NULL);
227 :
228 1 : egg_asn1x_destroy (asn);
229 :
230 : /* Now decode it all nicely */
231 1 : asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestString", data);
232 1 : g_assert (asn != NULL);
233 :
234 1 : ret = gkm_data_asn1_read_string (egg_asn1x_node (asn, "data", NULL),
235 : &target);
236 1 : egg_asn1x_destroy (asn);
237 1 : g_assert ("couldn't read string from asn1" && ret);
238 1 : g_assert ("string returned is null" && target != NULL);
239 1 : g_assert ("The strings differ" && g_bytes_equal (source, target));
240 :
241 1 : g_bytes_unref (data);
242 1 : g_bytes_unref (source);
243 1 : g_bytes_unref (target);
244 : }
245 :
246 : static void
247 1 : test_asn1_oid (Test *test, gconstpointer unused)
248 : {
249 : GNode *asn;
250 : GBytes *data;
251 : gboolean ret;
252 : GQuark source, target;
253 :
254 1 : asn = egg_asn1x_create (test_asn1_tab, "TestOid");
255 1 : g_assert ("asn test structure is null" && asn != NULL);
256 :
257 : /* Create a OID Quark */
258 1 : OID_ANSI_SECP256R1 = g_quark_from_static_string("1.2.840.10045.3.1.7");
259 1 : source = OID_ANSI_SECP256R1;
260 :
261 : /* Write the OID out */
262 1 : ret = gkm_data_asn1_write_oid (egg_asn1x_node (asn, "oid", NULL), source);
263 1 : g_assert ("couldn't write OID to asn1" && ret);
264 :
265 : /* Now encode the whole caboodle */
266 1 : data = egg_asn1x_encode (asn, NULL);
267 1 : g_assert ("encoding asn1 didn't work" && data != NULL);
268 :
269 1 : egg_asn1x_destroy (asn);
270 :
271 : /* Now decode it all nicely */
272 1 : asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestOid", data);
273 1 : g_assert (asn != NULL);
274 :
275 1 : ret = gkm_data_asn1_read_oid (egg_asn1x_node (asn, "oid", NULL), &target);
276 1 : egg_asn1x_destroy (asn);
277 1 : g_assert ("couldn't read oid from asn1" && ret);
278 1 : g_assert ("oid returned is 0" && target != 0);
279 1 : g_assert ("mpi is wrong number" && source == target);
280 :
281 1 : g_bytes_unref (data);
282 1 : }
283 :
284 : int
285 1 : main (int argc, char **argv)
286 : {
287 : #if !GLIB_CHECK_VERSION(2,35,0)
288 : g_type_init ();
289 : #endif
290 1 : egg_libgcrypt_initialize();
291 :
292 1 : g_test_init (&argc, &argv, NULL);
293 :
294 1 : g_test_add ("/gkm/data-asn1/integers", Test, NULL, setup, test_asn1_integers, teardown);
295 1 : g_test_add ("/gkm/data-asn1/string_mpi", Test, NULL, setup, test_asn1_string_mpi, teardown);
296 1 : g_test_add ("/gkm/data-asn1/bit_string", Test, NULL, setup, test_asn1_bit_string, teardown);
297 1 : g_test_add ("/gkm/data-asn1/string", Test, NULL, setup, test_asn1_string, teardown);
298 1 : g_test_add ("/gkm/data-asn1/oid", Test, NULL, setup, test_asn1_oid, teardown);
299 :
300 1 : return g_test_run ();
301 : }
|