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
|
/* -*- 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 "egg/egg-asn1-defs.h"
#include "egg/egg-asn1x.h"
#include "egg/egg-dn.h"
#include "egg/egg-oid.h"
#include "egg/egg-testing.h"
#include <glib.h>
#include <gcrypt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct {
GNode* asn1;
guchar *data;
gsize n_data;
} Test;
static void
setup (Test *test, gconstpointer unused)
{
EggBytes *bytes;
if (!g_file_get_contents (SRCDIR "/files/test-certificate-1.der",
(gchar**)&test->data, &test->n_data, NULL))
g_assert_not_reached ();
test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate");
g_assert (test->asn1 != NULL);
bytes = egg_bytes_new_static (test->data, test->n_data);
if (!egg_asn1x_decode (test->asn1, bytes))
g_assert_not_reached ();
egg_bytes_unref (bytes);
}
static void
teardown (Test *test, gconstpointer unused)
{
egg_asn1x_destroy (test->asn1);
g_free (test->data);
}
static void
test_read_dn (Test* test, gconstpointer unused)
{
gchar *dn;
dn = egg_dn_read (egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL));
g_assert (dn != NULL);
g_assert_cmpstr (dn, ==, "C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting, OU=Certification Services Division, CN=Thawte Personal Premium CA, EMAIL=personal-premium@thawte.com");
g_free (dn);
}
static void
test_dn_value (Test* test, gconstpointer unused)
{
const guchar value[] = { 0x13, 0x1a, 0x54, 0x68, 0x61, 0x77, 0x74, 0x65, 0x20, 0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x61, 0x6c, 0x20, 0x50, 0x72, 0x65, 0x6d, 0x69, 0x75, 0x6d, 0x20, 0x43, 0x41 };
gsize n_value = 28;
EggBytes *bytes;
GQuark oid;
gchar *text;
/* Some printable strings */
oid = g_quark_from_static_string ("2.5.4.3");
bytes = egg_bytes_new_static (value, n_value);
text = egg_dn_print_value (oid, bytes);
egg_bytes_unref (bytes);
g_assert_cmpstr (text, ==, "Thawte Personal Premium CA");
g_free (text);
/* Unknown oid */
oid = g_quark_from_static_string ("1.1.1.1.1.1");
bytes = egg_bytes_new_static (value, n_value);
text = egg_dn_print_value (oid, bytes);
egg_bytes_unref (bytes);
g_assert_cmpstr (text, ==, "#131A54686177746520506572736F6E616C205072656D69756D204341");
g_free (text);
}
static int last_index = 0;
static void
concatenate_dn (guint index,
GQuark oid,
EggBytes *value,
gpointer user_data)
{
GString *dn = user_data;
gchar *text;
g_assert (oid);
g_assert (value != NULL);
g_assert (egg_bytes_get_size (value) != 0);
g_assert (index == last_index);
++last_index;
if (index != 1) {
g_string_append (dn, ", ");
}
g_string_append (dn, egg_oid_get_name (oid));
g_string_append_c (dn, '=');
text = egg_dn_print_value (oid, value);
g_string_append (dn, text);
g_free (text);
}
static void
test_parse_dn (Test* test, gconstpointer unused)
{
GString *dn = g_string_new ("");
last_index = 1;
if (!egg_dn_parse (egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL), concatenate_dn, dn))
g_assert_not_reached ();
g_assert_cmpstr (dn->str, ==, "C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting, OU=Certification Services Division, CN=Thawte Personal Premium CA, EMAIL=personal-premium@thawte.com");
g_string_free (dn, TRUE);
}
static void
test_read_dn_part (Test* test, gconstpointer unused)
{
GNode *node;
gchar *value;
node = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL);
value = egg_dn_read_part (node, "CN");
g_assert (value != NULL);
g_assert_cmpstr (value, ==, "Thawte Personal Premium CA");
g_free (value);
value = egg_dn_read_part (node, "2.5.4.8");
g_assert (value != NULL);
g_assert_cmpstr (value, ==, "Western Cape");
g_free (value);
value = egg_dn_read_part (node, "DC");
g_assert (value == NULL);
value = egg_dn_read_part (node, "0.0.0.0");
g_assert (value == NULL);
value = egg_dn_read_part (node, "2.5.4.9");
g_assert (value == NULL);
}
static void
test_add_dn_part (Test *test,
gconstpointer unused)
{
EggBytes *check;
EggBytes *dn;
GNode *check_dn;
GNode *asn;
GNode *node;
asn = egg_asn1x_create (pkix_asn1_tab, "Name");
node = egg_asn1x_node (asn, "rdnSequence", NULL);
egg_asn1x_set_choice (asn, node);
egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.6"), "ZA");
egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.8"), "Western Cape");
egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.7"), "Cape Town");
egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.10"), "Thawte Consulting");
egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.11"), "Certification Services Division");
egg_dn_add_string_part (node, g_quark_from_static_string ("2.5.4.3"), "Thawte Personal Premium CA");
egg_dn_add_string_part (node, g_quark_from_static_string ("1.2.840.113549.1.9.1"), "personal-premium@thawte.com");
dn = egg_asn1x_encode (asn, NULL);
if (dn == NULL) {
g_warning ("couldn't encode dn: %s", egg_asn1x_message (asn));
g_assert_not_reached ();
}
check_dn = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL);
check = egg_asn1x_encode (check_dn, NULL);
egg_asn1x_destroy (asn);
egg_assert_cmpbytes (dn, ==, egg_bytes_get_data (check), egg_bytes_get_size (check));
egg_bytes_unref (dn);
egg_bytes_unref (check);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add ("/dn/read_dn", Test, NULL, setup, test_read_dn, teardown);
g_test_add ("/dn/dn_value", Test, NULL, setup, test_dn_value, teardown);
g_test_add ("/dn/parse_dn", Test, NULL, setup, test_parse_dn, teardown);
g_test_add ("/dn/read_dn_part", Test, NULL, setup, test_read_dn_part, teardown);
g_test_add ("/dn/add_dn_part", Test, NULL, setup, test_add_dn_part, teardown);
return g_test_run ();
}
|