summaryrefslogtreecommitdiffstats
path: root/lib/util/tests/asn1_tests.c
blob: f2bd163c428591f78b11718876a8c9bb0bb7bc88 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
   Unix SMB/CIFS implementation.

   util_asn1 testing

   Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
   Copyright (C) Volker Lendecke 2004
   Copyright (C) Andrew Bartlett 2011

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

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "torture/torture.h"
#include "torture/local/proto.h"
#include "../asn1.h"

struct oid_data {
	const char *oid;	/* String OID */
	const char *bin_oid;	/* Binary OID represented as string */
};

/* Data for successful OIDs conversions */
static const struct oid_data oid_data_ok[] = {
	{
		.oid = "2.5.4.0",
		.bin_oid = "550400"
	},
	{
		.oid = "2.5.4.1",
		.bin_oid = "550401"
	},
	{
		.oid = "2.5.4.130",
		.bin_oid = "55048102"
	},
	{
		.oid = "2.5.130.4",
		.bin_oid = "55810204"
	},
	{
		.oid = "2.5.4.16387",
		.bin_oid = "5504818003"
	},
	{
		.oid = "2.5.16387.4",
		.bin_oid = "5581800304"
	},
	{
		.oid = "2.5.2097155.4",
		.bin_oid = "558180800304"
	},
	{
		.oid = "2.5.4.130.16387.2097155.268435459",
		.bin_oid = "55048102818003818080038180808003"
	},
};

/* Data for successful OIDs conversions */
static const char *oid_data_err[] = {
		"",		/* empty OID */
		".2.5.4.130",	/* first sub-identifier is empty */
		"2.5.4.130.",	/* last sub-identifier is empty */
		"2..5.4.130",	/* second sub-identifier is empty */
		"2.5..4.130",	/* third sub-identifier is empty */
		"2.abc.4.130", 	/* invalid sub-identifier */
		"2.5abc.4.130", /* invalid sub-identifier (alphanumeric)*/
};

/* Data for successful Partial OIDs conversions */
static const struct oid_data partial_oid_data_ok[] = {
	{
		.oid = "2.5.4.130:0x81",
		.bin_oid = "5504810281"
	},
	{
		.oid = "2.5.4.16387:0x8180",
		.bin_oid = "55048180038180"
	},
	{
		.oid = "2.5.4.16387:0x81",
		.bin_oid = "550481800381"
	},
	{
		.oid = "2.5.2097155.4:0x818080",
		.bin_oid = "558180800304818080"
	},
	{
		.oid = "2.5.2097155.4:0x8180",
		.bin_oid = "5581808003048180"
	},
	{
		.oid = "2.5.2097155.4:0x81",
		.bin_oid = "55818080030481"
	},
};

static const struct {
	DATA_BLOB blob;
	int value;
} integer_tests[] = {
        {
		.blob = { discard_const_p(uint8_t, "\x02\x01\x00"), 3},
		.value = 0
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x01\x7f"), 3},
		.value = 127
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x02\x00\x80"), 4},
		.value = 128
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x02\x01\x00"), 4},
		.value = 256
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x01\x80"), 3},
		.value = -128
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x02\xff\x7f"), 4},
		.value = -129
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x01\xff"), 3},
		.value = -1
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x02\xff\x01"), 4},
		.value = -255
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x02\x00\xff"), 4},
		.value = 255
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x04\x80\x00\x00\x00"), 6},
		.value = 0x80000000
	},
	{
		.blob = { discard_const_p(uint8_t, "\x02\x04\x7f\xff\xff\xff"), 6},
		.value = 0x7fffffff
	}
};

/* Testing ber_write_OID_String() function */
static bool test_ber_write_OID_String(struct torture_context *tctx)
{
	int i;
	char *hex_str;
	DATA_BLOB blob;
	TALLOC_CTX *mem_ctx;
	const struct oid_data *data = oid_data_ok;

	mem_ctx = talloc_new(tctx);

	/* check for valid OIDs */
	for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
		torture_assert(tctx, ber_write_OID_String(mem_ctx, &blob, data[i].oid),
				"ber_write_OID_String failed");

		hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
		torture_assert(tctx, hex_str, "No memory!");

		torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
				talloc_asprintf(mem_ctx,
						"Failed: oid=%s, bin_oid:%s",
						data[i].oid, data[i].bin_oid));
	}

	/* check for invalid OIDs */
	for (i = 0; i < ARRAY_SIZE(oid_data_err); i++) {
		torture_assert(tctx,
			       !ber_write_OID_String(mem_ctx, &blob, oid_data_err[i]),
			       talloc_asprintf(mem_ctx,
					       "Should fail for [%s] -> %s",
					       oid_data_err[i],
					       hex_encode_talloc(mem_ctx, blob.data, blob.length)));
	}

	talloc_free(mem_ctx);

	return true;
}

