summaryrefslogtreecommitdiff
path: root/daemon/pkix/tests/unit-test-pkix-parser.c
blob: 80504252f5ccae2b33d48b71ab1f038c3a7411d3 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-pkix-parser.c: Test PKIX parser

   Copyright (C) 2007 Stefan Walter

   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,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "run-auto-test.h"

#include "common/gkr-location.h"
#include "common/gkr-crypto.h"
#include "common/gkr-secure-memory.h"

#include "pkix/gkr-pkix-parser.h"

#include <glib.h>
#include <gcrypt.h>
#include <libtasn1.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* 
 * Each test looks like (on one line):
 *     void unit_test_xxxxx (CuTest* cu)
 * 
 * Each setup looks like (on one line):
 *     void unit_setup_xxxxx (void);
 * 
 * Each teardown looks like (on one line):
 *     void unit_teardown_xxxxx (void);
 * 
 * Tests be run in the order specified here.
 */

static GkrPkixParser *parser = NULL;

static GQuark last_type_parsed = 0;
static gcry_sexp_t last_sexp_parsed = NULL;
static ASN1_TYPE last_asn1_parsed = NULL;
static guint n_parsed = 0;

static CuTest *the_cu = NULL;

static gboolean
parsed_partial (GkrPkixParser *parser, GQuark location, gkrconstid digest, 
                GQuark type, gpointer user_data)
{
	CuTest *cu = the_cu;
	g_assert (cu);
		
	CuAssert (cu, "location is empty", location != 0);
	CuAssert (cu, "location is invalid", gkr_location_to_path (location) != NULL);
	CuAssert (cu, "digest is empty", digest != NULL);
	CuAssert (cu, "type is invalid", type != 0);
	
	g_print ("parsed partial at: %s\n", g_quark_to_string (location));
	last_sexp_parsed = NULL;
	last_type_parsed = type;
	++n_parsed;
	
	return TRUE;
}

static gboolean
parsed_sexp (GkrPkixParser *parser, GQuark location, gkrconstid digest, 
             GQuark type, gcry_sexp_t sexp, gpointer user_data)
{
	CuTest *cu = the_cu;
	g_assert (cu);

	CuAssert (cu, "location is empty", location != 0);
	CuAssert (cu, "location is invalid", gkr_location_to_path (location) != NULL);
	CuAssert (cu, "digest is empty", digest != NULL);
	CuAssert (cu, "type is invalid", type != 0);
	CuAssert (cu, "sexp is invalid", sexp != NULL);

	g_print ("parsed sexp at: %s\n", g_quark_to_string (location));
		
	last_sexp_parsed = sexp;
	last_type_parsed = type;
	++n_parsed;
	
	return TRUE;
}

static gboolean
parsed_asn1 (GkrPkixParser *parser, GQuark location, gkrconstid digest,
             GQuark type, ASN1_TYPE asn1, gpointer user_data)
{
	CuTest *cu = the_cu;
	g_assert (cu);

	CuAssert (cu, "location is empty", location != 0);
	CuAssert (cu, "location is invalid", gkr_location_to_path (location) != NULL);
	CuAssert (cu, "digest is empty", digest != NULL);
	CuAssert (cu, "type is invalid", type != 0);
	CuAssert (cu, "asn1 is invalid", asn1 != NULL);

	g_print ("parsed asn1 at: %s\n", g_quark_to_string (location));
		
	last_asn1_parsed = asn1;
	last_type_parsed = type;
	++n_parsed;
	
	return TRUE;
}

static gboolean
ask_password (GkrPkixParser *parser, GQuark loc, gkrconstid digest, 
              GQuark type, const gchar *details, gint *state, 
              gchar **password, gpointer user_data) 
{
	CuTest *cu = the_cu;
	gchar *msg;
	gint st;
	
	g_assert (cu);
	CuAssert (cu, "state is null", state != NULL);
	CuAssert (cu, "state is bad", *state >= 0 && *state < 3);

	st = *state;
	(*state)++;

	CuAssert (cu, "location is empty", loc != 0);
	CuAssert (cu, "details is null", details != NULL);
	
	/* Return "", null, and "booo" in that order */
	switch (st) {
	case 0:
		*password = gkr_secure_strdup ("");
		return TRUE;
	case 1:
		*password = NULL;
		return TRUE;
	case 2:
		/* Most of our test encrypted stuff use this password */
		g_print ("getting password 'booo' for: %s\n", details); 	
		*password = gkr_secure_strdup ("booo");
		return TRUE;
	default:
		msg = g_strdup_printf ("decryption didn't work for: %s", g_quark_to_string (loc));
		CuAssert (cu, msg, FALSE);
		return FALSE;
	};
}

static void
read_file (CuTest *cu, const gchar *filename, GQuark *location, guchar **contents, gsize *len)
{
	gchar *path;
	gboolean ret;
	
	the_cu = cu;
	
	path = g_build_filename (g_get_current_dir (), "test-data", filename, NULL);
	*location = gkr_location_from_path (path);
	CuAssert (cu, "location is empty", *location != 0);
	
	ret = g_file_get_contents (path, (gchar**)contents, len, NULL);
	CuAssert (cu, "couldn't read in file", ret);
	
	g_free (path);
}
	 

void unit_test_start_parser (CuTest *cu)
{
	parser = gkr_pkix_parser_new (FALSE);
	g_signal_connect (parser, "parsed-partial", G_CALLBACK (parsed_partial), NULL);
	g_signal_connect (parser, "parsed-sexp", G_CALLBACK (parsed_sexp), NULL);
	g_signal_connect (parser, "parsed-asn1", G_CALLBACK (parsed_asn1), NULL);
	g_signal_connect (parser, "ask-password", G_CALLBACK (ask_password), NULL);
}

void unit_test_pkix_parse_der_keys (CuTest* cu)
{
	guchar *contents;
	GkrPkixResult result;
	GQuark location;
	gsize len;
	
	the_cu = cu;

	/* First an RSA key */
	read_file (cu, "der-rsa-1024.key", &location, &contents, &len);
	
	last_sexp_parsed = NULL;
	result = gkr_pkix_parser_der_private_key (parser, location, contents, len);
	CuAssert (cu, "couldn't parse RSA key", result == GKR_PKIX_SUCCESS);
	CuAssert (cu, "parsed object is invalid", last_sexp_parsed != NULL);
	
	gkr_crypto_sexp_dump (last_sexp_parsed);

	/* Now a DSA key */	
	read_file (cu, "der-dsa-1024.key", &location, &contents, &len);
	
	last_sexp_parsed = NULL;
	result = gkr_pkix_parser_der_private_key (parser, location, contents, len);
	CuAssert (cu, "couldn't parse DSA key", result == GKR_PKIX_SUCCESS);
	CuAssert (cu, "parsed object is invalid", last_sexp_parsed != NULL);
	
	gkr_crypto_sexp_dump (last_sexp_parsed);
}

void unit_test_pkix_parse_der_pkcs8 (CuTest* cu)
{
	guchar *contents;
	GkrPkixResult result;
	GQuark location;
	gsize len;
	
	the_cu = cu;

	/* First an DSA key */
	read_file (cu, "der-pkcs8-dsa.key", &location, &contents, &len);
	
	last_sexp_parsed = NULL;
	result = gkr_pkix_parser_der_pkcs8_plain (parser, location, contents, len);
	CuAssert (cu, "couldn't parse PKCS8 key", result == GKR_PKIX_SUCCESS);
	CuAssert (cu, "parsed object is invalid", last_sexp_parsed != NULL);
	
	gkr_crypto_sexp_dump (last_sexp_parsed);
	
	/* Now an encrypted key */
	read_file (cu, "der-pkcs8-encrypted-pkcs5.key", &location, &contents, &len);
	
	last_sexp_parsed = NULL;
	result = gkr_pkix_parser_der_pkcs8_encrypted (parser, location, contents, len);
	CuAssert (cu, "couldn't parse PKCS8 key", result == GKR_PKIX_SUCCESS);
	CuAssert (cu, "parsed object is invalid", last_sexp_parsed != NULL);
	
	gkr_crypto_sexp_dump (last_sexp_parsed);	
}

void unit_test_pkix_parse_pem (CuTest *cu)
{
	guchar *contents;
	GkrPkixResult result;
	GQuark location;
	gsize len;
	
	the_cu = cu;

	/* First an RSA key */
	read_file (cu, "pem-dsa-1024.key", &location, &contents, &len);
	
	n_parsed = 0;
	result = gkr_pkix_parser_pem (parser, location, contents, len);
	CuAssert (cu, "couldn't parse PEM data", result == GKR_PKIX_SUCCESS);

	CuAssert (cu, "invalid number of items parsed", n_parsed == 1);
	CuAssert (cu, "invalid type of data parsed", last_sexp_parsed != NULL);
	
	gkr_crypto_sexp_dump (last_sexp_parsed);
}

void unit_test_pkix_parse_all (CuTest *cu)
{
	gchar *path, *filepath, *msg;
	guchar *contents;
	GError *err = NULL;
	gboolean result;
	const gchar *filename;
	GQuark location;
	gsize len;
	GDir *dir;
	
	the_cu = cu;
	path = g_build_filename (g_get_current_dir (), "test-data", NULL);
	
	dir = g_dir_open (path, 0, NULL);
	CuAssert (cu, "couldn't open directory", dir != NULL); 

	while (dir) {
		filename = g_dir_read_name (dir);
		if (!filename)
			break;
			
		filepath = g_build_filename (path, filename, NULL);
		if (!g_file_test (filepath, G_FILE_TEST_IS_REGULAR))
			continue;
		
		read_file (cu, filename, &location, &contents, &len);
		
		result = gkr_pkix_parser_parse (parser, location, contents, len, &err);
		if (!result) { 
			msg = g_strdup_printf ("couldn't parse file data: %s: %s", 
			                       filename, err && err->message ? err->message : "");
			g_error_free (err);
			err = NULL;
			CuAssert (cu, msg, FALSE);
		}
	}
}