/* Testing ber_read_OID_String() function */
static bool test_ber_read_OID_String(struct torture_context *tctx)
{
	int i;
	char *oid;
	DATA_BLOB oid_blob;
	TALLOC_CTX *mem_ctx;
	const struct oid_data *data = oid_data_ok;

	mem_ctx = talloc_new(tctx);

	for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
		oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);

		torture_assert(tctx, ber_read_OID_String(mem_ctx, oid_blob, &oid),
				"ber_read_OID_String failed");

		torture_assert(tctx, strequal(data[i].oid, oid),
				talloc_asprintf(mem_ctx,
						"Failed: oid=%s, bin_oid:%s",
						data[i].oid, data[i].bin_oid));
	}

	talloc_free(mem_ctx);

	return true;
}

/* Testing ber_write_partial_OID_String() function */
static bool test_ber_write_partial_OID_String(struct torture_context *tctx)
{
	int i;
	char *hex_str;
	DATA_BLOB blob;
	TALLOC_CTX *mem_ctx;
	const struct oid_data *data = oid_data_ok;

	mem_ctx = talloc_new(tctx);

	/* ber_write_partial_OID_String() should work with not partial OIDs also */
	for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
		torture_assert(tctx, ber_write_partial_OID_String(mem_ctx, &blob, data[i].oid),
				"ber_write_partial_OID_String failed");

		hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
		torture_assert(tctx, hex_str, "No memory!");

		torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
				talloc_asprintf(mem_ctx,
						"Failed: oid=%s, bin_oid:%s",
						data[i].oid, data[i].bin_oid));
	}

	/* ber_write_partial_OID_String() test with partial OIDs */
	data = partial_oid_data_ok;
	for (i = 0; i < ARRAY_SIZE(partial_oid_data_ok); i++) {
		torture_assert(tctx, ber_write_partial_OID_String(mem_ctx, &blob, data[i].oid),
				"ber_write_partial_OID_String failed");

		hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
		torture_assert(tctx, hex_str, "No memory!");

		torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
				talloc_asprintf(mem_ctx,
						"Failed: oid=%s, bin_oid:%s",
						data[i].oid, data[i].bin_oid));
	}

	talloc_free(mem_ctx);

	return true;
}

/* Testing ber_read_partial_OID_String() function */
static bool test_ber_read_partial_OID_String(struct torture_context *tctx)
{
	int i;
	char *oid;
	DATA_BLOB oid_blob;
	TALLOC_CTX *mem_ctx;
	const struct oid_data *data = oid_data_ok;

	mem_ctx = talloc_new(tctx);

	/* ber_read_partial_OID_String() should work with not partial OIDs also */
	for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
		oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);

		torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
				"ber_read_partial_OID_String failed");

		torture_assert(tctx, strequal(data[i].oid, oid),
				talloc_asprintf(mem_ctx,
						"Failed: oid=%s, bin_oid:%s",
						data[i].oid, data[i].bin_oid));
	}

	/* ber_read_partial_OID_String() test with partial OIDs */
	data = partial_oid_data_ok;
	for (i = 0; i < ARRAY_SIZE(partial_oid_data_ok); i++) {
		oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);

		torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
				"ber_read_partial_OID_String failed");

		torture_assert(tctx, strequal(data[i].oid, oid),
				talloc_asprintf(mem_ctx,
						"Failed: oid=%s, bin_oid:%s",
						data[i].oid, data[i].bin_oid));
	}

	talloc_free(mem_ctx);

	return true;
}

/*
 * Testing asn1_read_Integer and asn1_write_Integer functions,
 * inspired by Love Hornquist Astrand
 */

static bool test_asn1_Integer(struct torture_context *tctx)
{
	int i;
	TALLOC_CTX *mem_ctx;
	bool ret = false;

	mem_ctx = talloc_new(tctx);

	for (i = 0; i < ARRAY_SIZE(integer_tests); i++) {
		ASN1_DATA *data;
		DATA_BLOB blob;
		int val;

		data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH);
		if (!data) {
			goto err;
		}

		if (!asn1_write_Integer(data, integer_tests[i].value)) goto err;

		if (!asn1_blob(data, &blob)) {
			goto err;
		}

		torture_assert_data_blob_equal(tctx, blob, integer_tests[i].blob, "asn1_write_Integer gave incorrect result");

		if (!asn1_load(data, blob)) goto err;
		torture_assert(tctx, asn1_read_Integer(data, &val), "asn1_write_Integer output could not be read by asn1_read_Integer()");

		torture_assert_int_equal(tctx, val, integer_tests[i].value,
			"readback of asn1_write_Integer output by asn1_read_Integer() failed");
	}

	ret = true;

  err:

	talloc_free(mem_ctx);
	return ret;
}


/* LOCAL-ASN1 test suite creation */
struct torture_suite *torture_local_util_asn1(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx, "asn1");

	torture_suite_add_simple_test(suite, "ber_write_OID_String",
				      test_ber_write_OID_String);

	torture_suite_add_simple_test(suite, "ber_read_OID_String",
				      test_ber_read_OID_String);

	torture_suite_add_simple_test(suite, "ber_write_partial_OID_String",
				      test_ber_write_partial_OID_String);

	torture_suite_add_simple_test(suite, "ber_read_partial_OID_String",
				      test_ber_read_partial_OID_String);

	torture_suite_add_simple_test(suite, "asn1_Integer",
				      test_asn1_Integer);

	return suite;
}